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