Skip to content

Commit a7e0dbd

Browse files
Fix LLVM 23 compatibility: replace removed NoNaNsFPMath/NoInfsFPMath TargetOptions fields (#9029)
* Initial plan * Fix LLVM 23 compatibility: guard NoNaNsFPMath usage with #if LLVM_VERSION < 230 Co-authored-by: alexreinking <169273+alexreinking@users.noreply.github.com> * Add no-nans-fp-math/no-infs-fp-math function attrs as LLVM 23 replacement for removed TargetOptions fields Co-authored-by: alexreinking <169273+alexreinking@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexreinking <169273+alexreinking@users.noreply.github.com>
1 parent 3679205 commit a7e0dbd

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/CodeGen_Internal.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,15 +607,16 @@ void get_target_options(const llvm::Module &module, llvm::TargetOptions &options
607607
bool use_soft_float_abi = get_modflag_bool(module, "halide_use_soft_float_abi");
608608
std::string mabi = get_modflag_string(module, "halide_mabi");
609609

610-
// FIXME: can this be migrated into `set_function_attributes_from_halide_target_options()`?
611610
bool per_instruction_fast_math_flags = get_modflag_bool(module, "halide_per_instruction_fast_math_flags");
612611

613612
options = llvm::TargetOptions();
614613
options.AllowFPOpFusion = per_instruction_fast_math_flags ? llvm::FPOpFusion::Strict : llvm::FPOpFusion::Fast;
615614
#if LLVM_VERSION < 230
616615
options.NoInfsFPMath = !per_instruction_fast_math_flags;
617616
#endif
617+
#if LLVM_VERSION < 230
618618
options.NoNaNsFPMath = !per_instruction_fast_math_flags;
619+
#endif
619620
options.HonorSignDependentRoundingFPMathOption = !per_instruction_fast_math_flags;
620621
options.NoZerosInBSS = false;
621622
options.GuaranteedTailCallOpt = false;
@@ -720,6 +721,16 @@ void set_function_attributes_from_halide_target_options(llvm::Function &fn) {
720721
fn.addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
721722
module.getContext(), vscale_range, vscale_range));
722723
}
724+
725+
// When not using per-instruction fast-math flags (i.e., the whole module
726+
// is in fast-math mode), propagate the fast-math assumptions as function
727+
// attributes. In LLVM 23+, NoNaNsFPMath and NoInfsFPMath were removed from
728+
// TargetOptions in favor of these per-function attributes.
729+
bool per_instruction_fast_math_flags = get_modflag_bool(module, "halide_per_instruction_fast_math_flags");
730+
if (!per_instruction_fast_math_flags) {
731+
fn.addFnAttr("no-nans-fp-math", "true");
732+
fn.addFnAttr("no-infs-fp-math", "true");
733+
}
723734
}
724735

725736
void embed_bitcode(llvm::Module *M, const string &halide_command) {

src/CodeGen_PTX_Dev.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ void CodeGen_PTX_Dev::init_module() {
230230

231231
module = get_initial_module_for_ptx_device(target, context);
232232

233+
// Propagate the strict-float flag as a module flag so that
234+
// set_function_attributes_from_halide_target_options can read it.
235+
module->addModuleFlag(llvm::Module::Warning, "halide_per_instruction_fast_math_flags",
236+
CodeGen_GPU_Dev::any_strict_float ? 1 : 0);
237+
233238
struct Intrinsic {
234239
const char *name;
235240
Type ret_type;
@@ -631,7 +636,9 @@ vector<char> CodeGen_PTX_Dev::compile_to_src() {
631636
#if LLVM_VERSION < 230
632637
options.NoInfsFPMath = !CodeGen_GPU_Dev::any_strict_float;
633638
#endif
639+
#if LLVM_VERSION < 230
634640
options.NoNaNsFPMath = !CodeGen_GPU_Dev::any_strict_float;
641+
#endif
635642
options.HonorSignDependentRoundingFPMathOption = !CodeGen_GPU_Dev::any_strict_float;
636643
options.NoZerosInBSS = false;
637644
options.GuaranteedTailCallOpt = false;

0 commit comments

Comments
 (0)