Skip to content

Commit 2778b7d

Browse files
committed
Add nodes for float extend/truncate operations
These always go between `f32` and `f64`, much like the integer ones.
1 parent 575b733 commit 2778b7d

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# run: verify-err
2+
3+
func @fext_bad_input_count:f32() {
4+
# check: function `fext_bad_input_count`:
5+
# unordered: `$val:f32 = fext $val, $val, $val`: bad input count, expected 1
6+
# unordered: `$val:f32 = fext`: bad input count, expected 1
7+
8+
%0:ctrl = entry
9+
%1:f32 = fext
10+
%2:f32 = fext %1, %1, %1
11+
return %0, %2
12+
}
13+
14+
func @fext_bad_output_count:f64(f32) {
15+
# check: function `fext_bad_output_count`:
16+
# unordered: `$val:f64, $val:f64 = fext $val`: bad output count, expected 1
17+
18+
%0:ctrl, %1:f32 = entry
19+
%2:f64, %3:f64 = fext %1
20+
return %0, %2
21+
}
22+
23+
func @fext_bad_output_kind:f32(f32) {
24+
# check: function `fext_bad_output_kind`:
25+
# unordered: `$val:f32 = fext $val`: bad value kind for output 0, expected one of `f64`, got `f32`
26+
27+
%0:ctrl, %1:f32 = entry
28+
%2:f32 = fext %1
29+
return %0, %2
30+
}
31+
32+
func @fext_bad_input_kind:f64(f64) {
33+
# check: function `fext_bad_input_kind`:
34+
# unordered: `$val:f64 = fext $val`: bad value kind for input 0, expected one of `f32`, got `f64`
35+
36+
%0:ctrl, %1:f64 = entry
37+
%2:f64 = fext %1
38+
return %0, %2
39+
}
40+
41+
func @ftrunc_bad_input_count:f32() {
42+
# check: function `ftrunc_bad_input_count`:
43+
# unordered: `$val:f32 = ftrunc $val, $val, $val`: bad input count, expected 1
44+
# unordered: `$val:f32 = ftrunc`: bad input count, expected 1
45+
46+
%0:ctrl = entry
47+
%1:f32 = ftrunc
48+
%2:f32 = ftrunc %1, %1, %1
49+
return %0, %2
50+
}
51+
52+
func @ftrunc_bad_output_count:f32(f64) {
53+
# check: function `ftrunc_bad_output_count`:
54+
# unordered: `$val:f32, $val:f32 = ftrunc $val`: bad output count, expected 1
55+
56+
%0:ctrl, %1:f64 = entry
57+
%2:f32, %3:f32 = ftrunc %1
58+
return %0, %2
59+
}
60+
61+
func @ftrunc_bad_output_kind:f64(f64) {
62+
# check: function `ftrunc_bad_output_kind`:
63+
# unordered: `$val:f64 = ftrunc $val`: bad value kind for output 0, expected one of `f32`, got `f64`
64+
65+
%0:ctrl, %1:f64 = entry
66+
%2:f64 = ftrunc %1
67+
return %0, %2
68+
}
69+
70+
func @ftrunc_bad_input_kind:f32(f32) {
71+
# check: function `ftrunc_bad_input_kind`:
72+
# unordered: `$val:f32 = ftrunc $val`: bad value kind for input 0, expected one of `f64`, got `f32`
73+
74+
%0:ctrl, %1:f32 = entry
75+
%2:f32 = ftrunc %1
76+
return %0, %2
77+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# run: verify-ok
2+
3+
# check: $()
4+
5+
func @fext:f64(f32) {
6+
%0:ctrl, %1:f32 = entry
7+
%2:f64 = fext %1
8+
return %0, %2
9+
}
10+
11+
func @ftrunc:f32(f64) {
12+
%0:ctrl, %1:f64 = entry
13+
%2:f32 = ftrunc %1
14+
return %0, %2
15+
}

crates/ir/src/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ pub enum NodeKind {
239239
Fsub,
240240
Fmul,
241241
Fdiv,
242+
Fext,
243+
Ftrunc,
242244
Fcmp(FcmpKind),
243245
SintToFloat,
244246
UintToFloat,

crates/ir/src/verify/node.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub fn verify_node_kind(
4848
NodeKind::Fsub => verify_float_binop(graph, node, errors),
4949
NodeKind::Fmul => verify_float_binop(graph, node, errors),
5050
NodeKind::Fdiv => verify_float_binop(graph, node, errors),
51+
NodeKind::Fext => verify_fext(graph, node, errors),
52+
NodeKind::Ftrunc => verify_ftrunc(graph, node, errors),
5153
NodeKind::Fcmp(_) => verify_fcmp(graph, node, errors),
5254
NodeKind::SintToFloat => verify_inttofloat(graph, node, errors),
5355
NodeKind::UintToFloat => verify_inttofloat(graph, node, errors),
@@ -332,6 +334,22 @@ fn verify_float_binop(graph: &ValGraph, node: Node, errors: &mut Vec<FunctionVer
332334
let _ = verify_input_kind(graph, node, 1, &[result_kind], errors);
333335
}
334336

337+
fn verify_fext(graph: &ValGraph, node: Node, errors: &mut Vec<FunctionVerifierError>) {
338+
let Ok([result]) = verify_node_arity(graph, node, 1, errors) else {
339+
return;
340+
};
341+
let _ = verify_input_kind(graph, node, 0, &[DepValueKind::Value(Type::F32)], errors);
342+
let _ = verify_output_kind(graph, result, &[DepValueKind::Value(Type::F64)], errors);
343+
}
344+
345+
fn verify_ftrunc(graph: &ValGraph, node: Node, errors: &mut Vec<FunctionVerifierError>) {
346+
let Ok([result]) = verify_node_arity(graph, node, 1, errors) else {
347+
return;
348+
};
349+
let _ = verify_input_kind(graph, node, 0, &[DepValueKind::Value(Type::F64)], errors);
350+
let _ = verify_output_kind(graph, result, &[DepValueKind::Value(Type::F32)], errors);
351+
}
352+
335353
fn verify_fcmp(graph: &ValGraph, node: Node, errors: &mut Vec<FunctionVerifierError>) {
336354
let Ok([result]) = verify_node_arity(graph, node, 2, errors) else {
337355
return;

crates/ir/src/write.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ pub fn write_node_kind(
289289
NodeKind::Fsub => write!(w, "fsub")?,
290290
NodeKind::Fmul => write!(w, "fmul")?,
291291
NodeKind::Fdiv => write!(w, "fdiv")?,
292+
NodeKind::Fext => write!(w, "fext")?,
293+
NodeKind::Ftrunc => write!(w, "ftrunc")?,
292294
NodeKind::Fcmp(kind) => write!(w, "fcmp {kind}")?,
293295
NodeKind::SintToFloat => write!(w, "sinttofloat")?,
294296
NodeKind::UintToFloat => write!(w, "uinttofloat")?,

crates/parser/src/grammar.pest

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ nodekind = {
7676
| "fsub"
7777
| "fmul"
7878
| "fdiv"
79+
| "fext"
80+
| "ftrunc"
7981
| fcmp_nodekind
8082
| "sinttofloat"
8183
| "uinttofloat"

crates/parser/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ fn extract_node_kind(
278278
"fsub" => Ok(NodeKind::Fsub),
279279
"fmul" => Ok(NodeKind::Fmul),
280280
"fdiv" => Ok(NodeKind::Fdiv),
281+
"fext" => Ok(NodeKind::Fext),
282+
"ftrunc" => Ok(NodeKind::Ftrunc),
281283
"sinttofloat" => Ok(NodeKind::SintToFloat),
282284
"uinttofloat" => Ok(NodeKind::UintToFloat),
283285
"floattosint" => Ok(NodeKind::FloatToSint),
@@ -702,6 +704,8 @@ mod tests {
702704
"fsub",
703705
"fmul",
704706
"fdiv",
707+
"fext",
708+
"ftrunc",
705709
"fcmp oeq",
706710
"fcmp one",
707711
"fcmp olt",

0 commit comments

Comments
 (0)