Skip to content

Commit

Permalink
winch: Add support for br_table (#6951)
Browse files Browse the repository at this point in the history
* winch: Add support for `br_table`

This change adds support for the `br_table` instruction, including
several modifications to the existing control flow implementation:

* Improved handling of jumps to loops: Previously, the compiler erroneously
treated the result of loop blocks as the definitive result of the jump. This
change fixes this bug.

* Streamlined result handling and stack pointer balancing: In the past, these
operations were executed in two distinct steps, complicating the process of
ensuring the correct invariants when emitting unconditional jumps. To simplify
this, `CodeGenContext::unconditional_jump` is introduced . This function
guarantees all necessary invariants are met, encapsulating the entire operation
within a single function for easier understanding and maintenance.

* Handling of unreachable state at the end of a function: when reaching the end
of a function in an unreachable state, clear the stack and ensure that the
machine stack pointer is correctly placed according to the expectations of the
outermost block.

In addition to the above refactoring, the main implementation of the
`br_table` instruction involves emitting labels for each target. Within each
label, an unconditional jump is emitted to the frame's label, ensuring correct
stack pointer balancing  when the jump is emitted.

While it is possible to optimize this process by avoiding intermediate labels
when balancing isn't required, I've opted to maintain the current
implementation until such optimization becomes necessary.

* chore: Rust fmt

* fuzzing: Add `BrTable` to list of support instructions

* docs: Improve documentation for `unconditional_jump`
  • Loading branch information
saulecabrera authored Sep 1, 2023
1 parent e5e9cf8 commit 350410a
Show file tree
Hide file tree
Showing 27 changed files with 33,362 additions and 139 deletions.
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/differential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ fn winch_supports_module(module: &[u8]) -> bool {
| Loop { .. }
| Br { .. }
| BrIf { .. }
| BrTable { .. }
| Unreachable { .. }
| Return { .. }
| F32Const { .. }
Expand Down
33 changes: 33 additions & 0 deletions tests/misc_testsuite/winch/block.wast
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@
)
)

(func (export "as-br_table-first") (result i32)
(block (result i32) (block (result i32) (i32.const 1)) (i32.const 2) (br_table 0 0))
)
(func (export "as-br_table-last") (result i32)
(block (result i32) (i32.const 2) (block (result i32) (i32.const 1)) (br_table 0 0))
)

(func (export "break-bare") (result i32)
(block (br 0) (unreachable))
(block (br_if 0 (i32.const 1)) (unreachable))
(block (br_table 0 (i32.const 0)) (unreachable))
(block (br_table 0 0 0 (i32.const 1)) (unreachable))
(i32.const 19)
)

(func (export "break-repeated") (result i32)
(block (result i32)
(br 0 (i32.const 18))
(br 0 (i32.const 19))
(drop (br_if 0 (i32.const 20) (i32.const 0)))
(drop (br_if 0 (i32.const 20) (i32.const 1)))
(br 0 (i32.const 21))
(br_table 0 (i32.const 22) (i32.const 4))
(br_table 0 0 0 (i32.const 23) (i32.const 1))
(i32.const 21)
)
)

(func (export "deep") (result i32)
(block (result i32) (block (result i32)
(block (result i32) (block (result i32)
Expand Down Expand Up @@ -163,3 +191,8 @@
(assert_return (invoke "as-test-operand") (i32.const 0))
(assert_return (invoke "break-inner") (i32.const 0xf))
(assert_return (invoke "effects") (i32.const 1))

(assert_return (invoke "as-br_table-first") (i32.const 1))
(assert_return (invoke "as-br_table-last") (i32.const 2))
(assert_return (invoke "break-bare") (i32.const 19))
(assert_return (invoke "break-repeated") (i32.const 18))
47 changes: 47 additions & 0 deletions tests/misc_testsuite/winch/br.wast
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,47 @@
)
)
)

(func (export "as-br_table-index")
(block (br_table 0 0 0 (br 0)))
)
(func (export "as-br_table-value") (result i32)
(block (result i32)
(br_table 0 0 0 (br 0 (i32.const 10)) (i32.const 1)) (i32.const 7)
)
)
(func (export "as-br_table-value-index") (result i32)
(block (result i32)
(br_table 0 0 (i32.const 6) (br 0 (i32.const 11))) (i32.const 7)
)
)

(func (export "nested-br_table-value") (result i32)
(i32.add
(i32.const 1)
(block (result i32)
(drop (i32.const 2))
(drop
(block (result i32)
(drop (i32.const 4))
(br_table 0 (br 1 (i32.const 8)) (i32.const 1))
)
)
(i32.const 16)
)
)
)

(func (export "nested-br_table-value-index") (result i32)
(i32.add
(i32.const 1)
(block (result i32)
(drop (i32.const 2))
(br_table 0 (i32.const 4) (br 0 (i32.const 8)))
(i32.const 16)
)
)
)
)

(assert_return (invoke "type-i32-value") (i32.const 1))
Expand Down Expand Up @@ -213,3 +254,9 @@
(assert_return (invoke "nested-br-value") (i32.const 9))
(assert_return (invoke "nested-br_if-value") (i32.const 9))
(assert_return (invoke "nested-br_if-value-cond") (i32.const 9))

(assert_return (invoke "as-br_table-index"))
(assert_return (invoke "as-br_table-value") (i32.const 10))
(assert_return (invoke "as-br_table-value-index") (i32.const 11))
(assert_return (invoke "nested-br_table-value") (i32.const 9))
(assert_return (invoke "nested-br_table-value-index") (i32.const 9))
55 changes: 55 additions & 0 deletions tests/misc_testsuite/winch/br_if.wast
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,52 @@
)
)
)

(func (export "as-br_table-index")
(block (br_table 0 0 0 (br_if 0 (i32.const 1) (i32.const 2))))
)
(func (export "as-br_table-value") (result i32)
(block (result i32)
(br_table 0 0 0 (br_if 0 (i32.const 1) (i32.const 2)) (i32.const 3)) (i32.const 4)
)
)
(func (export "as-br_table-value-index") (result i32)
(block (result i32)
(br_table 0 0 (i32.const 2) (br_if 0 (i32.const 1) (i32.const 3))) (i32.const 4)
)
)

(func (export "nested-br_table-value") (param i32) (result i32)
(i32.add
(i32.const 1)
(block (result i32)
(drop (i32.const 2))
(br_table 0
(block (result i32)
(drop (br_if 1 (i32.const 8) (local.get 0))) (i32.const 4)
)
(i32.const 1)
)
(i32.const 16)
)
)
)

(func (export "nested-br_table-value-index") (param i32) (result i32)
(i32.add
(i32.const 1)
(block (result i32)
(drop (i32.const 2))
(br_table 0
(i32.const 4)
(block (result i32)
(drop (br_if 1 (i32.const 8) (local.get 0))) (i32.const 1)
)
)
(i32.const 16)
)
)
)
)


Expand Down Expand Up @@ -260,3 +306,12 @@
(assert_return (invoke "nested-br_if-value" (i32.const 1)) (i32.const 9))
(assert_return (invoke "nested-br_if-value-cond" (i32.const 0)) (i32.const 5))
(assert_return (invoke "nested-br_if-value-cond" (i32.const 1)) (i32.const 9))

(assert_return (invoke "as-br_table-index"))
(assert_return (invoke "as-br_table-value") (i32.const 1))
(assert_return (invoke "as-br_table-value-index") (i32.const 1))

(assert_return (invoke "nested-br_table-value" (i32.const 0)) (i32.const 5))
(assert_return (invoke "nested-br_table-value" (i32.const 1)) (i32.const 9))
(assert_return (invoke "nested-br_table-value-index" (i32.const 0)) (i32.const 5))
(assert_return (invoke "nested-br_table-value-index" (i32.const 1)) (i32.const 9))
Loading

0 comments on commit 350410a

Please sign in to comment.