1 //===------ RegisterPasses.cpp - Add the Polly Passes to default passes --===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file composes the individual LLVM-IR passes provided by Polly to a 11 // functional polyhedral optimizer. The polyhedral optimizer is automatically 12 // made available to LLVM based compilers by loading the Polly shared library 13 // into such a compiler. 14 // 15 // The Polly optimizer is made available by executing a static constructor that 16 // registers the individual Polly passes in the LLVM pass manager builder. The 17 // passes are registered such that the default behaviour of the compiler is not 18 // changed, but that the flag '-polly' provided at optimization level '-O3' 19 // enables additional polyhedral optimizations. 20 //===----------------------------------------------------------------------===// 21 22 #include "polly/RegisterPasses.h" 23 #include "polly/Canonicalization.h" 24 #include "polly/CodeGen/CodeGeneration.h" 25 #include "polly/CodeGen/CodegenCleanup.h" 26 #include "polly/DependenceInfo.h" 27 #include "polly/LinkAllPasses.h" 28 #include "polly/Options.h" 29 #include "polly/ScopDetection.h" 30 #include "polly/ScopInfo.h" 31 #include "llvm/Analysis/CFGPrinter.h" 32 #include "llvm/IR/LegacyPassManager.h" 33 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 34 #include "llvm/Transforms/Scalar.h" 35 #include "llvm/Transforms/Vectorize.h" 36 37 using namespace llvm; 38 using namespace polly; 39 40 cl::OptionCategory PollyCategory("Polly Options", 41 "Configure the polly loop optimizer"); 42 43 static cl::opt<bool> 44 PollyEnabled("polly", cl::desc("Enable the polly optimizer (only at -O3)"), 45 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 46 47 static cl::opt<bool> PollyDetectOnly( 48 "polly-only-scop-detection", 49 cl::desc("Only run scop detection, but no other optimizations"), 50 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 51 52 enum PassPositionChoice { 53 POSITION_EARLY, 54 POSITION_AFTER_LOOPOPT, 55 POSITION_BEFORE_VECTORIZER 56 }; 57 58 enum OptimizerChoice { OPTIMIZER_NONE, OPTIMIZER_ISL }; 59 60 static cl::opt<PassPositionChoice> PassPosition( 61 "polly-position", cl::desc("Where to run polly in the pass pipeline"), 62 cl::values( 63 clEnumValN(POSITION_EARLY, "early", "Before everything"), 64 clEnumValN(POSITION_AFTER_LOOPOPT, "after-loopopt", 65 "After the loop optimizer (but within the inline cycle)"), 66 clEnumValN(POSITION_BEFORE_VECTORIZER, "before-vectorizer", 67 "Right before the vectorizer"), 68 clEnumValEnd), 69 cl::Hidden, cl::init(POSITION_EARLY), cl::ZeroOrMore, 70 cl::cat(PollyCategory)); 71 72 static cl::opt<OptimizerChoice> Optimizer( 73 "polly-optimizer", cl::desc("Select the scheduling optimizer"), 74 cl::values(clEnumValN(OPTIMIZER_NONE, "none", "No optimizer"), 75 clEnumValN(OPTIMIZER_ISL, "isl", "The isl scheduling optimizer"), 76 clEnumValEnd), 77 cl::Hidden, cl::init(OPTIMIZER_ISL), cl::ZeroOrMore, 78 cl::cat(PollyCategory)); 79 80 enum CodeGenChoice { CODEGEN_ISL, CODEGEN_NONE }; 81 static cl::opt<CodeGenChoice> CodeGenerator( 82 "polly-code-generator", cl::desc("Select the code generator"), 83 cl::values(clEnumValN(CODEGEN_ISL, "isl", "isl code generator"), 84 clEnumValN(CODEGEN_NONE, "none", "no code generation"), 85 clEnumValEnd), 86 cl::Hidden, cl::init(CODEGEN_ISL), cl::ZeroOrMore, cl::cat(PollyCategory)); 87 88 VectorizerChoice polly::PollyVectorizerChoice; 89 static cl::opt<polly::VectorizerChoice, true> Vectorizer( 90 "polly-vectorizer", cl::desc("Select the vectorization strategy"), 91 cl::values( 92 clEnumValN(polly::VECTORIZER_NONE, "none", "No Vectorization"), 93 clEnumValN(polly::VECTORIZER_POLLY, "polly", 94 "Polly internal vectorizer"), 95 clEnumValN(polly::VECTORIZER_STRIPMINE, "stripmine", 96 "Strip-mine outer loops for the loop-vectorizer to trigger"), 97 clEnumValEnd), 98 cl::location(PollyVectorizerChoice), cl::init(polly::VECTORIZER_NONE), 99 cl::ZeroOrMore, cl::cat(PollyCategory)); 100 101 static cl::opt<bool> ImportJScop( 102 "polly-import", 103 cl::desc("Export the polyhedral description of the detected Scops"), 104 cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 105 106 static cl::opt<bool> ExportJScop( 107 "polly-export", 108 cl::desc("Export the polyhedral description of the detected Scops"), 109 cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 110 111 static cl::opt<bool> DeadCodeElim("polly-run-dce", 112 cl::desc("Run the dead code elimination"), 113 cl::Hidden, cl::init(false), cl::ZeroOrMore, 114 cl::cat(PollyCategory)); 115 116 static cl::opt<bool> PollyViewer( 117 "polly-show", 118 cl::desc("Highlight the code regions that will be optimized in a " 119 "(CFG BBs and LLVM-IR instructions)"), 120 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 121 122 static cl::opt<bool> PollyOnlyViewer( 123 "polly-show-only", 124 cl::desc("Highlight the code regions that will be optimized in " 125 "a (CFG only BBs)"), 126 cl::init(false), cl::cat(PollyCategory)); 127 128 static cl::opt<bool> 129 PollyPrinter("polly-dot", cl::desc("Enable the Polly DOT printer in -O3"), 130 cl::Hidden, cl::value_desc("Run the Polly DOT printer at -O3"), 131 cl::init(false), cl::cat(PollyCategory)); 132 133 static cl::opt<bool> PollyOnlyPrinter( 134 "polly-dot-only", 135 cl::desc("Enable the Polly DOT printer in -O3 (no BB content)"), cl::Hidden, 136 cl::value_desc("Run the Polly DOT printer at -O3 (no BB content"), 137 cl::init(false), cl::cat(PollyCategory)); 138 139 static cl::opt<bool> 140 CFGPrinter("polly-view-cfg", 141 cl::desc("Show the Polly CFG right after code generation"), 142 cl::Hidden, cl::init(false), cl::cat(PollyCategory)); 143 144 namespace polly { 145 void initializePollyPasses(PassRegistry &Registry) { 146 initializeCodeGenerationPass(Registry); 147 initializeCodePreparationPass(Registry); 148 initializeDeadCodeElimPass(Registry); 149 initializeDependenceInfoPass(Registry); 150 initializeJSONExporterPass(Registry); 151 initializeJSONImporterPass(Registry); 152 initializeIslAstInfoPass(Registry); 153 initializeIslScheduleOptimizerPass(Registry); 154 initializePollyCanonicalizePass(Registry); 155 initializeScopDetectionPass(Registry); 156 initializeScopInfoPass(Registry); 157 initializeCodegenCleanupPass(Registry); 158 } 159 160 /// @brief Register Polly passes such that they form a polyhedral optimizer. 161 /// 162 /// The individual Polly passes are registered in the pass manager such that 163 /// they form a full polyhedral optimizer. The flow of the optimizer starts with 164 /// a set of preparing transformations that canonicalize the LLVM-IR such that 165 /// the LLVM-IR is easier for us to understand and to optimizes. On the 166 /// canonicalized LLVM-IR we first run the ScopDetection pass, which detects 167 /// static control flow regions. Those regions are then translated by the 168 /// ScopInfo pass into a polyhedral representation. As a next step, a scheduling 169 /// optimizer is run on the polyhedral representation and finally the optimized 170 /// polyhedral representation is code generated back to LLVM-IR. 171 /// 172 /// Besides this core functionality, we optionally schedule passes that provide 173 /// a graphical view of the scops (Polly[Only]Viewer, Polly[Only]Printer), that 174 /// allow the export/import of the polyhedral representation 175 /// (JSCON[Exporter|Importer]) or that show the cfg after code generation. 176 /// 177 /// For certain parts of the Polly optimizer, several alternatives are provided: 178 /// 179 /// As scheduling optimizer we support the isl scheduling optimizer 180 /// (http://freecode.com/projects/isl). 181 /// It is also possible to run Polly with no optimizer. This mode is mainly 182 /// provided to analyze the run and compile time changes caused by the 183 /// scheduling optimizer. 184 /// 185 /// Polly supports the isl internal code generator. 186 void registerPollyPasses(llvm::legacy::PassManagerBase &PM) { 187 PM.add(polly::createScopDetectionPass()); 188 189 if (PollyDetectOnly) 190 return; 191 192 if (PollyViewer) 193 PM.add(polly::createDOTViewerPass()); 194 if (PollyOnlyViewer) 195 PM.add(polly::createDOTOnlyViewerPass()); 196 if (PollyPrinter) 197 PM.add(polly::createDOTPrinterPass()); 198 if (PollyOnlyPrinter) 199 PM.add(polly::createDOTOnlyPrinterPass()); 200 201 PM.add(polly::createScopInfoPass()); 202 203 if (ImportJScop) 204 PM.add(polly::createJSONImporterPass()); 205 206 if (DeadCodeElim) 207 PM.add(polly::createDeadCodeElimPass()); 208 209 switch (Optimizer) { 210 case OPTIMIZER_NONE: 211 break; /* Do nothing */ 212 213 case OPTIMIZER_ISL: 214 PM.add(polly::createIslScheduleOptimizerPass()); 215 break; 216 } 217 218 if (ExportJScop) 219 PM.add(polly::createJSONExporterPass()); 220 221 switch (CodeGenerator) { 222 case CODEGEN_ISL: 223 PM.add(polly::createCodeGenerationPass()); 224 break; 225 case CODEGEN_NONE: 226 break; 227 } 228 229 if (CFGPrinter) 230 PM.add(llvm::createCFGPrinterPass()); 231 } 232 233 static bool shouldEnablePolly() { 234 if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer) 235 PollyTrackFailures = true; 236 237 if (PollyOnlyPrinter || PollyPrinter || PollyOnlyViewer || PollyViewer || 238 ExportJScop || ImportJScop) 239 PollyEnabled = true; 240 241 return PollyEnabled; 242 } 243 244 static void 245 registerPollyEarlyAsPossiblePasses(const llvm::PassManagerBuilder &Builder, 246 llvm::legacy::PassManagerBase &PM) { 247 if (!polly::shouldEnablePolly()) 248 return; 249 250 if (PassPosition != POSITION_EARLY) 251 return; 252 253 registerCanonicalicationPasses(PM); 254 polly::registerPollyPasses(PM); 255 } 256 257 static void 258 registerPollyLoopOptimizerEndPasses(const llvm::PassManagerBuilder &Builder, 259 llvm::legacy::PassManagerBase &PM) { 260 if (!polly::shouldEnablePolly()) 261 return; 262 263 if (PassPosition != POSITION_AFTER_LOOPOPT) 264 return; 265 266 PM.add(polly::createCodePreparationPass()); 267 polly::registerPollyPasses(PM); 268 PM.add(createCodegenCleanupPass()); 269 } 270 271 static void 272 registerPollyScalarOptimizerLatePasses(const llvm::PassManagerBuilder &Builder, 273 llvm::legacy::PassManagerBase &PM) { 274 if (!polly::shouldEnablePolly()) 275 return; 276 277 if (PassPosition != POSITION_BEFORE_VECTORIZER) 278 return; 279 280 PM.add(polly::createCodePreparationPass()); 281 polly::registerPollyPasses(PM); 282 PM.add(createCodegenCleanupPass()); 283 } 284 285 /// @brief Register Polly to be available as an optimizer 286 /// 287 /// 288 /// We can currently run Polly at three different points int the pass manager. 289 /// a) very early, b) after the canonicalizing loop transformations and c) right 290 /// before the vectorizer. 291 /// 292 /// The default is currently a), to register Polly such that it runs as early as 293 /// possible. This has several implications: 294 /// 295 /// 1) We need to schedule more canonicalization passes 296 /// 297 /// As nothing is run before Polly, it is necessary to run a set of preparing 298 /// transformations before Polly to canonicalize the LLVM-IR and to allow 299 /// Polly to detect and understand the code. 300 /// 301 /// 2) LICM and LoopIdiom pass have not yet been run 302 /// 303 /// Loop invariant code motion as well as the loop idiom recognition pass make 304 /// it more difficult for Polly to transform code. LICM may introduce 305 /// additional data dependences that are hard to eliminate and the loop idiom 306 /// recognition pass may introduce calls to memset that we currently do not 307 /// understand. By running Polly early enough (meaning before these passes) we 308 /// avoid difficulties that may be introduced by these passes. 309 /// 310 /// 3) We get the full -O3 optimization sequence after Polly 311 /// 312 /// The LLVM-IR that is generated by Polly has been optimized on a high level, 313 /// but it may be rather inefficient on the lower/scalar level. By scheduling 314 /// Polly before all other passes, we have the full sequence of -O3 315 /// optimizations behind us, such that inefficiencies on the low level can 316 /// be optimized away. 317 /// 318 /// We are currently evaluating the benefit or running Polly at position b) or 319 /// c). b) is likely to early as it interacts with the inliner. c) is nice 320 /// as everything is fully inlined and canonicalized, but we need to be able 321 /// to handle LICMed code to make it useful. 322 static llvm::RegisterStandardPasses RegisterPollyOptimizerEarly( 323 llvm::PassManagerBuilder::EP_ModuleOptimizerEarly, 324 registerPollyEarlyAsPossiblePasses); 325 326 static llvm::RegisterStandardPasses 327 RegisterPollyOptimizerLoopEnd(llvm::PassManagerBuilder::EP_LoopOptimizerEnd, 328 registerPollyLoopOptimizerEndPasses); 329 330 static llvm::RegisterStandardPasses RegisterPollyOptimizerScalarLate( 331 llvm::PassManagerBuilder::EP_VectorizerStart, 332 registerPollyScalarOptimizerLatePasses); 333 } 334