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

Convert TrapCode to a single byte #9338

Merged

Conversation

alexcrichton
Copy link
Member

This commit refactors the representation of
cranelift_codegen::ir::TrapCode to be a single byte. The previous enumeration is replaced with an opaque byte-sized structure. Previous variants that Cranelift uses internally are now associated const values on TrapCode itself. For example TrapCode::IntegerOverflow is now TrapCode::INTEGER_OVERFLOW. All non-Cranelift traps are now removed and exclusively live in the wasmtime-cranelift crate now.

The representation of a TrapCode is now:

  • 0 - invalid, used in MemFlags for "no trap code"
  • 1..256-N - user traps
  • 256-N..256 - built-in Cranelift traps (it uses N of these)

This enables embedders to have 255-N trap codes which is more than enough for Wasmtime for example. Cranelift reserves a few built-in codes for itself which shouldn't eat too much into the trap space. Additionally if Cranelift needs to grow a new trap it can do so pretty easily too.

The overall intent of this commit is to reduce the coupling of Wasmtime and Cranelift further and generally refactor Wasmtime to use user traps more often. This additionally shrinks the size of TrapCode for storage in various locations, notably it can now infallibly be represented inside of a MemFlags.

Closes #9310

@alexcrichton alexcrichton requested review from a team as code owners September 30, 2024 21:46
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team September 30, 2024 21:46
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. labels Oct 1, 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.

Nice! A few nitpicks inline below.

cranelift/codegen/src/ir/memflags.rs Outdated Show resolved Hide resolved
Comment on lines 369 to +376
// This is the default trap code, so don't print anything extra
// for this.
Some(TrapCode::HeapOutOfBounds) => {}
Some(TrapCode::HEAP_OUT_OF_BOUNDS) => {}
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR, but it seems like we should print heap oob traps, since it is possible to not have any traps...

Copy link
Member Author

Choose a reason for hiding this comment

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

Lack of trap though prints as notrap, so I think we're good?

cranelift/codegen/src/ir/trapcode.rs Outdated Show resolved Hide resolved
cranelift/codegen/src/ir/trapcode.rs Outdated Show resolved Hide resolved
crates/cranelift/src/lib.rs Outdated Show resolved Hide resolved
@alexcrichton alexcrichton added this pull request to the merge queue Oct 1, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 1, 2024
This commit refactors the representation of
`cranelift_codegen::ir::TrapCode` to be a single byte. The previous
enumeration is replaced with an opaque byte-sized structure. Previous
variants that Cranelift uses internally are now associated `const`
values on `TrapCode` itself. For example `TrapCode::IntegerOverflow` is
now `TrapCode::INTEGER_OVERFLOW`. All non-Cranelift traps are now
removed and exclusively live in the `wasmtime-cranelift` crate now.

The representation of a `TrapCode` is now:

* 0 - invalid, used in `MemFlags` for "no trap code"
* 1..256-N - user traps
* 256-N..256 - built-in Cranelift traps (it uses N of these)

This enables embedders to have 255-N trap codes which is more than
enough for Wasmtime for example. Cranelift reserves a few built-in codes
for itself which shouldn't eat too much into the trap space.
Additionally if Cranelift needs to grow a new trap it can do so pretty
easily too.

The overall intent of this commit is to reduce the coupling of Wasmtime
and Cranelift further and generally refactor Wasmtime to use user traps
more often. This additionally shrinks the size of `TrapCode` for storage
in various locations, notably it can now infallibly be represented
inside of a `MemFlags`.

Closes bytecodealliance#9310
@alexcrichton alexcrichton added this pull request to the merge queue Oct 1, 2024
Merged via the queue into bytecodealliance:main with commit 9fc41ba Oct 1, 2024
71 checks passed
@alexcrichton alexcrichton deleted the make-trap-just-a-byte branch October 1, 2024 18:22
alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request Oct 3, 2024
This commit fixes an issue found on OSS-Fuzz for the cranelift-fuzzgen
fuzzer which was caused by bytecodealliance#9338 but that PR forgot to fix.
github-merge-queue bot pushed a commit that referenced this pull request Oct 3, 2024
This commit fixes an issue found on OSS-Fuzz for the cranelift-fuzzgen
fuzzer which was caused by #9338 but that PR forgot to fix.
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:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. cranelift Issues related to the Cranelift code generator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace cranelift_codegen::ir::TrapCode with a u8
2 participants