Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

winch: Add support for br_table #6951

Merged
merged 4 commits into from
Sep 2, 2023

Conversation

saulecabrera
Copy link
Member

@saulecabrera saulecabrera commented Sep 1, 2023

Part of #6528

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.

@saulecabrera saulecabrera requested review from a team as code owners September 1, 2023 15:16
@saulecabrera saulecabrera requested review from alexcrichton and removed request for a team and alexcrichton September 1, 2023 15:16
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.
@saulecabrera saulecabrera requested a review from a team as a code owner September 1, 2023 15:53
@saulecabrera saulecabrera requested review from elliottt and removed request for a team and elliottt September 1, 2023 15:53
@github-actions github-actions bot added fuzzing Issues related to our fuzzing infrastructure winch Winch issues or pull requests labels Sep 1, 2023
@github-actions
Copy link

github-actions bot commented Sep 1, 2023

Subscribe to Label Action

cc @fitzgen, @saulecabrera

This issue or pull request has been labeled: "fuzzing", "winch"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: fuzzing
  • saulecabrera: winch

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

This looks overall good to me, except for a bit of confusion around one stack-alignment case. With a slightly more detailed description in the comment I think it should be good though!

(also as an fyi, I'm on PTO all of next week; happy to re-review today if it happens, otherwise further reviews will be a bit delayed, sorry)

winch/codegen/src/codegen/context.rs Outdated Show resolved Hide resolved
winch/codegen/src/codegen/context.rs Show resolved Hide resolved
Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

LGTM!

@cfallin cfallin added this pull request to the merge queue Sep 1, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 1, 2023
@saulecabrera
Copy link
Member Author

Hmm the failure seems to be related to WASI sockets, I suspect it's the same failure reported in #6952 (comment)

@cfallin
Copy link
Member

cfallin commented Sep 1, 2023

Now that #6953 is merged let's see if this goes in...

@cfallin cfallin added this pull request to the merge queue Sep 1, 2023
Merged via the queue into bytecodealliance:main with commit 350410a Sep 2, 2023
18 checks passed
@saulecabrera saulecabrera deleted the winch-br_table branch September 2, 2023 12:31
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Sep 6, 2023
* 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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fuzzing Issues related to our fuzzing infrastructure winch Winch issues or pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants