wasmtime
wasmtime copied to clipboard
Implement an incremental compilation cache for Cranelift
This is the implementation of https://github.com/bytecodealliance/wasmtime/issues/4155, using the "inverted API" approach suggested by @cfallin (thanks!) in Cranelift, and trait object to provide a backend for an all-included experience in Wasmtime.
After the suggestion of Chris, Function has been split into mostly two parts:
- on the one hand,
FunctionStencilcontains all the fields required during compilation, and that act as a compilation cache key: if two function stencils are the same, then the result of their compilation (CompiledCodeBase<Stencil>) will be the same. This makes caching trivial, as the only thing to cache is theFunctionStencil. - on the other hand,
FunctionParameterscontain the... function parameters that are required to finalize the result of compilation into aCompiledCode(akaCompiledCodeBase<Final>) with proper final relocations etc., by applying fixups and so on.
Most changes are here to accomodate those requirements, in particular that FunctionStencil should be Hashable to be used as a key in the cache:
- most source locations are now relative to a base source location in the function, and as such they're encoded as
RelSourceLocin theFunctionStencil. This required changes so that there's no need to explicitly mark aSourceLocas the base source location, it's automatically detected instead the first time a non-defaultSourceLocis set. - user-defined external names in the
FunctionStencil(aka before this patchExternalName::User { namespace, index }) are now references into an external table ofUserExternalNameRef -> UserExternalName, present in theFunctionParameters, and must be explicitly declared usingFunction::declare_imported_user_function. - some refactorings have been made for function names:
ExternalNamewas used as the type for aFunction's name; while it thus allowedExternalName::Libcallin this place, this would have been quite confusing to use it there. Instead, a new enumUserFuncNameis introduced for this name, that's either a user-defined function name (the aboveUserExternalName) or a test case name.- The future of
ExternalNameis likely to become a full reference into theFunctionParameters's mapping, instead of being "either a handle for user-defined external names, or the thing itself for other variants". I'm running out of time to do this, and this is not trivial as it implies touching ISLE which I'm less familiar with.
The cache computes a sha256 hash of the FunctionStencil, and uses this as the cache key. No equality check (using PartialEq) is performed in addition to the hash being the same, as we hope that this is sufficient data to avoid collisions.
A basic fuzz target has been introduced that tries to do the bare minimum:
- check that a function successfully compiled and cached will be also successfully reloaded from the cache, and returns the exact same function.
- check that a trivial modification in the external mapping of
UserExternalNameRef -> UserExternalNamehits the cache, and that other modifications don't hit the cache.- This last check is less efficient and less likely to happen, so probably should be rethought a bit.
Thanks to both @alexcrichton and @cfallin for your very useful feedback on Zulip.
Some numbers show that for a large wasm module we're using internally, this is a 20% compile-time speedup, because so many FunctionStencils are the same, even within a single module. For a group of modules that have a lot of code in common, we get hit rates up to 70% when they're used together. When a single function changes in a wasm module, every other function is reloaded; that's still slower than I expect (between 10% and 50% of the overall compile time), so there's likely room for improvement.
Fixes #4155.
Thanks all for the first batch of reviews!
@cfallin this makes sense to me. At this point there's not much that needs those relocations, if I'm not mistaken; mostly things that weren't trivially Hashables, or source locations that were absolute, or external names being embedded and copied around. I'm looking into this.
I've updated the initial text of this PR, and while not ideal/perfect, I claim this is ready for review and could be merged as a first step. Remaining things to do as follow-ups, either myself in my copious free time, or other interested contributors :heart_eyes: :
- fuzzing could be improved, to generate an instruction that could serve as replacement of another instruction. This would likely also require a position where to put this instruction, and some care ensuring that the instruction is compatible (same number of input/returns + same types for all inputs/returns)
- there's a proliferation of
Nametypes that could potentially be unified, maybe? ExternalNamecould just become a reference into theFunctionParametersmapping of reference to actual name value, as (it's likely, not proven that) codegen doesn't care about the identity of the function callee. SoExternalNamewould become a new entity type, and there'd beExternalNameDatain the table. Might not be true for libcalls which identity may matter during codegen, so may require thatExternalNamebecomes a sum type: either a reference into theFunctionParameter's table, or theLibcallembedded itself.- right now, if a
UserExternalNameRefchanges (that is, a function call switches the callee identity), then it changes the identity of theFunctionStencil, thus resulting in a cache miss; this could likely be refactored and improved, enabling more cache hits. - optimization work could be done to identify why a recompilation is, in the worst case, only 50% faster than compiling from scratch (it should be much higher in theory).
@cfallin This new version incorporates changes to address your review feedback during the live review session (that we organized to help going through the changes, as this is a large patch). This is now ready and passes cargo test --all on my machine, with no performance regression on compile times, so I would claim this is ready :-) Cheers!
I just merged in main to this PR to see if it fixes CI.
Hmm, @bnjbvr it looks like some of the changes broke the fuzz targets -- happy to merge once that is fixed up.