1 //===------ RegisterPasses.cpp - Add the Polly Passes to default passes --===//
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 // This file composes the individual LLVM-IR passes provided by Polly to a
10 // functional polyhedral optimizer. The polyhedral optimizer is automatically
11 // made available to LLVM based compilers by loading the Polly shared library
12 // into such a compiler.
14 // The Polly optimizer is made available by executing a static constructor that
15 // registers the individual Polly passes in the LLVM pass manager builder. The
16 // passes are registered such that the default behaviour of the compiler is not
17 // changed, but that the flag '-polly' provided at optimization level '-O3'
18 // enables additional polyhedral optimizations.
19 //===----------------------------------------------------------------------===//
21 #include "polly/RegisterPasses.h"
22 #include "polly/Canonicalization.h"
23 #include "polly/CodeGen/CodeGeneration.h"
24 #include "polly/CodeGen/IslAst.h"
25 #include "polly/CodePreparation.h"
26 #include "polly/DeLICM.h"
27 #include "polly/DeadCodeElimination.h"
28 #include "polly/DependenceInfo.h"
29 #include "polly/ForwardOpTree.h"
30 #include "polly/JSONExporter.h"
31 #include "polly/LinkAllPasses.h"
32 #include "polly/MaximalStaticExpansion.h"
33 #include "polly/PolyhedralInfo.h"
34 #include "polly/PruneUnprofitable.h"
35 #include "polly/ScheduleOptimizer.h"
36 #include "polly/ScopDetection.h"
37 #include "polly/ScopGraphPrinter.h"
38 #include "polly/ScopInfo.h"
39 #include "polly/Simplify.h"
40 #include "polly/Support/DumpFunctionPass.h"
41 #include "polly/Support/DumpModulePass.h"
42 #include "llvm/Analysis/CFGPrinter.h"
43 #include "llvm/IR/LegacyPassManager.h"
44 #include "llvm/IR/PassManager.h"
45 #include "llvm/IR/Verifier.h"
46 #include "llvm/Passes/PassBuilder.h"
47 #include "llvm/Passes/PassPlugin.h"
48 #include "llvm/Support/CommandLine.h"
49 #include "llvm/Support/TargetSelect.h"
50 #include "llvm/Transforms/IPO.h"
52 namespace cl
= llvm::cl
;
54 using llvm::FunctionPassManager
;
55 using llvm::OptimizationLevel
;
56 using llvm::PassBuilder
;
57 using llvm::PassInstrumentationCallbacks
;
59 cl::OptionCategory
PollyCategory("Polly Options",
60 "Configure the polly loop optimizer");
65 cl::desc("Enable the polly optimizer (with -O1, -O2 or -O3)"),
66 cl::cat(PollyCategory
));
68 static cl::opt
<bool> PollyDetectOnly(
69 "polly-only-scop-detection",
70 cl::desc("Only run scop detection, but no other optimizations"),
71 cl::cat(PollyCategory
));
73 enum PassPositionChoice
{ POSITION_EARLY
, POSITION_BEFORE_VECTORIZER
};
75 enum OptimizerChoice
{ OPTIMIZER_NONE
, OPTIMIZER_ISL
};
77 static cl::opt
<PassPositionChoice
> PassPosition(
78 "polly-position", cl::desc("Where to run polly in the pass pipeline"),
79 cl::values(clEnumValN(POSITION_EARLY
, "early", "Before everything"),
80 clEnumValN(POSITION_BEFORE_VECTORIZER
, "before-vectorizer",
81 "Right before the vectorizer")),
82 cl::Hidden
, cl::init(POSITION_BEFORE_VECTORIZER
), cl::cat(PollyCategory
));
84 static cl::opt
<OptimizerChoice
>
85 Optimizer("polly-optimizer", cl::desc("Select the scheduling optimizer"),
86 cl::values(clEnumValN(OPTIMIZER_NONE
, "none", "No optimizer"),
87 clEnumValN(OPTIMIZER_ISL
, "isl",
88 "The isl scheduling optimizer")),
89 cl::Hidden
, cl::init(OPTIMIZER_ISL
), cl::cat(PollyCategory
));
91 enum CodeGenChoice
{ CODEGEN_FULL
, CODEGEN_AST
, CODEGEN_NONE
};
92 static cl::opt
<CodeGenChoice
> CodeGeneration(
93 "polly-code-generation", cl::desc("How much code-generation to perform"),
94 cl::values(clEnumValN(CODEGEN_FULL
, "full", "AST and IR generation"),
95 clEnumValN(CODEGEN_AST
, "ast", "Only AST generation"),
96 clEnumValN(CODEGEN_NONE
, "none", "No code generation")),
97 cl::Hidden
, cl::init(CODEGEN_FULL
), cl::cat(PollyCategory
));
99 VectorizerChoice PollyVectorizerChoice
;
101 static cl::opt
<VectorizerChoice
, true> Vectorizer(
102 "polly-vectorizer", cl::desc("Select the vectorization strategy"),
104 clEnumValN(VECTORIZER_NONE
, "none", "No Vectorization"),
106 VECTORIZER_STRIPMINE
, "stripmine",
107 "Strip-mine outer loops for the loop-vectorizer to trigger")),
108 cl::location(PollyVectorizerChoice
), cl::init(VECTORIZER_NONE
),
109 cl::cat(PollyCategory
));
111 static cl::opt
<bool> ImportJScop(
113 cl::desc("Import the polyhedral description of the detected Scops"),
114 cl::Hidden
, cl::cat(PollyCategory
));
116 static cl::opt
<bool> FullyIndexedStaticExpansion(
118 cl::desc("Fully expand the memory accesses of the detected Scops"),
119 cl::Hidden
, cl::cat(PollyCategory
));
121 static cl::opt
<bool> ExportJScop(
123 cl::desc("Export the polyhedral description of the detected Scops"),
124 cl::Hidden
, cl::cat(PollyCategory
));
126 static cl::opt
<bool> DeadCodeElim("polly-run-dce",
127 cl::desc("Run the dead code elimination"),
128 cl::Hidden
, cl::cat(PollyCategory
));
130 static cl::opt
<bool> PollyViewer(
132 cl::desc("Highlight the code regions that will be optimized in a "
133 "(CFG BBs and LLVM-IR instructions)"),
134 cl::cat(PollyCategory
));
136 static cl::opt
<bool> PollyOnlyViewer(
138 cl::desc("Highlight the code regions that will be optimized in "
140 cl::init(false), cl::cat(PollyCategory
));
143 PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"),
144 cl::Hidden
, cl::value_desc("Run the Polly DOT printer at -O3"),
145 cl::init(false), cl::cat(PollyCategory
));
147 static cl::opt
<bool> PollyOnlyPrinter(
149 cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden
,
150 cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"),
151 cl::init(false), cl::cat(PollyCategory
));
154 CFGPrinter("polly-view-cfg",
155 cl::desc("Show the Polly CFG right after code generation"),
156 cl::Hidden
, cl::init(false), cl::cat(PollyCategory
));
159 EnableForwardOpTree("polly-enable-optree",
160 cl::desc("Enable operand tree forwarding"), cl::Hidden
,
161 cl::init(true), cl::cat(PollyCategory
));
164 DumpBefore("polly-dump-before",
165 cl::desc("Dump module before Polly transformations into a file "
166 "suffixed with \"-before\""),
167 cl::init(false), cl::cat(PollyCategory
));
169 static cl::list
<std::string
> DumpBeforeFile(
170 "polly-dump-before-file",
171 cl::desc("Dump module before Polly transformations to the given file"),
172 cl::cat(PollyCategory
));
175 DumpAfter("polly-dump-after",
176 cl::desc("Dump module after Polly transformations into a file "
177 "suffixed with \"-after\""),
178 cl::init(false), cl::cat(PollyCategory
));
180 static cl::list
<std::string
> DumpAfterFile(
181 "polly-dump-after-file",
182 cl::desc("Dump module after Polly transformations to the given file"),
183 cl::cat(PollyCategory
));
186 EnableDeLICM("polly-enable-delicm",
187 cl::desc("Eliminate scalar loop carried dependences"),
188 cl::Hidden
, cl::init(true), cl::cat(PollyCategory
));
191 EnableSimplify("polly-enable-simplify",
192 cl::desc("Simplify SCoP after optimizations"),
193 cl::init(true), cl::cat(PollyCategory
));
195 static cl::opt
<bool> EnablePruneUnprofitable(
196 "polly-enable-prune-unprofitable",
197 cl::desc("Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden
,
198 cl::init(true), cl::cat(PollyCategory
));
202 /// Initialize Polly passes when library is loaded.
204 /// We use the constructor of a statically declared object to initialize the
205 /// different Polly passes right after the Polly library is loaded. This ensures
206 /// that the Polly passes are available e.g. in the 'opt' tool.
207 struct StaticInitializer
{
208 StaticInitializer() {
209 llvm::PassRegistry
&Registry
= *llvm::PassRegistry::getPassRegistry();
210 polly::initializePollyPasses(Registry
);
213 static StaticInitializer InitializeEverything
;
214 } // end of anonymous namespace.
216 void initializePollyPasses(llvm::PassRegistry
&Registry
) {
217 initializeCodeGenerationPass(Registry
);
219 initializeCodePreparationPass(Registry
);
220 initializeDeadCodeElimWrapperPassPass(Registry
);
221 initializeDependenceInfoPass(Registry
);
222 initializeDependenceInfoPrinterLegacyPassPass(Registry
);
223 initializeDependenceInfoWrapperPassPass(Registry
);
224 initializeDependenceInfoPrinterLegacyFunctionPassPass(Registry
);
225 initializeJSONExporterPass(Registry
);
226 initializeJSONImporterPass(Registry
);
227 initializeJSONImporterPrinterLegacyPassPass(Registry
);
228 initializeMaximalStaticExpanderWrapperPassPass(Registry
);
229 initializeIslAstInfoWrapperPassPass(Registry
);
230 initializeIslAstInfoPrinterLegacyPassPass(Registry
);
231 initializeIslScheduleOptimizerWrapperPassPass(Registry
);
232 initializeIslScheduleOptimizerPrinterLegacyPassPass(Registry
);
233 initializePollyCanonicalizePass(Registry
);
234 initializePolyhedralInfoPass(Registry
);
235 initializePolyhedralInfoPrinterLegacyPassPass(Registry
);
236 initializeScopDetectionWrapperPassPass(Registry
);
237 initializeScopDetectionPrinterLegacyPassPass(Registry
);
238 initializeScopInlinerPass(Registry
);
239 initializeScopInfoRegionPassPass(Registry
);
240 initializeScopInfoPrinterLegacyRegionPassPass(Registry
);
241 initializeScopInfoWrapperPassPass(Registry
);
242 initializeScopInfoPrinterLegacyFunctionPassPass(Registry
);
243 initializeFlattenSchedulePass(Registry
);
244 initializeFlattenSchedulePrinterLegacyPassPass(Registry
);
245 initializeForwardOpTreeWrapperPassPass(Registry
);
246 initializeForwardOpTreePrinterLegacyPassPass(Registry
);
247 initializeDeLICMWrapperPassPass(Registry
);
248 initializeDeLICMPrinterLegacyPassPass(Registry
);
249 initializeSimplifyWrapperPassPass(Registry
);
250 initializeSimplifyPrinterLegacyPassPass(Registry
);
251 initializeDumpModuleWrapperPassPass(Registry
);
252 initializePruneUnprofitableWrapperPassPass(Registry
);
255 static bool shouldEnablePollyForOptimization() { return PollyEnabled
; }
257 static bool shouldEnablePollyForDiagnostic() {
258 // FIXME: PollyTrackFailures is user-controlled, should not be set
260 if (PollyOnlyPrinter
|| PollyPrinter
|| PollyOnlyViewer
|| PollyViewer
)
261 PollyTrackFailures
= true;
263 return PollyOnlyPrinter
|| PollyPrinter
|| PollyOnlyViewer
|| PollyViewer
||
267 /// Register Polly passes such that they form a polyhedral optimizer.
269 /// The individual Polly passes are registered in the pass manager such that
270 /// they form a full polyhedral optimizer. The flow of the optimizer starts with
271 /// a set of preparing transformations that canonicalize the LLVM-IR such that
272 /// the LLVM-IR is easier for us to understand and to optimizes. On the
273 /// canonicalized LLVM-IR we first run the ScopDetection pass, which detects
274 /// static control flow regions. Those regions are then translated by the
275 /// ScopInfo pass into a polyhedral representation. As a next step, a scheduling
276 /// optimizer is run on the polyhedral representation and finally the optimized
277 /// polyhedral representation is code generated back to LLVM-IR.
279 /// Besides this core functionality, we optionally schedule passes that provide
280 /// a graphical view of the scops (Polly[Only]Viewer, Polly[Only]Printer), that
281 /// allow the export/import of the polyhedral representation
282 /// (JSCON[Exporter|Importer]) or that show the cfg after code generation.
284 /// For certain parts of the Polly optimizer, several alternatives are provided:
286 /// As scheduling optimizer we support the isl scheduling optimizer
287 /// (http://freecode.com/projects/isl).
288 /// It is also possible to run Polly with no optimizer. This mode is mainly
289 /// provided to analyze the run and compile time changes caused by the
290 /// scheduling optimizer.
292 /// Polly supports the isl internal code generator.
294 /// Add the pass sequence required for Polly to the New Pass Manager.
296 /// @param PM The pass manager itself.
297 /// @param Level The optimization level. Used for the cleanup of Polly's
299 /// @param EnableForOpt Whether to add Polly IR transformations. If False, only
300 /// the analysis passes are added, skipping Polly itself.
301 /// The IR may still be modified.
302 static void buildCommonPollyPipeline(FunctionPassManager
&PM
,
303 OptimizationLevel Level
,
308 PM
.addPass(CodePreparationPass());
310 // TODO add utility passes for the various command line options, once they're
313 if (PollyDetectOnly
) {
314 // Don't add more passes other than the ScopPassManager's detection passes.
315 PM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
320 PM
.addPass(ScopViewer());
322 PM
.addPass(ScopOnlyViewer());
324 PM
.addPass(ScopPrinter());
325 if (PollyOnlyPrinter
)
326 PM
.addPass(ScopOnlyPrinter());
328 SPM
.addPass(SimplifyPass(0));
329 if (EnableForwardOpTree
)
330 SPM
.addPass(ForwardOpTreePass());
332 SPM
.addPass(DeLICMPass());
334 SPM
.addPass(SimplifyPass(1));
337 SPM
.addPass(JSONImportPass());
340 SPM
.addPass(DeadCodeElimPass());
342 if (FullyIndexedStaticExpansion
)
343 SPM
.addPass(MaximalStaticExpansionPass());
345 if (EnablePruneUnprofitable
)
346 SPM
.addPass(PruneUnprofitablePass());
350 break; /* Do nothing */
352 SPM
.addPass(IslScheduleOptimizerPass());
357 SPM
.addPass(JSONExportPass());
362 switch (CodeGeneration
) {
365 llvm::RequireAnalysisPass
<IslAstAnalysis
, Scop
, ScopAnalysisManager
,
366 ScopStandardAnalysisResults
&,
370 SPM
.addPass(CodeGenerationPass());
376 PM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
377 PM
.addPass(PB
.buildFunctionSimplificationPipeline(
378 Level
, llvm::ThinOrFullLTOPhase::None
)); // Cleanup
381 PM
.addPass(llvm::CFGPrinterPass());
384 static void buildEarlyPollyPipeline(llvm::ModulePassManager
&MPM
,
385 llvm::OptimizationLevel Level
) {
387 shouldEnablePollyForOptimization() && Level
.isOptimizingForSpeed();
388 if (!shouldEnablePollyForDiagnostic() && !EnableForOpt
)
391 FunctionPassManager FPM
= buildCanonicalicationPassesForNPM(MPM
, Level
);
393 if (DumpBefore
|| !DumpBeforeFile
.empty()) {
394 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
397 MPM
.addPass(DumpModulePass("-before", true));
398 for (auto &Filename
: DumpBeforeFile
)
399 MPM
.addPass(DumpModulePass(Filename
, false));
401 FPM
= FunctionPassManager();
404 buildCommonPollyPipeline(FPM
, Level
, EnableForOpt
);
405 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
408 MPM
.addPass(DumpModulePass("-after", true));
409 for (auto &Filename
: DumpAfterFile
)
410 MPM
.addPass(DumpModulePass(Filename
, false));
413 static void buildLatePollyPipeline(FunctionPassManager
&PM
,
414 llvm::OptimizationLevel Level
) {
416 shouldEnablePollyForOptimization() && Level
.isOptimizingForSpeed();
417 if (!shouldEnablePollyForDiagnostic() && !EnableForOpt
)
421 PM
.addPass(DumpFunctionPass("-before"));
422 if (!DumpBeforeFile
.empty())
423 llvm::report_fatal_error(
424 "Option -polly-dump-before-file at -polly-position=late "
425 "not supported with NPM",
428 buildCommonPollyPipeline(PM
, Level
, EnableForOpt
);
431 PM
.addPass(DumpFunctionPass("-after"));
432 if (!DumpAfterFile
.empty())
433 llvm::report_fatal_error(
434 "Option -polly-dump-after-file at -polly-position=late "
435 "not supported with NPM",
439 static OwningScopAnalysisManagerFunctionProxy
440 createScopAnalyses(FunctionAnalysisManager
&FAM
,
441 PassInstrumentationCallbacks
*PIC
) {
442 OwningScopAnalysisManagerFunctionProxy Proxy
;
443 #define SCOP_ANALYSIS(NAME, CREATE_PASS) \
444 Proxy.getManager().registerPass([PIC] { \
446 return CREATE_PASS; \
448 #include "PollyPasses.def"
450 Proxy
.getManager().registerPass(
451 [&FAM
] { return FunctionAnalysisManagerScopProxy(FAM
); });
455 static void registerFunctionAnalyses(FunctionAnalysisManager
&FAM
,
456 PassInstrumentationCallbacks
*PIC
) {
458 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
459 FAM.registerPass([] { return CREATE_PASS; });
461 #include "PollyPasses.def"
463 FAM
.registerPass([&FAM
, PIC
] { return createScopAnalyses(FAM
, PIC
); });
467 parseFunctionPipeline(StringRef Name
, FunctionPassManager
&FPM
,
468 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) {
469 if (llvm::parseAnalysisUtilityPasses
<OwningScopAnalysisManagerFunctionProxy
>(
470 "polly-scop-analyses", Name
, FPM
))
473 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
474 if (llvm::parseAnalysisUtilityPasses< \
475 std::remove_reference<decltype(CREATE_PASS)>::type>(NAME, Name, \
479 #define FUNCTION_PASS(NAME, CREATE_PASS) \
480 if (Name == NAME) { \
481 FPM.addPass(CREATE_PASS); \
485 #include "PollyPasses.def"
489 static bool parseScopPass(StringRef Name
, ScopPassManager
&SPM
,
490 PassInstrumentationCallbacks
*PIC
) {
491 #define SCOP_ANALYSIS(NAME, CREATE_PASS) \
492 if (llvm::parseAnalysisUtilityPasses< \
493 std::remove_reference<decltype(CREATE_PASS)>::type>(NAME, Name, \
497 #define SCOP_PASS(NAME, CREATE_PASS) \
498 if (Name == NAME) { \
499 SPM.addPass(CREATE_PASS); \
503 #include "PollyPasses.def"
508 static bool parseScopPipeline(StringRef Name
, FunctionPassManager
&FPM
,
509 PassInstrumentationCallbacks
*PIC
,
510 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) {
513 if (!Pipeline
.empty()) {
515 for (const auto &E
: Pipeline
)
516 if (!parseScopPass(E
.Name
, SPM
, PIC
))
518 FPM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
523 static bool isScopPassName(StringRef Name
) {
524 #define SCOP_ANALYSIS(NAME, CREATE_PASS) \
525 if (Name == "require<" NAME ">") \
527 if (Name == "invalidate<" NAME ">") \
530 #define SCOP_PASS(NAME, CREATE_PASS) \
534 #include "PollyPasses.def"
540 parseTopLevelPipeline(llvm::ModulePassManager
&MPM
,
541 PassInstrumentationCallbacks
*PIC
,
542 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) {
543 std::vector
<PassBuilder::PipelineElement
> FullPipeline
;
544 StringRef FirstName
= Pipeline
.front().Name
;
546 if (!isScopPassName(FirstName
))
549 FunctionPassManager FPM
;
552 for (auto &Element
: Pipeline
) {
553 auto &Name
= Element
.Name
;
554 auto &InnerPipeline
= Element
.InnerPipeline
;
555 if (!InnerPipeline
.empty()) // Scop passes don't have inner pipelines
557 if (!parseScopPass(Name
, SPM
, PIC
))
561 FPM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
562 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
567 /// Register Polly to be available as an optimizer
570 /// We can currently run Polly at two different points int the pass manager.
571 /// a) very early, b) right before the vectorizer.
573 /// The default is currently a), to register Polly such that it runs as early as
574 /// possible. This has several implications:
576 /// 1) We need to schedule more canonicalization passes
578 /// As nothing is run before Polly, it is necessary to run a set of preparing
579 /// transformations before Polly to canonicalize the LLVM-IR and to allow
580 /// Polly to detect and understand the code.
582 /// 2) We get the full -O3 optimization sequence after Polly
584 /// The LLVM-IR that is generated by Polly has been optimized on a high level,
585 /// but it may be rather inefficient on the lower/scalar level. By scheduling
586 /// Polly before all other passes, we have the full sequence of -O3
587 /// optimizations behind us, such that inefficiencies on the low level can
588 /// be optimized away.
590 /// We are currently evaluating the benefit or running Polly at b). b) is nice
591 /// as everything is fully inlined and canonicalized, but we need to be able to
592 /// handle LICMed code to make it useful.
593 void registerPollyPasses(PassBuilder
&PB
) {
594 PassInstrumentationCallbacks
*PIC
= PB
.getPassInstrumentationCallbacks();
595 PB
.registerAnalysisRegistrationCallback([PIC
](FunctionAnalysisManager
&FAM
) {
596 registerFunctionAnalyses(FAM
, PIC
);
598 PB
.registerPipelineParsingCallback(parseFunctionPipeline
);
599 PB
.registerPipelineParsingCallback(
600 [PIC
](StringRef Name
, FunctionPassManager
&FPM
,
601 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) -> bool {
602 return parseScopPipeline(Name
, FPM
, PIC
, Pipeline
);
604 PB
.registerParseTopLevelPipelineCallback(
605 [PIC
](llvm::ModulePassManager
&MPM
,
606 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) -> bool {
607 return parseTopLevelPipeline(MPM
, PIC
, Pipeline
);
610 switch (PassPosition
) {
612 PB
.registerPipelineStartEPCallback(buildEarlyPollyPipeline
);
614 case POSITION_BEFORE_VECTORIZER
:
615 PB
.registerVectorizerStartEPCallback(buildLatePollyPipeline
);
621 llvm::PassPluginLibraryInfo
getPollyPluginInfo() {
622 return {LLVM_PLUGIN_API_VERSION
, "Polly", LLVM_VERSION_STRING
,
623 polly::registerPollyPasses
};