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/Plugins/PassPlugin.h"
50#include "llvm/Support/CommandLine.h"
51#include "llvm/Support/Error.h"
52#include "llvm/Support/TargetSelect.h"
53#include "llvm/Support/raw_ostream.h"
54#include "llvm/Transforms/IPO.h"
59namespace cl = llvm::cl;
62using llvm::FunctionPassManager;
63using llvm::OptimizationLevel;
64using llvm::PassBuilder;
65using llvm::PassInstrumentationCallbacks;
68 "Configure the polly loop optimizer");
73 cl::desc(
"Enable the polly optimizer (with -O1, -O2 or -O3)"),
77 "polly-only-scop-detection",
78 cl::desc(
"Only run scop detection, but no other optimizations"),
86 "polly-position", cl::desc(
"Where to run polly in the pass pipeline"),
87 cl::values(clEnumValN(
POSITION_EARLY,
"early",
"Before everything"),
89 "Right before the vectorizer")),
92static cl::opt<OptimizerChoice>
93 Optimizer(
"polly-optimizer", cl::desc(
"Select the scheduling optimizer"),
96 "The isl scheduling optimizer")),
101 "polly-code-generation", cl::desc(
"How much code-generation to perform"),
102 cl::values(clEnumValN(
CODEGEN_FULL,
"full",
"AST and IR generation"),
103 clEnumValN(
CODEGEN_AST,
"ast",
"Only AST generation"),
104 clEnumValN(
CODEGEN_NONE,
"none",
"No code generation")),
110 "polly-vectorizer", cl::desc(
"Select the vectorization strategy"),
115 "Strip-mine outer loops for the loop-vectorizer to trigger")),
121 cl::desc(
"Import the polyhedral description of the detected Scops"),
126 cl::desc(
"Fully expand the memory accesses of the detected Scops"),
131 cl::desc(
"Export the polyhedral description of the detected Scops"),
135 cl::desc(
"Run the dead code elimination"),
140 cl::desc(
"Highlight the code regions that will be optimized in a "
141 "(CFG BBs and LLVM-IR instructions)"),
146 cl::desc(
"Highlight the code regions that will be optimized in "
151 PollyPrinter(
"polly-dot", cl::desc(
"Enable the Polly DOT printer in -O3"),
152 cl::Hidden, cl::value_desc(
"Run the Polly DOT printer at -O3"),
157 cl::desc(
"Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden,
158 cl::value_desc(
"Run the Polly DOT printer at -O3 (no BB content"),
163 cl::desc(
"Show the Polly CFG right after code generation"),
168 cl::desc(
"Enable operand tree forwarding"), cl::Hidden,
173 cl::desc(
"Dump module before Polly transformations into a file "
174 "suffixed with \"-before\""),
178 "polly-dump-before-file",
179 cl::desc(
"Dump module before Polly transformations to the given file"),
184 cl::desc(
"Dump module after Polly transformations into a file "
185 "suffixed with \"-after\""),
189 "polly-dump-after-file",
190 cl::desc(
"Dump module after Polly transformations to the given file"),
195 cl::desc(
"Eliminate scalar loop carried dependences"),
200 cl::desc(
"Simplify SCoP after optimizations"),
204 "polly-enable-prune-unprofitable",
205 cl::desc(
"Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden,
210 cl::desc(
"Polly - Print static control parts (SCoPs)"),
215 cl::desc(
"Print polyhedral description of all regions"),
219 cl::desc(
"Polly - Print dependences"),
239 bool EnableDefaultOpts = !IsCustom;
240 bool EnableEnd2End = !IsCustom;
303 while (!Params.empty()) {
305 std::tie(Param, Params) = Params.split(
';');
306 auto [ParamName, ParamVal] = Param.split(
'=');
308 if (ParamName ==
"stopafter") {
311 return make_error<StringError>(
312 formatv(
"invalid stopafter parameter value '{0}'", ParamVal).
str(),
313 inconvertibleErrorCode());
317 if (!ParamVal.empty())
318 return make_error<StringError>(
319 formatv(
"parameter '{0}' does not take value", ParamName).
str(),
320 inconvertibleErrorCode());
323 if (ParamName.starts_with(
"no-")) {
325 ParamName = ParamName.drop_front(3);
328 if (ParamName ==
"default-opts") {
329 EnableDefaultOpts = Enabled;
333 if (ParamName ==
"end2end") {
334 EnableEnd2End = Enabled;
341 if (ParamName ==
"simplify") {
348 return make_error<StringError>(
349 formatv(
"invalid Polly parameter/phase name '{0}'", ParamName)
351 inconvertibleErrorCode());
353 if (PrevPhase >= Phase)
354 return make_error<StringError>(
355 formatv(
"phases must not be repeated and enumerated in-order: "
356 "'{0}' listed before '{1}'",
359 inconvertibleErrorCode());
361 PassEnabled[
static_cast<size_t>(Phase)] = Enabled;
375 bool Enabled = PassEnabled[
static_cast<size_t>(P)].value_or(
false);
395 if (EnableDefaultOpts)
400 std::optional<bool> Enabled = PassEnabled[
static_cast<size_t>(P)];
403 if (Enabled.has_value())
416static llvm::Expected<PollyPassOptions>
421static llvm::Expected<PollyPassOptions>
462 OptimizationLevel Level,
463 IntrusiveRefCntPtr<vfs::FileSystem> FS,
469 nullptr, std::move(FS));
471 ExitOnError Err(
"Inconsistent Polly configuration: ");
476 PM.addPass(PB.buildFunctionSimplificationPipeline(
477 Level, llvm::ThinOrFullLTOPhase::None));
480 PM.addPass(llvm::CFGPrinterPass());
484 llvm::OptimizationLevel Level,
485 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
494 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
501 FPM = FunctionPassManager();
505 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
514 llvm::OptimizationLevel Level,
515 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
524 llvm::report_fatal_error(
525 "Option -polly-dump-before-file at -polly-position=late "
526 "not supported with NPM",
534 llvm::report_fatal_error(
535 "Option -polly-dump-after-file at -polly-position=late "
536 "not supported with NPM",
542 return make_error<StringError>(
543 formatv(
"'{0}' passed to pass that does not take any options", Params)
545 inconvertibleErrorCode());
547 return std::monostate{};
550static llvm::Expected<bool>
552 PassInstrumentationCallbacks *PIC,
553 ArrayRef<PassBuilder::PipelineElement> Pipeline,
554 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
555#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
556 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
557 auto Params = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
559 return Params.takeError(); \
560 CGPM.addPass(CREATE_PASS); \
563#include "PollyPasses.def"
568static llvm::Expected<bool>
570 PassInstrumentationCallbacks *PIC,
571 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
573#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
574 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
575 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
577 return ExpectedOpts.takeError(); \
578 auto &&Opts = *ExpectedOpts; \
580 FPM.addPass(CREATE_PASS); \
584#include "PollyPasses.def"
588static llvm::Expected<bool>
590 PassInstrumentationCallbacks *PIC,
591 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
592#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
593 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
594 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
596 return ExpectedOpts.takeError(); \
597 auto &&Opts = *ExpectedOpts; \
599 MPM.addPass(CREATE_PASS); \
603#include "PollyPasses.def"
635 PassInstrumentationCallbacks *PIC = PB.getPassInstrumentationCallbacks();
636 IntrusiveRefCntPtr<vfs::FileSystem> FS = PB.getVirtualFileSystemPtr();
638#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
640 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
642 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
644#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
646 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
648 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
650#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
652 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
654 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
656#include "PollyPasses.def"
658 PB.registerPipelineParsingCallback(
659 [PIC](StringRef Name, FunctionPassManager &FPM,
660 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
661 ExitOnError Err(
"Unable to parse Polly module pass: ");
664 PB.registerPipelineParsingCallback(
665 [PIC, FS](StringRef Name, CGSCCPassManager &CGPM,
666 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
667 ExitOnError Err(
"Unable to parse Polly call graph pass: ");
670 PB.registerPipelineParsingCallback(
671 [PIC](StringRef Name, ModulePassManager &MPM,
672 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
673 ExitOnError Err(
"Unable to parse Polly module pass: ");
679 PB.registerPipelineStartEPCallback(
680 [FS](ModulePassManager &MPM, OptimizationLevel Level) {
685 PB.registerVectorizerStartEPCallback(
686 [FS](FunctionPassManager &FPM, OptimizationLevel Level) {
695 SmallVector<const char *> Argv = {
"Polly"};
698 raw_string_ostream OS(Msg);
699 if (!cl::ParseCommandLineOptions(Argv.size(), Argv.data(),
"", &OS))
700 return createStringError(StringRef(Msg).trim());
701 return Error::success();
705 llvm::PassPluginLibraryInfo Info = {LLVM_PLUGIN_API_VERSION,
"Polly",
llvm::cl::OptionCategory PollyCategory
static Error parseArguments(ArrayRef< const char * > Args)
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 __isl_give isl_union_map * append_range(__isl_take isl_union_map *umap, int extra)
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))
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)
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))
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.