Skip to content

Commit f79fc4a

Browse files
authored
Add f16 and f128 support to NaN canonicalization (#12337)
Extend the NaN canonicalization pass to handle f16 and f128 floating point types, which were previously unsupported and would cause a panic. Closes #12336
1 parent 55105fb commit f79fc4a

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

cranelift/codegen/src/nan_canonicalization.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use crate::cursor::{Cursor, FuncCursor};
66
use crate::ir::condcodes::FloatCC;
7-
use crate::ir::immediates::{Ieee32, Ieee64};
7+
use crate::ir::immediates::{Ieee16, Ieee32, Ieee64, Ieee128};
88
use crate::ir::types::{self};
99
use crate::ir::{Function, Inst, InstBuilder, InstructionData, Opcode, Value};
1010
use crate::opts::MemFlags;
@@ -90,6 +90,10 @@ fn add_nan_canon_seq(pos: &mut FuncCursor, inst: Inst, has_vector_support: bool)
9090
};
9191

9292
match val_type {
93+
types::F16 => {
94+
let canon_nan = pos.ins().f16const(Ieee16::NAN);
95+
scalar_select(pos, canon_nan);
96+
}
9397
types::F32 => {
9498
let canon_nan = pos.ins().f32const(Ieee32::NAN);
9599
if has_vector_support {
@@ -116,6 +120,11 @@ fn add_nan_canon_seq(pos: &mut FuncCursor, inst: Inst, has_vector_support: bool)
116120
let canon_nan = pos.ins().splat(types::F64X2, canon_nan);
117121
vector_select(pos, canon_nan);
118122
}
123+
types::F128 => {
124+
let nan_const = pos.func.dfg.constants.insert(Ieee128::NAN.into());
125+
let canon_nan = pos.ins().f128const(nan_const);
126+
scalar_select(pos, canon_nan);
127+
}
119128
_ => {
120129
// Panic if the type given was not an IEEE floating point type.
121130
panic!("Could not canonicalize NaN: Unexpected result type found.");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test compile
2+
set enable_nan_canonicalization=true
3+
target riscv64 has_zfhmin has_zfh
4+
5+
function %fadd_f16(f16, f16) -> f16 {
6+
block0(v0: f16, v1: f16):
7+
v2 = fadd v0, v1
8+
return v2
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
test compile
2+
set enable_nan_canonicalization=true
3+
target s390x
4+
5+
function %fadd_f128(i64, f128, f128) {
6+
block0(v0: i64, v1: f128, v2: f128):
7+
v3 = fadd v1, v2
8+
store v3, v0
9+
return
10+
}

0 commit comments

Comments
 (0)