Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wasm GC] ReFinalize when needed in SimplifyGlobals #5682

Merged
merged 3 commits into from
Apr 20, 2023
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
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)
)
)
)
)