diff --git a/llvm/test/CodeGen/X86/unreachableblockelim.ll b/llvm/test/CodeGen/X86/unreachableblockelim.ll --- a/llvm/test/CodeGen/X86/unreachableblockelim.ll +++ b/llvm/test/CodeGen/X86/unreachableblockelim.ll @@ -1,4 +1,5 @@ ; RUN: opt -S < %s -unreachableblockelim | FileCheck %s +; RUN: opt -S < %s -passes='unreachableblockelim' | FileCheck %s target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" diff --git a/llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll b/llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll --- a/llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll +++ b/llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll @@ -1,4 +1,5 @@ ; RUN: opt -pre-isel-intrinsic-lowering -S -o - %s | FileCheck %s +; RUN: opt -passes='pre-isel-intrinsic-lowering' -S < %s | FileCheck %s ; CHECK: define i8* @foo32(i8* [[P:%.*]], i32 [[O:%.*]]) define i8* @foo32(i8* %p, i32 %o) { diff --git a/llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll b/llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll --- a/llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll +++ b/llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll @@ -1,4 +1,5 @@ ; RUN: opt -pre-isel-intrinsic-lowering -S -o - %s | FileCheck %s +; RUN: opt -passes='pre-isel-intrinsic-lowering' -S < %s | FileCheck %s ; Make sure calls to the objc intrinsics are translated to calls in to the ; runtime diff --git a/llvm/tools/opt/NewPMDriver.h b/llvm/tools/opt/NewPMDriver.h --- a/llvm/tools/opt/NewPMDriver.h +++ b/llvm/tools/opt/NewPMDriver.h @@ -21,6 +21,8 @@ #define LLVM_TOOLS_OPT_NEWPMDRIVER_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Triple.h" namespace llvm { class StringRef; @@ -58,15 +60,15 @@ /// /// ThinLTOLinkOut is only used when OK is OK_OutputThinLTOBitcode, and can be /// nullptr. -bool runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, - ToolOutputFile *Out, ToolOutputFile *ThinLinkOut, - ToolOutputFile *OptRemarkFile, StringRef PassPipeline, - ArrayRef PassInfos, opt_tool::OutputKind OK, - opt_tool::VerifierKind VK, - bool ShouldPreserveAssemblyUseListOrder, - bool ShouldPreserveBitcodeUseListOrder, - bool EmitSummaryIndex, bool EmitModuleHash, - bool EnableDebugify, bool Coroutines); +bool runPassPipeline( + StringRef Arg0, Module &M, TargetMachine *TM, ToolOutputFile *Out, + ToolOutputFile *ThinLinkOut, ToolOutputFile *OptRemarkFile, + StringRef PassPipeline, ArrayRef PassInfos, + opt_tool::OutputKind OK, opt_tool::VerifierKind VK, + bool ShouldPreserveAssemblyUseListOrder, + bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex, + bool EmitModuleHash, bool EnableDebugify, bool Coroutines, + llvm::function_ref GetTargetMachine); } // namespace llvm #endif diff --git a/llvm/tools/opt/NewPMDriver.cpp b/llvm/tools/opt/NewPMDriver.cpp --- a/llvm/tools/opt/NewPMDriver.cpp +++ b/llvm/tools/opt/NewPMDriver.cpp @@ -31,6 +31,7 @@ #include "llvm/Passes/StandardInstrumentations.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Host.h" #include "llvm/Support/ToolOutputFile.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" @@ -211,15 +212,14 @@ llvm::PassPluginLibraryInfo get##Ext##PluginInfo(); #include "llvm/Support/Extension.def" -bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM, - ToolOutputFile *Out, ToolOutputFile *ThinLTOLinkOut, - ToolOutputFile *OptRemarkFile, - StringRef PassPipeline, ArrayRef Passes, - OutputKind OK, VerifierKind VK, - bool ShouldPreserveAssemblyUseListOrder, - bool ShouldPreserveBitcodeUseListOrder, - bool EmitSummaryIndex, bool EmitModuleHash, - bool EnableDebugify, bool Coroutines) { +bool llvm::runPassPipeline( + StringRef Arg0, Module &M, TargetMachine *TM, ToolOutputFile *Out, + ToolOutputFile *ThinLTOLinkOut, ToolOutputFile *OptRemarkFile, + StringRef PassPipeline, ArrayRef Passes, OutputKind OK, + VerifierKind VK, bool ShouldPreserveAssemblyUseListOrder, + bool ShouldPreserveBitcodeUseListOrder, bool EmitSummaryIndex, + bool EmitModuleHash, bool EnableDebugify, bool Coroutines, + llvm::function_ref GetTargetMachine) { bool VerifyEachPass = VK == VK_VerifyEachPass; Optional P; @@ -298,6 +298,8 @@ } return false; }); + + // ASAN PB.registerPipelineParsingCallback( [](StringRef Name, ModulePassManager &MPM, ArrayRef) { @@ -318,6 +320,51 @@ return false; }); + // Register codegen IR passes. This includs both target-dependent and + // target-independent passes. + + // Under new pass manager, target-independent IR passes are exposed through + // LLVMTargetMachine. So it must not be null. + // FIXME: This could be done earlier in opt.cpp but it means optimization + // IR passes would see a default TargetMachine instead of a null one. It + // breaks some tests. + if (!TM) { + std::string TripleStr = M.getTargetTriple(); + if (TripleStr.empty()) + TripleStr = sys::getDefaultTargetTriple(); + TM = GetTargetMachine(Triple(TripleStr)); + } + + LLVMTargetMachine *LLVMTM = static_cast(TM); + assert(LLVMTM); + + PB.registerAnalysisRegistrationCallback([LLVMTM](ModuleAnalysisManager &MAM) { + LLVMTM->registerModuleAnalyses(MAM); + }); + PB.registerAnalysisRegistrationCallback( + [LLVMTM](FunctionAnalysisManager &FAM) { + LLVMTM->registerFunctionAnalyses(FAM); + }); + PB.registerAnalysisRegistrationCallback([LLVMTM](LoopAnalysisManager &LAM) { + LLVMTM->registerLoopAnalyses(LAM); + }); + + PB.registerPipelineParsingCallback( + [LLVMTM](StringRef Name, ModulePassManager &MPM, + ArrayRef) { + return LLVMTM->parseModulePass(Name, MPM); + }); + PB.registerPipelineParsingCallback( + [LLVMTM](StringRef Name, FunctionPassManager &FPM, + ArrayRef) { + return LLVMTM->parseFunctionPass(Name, FPM); + }); + PB.registerPipelineParsingCallback( + [LLVMTM](StringRef Name, LoopPassManager &LPM, + ArrayRef) { + return LLVMTM->parseLoopPass(Name, LPM); + }); + #define HANDLE_EXTENSION(Ext) \ get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB); #include "llvm/Support/Extension.def" diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -424,24 +424,6 @@ return CodeGenOpt::None; } -// Returns the TargetMachine instance or zero if no triple is provided. -static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr, - StringRef FeaturesStr, - const TargetOptions &Options) { - std::string Error; - const Target *TheTarget = - TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error); - // Some modules don't specify a triple, and this is okay. - if (!TheTarget) { - return nullptr; - } - - return TheTarget->createTargetMachine( - TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(), - Options, codegen::getExplicitRelocModel(), - codegen::getExplicitCodeModel(), GetCodeGenOptLevel()); -} - #ifdef BUILD_EXAMPLES void initializeExampleIRTransforms(llvm::PassRegistry &Registry); #endif @@ -688,15 +670,27 @@ } } + // Returns the TargetMachine instance or zero if no triple is provided. + auto GetTargetMachine = [](Triple TheTriple) -> TargetMachine * { + std::string Error; + const Target *TheTarget = + TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error); + // Some modules don't specify a triple, and this is okay. + if (!TheTarget) + return nullptr; + + return TheTarget->createTargetMachine( + TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(), + codegen::InitTargetOptionsFromCodeGenFlags(), + codegen::getExplicitRelocModel(), codegen::getExplicitCodeModel(), + GetCodeGenOptLevel()); + }; + Triple ModuleTriple(M->getTargetTriple()); - std::string CPUStr, FeaturesStr; TargetMachine *Machine = nullptr; - const TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags(); if (ModuleTriple.getArch()) { - CPUStr = codegen::getCPUStr(); - FeaturesStr = codegen::getFeaturesStr(); - Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options); + Machine = GetTargetMachine(ModuleTriple); } else if (ModuleTriple.getArchName() != "unknown" && ModuleTriple.getArchName() != "") { errs() << argv[0] << ": unrecognized architecture '" @@ -704,6 +698,8 @@ return 1; } + std::string CPUStr = codegen::getCPUStr(); + std::string FeaturesStr = codegen::getFeaturesStr(); std::unique_ptr TM(Machine); // Override function attributes based on CPUStr, FeaturesStr, and command line @@ -766,7 +762,8 @@ RemarksFile.get(), PassPipeline, Passes, OK, VK, PreserveAssemblyUseListOrder, PreserveBitcodeUseListOrder, EmitSummaryIndex, - EmitModuleHash, EnableDebugify, Coroutines) + EmitModuleHash, EnableDebugify, Coroutines, + GetTargetMachine) ? 0 : 1; }