Skip to content
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
9 changes: 9 additions & 0 deletions src/passes/Unsubtyping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <memory>

#include "ir/effects.h"
#include "ir/eh-utils.h"
#include "ir/js-utils.h"
#include "ir/localize.h"
#include "ir/module-utils.h"
Expand Down Expand Up @@ -1073,6 +1074,7 @@ struct Unsubtyping : Pass, Noter<Unsubtyping> {
// collecting and iterating over all the types, though.
struct Rewriter : WalkerPass<PostWalker<Rewriter>> {
const TypeTree& types;
bool needEHFixups = false;

// Allocations that might trap that have been removed from module-level
// initializers. These need to be placed in new globals to preserve any
Expand Down Expand Up @@ -1114,6 +1116,7 @@ struct Unsubtyping : Pass, Noter<Unsubtyping> {
block->list.push_back(curr);
block->type = curr->type;
replaceCurrent(block);
needEHFixups = true;
} else {
// We are dropping this descriptor, but it might have a potential trap
// nested inside it. In that case we need to preserve the trap by
Expand All @@ -1125,6 +1128,12 @@ struct Unsubtyping : Pass, Noter<Unsubtyping> {
}
curr->desc = nullptr;
}

void visitFunction(Function* curr) {
if (needEHFixups) {
EHUtils::handleBlockNestedPops(curr, *getModule());
}
}
};

Rewriter rewriter(types);
Expand Down
52 changes: 52 additions & 0 deletions test/lit/passes/unsubtyping-desc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1240,3 +1240,55 @@
)
)
)

;; Removing a descriptor from a struct.new_desc uses ChildLocalizer, which wraps
;; the allocation in a block. Ensure any nested `pop` inside a catch block is
;; hoisted out of the new block so the IR remains valid.
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $A (struct))
(type $A (descriptor $A.desc) (struct))
;; CHECK: (type $A.desc (struct))
(type $A.desc (describes $A) (struct))
)
;; CHECK: (type $2 (func (param (ref (exact $A.desc)))))

;; CHECK: (type $3 (func))

;; CHECK: (tag $e (type $2) (param (ref (exact $A.desc))))
(tag $e (param (ref (exact $A.desc))))
;; CHECK: (func $pop-in-struct-new-desc (type $3)
;; CHECK-NEXT: (local $0 (ref (exact $A.desc)))
;; CHECK-NEXT: (local $1 (ref (exact $A.desc)))
;; CHECK-NEXT: (try
;; CHECK-NEXT: (do
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $e
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (pop (ref (exact $A.desc)))
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result (ref (exact $A)))
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.new_default $A)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $pop-in-struct-new-desc
(try
(do)
(catch $e
(drop
(struct.new_desc $A
(pop (ref (exact $A.desc)))
)
)
)
)
)
)
Loading