RegisterPasses.cpp 24 KB

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