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