| ... | ... | @@ -20,6 +20,7 @@ |
| 20 | 20 | #pragma GCC diagnostic ignored "-Winit-list-lifetime" |
| 21 | 21 | #endif |
| 22 | 22 | |
| 23 | #include <llvm/Analysis/AliasAnalysis.h> |
| 23 | 24 | #include <llvm/Analysis/TargetLibraryInfo.h> |
| 24 | 25 | #include <llvm/Analysis/TargetTransformInfo.h> |
| 25 | 26 | #include <llvm/Bitcode/BitcodeWriter.h> |
| ... | ... | @@ -30,9 +31,12 @@ |
| 30 | 31 | #include <llvm/IR/Instructions.h> |
| 31 | 32 | #include <llvm/IR/LegacyPassManager.h> |
| 32 | 33 | #include <llvm/IR/Module.h> |
| 34 | #include <llvm/IR/PassManager.h> |
| 33 | 35 | #include <llvm/IR/Verifier.h> |
| 34 | 36 | #include <llvm/InitializePasses.h> |
| 35 | 37 | #include <llvm/MC/SubtargetFeature.h> |
| 38 | #include <llvm/Passes/PassBuilder.h> |
| 39 | #include <llvm/Passes/StandardInstrumentations.h> |
| 36 | 40 | #include <llvm/Object/Archive.h> |
| 37 | 41 | #include <llvm/Object/ArchiveWriter.h> |
| 38 | 42 | #include <llvm/Object/COFF.h> |
| ... | ... | @@ -54,6 +58,9 @@ |
| 54 | 58 | #include <llvm/Transforms/Instrumentation/ThreadSanitizer.h> |
| 55 | 59 | #include <llvm/Transforms/Scalar.h> |
| 56 | 60 | #include <llvm/Transforms/Utils.h> |
| 61 | #include <llvm/Transforms/Utils/AddDiscriminators.h> |
| 62 | #include <llvm/Transforms/Utils/CanonicalizeAliases.h> |
| 63 | #include <llvm/Transforms/Utils/NameAnonGlobals.h> |
| 57 | 64 | |
| 58 | 65 | #include <lld/Common/Driver.h> |
| 59 | 66 | |
| ... | ... | @@ -91,14 +98,6 @@ char *ZigLLVMGetNativeFeatures(void) { |
| 91 | 98 | return strdup((const char *)StringRef(features.getString()).bytes_begin()); |
| 92 | 99 | } |
| 93 | 100 | |
| 94 | | static void addDiscriminatorsPass(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) { |
| 95 | | PM.add(createAddDiscriminatorsPass()); |
| 96 | | } |
| 97 | | |
| 98 | | static void addThreadSanitizerPass(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) { |
| 99 | | PM.add(createThreadSanitizerLegacyPassPass()); |
| 100 | | } |
| 101 | | |
| 102 | 101 | #ifndef NDEBUG |
| 103 | 102 | static const bool assertions_on = true; |
| 104 | 103 | #else |
| ... | ... | @@ -193,14 +192,16 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM |
| 193 | 192 | bool is_small, bool time_report, bool tsan, bool lto, |
| 194 | 193 | const char *asm_filename, const char *bin_filename, const char *llvm_ir_filename) |
| 195 | 194 | { |
| 195 | // TODO: Maybe we should collect time trace rather than using timer |
| 196 | // to get a more hierarchical timeline view |
| 196 | 197 | TimePassesIsEnabled = time_report; |
| 197 | 198 | |
| 198 | | raw_fd_ostream *dest_asm = nullptr; |
| 199 | | raw_fd_ostream *dest_bin = nullptr; |
| 199 | raw_fd_ostream *dest_asm_ptr = nullptr; |
| 200 | raw_fd_ostream *dest_bin_ptr = nullptr; |
| 200 | 201 | |
| 201 | 202 | if (asm_filename) { |
| 202 | 203 | std::error_code EC; |
| 203 | | dest_asm = new(std::nothrow) raw_fd_ostream(asm_filename, EC, sys::fs::F_None); |
| 204 | dest_asm_ptr = new(std::nothrow) raw_fd_ostream(asm_filename, EC, sys::fs::F_None); |
| 204 | 205 | if (EC) { |
| 205 | 206 | *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin()); |
| 206 | 207 | return true; |
| ... | ... | @@ -208,116 +209,154 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM |
| 208 | 209 | } |
| 209 | 210 | if (bin_filename) { |
| 210 | 211 | std::error_code EC; |
| 211 | | dest_bin = new(std::nothrow) raw_fd_ostream(bin_filename, EC, sys::fs::F_None); |
| 212 | dest_bin_ptr = new(std::nothrow) raw_fd_ostream(bin_filename, EC, sys::fs::F_None); |
| 212 | 213 | if (EC) { |
| 213 | 214 | *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin()); |
| 214 | 215 | return true; |
| 215 | 216 | } |
| 216 | 217 | } |
| 217 | 218 | |
| 218 | | TargetMachine* target_machine = reinterpret_cast<TargetMachine*>(targ_machine_ref); |
| 219 | | target_machine->setO0WantsFastISel(true); |
| 220 | | |
| 221 | | Module* module = unwrap(module_ref); |
| 222 | | |
| 223 | | PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder(); |
| 224 | | if (PMBuilder == nullptr) { |
| 225 | | *error_message = strdup("memory allocation failure"); |
| 226 | | return true; |
| 219 | std::unique_ptr<raw_fd_ostream> dest_asm(dest_asm_ptr), |
| 220 | dest_bin(dest_bin_ptr); |
| 221 | |
| 222 | TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref); |
| 223 | target_machine.setO0WantsFastISel(true); |
| 224 | |
| 225 | Module &module = *unwrap(module_ref); |
| 226 | |
| 227 | // Pipeline configurations |
| 228 | PipelineTuningOptions pipeline_opts; |
| 229 | pipeline_opts.LoopUnrolling = !is_debug; |
| 230 | pipeline_opts.SLPVectorization = !is_debug; |
| 231 | pipeline_opts.LoopVectorization = !is_debug; |
| 232 | pipeline_opts.LoopInterleaving = !is_debug; |
| 233 | pipeline_opts.MergeFunctions = !is_debug; |
| 234 | |
| 235 | // Instrumentations |
| 236 | PassInstrumentationCallbacks instr_callbacks; |
| 237 | StandardInstrumentations std_instrumentations(false); |
| 238 | std_instrumentations.registerCallbacks(instr_callbacks); |
| 239 | |
| 240 | PassBuilder pass_builder(false, &target_machine, pipeline_opts, |
| 241 | None, &instr_callbacks); |
| 242 | using OptimizationLevel = typename PassBuilder::OptimizationLevel; |
| 243 | |
| 244 | LoopAnalysisManager loop_am; |
| 245 | FunctionAnalysisManager function_am; |
| 246 | CGSCCAnalysisManager cgscc_am; |
| 247 | ModuleAnalysisManager module_am; |
| 248 | |
| 249 | // Register the AA manager first so that our version is the one used |
| 250 | function_am.registerPass([&] { |
| 251 | return pass_builder.buildDefaultAAPipeline(); |
| 252 | }); |
| 253 | |
| 254 | Triple target_triple(module.getTargetTriple()); |
| 255 | auto tlii = std::make_unique<TargetLibraryInfoImpl>(target_triple); |
| 256 | function_am.registerPass([&] { return TargetLibraryAnalysis(*tlii); }); |
| 257 | |
| 258 | // Initialize the AnalysisManagers |
| 259 | pass_builder.registerModuleAnalyses(module_am); |
| 260 | pass_builder.registerCGSCCAnalyses(cgscc_am); |
| 261 | pass_builder.registerFunctionAnalyses(function_am); |
| 262 | pass_builder.registerLoopAnalyses(loop_am); |
| 263 | pass_builder.crossRegisterProxies(loop_am, function_am, |
| 264 | cgscc_am, module_am); |
| 265 | |
| 266 | // IR verification |
| 267 | if (assertions_on) { |
| 268 | // Verify the input |
| 269 | pass_builder.registerPipelineStartEPCallback( |
| 270 | [](ModulePassManager &module_pm, OptimizationLevel OL) { |
| 271 | module_pm.addPass(VerifierPass()); |
| 272 | }); |
| 273 | // Verify the output |
| 274 | pass_builder.registerOptimizerLastEPCallback( |
| 275 | [](ModulePassManager &module_pm, OptimizationLevel OL) { |
| 276 | module_pm.addPass(VerifierPass()); |
| 277 | }); |
| 227 | 278 | } |
| 228 | | PMBuilder->OptLevel = target_machine->getOptLevel(); |
| 229 | | PMBuilder->SizeLevel = is_small ? 2 : 0; |
| 230 | | |
| 231 | | PMBuilder->DisableTailCalls = is_debug; |
| 232 | | PMBuilder->DisableUnrollLoops = is_debug; |
| 233 | | PMBuilder->SLPVectorize = !is_debug; |
| 234 | | PMBuilder->LoopVectorize = !is_debug; |
| 235 | | PMBuilder->LoopsInterleaved = !PMBuilder->DisableUnrollLoops; |
| 236 | | PMBuilder->RerollLoops = !is_debug; |
| 237 | | // Leaving NewGVN as default (off) because when on it caused issue #673 |
| 238 | | //PMBuilder->NewGVN = !is_debug; |
| 239 | | PMBuilder->DisableGVNLoadPRE = is_debug; |
| 240 | | PMBuilder->VerifyInput = assertions_on; |
| 241 | | PMBuilder->VerifyOutput = assertions_on; |
| 242 | | PMBuilder->MergeFunctions = !is_debug; |
| 243 | | PMBuilder->PrepareForLTO = lto; |
| 244 | | PMBuilder->PrepareForThinLTO = false; |
| 245 | | PMBuilder->PerformThinLTO = false; |
| 246 | | |
| 247 | | TargetLibraryInfoImpl tlii(Triple(module->getTargetTriple())); |
| 248 | | PMBuilder->LibraryInfo = &tlii; |
| 249 | 279 | |
| 280 | // Passes for either debug or release build |
| 250 | 281 | if (is_debug) { |
| 251 | | PMBuilder->Inliner = createAlwaysInlinerLegacyPass(false); |
| 282 | // NOTE: Always inliner will go away (in debug build) |
| 283 | // when the self-hosted compiler becomes mature. |
| 284 | pass_builder.registerPipelineStartEPCallback( |
| 285 | [](ModulePassManager &module_pm, OptimizationLevel OL) { |
| 286 | module_pm.addPass(AlwaysInlinerPass()); |
| 287 | }); |
| 252 | 288 | } else { |
| 253 | | target_machine->adjustPassManager(*PMBuilder); |
| 254 | | |
| 255 | | PMBuilder->addExtension(PassManagerBuilder::EP_EarlyAsPossible, addDiscriminatorsPass); |
| 256 | | PMBuilder->Inliner = createFunctionInliningPass(PMBuilder->OptLevel, PMBuilder->SizeLevel, false); |
| 289 | pass_builder.registerPipelineStartEPCallback( |
| 290 | [](ModulePassManager &module_pm, OptimizationLevel OL) { |
| 291 | module_pm.addPass( |
| 292 | createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); |
| 293 | }); |
| 257 | 294 | } |
| 258 | 295 | |
| 296 | // Thread sanitizer |
| 259 | 297 | if (tsan) { |
| 260 | | PMBuilder->addExtension(PassManagerBuilder::EP_OptimizerLast, addThreadSanitizerPass); |
| 261 | | PMBuilder->addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, addThreadSanitizerPass); |
| 298 | pass_builder.registerOptimizerLastEPCallback( |
| 299 | [](ModulePassManager &module_pm, OptimizationLevel level) { |
| 300 | module_pm.addPass(ThreadSanitizerPass()); |
| 301 | }); |
| 262 | 302 | } |
| 263 | 303 | |
| 264 | | // Set up the per-function pass manager. |
| 265 | | legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module); |
| 266 | | auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii); |
| 267 | | FPM.add(tliwp); |
| 268 | | FPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis())); |
| 269 | | if (assertions_on) { |
| 270 | | FPM.add(createVerifierPass()); |
| 304 | ModulePassManager module_pm; |
| 305 | OptimizationLevel opt_level; |
| 306 | // Setting up the optimization level |
| 307 | if (is_debug) |
| 308 | opt_level = OptimizationLevel::O0; |
| 309 | else if (is_small) |
| 310 | opt_level = OptimizationLevel::Oz; |
| 311 | else |
| 312 | opt_level = OptimizationLevel::O3; |
| 313 | |
| 314 | // Initialize the PassManager |
| 315 | if (lto) { |
| 316 | module_pm = pass_builder.buildLTOPreLinkDefaultPipeline(opt_level); |
| 317 | module_pm.addPass(CanonicalizeAliasesPass()); |
| 318 | module_pm.addPass(NameAnonGlobalPass()); |
| 319 | } else { |
| 320 | module_pm = pass_builder.buildPerModuleDefaultPipeline(opt_level); |
| 271 | 321 | } |
| 272 | | PMBuilder->populateFunctionPassManager(FPM); |
| 273 | | |
| 274 | | { |
| 275 | | // Set up the per-module pass manager. |
| 276 | | legacy::PassManager MPM; |
| 277 | | MPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis())); |
| 278 | | PMBuilder->populateModulePassManager(MPM); |
| 279 | | |
| 280 | | // Set output passes. |
| 281 | | if (dest_bin && !lto) { |
| 282 | | if (target_machine->addPassesToEmitFile(MPM, *dest_bin, nullptr, CGFT_ObjectFile)) { |
| 283 | | *error_message = strdup("TargetMachine can't emit an object file"); |
| 284 | | return true; |
| 285 | | } |
| 286 | | } |
| 287 | | if (dest_asm) { |
| 288 | | if (target_machine->addPassesToEmitFile(MPM, *dest_asm, nullptr, CGFT_AssemblyFile)) { |
| 289 | | *error_message = strdup("TargetMachine can't emit an assembly file"); |
| 290 | | return true; |
| 291 | | } |
| 292 | | } |
| 293 | | |
| 294 | | // run per function optimization passes |
| 295 | | FPM.doInitialization(); |
| 296 | | for (Function &F : *module) |
| 297 | | if (!F.isDeclaration()) |
| 298 | | FPM.run(F); |
| 299 | | FPM.doFinalization(); |
| 300 | 322 | |
| 301 | | MPM.run(*module); |
| 323 | // Unfortunately we don't have new PM for code generation |
| 324 | legacy::PassManager codegen_pm; |
| 325 | codegen_pm.add( |
| 326 | createTargetTransformInfoWrapperPass(target_machine.getTargetIRAnalysis())); |
| 302 | 327 | |
| 303 | | if (llvm_ir_filename) { |
| 304 | | if (LLVMPrintModuleToFile(module_ref, llvm_ir_filename, error_message)) { |
| 305 | | return true; |
| 306 | | } |
| 328 | if (dest_bin && !lto) { |
| 329 | if (target_machine.addPassesToEmitFile(codegen_pm, *dest_bin, nullptr, CGFT_ObjectFile)) { |
| 330 | *error_message = strdup("TargetMachine can't emit an object file"); |
| 331 | return true; |
| 307 | 332 | } |
| 308 | | if (dest_bin && lto) { |
| 309 | | WriteBitcodeToFile(*module, *dest_bin); |
| 333 | } |
| 334 | if (dest_asm) { |
| 335 | if (target_machine.addPassesToEmitFile(codegen_pm, *dest_asm, nullptr, CGFT_AssemblyFile)) { |
| 336 | *error_message = strdup("TargetMachine can't emit an assembly file"); |
| 337 | return true; |
| 310 | 338 | } |
| 339 | } |
| 311 | 340 | |
| 312 | | if (time_report) { |
| 313 | | TimerGroup::printAll(errs()); |
| 341 | // Optimization phase |
| 342 | module_pm.run(module, module_am); |
| 343 | |
| 344 | // Code generation phase |
| 345 | codegen_pm.run(module); |
| 346 | |
| 347 | if (llvm_ir_filename) { |
| 348 | if (LLVMPrintModuleToFile(module_ref, llvm_ir_filename, error_message)) { |
| 349 | return true; |
| 314 | 350 | } |
| 351 | } |
| 315 | 352 | |
| 316 | | // MPM goes out of scope and writes to the out streams |
| 353 | if (dest_bin && lto) { |
| 354 | WriteBitcodeToFile(module, *dest_bin); |
| 317 | 355 | } |
| 318 | 356 | |
| 319 | | delete dest_asm; |
| 320 | | delete dest_bin; |
| 357 | if (time_report) { |
| 358 | TimerGroup::printAll(errs()); |
| 359 | } |
| 321 | 360 | |
| 322 | 361 | return false; |
| 323 | 362 | } |