diff options
author | Thomas Lively <tlively@google.com> | 2024-11-26 21:57:29 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-26 21:57:29 -0800 |
commit | f8e1622bf0835dec2bb97bcb73281d34dbde4e2d (patch) | |
tree | 76bfc786e8dc113005be6167721b94ae4639e137 /test/lit/binary | |
parent | 6f0f2e00521843118b63f41732dc2eb86d39fa09 (diff) | |
download | binaryen-f8e1622bf0835dec2bb97bcb73281d34dbde4e2d.tar.gz binaryen-f8e1622bf0835dec2bb97bcb73281d34dbde4e2d.tar.bz2 binaryen-f8e1622bf0835dec2bb97bcb73281d34dbde4e2d.zip |
Use IRBuilder in the binary parser (#6963)
IRBuilder is a utility for turning arbitrary valid streams of Wasm
instructions into valid Binaryen IR. It is already used in the text
parser, so now use it in the binary parser as well. Since the IRBuilder
API for building each intruction requires only the information that the
binary and text formats include as immediates to that instruction, the
parser is now much simpler than before. In particular, it does not need
to manage a stack of instructions to figure out what the children of
each expression should be; IRBuilder handles this instead.
There are some differences between the IR constructed by IRBuilder and
the IR the binary parser constructed before this change. Most
importantly, IRBuilder generates better multivalue code because it
avoids eagerly breaking up multivalue results into individual components
that might need to be immediately reassembled into a tuple. It also
parses try-delegate more correctly, allowing the delegate to target
arbitrary labels, not just other `try`s. There are also a couple
superficial differences in the generated label and scratch local names.
As part of this change, add support for recording binary source
locations in IRBuilder.
Diffstat (limited to 'test/lit/binary')
-rw-r--r-- | test/lit/binary/bad-delegate.test | 17 | ||||
-rw-r--r-- | test/lit/binary/declarative-element-use-expr.test | 8 | ||||
-rw-r--r-- | test/lit/binary/delegate-block.test | 27 | ||||
-rw-r--r-- | test/lit/binary/delegate-block.test.wasm (renamed from test/lit/binary/bad-delegate.test.wasm) | bin | 32 -> 32 bytes | |||
-rw-r--r-- | test/lit/binary/dwarf-multivalue.test | 16 | ||||
-rw-r--r-- | test/lit/binary/stacky-eh-legacy.test | 19 | ||||
-rw-r--r-- | test/lit/binary/stacky-nn-tuple.test | 228 |
7 files changed, 169 insertions, 146 deletions
diff --git a/test/lit/binary/bad-delegate.test b/test/lit/binary/bad-delegate.test deleted file mode 100644 index 8e6a011a5..000000000 --- a/test/lit/binary/bad-delegate.test +++ /dev/null @@ -1,17 +0,0 @@ -;; Test that we error properly on a file with a bad delegate (a delegate of an -;; index that does not refer to a valid try-catch). - -;; Disassembled binary from wabt: -;; -;; (module -;; (type (;0;) (func)) -;; (func (;0;) (type 0) -;; block ;; label = @1 -;; try ;; label = @2 -;; nop -;; delegate 0 -;; end)) - -;; RUN: not wasm-opt -all %s.wasm 2>&1 | filecheck %s - -;; CHECK: exceptionTargetNames not empty - invalid delegate diff --git a/test/lit/binary/declarative-element-use-expr.test b/test/lit/binary/declarative-element-use-expr.test index aecdf9ebd..fa0eb965f 100644 --- a/test/lit/binary/declarative-element-use-expr.test +++ b/test/lit/binary/declarative-element-use-expr.test @@ -13,16 +13,14 @@ ;; preserve declarative segments. This is fine, as we test that the ;; binary parser can parse it correctly. -;; RUN: wasm-opt -all %s.wasm -all --print | filecheck %s +;; RUN: wasm-opt -all %s.wasm -S -o - | filecheck %s ;; CHECK: (module ;; CHECK-NEXT: (type $0 (func)) ;; CHECK-NEXT: (elem declare func $0) ;; CHECK-NEXT: (func $0 (type $0) -;; CHECK-NEXT: (block $label$1 -;; CHECK-NEXT: (drop -;; CHECK-NEXT: (ref.func $0) -;; CHECK-NEXT: ) +;; CHECK-NEXT: (drop +;; CHECK-NEXT: (ref.func $0) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) diff --git a/test/lit/binary/delegate-block.test b/test/lit/binary/delegate-block.test new file mode 100644 index 000000000..7da386cb0 --- /dev/null +++ b/test/lit/binary/delegate-block.test @@ -0,0 +1,27 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. + +;; Test that we can parse a binary with a delegate that targets a block instead +;; of a try-catch. + +;; Disassembled binary from wabt: +;; +;; (module +;; (type (;0;) (func)) +;; (func (;0;) (type 0) +;; block ;; label = @1 +;; try ;; label = @2 +;; nop +;; delegate 0 +;; end)) + +;; RUN: wasm-opt -all %s.wasm -S -o - | filecheck %s +;; CHECK: (type $0 (func)) + +;; CHECK: (func $0 (type $0) +;; CHECK-NEXT: (try +;; CHECK-NEXT: (do +;; CHECK-NEXT: (nop) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (delegate 0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) diff --git a/test/lit/binary/bad-delegate.test.wasm b/test/lit/binary/delegate-block.test.wasm Binary files differindex db9645e35..db9645e35 100644 --- a/test/lit/binary/bad-delegate.test.wasm +++ b/test/lit/binary/delegate-block.test.wasm diff --git a/test/lit/binary/dwarf-multivalue.test b/test/lit/binary/dwarf-multivalue.test index c803dea14..250a861bf 100644 --- a/test/lit/binary/dwarf-multivalue.test +++ b/test/lit/binary/dwarf-multivalue.test @@ -39,8 +39,8 @@ ;; (local $10 f32) ;; If we parse this wasm file into Binaryen IR, two locals are added in the -;; process. Here $11 is added for tuple parsing and $12 is added for stacky IR -;; resolving during binary reading process. +;; process. Here $scratch is added for tuple parsing and $scratch_12 is added +;; for stacky IR resolving during binary reading process. ;; RUN: wasm-dis %s.wasm -o - | filecheck %s --check-prefix=ORIG ;; ORIG: (func $test ;; ORIG-NEXT: (local $0 i32) @@ -54,8 +54,8 @@ ;; ORIG-NEXT: (local $8 f32) ;; ORIG-NEXT: (local $9 i32) ;; ORIG-NEXT: (local $10 f32) -;; ORIG-NEXT: (local $11 (tuple i32 f32)) -;; ORIG-NEXT: (local $12 i32) +;; ORIG-NEXT: (local $scratch (tuple i32 f32)) +;; ORIG-NEXT: (local $scratch_12 i32) ;; If we write this IR into binary, even if this cannot be displayed in the wast ;; format, the local order of $test will look like this, because we don't @@ -92,11 +92,11 @@ ;; ROUNDTRIP-NEXT: (local $8 f32) ;; ROUNDTRIP-NEXT: (local $9 i32) ;; ROUNDTRIP-NEXT: (local $10 f32) -;; ROUNDTRIP-NEXT: (local $11 i32) +;; ROUNDTRIP-NEXT: (local $scratch i32) ;; ROUNDTRIP-NEXT: (local $12 f32) -;; ROUNDTRIP-NEXT: (local $13 i32) -;; ROUNDTRIP-NEXT: (local $14 (tuple i32 f32)) -;; ROUNDTRIP-NEXT: (local $15 i32) +;; ROUNDTRIP-NEXT: (local $scratch_12 i32) +;; ROUNDTRIP-NEXT: (local $scratch_14 (tuple i32 f32)) +;; ROUNDTRIP-NEXT: (local $scratch_15 i32) ;; We can see that we don't reorder the locals during the process and the ;; original list of locals, local $0~$10, is untouched, to NOT invalidate DWARF diff --git a/test/lit/binary/stacky-eh-legacy.test b/test/lit/binary/stacky-eh-legacy.test index c22a5165d..c77435f0b 100644 --- a/test/lit/binary/stacky-eh-legacy.test +++ b/test/lit/binary/stacky-eh-legacy.test @@ -1,3 +1,4 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. ;; Verify stacky EH binary can be parsed correctly. ;; ;; stacky-eh-old.test.wasm contains below: @@ -34,15 +35,21 @@ ;; The fixup will hoist the 'pop' and create another local to store it right ;; after 'catch'. -RUN: wasm-opt -all %s.wasm --print | filecheck %s +;; RUN: wasm-opt -all %s.wasm -S -o - | filecheck %s -;; CHECK: (func $0 +;; CHECK: (type $0 (func (param i32))) + +;; CHECK: (type $1 (func)) + +;; CHECK: (tag $tag$0 (param i32)) + +;; CHECK: (func $0 (type $1) ;; CHECK-NEXT: (local $0 i32) ;; CHECK-NEXT: (local $1 i32) ;; CHECK-NEXT: (local $2 i32) -;; CHECK-NEXT: (local $3 i32) +;; CHECK-NEXT: (local $scratch i32) ;; CHECK-NEXT: (local $4 i32) -;; CHECK-NEXT: (try $label$3 +;; CHECK-NEXT: (try ;; CHECK-NEXT: (do ;; CHECK-NEXT: (nop) ;; CHECK-NEXT: ) @@ -52,13 +59,13 @@ RUN: wasm-opt -all %s.wasm --print | filecheck %s ;; CHECK-NEXT: ) ;; CHECK-NEXT: (local.set $2 ;; CHECK-NEXT: (block (result i32) -;; CHECK-NEXT: (local.set $3 +;; CHECK-NEXT: (local.set $scratch ;; CHECK-NEXT: (local.get $4) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (local.set $1 ;; CHECK-NEXT: (i32.const 3) ;; CHECK-NEXT: ) -;; CHECK-NEXT: (local.get $3) +;; CHECK-NEXT: (local.get $scratch) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) diff --git a/test/lit/binary/stacky-nn-tuple.test b/test/lit/binary/stacky-nn-tuple.test index e8a0475b5..1f6b5bb59 100644 --- a/test/lit/binary/stacky-nn-tuple.test +++ b/test/lit/binary/stacky-nn-tuple.test @@ -1,112 +1,120 @@ -# Verify stacky non-nullable tuples binary can be parsed correctly. The wasm -# contains code that uses pops to get a tuple and store it in a local, then -# reads those values. The file contains this: -# -# (module -# (type $A (struct (field (mut i32)))) -# (type $B (struct (field (mut i32)) (field (mut i32)))) -# (tag $tag$0 (param (ref $A) (ref $B))) -# (func $foo -# (local $temp ((ref null $A) (ref null $B))) -# (try $label$3 -# (do -# (nop) -# ) -# (catch $tag$0 -# (local.set $temp -# (pop (ref $A) (ref $B)) -# ) -# (drop -# (ref.as_non_null -# (tuple.extract 0 -# (local.get $temp) -# ) -# ) -# ) -# (drop -# (ref.as_non_null -# (tuple.extract 1 -# (local.get $temp) -# ) -# ) -# ) -# (unreachable) -# ) -# ) -# ) -# ) +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. +;; Verify stacky non-nullable tuples binary can be parsed correctly. The wasm +;; contains code that uses pops to get a tuple and store it in a local, then +;; reads those values. The file contains this: +;; +;; (module +;; (type $A (struct (field (mut i32)))) +;; (type $B (struct (field (mut i32)) (field (mut i32)))) +;; (tag $tag$0 (param (ref $A) (ref $B))) +;; (func $foo +;; (local $temp ((ref null $A) (ref null $B))) +;; (try $label$3 +;; (do +;; (nop) +;; ) +;; (catch $tag$0 +;; (local.set $temp +;; (pop (ref $A) (ref $B)) +;; ) +;; (drop +;; (ref.as_non_null +;; (tuple.extract 0 +;; (local.get $temp) +;; ) +;; ) +;; ) +;; (drop +;; (ref.as_non_null +;; (tuple.extract 1 +;; (local.get $temp) +;; ) +;; ) +;; ) +;; (unreachable) +;; ) +;; ) +;; ) +;; ) -RUN: wasm-opt -all %s.wasm -all --print +;; RUN: wasm-opt -all %s.wasm -all -S -o - | filecheck %s -# CHECK: (module -# CHECK-NEXT: (type ${mut:i32} (struct (field (mut i32)))) -# CHECK-NEXT: (type ${mut:i32_mut:i32} (struct (field (mut i32)) (field (mut i32)))) -# CHECK-NEXT: (type $ref|{mut:i32}|_ref|{mut:i32_mut:i32}|_=>_none (func (param (ref ${mut:i32}) (ref ${mut:i32_mut:i32})))) -# CHECK-NEXT: (type $none_=>_none (func)) -# CHECK-NEXT: (tag $tag$0 (param (ref ${mut:i32}) (ref ${mut:i32_mut:i32}))) -# CHECK-NEXT: (func $0 -# CHECK-NEXT: (local $0 (ref null ${mut:i32})) -# CHECK-NEXT: (local $1 (ref null ${mut:i32_mut:i32})) -# CHECK-NEXT: (local $2 (ref null ${mut:i32_mut:i32})) -# CHECK-NEXT: (local $3 ((ref ${mut:i32}) (ref ${mut:i32_mut:i32}))) -# CHECK-NEXT: (local $4 (ref ${mut:i32})) -# CHECK-NEXT: (local $5 (ref null ${mut:i32})) -# CHECK-NEXT: (local $6 (ref null ${mut:i32})) -# CHECK-NEXT: (try $label$3 -# CHECK-NEXT: (do -# CHECK-NEXT: (nop) -# CHECK-NEXT: ) -# CHECK-NEXT: (catch $tag$0 -# CHECK-NEXT: (local.set $3 -# CHECK-NEXT: (pop (ref ${mut:i32}) (ref ${mut:i32_mut:i32})) -# CHECK-NEXT: ) -# CHECK-NEXT: (local.set $0 -# CHECK-NEXT: (block (result (ref ${mut:i32})) -# CHECK-NEXT: (local.set $4 -# CHECK-NEXT: (tuple.extract 0 -# CHECK-NEXT: (local.get $3) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: (local.set $1 -# CHECK-NEXT: (tuple.extract 1 -# CHECK-NEXT: (local.get $3) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: (local.get $4) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: (drop -# CHECK-NEXT: (ref.as_non_null -# CHECK-NEXT: (block (result (ref null ${mut:i32})) -# CHECK-NEXT: (local.set $5 -# CHECK-NEXT: (local.get $0) -# CHECK-NEXT: ) -# CHECK-NEXT: (drop -# CHECK-NEXT: (local.get $1) -# CHECK-NEXT: ) -# CHECK-NEXT: (local.get $5) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: (drop -# CHECK-NEXT: (block (result (ref null ${mut:i32})) -# CHECK-NEXT: (local.set $6 -# CHECK-NEXT: (local.get $0) -# CHECK-NEXT: ) -# CHECK-NEXT: (local.set $2 -# CHECK-NEXT: (local.get $1) -# CHECK-NEXT: ) -# CHECK-NEXT: (local.get $6) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: (drop -# CHECK-NEXT: (ref.as_non_null -# CHECK-NEXT: (local.get $2) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: (unreachable) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: ) -# CHECK-NEXT: +;; CHECK: (type $0 (struct (field (mut i32)))) + +;; CHECK: (type $1 (struct (field (mut i32)) (field (mut i32)))) + +;; CHECK: (type $2 (func (param (ref null $0) (ref null $1)))) + +;; CHECK: (type $3 (func)) + +;; CHECK: (tag $tag$0 (param (ref null $0) (ref null $1))) + +;; CHECK: (func $0 (type $3) +;; CHECK-NEXT: (local $0 (ref null $0)) +;; CHECK-NEXT: (local $1 (ref null $1)) +;; CHECK-NEXT: (local $2 (ref null $1)) +;; CHECK-NEXT: (local $scratch (tuple (ref null $0) (ref null $1))) +;; CHECK-NEXT: (local $scratch_4 (ref null $0)) +;; CHECK-NEXT: (local $scratch_5 (ref null $0)) +;; CHECK-NEXT: (local $scratch_6 (ref null $0)) +;; CHECK-NEXT: (local $7 (tuple (ref null $0) (ref null $1))) +;; CHECK-NEXT: (try +;; CHECK-NEXT: (do +;; CHECK-NEXT: (nop) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (catch $tag$0 +;; CHECK-NEXT: (local.set $7 +;; CHECK-NEXT: (pop (tuple (ref null $0) (ref null $1))) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (block +;; CHECK-NEXT: (local.set $0 +;; CHECK-NEXT: (block (result (ref null $0)) +;; CHECK-NEXT: (local.set $scratch_4 +;; CHECK-NEXT: (tuple.extract 2 0 +;; CHECK-NEXT: (local.tee $scratch +;; CHECK-NEXT: (local.get $7) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (local.set $1 +;; CHECK-NEXT: (tuple.extract 2 1 +;; CHECK-NEXT: (local.get $scratch) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (local.get $scratch_4) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (drop +;; CHECK-NEXT: (ref.as_non_null +;; CHECK-NEXT: (block (result (ref null $0)) +;; CHECK-NEXT: (local.set $scratch_5 +;; CHECK-NEXT: (local.get $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (drop +;; CHECK-NEXT: (local.get $1) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (local.get $scratch_5) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (drop +;; CHECK-NEXT: (block (result (ref null $0)) +;; CHECK-NEXT: (local.set $scratch_6 +;; CHECK-NEXT: (local.get $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (local.set $2 +;; CHECK-NEXT: (local.get $1) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (local.get $scratch_6) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (drop +;; CHECK-NEXT: (ref.as_non_null +;; CHECK-NEXT: (local.get $2) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (unreachable) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) |