Skip to content

Commit cc8df84

Browse files
authored
pulley: Support smaller int-to-float conversions (bytecodealliance#10017)
* pulley: Support smaller int-to-float conversions This adds support for converting 8/16-bit integers to floats to Pulley. This is not directly accessible from wasm instructions but is possible through various optimizations to create. A new `*.clif` runtest was added exercising many combinations of scalars-to-scalars for int-to-floats of varying widths. * Include new test
1 parent bf1b863 commit cc8df84

File tree

3 files changed

+195
-12
lines changed

3 files changed

+195
-12
lines changed

cranelift/codegen/src/isa/pulley_shared/lower.isle

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,28 +1258,28 @@
12581258

12591259
;;;; Rules for `fcvt_from_{u,s}int` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12601260

1261-
(rule (lower (has_type $F32 (fcvt_from_uint val @ (value_type $I32))))
1262-
(pulley_f32_from_x32_u val))
1261+
(rule 0 (lower (has_type $F32 (fcvt_from_uint val @ (value_type (fits_in_32 _)))))
1262+
(pulley_f32_from_x32_u (zext32 val)))
12631263

1264-
(rule (lower (has_type $F32 (fcvt_from_uint val @ (value_type $I64))))
1264+
(rule 1 (lower (has_type $F32 (fcvt_from_uint val @ (value_type $I64))))
12651265
(pulley_f32_from_x64_u val))
12661266

1267-
(rule (lower (has_type $F64 (fcvt_from_uint val @ (value_type $I32))))
1268-
(pulley_f64_from_x32_u val))
1267+
(rule 0 (lower (has_type $F64 (fcvt_from_uint val @ (value_type (fits_in_32 _)))))
1268+
(pulley_f64_from_x32_u (zext32 val)))
12691269

1270-
(rule (lower (has_type $F64 (fcvt_from_uint val @ (value_type $I64))))
1270+
(rule 1 (lower (has_type $F64 (fcvt_from_uint val @ (value_type $I64))))
12711271
(pulley_f64_from_x64_u val))
12721272

1273-
(rule (lower (has_type $F32 (fcvt_from_sint val @ (value_type $I32))))
1274-
(pulley_f32_from_x32_s val))
1273+
(rule 0 (lower (has_type $F32 (fcvt_from_sint val @ (value_type (fits_in_32 _)))))
1274+
(pulley_f32_from_x32_s (sext32 val)))
12751275

1276-
(rule (lower (has_type $F32 (fcvt_from_sint val @ (value_type $I64))))
1276+
(rule 1 (lower (has_type $F32 (fcvt_from_sint val @ (value_type $I64))))
12771277
(pulley_f32_from_x64_s val))
12781278

1279-
(rule (lower (has_type $F64 (fcvt_from_sint val @ (value_type $I32))))
1280-
(pulley_f64_from_x32_s val))
1279+
(rule 0 (lower (has_type $F64 (fcvt_from_sint val @ (value_type (fits_in_32 _)))))
1280+
(pulley_f64_from_x32_s (sext32 val)))
12811281

1282-
(rule (lower (has_type $F64 (fcvt_from_sint val @ (value_type $I64))))
1282+
(rule 1 (lower (has_type $F64 (fcvt_from_sint val @ (value_type $I64))))
12831283
(pulley_f64_from_x64_s val))
12841284

12851285
(rule (lower (has_type $F32X4 (fcvt_from_sint val @ (value_type $I32X4))))
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
test interpret
2+
test run
3+
target aarch64
4+
target x86_64
5+
target s390x
6+
target riscv64
7+
target riscv64 has_c has_zcb
8+
target pulley32
9+
target pulley32be
10+
target pulley64
11+
target pulley64be
12+
13+
function %i8_to_f32(i8) -> f32 {
14+
block0(v0: i8):
15+
v1 = fcvt_from_sint.f32 v0
16+
return v1
17+
}
18+
19+
; run: %i8_to_f32(0) == 0.0
20+
; run: %i8_to_f32(1) == 0x1.0
21+
; run: %i8_to_f32(-1) == -0x1.0
22+
23+
24+
function %u8_to_f32(i8) -> f32 {
25+
block0(v0: i8):
26+
v1 = fcvt_from_uint.f32 v0
27+
return v1
28+
}
29+
30+
; run: %u8_to_f32(0) == 0.0
31+
; run: %u8_to_f32(1) == 0x1.0
32+
; run: %u8_to_f32(-1) == 0x1.fep7
33+
34+
function %i16_to_f32(i16) -> f32 {
35+
block0(v0: i16):
36+
v1 = fcvt_from_sint.f32 v0
37+
return v1
38+
}
39+
40+
; run: %i16_to_f32(0) == 0.0
41+
; run: %i16_to_f32(1) == 0x1.0
42+
; run: %i16_to_f32(-1) == -0x1.0
43+
44+
45+
function %u16_to_f32(i16) -> f32 {
46+
block0(v0: i16):
47+
v1 = fcvt_from_uint.f32 v0
48+
return v1
49+
}
50+
51+
; run: %u16_to_f32(0) == 0.0
52+
; run: %u16_to_f32(1) == 0x1.0
53+
; run: %u16_to_f32(-1) == 0x1.fffep15
54+
55+
function %i32_to_f32(i32) -> f32 {
56+
block0(v0: i32):
57+
v1 = fcvt_from_sint.f32 v0
58+
return v1
59+
}
60+
61+
; run: %i32_to_f32(0) == 0.0
62+
; run: %i32_to_f32(1) == 0x1.0
63+
; run: %i32_to_f32(-1) == -0x1.0
64+
65+
66+
function %u32_to_f32(i32) -> f32 {
67+
block0(v0: i32):
68+
v1 = fcvt_from_uint.f32 v0
69+
return v1
70+
}
71+
72+
; run: %u32_to_f32(0) == 0.0
73+
; run: %u32_to_f32(1) == 0x1.0
74+
; run: %u32_to_f32(-1) == 0x1.0p32
75+
76+
function %i64_to_f32(i64) -> f32 {
77+
block0(v0: i64):
78+
v1 = fcvt_from_sint.f32 v0
79+
return v1
80+
}
81+
82+
; run: %i64_to_f32(0) == 0.0
83+
; run: %i64_to_f32(1) == 0x1.0
84+
; run: %i64_to_f32(-1) == -0x1.0
85+
86+
87+
function %u64_to_f32(i64) -> f32 {
88+
block0(v0: i64):
89+
v1 = fcvt_from_uint.f32 v0
90+
return v1
91+
}
92+
93+
; run: %u64_to_f32(0) == 0.0
94+
; run: %u64_to_f32(1) == 0x1.0
95+
; run: %u64_to_f32(-1) == 0x1.0p64
96+
97+
function %i8_to_f64(i8) -> f64 {
98+
block0(v0: i8):
99+
v1 = fcvt_from_sint.f64 v0
100+
return v1
101+
}
102+
103+
; run: %i8_to_f64(0) == 0.0
104+
; run: %i8_to_f64(1) == 0x1.0
105+
; run: %i8_to_f64(-1) == -0x1.0
106+
107+
108+
function %u8_to_f64(i8) -> f64 {
109+
block0(v0: i8):
110+
v1 = fcvt_from_uint.f64 v0
111+
return v1
112+
}
113+
114+
; run: %u8_to_f64(0) == 0.0
115+
; run: %u8_to_f64(1) == 0x1.0
116+
; run: %u8_to_f64(-1) == 0x1.fep7
117+
118+
function %i16_to_f64(i16) -> f64 {
119+
block0(v0: i16):
120+
v1 = fcvt_from_sint.f64 v0
121+
return v1
122+
}
123+
124+
; run: %i16_to_f64(0) == 0.0
125+
; run: %i16_to_f64(1) == 0x1.0
126+
; run: %i16_to_f64(-1) == -0x1.0
127+
128+
129+
function %u16_to_f64(i16) -> f64 {
130+
block0(v0: i16):
131+
v1 = fcvt_from_uint.f64 v0
132+
return v1
133+
}
134+
135+
; run: %u16_to_f64(0) == 0.0
136+
; run: %u16_to_f64(1) == 0x1.0
137+
; run: %u16_to_f64(-1) == 0x1.fffep15
138+
139+
function %i32_to_f64(i32) -> f64 {
140+
block0(v0: i32):
141+
v1 = fcvt_from_sint.f64 v0
142+
return v1
143+
}
144+
145+
; run: %i32_to_f64(0) == 0.0
146+
; run: %i32_to_f64(1) == 0x1.0
147+
; run: %i32_to_f64(-1) == -0x1.0
148+
149+
150+
function %u32_to_f64(i32) -> f64 {
151+
block0(v0: i32):
152+
v1 = fcvt_from_uint.f64 v0
153+
return v1
154+
}
155+
156+
; run: %u32_to_f64(0) == 0.0
157+
; run: %u32_to_f64(1) == 0x1.0
158+
; run: %u32_to_f64(-1) == 0x1.fffffffep31
159+
160+
function %i64_to_f64(i64) -> f64 {
161+
block0(v0: i64):
162+
v1 = fcvt_from_sint.f64 v0
163+
return v1
164+
}
165+
166+
; run: %i64_to_f64(0) == 0.0
167+
; run: %i64_to_f64(1) == 0x1.0
168+
; run: %i64_to_f64(-1) == -0x1.0
169+
170+
171+
function %u64_to_f64(i64) -> f64 {
172+
block0(v0: i64):
173+
v1 = fcvt_from_uint.f64 v0
174+
return v1
175+
}
176+
177+
; run: %u64_to_f64(0) == 0.0
178+
; run: %u64_to_f64(1) == 0x1.0
179+
; run: %u64_to_f64(-1) == 0x1.0p64

cranelift/filetests/filetests/runtests/issue5952.clif

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ target x86_64
55
target s390x
66
target riscv64
77
target riscv64 has_c has_zcb
8+
target pulley32
9+
target pulley32be
10+
target pulley64
11+
target pulley64be
812

913
function %a(i16 uext) -> f32 {
1014
block0(v0: i16):

0 commit comments

Comments
 (0)