1 //===- MaximalStaticExpansion.cpp -----------------------------------------===//
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 fully expand the memory accesses of a Scop to get rid of
11 // dependencies.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "polly/DependenceInfo.h"
16 #include "polly/LinkAllPasses.h"
17 #include "polly/ScopInfo.h"
18 #include "polly/ScopPass.h"
19 #include "polly/Support/GICHelper.h"
20 #include "polly/Support/ISLTools.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
24 #include "llvm/Pass.h"
25 #include "isl/isl-noexceptions.h"
26 #include "isl/union_map.h"
27 #include <cassert>
28 #include <limits>
29 #include <string>
30 #include <vector>
31 
32 using namespace llvm;
33 using namespace polly;
34 
35 #define DEBUG_TYPE "polly-mse"
36 
37 namespace {
38 
39 class MaximalStaticExpander : public ScopPass {
40 public:
41   static char ID;
42 
43   explicit MaximalStaticExpander() : ScopPass(ID) {}
44 
45   ~MaximalStaticExpander() override = default;
46 
47   /// Expand the accesses of the SCoP.
48   ///
49   /// @param S The SCoP that must be expanded.
50   bool runOnScop(Scop &S) override;
51 
52   /// Print the SCoP.
53   ///
54   /// @param OS The stream where to print.
55   /// @param S The SCop that must be printed.
56   void printScop(raw_ostream &OS, Scop &S) const override;
57 
58   /// Register all analyses and transformations required.
59   void getAnalysisUsage(AnalysisUsage &AU) const override;
60 
61 private:
62   /// OptimizationRemarkEmitter object for displaying diagnostic remarks.
63   OptimizationRemarkEmitter *ORE;
64 
65   /// Emit remark
66   void emitRemark(StringRef Msg, Instruction *Inst);
67 
68   /// Return true if the SAI in parameter is expandable.
69   ///
70   /// @param SAI the SAI that need to be checked.
71   /// @param Writes A set that will contains all the write accesses.
72   /// @param Reads A set that will contains all the read accesses.
73   /// @param S The SCop in which the SAI is in.
74   /// @param Dependences The RAW dependences of the SCop.
75   bool isExpandable(const ScopArrayInfo *SAI,
76                     SmallPtrSetImpl<MemoryAccess *> &Writes,
77                     SmallPtrSetImpl<MemoryAccess *> &Reads, Scop &S,
78                     const isl::union_map &Dependences);
79 
80   /// Expand the MemoryAccess according to its domain.
81   ///
82   /// @param S The SCop in which the memory access appears in.
83   /// @param MA The memory access that need to be expanded.
84   ScopArrayInfo *expandAccess(Scop &S, MemoryAccess *MA);
85 
86   /// Filter the dependences to have only one related to current memory access.
87   ///
88   /// @param S The SCop in which the memory access appears in.
89   /// @param MapDependences The dependences to filter.
90   /// @param MA The memory access that need to be expanded.
91   isl::union_map filterDependences(Scop &S,
92                                    const isl::union_map &MapDependences,
93                                    MemoryAccess *MA);
94 
95   /// Expand the MemoryAccess according to Dependences and already expanded
96   /// MemoryAccesses.
97   ///
98   /// @param The SCop in which the memory access appears in.
99   /// @param The memory access that need to be expanded.
100   /// @param Dependences The RAW dependences of the SCop.
101   /// @param ExpandedSAI The expanded SAI created during write expansion.
102   /// @param Reverse if true, the Dependences union_map is reversed before
103   /// intersection.
104   void mapAccess(Scop &S, SmallPtrSetImpl<MemoryAccess *> &Accesses,
105                  const isl::union_map &Dependences, ScopArrayInfo *ExpandedSAI,
106                  bool Reverse);
107 
108   /// Expand PHI memory accesses.
109   ///
110   /// @param The SCop in which the memory access appears in.
111   /// @param The ScopArrayInfo representing the PHI accesses to expand.
112   /// @param Dependences The RAW dependences of the SCop.
113   void expandPhi(Scop &S, const ScopArrayInfo *SAI,
114                  const isl::union_map &Dependences);
115 };
116 } // namespace
117 
118 #ifndef NDEBUG
119 /// Whether a dimension of a set is bounded (lower and upper) by a constant,
120 /// i.e. there are two constants Min and Max, such that every value x of the
121 /// chosen dimensions is Min <= x <= Max.
122 static bool isDimBoundedByConstant(isl::set Set, unsigned dim) {
123   auto ParamDims = Set.dim(isl::dim::param);
124   Set = Set.project_out(isl::dim::param, 0, ParamDims);
125   Set = Set.project_out(isl::dim::set, 0, dim);
126   auto SetDims = Set.dim(isl::dim::set);
127   Set = Set.project_out(isl::dim::set, 1, SetDims - 1);
128   return bool(Set.is_bounded());
129 }
130 #endif
131 
132 char MaximalStaticExpander::ID = 0;
133 
134 isl::union_map MaximalStaticExpander::filterDependences(
135     Scop &S, const isl::union_map &Dependences, MemoryAccess *MA) {
136   auto SAI = MA->getLatestScopArrayInfo();
137 
138   auto AccessDomainSet = MA->getAccessRelation().domain();
139   auto AccessDomainId = AccessDomainSet.get_tuple_id();
140 
141   isl::union_map MapDependences = isl::union_map::empty(S.getParamSpace());
142 
143   for (isl::map Map : Dependences.get_map_list()) {
144     // Filter out Statement to Statement dependences.
145     if (!Map.can_curry())
146       continue;
147 
148     // Intersect with the relevant SAI.
149     auto TmpMapDomainId =
150         Map.get_space().domain().unwrap().range().get_tuple_id(isl::dim::set);
151 
152     ScopArrayInfo *UserSAI =
153         static_cast<ScopArrayInfo *>(TmpMapDomainId.get_user());
154 
155     if (SAI != UserSAI)
156       continue;
157 
158     // Get the correct S1[] -> S2[] dependence.
159     auto NewMap = Map.factor_domain();
160     auto NewMapDomainId = NewMap.domain().get_tuple_id();
161 
162     if (AccessDomainId.get() != NewMapDomainId.get())
163       continue;
164 
165     // Add the corresponding map to MapDependences.
166     MapDependences = MapDependences.add_map(NewMap);
167   }
168 
169   return MapDependences;
170 }
171 
172 bool MaximalStaticExpander::isExpandable(
173     const ScopArrayInfo *SAI, SmallPtrSetImpl<MemoryAccess *> &Writes,
174     SmallPtrSetImpl<MemoryAccess *> &Reads, Scop &S,
175     const isl::union_map &Dependences) {
176   if (SAI->isValueKind()) {
177     Writes.insert(S.getValueDef(SAI));
178     for (auto MA : S.getValueUses(SAI))
179       Reads.insert(MA);
180     return true;
181   } else if (SAI->isPHIKind()) {
182     auto Read = S.getPHIRead(SAI);
183 
184     auto StmtDomain = isl::union_set(Read->getStatement()->getDomain());
185 
186     auto Writes = S.getPHIIncomings(SAI);
187 
188     // Get the domain where all the writes are writing to.
189     auto WriteDomain = isl::union_set::empty(S.getParamSpace());
190 
191     for (auto Write : Writes) {
192       auto MapDeps = filterDependences(S, Dependences, Write);
193       for (isl::map Map : MapDeps.get_map_list())
194         WriteDomain = WriteDomain.add_set(Map.range());
195     }
196 
197     // For now, read from original scalar is not possible.
198     if (!StmtDomain.is_equal(WriteDomain)) {
199       emitRemark(SAI->getName() + " read from its original value.",
200                  Read->getAccessInstruction());
201       return false;
202     }
203 
204     return true;
205   } else if (SAI->isExitPHIKind()) {
206     // For now, we are not able to expand ExitPhi.
207     emitRemark(SAI->getName() + " is a ExitPhi node.",
208                S.getEnteringBlock()->getFirstNonPHI());
209     return false;
210   }
211 
212   int NumberWrites = 0;
213   for (ScopStmt &Stmt : S) {
214     auto StmtReads = isl::union_map::empty(S.getParamSpace());
215     auto StmtWrites = isl::union_map::empty(S.getParamSpace());
216 
217     for (MemoryAccess *MA : Stmt) {
218       // Check if the current MemoryAccess involved the current SAI.
219       if (SAI != MA->getLatestScopArrayInfo())
220         continue;
221 
222       // For now, we are not able to expand array where read come after write
223       // (to the same location) in a same statement.
224       auto AccRel = isl::union_map(MA->getAccessRelation());
225       if (MA->isRead()) {
226         // Reject load after store to same location.
227         if (!StmtWrites.is_disjoint(AccRel)) {
228           emitRemark(SAI->getName() + " has read after write to the same "
229                                       "element in same statement. The "
230                                       "dependences found during analysis may "
231                                       "be wrong because Polly is not able to "
232                                       "handle such case for now.",
233                      MA->getAccessInstruction());
234           return false;
235         }
236 
237         StmtReads = StmtReads.unite(AccRel);
238       } else {
239         StmtWrites = StmtWrites.unite(AccRel);
240       }
241 
242       // For now, we are not able to expand MayWrite.
243       if (MA->isMayWrite()) {
244         emitRemark(SAI->getName() + " has a maywrite access.",
245                    MA->getAccessInstruction());
246         return false;
247       }
248 
249       // For now, we are not able to expand SAI with more than one write.
250       if (MA->isMustWrite()) {
251         Writes.insert(MA);
252         NumberWrites++;
253         if (NumberWrites > 1) {
254           emitRemark(SAI->getName() + " has more than 1 write access.",
255                      MA->getAccessInstruction());
256           return false;
257         }
258       }
259 
260       // Check if it is possible to expand this read.
261       if (MA->isRead()) {
262         // Get the domain of the current ScopStmt.
263         auto StmtDomain = Stmt.getDomain();
264 
265         // Get the domain of the future Read access.
266         auto ReadDomainSet = MA->getAccessRelation().domain();
267         auto ReadDomain = isl::union_set(ReadDomainSet);
268 
269         // Get the dependences relevant for this MA
270         auto MapDependences = filterDependences(S, Dependences.reverse(), MA);
271         unsigned NumberElementMap = isl_union_map_n_map(MapDependences.get());
272 
273         if (NumberElementMap == 0) {
274           emitRemark("The expansion of " + SAI->getName() +
275                          " would lead to a read from the original array.",
276                      MA->getAccessInstruction());
277           return false;
278         }
279 
280         auto DepsDomain = MapDependences.domain();
281 
282         // If there are multiple maps in the Deps, we cannot handle this case
283         // for now.
284         if (NumberElementMap != 1) {
285           emitRemark(SAI->getName() +
286                          " has too many dependences to be handle for now.",
287                      MA->getAccessInstruction());
288           return false;
289         }
290 
291         auto DepsDomainSet = isl::set(DepsDomain);
292 
293         // For now, read from the original array is not possible.
294         if (!StmtDomain.is_subset(DepsDomainSet)) {
295           emitRemark("The expansion of " + SAI->getName() +
296                          " would lead to a read from the original array.",
297                      MA->getAccessInstruction());
298           return false;
299         }
300 
301         Reads.insert(MA);
302       }
303     }
304   }
305 
306   // No need to expand SAI with no write.
307   if (NumberWrites == 0) {
308     emitRemark(SAI->getName() + " has 0 write access.",
309                S.getEnteringBlock()->getFirstNonPHI());
310     return false;
311   }
312 
313   return true;
314 }
315 
316 void MaximalStaticExpander::mapAccess(Scop &S,
317                                       SmallPtrSetImpl<MemoryAccess *> &Accesses,
318                                       const isl::union_map &Dependences,
319                                       ScopArrayInfo *ExpandedSAI,
320                                       bool Reverse) {
321   for (auto MA : Accesses) {
322     // Get the current AM.
323     auto CurrentAccessMap = MA->getAccessRelation();
324 
325     // Get RAW dependences for the current WA.
326     auto DomainSet = MA->getAccessRelation().domain();
327     auto Domain = isl::union_set(DomainSet);
328 
329     // Get the dependences relevant for this MA.
330     isl::union_map MapDependences =
331         filterDependences(S, Reverse ? Dependences.reverse() : Dependences, MA);
332 
333     // If no dependences, no need to modify anything.
334     if (MapDependences.is_empty())
335       return;
336 
337     assert(isl_union_map_n_map(MapDependences.get()) == 1 &&
338            "There are more than one RAW dependencies in the union map.");
339     auto NewAccessMap = isl::map::from_union_map(MapDependences);
340 
341     auto Id = ExpandedSAI->getBasePtrId();
342 
343     // Replace the out tuple id with the one of the access array.
344     NewAccessMap = NewAccessMap.set_tuple_id(isl::dim::out, Id);
345 
346     // Set the new access relation.
347     MA->setNewAccessRelation(NewAccessMap);
348   }
349 }
350 
351 ScopArrayInfo *MaximalStaticExpander::expandAccess(Scop &S, MemoryAccess *MA) {
352   // Get the current AM.
353   auto CurrentAccessMap = MA->getAccessRelation();
354 
355   unsigned in_dimensions = CurrentAccessMap.dim(isl::dim::in);
356 
357   // Get domain from the current AM.
358   auto Domain = CurrentAccessMap.domain();
359 
360   // Create a new AM from the domain.
361   auto NewAccessMap = isl::map::from_domain(Domain);
362 
363   // Add dimensions to the new AM according to the current in_dim.
364   NewAccessMap = NewAccessMap.add_dims(isl::dim::out, in_dimensions);
365 
366   // Create the string representing the name of the new SAI.
367   // One new SAI for each statement so that each write go to a different memory
368   // cell.
369   auto CurrentStmtDomain = MA->getStatement()->getDomain();
370   auto CurrentStmtName = CurrentStmtDomain.get_tuple_name();
371   auto CurrentOutId = CurrentAccessMap.get_tuple_id(isl::dim::out);
372   std::string CurrentOutIdString =
373       MA->getScopArrayInfo()->getName() + "_" + CurrentStmtName + "_expanded";
374 
375   // Set the tuple id for the out dimension.
376   NewAccessMap = NewAccessMap.set_tuple_id(isl::dim::out, CurrentOutId);
377 
378   // Create the size vector.
379   std::vector<unsigned> Sizes;
380   for (unsigned i = 0; i < in_dimensions; i++) {
381     assert(isDimBoundedByConstant(CurrentStmtDomain, i) &&
382            "Domain boundary are not constant.");
383     auto UpperBound = getConstant(CurrentStmtDomain.dim_max(i), true, false);
384     assert(!UpperBound.is_null() && UpperBound.is_pos() &&
385            !UpperBound.is_nan() &&
386            "The upper bound is not a positive integer.");
387     assert(UpperBound.le(isl::val(CurrentAccessMap.get_ctx(),
388                                   std::numeric_limits<int>::max() - 1)) &&
389            "The upper bound overflow a int.");
390     Sizes.push_back(UpperBound.get_num_si() + 1);
391   }
392 
393   // Get the ElementType of the current SAI.
394   auto ElementType = MA->getLatestScopArrayInfo()->getElementType();
395 
396   // Create (or get if already existing) the new expanded SAI.
397   auto ExpandedSAI =
398       S.createScopArrayInfo(ElementType, CurrentOutIdString, Sizes);
399   ExpandedSAI->setIsOnHeap(true);
400 
401   // Get the out Id of the expanded Array.
402   auto NewOutId = ExpandedSAI->getBasePtrId();
403 
404   // Set the out id of the new AM to the new SAI id.
405   NewAccessMap = NewAccessMap.set_tuple_id(isl::dim::out, NewOutId);
406 
407   // Add constraints to linked output with input id.
408   auto SpaceMap = NewAccessMap.get_space();
409   auto ConstraintBasicMap =
410       isl::basic_map::equal(SpaceMap, SpaceMap.dim(isl::dim::in));
411   NewAccessMap = isl::map(ConstraintBasicMap);
412 
413   // Set the new access relation map.
414   MA->setNewAccessRelation(NewAccessMap);
415 
416   return ExpandedSAI;
417 }
418 
419 void MaximalStaticExpander::expandPhi(Scop &S, const ScopArrayInfo *SAI,
420                                       const isl::union_map &Dependences) {
421   SmallPtrSet<MemoryAccess *, 4> Writes;
422   for (auto MA : S.getPHIIncomings(SAI))
423     Writes.insert(MA);
424   auto Read = S.getPHIRead(SAI);
425   auto ExpandedSAI = expandAccess(S, Read);
426 
427   mapAccess(S, Writes, Dependences, ExpandedSAI, false);
428 }
429 
430 void MaximalStaticExpander::emitRemark(StringRef Msg, Instruction *Inst) {
431   ORE->emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "ExpansionRejection", Inst)
432             << Msg);
433 }
434 
435 bool MaximalStaticExpander::runOnScop(Scop &S) {
436   // Get the ORE from OptimizationRemarkEmitterWrapperPass.
437   ORE = &(getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE());
438 
439   // Get the RAW Dependences.
440   auto &DI = getAnalysis<DependenceInfo>();
441   auto &D = DI.getDependences(Dependences::AL_Reference);
442   isl::union_map Dependences = D.getDependences(Dependences::TYPE_RAW);
443 
444   SmallVector<ScopArrayInfo *, 4> CurrentSAI(S.arrays().begin(),
445                                              S.arrays().end());
446 
447   for (auto SAI : CurrentSAI) {
448     SmallPtrSet<MemoryAccess *, 4> AllWrites;
449     SmallPtrSet<MemoryAccess *, 4> AllReads;
450     if (!isExpandable(SAI, AllWrites, AllReads, S, Dependences))
451       continue;
452 
453     if (SAI->isValueKind() || SAI->isArrayKind()) {
454       assert(AllWrites.size() == 1 || SAI->isValueKind());
455 
456       auto TheWrite = *(AllWrites.begin());
457       ScopArrayInfo *ExpandedArray = expandAccess(S, TheWrite);
458 
459       mapAccess(S, AllReads, Dependences, ExpandedArray, true);
460     } else if (SAI->isPHIKind()) {
461       expandPhi(S, SAI, Dependences);
462     }
463   }
464 
465   return false;
466 }
467 
468 void MaximalStaticExpander::printScop(raw_ostream &OS, Scop &S) const {
469   S.print(OS, false);
470 }
471 
472 void MaximalStaticExpander::getAnalysisUsage(AnalysisUsage &AU) const {
473   ScopPass::getAnalysisUsage(AU);
474   AU.addRequired<DependenceInfo>();
475   AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
476 }
477 
478 Pass *polly::createMaximalStaticExpansionPass() {
479   return new MaximalStaticExpander();
480 }
481 
482 INITIALIZE_PASS_BEGIN(MaximalStaticExpander, "polly-mse",
483                       "Polly - Maximal static expansion of SCoP", false, false);
484 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
485 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass);
486 INITIALIZE_PASS_END(MaximalStaticExpander, "polly-mse",
487                     "Polly - Maximal static expansion of SCoP", false, false)
488