Skip to content

Commit

Permalink
[EH] Fix nested pops when GTO removes a GC operation (#7242)
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken authored Jan 24, 2025
1 parent bb876a5 commit d555a06
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/passes/GlobalTypeOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// * Fields that are never read from can be removed entirely.
//

#include "ir/eh-utils.h"
#include "ir/localize.h"
#include "ir/module-utils.h"
#include "ir/ordering.h"
Expand Down Expand Up @@ -453,6 +454,8 @@ struct GlobalTypeOptimization : public Pass {
return std::make_unique<FieldRemover>(parent);
}

bool needEHFixups = false;

void visitStructNew(StructNew* curr) {
if (curr->type == Type::unreachable) {
return;
Expand All @@ -476,6 +479,8 @@ struct GlobalTypeOptimization : public Pass {
ChildLocalizer localizer(
curr, getFunction(), *getModule(), getPassOptions());
replaceCurrent(localizer.getReplacement());
// Adding a block here requires EH fixups.
needEHFixups = true;

// Remove and reorder operands.
Index removed = 0;
Expand Down Expand Up @@ -519,6 +524,7 @@ struct GlobalTypeOptimization : public Pass {
getFunction(),
getModule(),
getPassOptions());
needEHFixups = true;
Expression* replacement =
builder.makeDrop(builder.makeRefAs(RefAsNonNull, flipped));
if (curr->order == MemoryOrder::SeqCst) {
Expand All @@ -545,6 +551,12 @@ struct GlobalTypeOptimization : public Pass {
curr->index = newIndex;
}

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

private:
Index getNewIndex(HeapType type, Index index) {
auto iter = parent.indexesAfterRemovals.find(type);
Expand Down
52 changes: 52 additions & 0 deletions test/lit/passes/gto-removals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,55 @@
)
)
)

;; A struct with a pop, which requires EH fixups to avoid popping in a nested
;; block.
(module
(type $i32 (func (param i32)))

;; CHECK: (rec
;; CHECK-NEXT: (type $struct (struct))
(type $struct (struct (field (mut i32))))

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

;; CHECK: (type $i32 (func (param i32)))

;; CHECK: (tag $tag (type $i32) (param i32))
(tag $tag (type $i32) (param i32))

;; CHECK: (func $func (type $1)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (try
;; CHECK-NEXT: (do
;; CHECK-NEXT: )
;; CHECK-NEXT: (catch $tag
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (pop i32)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result (ref $struct))
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $func
(try
(do
)
(catch $tag
(drop
(struct.new $struct
(pop i32)
)
)
)
)
)
)

0 comments on commit d555a06

Please sign in to comment.