1 //===--------- PolyhedralInfo.cpp  - Create Scops from LLVM IR-------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // An interface to the Polyhedral analysis engine(Polly) of LLVM.
10 //
11 // This pass provides an interface to the polyhedral analysis performed by
12 // Polly.
13 //
14 // This interface provides basic interface like isParallel, isVectorizable
15 // that can be used in LLVM transformation passes.
16 //
17 // Work in progress, this file is subject to change.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #include "polly/PolyhedralInfo.h"
22 #include "polly/DependenceInfo.h"
23 #include "polly/LinkAllPasses.h"
24 #include "polly/Options.h"
25 #include "polly/ScopInfo.h"
26 #include "polly/Support/GICHelper.h"
27 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/InitializePasses.h"
29 #include "llvm/Support/Debug.h"
30 #include "isl/union_map.h"
31 
32 using namespace llvm;
33 using namespace polly;
34 
35 #define DEBUG_TYPE "polyhedral-info"
36 
37 static cl::opt<bool> CheckParallel("polly-check-parallel",
38                                    cl::desc("Check for parallel loops"),
39                                    cl::Hidden, cl::init(false), cl::ZeroOrMore,
40                                    cl::cat(PollyCategory));
41 
42 static cl::opt<bool> CheckVectorizable("polly-check-vectorizable",
43                                        cl::desc("Check for vectorizable loops"),
44                                        cl::Hidden, cl::init(false),
45                                        cl::ZeroOrMore, cl::cat(PollyCategory));
46 
getAnalysisUsage(AnalysisUsage & AU) const47 void PolyhedralInfo::getAnalysisUsage(AnalysisUsage &AU) const {
48   AU.addRequiredTransitive<DependenceInfoWrapperPass>();
49   AU.addRequired<LoopInfoWrapperPass>();
50   AU.addRequiredTransitive<ScopInfoWrapperPass>();
51   AU.setPreservesAll();
52 }
53 
runOnFunction(Function & F)54 bool PolyhedralInfo::runOnFunction(Function &F) {
55   DI = &getAnalysis<DependenceInfoWrapperPass>();
56   SI = getAnalysis<ScopInfoWrapperPass>().getSI();
57   return false;
58 }
59 
print(raw_ostream & OS,const Module *) const60 void PolyhedralInfo::print(raw_ostream &OS, const Module *) const {
61   auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
62   for (auto *TopLevelLoop : LI) {
63     for (auto *L : depth_first(TopLevelLoop)) {
64       OS.indent(2) << L->getHeader()->getName() << ":\t";
65       if (CheckParallel && isParallel(L))
66         OS << "Loop is parallel.\n";
67       else if (CheckParallel)
68         OS << "Loop is not parallel.\n";
69     }
70   }
71 }
72 
checkParallel(Loop * L,isl_pw_aff ** MinDepDistPtr) const73 bool PolyhedralInfo::checkParallel(Loop *L, isl_pw_aff **MinDepDistPtr) const {
74   bool IsParallel;
75   const Scop *S = getScopContainingLoop(L);
76   if (!S)
77     return false;
78   const Dependences &D =
79       DI->getDependences(const_cast<Scop *>(S), Dependences::AL_Access);
80   if (!D.hasValidDependences())
81     return false;
82   LLVM_DEBUG(dbgs() << "Loop :\t" << L->getHeader()->getName() << ":\n");
83 
84   isl_union_map *Deps =
85       D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_WAW |
86                        Dependences::TYPE_WAR | Dependences::TYPE_RED)
87           .release();
88 
89   LLVM_DEBUG(dbgs() << "Dependences :\t" << stringFromIslObj(Deps, "null")
90                     << "\n");
91 
92   isl_union_map *Schedule = getScheduleForLoop(S, L);
93   LLVM_DEBUG(dbgs() << "Schedule: \t" << stringFromIslObj(Schedule, "null")
94                     << "\n");
95 
96   IsParallel = D.isParallel(Schedule, Deps, MinDepDistPtr);
97   isl_union_map_free(Schedule);
98   return IsParallel;
99 }
100 
isParallel(Loop * L) const101 bool PolyhedralInfo::isParallel(Loop *L) const { return checkParallel(L); }
102 
getScopContainingLoop(Loop * L) const103 const Scop *PolyhedralInfo::getScopContainingLoop(Loop *L) const {
104   assert((SI) && "ScopInfoWrapperPass is required by PolyhedralInfo pass!\n");
105   for (auto &It : *SI) {
106     Region *R = It.first;
107     if (R->contains(L))
108       return It.second.get();
109   }
110   return nullptr;
111 }
112 
113 //  Given a Loop and the containing SCoP, we compute the partial schedule
114 //  by taking union of individual schedules of each ScopStmt within the loop
115 //  and projecting out the inner dimensions from the range of the schedule.
116 //   for (i = 0; i < n; i++)
117 //      for (j = 0; j < n; j++)
118 //        A[j] = 1;  //Stmt
119 //
120 //  The original schedule will be
121 //    Stmt[i0, i1] -> [i0, i1]
122 //  The schedule for the outer loop will be
123 //    Stmt[i0, i1] -> [i0]
124 //  The schedule for the inner loop will be
125 //    Stmt[i0, i1] -> [i0, i1]
getScheduleForLoop(const Scop * S,Loop * L) const126 __isl_give isl_union_map *PolyhedralInfo::getScheduleForLoop(const Scop *S,
127                                                              Loop *L) const {
128   isl_union_map *Schedule = isl_union_map_empty(S->getParamSpace().release());
129   int CurrDim = S->getRelativeLoopDepth(L);
130   LLVM_DEBUG(dbgs() << "Relative loop depth:\t" << CurrDim << "\n");
131   assert(CurrDim >= 0 && "Loop in region should have at least depth one");
132 
133   for (auto &SS : *S) {
134     if (L->contains(SS.getSurroundingLoop())) {
135 
136       unsigned int MaxDim = SS.getNumIterators();
137       LLVM_DEBUG(dbgs() << "Maximum depth of Stmt:\t" << MaxDim << "\n");
138       isl_map *ScheduleMap = SS.getSchedule().release();
139       assert(
140           ScheduleMap &&
141           "Schedules that contain extension nodes require special handling.");
142 
143       ScheduleMap = isl_map_project_out(ScheduleMap, isl_dim_out, CurrDim + 1,
144                                         MaxDim - CurrDim - 1);
145       ScheduleMap = isl_map_set_tuple_id(ScheduleMap, isl_dim_in,
146                                          SS.getDomainId().release());
147       Schedule =
148           isl_union_map_union(Schedule, isl_union_map_from_map(ScheduleMap));
149     }
150   }
151   Schedule = isl_union_map_coalesce(Schedule);
152   return Schedule;
153 }
154 
155 char PolyhedralInfo::ID = 0;
156 
createPolyhedralInfoPass()157 Pass *polly::createPolyhedralInfoPass() { return new PolyhedralInfo(); }
158 
159 INITIALIZE_PASS_BEGIN(PolyhedralInfo, "polyhedral-info",
160                       "Polly - Interface to polyhedral analysis engine", false,
161                       false);
162 INITIALIZE_PASS_DEPENDENCY(DependenceInfoWrapperPass);
163 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
164 INITIALIZE_PASS_DEPENDENCY(ScopInfoWrapperPass);
165 INITIALIZE_PASS_END(PolyhedralInfo, "polyhedral-info",
166                     "Polly - Interface to polyhedral analysis engine", false,
167                     false)
168