summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-12-12 16:12:08 -0800
committerGitHub <noreply@github.com>2022-12-12 16:12:08 -0800
commit0668d9328ad57100103d6b59f40de513659e1c6b (patch)
tree0b0c59d5340034a151369e96906a45e1ecf1421f /test
parent6c0d8f7bb674da1cf22499873f6fe87e45f3be64 (diff)
downloadbinaryen-0668d9328ad57100103d6b59f40de513659e1c6b.tar.gz
binaryen-0668d9328ad57100103d6b59f40de513659e1c6b.tar.bz2
binaryen-0668d9328ad57100103d6b59f40de513659e1c6b.zip
[Wasm GC] Fix GlobalStructInference on unrefined globals (#5338)
If a global's type is not fully refined, then when --gsi replaces a reference with a global.get, we end up with a type that might not be good enough. For example, if the type is any then it is not a subtype of eq and we can't do ref.eq on it, which this pass requires. We also can't just do struct.get on it if it is a too-distant parent or such.
Diffstat (limited to 'test')
-rw-r--r--test/lit/passes/gsi.wast93
1 files changed, 93 insertions, 0 deletions
diff --git a/test/lit/passes/gsi.wast b/test/lit/passes/gsi.wast
index 52ba5d7f6..4cec6aa3c 100644
--- a/test/lit/passes/gsi.wast
+++ b/test/lit/passes/gsi.wast
@@ -1175,3 +1175,96 @@
)
)
)
+
+;; One global is declared as heap type |any|, which we cannot do a ref.eq on, so
+;; we do not optimize.
+(module
+ ;; CHECK: (type $A (struct (field i32)))
+ (type $A (struct (field i32)))
+
+ ;; CHECK: (type $ref?|$A|_=>_i32 (func (param (ref null $A)) (result i32)))
+
+ ;; CHECK: (global $A0 (ref any) (struct.new $A
+ ;; CHECK-NEXT: (i32.const 1337)
+ ;; CHECK-NEXT: ))
+ (global $A0 (ref any) (struct.new $A
+ (i32.const 1337)
+ ))
+
+ ;; CHECK: (global $A1 (ref $A) (struct.new $A
+ ;; CHECK-NEXT: (i32.const 9999)
+ ;; CHECK-NEXT: ))
+ (global $A1 (ref $A) (struct.new $A
+ (i32.const 9999)
+ ))
+
+ ;; CHECK: (func $func (type $ref?|$A|_=>_i32) (param $ref (ref null $A)) (result i32)
+ ;; CHECK-NEXT: (struct.get $A 0
+ ;; CHECK-NEXT: (local.get $ref)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $func (param $ref (ref null $A)) (result i32)
+ (struct.get $A 0
+ (local.get $ref)
+ )
+ )
+)
+
+;; As above, but now there is just a single global. Again, we should not
+;; optimize because the global is not declared as a struct type (which means we
+;; cannot do a struct.get on a global.get of that global - we'd need a cast; it
+;; is simpler to not optimize here and let other passes first refine the global
+;; type).
+(module
+ ;; CHECK: (type $A (struct (field i32)))
+ (type $A (struct (field i32)))
+
+ ;; CHECK: (type $ref?|$A|_=>_i32 (func (param (ref null $A)) (result i32)))
+
+ ;; CHECK: (global $A0 (ref any) (struct.new $A
+ ;; CHECK-NEXT: (i32.const 1337)
+ ;; CHECK-NEXT: ))
+ (global $A0 (ref any) (struct.new $A
+ (i32.const 1337)
+ ))
+
+ ;; CHECK: (func $func (type $ref?|$A|_=>_i32) (param $ref (ref null $A)) (result i32)
+ ;; CHECK-NEXT: (struct.get $A 0
+ ;; CHECK-NEXT: (local.get $ref)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $func (param $ref (ref null $A)) (result i32)
+ (struct.get $A 0
+ (local.get $ref)
+ )
+ )
+)
+
+(module
+ ;; CHECK: (type $A (struct (field i32)))
+ (type $A (struct (field i32)))
+
+ ;; CHECK: (type $ref?|$A|_=>_i32 (func (param (ref null $A)) (result i32)))
+
+ ;; CHECK: (global $A0 (ref $A) (struct.new $A
+ ;; CHECK-NEXT: (i32.const 1337)
+ ;; CHECK-NEXT: ))
+ (global $A0 (ref $A) (struct.new $A
+ (i32.const 1337)
+ ))
+
+ ;; CHECK: (func $func (type $ref?|$A|_=>_i32) (param $ref (ref null $A)) (result i32)
+ ;; CHECK-NEXT: (block ;; (replaces something unreachable we can't emit)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (ref.null none)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $func (param $ref (ref null $A)) (result i32)
+ ;; Test that we do not error when we see a struct.get of a bottom type.
+ (struct.get $A 0
+ (ref.null none)
+ )
+ )
+)