A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Cranelift: fix two idempotent-store elimination bugs (#15073)
Nick Fitzgerald9cfa762beac0
diff --git a/cranelift/codegen/src/alias_analysis.rs b/cranelift/codegen/src/alias_analysis.rsindex 0cbf5475ad..b6f062bae0 100644--- a/cranelift/codegen/src/alias_analysis.rs+++ b/cranelift/codegen/src/alias_analysis.rs@@ -1492,19 +1492,38 @@ impl<'a> AliasAnalysis<'a> {// paths leading to it go through the original store// instruction.&& self.domtree.dominates(def_inst, inst, &func.layout)+ // An atomic store still publishes its value to other+ // threads and orders the accesses around it, even when+ // the bytes do not change.+ && !has_memory_fence_semantics(opcode){trace!(" --> idempotent store of {store_data} to loc {check_loc:?}");- // We are removing this idempotent store in favor of the- // original, so if this idempotent store was observed,- // then the original must now be observed as well.if let LastStore::Inst(last_store) = last_store {+ // We are removing this idempotent store in favor of+ // the original, so if this idempotent store was+ // observed, then the original must now be observed+ // as well.if let Some(observer) = self.observed_stores.get(&inst).copied() {let entry =self.observed_stores.entry(last_store).or_insert(observer);*entry = Observer::meet(*entry, observer);trace!(" observed_stores[{last_store:?}] = {entry:?}");}++ // And if the `last_store` has a trap code but this+ // one doesn't, then we need to additionally mark it+ // as observed by many because a later non-trapping+ // store can observe whether or not it trapped.+ let trap_code =+ |i: Inst| func.dfg.insts[i].memflags_trap_code(&func.dfg);+ if trap_code(inst).is_none()+ && trap_code(last_store).is_some()+ && func.dfg.insts[last_store].opcode().can_store()+ {+ self.observed_stores.insert(last_store, Observer::Many);+ trace!(" observed_stores[{last_store:?}] = Many");+ }}return OptResult::IdempotentStore;diff --git a/cranelift/filetests/filetests/alias/idempotent-store-fence-semantics.clif b/cranelift/filetests/filetests/alias/idempotent-store-fence-semantics.clifnew file mode 100644index 0000000000..ba49377cce--- /dev/null+++ b/cranelift/filetests/filetests/alias/idempotent-store-fence-semantics.clif@@ -0,0 +1,70 @@+test optimize precise-output+set opt_level=speed+target x86_64++;; An atomic store publishes its value to other threads and orders the accesses+;; around it, so it is not a no-op even when the bytes do not change.+function %two_atomic_stores(i64, i32) {+block0(v0: i64, v1: i32):+ atomic_store v1, v0+ atomic_store v1, v0+ return+}++; function %two_atomic_stores(i64, i32) fast {+; block0(v0: i64, v1: i32):+; atomic_store v1, v0+; atomic_store v1, v0+; return+; }++;; Same, but with regions.+function %two_atomic_stores_region(i64, i32) {+ region0 = 1 "heap"++block0(v0: i64, v1: i32):+ atomic_store region0 v1, v0+ atomic_store region0 v1, v0+ return+}++; function %two_atomic_stores_region(i64, i32) fast {+; region0 = 1 "heap"+;+; block0(v0: i64, v1: i32):+; atomic_store region0 v1, v0+; atomic_store region0 v1, v0+; return+; }++;; The trailing atomic store is what makes the `region0` store visible to other+;; threads before it.+function %atomic_store_orders_other_region(i64, i32, i64, i32) {+ region0 = 1 "heap"++block0(v0: i64, v1: i32, v2: i64, v3: i32):+ atomic_store v1, v0++ ;; Cannot be removed by dead-store elimination because the following atomic+ ;; store is a fence.+ store notrap region0 v3, v2++ atomic_store v1, v0++ ;; Cannot be removed by idempotent-store elimination because the previous+ ;; atomic store is a fence.+ store notrap region0 v3, v2++ return+}++; function %atomic_store_orders_other_region(i64, i32, i64, i32) fast {+; region0 = 1 "heap"+;+; block0(v0: i64, v1: i32, v2: i64, v3: i32):+; atomic_store v1, v0+; store notrap region0 v3, v2+; atomic_store v1, v0+; store notrap region0 v3, v2+; return+; }diff --git a/cranelift/filetests/filetests/alias/issue-15053.clif b/cranelift/filetests/filetests/alias/issue-15053.clifnew file mode 100644index 0000000000..ed503cd4fd--- /dev/null+++ b/cranelift/filetests/filetests/alias/issue-15053.clif@@ -0,0 +1,60 @@+test optimize precise-output+set opt_level=speed+target x86_64++;; Regression tests for https://github.com/bytecodealliance/wasmtime/issues/15053++function %issue_15053(i64, i64, i64, i64) {+ region0 = 1 "heap"+block0(v0: i64, v1: i64, v2: i64, v3: i64):+ ;; This store is visible if the `region0` store traps, so it cannot be+ ;; removed by dead-store elimination.+ store user42 v2, v0++ ;; This is idempotent, so it should be removed, which means we need to+ ;; mark the previous store observed.+ store notrap v2, v0++ ;; This can trap, at which point someone could observe the first store.+ store user42 region0 v3, v1++ ;; This overwrites the first store (assuming the store to `region0` didn't+ ;; trap, which the optimizer cannot assume while remaining sound).+ store user42 v2, v2++ return+}++; function %issue_15053(i64, i64, i64, i64) fast {+; region0 = 1 "heap"+;+; block0(v0: i64, v1: i64, v2: i64, v3: i64):+; store user42 v2, v0+; store user42 region0 v3, v1+; store user42 v2, v2+; return+; }++;; The same as above, but instead of having a region-less store that sits in the+;; `last_fence` slot, we have named regions.+function %region_slot(i64, i64, i64, i64) {+ region0 = 1 "a"+ region1 = 2 "b"+block0(v0: i64, v1: i64, v2: i64, v3: i64):+ store user42 region1 v2, v0+ store notrap region1 v2, v0+ store user42 region0 v3, v1+ store user42 region1 v2, v2+ return+}++; function %region_slot(i64, i64, i64, i64) fast {+; region0 = 1 "a"+; region1 = 2 "b"+;+; block0(v0: i64, v1: i64, v2: i64, v3: i64):+; store user42 region1 v2, v0+; store user42 region0 v3, v1+; store user42 region1 v2, v2+; return+; }