xref: /llvm-project/polly/lib/Support/RegisterPasses.cpp (revision c7ca01b8d7d66e60d59e45340b9592fcd579ed38)
1 //===------ RegisterPasses.cpp - Add the Polly Passes to default passes  --===//
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 // 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.
13 //
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 //===----------------------------------------------------------------------===//
20 
21 #include "polly/RegisterPasses.h"
22 #include "polly/Canonicalization.h"
23 #include "polly/CodeGen/CodeGeneration.h"
24 #include "polly/CodeGen/CodegenCleanup.h"
25 #include "polly/CodeGen/IslAst.h"
26 #include "polly/CodePreparation.h"
27 #include "polly/DeLICM.h"
28 #include "polly/DeadCodeElimination.h"
29 #include "polly/DependenceInfo.h"
30 #include "polly/ForwardOpTree.h"
31 #include "polly/JSONExporter.h"
32 #include "polly/LinkAllPasses.h"
33 #include "polly/MaximalStaticExpansion.h"
34 #include "polly/PolyhedralInfo.h"
35 #include "polly/PruneUnprofitable.h"
36 #include "polly/ScheduleOptimizer.h"
37 #include "polly/ScopDetection.h"
38 #include "polly/ScopGraphPrinter.h"
39 #include "polly/ScopInfo.h"
40 #include "polly/Simplify.h"
41 #include "polly/Support/DumpFunctionPass.h"
42 #include "polly/Support/DumpModulePass.h"
43 #include "llvm/Analysis/CFGPrinter.h"
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"
52 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
53 
54 namespace cl = llvm::cl;
55 
56 using llvm::FunctionPassManager;
57 using llvm::OptimizationLevel;
58 using llvm::PassBuilder;
59 using llvm::PassInstrumentationCallbacks;
60 
61 cl::OptionCategory PollyCategory("Polly Options",
62                                  "Configure the polly loop optimizer");
63 
64 namespace polly {
65 static cl::opt<bool>
66     PollyEnabled("polly",
67                  cl::desc("Enable the polly optimizer (with -O1, -O2 or -O3)"),
68                  cl::cat(PollyCategory));
69 
70 static cl::opt<bool> PollyDetectOnly(
71     "polly-only-scop-detection",
72     cl::desc("Only run scop detection, but no other optimizations"),
73     cl::cat(PollyCategory));
74 
75 enum PassPositionChoice { POSITION_EARLY, POSITION_BEFORE_VECTORIZER };
76 
77 enum OptimizerChoice { OPTIMIZER_NONE, OPTIMIZER_ISL };
78 
79 static cl::opt<PassPositionChoice> PassPosition(
80     "polly-position", cl::desc("Where to run polly in the pass pipeline"),
81     cl::values(clEnumValN(POSITION_EARLY, "early", "Before everything"),
82                clEnumValN(POSITION_BEFORE_VECTORIZER, "before-vectorizer",
83                           "Right before the vectorizer")),
84     cl::Hidden, cl::init(POSITION_BEFORE_VECTORIZER), cl::cat(PollyCategory));
85 
86 static cl::opt<OptimizerChoice>
87     Optimizer("polly-optimizer", cl::desc("Select the scheduling optimizer"),
88               cl::values(clEnumValN(OPTIMIZER_NONE, "none", "No optimizer"),
89                          clEnumValN(OPTIMIZER_ISL, "isl",
90                                     "The isl scheduling optimizer")),
91               cl::Hidden, cl::init(OPTIMIZER_ISL), cl::cat(PollyCategory));
92 
93 enum CodeGenChoice { CODEGEN_FULL, CODEGEN_AST, CODEGEN_NONE };
94 static cl::opt<CodeGenChoice> CodeGeneration(
95     "polly-code-generation", cl::desc("How much code-generation to perform"),
96     cl::values(clEnumValN(CODEGEN_FULL, "full", "AST and IR generation"),
97                clEnumValN(CODEGEN_AST, "ast", "Only AST generation"),
98                clEnumValN(CODEGEN_NONE, "none", "No code generation")),
99     cl::Hidden, cl::init(CODEGEN_FULL), cl::cat(PollyCategory));
100 
101 VectorizerChoice PollyVectorizerChoice;
102 
103 static cl::opt<VectorizerChoice, true> Vectorizer(
104     "polly-vectorizer", cl::desc("Select the vectorization strategy"),
105     cl::values(
106         clEnumValN(VECTORIZER_NONE, "none", "No Vectorization"),
107         clEnumValN(VECTORIZER_POLLY, "polly", "Polly internal vectorizer"),
108         clEnumValN(
109             VECTORIZER_STRIPMINE, "stripmine",
110             "Strip-mine outer loops for the loop-vectorizer to trigger")),
111     cl::location(PollyVectorizerChoice), cl::init(VECTORIZER_NONE),
112     cl::cat(PollyCategory));
113 
114 static cl::opt<bool> ImportJScop(
115     "polly-import",
116     cl::desc("Import the polyhedral description of the detected Scops"),
117     cl::Hidden, cl::cat(PollyCategory));
118 
119 static cl::opt<bool> FullyIndexedStaticExpansion(
120     "polly-enable-mse",
121     cl::desc("Fully expand the memory accesses of the detected Scops"),
122     cl::Hidden, cl::cat(PollyCategory));
123 
124 static cl::opt<bool> ExportJScop(
125     "polly-export",
126     cl::desc("Export the polyhedral description of the detected Scops"),
127     cl::Hidden, cl::cat(PollyCategory));
128 
129 static cl::opt<bool> DeadCodeElim("polly-run-dce",
130                                   cl::desc("Run the dead code elimination"),
131                                   cl::Hidden, cl::cat(PollyCategory));
132 
133 static cl::opt<bool> PollyViewer(
134     "polly-show",
135     cl::desc("Highlight the code regions that will be optimized in a "
136              "(CFG BBs and LLVM-IR instructions)"),
137     cl::cat(PollyCategory));
138 
139 static cl::opt<bool> PollyOnlyViewer(
140     "polly-show-only",
141     cl::desc("Highlight the code regions that will be optimized in "
142              "a (CFG only BBs)"),
143     cl::init(false), cl::cat(PollyCategory));
144 
145 static cl::opt<bool>
146     PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"),
147                  cl::Hidden, cl::value_desc("Run the Polly DOT printer at -O3"),
148                  cl::init(false), cl::cat(PollyCategory));
149 
150 static cl::opt<bool> PollyOnlyPrinter(
151     "polly-dot-only",
152     cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden,
153     cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"),
154     cl::init(false), cl::cat(PollyCategory));
155 
156 static cl::opt<bool>
157     CFGPrinter("polly-view-cfg",
158                cl::desc("Show the Polly CFG right after code generation"),
159                cl::Hidden, cl::init(false), cl::cat(PollyCategory));
160 
161 static cl::opt<bool>
162     EnableForwardOpTree("polly-enable-optree",
163                         cl::desc("Enable operand tree forwarding"), cl::Hidden,
164                         cl::init(true), cl::cat(PollyCategory));
165 
166 static cl::opt<bool>
167     DumpBefore("polly-dump-before",
168                cl::desc("Dump module before Polly transformations into a file "
169                         "suffixed with \"-before\""),
170                cl::init(false), cl::cat(PollyCategory));
171 
172 static cl::list<std::string> DumpBeforeFile(
173     "polly-dump-before-file",
174     cl::desc("Dump module before Polly transformations to the given file"),
175     cl::cat(PollyCategory));
176 
177 static cl::opt<bool>
178     DumpAfter("polly-dump-after",
179               cl::desc("Dump module after Polly transformations into a file "
180                        "suffixed with \"-after\""),
181               cl::init(false), cl::cat(PollyCategory));
182 
183 static cl::list<std::string> DumpAfterFile(
184     "polly-dump-after-file",
185     cl::desc("Dump module after Polly transformations to the given file"),
186     cl::cat(PollyCategory));
187 
188 static cl::opt<bool>
189     EnableDeLICM("polly-enable-delicm",
190                  cl::desc("Eliminate scalar loop carried dependences"),
191                  cl::Hidden, cl::init(true), cl::cat(PollyCategory));
192 
193 static cl::opt<bool>
194     EnableSimplify("polly-enable-simplify",
195                    cl::desc("Simplify SCoP after optimizations"),
196                    cl::init(true), cl::cat(PollyCategory));
197 
198 static cl::opt<bool> EnablePruneUnprofitable(
199     "polly-enable-prune-unprofitable",
200     cl::desc("Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden,
201     cl::init(true), cl::cat(PollyCategory));
202 
203 namespace {
204 
205 /// Initialize Polly passes when library is loaded.
206 ///
207 /// We use the constructor of a statically declared object to initialize the
208 /// different Polly passes right after the Polly library is loaded. This ensures
209 /// that the Polly passes are available e.g. in the 'opt' tool.
210 struct StaticInitializer {
211   StaticInitializer() {
212     llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
213     polly::initializePollyPasses(Registry);
214   }
215 };
216 static StaticInitializer InitializeEverything;
217 } // end of anonymous namespace.
218 
219 void initializePollyPasses(llvm::PassRegistry &Registry) {
220   initializeCodeGenerationPass(Registry);
221 
222 #ifdef GPU_CODEGEN
223   initializePPCGCodeGenerationPass(Registry);
224   initializeManagedMemoryRewritePassPass(Registry);
225   LLVMInitializeNVPTXTarget();
226   LLVMInitializeNVPTXTargetInfo();
227   LLVMInitializeNVPTXTargetMC();
228   LLVMInitializeNVPTXAsmPrinter();
229 #endif
230   initializeCodePreparationPass(Registry);
231   initializeDeadCodeElimWrapperPassPass(Registry);
232   initializeDependenceInfoPass(Registry);
233   initializeDependenceInfoPrinterLegacyPassPass(Registry);
234   initializeDependenceInfoWrapperPassPass(Registry);
235   initializeDependenceInfoPrinterLegacyFunctionPassPass(Registry);
236   initializeJSONExporterPass(Registry);
237   initializeJSONImporterPass(Registry);
238   initializeJSONImporterPrinterLegacyPassPass(Registry);
239   initializeMaximalStaticExpanderWrapperPassPass(Registry);
240   initializeIslAstInfoWrapperPassPass(Registry);
241   initializeIslAstInfoPrinterLegacyPassPass(Registry);
242   initializeIslScheduleOptimizerWrapperPassPass(Registry);
243   initializeIslScheduleOptimizerPrinterLegacyPassPass(Registry);
244   initializePollyCanonicalizePass(Registry);
245   initializePolyhedralInfoPass(Registry);
246   initializePolyhedralInfoPrinterLegacyPassPass(Registry);
247   initializeScopDetectionWrapperPassPass(Registry);
248   initializeScopDetectionPrinterLegacyPassPass(Registry);
249   initializeScopInlinerPass(Registry);
250   initializeScopInfoRegionPassPass(Registry);
251   initializeScopInfoPrinterLegacyRegionPassPass(Registry);
252   initializeScopInfoWrapperPassPass(Registry);
253   initializeScopInfoPrinterLegacyFunctionPassPass(Registry);
254   initializeCodegenCleanupPass(Registry);
255   initializeFlattenSchedulePass(Registry);
256   initializeFlattenSchedulePrinterLegacyPassPass(Registry);
257   initializeForwardOpTreeWrapperPassPass(Registry);
258   initializeForwardOpTreePrinterLegacyPassPass(Registry);
259   initializeDeLICMWrapperPassPass(Registry);
260   initializeDeLICMPrinterLegacyPassPass(Registry);
261   initializeSimplifyWrapperPassPass(Registry);
262   initializeSimplifyPrinterLegacyPassPass(Registry);
263   initializeDumpModuleWrapperPassPass(Registry);
264   initializePruneUnprofitableWrapperPassPass(Registry);
265 }
266 
267 static bool shouldEnablePollyForOptimization() { return PollyEnabled; }
268 
269 static bool shouldEnablePollyForDiagnostic() {
270   // FIXME: PollyTrackFailures is user-controlled, should not be set
271   // programmatically.
272   if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer)
273     PollyTrackFailures = true;
274 
275   return PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer ||
276          ExportJScop;
277 }
278 
279 /// Register Polly passes such that they form a polyhedral optimizer.
280 ///
281 /// The individual Polly passes are registered in the pass manager such that
282 /// they form a full polyhedral optimizer. The flow of the optimizer starts with
283 /// a set of preparing transformations that canonicalize the LLVM-IR such that
284 /// the LLVM-IR is easier for us to understand and to optimizes. On the
285 /// canonicalized LLVM-IR we first run the ScopDetection pass, which detects
286 /// static control flow regions. Those regions are then translated by the
287 /// ScopInfo pass into a polyhedral representation. As a next step, a scheduling
288 /// optimizer is run on the polyhedral representation and finally the optimized
289 /// polyhedral representation is code generated back to LLVM-IR.
290 ///
291 /// Besides this core functionality, we optionally schedule passes that provide
292 /// a graphical view of the scops (Polly[Only]Viewer, Polly[Only]Printer), that
293 /// allow the export/import of the polyhedral representation
294 /// (JSCON[Exporter|Importer]) or that show the cfg after code generation.
295 ///
296 /// For certain parts of the Polly optimizer, several alternatives are provided:
297 ///
298 /// As scheduling optimizer we support the isl scheduling optimizer
299 /// (http://freecode.com/projects/isl).
300 /// It is also possible to run Polly with no optimizer. This mode is mainly
301 /// provided to analyze the run and compile time changes caused by the
302 /// scheduling optimizer.
303 ///
304 /// Polly supports the isl internal code generator.
305 
306 /// Add the pass sequence required for Polly to the New Pass Manager.
307 ///
308 /// @param PM           The pass manager itself.
309 /// @param Level        The optimization level. Used for the cleanup of Polly's
310 ///                     output.
311 /// @param EnableForOpt Whether to add Polly IR transformations. If False, only
312 ///                     the analysis passes are added, skipping Polly itself.
313 ///                     The IR may still be modified.
314 static void buildCommonPollyPipeline(FunctionPassManager &PM,
315                                      OptimizationLevel Level,
316                                      bool EnableForOpt) {
317   PassBuilder PB;
318   ScopPassManager SPM;
319 
320   PM.addPass(CodePreparationPass());
321 
322   // TODO add utility passes for the various command line options, once they're
323   // ported
324 
325   if (PollyDetectOnly) {
326     // Don't add more passes other than the ScopPassManager's detection passes.
327     PM.addPass(createFunctionToScopPassAdaptor(std::move(SPM)));
328     return;
329   }
330 
331   if (PollyViewer)
332     PM.addPass(ScopViewer());
333   if (PollyOnlyViewer)
334     PM.addPass(ScopOnlyViewer());
335   if (PollyPrinter)
336     PM.addPass(ScopPrinter());
337   if (PollyOnlyPrinter)
338     PM.addPass(ScopOnlyPrinter());
339   if (EnableSimplify)
340     SPM.addPass(SimplifyPass(0));
341   if (EnableForwardOpTree)
342     SPM.addPass(ForwardOpTreePass());
343   if (EnableDeLICM)
344     SPM.addPass(DeLICMPass());
345   if (EnableSimplify)
346     SPM.addPass(SimplifyPass(1));
347 
348   if (ImportJScop)
349     SPM.addPass(JSONImportPass());
350 
351   if (DeadCodeElim)
352     SPM.addPass(DeadCodeElimPass());
353 
354   if (FullyIndexedStaticExpansion)
355     SPM.addPass(MaximalStaticExpansionPass());
356 
357   if (EnablePruneUnprofitable)
358     SPM.addPass(PruneUnprofitablePass());
359 
360   switch (Optimizer) {
361   case OPTIMIZER_NONE:
362     break; /* Do nothing */
363   case OPTIMIZER_ISL:
364     SPM.addPass(IslScheduleOptimizerPass());
365     break;
366   }
367 
368   if (ExportJScop)
369     SPM.addPass(JSONExportPass());
370 
371   if (!EnableForOpt)
372     return;
373 
374   switch (CodeGeneration) {
375   case CODEGEN_AST:
376     SPM.addPass(
377         llvm::RequireAnalysisPass<IslAstAnalysis, Scop, ScopAnalysisManager,
378                                   ScopStandardAnalysisResults &,
379                                   SPMUpdater &>());
380     break;
381   case CODEGEN_FULL:
382     SPM.addPass(CodeGenerationPass());
383     break;
384   case CODEGEN_NONE:
385     break;
386   }
387 
388   PM.addPass(createFunctionToScopPassAdaptor(std::move(SPM)));
389   PM.addPass(PB.buildFunctionSimplificationPipeline(
390       Level, llvm::ThinOrFullLTOPhase::None)); // Cleanup
391 
392   if (CFGPrinter)
393     PM.addPass(llvm::CFGPrinterPass());
394 }
395 
396 static void buildEarlyPollyPipeline(llvm::ModulePassManager &MPM,
397                                     llvm::OptimizationLevel Level) {
398   bool EnableForOpt =
399       shouldEnablePollyForOptimization() && Level.isOptimizingForSpeed();
400   if (!shouldEnablePollyForDiagnostic() && !EnableForOpt)
401     return;
402 
403   FunctionPassManager FPM = buildCanonicalicationPassesForNPM(MPM, Level);
404 
405   if (DumpBefore || !DumpBeforeFile.empty()) {
406     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
407 
408     if (DumpBefore)
409       MPM.addPass(DumpModulePass("-before", true));
410     for (auto &Filename : DumpBeforeFile)
411       MPM.addPass(DumpModulePass(Filename, false));
412 
413     FPM = FunctionPassManager();
414   }
415 
416   buildCommonPollyPipeline(FPM, Level, EnableForOpt);
417   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
418 
419   if (DumpAfter)
420     MPM.addPass(DumpModulePass("-after", true));
421   for (auto &Filename : DumpAfterFile)
422     MPM.addPass(DumpModulePass(Filename, false));
423 }
424 
425 static void buildLatePollyPipeline(FunctionPassManager &PM,
426                                    llvm::OptimizationLevel Level) {
427   bool EnableForOpt =
428       shouldEnablePollyForOptimization() && Level.isOptimizingForSpeed();
429   if (!shouldEnablePollyForDiagnostic() && !EnableForOpt)
430     return;
431 
432   if (DumpBefore)
433     PM.addPass(DumpFunctionPass("-before"));
434   if (!DumpBeforeFile.empty())
435     llvm::report_fatal_error(
436         "Option -polly-dump-before-file at -polly-position=late "
437         "not supported with NPM",
438         false);
439 
440   buildCommonPollyPipeline(PM, Level, EnableForOpt);
441 
442   if (DumpAfter)
443     PM.addPass(DumpFunctionPass("-after"));
444   if (!DumpAfterFile.empty())
445     llvm::report_fatal_error(
446         "Option -polly-dump-after-file at -polly-position=late "
447         "not supported with NPM",
448         false);
449 }
450 
451 static OwningScopAnalysisManagerFunctionProxy
452 createScopAnalyses(FunctionAnalysisManager &FAM,
453                    PassInstrumentationCallbacks *PIC) {
454   OwningScopAnalysisManagerFunctionProxy Proxy;
455 #define SCOP_ANALYSIS(NAME, CREATE_PASS)                                       \
456   Proxy.getManager().registerPass([PIC] {                                      \
457     (void)PIC;                                                                 \
458     return CREATE_PASS;                                                        \
459   });
460 #include "PollyPasses.def"
461 
462   Proxy.getManager().registerPass(
463       [&FAM] { return FunctionAnalysisManagerScopProxy(FAM); });
464   return Proxy;
465 }
466 
467 static void registerFunctionAnalyses(FunctionAnalysisManager &FAM,
468                                      PassInstrumentationCallbacks *PIC) {
469 
470 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
471   FAM.registerPass([] { return CREATE_PASS; });
472 
473 #include "PollyPasses.def"
474 
475   FAM.registerPass([&FAM, PIC] { return createScopAnalyses(FAM, PIC); });
476 }
477 
478 static bool
479 parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM,
480                       ArrayRef<PassBuilder::PipelineElement> Pipeline) {
481   if (llvm::parseAnalysisUtilityPasses<OwningScopAnalysisManagerFunctionProxy>(
482           "polly-scop-analyses", Name, FPM))
483     return true;
484 
485 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
486   if (llvm::parseAnalysisUtilityPasses<                                        \
487           std::remove_reference<decltype(CREATE_PASS)>::type>(NAME, Name,      \
488                                                               FPM))            \
489     return true;
490 
491 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
492   if (Name == NAME) {                                                          \
493     FPM.addPass(CREATE_PASS);                                                  \
494     return true;                                                               \
495   }
496 
497 #include "PollyPasses.def"
498   return false;
499 }
500 
501 static bool parseScopPass(StringRef Name, ScopPassManager &SPM,
502                           PassInstrumentationCallbacks *PIC) {
503 #define SCOP_ANALYSIS(NAME, CREATE_PASS)                                       \
504   if (llvm::parseAnalysisUtilityPasses<                                        \
505           std::remove_reference<decltype(CREATE_PASS)>::type>(NAME, Name,      \
506                                                               SPM))            \
507     return true;
508 
509 #define SCOP_PASS(NAME, CREATE_PASS)                                           \
510   if (Name == NAME) {                                                          \
511     SPM.addPass(CREATE_PASS);                                                  \
512     return true;                                                               \
513   }
514 
515 #include "PollyPasses.def"
516 
517   return false;
518 }
519 
520 static bool parseScopPipeline(StringRef Name, FunctionPassManager &FPM,
521                               PassInstrumentationCallbacks *PIC,
522                               ArrayRef<PassBuilder::PipelineElement> Pipeline) {
523   if (Name != "scop")
524     return false;
525   if (!Pipeline.empty()) {
526     ScopPassManager SPM;
527     for (const auto &E : Pipeline)
528       if (!parseScopPass(E.Name, SPM, PIC))
529         return false;
530     FPM.addPass(createFunctionToScopPassAdaptor(std::move(SPM)));
531   }
532   return true;
533 }
534 
535 static bool isScopPassName(StringRef Name) {
536 #define SCOP_ANALYSIS(NAME, CREATE_PASS)                                       \
537   if (Name == "require<" NAME ">")                                             \
538     return true;                                                               \
539   if (Name == "invalidate<" NAME ">")                                          \
540     return true;
541 
542 #define SCOP_PASS(NAME, CREATE_PASS)                                           \
543   if (Name == NAME)                                                            \
544     return true;
545 
546 #include "PollyPasses.def"
547 
548   return false;
549 }
550 
551 static bool
552 parseTopLevelPipeline(llvm::ModulePassManager &MPM,
553                       PassInstrumentationCallbacks *PIC,
554                       ArrayRef<PassBuilder::PipelineElement> Pipeline) {
555   std::vector<PassBuilder::PipelineElement> FullPipeline;
556   StringRef FirstName = Pipeline.front().Name;
557 
558   if (!isScopPassName(FirstName))
559     return false;
560 
561   FunctionPassManager FPM;
562   ScopPassManager SPM;
563 
564   for (auto &Element : Pipeline) {
565     auto &Name = Element.Name;
566     auto &InnerPipeline = Element.InnerPipeline;
567     if (!InnerPipeline.empty()) // Scop passes don't have inner pipelines
568       return false;
569     if (!parseScopPass(Name, SPM, PIC))
570       return false;
571   }
572 
573   FPM.addPass(createFunctionToScopPassAdaptor(std::move(SPM)));
574   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
575 
576   return true;
577 }
578 
579 /// Register Polly to be available as an optimizer
580 ///
581 ///
582 /// We can currently run Polly at two different points int the pass manager.
583 /// a) very early, b) right before the vectorizer.
584 ///
585 /// The default is currently a), to register Polly such that it runs as early as
586 /// possible. This has several implications:
587 ///
588 ///   1) We need to schedule more canonicalization passes
589 ///
590 ///   As nothing is run before Polly, it is necessary to run a set of preparing
591 ///   transformations before Polly to canonicalize the LLVM-IR and to allow
592 ///   Polly to detect and understand the code.
593 ///
594 ///   2) We get the full -O3 optimization sequence after Polly
595 ///
596 ///   The LLVM-IR that is generated by Polly has been optimized on a high level,
597 ///   but it may be rather inefficient on the lower/scalar level. By scheduling
598 ///   Polly before all other passes, we have the full sequence of -O3
599 ///   optimizations behind us, such that inefficiencies on the low level can
600 ///   be optimized away.
601 ///
602 /// We are currently evaluating the benefit or running Polly at b). b) is nice
603 /// as everything is fully inlined and canonicalized, but we need to be able to
604 /// handle LICMed code to make it useful.
605 void registerPollyPasses(PassBuilder &PB) {
606   PassInstrumentationCallbacks *PIC = PB.getPassInstrumentationCallbacks();
607   PB.registerAnalysisRegistrationCallback([PIC](FunctionAnalysisManager &FAM) {
608     registerFunctionAnalyses(FAM, PIC);
609   });
610   PB.registerPipelineParsingCallback(parseFunctionPipeline);
611   PB.registerPipelineParsingCallback(
612       [PIC](StringRef Name, FunctionPassManager &FPM,
613             ArrayRef<PassBuilder::PipelineElement> Pipeline) -> bool {
614         return parseScopPipeline(Name, FPM, PIC, Pipeline);
615       });
616   PB.registerParseTopLevelPipelineCallback(
617       [PIC](llvm::ModulePassManager &MPM,
618             ArrayRef<PassBuilder::PipelineElement> Pipeline) -> bool {
619         return parseTopLevelPipeline(MPM, PIC, Pipeline);
620       });
621 
622   switch (PassPosition) {
623   case POSITION_EARLY:
624     PB.registerPipelineStartEPCallback(buildEarlyPollyPipeline);
625     break;
626   case POSITION_BEFORE_VECTORIZER:
627     PB.registerVectorizerStartEPCallback(buildLatePollyPipeline);
628     break;
629   }
630 }
631 } // namespace polly
632 
633 llvm::PassPluginLibraryInfo getPollyPluginInfo() {
634   return {LLVM_PLUGIN_API_VERSION, "Polly", LLVM_VERSION_STRING,
635           polly::registerPollyPasses};
636 }
637