Skip to content
Prev Previous commit
Next Next commit
test indirect and ref return calls
  • Loading branch information
tlively committed Apr 5, 2024
commit 1074d48cb411ea8ae8f0eb9b1d0004cbb7c685a7
185 changes: 149 additions & 36 deletions test/lit/passes/global-effects.wast
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,36 @@
;; RUN: foreach %s %t wasm-opt -all --generate-global-effects --discard-global-effects --vacuum -S -o - | filecheck %s --check-prefix WITHOUT

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

;; WITHOUT: (type $void (func))
;; INCLUDE: (type $void (func))
(type $void (func))

;; WITHOUT: (type $1 (func (result i32)))

;; WITHOUT: (type $2 (func (param i32)))

;; WITHOUT: (import "a" "b" (func $import (type $0)))
;; INCLUDE: (type $0 (func))

;; WITHOUT: (import "a" "b" (func $import (type $void)))
;; INCLUDE: (type $1 (func (result i32)))

;; INCLUDE: (type $2 (func (param i32)))

;; INCLUDE: (import "a" "b" (func $import (type $0)))
;; INCLUDE: (import "a" "b" (func $import (type $void)))
(import "a" "b" (func $import))

;; WITHOUT: (table $t 0 funcref)
;; INCLUDE: (table $t 0 funcref)
(table $t funcref 0)

;; WITHOUT: (elem declare func $throw)

;; WITHOUT: (tag $tag)
;; INCLUDE: (elem declare func $throw)

;; INCLUDE: (tag $tag)
(tag $tag)

;; WITHOUT: (func $main (type $0)
;; WITHOUT: (func $main (type $void)
;; WITHOUT-NEXT: (call $nop)
;; WITHOUT-NEXT: (call $unreachable)
;; WITHOUT-NEXT: (call $call-nop)
Expand All @@ -39,7 +48,7 @@
;; WITHOUT-NEXT: (call $throw)
;; WITHOUT-NEXT: (call $throw-and-import)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $main (type $0)
;; INCLUDE: (func $main (type $void)
;; INCLUDE-NEXT: (call $unreachable)
;; INCLUDE-NEXT: (call $call-unreachable)
;; INCLUDE-NEXT: (call $throw)
Expand Down Expand Up @@ -67,10 +76,10 @@
(call $throw-and-import)
)

;; WITHOUT: (func $cycle (type $0)
;; WITHOUT: (func $cycle (type $void)
;; WITHOUT-NEXT: (call $cycle)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $cycle (type $0)
;; INCLUDE: (func $cycle (type $void)
;; INCLUDE-NEXT: (call $cycle)
;; INCLUDE-NEXT: )
(func $cycle
Expand All @@ -79,62 +88,62 @@
(call $cycle)
)

;; WITHOUT: (func $cycle-1 (type $0)
;; WITHOUT: (func $cycle-1 (type $void)
;; WITHOUT-NEXT: (call $cycle-2)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $cycle-1 (type $0)
;; INCLUDE: (func $cycle-1 (type $void)
;; INCLUDE-NEXT: (call $cycle-2)
;; INCLUDE-NEXT: )
(func $cycle-1
;; $cycle-1 and -2 form a cycle together, in which no call can be removed.
(call $cycle-2)
)

;; WITHOUT: (func $cycle-2 (type $0)
;; WITHOUT: (func $cycle-2 (type $void)
;; WITHOUT-NEXT: (call $cycle-1)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $cycle-2 (type $0)
;; INCLUDE: (func $cycle-2 (type $void)
;; INCLUDE-NEXT: (call $cycle-1)
;; INCLUDE-NEXT: )
(func $cycle-2
(call $cycle-1)
)

;; WITHOUT: (func $nop (type $0)
;; WITHOUT: (func $nop (type $void)
;; WITHOUT-NEXT: (nop)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $nop (type $0)
;; INCLUDE: (func $nop (type $void)
;; INCLUDE-NEXT: (nop)
;; INCLUDE-NEXT: )
(func $nop
(nop)
)

;; WITHOUT: (func $unreachable (type $0)
;; WITHOUT: (func $unreachable (type $void)
;; WITHOUT-NEXT: (unreachable)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $unreachable (type $0)
;; INCLUDE: (func $unreachable (type $void)
;; INCLUDE-NEXT: (unreachable)
;; INCLUDE-NEXT: )
(func $unreachable
(unreachable)
)

;; WITHOUT: (func $call-nop (type $0)
;; WITHOUT: (func $call-nop (type $void)
;; WITHOUT-NEXT: (call $nop)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $call-nop (type $0)
;; INCLUDE: (func $call-nop (type $void)
;; INCLUDE-NEXT: (nop)
;; INCLUDE-NEXT: )
(func $call-nop
;; This call to a nop can be optimized out, as above, in INCLUDE.
(call $nop)
)

;; WITHOUT: (func $call-unreachable (type $0)
;; WITHOUT: (func $call-unreachable (type $void)
;; WITHOUT-NEXT: (call $unreachable)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $call-unreachable (type $0)
;; INCLUDE: (func $call-unreachable (type $void)
;; INCLUDE-NEXT: (call $unreachable)
;; INCLUDE-NEXT: )
(func $call-unreachable
Expand Down Expand Up @@ -172,7 +181,7 @@
)
)

;; WITHOUT: (func $call-throw-and-catch (type $0)
;; WITHOUT: (func $call-throw-and-catch (type $void)
;; WITHOUT-NEXT: (try $try
;; WITHOUT-NEXT: (do
;; WITHOUT-NEXT: (call $throw)
Expand All @@ -190,7 +199,7 @@
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; INCLUDE: (func $call-throw-and-catch (type $0)
;; INCLUDE: (func $call-throw-and-catch (type $void)
;; INCLUDE-NEXT: (try $try0
;; INCLUDE-NEXT: (do
;; INCLUDE-NEXT: (call $throw-and-import)
Expand Down Expand Up @@ -219,10 +228,10 @@
)
)

;; WITHOUT: (func $return-call-throw-and-catch (type $0)
;; WITHOUT: (func $return-call-throw-and-catch (type $void)
;; WITHOUT-NEXT: (return_call $throw)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $return-call-throw-and-catch (type $0)
;; INCLUDE: (func $return-call-throw-and-catch (type $void)
;; INCLUDE-NEXT: (return_call $throw)
;; INCLUDE-NEXT: )
(func $return-call-throw-and-catch
Expand All @@ -238,7 +247,57 @@
)
)

;; WITHOUT: (func $call-return-call-throw-and-catch (type $0)
;; WITHOUT: (func $return-call-indirect-throw-and-catch (type $void)
;; WITHOUT-NEXT: (return_call_indirect $t (type $void)
;; WITHOUT-NEXT: (i32.const 0)
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; INCLUDE: (func $return-call-indirect-throw-and-catch (type $void)
;; INCLUDE-NEXT: (return_call_indirect $t (type $void)
;; INCLUDE-NEXT: (i32.const 0)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: )
(func $return-call-indirect-throw-and-catch
(try
(do
;; This call cannot be optimized out, as the target may throw. However,
;; the surrounding try-catch can be removed even without global effects
;; because the throw from the return_call is never observed by this
;; try-catch.
(return_call_indirect
(i32.const 0)
)
)
(catch_all)
)
)

;; WITHOUT: (func $return-call-ref-throw-and-catch (type $void)
;; WITHOUT-NEXT: (return_call_ref $void
;; WITHOUT-NEXT: (ref.func $throw)
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; INCLUDE: (func $return-call-ref-throw-and-catch (type $void)
;; INCLUDE-NEXT: (return_call_ref $void
;; INCLUDE-NEXT: (ref.func $throw)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: )
(func $return-call-ref-throw-and-catch
(try
(do
;; This call cannot be optimized out, as the target may throw. However,
;; the surrounding try-catch can be removed even without global effects
;; because the throw from the return_call is never observed by this
;; try-catch.
(return_call_ref $void
(ref.func $throw)
)
)
(catch_all)
)
)

;; WITHOUT: (func $call-return-call-throw-and-catch (type $void)
;; WITHOUT-NEXT: (try $try
;; WITHOUT-NEXT: (do
;; WITHOUT-NEXT: (call $return-call-throw-and-catch)
Expand All @@ -247,10 +306,46 @@
;; WITHOUT-NEXT: (nop)
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: (try $try1
;; WITHOUT-NEXT: (do
;; WITHOUT-NEXT: (call $return-call-indirect-throw-and-catch)
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: (catch_all
;; WITHOUT-NEXT: (nop)
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: (try $try2
;; WITHOUT-NEXT: (do
;; WITHOUT-NEXT: (call $return-call-ref-throw-and-catch)
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: (catch_all
;; WITHOUT-NEXT: (nop)
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: (call $return-call-throw-and-catch)
;; WITHOUT-NEXT: (call $return-call-indirect-throw-and-catch)
;; WITHOUT-NEXT: (call $return-call-ref-throw-and-catch)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $call-return-call-throw-and-catch (type $0)
;; INCLUDE: (func $call-return-call-throw-and-catch (type $void)
;; INCLUDE-NEXT: (try $try1
;; INCLUDE-NEXT: (do
;; INCLUDE-NEXT: (call $return-call-indirect-throw-and-catch)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: (catch_all
;; INCLUDE-NEXT: (nop)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: (try $try2
;; INCLUDE-NEXT: (do
;; INCLUDE-NEXT: (call $return-call-ref-throw-and-catch)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: (catch_all
;; INCLUDE-NEXT: (nop)
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: )
;; INCLUDE-NEXT: (call $return-call-throw-and-catch)
;; INCLUDE-NEXT: (call $return-call-indirect-throw-and-catch)
;; INCLUDE-NEXT: (call $return-call-ref-throw-and-catch)
;; INCLUDE-NEXT: )
(func $call-return-call-throw-and-catch
(try
Expand All @@ -262,11 +357,29 @@
)
(catch_all)
)
;; This cannot be optimized out at all.
(try
(do
;; This would be the same, except since it performs an indirect call, we
;; conservatively assume it could have any effect, so we can't optimize.
(call $return-call-indirect-throw-and-catch)
)
(catch_all)
)
(try
(do
;; Same here.
(call $return-call-ref-throw-and-catch)
)
(catch_all)
)

;; These cannot be optimized out at all.
(call $return-call-throw-and-catch)
Copy link
Member

Choose a reason for hiding this comment

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

Please test return_call_indirect/ref as well, so we have coverage for them adding the hasReturnCallThrow field. (IIANM the tests below test the other property, that they do not have throws_ set and so they can be optimized better.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added tests, but I'm not sure they test what you're looking for because the callees also have the calls effect, which is enough to inhibit optimizations on its own.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I guess calls masks this. Still, those tests look useful - maybe some day we'll make calls more refined somehow, and these would become more important.

(call $return-call-indirect-throw-and-catch)
(call $return-call-ref-throw-and-catch)
)

;; WITHOUT: (func $call-unreachable-and-catch (type $0)
;; WITHOUT: (func $call-unreachable-and-catch (type $void)
;; WITHOUT-NEXT: (try $try
;; WITHOUT-NEXT: (do
;; WITHOUT-NEXT: (call $unreachable)
Expand All @@ -276,7 +389,7 @@
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; WITHOUT-NEXT: )
;; INCLUDE: (func $call-unreachable-and-catch (type $0)
;; INCLUDE: (func $call-unreachable-and-catch (type $void)
;; INCLUDE-NEXT: (call $unreachable)
;; INCLUDE-NEXT: )
(func $call-unreachable-and-catch
Expand Down Expand Up @@ -346,20 +459,20 @@
)
)

;; WITHOUT: (func $throw (type $0)
;; WITHOUT: (func $throw (type $void)
;; WITHOUT-NEXT: (throw $tag)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $throw (type $0)
;; INCLUDE: (func $throw (type $void)
;; INCLUDE-NEXT: (throw $tag)
;; INCLUDE-NEXT: )
(func $throw
(throw $tag)
)

;; WITHOUT: (func $throw-and-import (type $0)
;; WITHOUT: (func $throw-and-import (type $void)
;; WITHOUT-NEXT: (throw $tag)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $throw-and-import (type $0)
;; INCLUDE: (func $throw-and-import (type $void)
;; INCLUDE-NEXT: (throw $tag)
;; INCLUDE-NEXT: )
(func $throw-and-import
Expand All @@ -374,11 +487,11 @@
)
)

;; WITHOUT: (func $cycle-with-unknown-call (type $0)
;; WITHOUT: (func $cycle-with-unknown-call (type $void)
;; WITHOUT-NEXT: (call $cycle-with-unknown-call)
;; WITHOUT-NEXT: (call $import)
;; WITHOUT-NEXT: )
;; INCLUDE: (func $cycle-with-unknown-call (type $0)
;; INCLUDE: (func $cycle-with-unknown-call (type $void)
;; INCLUDE-NEXT: (call $cycle-with-unknown-call)
;; INCLUDE-NEXT: (call $import)
;; INCLUDE-NEXT: )
Expand Down