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 | |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 22 | #include "polly/CodeGen/CodeGeneration.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 23 | #include "polly/Dependences.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 24 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 25 | #include "polly/ScopInfo.h" |
| 26 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 27 | #include "isl/aff.h" |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 28 | #include "isl/band.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 29 | #include "isl/constraint.h" |
| 30 | #include "isl/map.h" |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 31 | #include "isl/options.h" |
Tobias Grosser | 8ad6bc3 | 2012-01-31 13:26:29 +0000 | [diff] [blame] | 32 | #include "isl/schedule.h" |
| 33 | #include "isl/space.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 | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 42 | namespace polly { bool DisablePollyTiling; } |
| 43 | static cl::opt<bool, true> DisableTiling( |
| 44 | "polly-no-tiling", cl::desc("Disable tiling in the scheduler"), cl::Hidden, |
| 45 | cl::location(polly::DisablePollyTiling), cl::init(false)); |
Tobias Grosser | 353a268 | 2011-10-23 20:59:26 +0000 | [diff] [blame] | 46 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 47 | static cl::opt<std::string> |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 48 | OptimizeDeps("polly-opt-optimize-only", |
| 49 | cl::desc("Only a certain kind of dependences (all/raw)"), |
| 50 | cl::Hidden, cl::init("all")); |
| 51 | |
| 52 | static cl::opt<std::string> |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 53 | SimplifyDeps("polly-opt-simplify-deps", |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 54 | cl::desc("Dependences should be simplified (yes/no)"), cl::Hidden, |
| 55 | cl::init("yes")); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 56 | |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 57 | static cl::opt<int> |
| 58 | MaxConstantTerm("polly-opt-max-constant-term", |
| 59 | cl::desc("The maximal constant term allowed (-1 is unlimited)"), |
| 60 | cl::Hidden, cl::init(20)); |
| 61 | |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 62 | static cl::opt<int> |
| 63 | MaxCoefficient("polly-opt-max-coefficient", |
| 64 | cl::desc("The maximal coefficient allowed (-1 is unlimited)"), |
| 65 | cl::Hidden, cl::init(20)); |
| 66 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 67 | static cl::opt<std::string> FusionStrategy( |
| 68 | "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"), |
| 69 | cl::Hidden, cl::init("min")); |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 70 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 71 | static cl::opt<std::string> MaximizeBandDepth( |
| 72 | "polly-opt-maximize-bands", cl::desc("Maximize the band depth (yes/no)"), |
| 73 | cl::Hidden, cl::init("yes")); |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 74 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 75 | namespace { |
| 76 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 77 | class IslScheduleOptimizer : public ScopPass { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 78 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 79 | public: |
| 80 | static char ID; |
| 81 | explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = NULL; } |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 82 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 83 | ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); } |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 84 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 85 | virtual bool runOnScop(Scop &S); |
| 86 | void printScop(llvm::raw_ostream &OS) const; |
| 87 | void getAnalysisUsage(AnalysisUsage &AU) const; |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 88 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 89 | private: |
| 90 | isl_schedule *LastSchedule; |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 91 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 92 | static void extendScattering(Scop &S, unsigned NewDimensions); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 93 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 94 | /// @brief Create a map that describes a n-dimensonal tiling. |
| 95 | /// |
| 96 | /// getTileMap creates a map from a n-dimensional scattering space into an |
| 97 | /// 2*n-dimensional scattering space. The map describes a rectangular |
| 98 | /// tiling. |
| 99 | /// |
| 100 | /// Example: |
| 101 | /// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32 |
| 102 | /// |
| 103 | /// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]: |
| 104 | /// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and |
| 105 | /// t1 % 32 = 0 and t1 <= s1 < t1 + 32} |
| 106 | /// |
| 107 | /// Before tiling: |
| 108 | /// |
| 109 | /// for (i = 0; i < N; i++) |
| 110 | /// for (j = 0; j < M; j++) |
| 111 | /// S(i,j) |
| 112 | /// |
| 113 | /// After tiling: |
| 114 | /// |
| 115 | /// for (t_i = 0; t_i < N; i+=32) |
| 116 | /// for (t_j = 0; t_j < M; j+=32) |
| 117 | /// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0 |
| 118 | /// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0 |
| 119 | /// S(i,j) |
| 120 | /// |
| 121 | static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions, |
| 122 | isl_space *SpaceModel, int tileSize = 32); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 123 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 124 | /// @brief Get the schedule for this band. |
| 125 | /// |
| 126 | /// Polly applies transformations like tiling on top of the isl calculated |
| 127 | /// value. This can influence the number of scheduling dimension. The |
| 128 | /// number of schedule dimensions is returned in the parameter 'Dimension'. |
| 129 | static isl_union_map *getScheduleForBand(isl_band *Band, int *Dimensions); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 130 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 131 | /// @brief Create a map that pre-vectorizes one scheduling dimension. |
| 132 | /// |
| 133 | /// getPrevectorMap creates a map that maps each input dimension to the same |
| 134 | /// output dimension, except for the dimension DimToVectorize. |
| 135 | /// DimToVectorize is strip mined by 'VectorWidth' and the newly created |
| 136 | /// point loop of DimToVectorize is moved to the innermost level. |
| 137 | /// |
| 138 | /// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4): |
| 139 | /// |
| 140 | /// | Before transformation |
| 141 | /// | |
| 142 | /// | A[i,j] -> [i,j] |
| 143 | /// | |
| 144 | /// | for (i = 0; i < 128; i++) |
| 145 | /// | for (j = 0; j < 128; j++) |
| 146 | /// | A(i,j); |
| 147 | /// |
| 148 | /// Prevector map: |
| 149 | /// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip |
| 150 | /// |
| 151 | /// | After transformation: |
| 152 | /// | |
| 153 | /// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip |
| 154 | /// | |
| 155 | /// | for (it = 0; it < 128; it+=4) |
| 156 | /// | for (j = 0; j < 128; j++) |
| 157 | /// | for (ip = max(0,it); ip < min(128, it + 3); ip++) |
| 158 | /// | A(ip,j); |
| 159 | /// |
| 160 | /// The goal of this transformation is to create a trivially vectorizable |
| 161 | /// loop. This means a parallel loop at the innermost level that has a |
| 162 | /// constant number of iterations corresponding to the target vector width. |
| 163 | /// |
| 164 | /// This transformation creates a loop at the innermost level. The loop has |
| 165 | /// a constant number of iterations, if the number of loop iterations at |
| 166 | /// DimToVectorize can be divided by VectorWidth. The default VectorWidth is |
| 167 | /// currently constant and not yet target specific. This function does not |
| 168 | /// reason about parallelism. |
| 169 | static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize, |
| 170 | int ScheduleDimensions, int VectorWidth = 4); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 171 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 172 | /// @brief Get the scheduling map for a list of bands. |
| 173 | /// |
| 174 | /// Walk recursively the forest of bands to combine the schedules of the |
| 175 | /// individual bands to the overall schedule. In case tiling is requested, |
| 176 | /// the individual bands are tiled. |
| 177 | static isl_union_map *getScheduleForBandList(isl_band_list *BandList); |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 178 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 179 | static isl_union_map *getScheduleMap(isl_schedule *Schedule); |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 180 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 181 | bool doFinalization() { |
| 182 | isl_schedule_free(LastSchedule); |
| 183 | LastSchedule = NULL; |
| 184 | return true; |
| 185 | } |
| 186 | }; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 187 | |
| 188 | } |
| 189 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 190 | char IslScheduleOptimizer::ID = 0; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 191 | |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 192 | void IslScheduleOptimizer::extendScattering(Scop &S, unsigned NewDimensions) { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 193 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 194 | ScopStmt *Stmt = *SI; |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 195 | unsigned OldDimensions = Stmt->getNumScattering(); |
| 196 | isl_space *Space; |
Tobias Grosser | 2966611 | 2012-05-22 10:47:31 +0000 | [diff] [blame] | 197 | isl_map *Map, *New; |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 198 | |
| 199 | Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions); |
Tobias Grosser | 2966611 | 2012-05-22 10:47:31 +0000 | [diff] [blame] | 200 | Map = isl_map_universe(Space); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 201 | |
Tobias Grosser | 2966611 | 2012-05-22 10:47:31 +0000 | [diff] [blame] | 202 | for (unsigned i = 0; i < OldDimensions; i++) |
| 203 | Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 204 | |
Tobias Grosser | 2966611 | 2012-05-22 10:47:31 +0000 | [diff] [blame] | 205 | for (unsigned i = OldDimensions; i < NewDimensions; i++) |
| 206 | Map = isl_map_fix_si(Map, isl_dim_out, i, 0); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 207 | |
Tobias Grosser | 2966611 | 2012-05-22 10:47:31 +0000 | [diff] [blame] | 208 | Map = isl_map_align_params(Map, S.getParamSpace()); |
| 209 | New = isl_map_apply_range(Stmt->getScattering(), Map); |
| 210 | Stmt->setScattering(New); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 214 | isl_basic_map *IslScheduleOptimizer::getTileMap( |
| 215 | isl_ctx *ctx, int scheduleDimensions, isl_space *SpaceModel, int tileSize) { |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 216 | // We construct |
| 217 | // |
| 218 | // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]: |
| 219 | // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and |
| 220 | // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32} |
| 221 | // |
| 222 | // and project out the auxilary dimensions a0 and a1. |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 223 | isl_space *Space = |
| 224 | isl_space_alloc(ctx, 0, scheduleDimensions, scheduleDimensions * 3); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 225 | isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space)); |
| 226 | |
| 227 | isl_local_space *LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 228 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 229 | for (int x = 0; x < scheduleDimensions; x++) { |
| 230 | int sX = x; |
| 231 | int tX = x; |
| 232 | int pX = scheduleDimensions + x; |
| 233 | int aX = 2 * scheduleDimensions + x; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 234 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 235 | isl_constraint *c; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 236 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 237 | // sX = aX * tileSize; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 238 | c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 239 | isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1); |
| 240 | isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize); |
| 241 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 242 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 243 | // pX = sX; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 244 | c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 245 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1); |
| 246 | isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1); |
| 247 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 248 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 249 | // tX <= pX |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 250 | c = isl_inequality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 251 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1); |
| 252 | isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1); |
| 253 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
| 254 | |
| 255 | // pX <= tX + (tileSize - 1) |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 256 | c = isl_inequality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 257 | isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1); |
| 258 | isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 259 | isl_constraint_set_constant_si(c, tileSize - 1); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 260 | tileMap = isl_basic_map_add_constraint(tileMap, c); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 263 | // Project out auxilary dimensions. |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 264 | // |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 265 | // The auxilary dimensions are transformed into existentially quantified ones. |
| 266 | // This reduces the number of visible scattering dimensions and allows Cloog |
| 267 | // to produces better code. |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 268 | tileMap = isl_basic_map_project_out( |
| 269 | tileMap, isl_dim_out, 2 * scheduleDimensions, scheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 270 | isl_local_space_free(LocalSpace); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 271 | return tileMap; |
| 272 | } |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 273 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 274 | isl_union_map * |
| 275 | IslScheduleOptimizer::getScheduleForBand(isl_band *Band, int *Dimensions) { |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 276 | isl_union_map *PartialSchedule; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 277 | isl_ctx *ctx; |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 278 | isl_space *Space; |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 279 | isl_basic_map *TileMap; |
| 280 | isl_union_map *TileUMap; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 281 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 282 | PartialSchedule = isl_band_get_partial_schedule(Band); |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 283 | *Dimensions = isl_band_n_member(Band); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 284 | |
Tobias Grosser | 79b3020 | 2011-11-17 12:56:00 +0000 | [diff] [blame] | 285 | if (DisableTiling) |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 286 | return PartialSchedule; |
Tobias Grosser | 353a268 | 2011-10-23 20:59:26 +0000 | [diff] [blame] | 287 | |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 288 | // It does not make any sense to tile a band with just one dimension. |
| 289 | if (*Dimensions == 1) |
| 290 | return PartialSchedule; |
| 291 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 292 | ctx = isl_union_map_get_ctx(PartialSchedule); |
| 293 | Space = isl_union_map_get_space(PartialSchedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 294 | |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 295 | TileMap = getTileMap(ctx, *Dimensions, Space); |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 296 | TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap)); |
| 297 | TileUMap = isl_union_map_align_params(TileUMap, Space); |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 298 | *Dimensions = 2 * *Dimensions; |
| 299 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 300 | return isl_union_map_apply_range(PartialSchedule, TileUMap); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 303 | isl_map *IslScheduleOptimizer::getPrevectorMap( |
| 304 | isl_ctx *ctx, int DimToVectorize, int ScheduleDimensions, int VectorWidth) { |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 305 | isl_space *Space; |
| 306 | isl_local_space *LocalSpace, *LocalSpaceRange; |
| 307 | isl_set *Modulo; |
| 308 | isl_map *TilingMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 309 | isl_constraint *c; |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 310 | isl_aff *Aff; |
| 311 | int PointDimension; /* ip */ |
| 312 | int TileDimension; /* it */ |
| 313 | isl_int VectorWidthMP; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 314 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 315 | assert(0 <= DimToVectorize && DimToVectorize < ScheduleDimensions); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 316 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 317 | Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1); |
| 318 | TilingMap = isl_map_universe(isl_space_copy(Space)); |
| 319 | LocalSpace = isl_local_space_from_space(Space); |
| 320 | PointDimension = ScheduleDimensions; |
| 321 | TileDimension = DimToVectorize; |
| 322 | |
| 323 | // Create an identity map for everything except DimToVectorize and map |
| 324 | // DimToVectorize to the point loop at the innermost dimension. |
| 325 | for (int i = 0; i < ScheduleDimensions; i++) { |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 326 | c = isl_equality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 327 | isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 328 | |
| 329 | if (i == DimToVectorize) |
| 330 | isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1); |
| 331 | else |
| 332 | isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1); |
| 333 | |
| 334 | TilingMap = isl_map_add_constraint(TilingMap, c); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 337 | // it % 'VectorWidth' = 0 |
| 338 | LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace)); |
| 339 | Aff = isl_aff_zero_on_domain(LocalSpaceRange); |
| 340 | Aff = isl_aff_set_constant_si(Aff, VectorWidth); |
| 341 | Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1); |
| 342 | isl_int_init(VectorWidthMP); |
| 343 | isl_int_set_si(VectorWidthMP, VectorWidth); |
| 344 | Aff = isl_aff_mod(Aff, VectorWidthMP); |
| 345 | isl_int_clear(VectorWidthMP); |
| 346 | Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff)); |
| 347 | TilingMap = isl_map_intersect_range(TilingMap, Modulo); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 348 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 349 | // it <= ip |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 350 | c = isl_inequality_alloc(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 351 | isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1); |
| 352 | isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1); |
| 353 | TilingMap = isl_map_add_constraint(TilingMap, c); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 354 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 355 | // ip <= it + ('VectorWidth' - 1) |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 356 | c = isl_inequality_alloc(LocalSpace); |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 357 | isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1); |
| 358 | isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1); |
| 359 | isl_constraint_set_constant_si(c, VectorWidth - 1); |
| 360 | TilingMap = isl_map_add_constraint(TilingMap, c); |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 361 | |
Tobias Grosser | 2493e92 | 2011-12-07 07:42:57 +0000 | [diff] [blame] | 362 | return TilingMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 365 | isl_union_map * |
| 366 | IslScheduleOptimizer::getScheduleForBandList(isl_band_list *BandList) { |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 367 | int NumBands; |
| 368 | isl_union_map *Schedule; |
| 369 | isl_ctx *ctx; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 370 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 371 | ctx = isl_band_list_get_ctx(BandList); |
| 372 | NumBands = isl_band_list_n_band(BandList); |
Tobias Grosser | 6287201 | 2011-11-17 12:56:04 +0000 | [diff] [blame] | 373 | Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0)); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 374 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 375 | for (int i = 0; i < NumBands; i++) { |
| 376 | isl_band *Band; |
| 377 | isl_union_map *PartialSchedule; |
| 378 | int ScheduleDimensions; |
| 379 | isl_space *Space; |
Tobias Grosser | 44f19ac | 2011-07-05 22:15:53 +0000 | [diff] [blame] | 380 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 381 | Band = isl_band_list_get_band(BandList, i); |
Tobias Grosser | b603339 | 2011-12-08 13:02:58 +0000 | [diff] [blame] | 382 | PartialSchedule = getScheduleForBand(Band, &ScheduleDimensions); |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 383 | Space = isl_union_map_get_space(PartialSchedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 384 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 385 | if (isl_band_has_children(Band)) { |
| 386 | isl_band_list *Children; |
| 387 | isl_union_map *SuffixSchedule; |
| 388 | |
| 389 | Children = isl_band_get_children(Band); |
| 390 | SuffixSchedule = getScheduleForBandList(Children); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 391 | PartialSchedule = |
| 392 | isl_union_map_flat_range_product(PartialSchedule, SuffixSchedule); |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 393 | isl_band_list_free(Children); |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 394 | } else if (PollyVectorizerChoice != VECTORIZER_NONE) { |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 395 | for (int j = 0; j < isl_band_n_member(Band); j++) { |
| 396 | if (isl_band_member_is_zero_distance(Band, j)) { |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 397 | isl_map *TileMap; |
| 398 | isl_union_map *TileUMap; |
Tobias Grosser | c6699b7 | 2011-06-30 20:29:13 +0000 | [diff] [blame] | 399 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 400 | TileMap = getPrevectorMap(ctx, ScheduleDimensions - j - 1, |
Tobias Grosser | 216ea58 | 2012-04-16 11:06:06 +0000 | [diff] [blame] | 401 | ScheduleDimensions); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 402 | TileUMap = isl_union_map_from_map(TileMap); |
| 403 | TileUMap = |
| 404 | isl_union_map_align_params(TileUMap, isl_space_copy(Space)); |
| 405 | PartialSchedule = |
| 406 | isl_union_map_apply_range(PartialSchedule, TileUMap); |
| 407 | break; |
| 408 | } |
Tobias Grosser | 7c5ba83 | 2011-06-30 20:29:20 +0000 | [diff] [blame] | 409 | } |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Tobias Grosser | 6287201 | 2011-11-17 12:56:04 +0000 | [diff] [blame] | 412 | Schedule = isl_union_map_union(Schedule, PartialSchedule); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 413 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 414 | isl_band_free(Band); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 415 | isl_space_free(Space); |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 418 | return Schedule; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Tobias Grosser | 460e9a4 | 2012-04-25 13:22:43 +0000 | [diff] [blame] | 421 | isl_union_map *IslScheduleOptimizer::getScheduleMap(isl_schedule *Schedule) { |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 422 | isl_band_list *BandList = isl_schedule_get_band_forest(Schedule); |
| 423 | isl_union_map *ScheduleMap = getScheduleForBandList(BandList); |
| 424 | isl_band_list_free(BandList); |
| 425 | return ScheduleMap; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 428 | bool IslScheduleOptimizer::runOnScop(Scop &S) { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 429 | Dependences *D = &getAnalysis<Dependences>(); |
| 430 | |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 431 | isl_schedule_free(LastSchedule); |
| 432 | LastSchedule = NULL; |
| 433 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 434 | // Build input data. |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 435 | int ValidityKinds = |
| 436 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 437 | int ProximityKinds; |
| 438 | |
| 439 | if (OptimizeDeps == "all") |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 440 | ProximityKinds = |
| 441 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 442 | else if (OptimizeDeps == "raw") |
Tobias Grosser | 1e03ad7 | 2012-02-15 09:58:42 +0000 | [diff] [blame] | 443 | ProximityKinds = Dependences::TYPE_RAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 444 | else { |
| 445 | errs() << "Do not know how to optimize for '" << OptimizeDeps << "'" |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 446 | << " Falling back to optimizing all dependences.\n"; |
| 447 | ProximityKinds = |
| 448 | Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; |
Tobias Grosser | 1deda29 | 2012-02-14 14:02:48 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 451 | isl_union_set *Domain = S.getDomains(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 452 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 453 | if (!Domain) |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 454 | return false; |
| 455 | |
Tobias Grosser | 8a50702 | 2012-03-16 11:51:41 +0000 | [diff] [blame] | 456 | isl_union_map *Validity = D->getDependences(ValidityKinds); |
| 457 | isl_union_map *Proximity = D->getDependences(ProximityKinds); |
| 458 | |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 459 | // Simplify the dependences by removing the constraints introduced by the |
| 460 | // domains. This can speed up the scheduling time significantly, as large |
| 461 | // constant coefficients will be removed from the dependences. The |
| 462 | // introduction of some additional dependences reduces the possible |
| 463 | // transformations, but in most cases, such transformation do not seem to be |
| 464 | // interesting anyway. In some cases this option may stop the scheduler to |
| 465 | // find any schedule. |
| 466 | if (SimplifyDeps == "yes") { |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 467 | Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain)); |
| 468 | Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain)); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 469 | Proximity = |
| 470 | isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 471 | Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain)); |
Tobias Grosser | a26db47 | 2012-01-30 19:38:43 +0000 | [diff] [blame] | 472 | } else if (SimplifyDeps != "no") { |
| 473 | errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' " |
| 474 | "or 'no'. Falling back to default: 'yes'\n"; |
| 475 | } |
| 476 | |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 477 | DEBUG(dbgs() << "\n\nCompute schedule from: "); |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 478 | DEBUG(dbgs() << "Domain := "; isl_union_set_dump(Domain); dbgs() << ";\n"); |
| 479 | DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(Proximity); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 480 | dbgs() << ";\n"); |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 481 | DEBUG(dbgs() << "Validity := "; isl_union_map_dump(Validity); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 482 | dbgs() << ";\n"); |
| 483 | |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 484 | int IslFusionStrategy; |
| 485 | |
| 486 | if (FusionStrategy == "max") { |
| 487 | IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX; |
| 488 | } else if (FusionStrategy == "min") { |
| 489 | IslFusionStrategy = ISL_SCHEDULE_FUSE_MIN; |
| 490 | } else { |
| 491 | errs() << "warning: Unknown fusion strategy. Falling back to maximal " |
| 492 | "fusion.\n"; |
| 493 | IslFusionStrategy = ISL_SCHEDULE_FUSE_MAX; |
| 494 | } |
| 495 | |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 496 | int IslMaximizeBands; |
| 497 | |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 498 | if (MaximizeBandDepth == "yes") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 499 | IslMaximizeBands = 1; |
Tobias Grosser | a4ea90b | 2012-01-30 22:43:56 +0000 | [diff] [blame] | 500 | } else if (MaximizeBandDepth == "no") { |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 501 | IslMaximizeBands = 0; |
| 502 | } else { |
| 503 | errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'" |
| 504 | " or 'no'. Falling back to default: 'yes'\n"; |
| 505 | IslMaximizeBands = 1; |
| 506 | } |
| 507 | |
Tobias Grosser | b3ad85b | 2012-01-30 19:38:50 +0000 | [diff] [blame] | 508 | isl_options_set_schedule_fuse(S.getIslCtx(), IslFusionStrategy); |
Tobias Grosser | 95e860c | 2012-01-30 19:38:54 +0000 | [diff] [blame] | 509 | isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands); |
Tobias Grosser | 992e60c | 2012-02-20 08:41:15 +0000 | [diff] [blame] | 510 | isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm); |
Tobias Grosser | 92f5480 | 2012-02-20 08:41:47 +0000 | [diff] [blame] | 511 | isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 512 | |
| 513 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE); |
Tobias Grosser | 00383a7 | 2012-02-14 14:02:44 +0000 | [diff] [blame] | 514 | isl_schedule *Schedule; |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 515 | Schedule = isl_union_set_compute_schedule(Domain, Validity, Proximity); |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 516 | isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT); |
| 517 | |
| 518 | // In cases the scheduler is not able to optimize the code, we just do not |
| 519 | // touch the schedule. |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 520 | if (!Schedule) |
Tobias Grosser | 42152ff | 2012-01-30 19:38:47 +0000 | [diff] [blame] | 521 | return false; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 522 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 523 | DEBUG(dbgs() << "Schedule := "; isl_schedule_dump(Schedule); dbgs() << ";\n"); |
Tobias Grosser | 4d63b9d | 2012-02-20 08:41:21 +0000 | [diff] [blame] | 524 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 525 | isl_union_map *ScheduleMap = getScheduleMap(Schedule); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 526 | |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 527 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) { |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 528 | ScopStmt *Stmt = *SI; |
Tobias Grosser | 249c4b1 | 2013-04-11 05:55:13 +0000 | [diff] [blame] | 529 | isl_map *StmtSchedule; |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 530 | isl_set *Domain = Stmt->getDomain(); |
| 531 | isl_union_map *StmtBand; |
| 532 | StmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap), |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 533 | isl_union_set_from_set(Domain)); |
Tobias Grosser | 249c4b1 | 2013-04-11 05:55:13 +0000 | [diff] [blame] | 534 | if (isl_union_map_is_empty(StmtBand)) { |
| 535 | // Statements with an empty iteration domain may not have a schedule |
| 536 | // assigned by the isl schedule optimizer. As Polly expects each statement |
| 537 | // to have a schedule, we keep the old schedule for this statement. As |
| 538 | // there are zero iterations to execute, the content of the schedule does |
| 539 | // not matter. |
| 540 | // |
| 541 | // TODO: Consider removing such statements when constructing the scop. |
Tobias Grosser | f242b80 | 2013-04-10 22:48:08 +0000 | [diff] [blame] | 542 | StmtSchedule = Stmt->getScattering(); |
| 543 | StmtSchedule = isl_map_set_tuple_id(StmtSchedule, isl_dim_out, NULL); |
Tobias Grosser | 249c4b1 | 2013-04-11 05:55:13 +0000 | [diff] [blame] | 544 | isl_union_map_free(StmtBand); |
| 545 | } else { |
| 546 | assert(isl_union_map_n_map(StmtBand) == 1); |
| 547 | StmtSchedule = isl_map_from_union_map(StmtBand); |
Tobias Grosser | f242b80 | 2013-04-10 22:48:08 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 550 | Stmt->setScattering(StmtSchedule); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Tobias Grosser | 1ae9a60 | 2011-11-17 12:56:03 +0000 | [diff] [blame] | 553 | isl_union_map_free(ScheduleMap); |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 554 | LastSchedule = Schedule; |
Tobias Grosser | de68cc9 | 2011-06-30 20:01:02 +0000 | [diff] [blame] | 555 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 556 | unsigned MaxScatDims = 0; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 557 | |
| 558 | for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 559 | MaxScatDims = std::max((*SI)->getNumScattering(), MaxScatDims); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 560 | |
Tobias Grosser | 98610ee | 2012-02-13 23:31:39 +0000 | [diff] [blame] | 561 | extendScattering(S, MaxScatDims); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 562 | return false; |
| 563 | } |
| 564 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 565 | void IslScheduleOptimizer::printScop(raw_ostream &OS) const { |
Tobias Grosser | 2878142 | 2012-10-16 07:29:19 +0000 | [diff] [blame] | 566 | isl_printer *p; |
| 567 | char *ScheduleStr; |
| 568 | |
| 569 | OS << "Calculated schedule:\n"; |
| 570 | |
| 571 | if (!LastSchedule) { |
| 572 | OS << "n/a\n"; |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule)); |
| 577 | p = isl_printer_print_schedule(p, LastSchedule); |
| 578 | ScheduleStr = isl_printer_get_str(p); |
| 579 | isl_printer_free(p); |
| 580 | |
| 581 | OS << ScheduleStr << "\n"; |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 584 | void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 585 | ScopPass::getAnalysisUsage(AU); |
| 586 | AU.addRequired<Dependences>(); |
| 587 | } |
| 588 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 589 | Pass *polly::createIslScheduleOptimizerPass() { |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 590 | return new IslScheduleOptimizer(); |
Tobias Grosser | 30aa24c | 2011-05-14 19:02:06 +0000 | [diff] [blame] | 591 | } |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 592 | |
| 593 | INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", |
| 594 | "Polly - Optimize schedule of SCoP", false, false); |
| 595 | INITIALIZE_PASS_DEPENDENCY(Dependences); |
| 596 | INITIALIZE_PASS_DEPENDENCY(ScopInfo); |
| 597 | INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", |
| 598 | "Polly - Optimize schedule of SCoP", false, false) |