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