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 | |
| 20 | #include "polly/Cloog.h" |
| 21 | #include "polly/LinkAllPasses.h" |
| 22 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 23 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 24 | #include "polly/Dependences.h" |
| 25 | #include "polly/ScopInfo.h" |
| 26 | |
| 27 | #include "isl/dim.h" |
| 28 | #include "isl/map.h" |
| 29 | #include "isl/constraint.h" |
| 30 | #include "isl/schedule.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 31 | #include "isl/band.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 32 | |
| 33 | #define DEBUG_TYPE "polly-optimize-isl" |
| 34 | #include "llvm/Support/Debug.h" |
| 35 | |
| 36 | using namespace llvm; |
| 37 | using namespace polly; |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | class ScheduleOptimizer : public ScopPass { |
| 42 | |
| 43 | public: |
| 44 | static char ID; |
| 45 | explicit ScheduleOptimizer() : ScopPass(ID) {} |
| 46 | |
| 47 | virtual bool runOnScop(Scop &S); |
| 48 | void printScop(llvm::raw_ostream &OS) const; |
| 49 | void getAnalysisUsage(AnalysisUsage &AU) const; |
| 50 | }; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | char ScheduleOptimizer::ID = 0; |
| 55 | |
| 56 | static int getSingleMap(__isl_take isl_map *map, void *user) { |
| 57 | isl_map **singleMap = (isl_map **) user; |
| 58 | *singleMap = map; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Tobias Grosser | 76747f7 | 2011-05-24 12:20:07 +0000 | [diff] [blame] | 63 | static void extendScattering(Scop &S, unsigned scatDimensions) { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 64 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { |
| 65 | ScopStmt *stmt = *SI; |
| 66 | |
| 67 | if (stmt->isFinalRead()) |
| 68 | continue; |
| 69 | |
| 70 | isl_map *scattering = stmt->getScattering(); |
| 71 | isl_dim *dim = isl_dim_alloc(isl_map_get_ctx(scattering), |
| 72 | isl_map_n_param(scattering), |
| 73 | isl_map_n_out(scattering), |
| 74 | scatDimensions); |
| 75 | isl_basic_map *changeScattering = isl_basic_map_universe(isl_dim_copy(dim)); |
| 76 | |
| 77 | for (unsigned i = 0; i < isl_map_n_out(scattering); i++) { |
| 78 | isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim)); |
| 79 | isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1); |
| 80 | isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1); |
| 81 | changeScattering = isl_basic_map_add_constraint(changeScattering, c); |
| 82 | } |
| 83 | |
| 84 | for (unsigned i = isl_map_n_out(scattering); i < scatDimensions; i++) { |
| 85 | isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim)); |
| 86 | isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); |
| 87 | changeScattering = isl_basic_map_add_constraint(changeScattering, c); |
| 88 | } |
| 89 | |
| 90 | isl_map *changeScatteringMap = isl_map_from_basic_map(changeScattering); |
| 91 | |
| 92 | stmt->setScattering(isl_map_apply_range(scattering, changeScatteringMap)); |
| 93 | } |
| 94 | } |
| 95 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 96 | // getTileMap - Create a map that describes a n-dimensonal tiling. |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 97 | // |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 98 | // getTileMap creates a map from a n-dimensional scattering space into an |
| 99 | // 2*n-dimensional scattering space. The map describes a rectangular tiling. |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 100 | // |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 101 | // Example: |
| 102 | // scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32 |
| 103 | // |
| 104 | // tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]: |
| 105 | // t0 % 32 = 0 and t0 <= s0 < t0 + 32 and |
| 106 | // t1 % 32 = 0 and t1 <= s1 < t1 + 32} |
| 107 | // |
| 108 | // Before tiling: |
| 109 | // |
| 110 | // for (i = 0; i < N; i++) |
| 111 | // for (j = 0; j < M; j++) |
| 112 | // S(i,j) |
| 113 | // |
| 114 | // After tiling: |
| 115 | // |
| 116 | // for (t_i = 0; t_i < N; i+=32) |
| 117 | // for (t_j = 0; t_j < M; j+=32) |
| 118 | // for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0 |
| 119 | // for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0 |
| 120 | // S(i,j) |
| 121 | // |
| 122 | static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions, |
| 123 | int parameterDimensions, int tileSize = 32) { |
| 124 | // We construct |
| 125 | // |
| 126 | // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]: |
| 127 | // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and |
| 128 | // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32} |
| 129 | // |
| 130 | // and project out the auxilary dimensions a0 and a1. |
| 131 | isl_dim *dim = isl_dim_alloc(ctx, parameterDimensions, scheduleDimensions, |
| 132 | scheduleDimensions * 3); |
| 133 | isl_basic_map *tileMap = isl_basic_map_universe(isl_dim_copy(dim)); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 134 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 135 | for (int x = 0; x < scheduleDimensions; x++) { |
| 136 | int sX = x; |
| 137 | int tX = x; |
| 138 | int pX = scheduleDimensions + x; |
| 139 | int aX = 2 * scheduleDimensions + x; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 140 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 141 | isl_constraint *c; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 142 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 143 | // sX = aX * tileSize; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 144 | c = isl_equality_alloc(isl_dim_copy(dim)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 145 | isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1); |
| 146 | isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize); |
| 147 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 148 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 149 | // pX = sX; |
| 150 | c = isl_equality_alloc(isl_dim_copy(dim)); |
| 151 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1); |
| 152 | isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1); |
| 153 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
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 | // tX <= pX |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 156 | c = isl_inequality_alloc(isl_dim_copy(dim)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 157 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1); |
| 158 | isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1); |
| 159 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
| 160 | |
| 161 | // pX <= tX + (tileSize - 1) |
| 162 | c = isl_inequality_alloc(isl_dim_copy(dim)); |
| 163 | isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1); |
| 164 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 165 | isl_constraint_set_constant_si(c, tileSize - 1); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 166 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 169 | // Project out auxilary dimensions. |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 170 | // |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 171 | // The auxilary dimensions are transformed into existentially quantified ones. |
| 172 | // This reduces the number of visible scattering dimensions and allows Cloog |
| 173 | // to produces better code. |
| 174 | tileMap = isl_basic_map_project_out(tileMap, isl_dim_out, |
| 175 | 2 * scheduleDimensions, |
| 176 | scheduleDimensions); |
| 177 | isl_dim_free(dim); |
| 178 | return tileMap; |
| 179 | } |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 180 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 181 | isl_union_map *getTiledPartialSchedule(isl_band *band) { |
| 182 | isl_union_map *partialSchedule; |
| 183 | int scheduleDimensions, parameterDimensions; |
| 184 | isl_ctx *ctx; |
| 185 | isl_dim *dim; |
| 186 | isl_basic_map *tileMap; |
| 187 | isl_union_map *tileUnionMap; |
| 188 | |
| 189 | partialSchedule = isl_band_get_partial_schedule(band); |
| 190 | ctx = isl_union_map_get_ctx(partialSchedule); |
| 191 | dim = isl_union_map_get_dim(partialSchedule); |
| 192 | scheduleDimensions = isl_band_n_member(band); |
| 193 | parameterDimensions = isl_dim_size(dim, isl_dim_param); |
| 194 | |
| 195 | tileMap = getTileMap(ctx, scheduleDimensions, parameterDimensions); |
| 196 | tileUnionMap = isl_union_map_from_map(isl_map_from_basic_map(tileMap)); |
| 197 | |
| 198 | partialSchedule = isl_union_map_apply_range(partialSchedule, tileUnionMap); |
| 199 | |
| 200 | isl_dim_free(dim); |
| 201 | isl_ctx_free(ctx); |
| 202 | |
| 203 | return partialSchedule; |
| 204 | } |
| 205 | |
| 206 | // tileBandList - Tile all bands contained in a band forest. |
| 207 | // |
| 208 | // Recursively walk the band forest and tile all bands in the forest. Return |
| 209 | // a schedule that describes the tiled scattering. |
| 210 | static isl_union_map *tileBandList(isl_band_list *blist) { |
| 211 | int numBands = isl_band_list_n_band(blist); |
| 212 | |
| 213 | isl_union_map *finalSchedule = 0; |
| 214 | |
| 215 | for (int i = 0; i < numBands; i++) { |
| 216 | isl_band *band; |
| 217 | isl_union_map *partialSchedule; |
| 218 | band = isl_band_list_get_band(blist, i); |
| 219 | partialSchedule = getTiledPartialSchedule(band); |
| 220 | |
| 221 | if (isl_band_has_children(band)) { |
| 222 | isl_band_list *children = isl_band_get_children(band); |
| 223 | isl_union_map *suffixSchedule = tileBandList(children); |
| 224 | partialSchedule = isl_union_map_flat_range_product(partialSchedule, |
| 225 | suffixSchedule); |
| 226 | } |
| 227 | |
| 228 | if (finalSchedule) |
| 229 | isl_union_map_union(finalSchedule, partialSchedule); |
| 230 | else |
| 231 | finalSchedule = partialSchedule; |
| 232 | |
| 233 | isl_band_free(band); |
| 234 | } |
| 235 | |
| 236 | return finalSchedule; |
| 237 | } |
| 238 | |
| 239 | static isl_union_map *tileSchedule(isl_schedule *schedule) { |
| 240 | isl_band_list *blist = isl_schedule_get_band_forest(schedule); |
| 241 | isl_union_map *tiledSchedule = tileBandList(blist); |
| 242 | isl_band_list_free(blist); |
| 243 | return tiledSchedule; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | bool ScheduleOptimizer::runOnScop(Scop &S) { |
| 247 | Dependences *D = &getAnalysis<Dependences>(); |
| 248 | |
| 249 | // Build input data. |
| 250 | int dependencyKinds = Dependences::TYPE_RAW |
| 251 | | Dependences::TYPE_WAR |
| 252 | | Dependences::TYPE_WAW; |
| 253 | |
| 254 | isl_union_map *validity = D->getDependences(dependencyKinds); |
| 255 | isl_union_map *proximity = D->getDependences(dependencyKinds); |
| 256 | isl_union_set *domain = NULL; |
| 257 | |
| 258 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) |
| 259 | if ((*SI)->isFinalRead()) |
| 260 | continue; |
| 261 | else if (!domain) |
| 262 | domain = isl_union_set_from_set((*SI)->getDomain()); |
| 263 | else |
| 264 | domain = isl_union_set_union(domain, |
| 265 | isl_union_set_from_set((*SI)->getDomain())); |
| 266 | |
| 267 | if (!domain) |
| 268 | return false; |
| 269 | |
| 270 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
| 271 | DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n"); |
| 272 | DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity); |
| 273 | dbgs() << ";\n"); |
| 274 | DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity); |
| 275 | dbgs() << ";\n"); |
| 276 | |
| 277 | isl_schedule *schedule; |
| 278 | |
| 279 | schedule = isl_union_set_compute_schedule(domain, validity, proximity); |
| 280 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 281 | DEBUG(dbgs() << "Computed schedule: "); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 282 | DEBUG(dbgs() << stringFromIslObj(schedule)); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 283 | DEBUG(dbgs() << "Individual bands: "); |
| 284 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 285 | isl_union_map *tiledSchedule = tileSchedule(schedule); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 286 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 287 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { |
| 288 | ScopStmt *stmt = *SI; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 289 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 290 | if (stmt->isFinalRead()) |
| 291 | continue; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 292 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 293 | isl_set *domain = stmt->getDomain(); |
| 294 | isl_union_map *stmtBand; |
| 295 | stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(tiledSchedule), |
| 296 | isl_union_set_from_set(domain)); |
| 297 | isl_map *stmtSchedule; |
| 298 | isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule); |
| 299 | stmt->setScattering(stmtSchedule); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame^] | 302 | isl_union_map_free(tiledSchedule); |
| 303 | isl_schedule_free(schedule); |
| 304 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 305 | unsigned maxScatDims = 0; |
| 306 | |
| 307 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) |
| 308 | maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims); |
| 309 | |
| 310 | extendScattering(S, maxScatDims); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
| 314 | void ScheduleOptimizer::printScop(raw_ostream &OS) const { |
| 315 | } |
| 316 | |
| 317 | void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
| 318 | ScopPass::getAnalysisUsage(AU); |
| 319 | AU.addRequired<Dependences>(); |
| 320 | } |
| 321 | |
| 322 | static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl", |
| 323 | "Polly - Calculate optimized " |
| 324 | "schedules using the isl schedule " |
| 325 | "calculator"); |
| 326 | |
| 327 | Pass* polly::createScheduleOptimizerPass() { |
| 328 | return new ScheduleOptimizer(); |
| 329 | } |