Rollup merge of #153834 - N1ark:generic-float-intrinsics, r=tgross35,RalfJung

Merge `fabsf16/32/64/128` into `fabs::<F>`

Following [a small conversation on Zulip](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Float.20intrinsics/with/521501401) (and because I'd be interested in starting to contribute on Rust), I thought I'd give a try at merging the float intrinsics :)

This PR just merges `fabsf16`, `fabsf32`, `fabsf64`, `fabsf128`, as it felt like an easy first target.

Notes:
- I'm opening the PR for one intrinsic as it's probably easier if the shift is done one intrinsic at a time, but let me know if you'd rather I do several at a time to reduce the number of PRs.
- Currently this PR increases LOCs, despite being an attempt at simplifying the intrinsics/compilers. I believe this increase is a one time thing as I had to define new functions and move some things around, and hopefully future PRs/commits will reduce overall LoCs
- `fabsf32` and `fabsf64` are `#[rustc_intrinsic_const_stable_indirect]`, while `fabsf16` and `fabsf128` aren't; because `f32`/`f64` expect the function to be const, the generic version must be made indirectly stable too. We'd need to check with T-lang this change is ok; the only other intrinsics where there is such a mismatch is `minnum`, `maxnum` and `copysign`.
- I haven't touched libm because I'm not familiar with how it works; any guidance would be welcome!
This commit is contained in:
Guillaume Gomez
2026-03-29 00:06:50 +01:00
committed by GitHub
20 changed files with 222 additions and 163 deletions
+15 -6
View File
@@ -107,11 +107,6 @@ fn call_simple_intrinsic<'ll, 'tcx>(
sym::fmuladdf64 => ("llvm.fmuladd", &[bx.type_f64()]),
sym::fmuladdf128 => ("llvm.fmuladd", &[bx.type_f128()]),
sym::fabsf16 => ("llvm.fabs", &[bx.type_f16()]),
sym::fabsf32 => ("llvm.fabs", &[bx.type_f32()]),
sym::fabsf64 => ("llvm.fabs", &[bx.type_f64()]),
sym::fabsf128 => ("llvm.fabs", &[bx.type_f128()]),
// FIXME: LLVM currently mis-compile those intrinsics, re-enable them
// when llvm/llvm-project#{139380,139381,140445} are fixed.
//sym::minimumf16 => ("llvm.minimum", &[bx.type_f16()]),
@@ -502,6 +497,20 @@ fn codegen_intrinsic_call(
}
}
sym::fabs => {
let ty = args[0].layout.ty;
let ty::Float(f) = ty.kind() else {
span_bug!(span, "the `fabs` intrinsic requires a floating-point argument, got {:?}", ty);
};
let llty = self.type_float_from_ty(*f);
let llvm_name = "llvm.fabs";
self.call_intrinsic(
llvm_name,
&[llty],
&args.iter().map(|arg| arg.immediate()).collect::<Vec<_>>(),
)
}
sym::raw_eq => {
use BackendRepr::*;
let tp_ty = fn_args.type_at(0);
@@ -1936,7 +1945,7 @@ macro_rules! return_error {
}
let ty::Float(f) = in_elem.kind() else {
return_error!(InvalidMonomorphization::FloatingPointType { span, name, in_ty });
return_error!(InvalidMonomorphization::BasicFloatType { span, name, ty: in_ty });
};
let elem_ty = bx.cx.type_float_from_ty(*f);