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