Skip to content

Commit

Permalink
[Wasm GC] ReFinalize when needed in SimplifyGlobals (#5682)
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken authored Apr 20, 2023
1 parent b2be621 commit 2cd0f40
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/passes/SimplifyGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ struct ConstantGlobalApplier
: public WalkerPass<
LinearExecutionWalker<ConstantGlobalApplier,
UnifiedExpressionVisitor<ConstantGlobalApplier>>> {
using super = WalkerPass<
LinearExecutionWalker<ConstantGlobalApplier,
UnifiedExpressionVisitor<ConstantGlobalApplier>>>;

bool isFunctionParallel() override { return true; }

ConstantGlobalApplier(NameSet* constantGlobals, bool optimize)
Expand All @@ -337,6 +341,16 @@ struct ConstantGlobalApplier
return std::make_unique<ConstantGlobalApplier>(constantGlobals, optimize);
}

bool refinalize = false;

void replaceCurrent(Expression* rep) {
if (rep->type != getCurrent()->type) {
// This operation will change the type, so refinalize.
refinalize = true;
}
super::replaceCurrent(rep);
}

void visitExpression(Expression* curr) {
if (auto* set = curr->dynCast<GlobalSet>()) {
if (Properties::isConstantExpression(set->value)) {
Expand Down Expand Up @@ -386,10 +400,15 @@ struct ConstantGlobalApplier
}

void visitFunction(Function* curr) {
if (replaced && optimize) {
PassRunner runner(getPassRunner());
runner.addDefaultFunctionOptimizationPasses();
runner.runOnFunction(curr);
if (replaced) {
if (refinalize) {
ReFinalize().walkFunctionInModule(curr, this->getModule());
}
if (optimize) {
PassRunner runner(getPassRunner());
runner.addDefaultFunctionOptimizationPasses();
runner.runOnFunction(curr);
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions test/lit/passes/simplify-globals-gc.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up.

;; RUN: foreach %s %t wasm-opt --simplify-globals -all -S -o - | filecheck %s

(module
;; CHECK: (type $A (func))
(type $A (func))

;; CHECK: (global $global$0 funcref (ref.func $func))
(global $global$0 (mut funcref) (ref.func $func))

;; CHECK: (elem declare func $func)

;; CHECK: (func $func (type $A)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast $A
;; CHECK-NEXT: (ref.func $func)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $func
(drop
(ref.cast null $A
;; This global can be replaced with a ref.func of $func. That has a more
;; refined type, so we should refinalize (if we do not, then the ref.cast
;; will fail to validate as it must become a non-nullable cast).
(global.get $global$0)
)
)
)
)

0 comments on commit 2cd0f40

Please sign in to comment.