Skip to content

Commit dd68b78

Browse files
Rollup merge of #162395 - N1ark:more-float-generics, r=folkertdev
Make more float intrinsics generic Another (big) step of #153834 + #160989 Makemore remaining float intrinsics generic! Each commit is self-contained. In order: - `ceil`, `floor`, `trunc`, `round`, `round_ties_even`, with const libm fallbacks (using `#[rustc_do_not_const_check]`, see #150961, cc @RalfJung @tgross35) - `sqrt`, with libm fallbacks - `powf`, with libm fallbacks - `fma` and `fmuladd`, with libm fallbacks (+`#[rustc_do_not_const_check]`) and a shared fallback, respectively. - `powi`, with no fallback (it already didn't have any) The first commit just removes the f16/f128 fallbacks for `fabs`, as mentioned [here](#160989 (comment)). I didn't try making the backends use more fallbacks, to make the PR less risky since it's quite large. Happy to do changes there though (cc @folkertdev) `copysign`, `minimum_number_nsz`, `maximum_number_nsz`, `minimum`, `maximum` will be handled separately as they require `rustc_allow_const_fn_unstable`: #162414
2 parents 0cefa27 + 49ec655 commit dd68b78

41 files changed

Lines changed: 799 additions & 1135 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎compiler/rustc_codegen_cranelift/src/codegen_f16_f128.rs‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,6 @@ pub(crate) fn neg_f128(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
160160
fx.bcx.ins().bitcast(types::F128, MemFlagsData::new(), bits)
161161
}
162162

163-
pub(crate) fn abs_f16(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
164-
let bits = fx.bcx.ins().bitcast(types::I16, MemFlagsData::new(), value);
165-
let bits = fx.bcx.ins().band_imm_u(bits, 0x7fff);
166-
fx.bcx.ins().bitcast(types::F16, MemFlagsData::new(), bits)
167-
}
168-
169-
pub(crate) fn abs_f128(fx: &mut FunctionCx<'_, '_, '_>, value: Value) -> Value {
170-
let bits = fx.bcx.ins().bitcast(types::I128, MemFlagsData::new(), value);
171-
let (low, high) = fx.bcx.ins().isplit(bits);
172-
let high = fx.bcx.ins().band_imm_u(high, 0x7fff_ffff_ffff_ffff_u64 as i64);
173-
let bits = fx.bcx.ins().iconcat(low, high);
174-
fx.bcx.ins().bitcast(types::F128, MemFlagsData::new(), bits)
175-
}
176-
177163
pub(crate) fn codegen_cast(
178164
fx: &mut FunctionCx<'_, '_, '_>,
179165
from: Value,

‎compiler/rustc_codegen_cranelift/src/compiler_builtins.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ builtin_functions! {
172172
#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
173173
fn rintf128(f: f128) -> f128;
174174
#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
175+
fn roundf16(f: f16) -> f16;
176+
#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
177+
fn roundf128(f: f128) -> f128;
178+
#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
175179
fn sqrtf16(f: f16) -> f16;
176180
#[cfg(not(all(target_os = "windows", target_env = "gnu")))]
177181
fn sqrtf128(f: f128) -> f128;

‎compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs‎

Lines changed: 120 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -322,63 +322,11 @@ fn codegen_float_intrinsic_call<'tcx>(
322322
ret: CPlace<'tcx>,
323323
) -> bool {
324324
let (name, arg_count, ty, clif_ty) = match intrinsic {
325-
sym::sqrtf16 => return false, // has a fallback via f32
326-
sym::sqrtf32 => ("sqrtf", 1, fx.tcx.types.f32, types::F32),
327-
sym::sqrtf64 => ("sqrt", 1, fx.tcx.types.f64, types::F64),
328-
sym::sqrtf128 => ("sqrtf128", 1, fx.tcx.types.f128, types::F128),
329-
330-
sym::powif16 => ("__powisf2", 2, fx.tcx.types.f16, types::F16), // compiler-builtins
331-
sym::powif32 => ("__powisf2", 2, fx.tcx.types.f32, types::F32), // compiler-builtins
332-
sym::powif64 => ("__powidf2", 2, fx.tcx.types.f64, types::F64), // compiler-builtins
333-
sym::powif128 => ("__powitf2", 2, fx.tcx.types.f128, types::F128), // compiler-builtins
334-
335-
sym::powf16 => return false, // has a fallback via f32
336-
sym::powf32 => ("powf", 2, fx.tcx.types.f32, types::F32),
337-
sym::powf64 => ("pow", 2, fx.tcx.types.f64, types::F64),
338-
sym::powf128 => ("powf128", 2, fx.tcx.types.f128, types::F128),
339-
340-
sym::fmaf16 => return false, // has a fallback via f64
341-
sym::fmaf32 => ("fmaf", 3, fx.tcx.types.f32, types::F32),
342-
sym::fmaf64 => ("fma", 3, fx.tcx.types.f64, types::F64),
343-
sym::fmaf128 => ("fmaf128", 3, fx.tcx.types.f128, types::F128),
344-
345-
// FIXME: calling `fma` from libc without FMA target feature uses expensive sofware
346-
// emulation, use cranelift intrinsic analogous to llvm.fmuladd.*.
347-
sym::fmuladdf16 => return false, // has a fallback
348-
sym::fmuladdf32 => ("fmaf", 3, fx.tcx.types.f32, types::F32),
349-
sym::fmuladdf64 => ("fma", 3, fx.tcx.types.f64, types::F64),
350-
sym::fmuladdf128 => return false, // has a fallback
351-
352325
sym::copysignf16 => return false, // has a fallback
353326
sym::copysignf32 => ("copysignf", 2, fx.tcx.types.f32, types::F32),
354327
sym::copysignf64 => ("copysign", 2, fx.tcx.types.f64, types::F64),
355328
sym::copysignf128 => return false, // has a fallback
356329

357-
sym::floorf16 => return false, // has a fallback via f32
358-
sym::floorf32 => ("floorf", 1, fx.tcx.types.f32, types::F32),
359-
sym::floorf64 => ("floor", 1, fx.tcx.types.f64, types::F64),
360-
sym::floorf128 => ("floorf128", 1, fx.tcx.types.f128, types::F128),
361-
362-
sym::ceilf16 => return false, // has a fallback via f32
363-
sym::ceilf32 => ("ceilf", 1, fx.tcx.types.f32, types::F32),
364-
sym::ceilf64 => ("ceil", 1, fx.tcx.types.f64, types::F64),
365-
sym::ceilf128 => ("ceilf128", 1, fx.tcx.types.f128, types::F128),
366-
367-
sym::truncf16 => return false, // has a fallback via f32
368-
sym::truncf32 => ("truncf", 1, fx.tcx.types.f32, types::F32),
369-
sym::truncf64 => ("trunc", 1, fx.tcx.types.f64, types::F64),
370-
sym::truncf128 => ("truncf128", 1, fx.tcx.types.f128, types::F128),
371-
372-
sym::round_ties_even_f16 => return false, // has a fallback via f32
373-
sym::round_ties_even_f32 => ("rintf", 1, fx.tcx.types.f32, types::F32),
374-
sym::round_ties_even_f64 => ("rint", 1, fx.tcx.types.f64, types::F64),
375-
sym::round_ties_even_f128 => ("rintf128", 1, fx.tcx.types.f128, types::F128),
376-
377-
sym::roundf16 => return false, // has a fallback via f32
378-
sym::roundf32 => ("roundf", 1, fx.tcx.types.f32, types::F32),
379-
sym::roundf64 => ("round", 1, fx.tcx.types.f64, types::F64),
380-
sym::roundf128 => ("roundf128", 1, fx.tcx.types.f128, types::F128),
381-
382330
_ => return false,
383331
};
384332

@@ -414,35 +362,8 @@ fn codegen_float_intrinsic_call<'tcx>(
414362
// FIXME(bytecodealliance/wasmtime#8312): Use native Cranelift operations
415363
// for `f16` and `f128` once the lowerings have been implemented in Cranelift.
416364
let val = match intrinsic {
417-
sym::fmaf32 | sym::fmaf64 | sym::fmuladdf32 | sym::fmuladdf64 => {
418-
fx.bcx.ins().fma(args[0], args[1], args[2])
419-
}
420365
sym::copysignf32 | sym::copysignf64 => fx.bcx.ins().fcopysign(args[0], args[1]),
421-
sym::floorf32 | sym::floorf64 => fx.bcx.ins().floor(args[0]),
422-
sym::ceilf32 | sym::ceilf64 => fx.bcx.ins().ceil(args[0]),
423-
sym::truncf32 | sym::truncf64 => fx.bcx.ins().trunc(args[0]),
424-
sym::round_ties_even_f32 | sym::round_ties_even_f64 => fx.bcx.ins().nearest(args[0]),
425-
sym::sqrtf32 | sym::sqrtf64 => fx.bcx.ins().sqrt(args[0]),
426-
427-
// These intrinsics aren't supported natively by Cranelift.
428-
// Lower them to a libcall.
429-
sym::powif16 | sym::powif32 | sym::powif64 | sym::powif128 => {
430-
let temp;
431-
let (clif_ty, args) = if intrinsic == sym::powif16 {
432-
temp = [codegen_f16_f128::f16_to_f32(fx, args[0]), args[1]];
433-
(types::F32, temp.as_slice())
434-
} else {
435-
(clif_ty, args)
436-
};
437-
let input_tys: Vec<_> =
438-
vec![AbiParam::new(clif_ty), lib_call_arg_param(fx.tcx, types::I32, true)];
439-
let ret_val = fx.lib_call(name, input_tys, vec![AbiParam::new(clif_ty)], args)[0];
440-
if intrinsic == sym::powif16 {
441-
codegen_f16_f128::f32_to_f16(fx, ret_val)
442-
} else {
443-
ret_val
444-
}
445-
}
366+
446367
_ => {
447368
let input_tys: Vec<_> = args.iter().map(|_| AbiParam::new(clif_ty)).collect();
448369
fx.lib_call(name, input_tys, vec![AbiParam::new(clif_ty)], args)[0]
@@ -455,6 +376,13 @@ fn codegen_float_intrinsic_call<'tcx>(
455376
true
456377
}
457378

379+
/// Used to distinguish fallbacks of float intrinsics. For some we have a codegen fallback,
380+
/// while for others we fallback to an external libcall.
381+
enum IntrinsicFallback {
382+
Fallback(&'static str),
383+
Codegen(Value),
384+
}
385+
458386
fn codegen_regular_intrinsic_call<'tcx>(
459387
fx: &mut FunctionCx<'_, '_, 'tcx>,
460388
instance: Instance<'tcx>,
@@ -1160,7 +1088,94 @@ fn codegen_regular_intrinsic_call<'tcx>(
11601088
ret.write_cvalue(fx, old);
11611089
}
11621090

1091+
sym::fma | sym::fmuladd => {
1092+
intrinsic_args!(fx, args => (arg1, arg2, arg3); intrinsic);
1093+
let layout = arg1.layout();
1094+
let ty::Float(float_ty) = layout.ty.kind() else {
1095+
span_bug!(
1096+
source_info.span,
1097+
"expected a float type for {:?} intrinsic, but got: {:?}",
1098+
intrinsic,
1099+
layout.ty
1100+
);
1101+
};
1102+
use FloatTy::*;
1103+
use IntrinsicFallback::*;
1104+
let x = arg1.load_scalar(fx);
1105+
let y = arg2.load_scalar(fx);
1106+
let z = arg3.load_scalar(fx);
1107+
let res = match (intrinsic, float_ty) {
1108+
// FIXME: calling `fma` from libc without FMA target feature uses expensive
1109+
// sofware emulation, use cranelift intrinsic analogous to llvm.fmuladd.*.
1110+
(sym::fma | sym::fmuladd, F32 | F64) => Codegen(fx.bcx.ins().fma(x, y, z)),
1111+
1112+
(sym::fma, F128) => Fallback("fmaf128"),
1113+
1114+
(sym::fmuladd, F128) | (_, F16) => {
1115+
// We use the intrinsic fallback bodies for the rest
1116+
return Err(Instance::new_raw(instance.def_id(), instance.args));
1117+
}
1118+
1119+
_ => unreachable!(),
1120+
};
1121+
let val = match res {
1122+
Codegen(val) => val,
1123+
Fallback(name) => {
1124+
let ty = fx.clif_type(layout.ty).unwrap();
1125+
let arg = AbiParam::new(ty);
1126+
fx.lib_call(name, vec![arg, arg, arg], vec![arg], &[x, y, z])[0]
1127+
}
1128+
};
1129+
let val = CValue::by_val(val, layout);
1130+
ret.write_cvalue(fx, val);
1131+
}
1132+
1133+
sym::powf => {
1134+
intrinsic_args!(fx, args => (arg1, arg2); intrinsic);
1135+
let layout = arg1.layout();
1136+
let ty::Float(float_ty) = layout.ty.kind() else {
1137+
span_bug!(
1138+
source_info.span,
1139+
"expected a float type for {:?} intrinsic, but got: {:?}",
1140+
intrinsic,
1141+
layout.ty
1142+
);
1143+
};
1144+
use FloatTy::*;
1145+
use IntrinsicFallback::*;
1146+
let x = arg1.load_scalar(fx);
1147+
let y = arg2.load_scalar(fx);
1148+
let res = match (intrinsic, float_ty) {
1149+
(sym::powf, F32) => Fallback("powf"),
1150+
(sym::powf, F64) => Fallback("pow"),
1151+
(sym::powf, F128) => Fallback("powf128"),
1152+
1153+
(_, F16) => {
1154+
// We use the intrinsic fallback bodies for the rest
1155+
return Err(Instance::new_raw(instance.def_id(), instance.args));
1156+
}
1157+
1158+
_ => unreachable!(),
1159+
};
1160+
let val = match res {
1161+
Codegen(_) => span_bug!(source_info.span, "`powf` is always lowered to a libcall"),
1162+
Fallback(name) => {
1163+
let ty = fx.clif_type(layout.ty).unwrap();
1164+
let arg = AbiParam::new(ty);
1165+
fx.lib_call(name, vec![arg, arg], vec![arg], &[x, y])[0]
1166+
}
1167+
};
1168+
let val = CValue::by_val(val, layout);
1169+
ret.write_cvalue(fx, val);
1170+
}
1171+
11631172
sym::fabs
1173+
| sym::sqrt
1174+
| sym::floor
1175+
| sym::ceil
1176+
| sym::trunc
1177+
| sym::round
1178+
| sym::round_ties_even
11641179
| sym::exp
11651180
| sym::exp2
11661181
| sym::log
@@ -1173,23 +1188,35 @@ fn codegen_regular_intrinsic_call<'tcx>(
11731188
let ty::Float(float_ty) = layout.ty.kind() else {
11741189
span_bug!(
11751190
source_info.span,
1176-
"expected float type for fabs intrinsic: {:?}",
1191+
"expected a float type for {:?} intrinsic, but got: {:?}",
1192+
intrinsic,
11771193
layout.ty
11781194
);
11791195
};
1180-
enum IntrinsicFallback {
1181-
Fallback(&'static str),
1182-
Codegen(Value),
1183-
}
11841196
use FloatTy::*;
11851197
use IntrinsicFallback::*;
11861198
let x = arg.load_scalar(fx);
11871199
let res = match (intrinsic, float_ty) {
11881200
(sym::fabs, F32 | F64) => Codegen(fx.bcx.ins().fabs(x)),
1189-
// FIXME(bytecodealliance/wasmtime#8312): Use `fabsf16` once Cranelift
1190-
// backend lowerings are implemented.
1191-
(sym::fabs, F16) => Codegen(codegen_f16_f128::abs_f16(fx, x)),
1192-
(sym::fabs, F128) => Codegen(codegen_f16_f128::abs_f128(fx, x)),
1201+
1202+
(sym::sqrt, F32 | F64) => Codegen(fx.bcx.ins().sqrt(x)),
1203+
(sym::sqrt, F128) => Fallback("sqrtf128"),
1204+
1205+
(sym::floor, F32 | F64) => Codegen(fx.bcx.ins().floor(x)),
1206+
(sym::floor, F128) => Fallback("floorf128"),
1207+
1208+
(sym::ceil, F32 | F64) => Codegen(fx.bcx.ins().ceil(x)),
1209+
(sym::ceil, F128) => Fallback("ceilf128"),
1210+
1211+
(sym::trunc, F32 | F64) => Codegen(fx.bcx.ins().trunc(x)),
1212+
(sym::trunc, F128) => Fallback("truncf128"),
1213+
1214+
(sym::round_ties_even, F32 | F64) => Codegen(fx.bcx.ins().nearest(x)),
1215+
(sym::round_ties_even, F128) => Fallback("rintf128"),
1216+
1217+
(sym::round, F32) => Fallback("roundf"),
1218+
(sym::round, F64) => Fallback("round"),
1219+
(sym::round, F128) => Fallback("roundf128"),
11931220

11941221
(sym::exp, F32) => Fallback("expf"),
11951222
(sym::exp, F64) => Fallback("exp"),
@@ -1219,8 +1246,10 @@ fn codegen_regular_intrinsic_call<'tcx>(
12191246
(sym::cos, F64) => Fallback("cos"),
12201247
(sym::cos, F128) => Fallback("cosf128"),
12211248

1222-
(_, F16) => {
1223-
// We implement fallbacks for other f16 intrinsics via f32
1249+
(sym::fabs, F128) | (_, F16) => {
1250+
// FIXME(bytecodealliance/wasmtime#8312): Use the native operations once
1251+
// Cranelift backend lowerings for `f16` are implemented.
1252+
// We use the intrinsic fallback bodies for the rest
12241253
return Err(Instance::new_raw(instance.def_id(), instance.args));
12251254
}
12261255

0 commit comments

Comments
 (0)