From 1854b5cb030d11a84cee13adf6a74b2d1946d75d Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Tue, 17 Nov 2026 13:25:22 -0800 Subject: [PATCH] [Heap2Local2] Support struct and array cmpxchg Model struct.atomic.rmw.cmpxchg and array.atomic.rmw.cmpxchg in Heap2Local2 by linking expected and replacement origins as conditional escape edges (so replacement only escapes if expected can match a heap value at runtime) and lowering cmpxchg operations on non-escaping allocations or non-matching expected operands in Optimizer. --- src/passes/Heap2Local2.cpp | 277 ++- test/lit/passes/heap2local-rmw.wast | 2429 +++++++++++++++++++++++---- 2 files changed, 2366 insertions(+), 340 deletions(-) diff --git a/src/passes/Heap2Local2.cpp b/src/passes/Heap2Local2.cpp index 74aaa7d009d..d2f4c9497a6 100644 --- a/src/passes/Heap2Local2.cpp +++ b/src/passes/Heap2Local2.cpp @@ -20,7 +20,8 @@ // // Phase 1 builds a dataflow graph over reference expressions in a function, // collecting candidate StructNew, fixed-size ArrayNew, and ArrayNewFixed -// allocations alongside identity and cast edges and escaping uses. +// allocations alongside identity and cast edges, atomic cmpxchg sites, and +// escaping uses. // // Phase 2 solves a smallest fixed point over Flat origins (i.e. the // flat lattice of all allocation sites) across all candidate allocations @@ -29,8 +30,8 @@ // - Val(a): definitely non-null reference to a single allocation `a`. // - Bot: unreachable or statically trapping cast output. // -// Phase 3 marks allocations escaped from escaping uses and identity-edge mixes -// (Val(a) flowing into Top). +// Phase 3 propagates escapes over cmpxchg (expected -> replacement) edges, +// escaping uses, and identity-edge mixes (Val(a) flowing into Top). // // Phase 4 rewrites every non-escaping allocation and all expressions it flows // into in a single post-order pass, followed by ReFinalize and @@ -100,6 +101,17 @@ struct AllocInfo { } }; +// An atomic cmpxchg site linking expected and replacement origins. If the +// expected allocation does not escape, it can never reside in the heap field +// being compared, so the comparison always fails at runtime and replacement is +// never written to memory. If expected escapes, however, it could match the +// heap value at runtime and cause cmpxchg to store replacement into the heap, +// forcing replacement to escape as well. +struct CmpxchgSite { + NodeId expNode; + NodeId repNode; +}; + // Shared per-function state populated by GraphBuilder (Phase 1) and consumed by // origin propagation (Phase 2), escape analysis (Phase 3), and IR rewriting // (Phase 4). @@ -122,6 +134,9 @@ struct AnalysisState { // Nodes whose values flow into contexts that escape the function or alias // heap memory. std::vector escapingNodes; + // Tracked cmpxchg sites where an escaping expected value forces replacement + // to escape. + std::vector cmpxchgSites; AnalysisState() { // Node 0 (TopNode) is a shared sentinel node with fixed origin Top @@ -194,7 +209,7 @@ struct AnalysisState { }; // Phase 1: Single post-order walk over the function body to collect candidate -// allocations, dataflow edges, and escaping uses. +// allocations, dataflow edges, cmpxchg sites, and escaping uses. struct GraphBuilder : public ControlFlowWalker> { @@ -456,6 +471,39 @@ struct GraphBuilder void visitStructRMW(StructRMW* curr) { state.escapeIfRef(curr->value); } + void handleCmpxchg(Expression* cmpxchg, + Expression* expected, + Expression* replacement) { + if (!replacement->type.isRef()) { + return; + } + NodeId repNode = state.getNodeOrTop(replacement); + if (repNode == TopNode) { + // We will not optimize replacement anyway, so it doesn't depend on + // whether expected escapes. + return; + } + if (cmpxchg->type == Type::unreachable || !expected->type.isRef()) { + // Optimizer leaves unreachable cmpxchgs in place. If a reachable `ref` + // requires `replacement` to be non-nullable, or a bottom nullref cmpxchg + // takes its non-nullable result type from `replacement->type` in + // `finalize()`, optimizing `replacement` and replacing it with a null + // would cause a validation failure. Escape `replacement` to avoid this. + state.escapingNodes.push_back(repNode); + return; + } + NodeId expNode = state.getNodeOrTop(expected); + if (expNode == TopNode) { + state.escapingNodes.push_back(repNode); + } else { + state.cmpxchgSites.push_back({expNode, repNode}); + } + } + + void visitStructCmpxchg(StructCmpxchg* curr) { + handleCmpxchg(curr, curr->expected, curr->replacement); + } + void visitArrayGet(ArrayGet* curr) { if (!curr->index->is()) { state.escapeIfRef(curr->ref); @@ -475,6 +523,14 @@ struct GraphBuilder } state.escapeIfRef(curr->value); } + + void visitArrayCmpxchg(ArrayCmpxchg* curr) { + if (!curr->index->is()) { + state.escapeIfRef(curr->ref); + } + // Note: expected never escapes even when index is non-constant. + handleCmpxchg(curr, curr->expected, curr->replacement); + } }; // Phase 2: Solve smallest fixed point over Flat origins to find which @@ -557,9 +613,58 @@ void computeOrigins(AnalysisState& state) { // Phase 3: Compute escaped allocations and return whether any remain unescaped // and can be optimized. Return true if we find something to optimize. bool computeEscapes(AnalysisState& state) { + size_t numAllocs = state.allocs.size(); + // Conditional escape adjacency list: escapeAdj[u] lists replacement + // allocations v that must escape if expected allocation u escapes in a + // cmpxchg. Lazily allocated since candidate-to-candidate cmpxchg is rare. + std::vector> escapeAdj; + std::vector worklist; + + auto addEscapeEdge = [&](AllocId from, AllocId to) { + if (escapeAdj.empty()) { + escapeAdj.resize(numAllocs); + } + escapeAdj[from].push_back(to); + }; + + auto markEscaped = [&](AllocId alloc) { + if (!state.allocs[alloc].escaped) { + state.allocs[alloc].escaped = true; + worklist.push_back(alloc); + } + }; + + for (const auto& site : state.cmpxchgSites) { + const auto* repAlloc = state.origins[site.repNode].getVal(); + if (!repAlloc) { + // Replacement is either unreachable (Bot) or already mixed to Top (whose + // incoming allocations escape via the mixing check below). + continue; + } + const auto& expOrigin = state.origins[site.expNode]; + if (expOrigin.isTop()) { + // Expected may be an arbitrary heap reference that matches the stored + // value at runtime, so replacement may be written to the heap. + markEscaped(*repAlloc); + } else if (const auto* expAlloc = expOrigin.getVal()) { + // If expAlloc ends up escaping, expected remains a live heap reference + // that could match the field at runtime and cause cmpxchg to write + // replacement to the heap, so repAlloc must escape too (when + // *expAlloc == *repAlloc, that is already vacuously true without another + // edge in the graph). + if (*expAlloc != *repAlloc) { + addEscapeEdge(*expAlloc, *repAlloc); + } + } else { + // The cmpxchg is never executed, so it cannot cause the replacement to + // escape. + assert(expOrigin.isBottom()); + } + } + for (NodeId u : state.escapingNodes) { if (const auto* alloc = state.origins[u].getVal()) { - state.allocs[*alloc].escaped = true; + markEscaped(*alloc); } } @@ -573,7 +678,17 @@ bool computeEscapes(AnalysisState& state) { } if (const auto* alloc = state.origins[u].getVal()) { if (state.origins[v].isTop()) { - state.allocs[*alloc].escaped = true; + markEscaped(*alloc); + } + } + } + + if (!escapeAdj.empty()) { + while (!worklist.empty()) { + AllocId alloc = worklist.back(); + worklist.pop_back(); + for (AllocId dep : escapeAdj[alloc]) { + markEscaped(dep); } } } @@ -646,6 +761,17 @@ struct Optimizer : public PostWalker { return std::nullopt; } + // Returns true when `curr` could match an existing heap value at runtime + // (i.e. it is neither a non-escaping allocation nor dynamically + // unreachable). + bool canMatchHeapValue(Expression* curr) { + if (getOptimizedAlloc(curr)) { + return false; + } + auto it = state.exprNodes.find(curr); + return it == state.exprNodes.end() || !state.origins[it->second].isBottom(); + } + // Create the null value with which we will replace an optimized allocation. Expression* makeOptimizedNull(AllocId alloc) { return builder.makeRefNull( @@ -1007,6 +1133,102 @@ struct Optimizer : public PostWalker { } } + void emitOptimizedRefCmpxchg(Expression* ref, + Expression* expected, + Expression* replacement, + bool expCanMatch, + const AllocInfo& info, + Index index) { + assert(!info.fields[index].isPacked()); + Index fieldLocal = info.fieldLocals[index]; + Type fieldType = info.fields[index].type; + if (!expCanMatch) { + // `expected` is non-escaping or dynamically unreachable, so the + // comparison never succeeds. + replaceCurrent( + builder.makeBlock({builder.makeDrop(ref), + builder.makeDrop(expected), + builder.makeDrop(replacement), + builder.makeLocalGet(fieldLocal, fieldType)})); + return; + } + + Type expectedType = fieldType; + if (fieldType.isRef()) { + expectedType = Type( + HeapTypes::eq.getBasic(fieldType.getHeapType().getShared()), Nullable); + } + // Stash `expected`, `replacement`, and the pre-cmpxchg field value in + // scratch locals so operand evaluation order is preserved if `replacement` + // traps or reads/mutates `local`. + Index oldScratch = builder.addVar(func, fieldType); + Index expectedScratch = builder.addVar(func, expectedType); + Index replacementScratch = builder.addVar(func, fieldType); + + Expression* pred = nullptr; + if (fieldType.isRef()) { + pred = + builder.makeRefEq(builder.makeLocalGet(fieldLocal, fieldType), + builder.makeLocalGet(expectedScratch, expectedType)); + } else { + pred = + builder.makeBinary(Abstract::getBinary(fieldType, Abstract::Eq), + builder.makeLocalGet(fieldLocal, fieldType), + builder.makeLocalGet(expectedScratch, expectedType)); + } + + replaceCurrent(builder.makeBlock( + {builder.makeDrop(ref), + builder.makeLocalSet(expectedScratch, expected), + builder.makeLocalSet(replacementScratch, replacement), + builder.makeLocalSet(oldScratch, + builder.makeLocalGet(fieldLocal, fieldType)), + builder.makeIf( + pred, + builder.makeLocalSet( + fieldLocal, builder.makeLocalGet(replacementScratch, fieldType))), + builder.makeLocalGet(oldScratch, fieldType)})); + } + + void visitStructCmpxchg(StructCmpxchg* curr) { + if (curr->type == Type::unreachable || + curr->ref->type == Type::unreachable) { + return; + } + auto refAlloc = getOptimizedAlloc(curr->ref); + bool expCanMatch = canMatchHeapValue(curr->expected); + if (refAlloc) { + emitOptimizedRefCmpxchg(curr->ref, + curr->expected, + curr->replacement, + expCanMatch, + state.allocs[*refAlloc], + curr->index); + return; + } + if (expCanMatch) { + return; + } + // `curr->ref` is unoptimized while `curr->expected` is a non-escaping + // allocation or dynamically unreachable, so `expected` cannot match the + // heap struct field. Stash `ref` in a scratch local before dropping + // `expected` and `replacement` to preserve evaluation order + // (`ChildLocalizer` cannot be used here because compound `expected` or + // `replacement` operands whose inner allocations became `nullref` still + // carry stale non-nullable types until `ReFinalize`, which would give + // `ChildLocalizer`'s scratch locals invalid non-nullable types). + Type refType = curr->ref->type.with(Nullable); + Index refScratch = builder.addVar(func, refType); + replaceCurrent(builder.makeBlock( + {builder.makeLocalSet(refScratch, curr->ref), + builder.makeDrop(curr->expected), + builder.makeDrop(curr->replacement), + builder.makeStructGet(curr->index, + builder.makeLocalGet(refScratch, refType), + curr->order, + curr->type)})); + } + void visitArrayGet(ArrayGet* curr) { if (curr->type == Type::unreachable) { return; @@ -1064,6 +1286,49 @@ struct Optimizer : public PostWalker { info.fields[idx].type); } } + + void visitArrayCmpxchg(ArrayCmpxchg* curr) { + if (curr->type == Type::unreachable || + curr->ref->type == Type::unreachable) { + return; + } + auto refAlloc = getOptimizedAlloc(curr->ref); + bool expCanMatch = canMatchHeapValue(curr->expected); + if (refAlloc) { + Index idx = curr->index->cast()->value.getUnsigned(); + const auto& info = state.allocs[*refAlloc]; + if (idx >= info.fields.size()) { + replaceCurrent(builder.makeBlock({builder.makeDrop(curr->ref), + builder.makeDrop(curr->expected), + builder.makeDrop(curr->replacement), + builder.makeUnreachable()})); + return; + } + emitOptimizedRefCmpxchg( + curr->ref, curr->expected, curr->replacement, expCanMatch, info, idx); + return; + } + if (expCanMatch) { + return; + } + // `curr->ref` is unoptimized while `curr->expected` is a non-escaping + // allocation or dynamically unreachable, so `expected` cannot match the + // element in memory. Stash `ref` and `index` in scratch locals before + // dropping `expected` and `replacement` to preserve evaluation order, then + // read the element directly. + Type refType = curr->ref->type.with(Nullable); + Index refScratch = builder.addVar(func, refType); + Index indexScratch = builder.addVar(func, Type::i32); + replaceCurrent(builder.makeBlock( + {builder.makeLocalSet(refScratch, curr->ref), + builder.makeLocalSet(indexScratch, curr->index), + builder.makeDrop(curr->expected), + builder.makeDrop(curr->replacement), + builder.makeArrayGet(builder.makeLocalGet(refScratch, refType), + builder.makeLocalGet(indexScratch, Type::i32), + curr->order, + curr->type)})); + } }; struct Heap2Local2 : public WalkerPass> { diff --git a/test/lit/passes/heap2local-rmw.wast b/test/lit/passes/heap2local-rmw.wast index c98828ccc16..e62bb1280e6 100644 --- a/test/lit/passes/heap2local-rmw.wast +++ b/test/lit/passes/heap2local-rmw.wast @@ -21,15 +21,12 @@ ;; HP2L2: (type $2 (func (result i64))) - ;; HP2L2: (type $i32 (struct (field (mut i32)))) - (type $i32 (struct (field (mut i32)))) - ;; HP2L2: (type $arr (array (mut i32))) - - ;; HP2L2: (type $5 (func (param (ref null $struct)) (result (ref null $struct)))) + ;; HP2L2: (type $3 (func (param (ref null $struct)) (result (ref null $struct)))) - ;; HP2L2: (type $6 (func (param (ref $struct)))) + ;; HP2L2: (type $arr (array (mut i32))) - ;; HP2L2: (type $i64 (struct (field (mut i64)))) + ;; HP2L2: (type $i32 (struct (field (mut i32)))) + (type $i32 (struct (field (mut i32)))) (type $i64 (struct (field (mut i64)))) (type $struct (struct (field (mut (ref null $struct))))) (type $arr (array (mut i32))) @@ -50,15 +47,17 @@ ;; CHECK-NEXT: (struct.new_default $struct) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (type $8 (func (param (ref null $struct) (ref null $struct)) (result (ref null $struct)))) + ;; HP2L2: (type $6 (func (param (ref $struct)))) + + ;; HP2L2: (type $7 (func (param (ref null $struct) (ref null $struct)) (result (ref null $struct)))) - ;; HP2L2: (type $9 (func (param i32) (result i32))) + ;; HP2L2: (type $8 (func (param i32) (result i32))) - ;; HP2L2: (type $10 (func)) + ;; HP2L2: (type $9 (func)) - ;; HP2L2: (type $11 (func (param (ref null $i32) (ref null $arr)))) + ;; HP2L2: (type $10 (func (param (ref null $i32) (ref null $arr)))) - ;; HP2L2: (func $escape-rmw (type $5) (param $0 (ref null $struct)) (result (ref null $struct)) + ;; HP2L2: (func $escape-rmw (type $3) (param $0 (ref null $struct)) (result (ref null $struct)) ;; HP2L2-NEXT: (struct.atomic.rmw.xchg $struct 0 ;; HP2L2-NEXT: (local.get $0) ;; HP2L2-NEXT: (struct.new_default $struct) @@ -79,7 +78,7 @@ ;; CHECK-NEXT: (struct.new_default $struct) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $escape-cmpxchg (type $5) (param $0 (ref null $struct)) (result (ref null $struct)) + ;; HP2L2: (func $escape-cmpxchg (type $3) (param $0 (ref null $struct)) (result (ref null $struct)) ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 ;; HP2L2-NEXT: (local.get $0) ;; HP2L2-NEXT: (local.get $0) @@ -116,12 +115,26 @@ ;; CHECK-NEXT: (local.get $2) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $no-escape-cmpxchg-expected (type $5) (param $0 (ref null $struct)) (result (ref null $struct)) - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; HP2L2: (func $no-escape-cmpxchg-expected (type $3) (param $0 (ref null $struct)) (result (ref null $struct)) + ;; HP2L2-NEXT: (local $1 (ref null $struct)) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) + ;; HP2L2-NEXT: (local.set $2 ;; HP2L2-NEXT: (local.get $0) - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (local.get $0) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $struct 0 + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) (func $no-escape-cmpxchg-expected (param (ref null $struct)) (result (ref null $struct)) ;; Allocations that flow into the cmpxchg `expected` operand do not escape @@ -166,20 +179,32 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; HP2L2: (func $no-escape-cmpxchg-expected-null-ref (type $6) (param $x (ref $struct)) + ;; HP2L2-NEXT: (local $1 (ref null $struct)) + ;; HP2L2-NEXT: (local $2 nullref) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (block $block (result (ref $struct)) - ;; HP2L2-NEXT: (br_on_cast_fail $block (ref $struct) (ref $struct) - ;; HP2L2-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) - ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block $block (result (ref none)) + ;; HP2L2-NEXT: (br_on_cast_fail $block (ref none) (ref none) + ;; HP2L2-NEXT: (block (result (ref none)) + ;; HP2L2-NEXT: (local.set $2 ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (local.get $x) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: (block ;; (replaces unreachable StructGet we can't emit) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -586,11 +611,39 @@ ;; CHECK-NEXT: (local.get $1) ;; CHECK-NEXT: ) ;; HP2L2: (func $rmw-cmpxchg-i32 (type $1) (result i32) - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $i32 0 - ;; HP2L2-NEXT: (struct.new_default $i32) + ;; HP2L2-NEXT: (local $0 i32) + ;; HP2L2-NEXT: (local $1 i32) + ;; HP2L2-NEXT: (local $2 i32) + ;; HP2L2-NEXT: (local $3 i32) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $2 ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 ;; HP2L2-NEXT: (i32.const 2) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (if + ;; HP2L2-NEXT: (i32.eq + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (then + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) (func $rmw-cmpxchg-i32 (result i32) (struct.atomic.rmw.cmpxchg $i32 0 @@ -984,11 +1037,39 @@ ;; CHECK-NEXT: (local.get $1) ;; CHECK-NEXT: ) ;; HP2L2: (func $rmw-cmpxchg-i64 (type $2) (result i64) - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $i64 0 - ;; HP2L2-NEXT: (struct.new_default $i64) + ;; HP2L2-NEXT: (local $0 i64) + ;; HP2L2-NEXT: (local $1 i64) + ;; HP2L2-NEXT: (local $2 i64) + ;; HP2L2-NEXT: (local $3 i64) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (i64.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $2 ;; HP2L2-NEXT: (i64.const 1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 ;; HP2L2-NEXT: (i64.const 2) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (if + ;; HP2L2-NEXT: (i64.eq + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (then + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) (func $rmw-cmpxchg-i64 (result i64) (struct.atomic.rmw.cmpxchg $i64 0 @@ -1021,7 +1102,7 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: (local.get $2) ;; CHECK-NEXT: ) - ;; HP2L2: (func $rmw-xchg-ref (type $5) (param $0 (ref null $struct)) (result (ref null $struct)) + ;; HP2L2: (func $rmw-xchg-ref (type $3) (param $0 (ref null $struct)) (result (ref null $struct)) ;; HP2L2-NEXT: (local $1 (ref null $struct)) ;; HP2L2-NEXT: (local $2 (ref null $struct)) ;; HP2L2-NEXT: (local $3 (ref null $struct)) @@ -1086,12 +1167,40 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: (local.get $3) ;; CHECK-NEXT: ) - ;; HP2L2: (func $rmw-cmpxchg-ref (type $8) (param $0 (ref null $struct)) (param $1 (ref null $struct)) (result (ref null $struct)) - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2: (func $rmw-cmpxchg-ref (type $7) (param $0 (ref null $struct)) (param $1 (ref null $struct)) (result (ref null $struct)) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) + ;; HP2L2-NEXT: (local $3 (ref null $struct)) + ;; HP2L2-NEXT: (local $4 eqref) + ;; HP2L2-NEXT: (local $5 (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $4 ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $5 ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (if + ;; HP2L2-NEXT: (ref.eq + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: (local.get $4) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (then + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (local.get $5) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $3) ;; HP2L2-NEXT: ) (func $rmw-cmpxchg-ref (param (ref null $struct) (ref null $struct)) (result (ref null $struct)) (struct.atomic.rmw.cmpxchg $struct 0 @@ -1196,11 +1305,39 @@ ;; CHECK-NEXT: (local.get $1) ;; CHECK-NEXT: ) ;; HP2L2: (func $cmpxchg-acqrel (type $1) (result i32) - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg acqrel acqrel $i32 0 - ;; HP2L2-NEXT: (struct.new_default $i32) + ;; HP2L2-NEXT: (local $0 i32) + ;; HP2L2-NEXT: (local $1 i32) + ;; HP2L2-NEXT: (local $2 i32) + ;; HP2L2-NEXT: (local $3 i32) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $2 ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 ;; HP2L2-NEXT: (i32.const 2) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (if + ;; HP2L2-NEXT: (i32.eq + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (then + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) (func $cmpxchg-acqrel (result i32) (struct.atomic.rmw.cmpxchg acqrel acqrel $i32 0 @@ -1275,10 +1412,23 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; HP2L2: (func $cmpxchg-unreachable-expected (type $1) (result i32) - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $i32 0 - ;; HP2L2-NEXT: (struct.new_default $i32) + ;; HP2L2-NEXT: (local $0 i32) + ;; HP2L2-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (unreachable) - ;; HP2L2-NEXT: (i32.const 1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) (func $cmpxchg-unreachable-expected (result i32) @@ -1428,15 +1578,51 @@ ;; CHECK-NEXT: (local.get $4) ;; CHECK-NEXT: ) ;; HP2L2: (func $array-cmpxchg (type $1) (result i32) - ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $arr - ;; HP2L2-NEXT: (array.new_fixed $arr 2 - ;; HP2L2-NEXT: (i32.const 0) - ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: (local $0 i32) + ;; HP2L2-NEXT: (local $1 i32) + ;; HP2L2-NEXT: (local $2 i32) + ;; HP2L2-NEXT: (local $3 i32) + ;; HP2L2-NEXT: (local $4 i32) + ;; HP2L2-NEXT: (local $5 i32) + ;; HP2L2-NEXT: (local $6 i32) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $5 ;; HP2L2-NEXT: (i32.const 10) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $6 ;; HP2L2-NEXT: (i32.const 20) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $4 + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (if + ;; HP2L2-NEXT: (i32.eq + ;; HP2L2-NEXT: (local.get $0) + ;; HP2L2-NEXT: (local.get $5) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (then + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $6) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $4) ;; HP2L2-NEXT: ) (func $array-cmpxchg (result i32) ;; Array atomic cmpxchg on a non-escaping fixed-size array should be @@ -1462,7 +1648,7 @@ ;; CHECK-NEXT: (i32.const 1) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $array-rmw-nonconstant-index (type $9) (param $idx i32) (result i32) + ;; HP2L2: (func $array-rmw-nonconstant-index (type $8) (param $idx i32) (result i32) ;; HP2L2-NEXT: (array.atomic.rmw.add $arr ;; HP2L2-NEXT: (array.new_fixed $arr 2 ;; HP2L2-NEXT: (i32.const 0) @@ -1534,14 +1720,18 @@ ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; HP2L2: (func $array-cmpxchg-oob (type $1) (result i32) - ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $arr - ;; HP2L2-NEXT: (array.new_default $arr - ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (i32.const 10) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (i32.const 20) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) ;; HP2L2-NEXT: ) (func $array-cmpxchg-oob (result i32) ;; As above, but for cmpxchg with an out-of-bounds index. @@ -1558,6 +1748,8 @@ ;; CHECK: (func $array-rmw-nonzero-oob (type $9) ;; CHECK-NEXT: (local $0 i32) ;; CHECK-NEXT: (local $1 i32) + ;; CHECK-NEXT: (local $2 i32) + ;; CHECK-NEXT: (local $3 i32) ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (block ;; CHECK-NEXT: (drop @@ -1577,10 +1769,34 @@ ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $3 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 10) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 20) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $array-rmw-nonzero-oob (type $10) + ;; HP2L2: (func $array-rmw-nonzero-oob (type $9) ;; HP2L2-NEXT: (local $0 i32) ;; HP2L2-NEXT: (local $1 i32) + ;; HP2L2-NEXT: (local $2 i32) + ;; HP2L2-NEXT: (local $3 i32) ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (block ;; HP2L2-NEXT: (drop @@ -1600,9 +1816,31 @@ ;; HP2L2-NEXT: (unreachable) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (i32.const 10) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (i32.const 20) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) (func $array-rmw-nonzero-oob - ;; Out-of-bounds atomic RMW on a non-empty array allocation. + ;; Out-of-bounds atomic RMW and cmpxchg on a non-empty array allocation. (drop (array.atomic.rmw.add $arr (array.new_default $arr @@ -1612,7 +1850,17 @@ (i32.const 1) ) ) - ) + (drop + (array.atomic.rmw.cmpxchg $arr + (array.new_default $arr + (i32.const 2) + ) + (i32.const 2) + (i32.const 10) + (i32.const 20) + ) + ) + ) ;; CHECK: (func $rmw-other-and-unreachable (type $10) (param $i (ref null $i32)) (param $a (ref null $arr)) ;; CHECK-NEXT: (local $2 i32) @@ -1665,7 +1913,7 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $rmw-other-and-unreachable (type $11) (param $i (ref null $i32)) (param $a (ref null $arr)) + ;; HP2L2: (func $rmw-other-and-unreachable (type $10) (param $i (ref null $i32)) (param $a (ref null $arr)) ;; HP2L2-NEXT: (local $2 i32) ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (block (result nullref) @@ -1818,15 +2066,40 @@ ;; HP2L2: (type $2 (func)) ;; HP2L2: (func $cmpxchg-expected-first (type $2) + ;; HP2L2-NEXT: (local $0 (ref null $array)) + ;; HP2L2-NEXT: (local $1 i32) + ;; HP2L2-NEXT: (local $2 i32) + ;; HP2L2-NEXT: (local $3 i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 - ;; HP2L2-NEXT: (struct.new_default $struct) - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: (block (result (ref null $array)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 2) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $0) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -1906,17 +2179,34 @@ ;; HP2L2: (func $cmpxchg-expected-first (type $2) (result (ref $array)) ;; HP2L2-NEXT: (local $array (ref $array)) + ;; HP2L2-NEXT: (local $1 (ref null $array)) + ;; HP2L2-NEXT: (local $2 i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 - ;; HP2L2-NEXT: (struct.new_default $struct) - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: (block (result (ref null $array)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (local.tee $array - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 2) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (local.tee $array + ;; HP2L2-NEXT: (array.new_default $array + ;; HP2L2-NEXT: (i32.const 2) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (local.get $array) @@ -1944,16 +2234,10 @@ ;; CHECK: (rec ;; CHECK-NEXT: (type $inner (array i8)) - ;; HP2L2: (type $0 (func)) - - ;; HP2L2: (rec - ;; HP2L2-NEXT: (type $inner (array i8)) (type $inner (array i8)) ;; CHECK: (type $struct (struct (field (mut eqref)))) - ;; HP2L2: (type $struct (struct (field (mut eqref)))) (type $struct (struct (field (mut eqref)))) ;; CHECK: (type $array (array (mut eqref))) - ;; HP2L2: (type $array (array (mut eqref))) (type $array (array (field (mut eqref)))) ) ;; CHECK: (func $test-cmpxchg-scratch-oob (type $0) @@ -1995,18 +2279,37 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) + ;; HP2L2: (type $0 (func)) + ;; HP2L2: (func $test-cmpxchg-scratch-oob (type $0) ;; HP2L2-NEXT: (local $a arrayref) + ;; HP2L2-NEXT: (local $1 eqref) ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (block (result nullref) ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 - ;; HP2L2-NEXT: (struct.new_default $struct) - ;; HP2L2-NEXT: (array.new_fixed $inner 0) - ;; HP2L2-NEXT: (array.new_fixed $inner 0) + ;; HP2L2-NEXT: (block (result eqref) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (unreachable) @@ -2089,19 +2392,33 @@ ;; CHECK-NEXT: ) ;; HP2L2: (func $test-cmpxchg-scratch-oob-array (type $0) ;; HP2L2-NEXT: (local $a arrayref) + ;; HP2L2-NEXT: (local $1 eqref) ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (block (result nullref) ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $array - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: (block (result eqref) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (i32.const 0) - ;; HP2L2-NEXT: (array.new_fixed $inner 0) - ;; HP2L2-NEXT: (array.new_fixed $inner 0) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (unreachable) @@ -2203,13 +2520,32 @@ ;; HP2L2: (type $4 (func)) ;; HP2L2: (func $cmpxchg-non-nullable-field (type $4) + ;; HP2L2-NEXT: (local $0 (ref $inner)) + ;; HP2L2-NEXT: (local $1 (ref $inner)) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $outer 0 - ;; HP2L2-NEXT: (struct.new $outer - ;; HP2L2-NEXT: (struct.new_default $inner) + ;; HP2L2-NEXT: (block (result (ref $inner)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (struct.new_default $inner) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (struct.new_default $inner) - ;; HP2L2-NEXT: (struct.new_default $inner) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $0) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2282,13 +2618,32 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; HP2L2: (func $cmpxchg-non-nullable-field-shared (type $4) + ;; HP2L2-NEXT: (local $0 (ref $shared-inner)) + ;; HP2L2-NEXT: (local $1 (ref $shared-inner)) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $shared-outer 0 - ;; HP2L2-NEXT: (struct.new $shared-outer - ;; HP2L2-NEXT: (struct.new_default $shared-inner) + ;; HP2L2-NEXT: (block (result (ref $shared-inner)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (struct.new_default $shared-inner) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (struct.new_default $shared-inner) - ;; HP2L2-NEXT: (struct.new_default $shared-inner) + ;; HP2L2-NEXT: (local.get $0) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2361,17 +2716,37 @@ ;; HP2L2: (func $cmpxchg-ref-and-expected (type $1) (result (ref null $struct)) ;; HP2L2-NEXT: (local $local (ref $struct)) + ;; HP2L2-NEXT: (local $1 (ref null $struct)) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 - ;; HP2L2-NEXT: (local.tee $local - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (local.get $local) - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (struct.get $struct 0 - ;; HP2L2-NEXT: (local.get $local) + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) (func $cmpxchg-ref-and-expected (export "test") (result (ref null $struct)) @@ -2427,17 +2802,31 @@ ;; CHECK-NEXT: ) ;; HP2L2: (func $cmpxchg-ref-and-expected-non-null-replacement (type $2) (param $replacement (ref $struct)) (result (ref null $struct)) ;; HP2L2-NEXT: (local $local (ref $struct)) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 - ;; HP2L2-NEXT: (local.tee $local - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (local.get $replacement) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (local.get $local) - ;; HP2L2-NEXT: (local.get $replacement) + ;; HP2L2-NEXT: (local.get $2) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (struct.get $struct 0 - ;; HP2L2-NEXT: (local.get $local) + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $2) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) (func $cmpxchg-ref-and-expected-non-null-replacement (param $replacement (ref $struct)) (result (ref null $struct)) @@ -2462,13 +2851,9 @@ ) (module - ;; HP2L2: (type $struct (shared (struct (field (mut (ref null (shared array))))))) - - ;; HP2L2: (type $1 (func (param (ref $struct)))) - - ;; HP2L2: (type $array (shared (array i8))) (type $array (shared (array i8))) ;; CHECK: (type $struct (shared (struct (field (mut (ref null (shared array))))))) + ;; HP2L2: (type $struct (shared (struct (field (mut (ref null (shared array))))))) (type $struct (shared (struct (field (mut (ref null (shared array))))))) ;; CHECK: (type $1 (func (param (ref $struct)))) @@ -2484,11 +2869,15 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) + ;; HP2L2: (type $1 (func (param (ref $struct)))) + ;; HP2L2: (func $unreachable-shared-expected (type $1) (param $struct (ref $struct)) ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 ;; HP2L2-NEXT: (local.get $struct) - ;; HP2L2-NEXT: (array.new_fixed $array 0) + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (unreachable) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2514,6 +2903,7 @@ ;; CHECK: (type $array (array (mut eqref))) ;; HP2L2: (type $array (array (mut eqref))) (type $array (array (mut eqref))) + (type $struct (struct (field (mut eqref)))) ;; CHECK: (type $1 (func (param (ref $array)))) @@ -2554,14 +2944,32 @@ ;; HP2L2: (type $2 (func (param (ref eq)))) ;; HP2L2: (func $array-cmpxchg-expected (type $1) (param $array (ref $array)) + ;; HP2L2-NEXT: (local $1 eqref) + ;; HP2L2-NEXT: (local $2 (ref null $array)) + ;; HP2L2-NEXT: (local $3 i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $array - ;; HP2L2-NEXT: (local.get $array) - ;; HP2L2-NEXT: (i32.const 0) - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: (block (result eqref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (local.get $array) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (array.atomic.get $array + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: (local.get $3) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2621,25 +3029,39 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; HP2L2: (func $array-cmpxchg-expected-null-ref (type $2) (param $x (ref eq)) + ;; HP2L2-NEXT: (local $1 eqref) + ;; HP2L2-NEXT: (local $2 nullref) + ;; HP2L2-NEXT: (local $3 i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (block $block (result (ref eq)) - ;; HP2L2-NEXT: (br_on_cast_fail $block (ref eq) (ref eq) - ;; HP2L2-NEXT: (block ;; (replaces unreachable ArrayCmpxchg we can't emit) - ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block $block (result (ref none)) + ;; HP2L2-NEXT: (br_on_cast_fail $block (ref none) (ref none) + ;; HP2L2-NEXT: (block (result (ref none)) + ;; HP2L2-NEXT: (local.set $2 ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (local.set $3 ;; HP2L2-NEXT: (i32.const 0) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (local.get $x) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: (block ;; (replaces unreachable ArrayGet we can't emit) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2666,67 +3088,248 @@ ) (module + ;; CHECK: (type $struct (struct (field (mut eqref)))) + ;; CHECK: (type $array (array (mut eqref))) + ;; HP2L2: (type $struct (struct (field (mut eqref)))) + ;; HP2L2: (type $array (array (mut eqref))) (type $array (array (mut eqref))) - ;; CHECK: (type $1 (func (param (ref $array)))) + (type $struct (struct (field (mut eqref)))) + ;; CHECK: (type $2 (func (param (ref $array)))) - ;; CHECK: (type $2 (func (result i32))) + ;; CHECK: (type $3 (func (result i32))) - ;; CHECK: (type $3 (func (result eqref))) + ;; CHECK: (type $4 (func (result eqref))) - ;; CHECK: (import "" "" (func $effect-i32 (type $2) (result i32))) - ;; HP2L2: (type $1 (func (param (ref $array)))) + ;; CHECK: (type $5 (func (param (ref $struct) (ref $struct)) (result eqref))) + + ;; CHECK: (type $6 (func (param (ref $struct)))) + + ;; CHECK: (type $7 (func (param eqref) (result eqref))) + + ;; CHECK: (import "" "" (func $effect-i32 (type $3) (result i32))) + ;; HP2L2: (type $2 (func (param (ref $array)))) + + ;; HP2L2: (type $3 (func (result i32))) - ;; HP2L2: (type $2 (func (result i32))) + ;; HP2L2: (type $4 (func (result eqref))) - ;; HP2L2: (type $3 (func (result eqref))) + ;; HP2L2: (type $5 (func (param (ref $struct) (ref $struct)) (result eqref))) - ;; HP2L2: (import "" "" (func $effect-i32 (type $2) (result i32))) + ;; HP2L2: (type $6 (func (param (ref $struct)))) + + ;; HP2L2: (type $7 (func (param eqref) (result eqref))) + + ;; HP2L2: (import "" "" (func $effect-i32 (type $3) (result i32))) (import "" "" (func $effect-i32 (result i32))) - ;; CHECK: (import "" "" (func $effect-eq (type $3) (result eqref))) - ;; HP2L2: (import "" "" (func $effect-eq (type $3) (result eqref))) + ;; CHECK: (import "" "" (func $effect-eq (type $4) (result eqref))) + ;; HP2L2: (import "" "" (func $effect-eq (type $4) (result eqref))) (import "" "" (func $effect-eq (result eqref))) - ;; CHECK: (func $array-cmpxchg-expected-index-effect (type $1) (param $array (ref $array)) - ;; CHECK-NEXT: (local $1 eqref) - ;; CHECK-NEXT: (local $2 (ref null $array)) - ;; CHECK-NEXT: (local $3 i32) + ;; CHECK: (func $cmpxchg-expected-ref-effects (type $5) (param $s (ref $struct)) (param $other (ref $struct)) (result eqref) + ;; CHECK-NEXT: (local $2 eqref) + ;; CHECK-NEXT: (local $3 (ref null $struct)) + ;; CHECK-NEXT: (local.set $3 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: ) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block (result eqref) - ;; CHECK-NEXT: (local.set $2 - ;; CHECK-NEXT: (local.get $array) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (local.set $3 - ;; CHECK-NEXT: (call $effect-i32) + ;; CHECK-NEXT: (block (result (ref null $struct)) + ;; CHECK-NEXT: (local.set $s + ;; CHECK-NEXT: (local.get $other) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block (result nullref) - ;; CHECK-NEXT: (local.set $1 - ;; CHECK-NEXT: (ref.null none) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (local.set $2 ;; CHECK-NEXT: (ref.null none) ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null none) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (call $effect-eq) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (array.atomic.get $array + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (call $effect-eq) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (struct.atomic.get $struct 0 + ;; CHECK-NEXT: (local.get $3) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-expected-ref-effects (type $5) (param $s (ref $struct)) (param $other (ref $struct)) (result eqref) + ;; HP2L2-NEXT: (local $2 eqref) + ;; HP2L2-NEXT: (local $3 (ref null $struct)) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $s + ;; HP2L2-NEXT: (local.get $other) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (call $effect-eq) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $struct 0 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-expected-ref-effects (param $s (ref $struct)) (param $other (ref $struct)) (result eqref) + ;; Localizes `ref` before `expected` mutates `$s`, and preserves + ;; `replacement`'s call effect before emitting struct.atomic.get. + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (block (result (ref $struct)) + (local.set $s + (local.get $other) + ) + (struct.new_default $struct) + ) + (call $effect-eq) + ) + ) + + ;; CHECK: (func $cmpxchg-expected-trapping-preserve-effects (type $6) (param $s (ref $struct)) + ;; CHECK-NEXT: (local $1 eqref) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; CHECK-NEXT: (block (result (ref $struct)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (call $effect-i32) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (call $effect-eq) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-expected-trapping-preserve-effects (type $6) (param $s (ref $struct)) + ;; HP2L2-NEXT: (local $1 eqref) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result eqref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (block (result (ref $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (call $effect-i32) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (call $effect-eq) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $struct 0 + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-expected-trapping-preserve-effects (param $s (ref $struct)) + ;; Even when expected traps in a failing cast, preceding side effects in + ;; `ref` are preserved before the trap and trailing operands are dropped. + (drop + (struct.atomic.rmw.cmpxchg $struct 0 + (block (result (ref $struct)) + (drop + (call $effect-i32) + ) + (local.get $s) + ) + (ref.cast (ref $array) + (struct.new_default $struct) + ) + (call $effect-eq) + ) + ) + ) + + ;; CHECK: (func $array-cmpxchg-expected-index-effect (type $2) (param $array (ref $array)) + ;; CHECK-NEXT: (local $1 eqref) + ;; CHECK-NEXT: (local $2 (ref null $array)) + ;; CHECK-NEXT: (local $3 i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result eqref) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (local.get $array) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $3 + ;; CHECK-NEXT: (call $effect-i32) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (call $effect-eq) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (array.atomic.get $array ;; CHECK-NEXT: (local.get $2) ;; CHECK-NEXT: (local.get $3) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $array-cmpxchg-expected-index-effect (type $1) (param $array (ref $array)) + ;; HP2L2: (func $array-cmpxchg-expected-index-effect (type $2) (param $array (ref $array)) + ;; HP2L2-NEXT: (local $1 eqref) + ;; HP2L2-NEXT: (local $2 (ref null $array)) + ;; HP2L2-NEXT: (local $3 i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $array - ;; HP2L2-NEXT: (local.get $array) - ;; HP2L2-NEXT: (call $effect-i32) - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: (block (result eqref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (local.get $array) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (call $effect-i32) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (call $effect-eq) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (array.atomic.get $array + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: (local.get $3) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (call $effect-eq) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2743,7 +3346,7 @@ ) ) - ;; CHECK: (func $array-cmpxchg-expected-index-oob (type $1) (param $array (ref $array)) + ;; CHECK: (func $array-cmpxchg-expected-index-oob (type $2) (param $array (ref $array)) ;; CHECK-NEXT: (local $1 eqref) ;; CHECK-NEXT: (local $2 (ref null $array)) ;; CHECK-NEXT: (local $3 i32) @@ -2773,15 +3376,33 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $array-cmpxchg-expected-index-oob (type $1) (param $array (ref $array)) + ;; HP2L2: (func $array-cmpxchg-expected-index-oob (type $2) (param $array (ref $array)) + ;; HP2L2-NEXT: (local $1 eqref) + ;; HP2L2-NEXT: (local $2 (ref null $array)) + ;; HP2L2-NEXT: (local $3 i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $array - ;; HP2L2-NEXT: (local.get $array) - ;; HP2L2-NEXT: (i32.const -1) - ;; HP2L2-NEXT: (array.new_default $array - ;; HP2L2-NEXT: (i32.const 1) + ;; HP2L2-NEXT: (block (result eqref) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (local.get $array) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (i32.const -1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (call $effect-eq) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (array.atomic.get $array + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: (local.get $3) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (call $effect-eq) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2797,15 +3418,102 @@ ) ) ) + + ;; CHECK: (func $array-cmpxchg-ref-unoptimized-expected (type $7) (param $expected eqref) (result eqref) + ;; CHECK-NEXT: (local $1 eqref) + ;; CHECK-NEXT: (local $2 eqref) + ;; CHECK-NEXT: (local $3 eqref) + ;; CHECK-NEXT: (local $4 eqref) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $3 + ;; CHECK-NEXT: (local.get $expected) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $4 + ;; CHECK-NEXT: (call $effect-eq) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (if + ;; CHECK-NEXT: (ref.eq + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: (local.get $3) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (then + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (local.get $4) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $2) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $array-cmpxchg-ref-unoptimized-expected (type $7) (param $expected eqref) (result eqref) + ;; HP2L2-NEXT: (local $1 eqref) + ;; HP2L2-NEXT: (local $2 eqref) + ;; HP2L2-NEXT: (local $3 eqref) + ;; HP2L2-NEXT: (local $4 eqref) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result nullref) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null none) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (local.get $expected) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $4 + ;; HP2L2-NEXT: (call $effect-eq) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (if + ;; HP2L2-NEXT: (ref.eq + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (then + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $4) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + (func $array-cmpxchg-ref-unoptimized-expected (param $expected eqref) (result eqref) + ;; Reference-element array with unescaped array ref and unoptimized + ;; expected emits the eqref scratch local and ref.eq comparison. + (array.atomic.rmw.cmpxchg $array + (array.new_default $array + (i32.const 1) + ) + (i32.const 0) + (local.get $expected) + (call $effect-eq) + ) + ) ) (module ;; CHECK: (type $struct (shared (struct (field (mut (ref null $struct)))))) ;; HP2L2: (type $struct (shared (struct (field (mut (ref null $struct)))))) (type $struct (shared (struct (field (mut (ref null $struct)))))) - ;; CHECK: (type $1 (func (result i32))) + ;; CHECK: (type $1 (func (param (ref $struct)) (result (ref null $struct)))) + + ;; CHECK: (type $2 (func (param (ref $struct)))) + + ;; CHECK: (type $3 (func (result i32))) - ;; CHECK: (func $must-optimize-ref-eq (type $1) (result i32) + ;; CHECK: (func $must-optimize-ref-eq (type $3) (result i32) ;; CHECK-NEXT: (local $local (ref null $struct)) ;; CHECK-NEXT: (local $1 (ref null $struct)) ;; CHECK-NEXT: (local $2 (ref null $struct)) @@ -2864,22 +3572,52 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (type $1 (func (result i32))) + ;; HP2L2: (type $1 (func (param (ref $struct)) (result (ref null $struct)))) + + ;; HP2L2: (type $2 (func (param (ref $struct)))) - ;; HP2L2: (func $must-optimize-ref-eq (type $1) (result i32) + ;; HP2L2: (type $3 (func (result i32))) + + ;; HP2L2: (func $must-optimize-ref-eq (type $3) (result i32) ;; HP2L2-NEXT: (local $local (ref null $struct)) + ;; HP2L2-NEXT: (local $1 (ref null $struct)) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) + ;; HP2L2-NEXT: (local $3 (ref null $struct)) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg acqrel acqrel $struct 0 - ;; HP2L2-NEXT: (local.tee $local - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (struct.new_default $struct) - ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (ref.is_null - ;; HP2L2-NEXT: (struct.get $struct 0 - ;; HP2L2-NEXT: (local.get $local) + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $1) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) @@ -2914,251 +3652,1274 @@ (struct.get $struct 0 (local.get $local)) ) ) -) - -(module - (type $s0 (shared (struct (field (mut (ref null (shared eq))))))) - ;; CHECK: (type $s1 (shared (struct))) - - ;; CHECK: (type $1 (func)) - - ;; CHECK: (type $a0 (shared (array (mut (ref null (shared eq)))))) - ;; HP2L2: (type $s1 (shared (struct))) - - ;; HP2L2: (type $1 (func)) - - ;; HP2L2: (type $a0 (shared (array (mut (ref null (shared eq)))))) - (type $a0 (shared (array (mut (ref null (shared eq)))))) - (type $s1 (shared (struct))) - - ;; CHECK: (type $3 (func (param (ref $a0)))) - - ;; CHECK: (type $4 (func (param (ref null $s1)) (result (ref null (shared eq))))) - ;; CHECK: (func $cmpxchg-became-unreachable-ref (type $1) - ;; CHECK-NEXT: (local $x (ref null $s1)) + ;; CHECK: (func $cmpxchg-expected-and-replacement-unescaped (type $1) (param $s (ref $struct)) (result (ref null $struct)) + ;; CHECK-NEXT: (local $1 (ref null $struct)) + ;; CHECK-NEXT: (local $2 (ref null $struct)) + ;; CHECK-NEXT: (local $3 (ref null $struct)) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: ) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block (result (ref null (shared none))) - ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (local.set $3 + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (struct.atomic.get $struct 0 + ;; CHECK-NEXT: (local.get $2) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-expected-and-replacement-unescaped (type $1) (param $s (ref $struct)) (result (ref null $struct)) + ;; HP2L2-NEXT: (local $1 (ref null $struct)) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) + ;; HP2L2-NEXT: (local $3 (ref null $struct)) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $struct 0 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-expected-and-replacement-unescaped (param $s (ref $struct)) (result (ref null $struct)) + ;; Conditional cmpxchg escape: unescaped expected allows replacement to + ;; stay unescaped too when neither escapes elsewhere. + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (struct.new_default $struct) + (struct.new_default $struct) + ) + ) + + ;; CHECK: (func $cmpxchg-expected-escaped-escapes-replacement (type $1) (param $s (ref $struct)) (result (ref null $struct)) + ;; CHECK-NEXT: (local $exp (ref null $struct)) + ;; CHECK-NEXT: (local.set $exp + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (local.get $exp) + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $exp) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-expected-escaped-escapes-replacement (type $1) (param $s (ref $struct)) (result (ref null $struct)) + ;; HP2L2-NEXT: (local $exp (ref null $struct)) + ;; HP2L2-NEXT: (local.set $exp + ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: (local.get $exp) + ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $exp) + ;; HP2L2-NEXT: ) + (func $cmpxchg-expected-escaped-escapes-replacement (param $s (ref $struct)) (result (ref null $struct)) + (local $exp (ref null $struct)) + ;; Escaping expected forces replacement to escape too. + (local.set $exp + (struct.new_default $struct) + ) + (drop + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (local.get $exp) + (struct.new_default $struct) + ) + ) + (local.get $exp) + ) + + ;; CHECK: (func $cmpxchg-self-expected-replacement (type $2) (param $s (ref $struct)) + ;; CHECK-NEXT: (local $x (ref null $struct)) + ;; CHECK-NEXT: (local.set $x + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-self-expected-replacement (type $2) (param $s (ref $struct)) + ;; HP2L2-NEXT: (local $x (ref null $struct)) + ;; HP2L2-NEXT: (local $2 (ref null $struct)) + ;; HP2L2-NEXT: (local $3 (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $struct 0 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-self-expected-replacement (param $s (ref $struct)) + (local $x (ref null $struct)) + ;; Self-dependency in escape graph keeps $x unescaped. + (local.set $x + (struct.new_default $struct) + ) + (drop + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (local.get $x) + (local.get $x) + ) + ) + ) + + ;; CHECK: (func $cmpxchg-multi-hop-chain (type $1) (param $s (ref $struct)) (result (ref null $struct)) + ;; CHECK-NEXT: (local $a (ref null $struct)) + ;; CHECK-NEXT: (local $b (ref null $struct)) + ;; CHECK-NEXT: (local.set $a + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $b + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: (local.get $b) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (local.get $b) + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-multi-hop-chain (type $1) (param $s (ref $struct)) (result (ref null $struct)) + ;; HP2L2-NEXT: (local $a (ref null $struct)) + ;; HP2L2-NEXT: (local $b (ref null $struct)) + ;; HP2L2-NEXT: (local.set $a + ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $b + ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: (local.get $a) + ;; HP2L2-NEXT: (local.get $b) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: (local.get $b) + ;; HP2L2-NEXT: (struct.new_default $struct) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $a) + ;; HP2L2-NEXT: ) + (func $cmpxchg-multi-hop-chain (param $s (ref $struct)) (result (ref null $struct)) + (local $a (ref null $struct)) + (local $b (ref null $struct)) + ;; Transitive escape chain: A -> B -> C where A escapes via return. + (local.set $a + (struct.new_default $struct) + ) + (local.set $b + (struct.new_default $struct) + ) + (drop + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (local.get $a) + (local.get $b) + ) + ) + (drop + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (local.get $b) + (struct.new_default $struct) + ) + ) + (local.get $a) + ) + + ;; CHECK: (func $cmpxchg-mutual-cycle (type $2) (param $s (ref $struct)) + ;; CHECK-NEXT: (local $a (ref null $struct)) + ;; CHECK-NEXT: (local $b (ref null $struct)) + ;; CHECK-NEXT: (local.set $a + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $b + ;; CHECK-NEXT: (struct.new_default $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: (local.get $b) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $struct 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (local.get $b) + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-mutual-cycle (type $2) (param $s (ref $struct)) + ;; HP2L2-NEXT: (local $a (ref null $struct)) + ;; HP2L2-NEXT: (local $b (ref null $struct)) + ;; HP2L2-NEXT: (local $3 (ref null $struct)) + ;; HP2L2-NEXT: (local $4 (ref null $struct)) + ;; HP2L2-NEXT: (local $5 (ref null $struct)) + ;; HP2L2-NEXT: (local $6 (ref null $struct)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $4 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (local.set $5 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $struct 0 + ;; HP2L2-NEXT: (local.get $5) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null $struct)) + ;; HP2L2-NEXT: (local.set $6 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $struct 0 + ;; HP2L2-NEXT: (local.get $6) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-mutual-cycle (param $s (ref $struct)) + (local $a (ref null $struct)) + (local $b (ref null $struct)) + ;; Mutual escape cycle A <-> B where neither escapes elsewhere optimizes + ;; both allocations cleanly. + (local.set $a + (struct.new_default $struct) + ) + (local.set $b + (struct.new_default $struct) + ) + (drop + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (local.get $a) + (local.get $b) + ) + ) + (drop + (struct.atomic.rmw.cmpxchg $struct 0 + (local.get $s) + (local.get $b) + (local.get $a) + ) + ) + ) +) + +(module + ;; CHECK: (type $s1 (shared (struct))) + + ;; CHECK: (type $s0 (shared (struct (field (mut (ref null (shared eq))))))) + ;; HP2L2: (type $s1 (shared (struct))) + + ;; HP2L2: (type $s0 (shared (struct (field (mut (ref null (shared eq))))))) + (type $s0 (shared (struct (field (mut (ref null (shared eq))))))) + ;; CHECK: (type $a0 (shared (array (mut (ref null (shared eq)))))) + ;; HP2L2: (type $a0 (shared (array (mut (ref null (shared eq)))))) + (type $a0 (shared (array (mut (ref null (shared eq)))))) + (type $s1 (shared (struct))) + + ;; CHECK: (type $3 (func (param (ref $a0)))) + + ;; CHECK: (type $4 (func (param (ref $s0)))) + + ;; CHECK: (type $5 (func)) + + ;; CHECK: (type $6 (func (param (ref null $s1)) (result (ref null (shared eq))))) + + ;; CHECK: (func $cmpxchg-became-unreachable-ref (type $5) + ;; CHECK-NEXT: (local $x (ref null $s1)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (type $3 (func (param (ref $a0)))) + + ;; HP2L2: (type $4 (func (param (ref $s0)))) + + ;; HP2L2: (type $5 (func)) + + ;; HP2L2: (type $6 (func (param (ref null $s1)) (result (ref null (shared eq))))) + + ;; HP2L2: (func $cmpxchg-became-unreachable-ref (type $5) + ;; HP2L2-NEXT: (local $x (ref null $s1)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-became-unreachable-ref + ;; The allocation flows into both a failing cast in `ref` and into + ;; `expected`. During the post-order rewrite, `ref` is replaced with a + ;; trapping block before the cmpxchg is visited, so the cmpxchg is left for + ;; refinalization once its `ref` becomes unreachable. + (local $x (ref null $s1)) + (drop + (struct.atomic.rmw.cmpxchg $s0 0 + (ref.cast (ref $s0) + (local.tee $x + (struct.new_default $s1) + ) + ) + (local.get $x) + (ref.null (shared none)) + ) + ) + ) + + ;; CHECK: (func $array-cmpxchg-became-unreachable-ref (type $5) + ;; CHECK-NEXT: (local $x (ref null $s1)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block ;; (replaces unreachable ArrayCmpxchg we can't emit) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $array-cmpxchg-became-unreachable-ref (type $5) + ;; HP2L2-NEXT: (local $x (ref null $s1)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block ;; (replaces unreachable ArrayCmpxchg we can't emit) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $array-cmpxchg-became-unreachable-ref + ;; Same as above, for array.atomic.rmw.cmpxchg. + (local $x (ref null $s1)) + (drop + (array.atomic.rmw.cmpxchg $a0 + (ref.cast (ref $a0) + (local.tee $x + (struct.new_default $s1) + ) + ) + (i32.const 0) + (local.get $x) + (ref.null (shared none)) + ) + ) + ) + + ;; CHECK: (func $array-cmpxchg-unreachable (type $3) (param $a (ref $a0)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (array.atomic.rmw.cmpxchg $a0 + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $array-cmpxchg-unreachable (type $3) (param $a (ref $a0)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $a0 + ;; HP2L2-NEXT: (local.get $a) + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $array-cmpxchg-unreachable (param $a (ref $a0)) + ;; An unreachable array.atomic.rmw.cmpxchg is left for DCE. + (drop + (array.atomic.rmw.cmpxchg $a0 + (local.get $a) + (i32.const 0) + (struct.new_default $s1) + (unreachable) + ) + ) + ) + + ;; CHECK: (func $array-rmw-xchg-ref (type $6) (param $r (ref null $s1)) (result (ref null (shared eq))) + ;; CHECK-NEXT: (local $1 (ref null (shared eq))) + ;; CHECK-NEXT: (local $2 (ref null (shared eq))) + ;; CHECK-NEXT: (local $3 (ref null (shared eq))) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $3 + ;; CHECK-NEXT: (local.get $r) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (local.get $3) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $2) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $array-rmw-xchg-ref (type $6) (param $r (ref null $s1)) (result (ref null (shared eq))) + ;; HP2L2-NEXT: (local $1 (ref null (shared eq))) + ;; HP2L2-NEXT: (local $2 (ref null (shared eq))) + ;; HP2L2-NEXT: (local $3 (ref null (shared eq))) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $3 + ;; HP2L2-NEXT: (local.get $r) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $3) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + (func $array-rmw-xchg-ref (param $r (ref null $s1)) (result (ref null (shared eq))) + ;; Non-escaping ref array with ref atomic RMW xchg. + (array.atomic.rmw.xchg $a0 + (array.new_default $a0 + (i32.const 1) + ) + (i32.const 0) + (local.get $r) + ) + ) + + ;; CHECK: (func $cmpxchg-unknown-replacement (type $4) (param $s (ref $s0)) + ;; CHECK-NEXT: (local $1 (ref null $s0)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared eq))) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (struct.atomic.get $s0 0 + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-unknown-replacement (type $4) (param $s (ref $s0)) + ;; HP2L2-NEXT: (local $1 (ref null $s0)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared eq))) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $s0 0 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-unknown-replacement (param $s (ref $s0)) + ;; Candidate expected operand with unknown null replacement on + ;; struct.atomic.rmw.cmpxchg. + (drop + (struct.atomic.rmw.cmpxchg $s0 0 + (local.get $s) + (struct.new_default $s1) + (ref.null (shared none)) + ) + ) + ) + + ;; CHECK: (func $cmpxchg-unknown-expected (type $4) (param $s (ref $s0)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $s0 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: (struct.new_default $s1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-unknown-expected (type $4) (param $s (ref $s0)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $s0 0 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: (struct.new_default $s1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-unknown-expected (param $s (ref $s0)) + ;; Unknown null expected operand forces candidate replacement to escape + ;; while an unrelated non-escaping allocation is still optimized. + (drop + (struct.new_default $s1) + ) + (drop + (struct.atomic.rmw.cmpxchg $s0 0 + (local.get $s) + (ref.null (shared eq)) + (struct.new_default $s1) + ) + ) + ) + + ;; CHECK: (func $array-cmpxchg-unknown-replacement (type $3) (param $a (ref $a0)) + ;; CHECK-NEXT: (local $1 (ref null $a0)) + ;; CHECK-NEXT: (local $2 i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared eq))) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (array.atomic.get $a0 + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: (local.get $2) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $array-cmpxchg-unknown-replacement (type $3) (param $a (ref $a0)) + ;; HP2L2-NEXT: (local $1 (ref null $a0)) + ;; HP2L2-NEXT: (local $2 i32) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared eq))) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $a) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $2 + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (array.atomic.get $a0 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: (local.get $2) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $array-cmpxchg-unknown-replacement (param $a (ref $a0)) + ;; Candidate expected operand with unknown null replacement on + ;; array.atomic.rmw.cmpxchg. + (drop + (array.atomic.rmw.cmpxchg $a0 + (local.get $a) + (i32.const 0) + (struct.new_default $s1) + (ref.null (shared none)) + ) + ) + ) + + ;; CHECK: (func $array-cmpxchg-unknown-expected (type $3) (param $a (ref $a0)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (array.atomic.rmw.cmpxchg $a0 + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: (struct.new_default $s1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $array-cmpxchg-unknown-expected (type $3) (param $a (ref $a0)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $a0 + ;; HP2L2-NEXT: (local.get $a) + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: (struct.new_default $s1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $array-cmpxchg-unknown-expected (param $a (ref $a0)) + ;; Unknown null expected operand on array.atomic.rmw.cmpxchg forces + ;; replacement to escape. + (drop + (array.atomic.rmw.cmpxchg $a0 + (local.get $a) + (i32.const 0) + (ref.null (shared eq)) + (struct.new_default $s1) + ) + ) + ) + + ;; CHECK: (func $cmpxchg-mixed-expected (type $4) (param $s (ref $s0)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $s0 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (select (result (ref null (exact $s1))) + ;; CHECK-NEXT: (struct.new_default $s1) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (struct.new_default $s1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-mixed-expected (type $4) (param $s (ref $s0)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $s0 0 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: (select (result (ref null (exact $s1))) + ;; HP2L2-NEXT: (struct.new_default $s1) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.new_default $s1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-mixed-expected (param $s (ref $s0)) + ;; Merging a candidate allocation with null in expected forces replacement + ;; to escape. + (drop + (struct.atomic.rmw.cmpxchg $s0 0 + (local.get $s) + (select (result (ref null (shared eq))) + (struct.new_default $s1) + (ref.null (shared eq)) + (i32.const 0) + ) + (struct.new_default $s1) + ) + ) + ) +) + +(module + (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $outer (shared (struct (field (mut (ref $inner)))))) + ;; HP2L2: (rec + ;; HP2L2-NEXT: (type $outer (shared (struct (field (mut (ref $inner)))))) + (type $outer (shared (struct (field (mut (ref $inner)))))) + ;; CHECK: (type $inner (shared (struct))) + ;; HP2L2: (type $inner (shared (struct))) + (type $inner (shared (struct))) + ;; CHECK: (type $other (shared (struct))) + ;; HP2L2: (type $other (shared (struct))) + (type $other (shared (struct))) + ) + + ;; CHECK: (type $arr (shared (array (mut (ref $inner))))) + ;; HP2L2: (type $arr (shared (array (mut (ref $inner))))) + (type $arr (shared (array (mut (ref $inner))))) + + + ;; CHECK: (type $4 (func (param (ref $outer)))) + + ;; CHECK: (type $5 (func)) + + ;; CHECK: (type $6 (func (param (ref $arr)))) + + ;; CHECK: (type $7 (func (param (ref $arr) i32))) + + ;; CHECK: (type $8 (func (result (ref $inner)))) + + ;; CHECK: (func $cmpxchg-trapping-expected-nonnull-replacement (type $4) (param $s (ref $outer)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $outer 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (struct.new_default $inner) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (type $4 (func (param (ref $outer)))) + + ;; HP2L2: (type $5 (func)) + + ;; HP2L2: (type $6 (func (param (ref $arr)))) + + ;; HP2L2: (type $7 (func (param (ref $arr) i32))) + + ;; HP2L2: (type $8 (func (result (ref $inner)))) + + ;; HP2L2: (func $cmpxchg-trapping-expected-nonnull-replacement (type $4) (param $s (ref $outer)) + ;; HP2L2-NEXT: (local $1 (ref null $outer)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref $inner)) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (struct.atomic.get $outer 0 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-trapping-expected-nonnull-replacement (param $s (ref $outer)) + ;; When expected traps in a failing cast, struct.atomic.rmw.cmpxchg never + ;; executes, so even a non-nullable replacement cannot be written to the + ;; heap and can be optimized away. + (drop + (struct.atomic.rmw.cmpxchg $outer 0 + (local.get $s) + (ref.cast (ref null $inner) + (struct.new_default $other) + ) + (struct.new_default $inner) + ) + ) + ) + + ;; CHECK: (func $cmpxchg-alloc-trapping-expected-nonnull-replacement (type $5) + ;; CHECK-NEXT: (local $0 (ref $inner)) + ;; CHECK-NEXT: (local $1 (ref $inner)) + ;; CHECK-NEXT: (local $2 (ref $inner)) + ;; CHECK-NEXT: (local $3 (ref null (shared eq))) + ;; CHECK-NEXT: (local $4 (ref null $inner)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref $inner)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (struct.new_default $inner) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $0 + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $3 + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (local.get $0) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: (if + ;; CHECK-NEXT: (ref.eq + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: (local.get $3) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (then + ;; CHECK-NEXT: (local.set $0 + ;; CHECK-NEXT: (ref.as_non_null + ;; CHECK-NEXT: (local.get $4) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $2) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (type $3 (func (param (ref $a0)))) - - ;; HP2L2: (type $4 (func (param (ref null $s1)) (result (ref null (shared eq))))) - - ;; HP2L2: (func $cmpxchg-became-unreachable-ref (type $1) - ;; HP2L2-NEXT: (local $x (ref null $s1)) + ;; HP2L2: (func $cmpxchg-alloc-trapping-expected-nonnull-replacement (type $5) + ;; HP2L2-NEXT: (local $0 (ref $inner)) + ;; HP2L2-NEXT: (local $1 (ref $inner)) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) + ;; HP2L2-NEXT: (block (result (ref $inner)) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (ref.cast (ref (shared none)) - ;; HP2L2-NEXT: (local.tee $x - ;; HP2L2-NEXT: (struct.new_default $s1) + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (struct.new_default $inner) ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (local.set $0 + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (ref.null (shared none)) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (local.get $x) + ;; HP2L2-NEXT: (block + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: (local.get $0) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - (func $cmpxchg-became-unreachable-ref - ;; The allocation flows into both a failing cast in `ref` and into - ;; `expected`. During the post-order rewrite, `ref` is replaced with a - ;; trapping block before the cmpxchg is visited, so the cmpxchg is left for - ;; refinalization once its `ref` becomes unreachable. - (local $x (ref null $s1)) + (func $cmpxchg-alloc-trapping-expected-nonnull-replacement + ;; Same when the struct reference itself is a non-escaping allocation. (drop - (struct.atomic.rmw.cmpxchg $s0 0 - (ref.cast (ref $s0) - (local.tee $x - (struct.new_default $s1) - ) + (struct.atomic.rmw.cmpxchg $outer 0 + (struct.new $outer + (struct.new_default $inner) ) - (local.get $x) - (ref.null (shared none)) + (ref.cast (ref null $inner) + (struct.new_default $other) + ) + (struct.new_default $inner) ) ) ) - ;; CHECK: (func $array-cmpxchg-became-unreachable-ref (type $1) - ;; CHECK-NEXT: (local $x (ref null $s1)) + ;; CHECK: (func $array-cmpxchg-trapping-expected-nonnull-replacement (type $6) (param $a (ref $arr)) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block ;; (replaces unreachable ArrayCmpxchg we can't emit) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block (result (ref null (shared none))) - ;; CHECK-NEXT: (ref.null (shared none)) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (array.atomic.rmw.cmpxchg $arr + ;; CHECK-NEXT: (local.get $a) + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref null (shared none))) + ;; CHECK-NEXT: (ref.null (shared none)) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (i32.const 0) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (ref.null (shared none)) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (ref.null (shared none)) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: (struct.new_default $inner) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $array-cmpxchg-became-unreachable-ref (type $1) - ;; HP2L2-NEXT: (local $x (ref null $s1)) + ;; HP2L2: (func $array-cmpxchg-trapping-expected-nonnull-replacement (type $6) (param $a (ref $arr)) + ;; HP2L2-NEXT: (local $1 (ref null $arr)) + ;; HP2L2-NEXT: (local $2 i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (block ;; (replaces unreachable ArrayCmpxchg we can't emit) - ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (ref.cast (ref (shared none)) - ;; HP2L2-NEXT: (local.tee $x - ;; HP2L2-NEXT: (struct.new_default $s1) - ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (block (result (ref $inner)) + ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (local.get $a) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (local.set $2 ;; HP2L2-NEXT: (i32.const 0) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (local.get $x) + ;; HP2L2-NEXT: (block + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: (block (result (ref null (shared none))) + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (array.atomic.get $arr + ;; HP2L2-NEXT: (local.get $1) + ;; HP2L2-NEXT: (local.get $2) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (unreachable) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - (func $array-cmpxchg-became-unreachable-ref - ;; Same as above, for array.atomic.rmw.cmpxchg. - (local $x (ref null $s1)) + (func $array-cmpxchg-trapping-expected-nonnull-replacement (param $a (ref $arr)) + ;; Same for array.atomic.rmw.cmpxchg. (drop - (array.atomic.rmw.cmpxchg $a0 - (ref.cast (ref $a0) - (local.tee $x - (struct.new_default $s1) - ) - ) + (array.atomic.rmw.cmpxchg $arr + (local.get $a) (i32.const 0) - (local.get $x) - (ref.null (shared none)) + (ref.cast (ref null $inner) + (struct.new_default $other) + ) + (struct.new_default $inner) ) ) ) - ;; CHECK: (func $array-cmpxchg-unreachable (type $3) (param $a (ref $a0)) + ;; CHECK: (func $cmpxchg-unreachable-ref-replacement (type $4) (param $s (ref $outer)) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (array.atomic.rmw.cmpxchg $a0 + ;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg $outer 0 + ;; CHECK-NEXT: (local.get $s) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: (struct.new_default $inner) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $cmpxchg-unreachable-ref-replacement (type $4) (param $s (ref $outer)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.atomic.rmw.cmpxchg $outer 0 + ;; HP2L2-NEXT: (local.get $s) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: (struct.new_default $inner) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $cmpxchg-unreachable-ref-replacement (param $s (ref $outer)) + ;; Optimizer skips unreachable struct.atomic.rmw.cmpxchg ops, and + ;; validation still checks replacement against a reachable ref's + ;; non-nullable field type, so replacement must not be optimized to null. + (drop + (struct.atomic.rmw.cmpxchg $outer 0 + (local.get $s) + (unreachable) + (struct.new_default $inner) + ) + ) + ) + + ;; CHECK: (func $array-cmpxchg-unreachable-ref-replacement (type $7) (param $a (ref $arr)) (param $i i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (array.atomic.rmw.cmpxchg $arr ;; CHECK-NEXT: (local.get $a) - ;; CHECK-NEXT: (i32.const 0) - ;; CHECK-NEXT: (block (result (ref null (shared none))) - ;; CHECK-NEXT: (ref.null (shared none)) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $i) ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: (struct.new_default $inner) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; HP2L2: (func $array-cmpxchg-unreachable (type $3) (param $a (ref $a0)) + ;; HP2L2: (func $array-cmpxchg-unreachable-ref-replacement (type $7) (param $a (ref $arr)) (param $i i32) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $a0 + ;; HP2L2-NEXT: (array.atomic.rmw.cmpxchg $arr ;; HP2L2-NEXT: (local.get $a) - ;; HP2L2-NEXT: (i32.const 0) - ;; HP2L2-NEXT: (struct.new_default $s1) + ;; HP2L2-NEXT: (local.get $i) ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: (struct.new_default $inner) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - (func $array-cmpxchg-unreachable (param $a (ref $a0)) - ;; An unreachable array.atomic.rmw.cmpxchg is left for DCE. + (func $array-cmpxchg-unreachable-ref-replacement (param $a (ref $arr)) (param $i i32) + ;; Optimizer skips unreachable array.atomic.rmw.cmpxchg ops, and + ;; validation still checks replacement against a reachable ref's + ;; non-nullable element type, so replacement must not be optimized to null. (drop - (array.atomic.rmw.cmpxchg $a0 + (array.atomic.rmw.cmpxchg $arr (local.get $a) - (i32.const 0) - (struct.new_default $s1) + (local.get $i) (unreachable) + (struct.new_default $inner) ) ) ) - ;; CHECK: (func $array-rmw-xchg-ref (type $4) (param $r (ref null $s1)) (result (ref null (shared eq))) - ;; CHECK-NEXT: (local $1 (ref null (shared eq))) - ;; CHECK-NEXT: (local $2 (ref null (shared eq))) - ;; CHECK-NEXT: (local $3 (ref null (shared eq))) + ;; CHECK: (func $cmpxchg-bottom-ref-i32-expected (type $5) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block (result (ref null (shared none))) - ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) + ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (ref.null (shared none)) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.new_default $inner) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (local.set $3 - ;; CHECK-NEXT: (local.get $r) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (local.set $2 - ;; CHECK-NEXT: (local.get $1) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (local.set $1 - ;; CHECK-NEXT: (local.get $3) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (local.get $2) ;; CHECK-NEXT: ) - ;; HP2L2: (func $array-rmw-xchg-ref (type $4) (param $r (ref null $s1)) (result (ref null (shared eq))) - ;; HP2L2-NEXT: (local $1 (ref null (shared eq))) - ;; HP2L2-NEXT: (local $2 (ref null (shared eq))) - ;; HP2L2-NEXT: (local $3 (ref null (shared eq))) + ;; HP2L2: (func $cmpxchg-bottom-ref-i32-expected (type $5) ;; HP2L2-NEXT: (drop - ;; HP2L2-NEXT: (block (result (ref null (shared none))) - ;; HP2L2-NEXT: (local.set $1 + ;; HP2L2-NEXT: (block ;; (replaces unreachable StructCmpxchg we can't emit) + ;; HP2L2-NEXT: (drop ;; HP2L2-NEXT: (ref.null (shared none)) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.new_default $inner) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) ;; HP2L2-NEXT: ) ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (local.set $3 - ;; HP2L2-NEXT: (local.get $r) - ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (local.set $2 - ;; HP2L2-NEXT: (local.get $1) - ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (local.set $1 - ;; HP2L2-NEXT: (local.get $3) - ;; HP2L2-NEXT: ) - ;; HP2L2-NEXT: (local.get $2) ;; HP2L2-NEXT: ) - (func $array-rmw-xchg-ref (param $r (ref null $s1)) (result (ref null (shared eq))) - ;; Non-escaping ref array with ref atomic RMW xchg. - (array.atomic.rmw.xchg $a0 - (array.new_default $a0 - (i32.const 1) + (func $cmpxchg-bottom-ref-i32-expected + ;; A bottom nullref in ref makes struct.atomic.rmw.cmpxchg unreachable and + ;; lets validation accept a non-reference expected operand; replacement + ;; stays unoptimized. + (drop + (struct.atomic.rmw.cmpxchg $outer 0 + (ref.null (shared none)) + (i32.const 0) + (struct.new_default $inner) ) + ) + ) + + ;; CHECK: (func $array-cmpxchg-bottom-ref-i32-expected (type $8) (result (ref $inner)) + ;; CHECK-NEXT: (block ;; (replaces unreachable ArrayCmpxchg we can't emit) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.null (shared none)) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.new_default $inner) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; HP2L2: (func $array-cmpxchg-bottom-ref-i32-expected (type $8) (result (ref $inner)) + ;; HP2L2-NEXT: (block ;; (replaces unreachable ArrayCmpxchg we can't emit) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (ref.null (shared none)) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (i32.const 0) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (drop + ;; HP2L2-NEXT: (struct.new_default $inner) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: (unreachable) + ;; HP2L2-NEXT: ) + ;; HP2L2-NEXT: ) + (func $array-cmpxchg-bottom-ref-i32-expected (result (ref $inner)) + ;; Same for array.atomic.rmw.cmpxchg, where finalize() takes the result + ;; type from replacement. + (array.atomic.rmw.cmpxchg $arr + (ref.null (shared none)) (i32.const 0) - (local.get $r) + (i32.const 0) + (struct.new_default $inner) ) ) )