2222#include < ir/branch-hints.h>
2323#include < ir/drop.h>
2424#include < ir/effects.h>
25+ #include < ir/eh-utils.h>
2526#include < ir/find_all.h>
2627#include < ir/intrinsics.h>
2728#include < ir/iteration.h>
@@ -38,8 +39,18 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> {
3839
3940 std::unique_ptr<Pass> create () override { return std::make_unique<Vacuum>(); }
4041
42+ // Track whether we need to fix up pops at the end: adding a block in a Try
43+ // can require that.
44+ bool hasTry = false ;
45+ bool addedBlocks = false ;
46+
4147 void doWalkFunction (Function* func) {
4248 walk (func->body );
49+
50+ if (hasTry && addedBlocks) {
51+ EHUtils::handleBlockNestedPops (func, *getModule ());
52+ }
53+
4354 ReFinalize ().walkFunctionInModule (func, getModule ());
4455 }
4556
@@ -122,6 +133,7 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> {
122133 if (curr->type .isDefaultable ()) {
123134 auto * dummy = Builder (*getModule ())
124135 .makeConstantExpression (Literal::makeZeros (curr->type ));
136+ addedBlocks = true ;
125137 return getDroppedChildrenAndAppend (
126138 curr, *getModule (), getPassOptions (), dummy);
127139 }
@@ -438,6 +450,8 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> {
438450 }
439451
440452 void visitTry (Try* curr) {
453+ hasTry = true ;
454+
441455 // If try's body does not throw, the whole try-catch can be replaced with
442456 // the try's body.
443457 if (!EffectAnalyzer (getPassOptions (), *getModule (), curr->body ).throws ()) {
0 commit comments