Skip to content

Extend getMaxbits to handle Block type #7590

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

Open
wants to merge 6 commits 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
6 changes: 6 additions & 0 deletions src/ir/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ Index getMaxBits(Expression* curr,
if (LoadUtils::isSignRelevant(load) && !load->signed_) {
return 8 * load->bytes;
}
} else if (auto* block = curr->dynCast<Block>()) {
// TODO: getFallthrough(block, ..., ...) is needed for localset.
if (localInfoProvider && !block->name.is() && block->list.size() > 0 &&
block->type.isConcrete()) {
return getMaxBits(block->list.back(), localInfoProvider);
}
}
switch (curr->type.getBasic()) {
case Type::i32:
Expand Down
13 changes: 13 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,19 @@ struct OptimizeInstructions

Index getMaxBitsForLocal(LocalGet* get) {
// check what we know about the local
if (localInfo.size() <= get->index) {
// we don't know anything about this local
switch (get->type.getBasic()) {
case Type::i32:
return 32;
case Type::i64:
return 64;
case Type::unreachable:
return 64; // not interesting, but don't crash
default:
WASM_UNREACHABLE("invalid type");
}
}
return localInfo[get->index].maxBits;
}

Expand Down
104 changes: 98 additions & 6 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -8412,36 +8412,128 @@
(i32.const 0)
)
)
;; CHECK: (func $andZero (param $0 i32) (result i32)
;; CHECK: (func $andZero (param $0 i32) (param $1 i64) (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $0
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $1
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call $andZero
;; CHECK-NEXT: (i32.const 1234)
;; CHECK-NEXT: (local.tee $0
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $1
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $andZero (param $0 i32) (result i32)
(func $andZero (param $0 i32) (param $1 i64) (result i32)
(drop
(i32.and
(local.get $0)
(i32.const 0)
)
)
(drop
(i64.and
(local.get $1)
(i64.const 0)
)
)
;; side effects, we must keep this, but
;; can drop it.
(drop
(i32.and
(call $andZero (i32.const 1234)) ;; side effects, we must keep this, but
;; can drop it.
(local.tee $0
(i32.const 1)
)
(i32.const 0)
)
)
(drop
(i64.and
(local.tee $1
(i64.const 1)
)
(i64.const 0)
)
)
(drop
(i32.and
(local.tee $0
(i32.const 1)
)
(block (result i32)
(local.set $0
(i32.const 1)
)
(i32.const 0)
)
)
)
(drop
(i64.and
(local.tee $1
(i64.const 1)
)
(block (result i64)
(local.set $1
(i64.const 1)
)
(i64.const 0)
)
)
)
(unreachable)
)
;; CHECK: (func $abstract-additions (param $x32 i32) (param $x64 i64) (param $y32 f32) (param $y64 f64)
Expand Down
Loading