2222// * Fields that are never read from can be removed entirely.
2323//
2424
25+ #include " ir/eh-utils.h"
2526#include " ir/localize.h"
2627#include " ir/module-utils.h"
2728#include " ir/ordering.h"
@@ -453,6 +454,8 @@ struct GlobalTypeOptimization : public Pass {
453454 return std::make_unique<FieldRemover>(parent);
454455 }
455456
457+ bool needEHFixups = false ;
458+
456459 void visitStructNew (StructNew* curr) {
457460 if (curr->type == Type::unreachable) {
458461 return ;
@@ -476,6 +479,8 @@ struct GlobalTypeOptimization : public Pass {
476479 ChildLocalizer localizer (
477480 curr, getFunction (), *getModule (), getPassOptions ());
478481 replaceCurrent (localizer.getReplacement ());
482+ // Adding a block here requires EH fixups.
483+ needEHFixups = true ;
479484
480485 // Remove and reorder operands.
481486 Index removed = 0 ;
@@ -519,6 +524,7 @@ struct GlobalTypeOptimization : public Pass {
519524 getFunction (),
520525 getModule (),
521526 getPassOptions ());
527+ needEHFixups = true ;
522528 Expression* replacement =
523529 builder.makeDrop (builder.makeRefAs (RefAsNonNull, flipped));
524530 if (curr->order == MemoryOrder::SeqCst) {
@@ -545,6 +551,12 @@ struct GlobalTypeOptimization : public Pass {
545551 curr->index = newIndex;
546552 }
547553
554+ void visitFunction (Function* curr) {
555+ if (needEHFixups) {
556+ EHUtils::handleBlockNestedPops (curr, *getModule ());
557+ }
558+ }
559+
548560 private:
549561 Index getNewIndex (HeapType type, Index index) {
550562 auto iter = parent.indexesAfterRemovals .find (type);
0 commit comments