blob: 877e2559dcde42e672db16455ead8bba6deb8662 [file] [log] [blame]
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001//===- 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
23#include "polly/Dependences.h"
24#include "polly/ScopInfo.h"
25
26#include "isl/dim.h"
27#include "isl/map.h"
28#include "isl/constraint.h"
29#include "isl/schedule.h"
30
31#define DEBUG_TYPE "polly-optimize-isl"
32#include "llvm/Support/Debug.h"
33
34using namespace llvm;
35using namespace polly;
36
37namespace {
38
39 class ScheduleOptimizer : public ScopPass {
40
41 public:
42 static char ID;
43 explicit ScheduleOptimizer() : ScopPass(ID) {}
44
45 virtual bool runOnScop(Scop &S);
46 void printScop(llvm::raw_ostream &OS) const;
47 void getAnalysisUsage(AnalysisUsage &AU) const;
48 };
49
50}
51
52char ScheduleOptimizer::ID = 0;
53
54static int getSingleMap(__isl_take isl_map *map, void *user) {
55 isl_map **singleMap = (isl_map **) user;
56 *singleMap = map;
57
58 return 0;
59}
60
Tobias Grosser76747f72011-05-24 12:20:07 +000061static void extendScattering(Scop &S, unsigned scatDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000062 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
63 ScopStmt *stmt = *SI;
64
65 if (stmt->isFinalRead())
66 continue;
67
68 isl_map *scattering = stmt->getScattering();
69 isl_dim *dim = isl_dim_alloc(isl_map_get_ctx(scattering),
70 isl_map_n_param(scattering),
71 isl_map_n_out(scattering),
72 scatDimensions);
73 isl_basic_map *changeScattering = isl_basic_map_universe(isl_dim_copy(dim));
74
75 for (unsigned i = 0; i < isl_map_n_out(scattering); i++) {
76 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
77 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
78 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
79 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
80 }
81
82 for (unsigned i = isl_map_n_out(scattering); i < scatDimensions; i++) {
83 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
84 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
85 changeScattering = isl_basic_map_add_constraint(changeScattering, c);
86 }
87
88 isl_map *changeScatteringMap = isl_map_from_basic_map(changeScattering);
89
90 stmt->setScattering(isl_map_apply_range(scattering, changeScatteringMap));
91 }
92}
93
94// @brief Tile a band.
95//
96// This function recieves a map that assigns to the instances of a statement
97// an execution time.
98//
99// [i_0, i_1, i_2] -> [o_0, o_1, o_2, i_0, i_1, i_2]:
100// o_0 % 32 = 0 and o_1 % 32 = 0 and o_2 % 32 = 0
101// and o0 <= i0 <= o0 + 32 and o1 <= i1 <= o1 + 32 and o2 <= i2 <= o2 + 32
102
Tobias Grosser76747f72011-05-24 12:20:07 +0000103static isl_map *tileBand(isl_map *band) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000104 int dimensions = isl_map_n_out(band);
105 int tileSize = 32;
106
107 isl_dim *dim = isl_dim_alloc(isl_map_get_ctx(band), isl_map_n_param(band),
108 dimensions, dimensions * 3);
109 isl_basic_map *tiledBand = isl_basic_map_universe(isl_dim_copy(dim));
110
111 for (int i = 0; i < dimensions; i++) {
112 isl_constraint *c = isl_equality_alloc(isl_dim_copy(dim));
113 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
114 isl_constraint_set_coefficient_si(c, isl_dim_out, 2 * dimensions + i,
115 -tileSize);
116 tiledBand = isl_basic_map_add_constraint(tiledBand, c);
117
118
119 c = isl_equality_alloc(isl_dim_copy(dim));
120 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
121 isl_constraint_set_coefficient_si(c, isl_dim_out, dimensions + i, 1);
122 tiledBand = isl_basic_map_add_constraint(tiledBand, c);
123
124 c = isl_inequality_alloc(isl_dim_copy(dim));
125 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
126 isl_constraint_set_coefficient_si(c, isl_dim_out, dimensions + i, 1);
127 tiledBand = isl_basic_map_add_constraint(tiledBand, c);
128
129 c = isl_inequality_alloc(isl_dim_copy(dim));
130 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
131 isl_constraint_set_coefficient_si(c, isl_dim_out, dimensions + i, -1);
132 isl_constraint_set_constant_si(c, tileSize - 1);
133 tiledBand = isl_basic_map_add_constraint(tiledBand, c);
134 }
135
136 // Project out auxilary dimensions (introduced to ensure 'ii % tileSize = 0')
137 //
138 // The real dimensions are transformed into existentially quantified ones.
139 // This reduces the number of visible scattering dimensions. Also, Cloog
140 // produces better code, if auxilary dimensions are existentially quantified.
141 tiledBand = isl_basic_map_project_out(tiledBand, isl_dim_out, 2 * dimensions,
142 dimensions);
143
144 return isl_map_apply_range(band, isl_map_from_basic_map(tiledBand));
145}
146
147bool ScheduleOptimizer::runOnScop(Scop &S) {
148 Dependences *D = &getAnalysis<Dependences>();
149
150 // Build input data.
151 int dependencyKinds = Dependences::TYPE_RAW
152 | Dependences::TYPE_WAR
153 | Dependences::TYPE_WAW;
154
155 isl_union_map *validity = D->getDependences(dependencyKinds);
156 isl_union_map *proximity = D->getDependences(dependencyKinds);
157 isl_union_set *domain = NULL;
158
159 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
160 if ((*SI)->isFinalRead())
161 continue;
162 else if (!domain)
163 domain = isl_union_set_from_set((*SI)->getDomain());
164 else
165 domain = isl_union_set_union(domain,
166 isl_union_set_from_set((*SI)->getDomain()));
167
168 if (!domain)
169 return false;
170
171 DEBUG(dbgs() << "\n\nCompute schedule from: ");
172 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
173 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
174 dbgs() << ";\n");
175 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
176 dbgs() << ";\n");
177
178 isl_schedule *schedule;
179
180 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
181
182 // Get the complete schedule.
183 isl_union_map *scheduleMap = isl_schedule_get_map(schedule);
184
185 DEBUG(dbgs() << "Computed schedule: ");
186 DEBUG(isl_union_map_dump(scheduleMap));
187 DEBUG(dbgs() << "Individual bands: ");
188
189 // Get individual tileable bands.
190 for (int i = 0; i < isl_schedule_n_band(schedule); i++) {
191 isl_union_map *band = isl_schedule_get_band(schedule, i);
192
193 DEBUG(dbgs() << "Band " << i << ": ");
194 DEBUG(isl_union_map_dump(band));
195
196 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
197 ScopStmt *stmt = *SI;
198
199 if (stmt->isFinalRead())
200 continue;
201
202 isl_set *domain = stmt->getDomain();
203 isl_union_map *stmtBand;
204 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(band),
205 isl_union_set_from_set(domain));
206
207 isl_map *sband;
208 isl_union_map_foreach_map(stmtBand, getSingleMap, &sband);
209
210 sband = tileBand(sband);
211 DEBUG(dbgs() << "tiled band: ");
212 DEBUG(isl_map_dump(sband));
213
214 if (i == 0)
215 stmt->setScattering(sband);
216 else {
217 isl_map *scattering = stmt->getScattering();
218 scattering = isl_map_range_product(scattering, sband);
219 scattering = isl_map_flatten(scattering);
220 stmt->setScattering(scattering);
221 }
222 }
223
224 }
225
226 unsigned maxScatDims = 0;
227
228 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
229 maxScatDims = std::max(isl_map_n_out((*SI)->getScattering()), maxScatDims);
230
231 extendScattering(S, maxScatDims);
232 isl_schedule_free(schedule);
233 return false;
234}
235
236void ScheduleOptimizer::printScop(raw_ostream &OS) const {
237}
238
239void ScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
240 ScopPass::getAnalysisUsage(AU);
241 AU.addRequired<Dependences>();
242}
243
244static RegisterPass<ScheduleOptimizer> A("polly-optimize-isl",
245 "Polly - Calculate optimized "
246 "schedules using the isl schedule "
247 "calculator");
248
249Pass* polly::createScheduleOptimizerPass() {
250 return new ScheduleOptimizer();
251}