Polly 23.0.0git
RegisterPasses.cpp
Go to the documentation of this file.
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
26#include "polly/DeLICM.h"
29#include "polly/ForwardOpTree.h"
30#include "polly/JSONExporter.h"
32#include "polly/Options.h"
36#include "polly/ScopDetection.h"
38#include "polly/ScopInfo.h"
39#include "polly/ScopInliner.h"
40#include "polly/Simplify.h"
43#include "llvm/Analysis/CFGPrinter.h"
44#include "llvm/Config/llvm-config.h" // for LLVM_VERSION_STRING
45#include "llvm/IR/LegacyPassManager.h"
46#include "llvm/IR/PassManager.h"
47#include "llvm/IR/Verifier.h"
48#include "llvm/Passes/PassBuilder.h"
49#include "llvm/Plugins/PassPlugin.h"
50#include "llvm/Support/CommandLine.h"
51#include "llvm/Support/Error.h"
52#include "llvm/Support/TargetSelect.h"
53#include "llvm/Transforms/IPO.h"
54
55using namespace llvm;
56using namespace polly;
57
58namespace cl = llvm::cl;
59using namespace polly;
60
61using llvm::FunctionPassManager;
62using llvm::OptimizationLevel;
63using llvm::PassBuilder;
64using llvm::PassInstrumentationCallbacks;
65
66cl::OptionCategory PollyCategory("Polly Options",
67 "Configure the polly loop optimizer");
68
69namespace polly {
70static cl::opt<bool>
71 PollyEnabled("polly",
72 cl::desc("Enable the polly optimizer (with -O1, -O2 or -O3)"),
73 cl::cat(PollyCategory));
74
75static cl::opt<bool> PollyDetectOnly(
76 "polly-only-scop-detection",
77 cl::desc("Only run scop detection, but no other optimizations"),
78 cl::cat(PollyCategory));
79
81
83
84static cl::opt<PassPositionChoice> PassPosition(
85 "polly-position", cl::desc("Where to run polly in the pass pipeline"),
86 cl::values(clEnumValN(POSITION_EARLY, "early", "Before everything"),
87 clEnumValN(POSITION_BEFORE_VECTORIZER, "before-vectorizer",
88 "Right before the vectorizer")),
89 cl::Hidden, cl::init(POSITION_BEFORE_VECTORIZER), cl::cat(PollyCategory));
90
91static cl::opt<OptimizerChoice>
92 Optimizer("polly-optimizer", cl::desc("Select the scheduling optimizer"),
93 cl::values(clEnumValN(OPTIMIZER_NONE, "none", "No optimizer"),
94 clEnumValN(OPTIMIZER_ISL, "isl",
95 "The isl scheduling optimizer")),
96 cl::Hidden, cl::init(OPTIMIZER_ISL), cl::cat(PollyCategory));
97
99static cl::opt<CodeGenChoice> CodeGeneration(
100 "polly-code-generation", cl::desc("How much code-generation to perform"),
101 cl::values(clEnumValN(CODEGEN_FULL, "full", "AST and IR generation"),
102 clEnumValN(CODEGEN_AST, "ast", "Only AST generation"),
103 clEnumValN(CODEGEN_NONE, "none", "No code generation")),
104 cl::Hidden, cl::init(CODEGEN_FULL), cl::cat(PollyCategory));
105
107
108static cl::opt<VectorizerChoice, true> Vectorizer(
109 "polly-vectorizer", cl::desc("Select the vectorization strategy"),
110 cl::values(
111 clEnumValN(VECTORIZER_NONE, "none", "No Vectorization"),
112 clEnumValN(
113 VECTORIZER_STRIPMINE, "stripmine",
114 "Strip-mine outer loops for the loop-vectorizer to trigger")),
115 cl::location(PollyVectorizerChoice), cl::init(VECTORIZER_NONE),
116 cl::cat(PollyCategory));
117
118static cl::opt<bool> ImportJScop(
119 "polly-import",
120 cl::desc("Import the polyhedral description of the detected Scops"),
121 cl::Hidden, cl::cat(PollyCategory));
122
123static cl::opt<bool> FullyIndexedStaticExpansion(
124 "polly-enable-mse",
125 cl::desc("Fully expand the memory accesses of the detected Scops"),
126 cl::Hidden, cl::cat(PollyCategory));
127
128static cl::opt<bool> ExportJScop(
129 "polly-export",
130 cl::desc("Export the polyhedral description of the detected Scops"),
131 cl::Hidden, cl::cat(PollyCategory));
132
133static cl::opt<bool> DeadCodeElim("polly-run-dce",
134 cl::desc("Run the dead code elimination"),
135 cl::Hidden, cl::cat(PollyCategory));
136
137static cl::opt<bool> PollyViewer(
138 "polly-show",
139 cl::desc("Highlight the code regions that will be optimized in a "
140 "(CFG BBs and LLVM-IR instructions)"),
141 cl::cat(PollyCategory));
142
143static cl::opt<bool> PollyOnlyViewer(
144 "polly-show-only",
145 cl::desc("Highlight the code regions that will be optimized in "
146 "a (CFG only BBs)"),
147 cl::init(false), cl::cat(PollyCategory));
148
149static cl::opt<bool>
150 PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"),
151 cl::Hidden, cl::value_desc("Run the Polly DOT printer at -O3"),
152 cl::init(false), cl::cat(PollyCategory));
153
154static cl::opt<bool> PollyOnlyPrinter(
155 "polly-dot-only",
156 cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden,
157 cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"),
158 cl::init(false), cl::cat(PollyCategory));
159
160static cl::opt<bool>
161 CFGPrinter("polly-view-cfg",
162 cl::desc("Show the Polly CFG right after code generation"),
163 cl::Hidden, cl::init(false), cl::cat(PollyCategory));
164
165static cl::opt<bool>
166 EnableForwardOpTree("polly-enable-optree",
167 cl::desc("Enable operand tree forwarding"), cl::Hidden,
168 cl::init(true), cl::cat(PollyCategory));
169
170static cl::opt<bool>
171 DumpBefore("polly-dump-before",
172 cl::desc("Dump module before Polly transformations into a file "
173 "suffixed with \"-before\""),
174 cl::init(false), cl::cat(PollyCategory));
175
176static cl::list<std::string> DumpBeforeFile(
177 "polly-dump-before-file",
178 cl::desc("Dump module before Polly transformations to the given file"),
179 cl::cat(PollyCategory));
180
181static cl::opt<bool>
182 DumpAfter("polly-dump-after",
183 cl::desc("Dump module after Polly transformations into a file "
184 "suffixed with \"-after\""),
185 cl::init(false), cl::cat(PollyCategory));
186
187static cl::list<std::string> DumpAfterFile(
188 "polly-dump-after-file",
189 cl::desc("Dump module after Polly transformations to the given file"),
190 cl::cat(PollyCategory));
191
192static cl::opt<bool>
193 EnableDeLICM("polly-enable-delicm",
194 cl::desc("Eliminate scalar loop carried dependences"),
195 cl::Hidden, cl::init(true), cl::cat(PollyCategory));
196
197static cl::opt<bool>
198 EnableSimplify("polly-enable-simplify",
199 cl::desc("Simplify SCoP after optimizations"),
200 cl::init(true), cl::cat(PollyCategory));
201
202static cl::opt<bool> EnablePruneUnprofitable(
203 "polly-enable-prune-unprofitable",
204 cl::desc("Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden,
205 cl::init(true), cl::cat(PollyCategory));
206
207static cl::opt<bool>
208 PollyPrintDetect("polly-print-detect",
209 cl::desc("Polly - Print static control parts (SCoPs)"),
210 cl::cat(PollyCategory));
211
212static cl::opt<bool>
213 PollyPrintScops("polly-print-scops",
214 cl::desc("Print polyhedral description of all regions"),
215 cl::cat(PollyCategory));
216
217static cl::opt<bool> PollyPrintDeps("polly-print-deps",
218 cl::desc("Polly - Print dependences"),
219 cl::cat(PollyCategory));
220
222
224 // FIXME: PollyTrackFailures is user-controlled, should not be set
225 // programmatically.
227 PollyTrackFailures = true;
228
231}
232
233/// Parser of parameters for LoopVectorize pass.
234static llvm::Expected<PollyPassOptions> parsePollyOptions(StringRef Params,
235 bool IsCustom) {
236 PassPhase PrevPhase = PassPhase::None;
237
238 bool EnableDefaultOpts = !IsCustom;
239 bool EnableEnd2End = !IsCustom;
240 std::optional<bool>
241 PassEnabled[static_cast<size_t>(PassPhase::PassPhaseLast) + 1];
242 PassPhase StopAfter = PassPhase::None;
243
244 // Passes enabled using command-line flags (can be overridden using
245 // 'polly<no-pass>')
247 PassEnabled[static_cast<size_t>(PassPhase::PrintDetect)] = true;
248 if (PollyPrintScops)
249 PassEnabled[static_cast<size_t>(PassPhase::PrintScopInfo)] = true;
250 if (PollyPrintDeps)
251 PassEnabled[static_cast<size_t>(PassPhase::PrintDependences)] = true;
252
253 if (PollyViewer)
254 PassEnabled[static_cast<size_t>(PassPhase::ViewScops)] = true;
255 if (PollyOnlyViewer)
256 PassEnabled[static_cast<size_t>(PassPhase::ViewScopsOnly)] = true;
257 if (PollyPrinter)
258 PassEnabled[static_cast<size_t>(PassPhase::DotScops)] = true;
260 PassEnabled[static_cast<size_t>(PassPhase::DotScopsOnly)] = true;
261 if (!EnableSimplify)
262 PassEnabled[static_cast<size_t>(PassPhase::Simplify0)] = false;
264 PassEnabled[static_cast<size_t>(PassPhase::Optree)] = false;
265 if (!EnableDeLICM)
266 PassEnabled[static_cast<size_t>(PassPhase::DeLICM)] = false;
267 if (!EnableSimplify)
268 PassEnabled[static_cast<size_t>(PassPhase::Simplify1)] = false;
269 if (ImportJScop)
270 PassEnabled[static_cast<size_t>(PassPhase::ImportJScop)] = true;
271 if (DeadCodeElim)
272 PassEnabled[static_cast<size_t>(PassPhase::DeadCodeElimination)] = true;
274 PassEnabled[static_cast<size_t>(PassPhase::MaximumStaticExtension)] = true;
276 PassEnabled[static_cast<size_t>(PassPhase::PruneUnprofitable)] = false;
277 switch (Optimizer) {
278 case OPTIMIZER_NONE:
279 // explicitly switched off
280 PassEnabled[static_cast<size_t>(PassPhase::Optimization)] = false;
281 break;
282 case OPTIMIZER_ISL:
283 // default: enabled
284 break;
285 }
286 if (ExportJScop)
287 PassEnabled[static_cast<size_t>(PassPhase::ExportJScop)] = true;
288 switch (CodeGeneration) {
289 case CODEGEN_AST:
290 PassEnabled[static_cast<size_t>(PassPhase::AstGen)] = true;
291 PassEnabled[static_cast<size_t>(PassPhase::CodeGen)] = false;
292 break;
293 case CODEGEN_FULL:
294 // default: ast and codegen enabled
295 break;
296 case CODEGEN_NONE:
297 PassEnabled[static_cast<size_t>(PassPhase::AstGen)] = false;
298 PassEnabled[static_cast<size_t>(PassPhase::CodeGen)] = false;
299 break;
300 }
301
302 while (!Params.empty()) {
303 StringRef Param;
304 std::tie(Param, Params) = Params.split(';');
305 auto [ParamName, ParamVal] = Param.split('=');
306
307 if (ParamName == "stopafter") {
308 StopAfter = parsePhase(ParamVal);
309 if (StopAfter == PassPhase::None)
310 return make_error<StringError>(
311 formatv("invalid stopafter parameter value '{0}'", ParamVal).str(),
312 inconvertibleErrorCode());
313 continue;
314 }
315
316 if (!ParamVal.empty())
317 return make_error<StringError>(
318 formatv("parameter '{0}' does not take value", ParamName).str(),
319 inconvertibleErrorCode());
320
321 bool Enabled = true;
322 if (ParamName.starts_with("no-")) {
323 Enabled = false;
324 ParamName = ParamName.drop_front(3);
325 }
326
327 if (ParamName == "default-opts") {
328 EnableDefaultOpts = Enabled;
329 continue;
330 }
331
332 if (ParamName == "end2end") {
333 EnableEnd2End = Enabled;
334 continue;
335 }
336
337 PassPhase Phase;
338
339 // Shortcut for both simplifys at the same time
340 if (ParamName == "simplify") {
341 PassEnabled[static_cast<size_t>(PassPhase::Simplify0)] = Enabled;
342 PassEnabled[static_cast<size_t>(PassPhase::Simplify1)] = Enabled;
343 Phase = PassPhase::Simplify0;
344 } else {
345 Phase = parsePhase(ParamName);
346 if (Phase == PassPhase::None)
347 return make_error<StringError>(
348 formatv("invalid Polly parameter/phase name '{0}'", ParamName)
349 .str(),
350 inconvertibleErrorCode());
351
352 if (PrevPhase >= Phase)
353 return make_error<StringError>(
354 formatv("phases must not be repeated and enumerated in-order: "
355 "'{0}' listed before '{1}'",
356 getPhaseName(PrevPhase), getPhaseName(Phase))
357 .str(),
358 inconvertibleErrorCode());
359
360 PassEnabled[static_cast<size_t>(Phase)] = Enabled;
361 }
362 PrevPhase = Phase;
363 }
364
365 PollyPassOptions Opts;
366 Opts.ViewAll = ViewAll;
367 Opts.ViewFilter = ViewFilter;
369
370 // Implicitly enable dependent phases first. May be overriden explicitly
371 // on/off later.
372 for (PassPhase P : llvm::enum_seq_inclusive(PassPhase::PassPhaseFirst,
374 bool Enabled = PassEnabled[static_cast<size_t>(P)].value_or(false);
375 if (!Enabled)
376 continue;
377
378 if (static_cast<size_t>(PassPhase::Detection) < static_cast<size_t>(P))
380
381 if (static_cast<size_t>(PassPhase::ScopInfo) < static_cast<size_t>(P))
383
386
387 if (static_cast<size_t>(PassPhase::AstGen) < static_cast<size_t>(P))
389 }
390
391 if (EnableEnd2End)
392 Opts.enableEnd2End();
393
394 if (EnableDefaultOpts)
395 Opts.enableDefaultOpts();
396
397 for (PassPhase P : llvm::enum_seq_inclusive(PassPhase::PassPhaseFirst,
399 std::optional<bool> Enabled = PassEnabled[static_cast<size_t>(P)];
400
401 // Apply only if set explicitly.
402 if (Enabled.has_value())
403 Opts.setPhaseEnabled(P, *Enabled);
404 }
405
406 if (StopAfter != PassPhase::None)
407 Opts.disableAfter(StopAfter);
408
409 if (Error CheckResult = Opts.checkConsistency())
410 return CheckResult;
411
412 return Opts;
413}
414
415static llvm::Expected<PollyPassOptions>
416parsePollyDefaultOptions(StringRef Params) {
417 return parsePollyOptions(Params, false);
418}
419
420static llvm::Expected<PollyPassOptions>
421parsePollyCustomOptions(StringRef Params) {
422 return parsePollyOptions(Params, true);
423}
424
425/// Register Polly passes such that they form a polyhedral optimizer.
426///
427/// The individual Polly passes are registered in the pass manager such that
428/// they form a full polyhedral optimizer. The flow of the optimizer starts with
429/// a set of preparing transformations that canonicalize the LLVM-IR such that
430/// the LLVM-IR is easier for us to understand and to optimizes. On the
431/// canonicalized LLVM-IR we first run the ScopDetection pass, which detects
432/// static control flow regions. Those regions are then translated by the
433/// ScopInfo pass into a polyhedral representation. As a next step, a scheduling
434/// optimizer is run on the polyhedral representation and finally the optimized
435/// polyhedral representation is code generated back to LLVM-IR.
436///
437/// Besides this core functionality, we optionally schedule passes that provide
438/// a graphical view of the scops (Polly[Only]Viewer, Polly[Only]Printer), that
439/// allow the export/import of the polyhedral representation
440/// (JSCON[Exporter|Importer]) or that show the cfg after code generation.
441///
442/// For certain parts of the Polly optimizer, several alternatives are provided:
443///
444/// As scheduling optimizer we support the isl scheduling optimizer
445/// (http://freecode.com/projects/isl).
446/// It is also possible to run Polly with no optimizer. This mode is mainly
447/// provided to analyze the run and compile time changes caused by the
448/// scheduling optimizer.
449///
450/// Polly supports the isl internal code generator.
451
452/// Add the pass sequence required for Polly to the New Pass Manager.
453///
454/// @param PM The pass manager itself.
455/// @param Level The optimization level. Used for the cleanup of Polly's
456/// output.
457/// @param EnableForOpt Whether to add Polly IR transformations. If False, only
458/// the analysis passes are added, skipping Polly itself.
459/// The IR may still be modified.
460static void buildCommonPollyPipeline(FunctionPassManager &PM,
461 OptimizationLevel Level,
462 IntrusiveRefCntPtr<vfs::FileSystem> FS,
463 bool EnableForOpt) {
464 PassBuilder PB(
465 /*TM=*/nullptr,
466 /*PipelineTuningOptions=*/{},
467 /*PGOOpt=*/{},
468 /*PIC=*/nullptr, std::move(FS));
469
470 ExitOnError Err("Inconsistent Polly configuration: ");
471 PollyPassOptions &&Opts =
472 Err(parsePollyOptions(StringRef(), /*IsCustom=*/false));
473 PM.addPass(PollyFunctionPass(Opts));
474
475 PM.addPass(PB.buildFunctionSimplificationPipeline(
476 Level, llvm::ThinOrFullLTOPhase::None)); // Cleanup
477
478 if (CFGPrinter)
479 PM.addPass(llvm::CFGPrinterPass());
480}
481
482static void buildEarlyPollyPipeline(llvm::ModulePassManager &MPM,
483 llvm::OptimizationLevel Level,
484 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
485 bool EnableForOpt =
486 shouldEnablePollyForOptimization() && Level.isOptimizingForSpeed();
487 if (!shouldEnablePollyForDiagnostic() && !EnableForOpt)
488 return;
489
490 FunctionPassManager FPM = buildCanonicalicationPassesForNPM(MPM, Level);
491
492 if (DumpBefore || !DumpBeforeFile.empty()) {
493 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
494
495 if (DumpBefore)
496 MPM.addPass(DumpModulePass("-before", true));
497 for (auto &Filename : DumpBeforeFile)
498 MPM.addPass(DumpModulePass(Filename, false));
499
500 FPM = FunctionPassManager();
501 }
502
503 buildCommonPollyPipeline(FPM, Level, std::move(FS), EnableForOpt);
504 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
505
506 if (DumpAfter)
507 MPM.addPass(DumpModulePass("-after", true));
508 for (auto &Filename : DumpAfterFile)
509 MPM.addPass(DumpModulePass(Filename, false));
510}
511
512static void buildLatePollyPipeline(FunctionPassManager &PM,
513 llvm::OptimizationLevel Level,
514 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
515 bool EnableForOpt =
516 shouldEnablePollyForOptimization() && Level.isOptimizingForSpeed();
517 if (!shouldEnablePollyForDiagnostic() && !EnableForOpt)
518 return;
519
520 if (DumpBefore)
521 PM.addPass(DumpFunctionPass("-before"));
522 if (!DumpBeforeFile.empty())
523 llvm::report_fatal_error(
524 "Option -polly-dump-before-file at -polly-position=late "
525 "not supported with NPM",
526 false);
527
528 buildCommonPollyPipeline(PM, Level, std::move(FS), EnableForOpt);
529
530 if (DumpAfter)
531 PM.addPass(DumpFunctionPass("-after"));
532 if (!DumpAfterFile.empty())
533 llvm::report_fatal_error(
534 "Option -polly-dump-after-file at -polly-position=late "
535 "not supported with NPM",
536 false);
537}
538
539static llvm::Expected<std::monostate> parseNoOptions(StringRef Params) {
540 if (!Params.empty())
541 return make_error<StringError>(
542 formatv("'{0}' passed to pass that does not take any options", Params)
543 .str(),
544 inconvertibleErrorCode());
545
546 return std::monostate{};
547}
548
549static llvm::Expected<bool>
550parseCGPipeline(StringRef Name, llvm::CGSCCPassManager &CGPM,
551 PassInstrumentationCallbacks *PIC,
552 ArrayRef<PassBuilder::PipelineElement> Pipeline,
553 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
554#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
555 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
556 auto Params = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
557 if (!Params) \
558 return Params.takeError(); \
559 CGPM.addPass(CREATE_PASS); \
560 return true; \
561 }
562#include "PollyPasses.def"
563
564 return false;
565}
566
567static llvm::Expected<bool>
568parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM,
569 PassInstrumentationCallbacks *PIC,
570 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
571
572#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
573 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
574 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
575 if (!ExpectedOpts) \
576 return ExpectedOpts.takeError(); \
577 auto &&Opts = *ExpectedOpts; \
578 (void)Opts; \
579 FPM.addPass(CREATE_PASS); \
580 return true; \
581 }
582
583#include "PollyPasses.def"
584 return false;
585}
586
587static llvm::Expected<bool>
588parseModulePipeline(StringRef Name, llvm::ModulePassManager &MPM,
589 PassInstrumentationCallbacks *PIC,
590 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
591#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
592 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
593 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
594 if (!ExpectedOpts) \
595 return ExpectedOpts.takeError(); \
596 auto &&Opts = *ExpectedOpts; \
597 (void)Opts; \
598 MPM.addPass(CREATE_PASS); \
599 return true; \
600 }
601
602#include "PollyPasses.def"
603
604 return false;
605}
606
607/// Register Polly to be available as an optimizer
608///
609///
610/// We can currently run Polly at two different points int the pass manager.
611/// a) very early, b) right before the vectorizer.
612///
613/// The default is currently a), to register Polly such that it runs as early as
614/// possible. This has several implications:
615///
616/// 1) We need to schedule more canonicalization passes
617///
618/// As nothing is run before Polly, it is necessary to run a set of preparing
619/// transformations before Polly to canonicalize the LLVM-IR and to allow
620/// Polly to detect and understand the code.
621///
622/// 2) We get the full -O3 optimization sequence after Polly
623///
624/// The LLVM-IR that is generated by Polly has been optimized on a high level,
625/// but it may be rather inefficient on the lower/scalar level. By scheduling
626/// Polly before all other passes, we have the full sequence of -O3
627/// optimizations behind us, such that inefficiencies on the low level can
628/// be optimized away.
629///
630/// We are currently evaluating the benefit or running Polly at b). b) is nice
631/// as everything is fully inlined and canonicalized, but we need to be able to
632/// handle LICMed code to make it useful.
633void registerPollyPasses(PassBuilder &PB) {
634 PassInstrumentationCallbacks *PIC = PB.getPassInstrumentationCallbacks();
635 IntrusiveRefCntPtr<vfs::FileSystem> FS = PB.getVirtualFileSystemPtr();
636
637#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
638 { \
639 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
640 (void)Opts; \
641 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
642 }
643#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
644 { \
645 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
646 (void)Opts; \
647 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
648 }
649#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
650 { \
651 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
652 (void)Opts; \
653 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
654 }
655#include "PollyPasses.def"
656
657 PB.registerPipelineParsingCallback(
658 [PIC](StringRef Name, FunctionPassManager &FPM,
659 ArrayRef<PassBuilder::PipelineElement> Pipeline) -> bool {
660 ExitOnError Err("Unable to parse Polly module pass: ");
661 return Err(parseFunctionPipeline(Name, FPM, PIC, Pipeline));
662 });
663 PB.registerPipelineParsingCallback(
664 [PIC, FS](StringRef Name, CGSCCPassManager &CGPM,
665 ArrayRef<PassBuilder::PipelineElement> Pipeline) -> bool {
666 ExitOnError Err("Unable to parse Polly call graph pass: ");
667 return Err(parseCGPipeline(Name, CGPM, PIC, Pipeline, FS));
668 });
669 PB.registerPipelineParsingCallback(
670 [PIC](StringRef Name, ModulePassManager &MPM,
671 ArrayRef<PassBuilder::PipelineElement> Pipeline) -> bool {
672 ExitOnError Err("Unable to parse Polly module pass: ");
673 return Err(parseModulePipeline(Name, MPM, PIC, Pipeline));
674 });
675
676 switch (PassPosition) {
677 case POSITION_EARLY:
678 PB.registerPipelineStartEPCallback(
679 [FS](ModulePassManager &MPM, OptimizationLevel Level) {
680 buildEarlyPollyPipeline(MPM, Level, FS);
681 });
682 break;
684 PB.registerVectorizerStartEPCallback(
685 [FS](FunctionPassManager &FPM, OptimizationLevel Level) {
686 buildLatePollyPipeline(FPM, Level, FS);
687 });
688 break;
689 }
690}
691} // namespace polly
692
693llvm::PassPluginLibraryInfo getPollyPluginInfo() {
694 return {LLVM_PLUGIN_API_VERSION, "Polly", LLVM_VERSION_STRING,
696}
llvm::cl::OptionCategory PollyCategory
llvm::PassPluginLibraryInfo getPollyPluginInfo()
Options for the Polly pass.
Dependences::AnalysisLevel PrintDepsAnalysisLevel
void enableDefaultOpts()
Enabled the default optimization phases.
void setPhaseEnabled(PassPhase Phase, bool Enabled=true)
void enableEnd2End()
Enable all phases that are necessary for a roundtrip from LLVM-IR back to LLVM-IR.
void disableAfter(PassPhase Phase)
Disable all phases following Phase.
llvm::Error checkConsistency() const
Check whether the options are coherent relative to each other.
const char * str
Definition isl_test.c:1880
static cl::opt< bool > ExportJScop("polly-export", cl::desc("Export the polyhedral description of the detected Scops"), cl::Hidden, cl::cat(PollyCategory))
static cl::opt< bool > FullyIndexedStaticExpansion("polly-enable-mse", cl::desc("Fully expand the memory accesses of the detected Scops"), cl::Hidden, cl::cat(PollyCategory))
@ POSITION_BEFORE_VECTORIZER
static llvm::Expected< PollyPassOptions > parsePollyCustomOptions(StringRef Params)
static cl::list< std::string > DumpBeforeFile("polly-dump-before-file", cl::desc("Dump module before Polly transformations to the given file"), cl::cat(PollyCategory))
static cl::opt< bool > PollyPrintDeps("polly-print-deps", cl::desc("Polly - Print dependences"), cl::cat(PollyCategory))
static void buildEarlyPollyPipeline(llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level, IntrusiveRefCntPtr< vfs::FileSystem > FS)
static bool shouldEnablePollyForDiagnostic()
static llvm::Expected< bool > parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM, PassInstrumentationCallbacks *PIC, ArrayRef< PassBuilder::PipelineElement > Pipeline)
PassPhase parsePhase(StringRef Name)
static void buildLatePollyPipeline(FunctionPassManager &PM, llvm::OptimizationLevel Level, IntrusiveRefCntPtr< vfs::FileSystem > FS)
static cl::opt< VectorizerChoice, true > Vectorizer("polly-vectorizer", cl::desc("Select the vectorization strategy"), cl::values(clEnumValN(VECTORIZER_NONE, "none", "No Vectorization"), clEnumValN(VECTORIZER_STRIPMINE, "stripmine", "Strip-mine outer loops for the loop-vectorizer to trigger")), cl::location(PollyVectorizerChoice), cl::init(VECTORIZER_NONE), cl::cat(PollyCategory))
static cl::opt< bool > PollyOnlyViewer("polly-show-only", cl::desc("Highlight the code regions that will be optimized in " "a (CFG only BBs)"), cl::init(false), cl::cat(PollyCategory))
static cl::opt< bool > EnableSimplify("polly-enable-simplify", cl::desc("Simplify SCoP after optimizations"), cl::init(true), cl::cat(PollyCategory))
bool PollyTrackFailures
StringRef getPhaseName(PassPhase Phase)
static cl::opt< bool > PollyPrintDetect("polly-print-detect", cl::desc("Polly - Print static control parts (SCoPs)"), cl::cat(PollyCategory))
static cl::opt< OptimizerChoice > Optimizer("polly-optimizer", cl::desc("Select the scheduling optimizer"), cl::values(clEnumValN(OPTIMIZER_NONE, "none", "No optimizer"), clEnumValN(OPTIMIZER_ISL, "isl", "The isl scheduling optimizer")), cl::Hidden, cl::init(OPTIMIZER_ISL), cl::cat(PollyCategory))
void registerPollyPasses(llvm::PassBuilder &PB)
static cl::opt< bool > PollyViewer("polly-show", cl::desc("Highlight the code regions that will be optimized in a " "(CFG BBs and LLVM-IR instructions)"), cl::cat(PollyCategory))
static cl::opt< bool > PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"), cl::Hidden, cl::value_desc("Run the Polly DOT printer at -O3"), cl::init(false), cl::cat(PollyCategory))
static cl::opt< bool > PollyEnabled("polly", cl::desc("Enable the polly optimizer (with -O1, -O2 or -O3)"), cl::cat(PollyCategory))
static llvm::Expected< bool > parseCGPipeline(StringRef Name, llvm::CGSCCPassManager &CGPM, PassInstrumentationCallbacks *PIC, ArrayRef< PassBuilder::PipelineElement > Pipeline, IntrusiveRefCntPtr< vfs::FileSystem > FS)
@ VECTORIZER_STRIPMINE
@ VECTORIZER_NONE
VectorizerChoice PollyVectorizerChoice
static cl::opt< bool > DumpBefore("polly-dump-before", cl::desc("Dump module before Polly transformations into a file " "suffixed with \"-before\""), cl::init(false), cl::cat(PollyCategory))
Dependences::AnalysisLevel OptAnalysisLevel
static llvm::Expected< PollyPassOptions > parsePollyDefaultOptions(StringRef Params)
static cl::opt< bool > EnableForwardOpTree("polly-enable-optree", cl::desc("Enable operand tree forwarding"), cl::Hidden, cl::init(true), cl::cat(PollyCategory))
static llvm::Expected< PollyPassOptions > parsePollyOptions(StringRef Params, bool IsCustom)
Parser of parameters for LoopVectorize pass.
static cl::opt< bool > DumpAfter("polly-dump-after", cl::desc("Dump module after Polly transformations into a file " "suffixed with \"-after\""), cl::init(false), cl::cat(PollyCategory))
static void buildCommonPollyPipeline(FunctionPassManager &PM, OptimizationLevel Level, IntrusiveRefCntPtr< vfs::FileSystem > FS, bool EnableForOpt)
Register Polly passes such that they form a polyhedral optimizer.
static cl::opt< bool > PollyDetectOnly("polly-only-scop-detection", cl::desc("Only run scop detection, but no other optimizations"), cl::cat(PollyCategory))
static cl::opt< bool > CFGPrinter("polly-view-cfg", cl::desc("Show the Polly CFG right after code generation"), cl::Hidden, cl::init(false), cl::cat(PollyCategory))
static cl::opt< bool > DeadCodeElim("polly-run-dce", cl::desc("Run the dead code elimination"), cl::Hidden, cl::cat(PollyCategory))
bool dependsOnDependenceInfo(PassPhase Phase)
static cl::opt< bool > EnablePruneUnprofitable("polly-enable-prune-unprofitable", cl::desc("Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden, cl::init(true), cl::cat(PollyCategory))
static cl::opt< bool > EnableDeLICM("polly-enable-delicm", cl::desc("Eliminate scalar loop carried dependences"), cl::Hidden, cl::init(true), cl::cat(PollyCategory))
PassPhase
Phases (in execution order) within the Polly pass.
static cl::opt< bool > PollyPrintScops("polly-print-scops", cl::desc("Print polyhedral description of all regions"), cl::cat(PollyCategory))
std::string ViewFilter
static cl::opt< bool > ImportJScop("polly-import", cl::desc("Import the polyhedral description of the detected Scops"), cl::Hidden, cl::cat(PollyCategory))
static cl::opt< bool > PollyOnlyPrinter("polly-dot-only", cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden, cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"), cl::init(false), cl::cat(PollyCategory))
llvm::FunctionPassManager buildCanonicalicationPassesForNPM(llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level)
Schedule a set of canonicalization passes to prepare for Polly.
static cl::opt< PassPositionChoice > PassPosition("polly-position", cl::desc("Where to run polly in the pass pipeline"), cl::values(clEnumValN(POSITION_EARLY, "early", "Before everything"), clEnumValN(POSITION_BEFORE_VECTORIZER, "before-vectorizer", "Right before the vectorizer")), cl::Hidden, cl::init(POSITION_BEFORE_VECTORIZER), cl::cat(PollyCategory))
static cl::list< std::string > DumpAfterFile("polly-dump-after-file", cl::desc("Dump module after Polly transformations to the given file"), cl::cat(PollyCategory))
static bool shouldEnablePollyForOptimization()
static llvm::Expected< std::monostate > parseNoOptions(StringRef Params)
static llvm::Expected< bool > parseModulePipeline(StringRef Name, llvm::ModulePassManager &MPM, PassInstrumentationCallbacks *PIC, ArrayRef< PassBuilder::PipelineElement > Pipeline)
static cl::opt< CodeGenChoice > CodeGeneration("polly-code-generation", cl::desc("How much code-generation to perform"), cl::values(clEnumValN(CODEGEN_FULL, "full", "AST and IR generation"), clEnumValN(CODEGEN_AST, "ast", "Only AST generation"), clEnumValN(CODEGEN_NONE, "none", "No code generation")), cl::Hidden, cl::init(CODEGEN_FULL), cl::cat(PollyCategory))
A pass that isolates a function into a new Module and writes it into a file.
A pass that prints the module into a file.