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