Skip to content

Commit 24a4e6c

Browse files
authored
[LLVM] Fix build failures when building with llvm>=22 (#18818)
This PR fixes Windows/macOS GitHub Actions build failures when building TVM with LLVM 22 (`TVM_LLVM_VERSION=221`), caused by LLVM API changes in target/command-line option handling. Link to the recent build failure log: https://github.com/apache/tvm/actions/runs/22387355836/job/64820136927?pr=18817
1 parent 8cb946e commit 24a4e6c

1 file changed

Lines changed: 51 additions & 5 deletions

File tree

src/target/llvm/llvm_instance.cc

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,19 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
233233
}
234234

235235
if (const auto& v = Downcast<ffi::Optional<ffi::Array<ffi::String>>>(target.Get("cl-opt"))) {
236-
llvm::StringMap<llvm::cl::Option*>& options = llvm::cl::getRegisteredOptions();
236+
auto& options = llvm::cl::getRegisteredOptions();
237237
bool parse_error = false;
238238
for (const ffi::String& s : v.value()) {
239239
Option opt = ParseOptionString(s);
240240
if (opt.type == Option::OptType::Invalid) {
241241
parse_error = true;
242242
continue;
243243
}
244+
#if TVM_LLVM_VERSION >= 220
245+
if (options.find(opt.name) != options.end()) {
246+
#else
244247
if (options.count(opt.name)) {
248+
#endif
245249
llvm_options_.push_back(opt);
246250
} else {
247251
// Flag an error, but don't abort. LLVM flags may change, and this would
@@ -318,7 +322,9 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
318322
// In clang, these are fed from LangOpts which describe language specific features
319323
// TODO(AndrewZhaoLuo): figure out how these relate to fast math flags
320324
target_options_.AllowFPOpFusion = llvm::FPOpFusion::Fast;
325+
#if TVM_LLVM_VERSION < 220
321326
target_options_.UnsafeFPMath = false;
327+
#endif
322328
target_options_.NoInfsFPMath = false;
323329
target_options_.NoNaNsFPMath = true;
324330
target_options_.FloatABIType = float_abi;
@@ -414,9 +420,16 @@ LLVMTargetInfo::~LLVMTargetInfo() = default;
414420
static const llvm::Target* CreateLLVMTargetInstance(const std::string triple,
415421
const bool allow_missing = true) {
416422
std::string error;
423+
#if TVM_LLVM_VERSION >= 220
424+
llvm::Triple triple_obj(triple);
425+
#endif
417426
// create LLVM instance
418427
// required mimimum: llvm::InitializeAllTargets()
428+
#if TVM_LLVM_VERSION >= 220
429+
const llvm::Target* llvm_instance = llvm::TargetRegistry::lookupTarget(triple_obj, error);
430+
#else
419431
const llvm::Target* llvm_instance = llvm::TargetRegistry::lookupTarget(triple, error);
432+
#endif
420433
if (!allow_missing && !llvm_instance) {
421434
TVM_FFI_ICHECK(llvm_instance) << "LLVM instance error: `" << error << "`";
422435
}
@@ -434,8 +447,14 @@ static std::unique_ptr<llvm::TargetMachine> CreateLLVMTargetMachine(
434447
#else
435448
const llvm::CodeGenOptLevel& opt_level = llvm::CodeGenOptLevel(0)) {
436449
#endif
450+
#if TVM_LLVM_VERSION >= 220
451+
llvm::Triple triple_obj(triple);
452+
llvm::TargetMachine* tm = llvm_instance->createTargetMachine(
453+
triple_obj, cpu, features, target_options, reloc_model, code_model, opt_level);
454+
#else
437455
llvm::TargetMachine* tm = llvm_instance->createTargetMachine(
438456
triple, cpu, features, target_options, reloc_model, code_model, opt_level);
457+
#endif
439458
TVM_FFI_ICHECK(tm != nullptr);
440459

441460
return std::unique_ptr<llvm::TargetMachine>(tm);
@@ -822,8 +841,22 @@ bool LLVMTargetInfo::MatchesGlobalState() const {
822841
}
823842

824843
void LLVMTargetInfo::GetOptionValue(LLVMTargetInfo::Option* opt) const {
825-
llvm::StringMap<llvm::cl::Option*>& options = llvm::cl::getRegisteredOptions();
826-
llvm::cl::Option* base_op = options[opt->name];
844+
auto& options = llvm::cl::getRegisteredOptions();
845+
llvm::cl::Option* base_op = nullptr;
846+
#if TVM_LLVM_VERSION >= 220
847+
auto it = options.find(opt->name);
848+
if (it != options.end()) {
849+
base_op = it->second;
850+
}
851+
#else
852+
if (options.count(opt->name)) {
853+
base_op = options[opt->name];
854+
}
855+
#endif
856+
if (base_op == nullptr) {
857+
opt->type = Option::OptType::Invalid;
858+
return;
859+
}
827860

828861
if (opt->type == Option::OptType::Bool) {
829862
auto* bool_op = static_cast<llvm::cl::opt<bool>*>(base_op);
@@ -1004,7 +1037,7 @@ void LLVMTarget::SetTargetMetadata(llvm::Module* module) const {
10041037
}
10051038

10061039
bool LLVMTarget::ApplyLLVMOptions(bool apply_otherwise_revert, bool dry_run) {
1007-
llvm::StringMap<llvm::cl::Option*>& options = llvm::cl::getRegisteredOptions();
1040+
auto& options = llvm::cl::getRegisteredOptions();
10081041
bool changed = false;
10091042

10101043
#define HANDLE_OPTION_VALUE(option, new_val, saved_val) \
@@ -1024,7 +1057,20 @@ bool LLVMTarget::ApplyLLVMOptions(bool apply_otherwise_revert, bool dry_run) {
10241057
const Option& new_opt = new_options[i];
10251058
const Option& saved_opt = saved_llvm_options_[i];
10261059

1027-
llvm::cl::Option* base_op = options[new_opt.name];
1060+
llvm::cl::Option* base_op = nullptr;
1061+
#if TVM_LLVM_VERSION >= 220
1062+
auto it = options.find(new_opt.name);
1063+
if (it != options.end()) {
1064+
base_op = it->second;
1065+
}
1066+
#else
1067+
if (options.count(new_opt.name)) {
1068+
base_op = options[new_opt.name];
1069+
}
1070+
#endif
1071+
if (base_op == nullptr) {
1072+
TVM_FFI_THROW(InternalError) << "LLVM option not found: " << new_opt.name;
1073+
}
10281074

10291075
if (new_opt.type == Option::OptType::Bool) {
10301076
auto* bool_op = static_cast<llvm::cl::opt<bool>*>(base_op);

0 commit comments

Comments
 (0)