Skip to content

[EH][GC] Fix nested pop after removing ref.cast #4407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <ir/bits.h>
#include <ir/cost.h>
#include <ir/effects.h>
#include <ir/eh-utils.h>
#include <ir/find_all.h>
#include <ir/gc-type-utils.h>
#include <ir/iteration.h>
Expand Down Expand Up @@ -229,6 +230,9 @@ struct OptimizeInstructions
// Some patterns create locals (like when we use getResultOfFirst), which we
// may need to fix up.
TypeUpdating::handleNonDefaultableLocals(func, *getModule());
// Some patterns create blocks that can interfere 'catch' and 'pop', nesting
// the 'pop' into a block making it invalid.
EHUtils::handleBlockNestedPops(func, *getModule());
}

// Set to true when one of the visitors makes a change (either replacing the
Expand Down
58 changes: 58 additions & 0 deletions test/lit/passes/optimize-instructions-iit-eh.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --ignore-implicit-traps --optimize-instructions -all -S -o - \
;; RUN: | filecheck %s

(module
;; CHECK: (type $struct.A (struct (field i32)))
(type $struct.A (struct i32))
;; CHECK: (global $g-struct.A (rtt $struct.A) (rtt.canon $struct.A))

;; CHECK: (tag $e (param (ref null $struct.A)))
(tag $e (param (ref null $struct.A)))
(global $g-struct.A (rtt $struct.A) (rtt.canon $struct.A))

;; CHECK: (func $ref-cast-statically-removed
;; CHECK-NEXT: (local $0 (ref null $struct.A))
;; CHECK-NEXT: (local $1 (ref null $struct.A))
;; CHECK-NEXT: (try $try
;; CHECK-NEXT: (do
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $e
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (pop (ref null $struct.A))
;; CHECK-NEXT: )
;; CHECK-NEXT: (throw $e
;; CHECK-NEXT: (block (result (ref null $struct.A))
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $g-struct.A)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $ref-cast-statically-removed
(try
(do)
(catch $e
(throw $e
;; Because --ignore-implicit-traps is given, this ref.cast is assumed
;; not to throw so this ref.cast can be statically removed. But that
;; creates a block around this, making 'pop' nested into it, which is
;; invalid. We fix this up at the end up OptimizeInstruction,
;; assigning the 'pop' to a local at the start of this 'catch' body
;; and later using 'local.get' to get it.
(ref.cast
(pop (ref null $struct.A))
(global.get $g-struct.A)
)
)
)
)
)
)