diff --git a/src/passes/Heap2Local.cpp b/src/passes/Heap2Local.cpp index ba4f3409164..7f8381e2a6c 100644 --- a/src/passes/Heap2Local.cpp +++ b/src/passes/Heap2Local.cpp @@ -661,6 +661,11 @@ struct Struct2Local : PostWalker { return; } + if (curr->type == Type::unreachable) { + // We ended up with unreachable code, and have nothing to do. + return; + } + // Our allocation passes through this expr. We must turn its type into a // nullable one, because we will remove things like RefAsNonNull of it, // which means we may no longer have a non-nullable value as our input, diff --git a/test/lit/passes/heap2local.wast b/test/lit/passes/heap2local.wast index ec1e06a4a1d..11d9659db5f 100644 --- a/test/lit/passes/heap2local.wast +++ b/test/lit/passes/heap2local.wast @@ -4808,3 +4808,59 @@ ) ) ) + +(module + (type $array (array (mut i32))) + + ;; CHECK: (type $0 (func)) + + ;; CHECK: (func $unreachable-flow (type $0) + ;; CHECK-NEXT: (local $0 i32) + ;; CHECK-NEXT: (local $1 i32) + ;; CHECK-NEXT: (local $2 i32) + ;; CHECK-NEXT: (local $3 i32) + ;; CHECK-NEXT: (local $4 i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block $block + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result nullref) + ;; CHECK-NEXT: (local.set $0 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; 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: (local.set $4 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (ref.null none) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (return) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $unreachable-flow + ;; The return flows into the br_if. This should not cause errors as we + ;; optimize out the allocation. + (drop + (block $block (result arrayref) + (br_if $block + (array.new_default $array + (i32.const 5) + ) + (return) + ) + ) + ) + ) +) +