From 71caa19f96adcf2796ce4ad00ac7bd2669f3e5ad Mon Sep 17 00:00:00 2001 From: Anirudh Sundar Subramaniam Date: Fri, 13 Oct 2023 08:45:05 +0530 Subject: [PATCH] [LLVM] Fix for llvm CodeGenOpt API change (#15921) This patch fixes the errors caused due to recent API change from LLVM for CodeGenOpt made in [this PR](https://github.com/llvm/llvm-project/pull/66295) --- src/target/llvm/codegen_amdgpu.cc | 11 +++++-- src/target/llvm/codegen_hexagon.cc | 5 ++- src/target/llvm/codegen_llvm.cc | 18 +++++++++++ src/target/llvm/codegen_nvptx.cc | 5 ++- src/target/llvm/llvm_instance.cc | 51 +++++++++++++++++++++++++++++- src/target/llvm/llvm_instance.h | 8 +++++ src/target/llvm/llvm_module.cc | 14 ++++++-- 7 files changed, 105 insertions(+), 7 deletions(-) diff --git a/src/target/llvm/codegen_amdgpu.cc b/src/target/llvm/codegen_amdgpu.cc index 0ab4771f6c69..80c2abb5f135 100644 --- a/src/target/llvm/codegen_amdgpu.cc +++ b/src/target/llvm/codegen_amdgpu.cc @@ -302,9 +302,12 @@ runtime::Module BuildAMDGPU(IRModule mod, Target target) { #elif TVM_LLVM_VERSION <= 90 ICHECK(tm->addPassesToEmitFile(pass, destObj, nullptr, llvm::TargetMachine::CGFT_ObjectFile) == 0) << "Cannot emit target CGFT_ObjectFile"; -#else +#elif TVM_LLVM_VERSION <= 170 ICHECK(tm->addPassesToEmitFile(pass, destObj, nullptr, llvm::CGFT_ObjectFile) == 0) << "Cannot emit target CGFT_ObjectFile"; +#else + ICHECK(tm->addPassesToEmitFile(pass, destObj, nullptr, llvm::CodeGenFileType::ObjectFile) == 0) + << "Cannot emit target CodeGenFileType::ObjectFile"; #endif pass.run(*mObj); std::string obj(dataObj.begin(), dataObj.end()); @@ -317,9 +320,13 @@ runtime::Module BuildAMDGPU(IRModule mod, Target target) { ICHECK(tm->addPassesToEmitFile(passAsm, destAsm, nullptr, llvm::TargetMachine::CGFT_AssemblyFile) == 0) << "Cannot emit target CGFT_AssemblyFile"; -#else +#elif TVM_LLVM_VERSION <= 170 ICHECK(tm->addPassesToEmitFile(passAsm, destAsm, nullptr, llvm::CGFT_AssemblyFile) == 0) << "Cannot emit target CGFT_AssemblyFile"; +#else + ICHECK(tm->addPassesToEmitFile(passAsm, destAsm, nullptr, llvm::CodeGenFileType::AssemblyFile) == + 0) + << "Cannot emit target CGFT_AssemblyFile"; #endif passAsm.run(*mAsm); std::string assembly(dataAsm.begin(), dataAsm.end()); diff --git a/src/target/llvm/codegen_hexagon.cc b/src/target/llvm/codegen_hexagon.cc index 2d0945c70498..8b884669c38c 100644 --- a/src/target/llvm/codegen_hexagon.cc +++ b/src/target/llvm/codegen_hexagon.cc @@ -588,8 +588,11 @@ runtime::Module BuildHexagon(IRModule mod, Target target) { #if TVM_LLVM_VERSION <= 90 auto ft = cgft == Asm ? llvm::TargetMachine::CodeGenFileType::CGFT_AssemblyFile : llvm::TargetMachine::CodeGenFileType::CGFT_ObjectFile; -#else +#elif TVM_LLVM_VERSION <= 170 auto ft = cgft == Asm ? llvm::CGFT_AssemblyFile : llvm::CGFT_ObjectFile; +#else + auto ft = + cgft == Asm ? llvm::CodeGenFileType::AssemblyFile : llvm::CodeGenFileType::ObjectFile; #endif llvm::SmallString<16384> ss; // Will grow on demand. diff --git a/src/target/llvm/codegen_llvm.cc b/src/target/llvm/codegen_llvm.cc index 02d203b7e97a..3d4d3def2411 100644 --- a/src/target/llvm/codegen_llvm.cc +++ b/src/target/llvm/codegen_llvm.cc @@ -429,6 +429,7 @@ void CodeGenLLVM::Optimize() { // Construct the default pass pipeline depending on the opt level. std::string pipeline; +#if TVM_LLVM_VERSION <= 170 switch (llvm_target_->GetOptLevel()) { case llvm::CodeGenOpt::Level::None: pipeline = "default"; @@ -444,6 +445,23 @@ void CodeGenLLVM::Optimize() { pipeline = "default"; break; } +#else + switch (llvm_target_->GetOptLevel()) { + case llvm::CodeGenOptLevel::None: + pipeline = "default"; + break; + case llvm::CodeGenOptLevel::Less: + pipeline = "default"; + break; + case llvm::CodeGenOptLevel::Default: + pipeline = "default"; + break; + default: + // CodeGenOptLevel::Aggressive + pipeline = "default"; + break; + } +#endif llvm::StandardInstrumentations si(*llvm_target_->GetContext(), debug_logging, verify_each); #if LLVM_VERSION_MAJOR >= 17 diff --git a/src/target/llvm/codegen_nvptx.cc b/src/target/llvm/codegen_nvptx.cc index b6c19c92d70f..0b13fce6783c 100644 --- a/src/target/llvm/codegen_nvptx.cc +++ b/src/target/llvm/codegen_nvptx.cc @@ -345,9 +345,12 @@ runtime::Module BuildNVPTX(IRModule mod, Target target) { ICHECK(tm->addPassesToEmitFile(pass, dest_ptx, nullptr, llvm::TargetMachine::CGFT_AssemblyFile) == 0) << "Cannot emit target CGFT_ObjectFile"; -#else +#elif TVM_LLVM_VERSION <= 170 ICHECK(tm->addPassesToEmitFile(pass, dest_ptx, nullptr, llvm::CGFT_AssemblyFile) == 0) << "Cannot emit target CGFT_ObjectFile"; +#else + ICHECK(tm->addPassesToEmitFile(pass, dest_ptx, nullptr, llvm::CodeGenFileType::AssemblyFile) == 0) + << "Cannot emit target CodeGenFileType::ObjectFile"; #endif pass.run(*module); std::string ptx(data_ptx.begin(), data_ptx.end()); diff --git a/src/target/llvm/llvm_instance.cc b/src/target/llvm/llvm_instance.cc index e270a9b66cb6..5079822277e5 100644 --- a/src/target/llvm/llvm_instance.cc +++ b/src/target/llvm/llvm_instance.cc @@ -94,7 +94,11 @@ namespace codegen { namespace { namespace defaults { static const char* cpu = "generic"; +#if TVM_LLVM_VERSION <= 170 static const llvm::CodeGenOpt::Level opt_level = llvm::CodeGenOpt::Aggressive; +#else +static const llvm::CodeGenOptLevel opt_level = llvm::CodeGenOptLevel::Aggressive; +#endif } // namespace defaults } // namespace @@ -258,7 +262,7 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance, const Target& target) { } auto maybe_level = target->GetAttr("opt-level"); - +#if TVM_LLVM_VERSION <= 170 if (maybe_level.defined()) { int level = maybe_level.value()->value; if (level <= 0) { @@ -274,6 +278,23 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance, const Target& target) { } else { opt_level_ = defaults::opt_level; } +#else + if (maybe_level.defined()) { + int level = maybe_level.value()->value; + if (level <= 0) { + opt_level_ = llvm::CodeGenOptLevel::None; + } else if (level == 1) { + opt_level_ = llvm::CodeGenOptLevel::Less; + } else if (level == 2) { + opt_level_ = llvm::CodeGenOptLevel::Default; + } else { + // level >= 3 + opt_level_ = llvm::CodeGenOptLevel::Aggressive; + } + } else { + opt_level_ = defaults::opt_level; + } +#endif target_options_.UseInitArray = true; @@ -338,7 +359,11 @@ static llvm::TargetMachine* CreateLLVMTargetMachine( const llvm::Target* llvm_instance, const std::string& triple, const std::string& cpu, const std::string& features, const llvm::TargetOptions& target_options, const llvm::Reloc::Model& reloc_model, const llvm::CodeModel::Model& code_model, +#if TVM_LLVM_VERSION <= 170 const llvm::CodeGenOpt::Level& opt_level) { +#else + const llvm::CodeGenOptLevel& opt_level) { +#endif llvm::TargetMachine* tm = llvm_instance->createTargetMachine( triple, cpu, features, target_options, reloc_model, code_model, opt_level); ICHECK(tm != nullptr); @@ -356,7 +381,11 @@ static const llvm::MCSubtargetInfo* GetLLVMSubtargetInfo(const std::string& trip llvm::TargetOptions target_options; auto tm = CreateLLVMTargetMachine(llvm_instance, triple, cpu_name, feats, target_options, llvm::Reloc::Static, llvm::CodeModel::Small, +#if TVM_LLVM_VERSION <= 170 llvm::CodeGenOpt::Level(0)); +#else + llvm::CodeGenOptLevel(0)); +#endif // create subtarget info module const llvm::MCSubtargetInfo* MCInfo = tm->getMCSubtargetInfo(); @@ -435,6 +464,7 @@ std::string LLVMTargetInfo::str() const { #endif } +#if TVM_LLVM_VERSION <= 170 if (opt_level_ != defaults::opt_level) { os << " -opt-level="; switch (opt_level_) { @@ -452,6 +482,25 @@ std::string LLVMTargetInfo::str() const { break; } } +#else + if (opt_level_ != defaults::opt_level) { + os << " -opt-level="; + switch (opt_level_) { + case llvm::CodeGenOptLevel::None: + os << "0"; + break; + case llvm::CodeGenOptLevel::Less: + os << "1"; + break; + case llvm::CodeGenOptLevel::Default: + os << "2"; + break; + case llvm::CodeGenOptLevel::Aggressive: + os << "3"; + break; + } + } +#endif if (size_t num = llvm_options_.size(); num > 0) { os << " -cl-opt="; diff --git a/src/target/llvm/llvm_instance.h b/src/target/llvm/llvm_instance.h index ac08008b8021..030a7db7210f 100644 --- a/src/target/llvm/llvm_instance.h +++ b/src/target/llvm/llvm_instance.h @@ -216,7 +216,11 @@ class LLVMTargetInfo { * \brief Get the LLVM optimization level * \return optimization level for this target */ +#if TVM_LLVM_VERSION <= 170 llvm::CodeGenOpt::Level GetOptLevel() const { return opt_level_; } +#else + llvm::CodeGenOptLevel GetOptLevel() const { return opt_level_; } +#endif /*! * \class Option @@ -312,7 +316,11 @@ class LLVMTargetInfo { std::vector