diff options
-rw-r--r-- | src/passes/Heap2Local.cpp | 14 | ||||
-rw-r--r-- | test/lit/passes/heap2local.wast | 43 |
2 files changed, 57 insertions, 0 deletions
diff --git a/src/passes/Heap2Local.cpp b/src/passes/Heap2Local.cpp index e027f5a46..f0f691299 100644 --- a/src/passes/Heap2Local.cpp +++ b/src/passes/Heap2Local.cpp @@ -440,6 +440,20 @@ struct Heap2LocalOptimizer { // the allocation is a subtype of the type of the cast, and so // cannot trap. replaceCurrent(curr->ref); + + // We need to refinalize after this, as while we know the cast is not + // logically needed - the value flowing through will not be used - we do + // need validation to succeed even before other optimizations remove the + // code. For example: + // + // (block (result $B) + // (ref.cast $B + // (block (result $A) + // + // Without the cast this does not validate, so we need to refinalize + // (which will fix this, as we replace the unused value with a null, so + // that type will propagate out). + refinalize = true; } void visitStructSet(StructSet* curr) { diff --git a/test/lit/passes/heap2local.wast b/test/lit/passes/heap2local.wast index b814c4987..04c0936c6 100644 --- a/test/lit/passes/heap2local.wast +++ b/test/lit/passes/heap2local.wast @@ -2035,3 +2035,46 @@ ) ) ) + +(module + (type $A (sub (struct (field (mut i32))))) + (type $B (sub $A (struct (field (mut i32))))) + + ;; CHECK: (func $func (type $0) + ;; CHECK-NEXT: (local $0 i32) + ;; CHECK-NEXT: (local $1 i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $0 + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $func + ;; We can replace the allocation with a local for the i32. While doing so we + ;; must be careful to still validate, and so when we remove the cast we must + ;; also ensure the blocks around it have types that still validate (using + ;; refinalize, which will make them all nullref, since the unused value + ;; flowing through them will be replaced with a null). + (drop + (block (result (ref $B)) + (ref.cast (ref $B) + (block (result (ref $A)) + (struct.new $B + (i32.const 0) + ) + ) + ) + ) + ) + ) +) |