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: Add a new backend for emitting Pulley bytecode #9089

Merged
merged 8 commits into from
Aug 14, 2024

Conversation

fitzgen
Copy link
Member

@fitzgen fitzgen commented Aug 8, 2024

This commit adds two new backends for Cranelift that emits 32- and 64-bit Pulley
bytecode. The backends are both actually the same, with a common implementation
living in cranelift/codegen/src/isa/pulley_shared. Each backend configures an
ISA flag that determines the pointer size, and lowering inspects this flag's
value when lowering memory accesses.

To avoid multiple ISLE compilation units, and to avoid compiling duplicate
copies of Pulley's generated MInst, I couldn't use MInst as the MachInst
implementation directly. Instead, there is an InstAndKind type that is a
newtype over the generated MInst but which also carries a phantom type
parameter that implements the PulleyTargetKind trait. There are two
implementations of this trait, a 32- and 64-bit version. This is necessary
because there are various static trait methods for the mach backend which we
must implement, and which return the pointer width, but don't have access to any
self. Therefore, we are forced to monomorphize some amount of code. This type
parameter is fairly infectious, and all the "big" backend
types (PulleyBackend<P>, PulleyABICallSite<P>, etc...) are parameterized
over it. Nonetheless, not everything is parameterized over a PulleyTargetKind,
and we manage to avoid duplicate MInst definitions and lowering code.

Note that many methods are still stubbed out with todo!s. It is expected that
we will fill in those implementations as the work on Pulley progresses.


The first commit is #9085; this depends on that PR.

@fitzgen fitzgen requested a review from cfallin August 8, 2024 00:31
@fitzgen fitzgen requested review from a team as code owners August 8, 2024 00:31
@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. isle Related to the ISLE domain-specific language labels Aug 8, 2024
Copy link

github-actions bot commented Aug 8, 2024

Subscribe to Label Action

cc @cfallin, @fitzgen

This issue or pull request has been labeled: "cranelift", "cranelift:area:aarch64", "cranelift:area:machinst", "cranelift:area:x64", "cranelift:meta", "isle"

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

  • cfallin: isle
  • fitzgen: isle

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

Learn more.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Very nice! Great to see this all shape up so well

cranelift/codegen/src/isa/pulley_shared/inst/emit.rs Outdated Show resolved Hide resolved
Comment on lines +468 to +398
if let Ok(offset) = i8::try_from(offset) {
enc::xconst8(sink, dst, offset);
} else if let Ok(offset) = i16::try_from(offset) {
enc::xconst16(sink, dst, offset);
} else if let Ok(offset) = i32::try_from(offset) {
enc::xconst32(sink, dst, offset);
} else {
enc::xconst64(sink, dst, offset);
}
Copy link
Member

Choose a reason for hiding this comment

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

I think I saw this as well in abi.rs as well as lower.isle perhaps, coul the Rust-side parts be deduplicated into a helper?

Copy link
Member Author

Choose a reason for hiding this comment

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

The one in abi.rs is constructing MInst::Xconst* variants while this one is calling differentenc::xconst* functions, so there isn't a great way to dedupe this. We could make something that is abstract enough to handle both but that abstraction won't actually be any more concise, simpler, or easy to understand, unfortunately.

cranelift/codegen/src/isa/pulley_shared/inst/emit.rs Outdated Show resolved Hide resolved
This commit adds two new backends for Cranelift that emits 32- and 64-bit Pulley
bytecode. The backends are both actually the same, with a common implementation
living in `cranelift/codegen/src/isa/pulley_shared`. Each backend configures an
ISA flag that determines the pointer size, and lowering inspects this flag's
value when lowering memory accesses.

To avoid multiple ISLE compilation units, and to avoid compiling duplicate
copies of Pulley's generated `MInst`, I couldn't use `MInst` as the `MachInst`
implementation directly. Instead, there is an `InstAndKind` type that is a
newtype over the generated `MInst` but which also carries a phantom type
parameter that implements the `PulleyTargetKind` trait. There are two
implementations of this trait, a 32- and 64-bit version. This is necessary
because there are various static trait methods for the mach backend which we
must implement, and which return the pointer width, but don't have access to any
`self`. Therefore, we are forced to monomorphize some amount of code. This type
parameter is fairly infectious, and all the "big" backend
types (`PulleyBackend<P>`, `PulleyABICallSite<P>`, etc...) are parameterized
over it. Nonetheless, not everything is parameterized over a `PulleyTargetKind`,
and we manage to avoid duplicate `MInst` definitions and lowering code.

Note that many methods are still stubbed out with `todo!`s. It is expected that
we will fill in those implementations as the work on Pulley progresses.
@fitzgen fitzgen enabled auto-merge August 14, 2024 18:19
@fitzgen fitzgen added this pull request to the merge queue Aug 14, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 14, 2024
@fitzgen fitzgen enabled auto-merge August 14, 2024 19:56
@fitzgen fitzgen added this pull request to the merge queue Aug 14, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 14, 2024
@fitzgen fitzgen enabled auto-merge August 14, 2024 21:04
@fitzgen fitzgen added this pull request to the merge queue Aug 14, 2024
Merged via the queue into bytecodealliance:main with commit b526865 Aug 14, 2024
69 checks passed
@fitzgen fitzgen deleted the pulley-cranelift-backend branch August 14, 2024 21:32
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 isle Related to the ISLE domain-specific language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants