1 //===------ FlattenSchedule.cpp --------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
23 using namespace polly
;
28 /// Print a schedule to @p OS.
30 /// Prints the schedule for each statements on a new line.
31 void printSchedule(raw_ostream
&OS
, const isl::union_map
&Schedule
,
33 for (isl::map Map
: Schedule
.get_map_list())
34 OS
.indent(indent
) << Map
<< "\n";
37 /// Flatten the schedule stored in an polly::Scop.
38 class FlattenSchedule final
: public ScopPass
{
40 FlattenSchedule(const FlattenSchedule
&) = delete;
41 const FlattenSchedule
&operator=(const FlattenSchedule
&) = delete;
43 std::shared_ptr
<isl_ctx
> IslCtx
;
44 isl::union_map OldSchedule
;
48 explicit FlattenSchedule() : ScopPass(ID
) {}
50 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
51 AU
.addRequiredTransitive
<ScopInfoRegionPass
>();
55 bool runOnScop(Scop
&S
) override
{
56 // Keep a reference to isl_ctx to ensure that it is not freed before we free
58 IslCtx
= S
.getSharedIslCtx();
60 LLVM_DEBUG(dbgs() << "Going to flatten old schedule:\n");
61 OldSchedule
= S
.getSchedule();
62 LLVM_DEBUG(printSchedule(dbgs(), OldSchedule
, 2));
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));
69 auto NewSchedule
= flattenSchedule(RestrictedOldSchedule
);
71 LLVM_DEBUG(dbgs() << "Flattened new schedule:\n");
72 LLVM_DEBUG(printSchedule(dbgs(), NewSchedule
, 2));
74 NewSchedule
= NewSchedule
.gist_domain(Domains
);
75 LLVM_DEBUG(dbgs() << "Gisted, flattened new schedule:\n");
76 LLVM_DEBUG(printSchedule(dbgs(), NewSchedule
, 2));
78 S
.setSchedule(NewSchedule
);
82 void printScop(raw_ostream
&OS
, Scop
&S
) const override
{
83 OS
<< "Schedule before flattening {\n";
84 printSchedule(OS
, OldSchedule
, 4);
87 OS
<< "Schedule after flattening {\n";
88 printSchedule(OS
, S
.getSchedule(), 4);
92 void releaseMemory() override
{
98 char FlattenSchedule::ID
;
100 /// Print result from FlattenSchedule.
101 class FlattenSchedulePrinterLegacyPass final
: public ScopPass
{
105 FlattenSchedulePrinterLegacyPass()
106 : FlattenSchedulePrinterLegacyPass(outs()){};
107 explicit FlattenSchedulePrinterLegacyPass(llvm::raw_ostream
&OS
)
108 : ScopPass(ID
), OS(OS
) {}
110 bool runOnScop(Scop
&S
) override
{
111 FlattenSchedule
&P
= getAnalysis
<FlattenSchedule
>();
113 OS
<< "Printing analysis '" << P
.getPassName() << "' for region: '"
114 << S
.getRegion().getNameStr() << "' in function '"
115 << S
.getFunction().getName() << "':\n";
121 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
122 ScopPass::getAnalysisUsage(AU
);
123 AU
.addRequired
<FlattenSchedule
>();
124 AU
.setPreservesAll();
128 llvm::raw_ostream
&OS
;
131 char FlattenSchedulePrinterLegacyPass::ID
= 0;
132 } // anonymous namespace
134 Pass
*polly::createFlattenSchedulePass() { return new FlattenSchedule(); }
136 Pass
*polly::createFlattenSchedulePrinterLegacyPass(llvm::raw_ostream
&OS
) {
137 return new FlattenSchedulePrinterLegacyPass(OS
);
140 INITIALIZE_PASS_BEGIN(FlattenSchedule
, "polly-flatten-schedule",
141 "Polly - Flatten schedule", false, false)
142 INITIALIZE_PASS_END(FlattenSchedule
, "polly-flatten-schedule",
143 "Polly - Flatten schedule", false, false)
145 INITIALIZE_PASS_BEGIN(FlattenSchedulePrinterLegacyPass
,
146 "polly-print-flatten-schedule",
147 "Polly - Print flattened schedule", false, false)
148 INITIALIZE_PASS_DEPENDENCY(FlattenSchedule
)
149 INITIALIZE_PASS_END(FlattenSchedulePrinterLegacyPass
,
150 "polly-print-flatten-schedule",
151 "Polly - Print flattened schedule", false, false)