Skip to content

winch: Refactoring wasmtime compiler integration pieces to share more between Cranelift and Winch#5944

Merged
alexcrichton merged 18 commits into
bytecodealliance:mainfrom
KevinRizzoTO:compiler-refactor
Mar 8, 2023
Merged

winch: Refactoring wasmtime compiler integration pieces to share more between Cranelift and Winch#5944
alexcrichton merged 18 commits into
bytecodealliance:mainfrom
KevinRizzoTO:compiler-refactor

Conversation

@KevinRizzoTO
Copy link
Copy Markdown
Contributor

As discussed with @saulecabrera and @alexcrichton, this PR adds in some refactors that will make the initial Winch integration with wasmtime smoother. At a high level:

  • move shared code into the new wasmtime-cranelift-shared crate, which will provide a temporary location to put shared code between Winch and Cranelift while we see what patterns need to be abtracted
  • combine some Isa operations under a shared IsaBuilder trait
  • have the enable_incremental_compilation method on the wasmtime_environ::CompilerBuilder trait return a Result so the implementation can decide if it's supported or not

Big thanks to @alexcrichton for helping out here! I ended up using a lot of his first pass (compared to my much messier version 😛).

Comment thread cranelift/codegen/shared/Cargo.toml Outdated
Comment thread cranelift/codegen/src/isa/mod.rs
@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:x64 Issues related to x64 codegen cranelift:meta Everything related to the meta-language. wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime winch Winch issues or pull requests labels Mar 7, 2023
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 7, 2023

Subscribe to Label Action

cc @peterhuene, @saulecabrera

Details This issue or pull request has been labeled: "cranelift", "cranelift:area:machinst", "cranelift:area:x64", "cranelift:meta", "wasmtime:api", "wasmtime:config", "winch"

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

  • peterhuene: wasmtime:api
  • saulecabrera: winch

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

Learn more.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 7, 2023

Label Messager: wasmtime:config

It looks like you are changing Wasmtime's configuration options. Make sure to
complete this check list:

  • If you added a new Config method, you wrote extensive documentation for
    it.

    Details

    Our documentation should be of the following form:

    Short, simple summary sentence.
    
    More details. These details can be multiple paragraphs. There should be
    information about not just the method, but its parameters and results as
    well.
    
    Is this method fallible? If so, when can it return an error?
    
    Can this method panic? If so, when does it panic?
    
    # Example
    
    Optional example here.
    
  • If you added a new Config method, or modified an existing one, you
    ensured that this configuration is exercised by the fuzz targets.

    Details

    For example, if you expose a new strategy for allocating the next instance
    slot inside the pooling allocator, you should ensure that at least one of our
    fuzz targets exercises that new strategy.

    Often, all that is required of you is to ensure that there is a knob for this
    configuration option in wasmtime_fuzzing::Config (or one
    of its nested structs).

    Rarely, this may require authoring a new fuzz target to specifically test this
    configuration. See our docs on fuzzing for more details.

  • If you are enabling a configuration option by default, make sure that it
    has been fuzzed for at least two weeks before turning it on by default.


Details

To modify this label's message, edit the .github/label-messager/wasmtime-config.md file.

To add new label messages or remove existing label messages, edit the
.github/label-messager.json configuration file.

Learn more.

Copy link
Copy Markdown
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.

It's been awhile since I drafted this but IIRC I got to a point where I started to question the existence of wasmtime-cranelift-shared since so much ended up being shared. Namely I originally thought that the CompiledFunction structure would be wasmtime-cranelift-specific but in the end I think I ended up concluding that winch would want to use CompiledFunction as-is. Either that or I think CompiledFunction could be almost entirely wholesale replaced with MachBuffer's result of compilation. It's been a bit though so I forget...

In any case though this PR as-is seems fine. I'm a little worried about the extent of boilerplate required for winch which is backed by Cranelift but it's not really a technical issue and it's always something that can be burned down later with more refactoring. I think it'd be ok to land this and continue to refactor in-tree as necessary, especially since I don't think there's a good picture on where all the refactoring will end up anyway.

Comment thread cranelift/codegen/shared/Cargo.toml Outdated
Comment thread cranelift/codegen/src/isa/mod.rs
alexcrichton and others added 14 commits March 7, 2023 11:50
Match cranelift-codegen's build script where if no architecture is
explicitly enabled then the host architecture is implicitly enabled.
This commit refactors the `Builder` type to have a type parameter
representing the finished ISA with Cranelift and Winch having their own
typedefs for `Builder` to represent their own builders. The intention is
to use this shared functionality to produce more shared code between the
two codegen backends.
This fixes an oversight from the previous commits to use
`cranelift-native` to infer flags for the native host when using default
settings with Wasmtime.
The `cranelift-codegen` crate doesn't need this and winch wants the same
implementation, so shuffle it around so everyone has access to it.
These are easy enough to plumb through with some shared code for
Wasmtime.
Just forwarding an isa-specific setting accessor.
I decided to move a couple things around from Alex's initial changes.
Instead of having the shared builder do everything, I went back to
having each compiler have a distinct builder implementation. I
refactored most of the flag setting logic into a single shared location,
so we can still reduce the amount of code duplication.

With them being separate, we don't need to maintain things like
`LinkOpts` which Winch doesn't currently use. We also have an avenue to
error when certain flags are sent to Winch if we don't support them. I'm
hoping this will make things more maintainable as we build out Winch.

I'm still unsure about keeping everything shared in a single crate
(`cranelift_shared`). It's starting to feel like this crate is doing too
much, which makes it difficult to name. There does seem to be a need for
two distinct abstraction: creating the final executable and the handling
of shared/ISA flags when building the compiler. I could make them into
two separate crates, but there doesn't seem to be enough there yet to
justify it.
@KevinRizzoTO KevinRizzoTO force-pushed the compiler-refactor branch 2 times, most recently from 60b6947 to 8c8e297 Compare March 7, 2023 17:19
Copy link
Copy Markdown
Member

@saulecabrera saulecabrera left a comment

Choose a reason for hiding this comment

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

The winch pieces look good to me. I left a couple of minor suggestions.

Initially (as discussed this with @KevinRizzoTO offline) I was a bit worried about having a crate without a defined scope, as cranelift-shared seems a bit broad to me, but given that we don't have a full picture of how the refactoring is going to play out I agree that we can iterate in-tree and potentially refine this approach as we build more of the integration pieces.

I'm going to defer the final approval to @alexcrichton once the builder conversation gets resolved.

Comment thread cranelift/codegen/src/isa/mod.rs Outdated
Comment thread winch/codegen/src/isa/x64/mod.rs Outdated
Comment thread winch/codegen/src/isa/mod.rs Outdated
Comment thread winch/codegen/src/isa/mod.rs Outdated
Comment thread winch/codegen/src/isa/mod.rs Outdated
Comment thread winch/codegen/src/isa/mod.rs Outdated
Comment thread winch/codegen/src/isa/aarch64/mod.rs Outdated
Comment thread crates/environ/src/compilation.rs Outdated
@KevinRizzoTO KevinRizzoTO force-pushed the compiler-refactor branch 4 times, most recently from 0f02af0 to 576de72 Compare March 7, 2023 19:51
Comment on lines 104 to 112
pub fn append_func(
&mut self,
name: &str,
func: &'a CompiledFunction,
body: &[u8],
alignment: u32,
unwind_info: Option<&'a UnwindInfo>,
relocations: &[Relocation],
resolve_reloc_target: impl Fn(FuncIndex) -> usize,
) -> (SymbolId, Range<u64>) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not something that needs to be handled here in this PR, but in the future this will need to be invoked by winch and I think that most of the arguments here can simply become the output of MachBuffer's compilation step since I think it's largely all packaged in there already (except maybe alignment IIRC). Just something to keep in mind though as more of the winch bits are filled out.

Comment thread crates/cranelift/src/builder.rs
KevinRizzoTO and others added 2 commits March 7, 2023 15:17
Copy link
Copy Markdown
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.

My guess is that this will continue to be iterated on as winch evolves over time and we see how it shapes up, but I think this is an ok interim point to reach to get the ball rolling on integrating winch into Wasmtime.

@alexcrichton alexcrichton added this pull request to the merge queue Mar 7, 2023
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 7, 2023
Comment thread scripts/publish.rs
"wasmtime-fiber",
"wasmtime-environ",
"wasmtime-runtime",
"wasmtime-cranelift-shared",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@alexcrichton @saulecabrera let me know if this works to add this here to pass merge queue CI, or if we should be adding publish = false to the new Cargo.toml file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah this is the expected fix

@alexcrichton alexcrichton enabled auto-merge March 7, 2023 22:50
@alexcrichton alexcrichton added this pull request to the merge queue Mar 7, 2023
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 7, 2023
@KevinRizzoTO
Copy link
Copy Markdown
Contributor Author

@alexcrichton Put in another fix for the last failure, hopefully all good this time 🤞

@alexcrichton alexcrichton added this pull request to the merge queue Mar 8, 2023
Merged via the queue into bytecodealliance:main with commit 013b35f Mar 8, 2023
saulecabrera added a commit to saulecabrera/wasmtime that referenced this pull request Apr 27, 2023
This change introduces handling of traps and relocations in Winch, which
was left out in bytecodealliance#6119.

In order to so, this change moves the `CompiledFunction` struct to the
`wasmtime-cranelift-shared` crate, allowing Cranelift and Winch to
operate on a single, shared representation, following some of the ideas
discussed in bytecodealliance#5944.

Even though Winch doesn't rely on all the fields of `CompiledFunction`,
it eventually will. With the addition of relocations and traps it
started to be more evident that even if we wanted to have different
representations of a compiled function, they would end up being very
similar.

This change also consolidates where the `traps` and `relocations` of the
`CompiledFunction` get created, by introducing a constructor that
operates on a `MachBufferFinalized<Final>`, esentially encapsulating
this process in a single place for both Winch and Cranelift.
saulecabrera added a commit to saulecabrera/wasmtime that referenced this pull request Apr 27, 2023
This change introduces handling of traps and relocations in Winch, which
was left out in bytecodealliance#6119.

In order to so, this change moves the `CompiledFunction` struct to the
`wasmtime-cranelift-shared` crate, allowing Cranelift and Winch to
operate on a single, shared representation, following some of the ideas
discussed in bytecodealliance#5944.

Even though Winch doesn't rely on all the fields of `CompiledFunction`,
it eventually will. With the addition of relocations and traps it
started to be more evident that even if we wanted to have different
representations of a compiled function, they would end up being very
similar.

This change also consolidates where the `traps` and `relocations` of the
`CompiledFunction` get created, by introducing a constructor that
operates on a `MachBufferFinalized<Final>`, esentially encapsulating
this process in a single place for both Winch and Cranelift.
saulecabrera added a commit to saulecabrera/wasmtime that referenced this pull request May 1, 2023
This change introduces handling of traps and relocations in Winch, which
was left out in bytecodealliance#6119.

In order to so, this change moves the `CompiledFunction` struct to the
`wasmtime-cranelift-shared` crate, allowing Cranelift and Winch to
operate on a single, shared representation, following some of the ideas
discussed in bytecodealliance#5944.

Even though Winch doesn't rely on all the fields of `CompiledFunction`,
it eventually will. With the addition of relocations and traps it
started to be more evident that even if we wanted to have different
representations of a compiled function, they would end up being very
similar.

This change also consolidates where the `traps` and `relocations` of the
`CompiledFunction` get created, by introducing a constructor that
operates on a `MachBufferFinalized<Final>`, esentially encapsulating
this process in a single place for both Winch and Cranelift.
saulecabrera added a commit that referenced this pull request May 2, 2023
* winch: Handle relocations and traps

This change introduces handling of traps and relocations in Winch, which
was left out in #6119.

In order to so, this change moves the `CompiledFunction` struct to the
`wasmtime-cranelift-shared` crate, allowing Cranelift and Winch to
operate on a single, shared representation, following some of the ideas
discussed in #5944.

Even though Winch doesn't rely on all the fields of `CompiledFunction`,
it eventually will. With the addition of relocations and traps it
started to be more evident that even if we wanted to have different
representations of a compiled function, they would end up being very
similar.

This change also consolidates where the `traps` and `relocations` of the
`CompiledFunction` get created, by introducing a constructor that
operates on a `MachBufferFinalized<Final>`, esentially encapsulating
this process in a single place for both Winch and Cranelift.

* Rework the shared `CompiledFunction`

This commit reworks the shared `CompiledFunction` struct. The compiled
function now contains the essential pieces to derive all the information
to create the final object file and to derive the debug information for
the function.

This commit also decouples the dwarf emission process by introducing
a `metadata` field in the `CompiledFunction` struct, which is used as
the central structure for dwarf emission.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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 wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime winch Winch issues or pull requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants