Polly 19.0.0git
FlattenSchedule.cpp
Go to the documentation of this file.
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
16#include "polly/FlattenAlgo.h"
17#include "polly/ScopInfo.h"
18#include "polly/ScopPass.h"
22#define DEBUG_TYPE "polly-flatten-schedule"
23
24using namespace polly;
25using namespace llvm;
26
27namespace {
28
29/// Print a schedule to @p OS.
30///
31/// Prints the schedule for each statements on a new line.
32void printSchedule(raw_ostream &OS, const isl::union_map &Schedule,
33 int indent) {
34 for (isl::map Map : Schedule.get_map_list())
35 OS.indent(indent) << Map << "\n";
36}
37
38/// Flatten the schedule stored in an polly::Scop.
39class FlattenSchedule final : public ScopPass {
40private:
41 FlattenSchedule(const FlattenSchedule &) = delete;
42 const FlattenSchedule &operator=(const FlattenSchedule &) = delete;
43
44 std::shared_ptr<isl_ctx> IslCtx;
45 isl::union_map OldSchedule;
46
47public:
48 static char ID;
49 explicit FlattenSchedule() : ScopPass(ID) {}
50
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.addRequiredTransitive<ScopInfoRegionPass>();
53 AU.setPreservesAll();
54 }
55
56 bool runOnScop(Scop &S) override {
57 // Keep a reference to isl_ctx to ensure that it is not freed before we free
58 // OldSchedule.
59 IslCtx = S.getSharedIslCtx();
60
61 POLLY_DEBUG(dbgs() << "Going to flatten old schedule:\n");
62 OldSchedule = S.getSchedule();
63 POLLY_DEBUG(printSchedule(dbgs(), OldSchedule, 2));
64
65 auto Domains = S.getDomains();
66 auto RestrictedOldSchedule = OldSchedule.intersect_domain(Domains);
67 POLLY_DEBUG(dbgs() << "Old schedule with domains:\n");
68 POLLY_DEBUG(printSchedule(dbgs(), RestrictedOldSchedule, 2));
69
70 auto NewSchedule = flattenSchedule(RestrictedOldSchedule);
71
72 POLLY_DEBUG(dbgs() << "Flattened new schedule:\n");
73 POLLY_DEBUG(printSchedule(dbgs(), NewSchedule, 2));
74
75 NewSchedule = NewSchedule.gist_domain(Domains);
76 POLLY_DEBUG(dbgs() << "Gisted, flattened new schedule:\n");
77 POLLY_DEBUG(printSchedule(dbgs(), NewSchedule, 2));
78
79 S.setSchedule(NewSchedule);
80 return false;
81 }
82
83 void printScop(raw_ostream &OS, Scop &S) const override {
84 OS << "Schedule before flattening {\n";
85 printSchedule(OS, OldSchedule, 4);
86 OS << "}\n\n";
87
88 OS << "Schedule after flattening {\n";
89 printSchedule(OS, S.getSchedule(), 4);
90 OS << "}\n";
91 }
92
93 void releaseMemory() override {
94 OldSchedule = {};
95 IslCtx.reset();
96 }
97};
98
99char FlattenSchedule::ID;
100
101/// Print result from FlattenSchedule.
102class FlattenSchedulePrinterLegacyPass final : public ScopPass {
103public:
104 static char ID;
105
106 FlattenSchedulePrinterLegacyPass()
107 : FlattenSchedulePrinterLegacyPass(outs()) {}
108 explicit FlattenSchedulePrinterLegacyPass(llvm::raw_ostream &OS)
109 : ScopPass(ID), OS(OS) {}
110
111 bool runOnScop(Scop &S) override {
112 FlattenSchedule &P = getAnalysis<FlattenSchedule>();
113
114 OS << "Printing analysis '" << P.getPassName() << "' for region: '"
115 << S.getRegion().getNameStr() << "' in function '"
116 << S.getFunction().getName() << "':\n";
117 P.printScop(OS, S);
118
119 return false;
120 }
121
122 void getAnalysisUsage(AnalysisUsage &AU) const override {
124 AU.addRequired<FlattenSchedule>();
125 AU.setPreservesAll();
126 }
127
128private:
129 llvm::raw_ostream &OS;
130};
131
132char FlattenSchedulePrinterLegacyPass::ID = 0;
133} // anonymous namespace
134
135Pass *polly::createFlattenSchedulePass() { return new FlattenSchedule(); }
136
138 return new FlattenSchedulePrinterLegacyPass(OS);
139}
140
141INITIALIZE_PASS_BEGIN(FlattenSchedule, "polly-flatten-schedule",
142 "Polly - Flatten schedule", false, false)
143INITIALIZE_PASS_END(FlattenSchedule, "polly-flatten-schedule",
144 "Polly - Flatten schedule", false, false)
145
146INITIALIZE_PASS_BEGIN(FlattenSchedulePrinterLegacyPass,
147 "polly-print-flatten-schedule",
148 "Polly - Print flattened schedule", false, false)
149INITIALIZE_PASS_DEPENDENCY(FlattenSchedule)
150INITIALIZE_PASS_END(FlattenSchedulePrinterLegacyPass,
151 "polly-print-flatten-schedule",
152 "Polly - Print flattened schedule", false, false)
INITIALIZE_PASS_END(DependenceInfo, "polly-dependences", "Polly - Calculate dependences", false, false) namespace
INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass)
INITIALIZE_PASS_BEGIN(FlattenSchedule, "polly-flatten-schedule", "Polly - Flatten schedule", false, false) INITIALIZE_PASS_END(FlattenSchedule
polly flatten schedule
#define POLLY_DEBUG(X)
Definition: PollyDebug.h:23
isl::map_list get_map_list() const
isl::union_map intersect_domain(isl::space space) const
The legacy pass manager's analysis pass to compute scop information for a region.
Definition: ScopInfo.h:2677
ScopPass - This class adapts the RegionPass interface to allow convenient creation of passes that ope...
Definition: ScopPass.h:161
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: ScopPass.cpp:44
virtual bool runOnScop(Scop &S)=0
runOnScop - This method must be overloaded to perform the desired Polyhedral transformation or analys...
virtual void printScop(raw_ostream &OS, Scop &S) const
Print method for SCoPs.
Definition: ScopPass.h:173
Static Control Part.
Definition: ScopInfo.h:1628
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
llvm::Pass * createFlattenSchedulePass()
llvm::Pass * createFlattenSchedulePrinterLegacyPass(llvm::raw_ostream &OS)
isl::union_map flattenSchedule(isl::union_map Schedule)
Recursively flatten a schedule.