1 //===------ FlattenSchedule.cpp --------------------------------*- C++ -*-===//
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 // Try to reduce the number of scatter dimension. Useful to make isl_union_map
10 // schedules more understandable. This is only intended for debugging and
11 // unittests, not for production use.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "polly/FlattenSchedule.h"
16 #include "polly/FlattenAlgo.h"
17 #include "polly/ScopInfo.h"
18 #include "polly/ScopPass.h"
19 #include "polly/Support/ISLOStream.h"
20 #include "polly/Support/ISLTools.h"
21 #define DEBUG_TYPE "polly-flatten-schedule"
22
23 using namespace polly;
24 using namespace llvm;
25
26 namespace {
27
28 /// Print a schedule to @p OS.
29 ///
30 /// Prints the schedule for each statements on a new line.
printSchedule(raw_ostream & OS,const isl::union_map & Schedule,int indent)31 void printSchedule(raw_ostream &OS, const isl::union_map &Schedule,
32 int indent) {
33 for (isl::map Map : Schedule.get_map_list())
34 OS.indent(indent) << Map << "\n";
35 }
36
37 /// Flatten the schedule stored in an polly::Scop.
38 class FlattenSchedule : public ScopPass {
39 private:
40 FlattenSchedule(const FlattenSchedule &) = delete;
41 const FlattenSchedule &operator=(const FlattenSchedule &) = delete;
42
43 std::shared_ptr<isl_ctx> IslCtx;
44 isl::union_map OldSchedule;
45
46 public:
47 static char ID;
FlattenSchedule()48 explicit FlattenSchedule() : ScopPass(ID) {}
49
getAnalysisUsage(AnalysisUsage & AU) const50 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.addRequiredTransitive<ScopInfoRegionPass>();
52 AU.setPreservesAll();
53 }
54
runOnScop(Scop & S)55 virtual bool runOnScop(Scop &S) override {
56 // Keep a reference to isl_ctx to ensure that it is not freed before we free
57 // OldSchedule.
58 IslCtx = S.getSharedIslCtx();
59
60 LLVM_DEBUG(dbgs() << "Going to flatten old schedule:\n");
61 OldSchedule = S.getSchedule();
62 LLVM_DEBUG(printSchedule(dbgs(), OldSchedule, 2));
63
64 auto Domains = S.getDomains();
65 auto RestrictedOldSchedule = OldSchedule.intersect_domain(Domains);
66 LLVM_DEBUG(dbgs() << "Old schedule with domains:\n");
67 LLVM_DEBUG(printSchedule(dbgs(), RestrictedOldSchedule, 2));
68
69 auto NewSchedule = flattenSchedule(RestrictedOldSchedule);
70
71 LLVM_DEBUG(dbgs() << "Flattened new schedule:\n");
72 LLVM_DEBUG(printSchedule(dbgs(), NewSchedule, 2));
73
74 NewSchedule = NewSchedule.gist_domain(Domains);
75 LLVM_DEBUG(dbgs() << "Gisted, flattened new schedule:\n");
76 LLVM_DEBUG(printSchedule(dbgs(), NewSchedule, 2));
77
78 S.setSchedule(NewSchedule);
79 return false;
80 }
81
printScop(raw_ostream & OS,Scop & S) const82 virtual void printScop(raw_ostream &OS, Scop &S) const override {
83 OS << "Schedule before flattening {\n";
84 printSchedule(OS, OldSchedule, 4);
85 OS << "}\n\n";
86
87 OS << "Schedule after flattening {\n";
88 printSchedule(OS, S.getSchedule(), 4);
89 OS << "}\n";
90 }
91
releaseMemory()92 virtual void releaseMemory() override {
93 OldSchedule = nullptr;
94 IslCtx.reset();
95 }
96 };
97
98 char FlattenSchedule::ID;
99 } // anonymous namespace
100
createFlattenSchedulePass()101 Pass *polly::createFlattenSchedulePass() { return new FlattenSchedule(); }
102
103 INITIALIZE_PASS_BEGIN(FlattenSchedule, "polly-flatten-schedule",
104 "Polly - Flatten schedule", false, false)
105 INITIALIZE_PASS_END(FlattenSchedule, "polly-flatten-schedule",
106 "Polly - Flatten schedule", false, false)
107