Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLVM] Fix for llvm CodeGenOpt API change #15921

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/target/llvm/codegen_amdgpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
5 changes: 4 additions & 1 deletion src/target/llvm/codegen_hexagon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/target/llvm/codegen_llvm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<O0>";
Expand All @@ -444,6 +445,23 @@ void CodeGenLLVM::Optimize() {
pipeline = "default<O3>";
break;
}
#else
switch (llvm_target_->GetOptLevel()) {
case llvm::CodeGenOptLevel::None:
pipeline = "default<O0>";
break;
case llvm::CodeGenOptLevel::Less:
pipeline = "default<O1>";
break;
case llvm::CodeGenOptLevel::Default:
pipeline = "default<O2>";
break;
default:
// CodeGenOptLevel::Aggressive
pipeline = "default<O3>";
break;
}
#endif

llvm::StandardInstrumentations si(*llvm_target_->GetContext(), debug_logging, verify_each);
#if LLVM_VERSION_MAJOR >= 17
Expand Down
5 changes: 4 additions & 1 deletion src/target/llvm/codegen_nvptx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
51 changes: 50 additions & 1 deletion src/target/llvm/llvm_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -258,7 +262,7 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance, const Target& target) {
}

auto maybe_level = target->GetAttr<Integer>("opt-level");

#if TVM_LLVM_VERSION <= 170
if (maybe_level.defined()) {
int level = maybe_level.value()->value;
if (level <= 0) {
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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_) {
Expand All @@ -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=";
Expand Down
8 changes: 8 additions & 0 deletions src/target/llvm/llvm_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -312,7 +316,11 @@ class LLVMTargetInfo {
std::vector<Option> llvm_options_;
llvm::TargetOptions target_options_;
llvm::FastMathFlags fast_math_flags_;
#if TVM_LLVM_VERSION <= 170
llvm::CodeGenOpt::Level opt_level_;
#else
llvm::CodeGenOptLevel opt_level_;
#endif
llvm::Reloc::Model reloc_model_ = llvm::Reloc::PIC_;
llvm::CodeModel::Model code_model_ = llvm::CodeModel::Small;
std::shared_ptr<llvm::TargetMachine> target_machine_;
Expand Down
14 changes: 12 additions & 2 deletions src/target/llvm/llvm_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ std::unique_ptr<llvm::Module> CloneLLVMModule(llvm::Module* mod) { return llvm::
#if TVM_LLVM_VERSION <= 90
constexpr auto llvm_object_file_target = llvm::TargetMachine::CGFT_ObjectFile;
constexpr auto llvm_assembly_file_target = llvm::TargetMachine::CGFT_AssemblyFile;
#else
#elif TVM_LLVM_VERSION <= 170
constexpr auto llvm_object_file_target = llvm::CGFT_ObjectFile;
constexpr auto llvm_assembly_file_target = llvm::CGFT_AssemblyFile;
#else
constexpr auto llvm_object_file_target = llvm::CodeGenFileType::ObjectFile;
constexpr auto llvm_assembly_file_target = llvm::CodeGenFileType::AssemblyFile;
#endif

bool LLVMAddPassesToEmitFile(llvm::TargetMachine* tm, llvm::legacy::PassManager* pm,
Expand Down Expand Up @@ -274,9 +277,12 @@ String LLVMModuleNode::GetSource(const String& format) {
#elif TVM_LLVM_VERSION <= 90
ICHECK(tm->addPassesToEmitFile(pass, rso, nullptr, llvm::TargetMachine::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#else
#elif TVM_LLVM_VERSION <= 170
ICHECK(tm->addPassesToEmitFile(pass, rso, nullptr, llvm::CGFT_AssemblyFile) == 0)
<< "Cannot emit target CGFT_AssemblyFile";
#else
ICHECK(tm->addPassesToEmitFile(pass, rso, nullptr, llvm::CodeGenFileType::AssemblyFile) == 0)
<< "Cannot emit target CodeGenFileType::AssemblyFile";
#endif
pass.run(*m);
return rso.str().str();
Expand Down Expand Up @@ -383,7 +389,11 @@ void LLVMModuleNode::LazyInitJIT() {
With<LLVMTarget> llvm_target(*llvm_instance_, LLVMTarget::GetTargetMetadata(*module_));
llvm::EngineBuilder builder(std::move(module_owning_ptr_));
builder.setEngineKind(llvm::EngineKind::JIT);
#if TVM_LLVM_VERSION <= 170
builder.setOptLevel(llvm::CodeGenOpt::Aggressive);
#else
builder.setOptLevel(llvm::CodeGenOptLevel::Aggressive);
#endif
builder.setMCPU(llvm_target->GetCPU());
builder.setMAttrs(llvm_target->GetTargetFeatures());
builder.setTargetOptions(llvm_target->GetTargetOptions());
Expand Down
Loading