alexcrichton opened PR #5160 from refactor-compilation
to main
:
This series of commits is an implementation of AOT-compiling components, namely creating a byte-based artifact from a
Component
which can, at a later date, be fed back into Wasmtime. The intention of this support is to match the AOT support forModule
, only with components instead.At a high level the serialized artifact is the same for modules. The artifact is an ELF object file with standard
.text
/data sections as well as Wasmtime-specific sections with bincode-encoded data or otherwise binary-encoded data. The difference with normal modules is that a component squashes together all of its component modules, so the.text
section contains all executable code for all modules contained within. Additionally the.rodata
section contains the concatenation of all module data segments. This enables having one mmap for the entire component, including all modules contained within.Lots of refactoring internally was necessary for this, namely around the end-part of compilation where an object is emitted and loaded into modules/components. The general shape of the
Module
compilation pipeline is now quite similar to theComponent
compilation pipeline. There were two caveats from this implementation, however:
- Modules within a component sharing the same mmap means that the module registration system within stores must now account for modules with overlapping text sections. The
.text()
accessor returns the same slice for multiple modules, meaning there's now a second-level of indirection for maps where we might want frame information from them.- Components can export modules, and in the embedding API this returns an actual
Module
. This module, however, shares the exact same serialized artifact as the component itself, meaning that if theModule
itself were serialized it couldn't be deserialized back into a module. For that reason I've at least for now disabledModule::serialize
dynamically for modules acquired from a component. This should be fixable but it didn't seem worth it at this time.Otherwise this is quite a large change so I've tried to break apart the commits as best I could. Each individual one should pass at least
cargo test
and represents an incremental step towards AOT-compiling components. If desired I can further split out commits into separate PRs as well.As a final note, the route I've chosen here of squashing everything into one object file is notably distinct from previously intended strategies. For example with module linking modules were serialized by concatenating all of their objects together. This was not easily introspectable so my other idea was to use an
ar
archive for storing a concatenation of objects. In the end I settled on the "one object file" approach for a few reasons:
- Using an
ar
format with the alignment requirements for our object files from mmap'ing the original file to an executable mmap was not going to be easy and I never bottomed out this idea.- Each object would have its own alignment requirements which was going to add up to a lot of padding. I was afraid of excessive padding in a component with multiple small modules internally.
- Conceptually to me at least a component encompassing modules felt better represented as one large mmap with one shared allocation between all the types. This commit for example cuts down on the individul number of
Arc
s required and additionally fixes longstanding issues such as only creating oneSignatureCollection
for a component.- Overall it felt easier at the time to implement shared systems for loading all of this and felt like the more reasonable course of action.
Of course the serialization format is just an internal detail of Wasmtime and if necessitated we can change it in the future as well.
alexcrichton edited PR #5160 from refactor-compilation
to main
:
This series of commits is an implementation of AOT-compiling components, namely creating a byte-based artifact from a
Component
which can, at a later date, be fed back into Wasmtime. The intention of this support is to match the AOT support forModule
, only with components instead.At a high level the serialized artifact is the same for modules. The artifact is an ELF object file with standard
.text
/data sections as well as Wasmtime-specific sections with bincode-encoded data or otherwise binary-encoded data. The difference with normal modules is that a component squashes together all of its component modules, so the.text
section contains all executable code for all modules contained within. Additionally the.rodata
section contains the concatenation of all module data segments. This enables having one mmap for the entire component, including all modules contained within.Lots of refactoring internally was necessary for this, namely around the end-part of compilation where an object is emitted and loaded into modules/components. The general shape of the
Module
compilation pipeline is now quite similar to theComponent
compilation pipeline. There were two caveats from this implementation, however:
- Modules within a component sharing the same mmap means that the module registration system within stores must now account for modules with overlapping text sections. The
.text()
accessor returns the same slice for multiple modules, meaning there's now a second-level of indirection for maps where we might want frame information from them.- Components can export modules, and in the embedding API this returns an actual
Module
. This module, however, shares the exact same serialized artifact as the component itself, meaning that if theModule
itself were serialized it couldn't be deserialized back into a module. For that reason I've at least for now disabledModule::serialize
dynamically for modules acquired from a component. This should be fixable but it didn't seem worth it at this time.Otherwise this is quite a large change so I've tried to break apart the commits as best I could. Each individual one should pass at least
cargo test
and represents an incremental step towards AOT-compiling components. If desired I can further split out commits into separate PRs as well.As a final note, the route I've chosen here of squashing everything into one object file is notably distinct from previously intended strategies. For example with module linking modules were serialized by concatenating all of their objects together. This was not easily introspectable so my other idea was to use an
ar
archive for storing a concatenation of objects. In the end I settled on the "one object file" approach for a few reasons:
- Using an
ar
format with the alignment requirements for our object files from mmap'ing the original file to an executable mmap was not going to be easy and I never bottomed out this idea.- Each object would have its own alignment requirements which was going to add up to a lot of padding. I was afraid of excessive padding in a component with multiple small modules internally.
- Conceptually to me at least a component encompassing modules felt better represented as one large mmap with one shared allocation between all the types. This commit for example cuts down on the individul number of
Arc
s required and additionally fixes longstanding issues such as only creating oneSignatureCollection
for a component.- Overall it felt easier at the time to implement shared systems for loading all of this and felt like the more reasonable course of action.
Of course the serialization format is just an internal detail of Wasmtime and if necessitated we can change it in the future as well.
Closes #5119
alexcrichton updated PR #5160 from refactor-compilation
to main
.
alexcrichton updated PR #5160 from refactor-compilation
to main
.
alexcrichton requested fitzgen for a review on PR #5160.
fitzgen created PR review comment:
Not totally clear to me what the motivation for changing this was. Not saying its necessarily wrong or anything but I'm just curious.
fitzgen created PR review comment:
/// Appends a list of compiled functions to an in-memory object.
fitzgen created PR review comment:
I guess its just because we are passing in
.len()
-ish things as input? And isn't necessarily tied to a singleu32
-sized index space anymore?
fitzgen submitted PR review.
fitzgen submitted PR review.
alexcrichton created PR review comment:
Yeah there ended up being a lot of casts around this and having everything be
usize
removed all the casts, so I went ahead and updated this tousize
.
alexcrichton submitted PR review.
alexcrichton updated PR #5160 from refactor-compilation
to main
.
alexcrichton has enabled auto merge for PR #5160.
alexcrichton merged PR #5160.
Last updated: Nov 22 2024 at 17:03 UTC