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"
60namespace cl = llvm::cl;
63using llvm::FunctionPassManager;
64using llvm::OptimizationLevel;
65using llvm::PassBuilder;
66using llvm::PassInstrumentationCallbacks;
69 "Configure the polly loop optimizer");
74 cl::desc(
"Enable the polly optimizer (with -O1, -O2 or -O3)"),
78 "polly-only-scop-detection",
79 cl::desc(
"Only run scop detection, but no other optimizations"),
87 "polly-position", cl::desc(
"Where to run polly in the pass pipeline"),
88 cl::values(clEnumValN(
POSITION_EARLY,
"early",
"Before everything"),
90 "Right before the vectorizer")),
93static cl::opt<OptimizerChoice>
94 Optimizer(
"polly-optimizer", cl::desc(
"Select the scheduling optimizer"),
97 "The isl scheduling optimizer")),
102 "polly-code-generation", cl::desc(
"How much code-generation to perform"),
103 cl::values(clEnumValN(
CODEGEN_FULL,
"full",
"AST and IR generation"),
104 clEnumValN(
CODEGEN_AST,
"ast",
"Only AST generation"),
105 clEnumValN(
CODEGEN_NONE,
"none",
"No code generation")),
111 "polly-vectorizer", cl::desc(
"Select the vectorization strategy"),
116 "Strip-mine outer loops for the loop-vectorizer to trigger")),
122 cl::desc(
"Import the polyhedral description of the detected Scops"),
127 cl::desc(
"Fully expand the memory accesses of the detected Scops"),
132 cl::desc(
"Export the polyhedral description of the detected Scops"),
136 cl::desc(
"Run the dead code elimination"),
141 cl::desc(
"Highlight the code regions that will be optimized in a "
142 "(CFG BBs and LLVM-IR instructions)"),
147 cl::desc(
"Highlight the code regions that will be optimized in "
152 PollyPrinter(
"polly-dot", cl::desc(
"Enable the Polly DOT printer in -O3"),
153 cl::Hidden, cl::value_desc(
"Run the Polly DOT printer at -O3"),
158 cl::desc(
"Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden,
159 cl::value_desc(
"Run the Polly DOT printer at -O3 (no BB content"),
164 cl::desc(
"Show the Polly CFG right after code generation"),
169 cl::desc(
"Enable operand tree forwarding"), cl::Hidden,
174 cl::desc(
"Dump module before Polly transformations into a file "
175 "suffixed with \"-before\""),
179 "polly-dump-before-file",
180 cl::desc(
"Dump module before Polly transformations to the given file"),
185 cl::desc(
"Dump module after Polly transformations into a file "
186 "suffixed with \"-after\""),
190 "polly-dump-after-file",
191 cl::desc(
"Dump module after Polly transformations to the given file"),
196 cl::desc(
"Eliminate scalar loop carried dependences"),
201 cl::desc(
"Simplify SCoP after optimizations"),
205 "polly-enable-prune-unprofitable",
206 cl::desc(
"Bail out on unprofitable SCoPs before rescheduling"), cl::Hidden,
211 cl::desc(
"Polly - Print static control parts (SCoPs)"),
216 cl::desc(
"Print polyhedral description of all regions"),
220 cl::desc(
"Polly - Print dependences"),
240 bool EnableDefaultOpts = !IsCustom;
241 bool EnableEnd2End = !IsCustom;
304 while (!Params.empty()) {
306 std::tie(Param, Params) = Params.split(
';');
307 auto [ParamName, ParamVal] = Param.split(
'=');
309 if (ParamName ==
"stopafter") {
312 return make_error<StringError>(
313 formatv(
"invalid stopafter parameter value '{0}'", ParamVal).
str(),
314 inconvertibleErrorCode());
318 if (!ParamVal.empty())
319 return make_error<StringError>(
320 formatv(
"parameter '{0}' does not take value", ParamName).
str(),
321 inconvertibleErrorCode());
324 if (ParamName.starts_with(
"no-")) {
326 ParamName = ParamName.drop_front(3);
329 if (ParamName ==
"default-opts") {
330 EnableDefaultOpts = Enabled;
334 if (ParamName ==
"end2end") {
335 EnableEnd2End = Enabled;
342 if (ParamName ==
"simplify") {
349 return make_error<StringError>(
350 formatv(
"invalid Polly parameter/phase name '{0}'", ParamName)
352 inconvertibleErrorCode());
354 if (PrevPhase >= Phase)
355 return make_error<StringError>(
356 formatv(
"phases must not be repeated and enumerated in-order: "
357 "'{0}' listed before '{1}'",
360 inconvertibleErrorCode());
362 PassEnabled[
static_cast<size_t>(Phase)] = Enabled;
376 bool Enabled = PassEnabled[
static_cast<size_t>(P)].value_or(
false);
396 if (EnableDefaultOpts)
401 std::optional<bool> Enabled = PassEnabled[
static_cast<size_t>(P)];
404 if (Enabled.has_value())
417static llvm::Expected<PollyPassOptions>
422static llvm::Expected<PollyPassOptions>
463 FunctionPassManager &PM,
464 OptimizationLevel Level,
465 IntrusiveRefCntPtr<vfs::FileSystem> FS,
467 PassBuilder PB(Context,
nullptr, std::nullopt,
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::ModulePassManager &MPM,
485 llvm::OptimizationLevel Level,
486 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
492 FunctionPassManager FPM =
496 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
503 FPM = FunctionPassManager();
507 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
516 FunctionPassManager &PM,
517 llvm::OptimizationLevel Level,
518 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
527 llvm::report_fatal_error(
528 "Option -polly-dump-before-file at -polly-position=late "
529 "not supported with NPM",
537 llvm::report_fatal_error(
538 "Option -polly-dump-after-file at -polly-position=late "
539 "not supported with NPM",
545 return make_error<StringError>(
546 formatv(
"'{0}' passed to pass that does not take any options", Params)
548 inconvertibleErrorCode());
550 return std::monostate{};
553static llvm::Expected<bool>
555 PassInstrumentationCallbacks *PIC,
556 ArrayRef<PassBuilder::PipelineElement> Pipeline,
557 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
558#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
559 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
560 auto Params = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
562 return Params.takeError(); \
563 CGPM.addPass(CREATE_PASS); \
566#include "PollyPasses.def"
571static llvm::Expected<bool>
573 PassInstrumentationCallbacks *PIC,
574 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
576#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
577 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
578 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
580 return ExpectedOpts.takeError(); \
581 auto &&Opts = *ExpectedOpts; \
583 FPM.addPass(CREATE_PASS); \
587#include "PollyPasses.def"
591static llvm::Expected<bool>
593 PassInstrumentationCallbacks *PIC,
594 ArrayRef<PassBuilder::PipelineElement> Pipeline) {
595#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
596 if (PassBuilder::checkParametrizedPassName(Name, NAME)) { \
597 auto ExpectedOpts = PassBuilder::parsePassParameters(PARSER, Name, NAME); \
599 return ExpectedOpts.takeError(); \
600 auto &&Opts = *ExpectedOpts; \
602 MPM.addPass(CREATE_PASS); \
606#include "PollyPasses.def"
638 PassInstrumentationCallbacks *PIC = PB.getPassInstrumentationCallbacks();
639 IntrusiveRefCntPtr<vfs::FileSystem> FS = PB.getVirtualFileSystemPtr();
640 LLVMContext &Context = PB.getContext();
642#define MODULE_PASS(NAME, CREATE_PASS, PARSER) \
644 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
646 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
648#define CGSCC_PASS(NAME, CREATE_PASS, PARSER) \
650 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
652 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
654#define FUNCTION_PASS(NAME, CREATE_PASS, PARSER) \
656 std::remove_reference_t<decltype(*PARSER(StringRef()))> Opts; \
658 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); \
660#include "PollyPasses.def"
662 PB.registerPipelineParsingCallback(
663 [PIC](StringRef Name, FunctionPassManager &FPM,
664 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
665 ExitOnError Err(
"Unable to parse Polly module pass: ");
668 PB.registerPipelineParsingCallback(
669 [PIC, FS](StringRef Name, CGSCCPassManager &CGPM,
670 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
671 ExitOnError Err(
"Unable to parse Polly call graph pass: ");
674 PB.registerPipelineParsingCallback(
675 [PIC](StringRef Name, ModulePassManager &MPM,
676 ArrayRef<PassBuilder::PipelineElement> Pipeline) ->
bool {
677 ExitOnError Err(
"Unable to parse Polly module pass: ");
683 PB.registerPipelineStartEPCallback(
684 [FS, &Context](ModulePassManager &MPM, OptimizationLevel Level) {
689 PB.registerVectorizerStartEPCallback(
690 [FS, &Context](FunctionPassManager &FPM, OptimizationLevel Level) {
699 SmallVector<const char *> Argv = {
"Polly"};
702 raw_string_ostream OS(Msg);
703 if (!cl::ParseCommandLineOptions(Argv.size(), Argv.data(),
"", &OS))
704 return createStringError(StringRef(Msg).trim());
705 return Error::success();
709 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 bool shouldEnablePollyForDiagnostic()
static llvm::Expected< bool > parseFunctionPipeline(StringRef Name, FunctionPassManager &FPM, PassInstrumentationCallbacks *PIC, ArrayRef< PassBuilder::PipelineElement > Pipeline)
PassPhase parsePhase(StringRef Name)
static void buildLatePollyPipeline(LLVMContext &Context, 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 void buildEarlyPollyPipeline(LLVMContext &Context, llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level, IntrusiveRefCntPtr< vfs::FileSystem > FS)
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 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))
llvm::FunctionPassManager buildCanonicalicationPassesForNPM(llvm::LLVMContext &Context, llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level)
Schedule a set of canonicalization passes to prepare for Polly.
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))
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 buildCommonPollyPipeline(LLVMContext &Context, FunctionPassManager &PM, OptimizationLevel Level, IntrusiveRefCntPtr< vfs::FileSystem > FS, bool EnableForOpt)
Register Polly passes such that they form a polyhedral optimizer.
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.