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/Config/llvm-config.h" // for LLVM_VERSION_STRING
44 #include "llvm/IR/LegacyPassManager.h"
45 #include "llvm/IR/PassManager.h"
46 #include "llvm/IR/Verifier.h"
47 #include "llvm/Passes/PassBuilder.h"
48 #include "llvm/Passes/PassPlugin.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/TargetSelect.h"
51 #include "llvm/Transforms/IPO.h"
53 namespace cl
= llvm::cl
;
55 using llvm::FunctionPassManager
;
56 using llvm::OptimizationLevel
;
57 using llvm::PassBuilder
;
58 using llvm::PassInstrumentationCallbacks
;
60 cl::OptionCategory
PollyCategory("Polly Options",
61 "Configure the polly loop optimizer");
66 cl::desc("Enable the polly optimizer (with -O1, -O2 or -O3)"),
67 cl::cat(PollyCategory
));
69 static cl::opt
<bool> PollyDetectOnly(
70 "polly-only-scop-detection",
71 cl::desc("Only run scop detection, but no other optimizations"),
72 cl::cat(PollyCategory
));
74 enum PassPositionChoice
{ POSITION_EARLY
, POSITION_BEFORE_VECTORIZER
};
76 enum OptimizerChoice
{ OPTIMIZER_NONE
, OPTIMIZER_ISL
};
78 static cl::opt
<PassPositionChoice
> PassPosition(
79 "polly-position", cl::desc("Where to run polly in the pass pipeline"),
80 cl::values(clEnumValN(POSITION_EARLY
, "early", "Before everything"),
81 clEnumValN(POSITION_BEFORE_VECTORIZER
, "before-vectorizer",
82 "Right before the vectorizer")),
83 cl::Hidden
, cl::init(POSITION_BEFORE_VECTORIZER
), cl::cat(PollyCategory
));
85 static cl::opt
<OptimizerChoice
>
86 Optimizer("polly-optimizer", cl::desc("Select the scheduling optimizer"),
87 cl::values(clEnumValN(OPTIMIZER_NONE
, "none", "No optimizer"),
88 clEnumValN(OPTIMIZER_ISL
, "isl",
89 "The isl scheduling optimizer")),
90 cl::Hidden
, cl::init(OPTIMIZER_ISL
), cl::cat(PollyCategory
));
92 enum CodeGenChoice
{ CODEGEN_FULL
, CODEGEN_AST
, CODEGEN_NONE
};
93 static cl::opt
<CodeGenChoice
> CodeGeneration(
94 "polly-code-generation", cl::desc("How much code-generation to perform"),
95 cl::values(clEnumValN(CODEGEN_FULL
, "full", "AST and IR generation"),
96 clEnumValN(CODEGEN_AST
, "ast", "Only AST generation"),
97 clEnumValN(CODEGEN_NONE
, "none", "No code generation")),
98 cl::Hidden
, cl::init(CODEGEN_FULL
), cl::cat(PollyCategory
));
100 VectorizerChoice PollyVectorizerChoice
;
102 static cl::opt
<VectorizerChoice
, true> Vectorizer(
103 "polly-vectorizer", cl::desc("Select the vectorization strategy"),
105 clEnumValN(VECTORIZER_NONE
, "none", "No Vectorization"),
107 VECTORIZER_STRIPMINE
, "stripmine",
108 "Strip-mine outer loops for the loop-vectorizer to trigger")),
109 cl::location(PollyVectorizerChoice
), cl::init(VECTORIZER_NONE
),
110 cl::cat(PollyCategory
));
112 static cl::opt
<bool> ImportJScop(
114 cl::desc("Import the polyhedral description of the detected Scops"),
115 cl::Hidden
, cl::cat(PollyCategory
));
117 static cl::opt
<bool> FullyIndexedStaticExpansion(
119 cl::desc("Fully expand the memory accesses of the detected Scops"),
120 cl::Hidden
, cl::cat(PollyCategory
));
122 static cl::opt
<bool> ExportJScop(
124 cl::desc("Export the polyhedral description of the detected Scops"),
125 cl::Hidden
, cl::cat(PollyCategory
));
127 static cl::opt
<bool> DeadCodeElim("polly-run-dce",
128 cl::desc("Run the dead code elimination"),
129 cl::Hidden
, cl::cat(PollyCategory
));
131 static cl::opt
<bool> PollyViewer(
133 cl::desc("Highlight the code regions that will be optimized in a "
134 "(CFG BBs and LLVM-IR instructions)"),
135 cl::cat(PollyCategory
));
137 static cl::opt
<bool> PollyOnlyViewer(
139 cl::desc("Highlight the code regions that will be optimized in "
141 cl::init(false), cl::cat(PollyCategory
));
144 PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"),
145 cl::Hidden
, cl::value_desc("Run the Polly DOT printer at -O3"),
146 cl::init(false), cl::cat(PollyCategory
));
148 static cl::opt
<bool> PollyOnlyPrinter(
150 cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden
,
151 cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"),
152 cl::init(false), cl::cat(PollyCategory
));
155 CFGPrinter("polly-view-cfg",
156 cl::desc("Show the Polly CFG right after code generation"),
157 cl::Hidden
, cl::init(false), cl::cat(PollyCategory
));
160 EnableForwardOpTree("polly-enable-optree",
161 cl::desc("Enable operand tree forwarding"), cl::Hidden
,
162 cl::init(true), cl::cat(PollyCategory
));
165 DumpBefore("polly-dump-before",
166 cl::desc("Dump module before Polly transformations into a file "
167 "suffixed with \"-before\""),
168 cl::init(false), cl::cat(PollyCategory
));
170 static cl::list
<std::string
> DumpBeforeFile(
171 "polly-dump-before-file",
172 cl::desc("Dump module before Polly transformations to the given file"),
173 cl::cat(PollyCategory
));
176 DumpAfter("polly-dump-after",
177 cl::desc("Dump module after Polly transformations into a file "
178 "suffixed with \"-after\""),
179 cl::init(false), cl::cat(PollyCategory
));
181 static cl::list
<std::string
> DumpAfterFile(
182 "polly-dump-after-file",
183 cl::desc("Dump module after Polly transformations to the given file"),
184 cl::cat(PollyCategory
));
187 EnableDeLICM("polly-enable-delicm",
188 cl::desc("Eliminate scalar loop carried dependences"),
189 cl::Hidden
, cl::init(true), cl::cat(PollyCategory
));
192 EnableSimplify("polly-enable-simplify",
193 cl::desc("Simplify SCoP after optimizations"),
194 cl::init(true), cl::cat(PollyCategory
));
196 static cl::opt
<bool> EnablePruneUnprofitable(
197 "polly-enable-prune-unprofitable",
198 cl::desc("Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden
,
199 cl::init(true), cl::cat(PollyCategory
));
203 /// Initialize Polly passes when library is loaded.
205 /// We use the constructor of a statically declared object to initialize the
206 /// different Polly passes right after the Polly library is loaded. This ensures
207 /// that the Polly passes are available e.g. in the 'opt' tool.
208 struct StaticInitializer
{
209 StaticInitializer() {
210 llvm::PassRegistry
&Registry
= *llvm::PassRegistry::getPassRegistry();
211 polly::initializePollyPasses(Registry
);
214 static StaticInitializer InitializeEverything
;
215 } // end of anonymous namespace.
217 void initializePollyPasses(llvm::PassRegistry
&Registry
) {
218 initializeCodeGenerationPass(Registry
);
220 initializeCodePreparationPass(Registry
);
221 initializeDeadCodeElimWrapperPassPass(Registry
);
222 initializeDependenceInfoPass(Registry
);
223 initializeDependenceInfoPrinterLegacyPassPass(Registry
);
224 initializeDependenceInfoWrapperPassPass(Registry
);
225 initializeDependenceInfoPrinterLegacyFunctionPassPass(Registry
);
226 initializeJSONExporterPass(Registry
);
227 initializeJSONImporterPass(Registry
);
228 initializeJSONImporterPrinterLegacyPassPass(Registry
);
229 initializeMaximalStaticExpanderWrapperPassPass(Registry
);
230 initializeIslAstInfoWrapperPassPass(Registry
);
231 initializeIslAstInfoPrinterLegacyPassPass(Registry
);
232 initializeIslScheduleOptimizerWrapperPassPass(Registry
);
233 initializeIslScheduleOptimizerPrinterLegacyPassPass(Registry
);
234 initializePollyCanonicalizePass(Registry
);
235 initializePolyhedralInfoPass(Registry
);
236 initializePolyhedralInfoPrinterLegacyPassPass(Registry
);
237 initializeScopDetectionWrapperPassPass(Registry
);
238 initializeScopDetectionPrinterLegacyPassPass(Registry
);
239 initializeScopInlinerPass(Registry
);
240 initializeScopInfoRegionPassPass(Registry
);
241 initializeScopInfoPrinterLegacyRegionPassPass(Registry
);
242 initializeScopInfoWrapperPassPass(Registry
);
243 initializeScopInfoPrinterLegacyFunctionPassPass(Registry
);
244 initializeFlattenSchedulePass(Registry
);
245 initializeFlattenSchedulePrinterLegacyPassPass(Registry
);
246 initializeForwardOpTreeWrapperPassPass(Registry
);
247 initializeForwardOpTreePrinterLegacyPassPass(Registry
);
248 initializeDeLICMWrapperPassPass(Registry
);
249 initializeDeLICMPrinterLegacyPassPass(Registry
);
250 initializeSimplifyWrapperPassPass(Registry
);
251 initializeSimplifyPrinterLegacyPassPass(Registry
);
252 initializeDumpModuleWrapperPassPass(Registry
);
253 initializePruneUnprofitableWrapperPassPass(Registry
);
256 static bool shouldEnablePollyForOptimization() { return PollyEnabled
; }
258 static bool shouldEnablePollyForDiagnostic() {
259 // FIXME: PollyTrackFailures is user-controlled, should not be set
261 if (PollyOnlyPrinter
|| PollyPrinter
|| PollyOnlyViewer
|| PollyViewer
)
262 PollyTrackFailures
= true;
264 return PollyOnlyPrinter
|| PollyPrinter
|| PollyOnlyViewer
|| PollyViewer
||
268 /// Register Polly passes such that they form a polyhedral optimizer.
270 /// The individual Polly passes are registered in the pass manager such that
271 /// they form a full polyhedral optimizer. The flow of the optimizer starts with
272 /// a set of preparing transformations that canonicalize the LLVM-IR such that
273 /// the LLVM-IR is easier for us to understand and to optimizes. On the
274 /// canonicalized LLVM-IR we first run the ScopDetection pass, which detects
275 /// static control flow regions. Those regions are then translated by the
276 /// ScopInfo pass into a polyhedral representation. As a next step, a scheduling
277 /// optimizer is run on the polyhedral representation and finally the optimized
278 /// polyhedral representation is code generated back to LLVM-IR.
280 /// Besides this core functionality, we optionally schedule passes that provide
281 /// a graphical view of the scops (Polly[Only]Viewer, Polly[Only]Printer), that
282 /// allow the export/import of the polyhedral representation
283 /// (JSCON[Exporter|Importer]) or that show the cfg after code generation.
285 /// For certain parts of the Polly optimizer, several alternatives are provided:
287 /// As scheduling optimizer we support the isl scheduling optimizer
288 /// (http://freecode.com/projects/isl).
289 /// It is also possible to run Polly with no optimizer. This mode is mainly
290 /// provided to analyze the run and compile time changes caused by the
291 /// scheduling optimizer.
293 /// Polly supports the isl internal code generator.
295 /// Add the pass sequence required for Polly to the New Pass Manager.
297 /// @param PM The pass manager itself.
298 /// @param Level The optimization level. Used for the cleanup of Polly's
300 /// @param EnableForOpt Whether to add Polly IR transformations. If False, only
301 /// the analysis passes are added, skipping Polly itself.
302 /// The IR may still be modified.
303 static void buildCommonPollyPipeline(FunctionPassManager
&PM
,
304 OptimizationLevel Level
,
309 PM
.addPass(CodePreparationPass());
311 // TODO add utility passes for the various command line options, once they're
314 if (PollyDetectOnly
) {
315 // Don't add more passes other than the ScopPassManager's detection passes.
316 PM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
321 PM
.addPass(ScopViewer());
323 PM
.addPass(ScopOnlyViewer());
325 PM
.addPass(ScopPrinter());
326 if (PollyOnlyPrinter
)
327 PM
.addPass(ScopOnlyPrinter());
329 SPM
.addPass(SimplifyPass(0));
330 if (EnableForwardOpTree
)
331 SPM
.addPass(ForwardOpTreePass());
333 SPM
.addPass(DeLICMPass());
335 SPM
.addPass(SimplifyPass(1));
338 SPM
.addPass(JSONImportPass());
341 SPM
.addPass(DeadCodeElimPass());
343 if (FullyIndexedStaticExpansion
)
344 SPM
.addPass(MaximalStaticExpansionPass());
346 if (EnablePruneUnprofitable
)
347 SPM
.addPass(PruneUnprofitablePass());
351 break; /* Do nothing */
353 SPM
.addPass(IslScheduleOptimizerPass());
358 SPM
.addPass(JSONExportPass());
363 switch (CodeGeneration
) {
366 llvm::RequireAnalysisPass
<IslAstAnalysis
, Scop
, ScopAnalysisManager
,
367 ScopStandardAnalysisResults
&,
371 SPM
.addPass(CodeGenerationPass());
377 PM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
378 PM
.addPass(PB
.buildFunctionSimplificationPipeline(
379 Level
, llvm::ThinOrFullLTOPhase::None
)); // Cleanup
382 PM
.addPass(llvm::CFGPrinterPass());
385 static void buildEarlyPollyPipeline(llvm::ModulePassManager
&MPM
,
386 llvm::OptimizationLevel Level
) {
388 shouldEnablePollyForOptimization() && Level
.isOptimizingForSpeed();
389 if (!shouldEnablePollyForDiagnostic() && !EnableForOpt
)
392 FunctionPassManager FPM
= buildCanonicalicationPassesForNPM(MPM
, Level
);
394 if (DumpBefore
|| !DumpBeforeFile
.empty()) {
395 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
398 MPM
.addPass(DumpModulePass("-before", true));
399 for (auto &Filename
: DumpBeforeFile
)
400 MPM
.addPass(DumpModulePass(Filename
, false));
402 FPM
= FunctionPassManager();
405 buildCommonPollyPipeline(FPM
, Level
, EnableForOpt
);
406 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
409 MPM
.addPass(DumpModulePass("-after", true));
410 for (auto &Filename
: DumpAfterFile
)
411 MPM
.addPass(DumpModulePass(Filename
, false));
414 static void buildLatePollyPipeline(FunctionPassManager
&PM
,
415 llvm::OptimizationLevel Level
) {
417 shouldEnablePollyForOptimization() && Level
.isOptimizingForSpeed();
418 if (!shouldEnablePollyForDiagnostic() && !EnableForOpt
)
422 PM
.addPass(DumpFunctionPass("-before"));
423 if (!DumpBeforeFile
.empty())
424 llvm::report_fatal_error(
425 "Option -polly-dump-before-file at -polly-position=late "
426 "not supported with NPM",
429 buildCommonPollyPipeline(PM
, Level
, EnableForOpt
);
432 PM
.addPass(DumpFunctionPass("-after"));
433 if (!DumpAfterFile
.empty())
434 llvm::report_fatal_error(
435 "Option -polly-dump-after-file at -polly-position=late "
436 "not supported with NPM",
440 static OwningScopAnalysisManagerFunctionProxy
441 createScopAnalyses(FunctionAnalysisManager
&FAM
,
442 PassInstrumentationCallbacks
*PIC
) {
443 OwningScopAnalysisManagerFunctionProxy Proxy
;
444 #define SCOP_ANALYSIS(NAME, CREATE_PASS) \
445 Proxy.getManager().registerPass([PIC] { \
447 return CREATE_PASS; \
449 #include "PollyPasses.def"
451 Proxy
.getManager().registerPass(
452 [&FAM
] { return FunctionAnalysisManagerScopProxy(FAM
); });
456 static void registerFunctionAnalyses(FunctionAnalysisManager
&FAM
,
457 PassInstrumentationCallbacks
*PIC
) {
459 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
460 FAM.registerPass([] { return CREATE_PASS; });
462 #include "PollyPasses.def"
464 FAM
.registerPass([&FAM
, PIC
] { return createScopAnalyses(FAM
, PIC
); });
468 parseFunctionPipeline(StringRef Name
, FunctionPassManager
&FPM
,
469 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) {
470 if (llvm::parseAnalysisUtilityPasses
<OwningScopAnalysisManagerFunctionProxy
>(
471 "polly-scop-analyses", Name
, FPM
))
474 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
475 if (llvm::parseAnalysisUtilityPasses< \
476 std::remove_reference<decltype(CREATE_PASS)>::type>(NAME, Name, \
480 #define FUNCTION_PASS(NAME, CREATE_PASS) \
481 if (Name == NAME) { \
482 FPM.addPass(CREATE_PASS); \
486 #include "PollyPasses.def"
490 static bool parseScopPass(StringRef Name
, ScopPassManager
&SPM
,
491 PassInstrumentationCallbacks
*PIC
) {
492 #define SCOP_ANALYSIS(NAME, CREATE_PASS) \
493 if (llvm::parseAnalysisUtilityPasses< \
494 std::remove_reference<decltype(CREATE_PASS)>::type>(NAME, Name, \
498 #define SCOP_PASS(NAME, CREATE_PASS) \
499 if (Name == NAME) { \
500 SPM.addPass(CREATE_PASS); \
504 #include "PollyPasses.def"
509 static bool parseScopPipeline(StringRef Name
, FunctionPassManager
&FPM
,
510 PassInstrumentationCallbacks
*PIC
,
511 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) {
514 if (!Pipeline
.empty()) {
516 for (const auto &E
: Pipeline
)
517 if (!parseScopPass(E
.Name
, SPM
, PIC
))
519 FPM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
524 static bool isScopPassName(StringRef Name
) {
525 #define SCOP_ANALYSIS(NAME, CREATE_PASS) \
526 if (Name == "require<" NAME ">") \
528 if (Name == "invalidate<" NAME ">") \
531 #define SCOP_PASS(NAME, CREATE_PASS) \
535 #include "PollyPasses.def"
541 parseTopLevelPipeline(llvm::ModulePassManager
&MPM
,
542 PassInstrumentationCallbacks
*PIC
,
543 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) {
544 std::vector
<PassBuilder::PipelineElement
> FullPipeline
;
545 StringRef FirstName
= Pipeline
.front().Name
;
547 if (!isScopPassName(FirstName
))
550 FunctionPassManager FPM
;
553 for (auto &Element
: Pipeline
) {
554 auto &Name
= Element
.Name
;
555 auto &InnerPipeline
= Element
.InnerPipeline
;
556 if (!InnerPipeline
.empty()) // Scop passes don't have inner pipelines
558 if (!parseScopPass(Name
, SPM
, PIC
))
562 FPM
.addPass(createFunctionToScopPassAdaptor(std::move(SPM
)));
563 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
568 /// Register Polly to be available as an optimizer
571 /// We can currently run Polly at two different points int the pass manager.
572 /// a) very early, b) right before the vectorizer.
574 /// The default is currently a), to register Polly such that it runs as early as
575 /// possible. This has several implications:
577 /// 1) We need to schedule more canonicalization passes
579 /// As nothing is run before Polly, it is necessary to run a set of preparing
580 /// transformations before Polly to canonicalize the LLVM-IR and to allow
581 /// Polly to detect and understand the code.
583 /// 2) We get the full -O3 optimization sequence after Polly
585 /// The LLVM-IR that is generated by Polly has been optimized on a high level,
586 /// but it may be rather inefficient on the lower/scalar level. By scheduling
587 /// Polly before all other passes, we have the full sequence of -O3
588 /// optimizations behind us, such that inefficiencies on the low level can
589 /// be optimized away.
591 /// We are currently evaluating the benefit or running Polly at b). b) is nice
592 /// as everything is fully inlined and canonicalized, but we need to be able to
593 /// handle LICMed code to make it useful.
594 void registerPollyPasses(PassBuilder
&PB
) {
595 PassInstrumentationCallbacks
*PIC
= PB
.getPassInstrumentationCallbacks();
596 PB
.registerAnalysisRegistrationCallback([PIC
](FunctionAnalysisManager
&FAM
) {
597 registerFunctionAnalyses(FAM
, PIC
);
599 PB
.registerPipelineParsingCallback(parseFunctionPipeline
);
600 PB
.registerPipelineParsingCallback(
601 [PIC
](StringRef Name
, FunctionPassManager
&FPM
,
602 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) -> bool {
603 return parseScopPipeline(Name
, FPM
, PIC
, Pipeline
);
605 PB
.registerParseTopLevelPipelineCallback(
606 [PIC
](llvm::ModulePassManager
&MPM
,
607 ArrayRef
<PassBuilder::PipelineElement
> Pipeline
) -> bool {
608 return parseTopLevelPipeline(MPM
, PIC
, Pipeline
);
611 switch (PassPosition
) {
613 PB
.registerPipelineStartEPCallback(buildEarlyPollyPipeline
);
615 case POSITION_BEFORE_VECTORIZER
:
616 PB
.registerVectorizerStartEPCallback(buildLatePollyPipeline
);
622 llvm::PassPluginLibraryInfo
getPollyPluginInfo() {
623 return {LLVM_PLUGIN_API_VERSION
, "Polly", LLVM_VERSION_STRING
,
624 polly::registerPollyPasses
};