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

cranelift-wasm: Stop using table_addr instructions #8063

Merged
merged 1 commit into from
Mar 13, 2024

Conversation

jameysharp
Copy link
Contributor

@jameysharp jameysharp commented Mar 7, 2024

This CLIF instruction is specific to WebAssembly, and doesn't expose any optimization opportunities or have any other reason to be treated specially by Cranelift. So this commit makes Wasmtime emit a series of simpler Cranelift instructions instead.

I copied the implementation from Cranelift's legalization pass, which already was rewriting these instructions to simpler ones, and then simplified the result to not generate the intermediate form at all.

Merging all the table-related code into one place should eventually let us experiment more with bounds-check elimination tricks, like we've been doing with heaps.

The filetest changes demonstrate that the new cranelift-wasm implementation generates the exact same sequence of instructions that the legalization pass did, except with different value numbers and fewer aliases.

Several features of Cranelift's table support were unused, so while copying parts of Cranelift into this crate I removed those features. Specifically:

  • TableData's min_size and index_type fields
  • table_addr's immediate byte-offset operand

This is a step toward #5532.

@alexcrichton
Copy link
Member

I'm quite excited for this 👍, thanks for taking this on!

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:docs cranelift:meta Everything related to the meta-language. cranelift:wasm labels Mar 8, 2024
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Also excited for this!

crates/cranelift/src/func_environ.rs Show resolved Hide resolved
This CLIF instruction is specific to WebAssembly, and doesn't expose any
optimization opportunities or have any other reason to be treated
specially by Cranelift. So this commit makes Wasmtime emit a series of
simpler Cranelift instructions instead.

I copied the implementation from Cranelift's legalization pass, which
already was rewriting these instructions to simpler ones, and then
simplified the result to not generate the intermediate form at all.

Merging all the table-related code into one place should eventually let
us experiment more with bounds-check elimination tricks, like we've been
doing with heaps.

The filetest changes demonstrate that the new cranelift-wasm
implementation generates the exact same sequence of instructions that
the legalization pass did, except with different value numbers and fewer
aliases.

Several features of Cranelift's table support were unused, so while
copying parts of Cranelift into this crate I removed those features.
Specifically:

- TableData's min_size and index_type fields
- table_addr's immediate byte-offset operand
@jameysharp jameysharp changed the title Stop using tables cranelift-wasm: Stop using table_addr instructions Mar 13, 2024
@jameysharp jameysharp marked this pull request as ready for review March 13, 2024 19:24
@jameysharp jameysharp requested review from a team as code owners March 13, 2024 19:24
@jameysharp jameysharp requested review from fitzgen and removed request for a team March 13, 2024 19:24
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Nice!

Comment on lines +58 to +63
} else if element_size.is_power_of_two() {
pos.ins()
.ishl_imm(index, i64::from(element_size.trailing_zeros()))
} else {
pos.ins().imul_imm(index, element_size as i64)
};
Copy link
Member

Choose a reason for hiding this comment

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

Not 100% convinced it is worth adding the power-of-2 optimization here, since our mid-end rules should definitely already handle this. I think this file should focus on just (a) the correctness of the bounds checks and (b) optimizations to the bounds checks that rely on knowledge of the Wasm table and its limits that the mid-end can't possibly know.

Copy link
Member

Choose a reason for hiding this comment

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

And apologies if tihs already existed. I think we can clean up a tiny bit of complexity here, if that was the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I considered removing that in favor of relying on the corresponding egraph optimizations, but decided to keep it since it was in the original. I'd prefer to land it in this form to avoid bigger changes to the filetests, where I specifically have not turned on the egraph pass so it's more clear what's being generated, but I wouldn't be upset about removing this later.

@jameysharp jameysharp added this pull request to the merge queue Mar 13, 2024
Merged via the queue into bytecodealliance:main with commit 22b8792 Mar 13, 2024
19 checks passed
@jameysharp jameysharp deleted the stop-using-tables branch March 13, 2024 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cranelift:docs 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