43#include "llvm/Analysis/CFGPrinter.h"
44#include "llvm/Config/llvm-config.h"
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/Passes/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"
58namespace cl = llvm::cl;
61using llvm::FunctionPassManager;
62using llvm::OptimizationLevel;
63using llvm::PassBuilder;
64using llvm::PassInstrumentationCallbacks;
67 "Configure the polly loop optimizer");
72 cl::desc(
"Enable the polly optimizer (with -O1, -O2 or -O3)"),
76 "polly-only-scop-detection",
77 cl::desc(
"Only run scop detection, but no other optimizations"),
85 "polly-position", cl::desc(
"Where to run polly in the pass pipeline"),
86 cl::values(clEnumValN(
POSITION_EARLY,
"early",
"Before everything"),
88 "Right before the vectorizer")),
91static cl::opt<OptimizerChoice>
92 Optimizer(
"polly-optimizer", cl::desc(
"Select the scheduling optimizer"),
95 "The isl scheduling optimizer")),
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")),
109 "polly-vectorizer", cl::desc(
"Select the vectorization strategy"),
114 "Strip-mine outer loops for the loop-vectorizer to trigger")),
120 cl::desc(
"Import the polyhedral description of the detected Scops"),
125 cl::desc(
"Fully expand the memory accesses of the detected Scops"),
130 cl::desc(
"Export the polyhedral description of the detected Scops"),
134 cl::desc(
"Run the dead code elimination"),
139 cl::desc(
"Highlight the code regions that will be optimized in a "
140 "(CFG BBs and LLVM-IR instructions)"),
145 cl::desc(
"Highlight the code regions that will be optimized in "
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"),
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"),
162 cl::desc(
"Show the Polly CFG right after code generation"),
167 cl::desc(
"Enable operand tree forwarding"), cl::Hidden,
172 cl::desc(
"Dump module before Polly transformations into a file "
173 "suffixed with \"-before\""),
177 "polly-dump-before-file",
178 cl::desc(
"Dump module before Polly transformations to the given file"),
183 cl::desc(
"Dump module after Polly transformations into a file "
184 "suffixed with \"-after\""),
188 "polly-dump-after-file",
189 cl::desc(
"Dump module after Polly transformations to the given file"),
194 cl::desc(
"Eliminate scalar loop carried dependences"),
199 cl::desc(
"Simplify SCoP after optimizations"),
203 "polly-enable-prune-unprofitable",
204 cl::desc(
"Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden,
209 cl::desc(
"Polly - Print static control parts (SCoPs)"),
214 cl::desc(
"Print polyhedral description of all regions"),
218 cl::desc(
"Polly - Print dependences"),
238 bool EnableDefaultOpts = !IsCustom;
239 bool EnableEnd2End = !IsCustom;
302 while (!Params.empty()) {
304 std::tie(Param, Params) = Params.split(
';');
305 auto [ParamName, ParamVal] = Param.split(
'=');
307 if (ParamName ==
"stopafter") {
310 return make_error<StringError>(
311 formatv(
"invalid stopafter parameter value '{0}'", ParamVal).
str(),
312 inconvertibleErrorCode());
316 if (!ParamVal.empty())
317 return make_error<StringError>(
318 formatv(
"parameter '{0}' does not take value", ParamName).
str(),
319 inconvertibleErrorCode());
322 if (ParamName.starts_with(
"no-")) {
324 ParamName = ParamName.drop_front(3);
327 if (ParamName ==
"default-opts") {
328 EnableDefaultOpts = Enabled;
332 if (ParamName ==
"end2end") {
333 EnableEnd2End = Enabled;
340 if (ParamName ==
"simplify") {
347 return make_error<StringError>(
348 formatv(
"invalid Polly parameter/phase name '{0}'", ParamName)
350 inconvertibleErrorCode());
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}'",
358 inconvertibleErrorCode());
360 PassEnabled[
static_cast<size_t>(Phase)] = Enabled;
374 bool Enabled = PassEnabled[
static_cast<size_t>(P)].value_or(
false);
394 if (EnableDefaultOpts)
399 std::optional<bool> Enabled = PassEnabled[
static_cast<size_t>(P)];
402 if (Enabled.has_value())
415static llvm::Expected<PollyPassOptions>
420static llvm::Expected<PollyPassOptions>
461 OptimizationLevel Level,
465 ExitOnError Err(
"Inconsistent Polly configuration: ");
470 PM.addPass(PB.buildFunctionSimplificationPipeline(
471 Level, llvm::ThinOrFullLTOPhase::None));
474 PM.addPass(llvm::CFGPrinterPass());
478 llvm::OptimizationLevel Level) {
487 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
494 FPM = FunctionPassManager();
498 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
507 llvm::OptimizationLevel Level) {
516 llvm::report_fatal_error(
517 "Option -polly-dump-before-file at -polly-position=late "
518 "not supported with NPM",
526 llvm::report_fatal_error(
527 "Option -polly-dump-after-file at -polly-position=late "
528 "not supported with NPM",
534 return make_error<StringError>(
535 formatv(
"'{0}' passed to pass that does not take any options", Params)
537 inconvertibleErrorCode());
539 return std::monostate{};
542static llvm::Expected<bool>
544 PassInstrumentationCallbacks *PIC,
545 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
546#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
547 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
548 auto Params = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
550 return Params.takeError(); \
551 CGPM.addPass(CREATE_PASS); \
554#include "PollyPasses.def"
559static llvm::Expected<bool>
561 PassInstrumentationCallbacks *PIC,
562 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
564#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
565 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
566 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
568 return ExpectedOpts.takeError(); \
569 auto &&Opts = *ExpectedOpts; \
571 FPM.addPass(CREATE_PASS); \
575#include "PollyPasses.def"
579static llvm::Expected<bool>
581 PassInstrumentationCallbacks *PIC,
582 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
583#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
584 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
585 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
587 return ExpectedOpts.takeError(); \
588 auto &&Opts = *ExpectedOpts; \
590 MPM.addPass(CREATE_PASS); \
594#include "PollyPasses.def"
626 PassInstrumentationCallbacks *PIC = PB.getPassInstrumentationCallbacks();
628#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
630 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
632 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
634#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
636 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
638 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
640#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
642 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
644 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
646#include "PollyPasses.def"
648 PB.registerPipelineParsingCallback(
649 [PIC](StringRef Name, FunctionPassManager &FPM,
650 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
651 ExitOnError Err(
"Unable to parse Polly module pass: ");
654 PB.registerPipelineParsingCallback(
655 [PIC](StringRef Name, CGSCCPassManager &CGPM,
656 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
657 ExitOnError Err(
"Unable to parse Polly call graph pass: ");
660 PB.registerPipelineParsingCallback(
661 [PIC](StringRef Name, ModulePassManager &MPM,
662 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
663 ExitOnError Err(
"Unable to parse Polly module pass: ");
679 return {LLVM_PLUGIN_API_VERSION,
"Polly", LLVM_VERSION_STRING,
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.
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 bool shouldEnablePollyForDiagnostic()
static llvm::Expected< bool > parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM, PassInstrumentationCallbacks *PIC, ArrayRef< PassBuilder::PipelineElement > Pipeline)
PassPhase parsePhase(StringRef Name)
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 void buildLatePollyPipeline(FunctionPassManager &PM, llvm::OptimizationLevel Level)
static cl::opt< bool > EnableSimplify("polly-enable-simplify", cl::desc("Simplify SCoP after optimizations"), cl::init(true), cl::cat(PollyCategory))
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))
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 llvm::Expected< bool > parseCGPipeline(StringRef Name, llvm::CGSCCPassManager &CGPM, PassInstrumentationCallbacks *PIC, ArrayRef< PassBuilder::PipelineElement > Pipeline)
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 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))
static void buildCommonPollyPipeline(FunctionPassManager &PM, OptimizationLevel Level, bool EnableForOpt)
Register Polly passes such that they form a polyhedral optimizer.
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))
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 void buildEarlyPollyPipeline(llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level)
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.