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