diff --git a/src/target/llvm/codegen_amdgpu.cc b/src/target/llvm/codegen_amdgpu.cc index 89b567b6b077..4e83e83ba7e3 100644 --- a/src/target/llvm/codegen_amdgpu.cc +++ b/src/target/llvm/codegen_amdgpu.cc @@ -202,9 +202,12 @@ class CodeGenAMDGPU : public CodeGenLLVM { } } +#if TVM_LLVM_VERSION < 160 + // This function only works with the legacy pass manager. void InitPassManagerBuilder(llvm::PassManagerBuilder* builder) final { // Additional optimization hook to tweak the builder. } +#endif unsigned GetGlobalAddressSpace() const final { return 1; } diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index 87479ec74237..dce7d0b82f0d 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -58,7 +58,6 @@ #include #include #include -#include #include #include #include @@ -66,6 +65,14 @@ #include #include #include +#if TVM_LLVM_VERSION >= 160 +#include // For VerifierPass +#include +#include +#else +#include +#include +#endif #if TVM_LLVM_VERSION >= 100 #include #include @@ -75,7 +82,6 @@ #include #include #include -#include #include #include #include @@ -351,6 +357,64 @@ llvm::Value* CodeGenLLVM::CreateStorageSync(const CallNode* op) { return nullptr; } +#if TVM_LLVM_VERSION >= 160 + +// Use new pass manager + +void CodeGenLLVM::Optimize() { + llvm::TargetMachine* tm = llvm_target_->GetOrCreateTargetMachine(); + + bool debug_logging = false; + bool verify_each = false; + + llvm::PipelineTuningOptions pto = llvm::PipelineTuningOptions(); + llvm::PassInstrumentationCallbacks pic; + llvm::PassBuilder builder(tm, pto, llvm::None, &pic); + + llvm::LoopAnalysisManager lam; + llvm::FunctionAnalysisManager fam; + llvm::CGSCCAnalysisManager cgam; + llvm::ModuleAnalysisManager mam; + builder.registerLoopAnalyses(lam); + builder.registerFunctionAnalyses(fam); + builder.registerCGSCCAnalyses(cgam); + builder.registerModuleAnalyses(mam); + builder.crossRegisterProxies(lam, fam, cgam, mam); + + // Construct the default pass pipeline depending on the opt level. + std::string pipeline; + switch (llvm_target_->GetOptLevel()) { + case llvm::CodeGenOpt::Level::None: + pipeline = "default"; + break; + case llvm::CodeGenOpt::Level::Less: + pipeline = "default"; + break; + case llvm::CodeGenOpt::Level::Default: + pipeline = "default"; + break; + default: + // CodeGenOpt::Level::Aggressive + pipeline = "default"; + break; + } + + llvm::StandardInstrumentations si(*llvm_target_->GetContext(), debug_logging, verify_each); + si.registerCallbacks(pic, &fam); + llvm::ModulePassManager mpass; + if (verify_each) { + mpass.addPass(llvm::VerifierPass()); + } + if (auto err = builder.parsePassPipeline(mpass, pipeline)) { + LOG(FATAL) << "error parsing pass pipeline '" << pipeline + << "':" << llvm::toString(std::move(err)) << '\n'; + } + + mpass.run(*module_, mam); +} + +#else // TVM_LLVM_VERSION + class FPassManager : public llvm::legacy::FunctionPassManager { public: explicit FPassManager(llvm::Module* m) : llvm::legacy::FunctionPassManager(m) {} @@ -420,6 +484,7 @@ void CodeGenLLVM::Optimize() { fpass.doFinalization(); mpass.run(*module_); } +#endif // TVM_LLVM_VERSION int CodeGenLLVM::NativeVectorBits(const runtime::StorageScope& storage_scope) const { return native_vector_bits_; diff --git a/src/target/llvm/codegen_llvm.h b/src/target/llvm/codegen_llvm.h index 7a8daf2e761f..1ae9d14dc4ad 100644 --- a/src/target/llvm/codegen_llvm.h +++ b/src/target/llvm/codegen_llvm.h @@ -302,8 +302,11 @@ class CodeGenLLVM : public ExprFunctor, virtual llvm::Value* GetThreadIndex(const IterVar& iv); // Get the corresponding thread index virtual llvm::Value* CreateStorageSync(const CallNode* op); +#if TVM_LLVM_VERSION < 160 + // This function only works with the legacy pass manager. // apply optimization on the module. virtual void InitPassManagerBuilder(llvm::PassManagerBuilder* builder); +#endif // Scalarize by iterating elements of e. // f is a callback that takes index and v. void Scalarize(const PrimExpr& e, std::function f); diff --git a/src/target/llvm/codegen_nvptx.cc b/src/target/llvm/codegen_nvptx.cc index 2442d2ccbaa4..ff330e52d33c 100644 --- a/src/target/llvm/codegen_nvptx.cc +++ b/src/target/llvm/codegen_nvptx.cc @@ -183,9 +183,12 @@ class CodeGenNVPTX : public CodeGenLLVM { } } +#if TVM_LLVM_VERSION < 160 + // This function only works with the legacy pass manager. void InitPassManagerBuilder(llvm::PassManagerBuilder* builder) final { // Additional optimization hook to tweak the builder. } +#endif void Optimize() final { for (auto& f : *module_) {