Skip to content

Commit 60a0443

Browse files
authored
Use a wrapping add in egraph optimizations (#11588)
* Use a wrapping add in egraph optimizations This commit fixes an accidental bug from #11526 found via fuzzing where the `u64_add` helper in ISLE is a panicking add but the desired semantics here were a wrapping add. A test is added here and the additions are updated to `u64_wrapping_add` instead. * Add output assertion
1 parent c934058 commit 60a0443

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

cranelift/codegen/src/opts/selects.isle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,5 @@
9696
(rule (simplify
9797
(iadd ty (select ty c (iconst_u ty x) (iconst_u ty y)) (iconst_u ty z)))
9898
(select ty c
99-
(iconst ty (imm64_masked ty (u64_add x z)))
100-
(iconst ty (imm64_masked ty (u64_add y z)))))
99+
(iconst ty (imm64_masked ty (u64_wrapping_add x z)))
100+
(iconst ty (imm64_masked ty (u64_wrapping_add y z)))))

cranelift/filetests/filetests/egraph/selects.clif

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ block0(v0: i8):
4141
; return v8
4242
; }
4343

44+
45+
;; (iadd ty (select ty c x y) z) -> (select ty c (x + z) (y + z))
46+
function %simplify_iadd_select_const_i64_big_constants(i8) -> i64 fast {
47+
block0(v0: i8):
48+
v1 = iconst.i64 0xa000_0000_0000_0000
49+
v2 = iconst.i64 0xb000_0000_0000_0000
50+
v3 = select v0, v1, v2
51+
v4 = iconst.i64 0xc000_0000_0000_0000
52+
v5 = iadd v3, v4
53+
return v5
54+
}
55+
56+
; function %simplify_iadd_select_const_i64_big_constants(i8) -> i64 fast {
57+
; block0(v0: i8):
58+
; v6 = iconst.i64 0x6000_0000_0000_0000
59+
; v7 = iconst.i64 0x7000_0000_0000_0000
60+
; v8 = select v0, v6, v7 ; v6 = 0x6000_0000_0000_0000, v7 = 0x7000_0000_0000_0000
61+
; return v8
62+
; }
63+

0 commit comments

Comments
 (0)