Skip to content

Move default blocks into jump tables#5756

Merged
elliottt merged 7 commits intobytecodealliance:mainfrom
elliottt:trevor/default-in-jump-table
Feb 10, 2023
Merged

Move default blocks into jump tables#5756
elliottt merged 7 commits intobytecodealliance:mainfrom
elliottt:trevor/default-in-jump-table

Conversation

@elliottt
Copy link
Member

@elliottt elliottt commented Feb 9, 2023

While working through #5731, adapting branch_destination to return an iterator over multiple slices of BlockCall values was a little awkward, and yielded more complicated indexing for heavily used operations like inst_values. As the root of the problem is the jump table default target and data not living in contiguous memory, there was a somewhat obvious solution: inline the default block into the JumpTableData, and remove it from the BranchTable instruction data.

The resulting refactoring is implemented in this PR: jump tables require a default block when they are created, and maintain an invariant that the last element of their internal vector is always the default label. Additionally, they now export slices for both the vector and just the jump table, to make it easy to adapt existing uses of as_slice.

For a little bit of additional motivation, consider this godbolt link. There are three indexing functions implemented: one that indexes into two slices using nth on a chained iterator; one that checks which slice the index falls on; and one that indexes into a single slice. By far the best option is indexing into a single slice, followed by choosing the slice to index, followed by using nth. Without this refactoring, we would need to pick the approach in either index1 or index2 to finish transitioning br_table to using BlockCall arguments, and both feel like bad options.

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. cranelift:wasm labels Feb 9, 2023
@elliottt elliottt force-pushed the trevor/default-in-jump-table branch from 0a1e9ad to aca99d1 Compare February 9, 2023 22:53
@elliottt elliottt marked this pull request as ready for review February 9, 2023 22:56
@bjorn3
Copy link
Contributor

bjorn3 commented Feb 9, 2023

I think it is less clear what the default case is for the br_table with this change. Maybe a JumpTableData could be formatted as default_block, [block1, block2] such that the effective syntax of br_table is preserved?

edit: nevermind you are already doing that. I thought you hadn't updated the tests yet. my bad.

/// Create a new empty jump table.
pub fn new() -> Self {
Self { table: Vec::new() }
pub fn new(def: Block) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

This sounds like define or definition to me. Default is a keyword, and default_ isn't the nicest either. Unless someone has a better suggestion I guess leaving it the way it is right now is fine.

@elliottt elliottt force-pushed the trevor/default-in-jump-table branch from 90a9b38 to dd61d04 Compare February 10, 2023 00:55
Copy link
Contributor

@jameysharp jameysharp left a comment

Choose a reason for hiding this comment

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

I like it!

visit(inst, table.default_block(), false);

for &dest in f.stencil.dfg.jump_tables[*table].as_slice() {
for &dest in table.as_slice() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was going to say that this should just use table.all_branches() but then I remembered that would change the visit order. Instead, would you update the comment above to note that some callers depend on the default block being visited first?

///
/// The default block for the jump table is stored as the last element of the underlying vector,
/// and is not included in the length of the jump table. It can be accessed through the
/// `default_block` function. The default block is iterated via the `iter` and `iter_mut` methods.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment should be updated to refer to all_branches instead of iter.

@elliottt elliottt merged commit d99783f into bytecodealliance:main Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. cranelift:wasm cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants