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