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

add aggressive-memory-offset-merging pass to merge offset and previous const #6972

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 41 additions & 6 deletions src/passes/OptimizeAddedConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <ir/local-graph.h>
#include <ir/local-utils.h>
#include <pass.h>
#include <utility>
#include <wasm-builder.h>
#include <wasm.h>

Expand Down Expand Up @@ -79,11 +80,18 @@ template<typename P, typename T> class MemoryAccessOptimizer {
optimizeConstantPointer();
return false;
}
if (auto* add = curr->ptr->template dynCast<Binary>()) {
if (add->op == AddInt32 || add->op == AddInt64) {
if (auto* binary = curr->ptr->template dynCast<Binary>()) {
if (binary->op == AddInt32 || binary->op == AddInt64) {
// Look for a constant on both sides.
if (tryToOptimizeConstant(add->right, add->left) ||
tryToOptimizeConstant(add->left, add->right)) {
if (tryToOptimizeConstantToOffset(binary->left, binary->right)) {
return false;
}
if (tryToOptimizeOffsetToConstant(binary->left, binary->right)) {
return false;
}
}
if (binary->op == SubInt32 || binary->op == SubInt64) {
if (tryToOptimizeOffsetToConstant(binary->left, binary->right)) {
return false;
}
}
Expand Down Expand Up @@ -176,11 +184,22 @@ template<typename P, typename T> class MemoryAccessOptimizer {
Result(Address total) : succeeded(true), total(total) {}
};

static std::pair<Const*, Expression*> getConst(Expression* a, Expression* b) {
if (auto* constA = a->dynCast<Const>()) {
return {constA, b};
} else if (auto* constB = b->dynCast<Const>()) {
return {constB, a};
} else {
return {nullptr, nullptr};
}
}

// See if we can optimize an offset from an expression. If we report
// success, the returned offset can be added as a replacement for the
// expression here.
bool tryToOptimizeConstant(Expression* oneSide, Expression* otherSide) {
if (auto* c = oneSide->dynCast<Const>()) {
bool tryToOptimizeConstantToOffset(Expression* left, Expression* right) {
auto [c, otherSide] = getConst(left, right);
if (c != nullptr) {
auto result = canOptimizeConstant(c->value);
if (result.succeeded) {
curr->offset = result.total;
Expand All @@ -194,6 +213,22 @@ template<typename P, typename T> class MemoryAccessOptimizer {
return false;
}

bool tryToOptimizeOffsetToConstant(Expression* left, Expression* right) {
// left '-' right
auto [c, other] = getConst(left, right);
if (c != nullptr) {
if (curr->offset < PassOptions::LowMemoryBound) {
uint64_t value = c->value.getInteger();
uint64_t total = curr->offset + value;
curr->offset = 0;
c->value =
Literal::makeFromInt64(static_cast<int64_t>(total), c->value.type);
return true;
}
}
return false;
}

bool tryToOptimizePropagatedAdd(Expression* oneSide,
Expression* otherSide,
LocalGet* ptr,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
;; 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 --optimize-added-constants --low-memory-unused -S -o - | filecheck %s

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

;; CHECK: (memory $0 1 1)
(memory $0 1 1)
;; CHECK: (func $load_const
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.const 110)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $load_const
(drop (i32.load offset=100 (i32.const 10)))
)
;; CHECK: (func $store_const
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.const 110)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $store_const
(local i32)
(drop (i32.load offset=100 (i32.const 10)))
)
;; CHECK: (func $load_add
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (i32.const 1100)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 1200)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $load_add
(local i32)
(drop (i32.load offset=1000 (i32.add (i32.const 100) (local.get 0))))
(drop (i32.load offset=1000 (i32.add (local.get 0) (i32.const 200))))
)
;; CHECK: (func $store_add
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (i32.store offset=110
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.store offset=120
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $store_add
(local i32)
(i32.store offset=100 (i32.add (i32.const 10) (local.get 0)) (local.get 0))
(i32.store offset=100 (i32.add (local.get 0) (i32.const 20)) (local.get 0))
)
;; CHECK: (func $load_sub
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (i32.const 110)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 120)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $load_sub
(local i32)
(drop (i32.load offset=100 (i32.sub (i32.const 10) (local.get 0))))
(drop (i32.load offset=100 (i32.sub (local.get 0) (i32.const 20))))
)
;; CHECK: (func $store_sub
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (i32.const 110)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i32.const 120)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $store_sub
(local i32)
(i32.store offset=100 (i32.sub (i32.const 10) (local.get 0)) (local.get 0))
(i32.store offset=100 (i32.sub (local.get 0) (i32.const 20)) (local.get 0))
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,32 @@
(local.get $0)
(local.get $0)
)
(i32.store offset=2
(i32.store
(i32.add
(i32.const -11)
(i32.const -9)
(local.get $0)
)
(local.get $0)
)
(i32.store offset=2
(i32.store
(i32.add
(local.get $0)
(i32.const -13)
(i32.const -11)
)
(local.get $0)
)
(i32.store offset=19
(i32.const -15)
(i32.store
(i32.add
(i32.const -13)
(i32.const 17)
)
(local.get $0)
)
(i32.store offset=21
(i32.const -21)
(i32.store
(i32.add
(i32.const -19)
(i32.const 19)
)
(local.get $0)
)
(i32.store
Expand Down
22 changes: 14 additions & 8 deletions test/passes/optimize-added-constants_low-memory-unused.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,32 @@
(local.get $0)
(local.get $0)
)
(i32.store offset=2
(i32.store
(i32.add
(i32.const -11)
(i32.const -9)
(local.get $0)
)
(local.get $0)
)
(i32.store offset=2
(i32.store
(i32.add
(local.get $0)
(i32.const -13)
(i32.const -11)
)
(local.get $0)
)
(i32.store offset=19
(i32.const -15)
(i32.store
(i32.add
(i32.const -13)
(i32.const 17)
)
(local.get $0)
)
(i32.store offset=21
(i32.const -21)
(i32.store
(i32.add
(i32.const -19)
(i32.const 19)
)
(local.get $0)
)
(i32.store
Expand Down
Loading