Skip to content
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
6 changes: 3 additions & 3 deletions src/passes/OptimizeCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,16 @@ struct EarlyCastFinder

void visitExpression(Expression* curr) {
// A new one is instantiated for each expression to determine
// if a cast can be moved past it.
// if a cast can be moved backward past it.
ShallowEffectAnalyzer currAnalyzer(options, *getModule(), curr);

if (testRefCast.invalidates(currAnalyzer)) {
if (currAnalyzer.orderedBefore(testRefCast)) {
for (size_t i = 0; i < numLocals; i++) {
flushRefCastResult(i, *getModule());
}
}

if (testRefAs.invalidates(currAnalyzer)) {
if (currAnalyzer.orderedBefore(testRefAs)) {
for (size_t i = 0; i < numLocals; i++) {
flushRefAsResult(i, *getModule());
}
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/merge-blocks-atomics.wast
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

;; CHECK: (func $disallowed (type $1) (param $x (ref $struct))
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: (block $label1 (result i32)
;; CHECK-NEXT: (block $block (result i32)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a fix to unrelated tests that were breaking CI. I landed this separately in #8777.

;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.atomic.load acqrel
;; CHECK-NEXT: (i32.const 0)
Expand Down Expand Up @@ -55,7 +55,7 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: (block $label2 (result i32)
;; CHECK-NEXT: (block $block (result i32)
;; CHECK-NEXT: (i32.atomic.store acqrel
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (i32.const 42)
Expand Down
149 changes: 149 additions & 0 deletions test/lit/passes/optimize-casts-order.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
;; RUN: wasm-opt %s --optimize-casts -all -S -o - | filecheck %s

(module
;; CHECK: (type $A (sub (struct)))
(type $A (sub (struct)))
;; CHECK: (type $B (sub $A (struct)))
(type $B (sub $A (struct)))

(memory 1)

;; CHECK: (global $g (mut i32) (i32.const 0))
(global $g (mut i32) (i32.const 0))

;; CHECK: (func $cast_allowed (type $2) (param $x (ref null $A))
;; CHECK-NEXT: (local $temp (ref null $A))
;; CHECK-NEXT: (local $2 (ref null $B))
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $2
;; CHECK-NEXT: (ref.cast (ref null $B)
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.load
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast (ref null $B)
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cast_allowed (param $x (ref null $A))
;; ref.cast can move back past load.
(local $temp (ref null $A))
(local.set $temp (local.get $x))
(block
(drop (local.get $temp))
(drop (i32.load (i32.const 0)))
(drop (ref.cast (ref null $B) (local.get $temp)))
)
)

;; CHECK: (func $cast_disallowed (type $2) (param $x (ref null $A))
;; CHECK-NEXT: (local $temp (ref null $A))
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.cast (ref null $B)
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $cast_disallowed (param $x (ref null $A))
;; ref.cast cannot move back past a store.
(local $temp (ref null $A))
(local.set $temp (local.get $x))
(block
(drop (local.get $temp))
(i32.store (i32.const 0) (i32.const 42))
(drop (ref.cast (ref null $B) (local.get $temp)))
)
)

;; Test 3: ref.as positive (moves past mutable global read)
;; CHECK: (func $as_allowed (type $3) (param $x anyref)
;; CHECK-NEXT: (local $temp anyref)
;; CHECK-NEXT: (local $2 (ref any))
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.tee $2
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (global.get $g)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $as_allowed (param $x anyref)
;; ref.as_non_null can move back past a global.get.
(local $temp anyref)
(local.set $temp (local.get $x))
(block
(drop (local.get $temp))
(drop (global.get $g))
(drop (ref.as_non_null (local.get $temp)))
)
)

;; Test 4: ref.as negative (blocked by mutable global write)
;; CHECK: (func $as_disallowed (type $3) (param $x anyref)
;; CHECK-NEXT: (local $temp anyref)
;; CHECK-NEXT: (local.set $temp
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $g
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $as_disallowed (param $x anyref)
;; ref.as_non_null cannot move back past a global.set.
(local $temp anyref)
(local.set $temp (local.get $x))
(block
(drop (local.get $temp))
(global.set $g (i32.const 42))
(drop (ref.as_non_null (local.get $temp)))
)
)
)
Loading