Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 1 | //===- Schedule.cpp - Calculate an optimized schedule ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass the isl to calculate a schedule that is optimized for parallelism |
| 11 | // and tileablility. The algorithm used in isl is an optimized version of the |
| 12 | // algorithm described in following paper: |
| 13 | // |
| 14 | // U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan. |
| 15 | // A Practical Automatic Polyhedral Parallelizer and Locality Optimizer. |
| 16 | // In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language |
| 17 | // Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008. |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Tobias Grosser | 967239c | 2011-10-23 20:59:44 +0000 | [diff] [blame] | 20 | #include "polly/ScheduleOptimizer.h" |
| 21 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 22 | #include "polly/Cloog.h" |
| 23 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 67707b7 | 2011-10-23 20:59:40 +0000 | [diff] [blame] | 24 | #include "polly/CodeGeneration.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 25 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 26 | #include "polly/Dependences.h" |
| 27 | #include "polly/ScopInfo.h" |
| 28 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 29 | #include "isl/aff.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 30 | #include "isl/space.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 31 | #include "isl/map.h" |
| 32 | #include "isl/constraint.h" |
| 33 | #include "isl/schedule.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 34 | #include "isl/band.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 35 | |
Tobias Grosser | 4dca439 | 2011-11-22 19:40:19 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "polly-opt-isl" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 39 | |
Tobias Grosser | 768140c | 2011-12-14 08:58:39 +0000 | [diff] [blame] | 40 | static const int CONSTANT_BOUND = 20; |
| 41 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | using namespace polly; |
| 44 | |
Tobias Grosser | 967239c | 2011-10-23 20:59:44 +0000 | [diff] [blame] | 45 | namespace polly { |
| 46 | bool DisablePollyTiling; |
| 47 | } |
| 48 | static cl::opt<bool, true> |
Tobias Grosser | 353a268 | 2011-10-23 20:59:26 +0000 | [diff] [blame] | 49 | DisableTiling("polly-no-tiling", |
Tobias Grosser | 967239c | 2011-10-23 20:59:44 +0000 | [diff] [blame] | 50 | cl::desc("Disable tiling in the scheduler"), cl::Hidden, |
| 51 | cl::location(polly::DisablePollyTiling), cl::init(false)); |
Tobias Grosser | 353a268 | 2011-10-23 20:59:26 +0000 | [diff] [blame] | 52 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 53 | namespace { |
| 54 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 55 | class IslScheduleOptimizer : public ScopPass { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 56 | |
| 57 | public: |
| 58 | static char ID; |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 59 | explicit IslScheduleOptimizer() : ScopPass(ID) {} |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 60 | |
| 61 | virtual bool runOnScop(Scop &S); |
| 62 | void printScop(llvm::raw_ostream &OS) const; |
| 63 | void getAnalysisUsage(AnalysisUsage &AU) const; |
| 64 | }; |
| 65 | |
| 66 | } |
| 67 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 68 | char IslScheduleOptimizer::ID = 0; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 69 | |
| 70 | static int getSingleMap(__isl_take isl_map *map, void *user) { |
| 71 | isl_map **singleMap = (isl_map **) user; |
| 72 | *singleMap = map; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 77 | static void extendScattering(Scop &S, unsigned NewDimensions) { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 78 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 79 | ScopStmt *Stmt = *SI; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 80 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 81 | if (Stmt->isFinalRead()) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 82 | continue; |
| 83 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 84 | unsigned OldDimensions = Stmt->getNumScattering(); |
| 85 | isl_space *Space; |
| 86 | isl_basic_map *ChangeScattering; |
| 87 | |
| 88 | Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions); |
| 89 | ChangeScattering = isl_basic_map_universe(isl_space_copy(Space)); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 90 | isl_local_space *LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 91 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 92 | for (unsigned i = 0; i < OldDimensions; i++) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 93 | isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 94 | isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1); |
| 95 | isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1); |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 96 | ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 99 | for (unsigned i = OldDimensions; i < NewDimensions; i++) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 100 | isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 101 | isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 102 | ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 105 | isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 106 | |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 107 | ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap, |
| 108 | S.getParamSpace()); |
| 109 | isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(), |
| 110 | ChangeScatteringMap); |
| 111 | Stmt->setScattering(NewScattering); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 112 | isl_local_space_free(LocalSpace); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 116 | // getTileMap - Create a map that describes a n-dimensonal tiling. |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 117 | // |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 118 | // getTileMap creates a map from a n-dimensional scattering space into an |
| 119 | // 2*n-dimensional scattering space. The map describes a rectangular tiling. |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 120 | // |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 121 | // Example: |
| 122 | // scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32 |
| 123 | // |
| 124 | // tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]: |
| 125 | // t0 % 32 = 0 and t0 <= s0 < t0 + 32 and |
| 126 | // t1 % 32 = 0 and t1 <= s1 < t1 + 32} |
| 127 | // |
| 128 | // Before tiling: |
| 129 | // |
| 130 | // for (i = 0; i < N; i++) |
| 131 | // for (j = 0; j < M; j++) |
| 132 | // S(i,j) |
| 133 | // |
| 134 | // After tiling: |
| 135 | // |
| 136 | // for (t_i = 0; t_i < N; i+=32) |
| 137 | // for (t_j = 0; t_j < M; j+=32) |
| 138 | // for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0 |
| 139 | // for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0 |
| 140 | // S(i,j) |
| 141 | // |
| 142 | static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions, |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 143 | isl_space *SpaceModel, int tileSize = 32) { |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 144 | // We construct |
| 145 | // |
| 146 | // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]: |
| 147 | // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and |
| 148 | // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32} |
| 149 | // |
| 150 | // and project out the auxilary dimensions a0 and a1. |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 151 | isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions, |
| 152 | scheduleDimensions * 3); |
| 153 | isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space)); |
| 154 | |
| 155 | isl_local_space *LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 156 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 157 | for (int x = 0; x < scheduleDimensions; x++) { |
| 158 | int sX = x; |
| 159 | int tX = x; |
| 160 | int pX = scheduleDimensions + x; |
| 161 | int aX = 2 * scheduleDimensions + x; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 162 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 163 | isl_constraint *c; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 164 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 165 | // sX = aX * tileSize; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 166 | c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 167 | isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1); |
| 168 | isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize); |
| 169 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 170 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 171 | // pX = sX; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 172 | c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 173 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1); |
| 174 | isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1); |
| 175 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 176 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 177 | // tX <= pX |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 178 | c = isl_inequality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 179 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1); |
| 180 | isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1); |
| 181 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
| 182 | |
| 183 | // pX <= tX + (tileSize - 1) |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 184 | c = isl_inequality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 185 | isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1); |
| 186 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 187 | isl_constraint_set_constant_si(c, tileSize - 1); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 188 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 191 | // Project out auxilary dimensions. |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 192 | // |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 193 | // The auxilary dimensions are transformed into existentially quantified ones. |
| 194 | // This reduces the number of visible scattering dimensions and allows Cloog |
| 195 | // to produces better code. |
| 196 | tileMap = isl_basic_map_project_out(tileMap, isl_dim_out, |
| 197 | 2 * scheduleDimensions, |
| 198 | scheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 199 | isl_local_space_free(LocalSpace); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 200 | return tileMap; |
| 201 | } |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 202 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 203 | // getScheduleForBand - Get the schedule for this band. |
| 204 | // |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 205 | // Polly applies transformations like tiling on top of the isl calculated value. |
| 206 | // This can influence the number of scheduling dimension. The number of |
| 207 | // schedule dimensions is returned in the parameter 'Dimension'. |
| 208 | isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions) { |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 209 | isl_union_map *PartialSchedule; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 210 | isl_ctx *ctx; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 211 | isl_space *Space; |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 212 | isl_basic_map *TileMap; |
| 213 | isl_union_map *TileUMap; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 214 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 215 | PartialSchedule = isl_band_get_partial_schedule(Band); |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 216 | *Dimensions = isl_band_n_member(Band); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 217 | |
Tobias Grosser | 79b3020 | 2011-11-17 12:56:00 +0000 | [diff] [blame] | 218 | if (DisableTiling) |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 219 | return PartialSchedule; |
Tobias Grosser | 353a268 | 2011-10-23 20:59:26 +0000 | [diff] [blame] | 220 | |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 221 | // It does not make any sense to tile a band with just one dimension. |
| 222 | if (*Dimensions == 1) |
| 223 | return PartialSchedule; |
| 224 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 225 | ctx = isl_union_map_get_ctx(PartialSchedule); |
| 226 | Space = isl_union_map_get_space(PartialSchedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 227 | |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 228 | TileMap = getTileMap(ctx, *Dimensions, Space); |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 229 | TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap)); |
| 230 | TileUMap = isl_union_map_align_params(TileUMap, Space); |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 231 | *Dimensions = 2 * *Dimensions; |
| 232 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 233 | return isl_union_map_apply_range(PartialSchedule, TileUMap); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 236 | // Create a map that pre-vectorizes one scheduling dimension. |
| 237 | // |
| 238 | // getPrevectorMap creates a map that maps each input dimension to the same |
| 239 | // output dimension, except for the dimension DimToVectorize. DimToVectorize is |
| 240 | // strip mined by 'VectorWidth' and the newly created point loop of |
| 241 | // DimToVectorize is moved to the innermost level. |
| 242 | // |
| 243 | // Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4): |
| 244 | // |
| 245 | // | Before transformation |
| 246 | // | |
| 247 | // | A[i,j] -> [i,j] |
| 248 | // | |
| 249 | // | for (i = 0; i < 128; i++) |
| 250 | // | for (j = 0; j < 128; j++) |
| 251 | // | A(i,j); |
| 252 | // |
| 253 | // Prevector map: |
| 254 | // [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip |
| 255 | // |
| 256 | // | After transformation: |
| 257 | // | |
| 258 | // | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip |
| 259 | // | |
| 260 | // | for (it = 0; it < 128; it+=4) |
| 261 | // | for (j = 0; j < 128; j++) |
| 262 | // | for (ip = max(0,it); ip < min(128, it + 3); ip++) |
| 263 | // | A(ip,j); |
| 264 | // |
| 265 | // The goal of this transformation is to create a trivially vectorizable loop. |
| 266 | // This means a parallel loop at the innermost level that has a constant number |
| 267 | // of iterations corresponding to the target vector width. |
| 268 | // |
| 269 | // This transformation creates a loop at the innermost level. The loop has a |
| 270 | // constant number of iterations, if the number of loop iterations at |
| 271 | // DimToVectorize can be devided by VectorWidth. The default VectorWidth is |
| 272 | // currently constant and not yet target specific. This function does not reason |
| 273 | // about parallelism. |
| 274 | static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize, |
| 275 | int ScheduleDimensions, |
| 276 | int VectorWidth = 4) { |
| 277 | isl_space *Space; |
| 278 | isl_local_space *LocalSpace, *LocalSpaceRange; |
| 279 | isl_set *Modulo; |
| 280 | isl_map *TilingMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 281 | isl_constraint *c; |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 282 | isl_aff *Aff; |
| 283 | int PointDimension; /* ip */ |
| 284 | int TileDimension; /* it */ |
| 285 | isl_int VectorWidthMP; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 286 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 287 | assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 288 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 289 | Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1); |
| 290 | TilingMap = isl_map_universe(isl_space_copy(Space)); |
| 291 | LocalSpace = isl_local_space_from_space(Space); |
| 292 | PointDimension = ScheduleDimensions; |
| 293 | TileDimension = DimToVectorize; |
| 294 | |
| 295 | // Create an identity map for everything except DimToVectorize and map |
| 296 | // DimToVectorize to the point loop at the innermost dimension. |
| 297 | for (int i = 0; i < ScheduleDimensions; i++) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 298 | c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 299 | isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 300 | |
| 301 | if (i == DimToVectorize) |
| 302 | isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1); |
| 303 | else |
| 304 | isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); |
| 305 | |
| 306 | TilingMap = isl_map_add_constraint(TilingMap, c); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 309 | // it % 'VectorWidth' = 0 |
| 310 | LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace)); |
| 311 | Aff = isl_aff_zero_on_domain(LocalSpaceRange); |
| 312 | Aff = isl_aff_set_constant_si(Aff, VectorWidth); |
| 313 | Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1); |
| 314 | isl_int_init(VectorWidthMP); |
| 315 | isl_int_set_si(VectorWidthMP, VectorWidth); |
| 316 | Aff = isl_aff_mod(Aff, VectorWidthMP); |
| 317 | isl_int_clear(VectorWidthMP); |
| 318 | Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff)); |
| 319 | TilingMap = isl_map_intersect_range(TilingMap, Modulo); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 320 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 321 | // it <= ip |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 322 | c = isl_inequality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 323 | isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1); |
| 324 | isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1); |
| 325 | TilingMap = isl_map_add_constraint(TilingMap, c); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 326 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 327 | // ip <= it + ('VectorWidth' - 1) |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 328 | c = isl_inequality_alloc(LocalSpace); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 329 | isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1); |
| 330 | isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1); |
| 331 | isl_constraint_set_constant_si(c, VectorWidth - 1); |
| 332 | TilingMap = isl_map_add_constraint(TilingMap, c); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 333 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 334 | isl_map_dump(TilingMap); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 335 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 336 | return TilingMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 339 | // getScheduleForBandList - Get the scheduling map for a list of bands. |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 340 | // |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 341 | // We walk recursively the forest of bands to combine the schedules of the |
| 342 | // individual bands to the overall schedule. In case tiling is requested, |
| 343 | // the individual bands are tiled. |
| 344 | static isl_union_map *getScheduleForBandList(isl_band_list *BandList) { |
| 345 | int NumBands; |
| 346 | isl_union_map *Schedule; |
| 347 | isl_ctx *ctx; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 348 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 349 | ctx = isl_band_list_get_ctx(BandList); |
| 350 | NumBands = isl_band_list_n_band(BandList); |
Tobias Grosser | 6287201 | 2011-11-17 12:56:04 +0000 | [diff] [blame] | 351 | Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 352 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 353 | for (int i = 0; i < NumBands; i++) { |
| 354 | isl_band *Band; |
| 355 | isl_union_map *PartialSchedule; |
| 356 | int ScheduleDimensions; |
| 357 | isl_space *Space; |
Tobias Grosser | 44f19ac | 2011-07-05 22:15:53 +0000 | [diff] [blame] | 358 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 359 | Band = isl_band_list_get_band(BandList, i); |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 360 | PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions); |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 361 | Space = isl_union_map_get_space(PartialSchedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 362 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 363 | if (isl_band_has_children(Band)) { |
| 364 | isl_band_list *Children; |
| 365 | isl_union_map *SuffixSchedule; |
| 366 | |
| 367 | Children = isl_band_get_children(Band); |
| 368 | SuffixSchedule = getScheduleForBandList(Children); |
| 369 | PartialSchedule = isl_union_map_flat_range_product(PartialSchedule, |
| 370 | SuffixSchedule); |
| 371 | isl_band_list_free(Children); |
Tobias Grosser | 67707b7 | 2011-10-23 20:59:40 +0000 | [diff] [blame] | 372 | } else if (EnablePollyVector) { |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 373 | for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) { |
| 374 | if (isl_band_member_is_zero_distance(Band, i)) { |
| 375 | isl_map *TileMap; |
| 376 | isl_union_map *TileUMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 377 | |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 378 | TileMap = getPrevectorMap(ctx, i, ScheduleDimensions); |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 379 | TileUMap = isl_union_map_from_map(TileMap); |
| 380 | TileUMap = isl_union_map_align_params(TileUMap, |
| 381 | isl_space_copy(Space)); |
| 382 | PartialSchedule = isl_union_map_apply_range(PartialSchedule, |
| 383 | TileUMap); |
Tobias Grosser | 7c5ba83 | 2011-06-30 20:29:20 +0000 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | } |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Tobias Grosser | 6287201 | 2011-11-17 12:56:04 +0000 | [diff] [blame] | 389 | Schedule = isl_union_map_union(Schedule, PartialSchedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 390 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 391 | isl_band_free(Band); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 392 | isl_space_free(Space); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 395 | return Schedule; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 398 | static isl_union_map *getScheduleMap(isl_schedule *Schedule) { |
| 399 | isl_band_list *BandList = isl_schedule_get_band_forest(Schedule); |
| 400 | isl_union_map *ScheduleMap = getScheduleForBandList(BandList); |
| 401 | isl_band_list_free(BandList); |
| 402 | return ScheduleMap; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 405 | bool IslScheduleOptimizer::runOnScop(Scop &S) { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 406 | Dependences *D = &getAnalysis<Dependences>(); |
| 407 | |
| 408 | // Build input data. |
| 409 | int dependencyKinds = Dependences::TYPE_RAW |
| 410 | | Dependences::TYPE_WAR |
| 411 | | Dependences::TYPE_WAW; |
| 412 | |
| 413 | isl_union_map *validity = D->getDependences(dependencyKinds); |
| 414 | isl_union_map *proximity = D->getDependences(dependencyKinds); |
| 415 | isl_union_set *domain = NULL; |
| 416 | |
| 417 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) |
| 418 | if ((*SI)->isFinalRead()) |
| 419 | continue; |
| 420 | else if (!domain) |
| 421 | domain = isl_union_set_from_set((*SI)->getDomain()); |
| 422 | else |
| 423 | domain = isl_union_set_union(domain, |
| 424 | isl_union_set_from_set((*SI)->getDomain())); |
| 425 | |
| 426 | if (!domain) |
| 427 | return false; |
| 428 | |
| 429 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
| 430 | DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n"); |
| 431 | DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity); |
| 432 | dbgs() << ";\n"); |
| 433 | DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity); |
| 434 | dbgs() << ";\n"); |
| 435 | |
| 436 | isl_schedule *schedule; |
| 437 | |
Tobias Grosser | 768140c | 2011-12-14 08:58:39 +0000 | [diff] [blame] | 438 | isl_options_set_schedule_max_constant_term(S.getIslCtx(), CONSTANT_BOUND); |
Tobias Grosser | f4bea39 | 2011-12-14 08:58:43 +0000 | [diff] [blame^] | 439 | isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), 1); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 440 | schedule = isl_union_set_compute_schedule(domain, validity, proximity); |
| 441 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 442 | DEBUG(dbgs() << "Computed schedule: "); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 443 | DEBUG(dbgs() << stringFromIslObj(schedule)); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 444 | DEBUG(dbgs() << "Individual bands: "); |
| 445 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 446 | isl_union_map *ScheduleMap = getScheduleMap(schedule); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 447 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 448 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { |
| 449 | ScopStmt *stmt = *SI; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 450 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 451 | if (stmt->isFinalRead()) |
| 452 | continue; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 453 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 454 | isl_set *domain = stmt->getDomain(); |
| 455 | isl_union_map *stmtBand; |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 456 | stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap), |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 457 | isl_union_set_from_set(domain)); |
| 458 | isl_map *stmtSchedule; |
| 459 | isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule); |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 460 | stmt->setScattering(stmtSchedule); |
Tobias Grosser | 6e0fdca | 2011-08-23 12:31:14 +0000 | [diff] [blame] | 461 | isl_union_map_free(stmtBand); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 464 | isl_union_map_free(ScheduleMap); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 465 | isl_schedule_free(schedule); |
| 466 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 467 | unsigned maxScatDims = 0; |
| 468 | |
| 469 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 470 | maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 471 | |
| 472 | extendScattering(S, maxScatDims); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 476 | void IslScheduleOptimizer::printScop(raw_ostream &OS) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 479 | void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 480 | ScopPass::getAnalysisUsage(AU); |
| 481 | AU.addRequired<Dependences>(); |
| 482 | } |
| 483 | |
Tobias Grosser | 4dca439 | 2011-11-22 19:40:19 +0000 | [diff] [blame] | 484 | INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 485 | "Polly - Optimize schedule of SCoP", false, false) |
| 486 | INITIALIZE_PASS_DEPENDENCY(Dependences) |
| 487 | INITIALIZE_PASS_DEPENDENCY(ScopInfo) |
Tobias Grosser | 4dca439 | 2011-11-22 19:40:19 +0000 | [diff] [blame] | 488 | INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 489 | "Polly - Optimize schedule of SCoP", false, false) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 490 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 491 | Pass* polly::createIslScheduleOptimizerPass() { |
| 492 | return new IslScheduleOptimizer(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 493 | } |