Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

cranelift: preserve rematerialization for cloned egraph values (#15205)

Lancelot de Ferrière0eccac5d55e8
diff --git a/cranelift/codegen/src/egraph/elaborate.rs b/cranelift/codegen/src/egraph/elaborate.rs
index 9cabc89f05..56a1f1f73e 100644
--- a/cranelift/codegen/src/egraph/elaborate.rs
+++ b/cranelift/codegen/src/egraph/elaborate.rs
@@ -473,6 +473,7 @@ impl<'a> Elaborator<'a> {
/// `true` if a rewrite occurred.
fn maybe_remat_arg(
remat_values: &FxHashSet<Value>,
+ value_to_best_value: &SecondaryMap<Value, BestEntry>,
func: &mut Function,
remat_copies: &mut FxHashMap<(Block, Value), Value>,
insert_block: Block,
@@ -486,7 +487,11 @@ impl<'a> Elaborator<'a> {
// would affect, e.g., adds-with-one-constant-arg, which are
// currently rematerialized. Right now we don't do this, to
// avoid the need for another fixpoint loop here.
- if arg.in_block != insert_block && remat_values.contains(&arg.value) {
+ // Elaboration may have cloned the instruction producing `arg`. The
+ // remat set contains original values, so look up the clone's original
+ // best value before checking whether it should be rematerialized.
+ if arg.in_block != insert_block && remat_values.contains(&value_to_best_value[arg.value].1)
+ {
let new_value = match remat_copies.entry((insert_block, arg.value)) {
HashEntry::Occupied(o) => *o.get(),
HashEntry::Vacant(v) => {
@@ -700,6 +705,7 @@ impl<'a> Elaborator<'a> {
for arg_value in arg_values.iter_mut() {
if Self::maybe_remat_arg(
&self.remat_values,
+ &self.value_to_best_value,
&mut self.func,
&mut self.remat_copies,
insert_block,
@@ -851,6 +857,7 @@ impl<'a> Elaborator<'a> {
let mut new_arg = self.elaborate_eclass_use(*arg, before);
Self::maybe_remat_arg(
&self.remat_values,
+ &self.value_to_best_value,
&mut self.func,
&mut self.remat_copies,
block,
diff --git a/cranelift/filetests/filetests/egraph/remat-clone.clif b/cranelift/filetests/filetests/egraph/remat-clone.clif
new file mode 100644
index 0000000000..82158da8a9
--- /dev/null
+++ b/cranelift/filetests/filetests/egraph/remat-clone.clif
@@ -0,0 +1,86 @@
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+;; The constant is first elaborated in one arm of the branch. In the other
+;; arm, it is cloned and hoisted out of the loop. Both the pure select and
+;; the side-effecting store must rematerialize the clone inside the loop.
+function %constant(i64, i64, i64) -> i64 {
+block0(v0: i64, v1: i64, v2: i64):
+ v3 = iconst.i64 0x7ff9_0000_0000_0000
+ brif v0, block2, block1
+
+block1:
+ return v3
+
+block2:
+ jump block3(v1)
+
+block3(v4: i64):
+ v5 = select v4, v3, v4
+ store v5, v2
+ store v3, v2+8
+ brif v4, block3(v5), block4
+
+block4:
+ return v4
+}
+
+; function %constant(i64, i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64, v2: i64):
+; brif v0, block2, block1
+;
+; block1:
+; v3 = iconst.i64 0x7ff9_0000_0000_0000
+; return v3 ; v3 = 0x7ff9_0000_0000_0000
+;
+; block2:
+; v6 = iconst.i64 0x7ff9_0000_0000_0000
+; jump block3(v1)
+;
+; block3(v4: i64):
+; v7 = iconst.i64 0x7ff9_0000_0000_0000
+; v8 = select v4, v7, v4 ; v7 = 0x7ff9_0000_0000_0000
+; store v8, v2
+; store v7, v2+8 ; v7 = 0x7ff9_0000_0000_0000
+; brif v4, block3(v8), block4
+;
+; block4:
+; return v4
+; }
+
+;; Cloning must also preserve rematerialization for non-constant instructions,
+;; even without loops. The clone in block2 should be rematerialized in block3.
+function %bnot(i64, i64, i64) -> i64 {
+block0(v0: i64, v1: i64, v2: i64):
+ v3 = bnot v1
+ brif v0, block2, block1
+
+block1:
+ return v3
+
+block2:
+ store v3, v2
+ jump block3
+
+block3:
+ return v3
+}
+
+; function %bnot(i64, i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64, v2: i64):
+; brif v0, block2, block1
+;
+; block1:
+; v3 = bnot.i64 v1
+; return v3
+;
+; block2:
+; v4 = bnot.i64 v1
+; store v4, v2
+; jump block3
+;
+; block3:
+; v5 = bnot.i64 v1
+; return v5
+; }
diff --git a/tests/disas/gc/drc/array-new-fixed-of-gc-refs.wat b/tests/disas/gc/drc/array-new-fixed-of-gc-refs.wat
index 23ce66d5c6..d592431e4a 100644
--- a/tests/disas/gc/drc/array-new-fixed-of-gc-refs.wat
+++ b/tests/disas/gc/drc/array-new-fixed-of-gc-refs.wat
@@ -117,11 +117,12 @@
;; block6:
;; @0025 v157 = uextend.i64 v181
;; @0025 v160 = iadd.i64 v20, v157
-;; @0025 v162 = iadd v160, v313 ; v313 = 8
-;; @0025 v163 = load.i64 user2 region8 v162
-;; @0025 v318 = iconst.i64 1
-;; @0025 v165 = iadd v163, v318 ; v318 = 1
-;; @0025 store user2 region8 v165, v162
+;; @0025 v318 = iconst.i64 8
+;; @0025 v319 = iadd v160, v318 ; v318 = 8
+;; @0025 v163 = load.i64 user2 region8 v319
+;; @0025 v320 = iconst.i64 1
+;; @0025 v165 = iadd v163, v320 ; v320 = 1
+;; @0025 store user2 region8 v165, v319
;; @0025 jump block7
;;
;; block7:
diff --git a/tests/disas/stack-switching/resume-suspend-data-passing.wat b/tests/disas/stack-switching/resume-suspend-data-passing.wat
index 83f7a22531..d856254dc5 100644
--- a/tests/disas/stack-switching/resume-suspend-data-passing.wat
+++ b/tests/disas/stack-switching/resume-suspend-data-passing.wat
@@ -144,28 +144,30 @@
;; @0044 brif v105, block10, block11
;;
;; block10 cold:
-;; @0044 v59 = load.i64 notrap aligned region4 v93+8
+;; @0044 v111 = iadd.i64 v9, v92 ; v92 = 168
+;; @0044 v59 = load.i64 notrap aligned region4 v111+8
;; @0044 v60 = load.i32 notrap aligned region6 v59
-;; @003a v110 = iconst.i32 0
-;; @0044 store notrap aligned region4 v110, v93 ; v110 = 0
-;; @0044 store notrap aligned region8 v110, v93+4 ; v110 = 0
-;; @0044 v111 = iconst.i64 0
-;; @0044 store notrap aligned region4 v111, v93+8 ; v111 = 0
+;; @003a v112 = iconst.i32 0
+;; @0044 store notrap aligned region4 v112, v111 ; v112 = 0
+;; @0044 store notrap aligned region8 v112, v111+4 ; v112 = 0
+;; @0044 v113 = iconst.i64 0
+;; @0044 store notrap aligned region4 v113, v111+8 ; v113 = 0
;; @0044 try_call fn0(v0, v60), sig0, block12, [ context v0 ]
;;
;; block12:
;; @0044 trap user12
;;
;; block11:
-;; @0044 v66 = load.i64 notrap aligned region4 v93+8
-;; @003a v106 = iconst.i32 0
-;; @0044 store notrap aligned region4 v106, v93 ; v106 = 0
-;; @0044 store notrap aligned region8 v106, v93+4 ; v106 = 0
-;; @0044 v107 = iconst.i64 0
-;; @0044 store notrap aligned region4 v107, v93+8 ; v107 = 0
-;; @0044 v108 = iconst.i32 1
-;; @004a v109 = isub.i32 v71, v108 ; v108 = 1
-;; @004d brif v109, block2(v109), block13
+;; @0044 v106 = iadd.i64 v9, v92 ; v92 = 168
+;; @0044 v66 = load.i64 notrap aligned region4 v106+8
+;; @003a v107 = iconst.i32 0
+;; @0044 store notrap aligned region4 v107, v106 ; v107 = 0
+;; @0044 store notrap aligned region8 v107, v106+4 ; v107 = 0
+;; @0044 v108 = iconst.i64 0
+;; @0044 store notrap aligned region4 v108, v106+8 ; v108 = 0
+;; @0044 v109 = iconst.i32 1
+;; @004a v110 = isub.i32 v71, v109 ; v109 = 1
+;; @004d brif v110, block2(v110), block13
;;
;; block13:
;; @004f jump block3