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

GTO: Remove minor optimization of avoiding ChildLocalizer sometimes #6818

Merged
merged 3 commits into from
Aug 7, 2024
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
28 changes: 7 additions & 21 deletions src/passes/GlobalTypeOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
// * Fields that are never read from can be removed entirely.
//

#include "ir/effects.h"
#include "ir/localize.h"
#include "ir/ordering.h"
#include "ir/struct-utils.h"
Expand Down Expand Up @@ -348,26 +347,11 @@ struct GlobalTypeOptimization : public Pass {
auto& operands = curr->operands;
assert(indexesAfterRemoval.size() == operands.size());

// Check for side effects in removed fields. If there are any, we must
// use locals to save the values (while keeping them in order).
bool useLocals = false;
for (Index i = 0; i < operands.size(); i++) {
auto newIndex = indexesAfterRemoval[i];
if (newIndex == RemovedField &&
EffectAnalyzer(getPassOptions(), *getModule(), operands[i])
.hasUnremovableSideEffects()) {
useLocals = true;
break;
}
}
if (useLocals) {
auto* func = getFunction();
if (!func) {
Fatal() << "TODO: side effects in removed fields in globals\n";
}
ChildLocalizer localizer(curr, func, *getModule(), getPassOptions());
replaceCurrent(localizer.getReplacement());
}
// Localize things so that we can simply remove the operands we no
// longer need.
ChildLocalizer localizer(
curr, getFunction(), *getModule(), getPassOptions());
replaceCurrent(localizer.getReplacement());

// Remove the unneeded operands.
Index removed = 0;
Expand All @@ -381,6 +365,8 @@ struct GlobalTypeOptimization : public Pass {
}
}
operands.resize(operands.size() - removed);
// We should only get here if we did actual work.
assert(removed > 0);
}

void visitStructSet(StructSet* curr) {
Expand Down
16 changes: 11 additions & 5 deletions test/lit/passes/gto-removals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -589,17 +589,23 @@
)

;; CHECK: (func $new-side-effect-in-kept (type $3) (param $any (ref any))
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (struct.new $struct
;; CHECK-NEXT: (call $helper0
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (block (result (ref $struct))
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (call $helper0
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.new $struct
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $new-side-effect-in-kept (param $any (ref any))
;; Side effects appear in fields that we do *not* remove. In that case,
;; we do not need to use locals.
;; Side effects appear in fields that we do *not* remove. We do not need to
;; use locals here, but for simplicity we do, and rely on later opts.
(drop
(struct.new $struct
(call $helper0 (i32.const 0))
Expand Down
Loading