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

Push struct.new down to make it more likely that heap stores can be optimized. #6584

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 52 additions & 3 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1882,17 +1882,33 @@ struct OptimizeInstructions

// This local.set of a struct.new looks good. Find struct.sets after it
// to optimize.
for (Index j = i + 1; j < list.size(); j++) {
Index localSetIndex = i;
for (Index j = localSetIndex + 1; j < list.size(); j++) {
auto* structSet = list[j]->dynCast<StructSet>();
// Any time the pattern no longer matches, we try to push the
// struct.new further down but if it is not possible we stop
// optimizing possible struct.sets for this struct.new.

if (!structSet) {
// Any time the pattern no longer matches, stop optimizing possible
// struct.sets for this struct.new.
if (trySwap(list, localSetIndex, j)) {
// Update the index an continue to try again.
localSetIndex = j;
continue;
}
break;
}

auto* localGet = structSet->ref->dynCast<LocalGet>();
if (!localGet || localGet->index != localSet->index) {
if (trySwap(list, localSetIndex, j)) {
// Update the index an continue to try again.
localSetIndex = j;
continue;
}
break;
}

// The pattern matches, try to optimize.
if (!optimizeSubsequentStructSet(new_, structSet, localGet->index)) {
break;
} else {
Expand All @@ -1904,6 +1920,39 @@ struct OptimizeInstructions
}
}

// Tries pushing the struct.new down so that it is closer
// to a potential struct.set.
bool trySwap(ExpressionList& list, Index i, Index j) {
if (j == list.size() - 1) {
// There is no reason to swap with the last element
// of the list as it won't match the pattern.
return false;
}

if (list[j]->is<LocalSet>() &&
list[j]->dynCast<LocalSet>()->value->is<StructNew>()) {
// Don't swap two struct.new instructions to avoid going back and forth.
return false;
}
// Check if the local is referencenced by the instruction we want to
// swap it with,
auto* localSet = list[i]->dynCast<LocalSet>();
auto otherEffects = effects(list[j]);
if (otherEffects.localsRead.count(localSet->index) ||
otherEffects.localsWritten.count(localSet->index)) {
return false;
}
// or if the effects don't permit moving one past the other.
auto structNewEffects = effects(localSet->value);
if (otherEffects.invalidates(structNewEffects)) {
return false;
}

list[i] = list[j];
list[j] = localSet;
return true;
}

// Given a struct.new and a struct.set that occurs right after it, and that
// applies to the same data, try to apply the set during the new. This can be
// either with a nested tee:
Expand Down
29 changes: 15 additions & 14 deletions test/lit/passes/optimize-instructions-gc-heap.wast
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --remove-unused-names --optimize-instructions -all -S -o - \
;; RUN: wasm-opt %s --remove-unused-names --optimize-instructions -all -S -o - \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unneeded space?

Suggested change
;; RUN: wasm-opt %s --remove-unused-names --optimize-instructions -all -S -o - \
;; RUN: wasm-opt %s --remove-unused-names --optimize-instructions -all -S -o - \

;; RUN: | filecheck %s
;;
;; --remove-unused-names allows the optimizer to see that the blocks have no
Expand All @@ -9,11 +9,10 @@
;; CHECK: (type $struct (struct (field (mut i32))))
(type $struct (struct (field (mut i32))))

;; CHECK: (type $struct3 (struct (field (mut i32)) (field (mut i32)) (field (mut i32))))

;; CHECK: (type $struct2 (struct (field (mut i32)) (field (mut i32))))
(type $struct2 (struct (field (mut i32)) (field (mut i32))))

;; CHECK: (type $struct3 (struct (field (mut i32)) (field (mut i32)) (field (mut i32))))
(type $struct3 (struct (field (mut i32)) (field (mut i32)) (field (mut i32))))

;; CHECK: (func $tee (type $1)
Expand Down Expand Up @@ -272,26 +271,31 @@

;; CHECK: (func $pattern-breaker (type $1)
;; CHECK-NEXT: (local $ref (ref null $struct))
;; CHECK-NEXT: (local $ref2 (ref null $struct))
;; CHECK-NEXT: (local.set $ref
;; CHECK-NEXT: (struct.new $struct
;; CHECK-NEXT: (i32.const 10)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (local.set $ref2
;; CHECK-NEXT: (local.get $ref)
;; CHECK-NEXT: )
;; CHECK-NEXT: (struct.set $struct 0
;; CHECK-NEXT: (local.get $ref)
;; CHECK-NEXT: (i32.const 20)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $pattern-breaker
(local $ref (ref null $struct))
(local $ref2 (ref null $struct))
(local.set $ref
(struct.new $struct
(i32.const 10)
)
)
;; Anything that we don't recognize breaks the pattern.
(nop)
;; Any instruction that can not be swapped and is not
;; the expected struct.set breaks the pattern.
(local.set $ref2 (local.get $ref))
(struct.set $struct 0
(local.get $ref)
(i32.const 20)
Expand Down Expand Up @@ -611,20 +615,17 @@
;; CHECK: (func $many-news (type $1)
;; CHECK-NEXT: (local $ref (ref null $struct3))
;; CHECK-NEXT: (local $ref2 (ref null $struct3))
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (local.set $ref
;; CHECK-NEXT: (struct.new $struct3
;; CHECK-NEXT: (i32.const 40)
;; CHECK-NEXT: (i32.const 50)
;; CHECK-NEXT: (i32.const 30)
;; CHECK-NEXT: (i32.const 60)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (struct.set $struct3 2
;; CHECK-NEXT: (local.get $ref)
;; CHECK-NEXT: (i32.const 60)
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (local.set $ref
;; CHECK-NEXT: (struct.new $struct3
;; CHECK-NEXT: (i32.const 400)
Expand Down
Loading