You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pathological time spent in register allocation for large function #3523
Without the PR, wasmtime takes around 15 seconds to compile the wasm. However, if we include the change (removal of the trait) wasmtime takes less than a second to compile. We wonder how these differences can be so big with such a small code change.
Running the script from your repository this file doesn't exist, but when corrected to moonbase_fast_runtime.wasm both modules compile in ~16s for me locally.
Can you detail a bit more what you're doing, what's fast, and what's slow?
When we inserted this change, we realized an increase on the wasm compilation time. So we are trying to monitor the reason why this happens. For that reason why created this PR moonbeam-foundation/moonbeam#989, with the code commented, compiled it and measured its wasm compilation time. This is moonbase_fast
For me the first takes 16 secs, while the latter takes around 1. And I am not able to picture the reason of the difference
FYI, some debugging I've done indicates that we get into one invocation of compile_function() (here) which takes the entire duration of the compilation (that is, if it takes 22 seconds to compile the entire binary, this one job takes 22 seconds itself).
Thanks for the update, I can indeed reproduce the large discrepancy in compile times. What's happening here is that something pathological is being hit in Cranelift, and it's definitely something that we should fix!
Specifically in this wasm file the <xcm_executor::XcmExecutor<Config> as xcm::v2::traits::ExecuteXcm<<Config as xcm_executor::config::Config>::Call>>::execute_xcm_in_credit function takes 16s to compile. In that wasm file it's function index 1198 and the mangled name is _ZN155_$LT$xcm_executor..XcmExecutor$LT$Config$GT$$u20$as$u20$xcm..v2..traits..ExecuteXcm$LT$$LT$Config$u20$as$u20$xcm_executor..config..Config$GT$..Call$GT$$GT$21execute_xcm_in_credit17hf41c2b7a319ac14dE.
Extracting just that one function to a wasm module yields extract.wasm.gz and this can be reproduced locally with wamtime compile extract.wasm taking 16-17s locally. The breakdown I see is:
There are known pitfalls with our register allocator at this time and plans to move to a better register allocator. My guess is that this is likely to fall into that category of "probably will get fixed", but @cfallin do you think it's worthwhile digging in more here to figure out why something is triggering the slowdown here?
@girazoki and @notlesh in the meantime for your specific use case if you'd like to avoid this slowdown I'd recommend taking a look at <xcm_executor::XcmExecutor<Config> as xcm::v2::traits::ExecuteXcm<<Config as xcm_executor::config::Config>::Call>>::execute_xcm_in_credit and trying to simplify it a bit. It's a huge function in the wasm and probably has a ton of stuff #[inline]'d into it which greatly increases the size of the function. It may be the case that adding #[inline(never)] or otherwise removing #[inline] annotations in a few places could improve wasmtime compile times. Nevertheless we should still fix this in Wasmtime, but that's at least a possibility for fixing things in the near future if you're interested.
Reacted by Stephen Shelton, girazoki and libreamartists
This is a slightly smaller minimization with data strings and import strings all removed as well which makes navigating the *.wat a bit easier, and still takes ~16s to compile the one function within.
@alexcrichton That's a good question; ordinarily we'd definitely want to chase something like this, but if the RA2 transition happens soon-ish then the payoff of the time it would take to understand the issue and update the old regalloc, and be confident in the fix, doesn't seem worth it. Especially as many of the details of regalloc.rs have fallen out of my mental L2 cache by now...
These sorts of things do put pressure on the transition, though, so hopefully the moving pieces needed for that will come together soon!
I'm going to close this given the discussion on #4060. The new regalloc probably improves things here but these sorts of outliers are inevitable with Cranelift's design goals.
Activity
alexcrichton commentedon Nov 15, 2021
Running the script from your repository this file doesn't exist, but when corrected to
moonbase_fast_runtime.wasmboth modules compile in ~16s for me locally.Can you detail a bit more what you're doing, what's fast, and what's slow?
peterhuene commentedon Nov 15, 2021
The repro has a bug where it isn't loading the "fast" wasm file (named
moonbase_fast_runtime.wasminstead ofmoonbase_runtime_fast.wasm).With that fixed, I see:
girazoki commentedon Nov 15, 2021
Sorry it was my bad, updated fast. Now you should be able to see the difference, I updated the fast one.
The context is: We have the moonbase runtime https://github.com/PureStake/moonbeam/blob/master/runtime/moonbase/src/lib.rs that adds https://github.com/PureStake/moonbeam/blob/57fdb3f18fe0a48471bfcad2c37d89708a3a274f/runtime/moonbase/src/lib.rs#L1023. This is moonbase_slow
When we inserted this change, we realized an increase on the wasm compilation time. So we are trying to monitor the reason why this happens. For that reason why created this PR moonbeam-foundation/moonbeam#989, with the code commented, compiled it and measured its wasm compilation time. This is moonbase_fast
For me the first takes 16 secs, while the latter takes around 1. And I am not able to picture the reason of the difference
notlesh commentedon Nov 15, 2021
FYI, some debugging I've done indicates that we get into one invocation of
compile_function()(here) which takes the entire duration of the compilation (that is, if it takes 22 seconds to compile the entire binary, this one job takes 22 seconds itself).alexcrichton commentedon Nov 15, 2021
Thanks for the update, I can indeed reproduce the large discrepancy in compile times. What's happening here is that something pathological is being hit in Cranelift, and it's definitely something that we should fix!
Specifically in this wasm file the
<xcm_executor::XcmExecutor<Config> as xcm::v2::traits::ExecuteXcm<<Config as xcm_executor::config::Config>::Call>>::execute_xcm_in_creditfunction takes 16s to compile. In that wasm file it's function index 1198 and the mangled name is_ZN155_$LT$xcm_executor..XcmExecutor$LT$Config$GT$$u20$as$u20$xcm..v2..traits..ExecuteXcm$LT$$LT$Config$u20$as$u20$xcm_executor..config..Config$GT$..Call$GT$$GT$21execute_xcm_in_credit17hf41c2b7a319ac14dE.Extracting just that one function to a wasm module yields extract.wasm.gz and this can be reproduced locally with
wamtime compile extract.wasmtaking 16-17s locally. The breakdown I see is:There are known pitfalls with our register allocator at this time and plans to move to a better register allocator. My guess is that this is likely to fall into that category of "probably will get fixed", but @cfallin do you think it's worthwhile digging in more here to figure out why something is triggering the slowdown here?
@girazoki and @notlesh in the meantime for your specific use case if you'd like to avoid this slowdown I'd recommend taking a look at
<xcm_executor::XcmExecutor<Config> as xcm::v2::traits::ExecuteXcm<<Config as xcm_executor::config::Config>::Call>>::execute_xcm_in_creditand trying to simplify it a bit. It's a huge function in the wasm and probably has a ton of stuff#[inline]'d into it which greatly increases the size of the function. It may be the case that adding#[inline(never)]or otherwise removing#[inline]annotations in a few places could improve wasmtime compile times. Nevertheless we should still fix this in Wasmtime, but that's at least a possibility for fixing things in the near future if you're interested.alexcrichton commentedon Nov 15, 2021
This is a slightly smaller minimization with data strings and import strings all removed as well which makes navigating the
*.wata bit easier, and still takes ~16s to compile the one function within.[-]Huge increase in wasmtime compilation[/-][+]Pathological time spent in register allocation for large function[/+]cfallin commentedon Nov 15, 2021
@alexcrichton That's a good question; ordinarily we'd definitely want to chase something like this, but if the RA2 transition happens soon-ish then the payoff of the time it would take to understand the issue and update the old regalloc, and be confident in the fix, doesn't seem worth it. Especially as many of the details of regalloc.rs have fallen out of my mental L2 cache by now...
These sorts of things do put pressure on the transition, though, so hopefully the moving pieces needed for that will come together soon!
alexcrichton commentedon Nov 15, 2021
@adamrk given your interest on this issue you might be interested in this one as well (detailed here) but if not no worries!
crystalin commentedon Nov 16, 2021
Not sure if that would help, but here are the perf (you can open in https://www.speedscope.app/ ) for when it is loading the wasm: https://drive.google.com/file/d/1LSid_h9vs8LGtbaQ8z41CD3vn4-y4g2U/view?usp=sharing
(Here is where most of the work goes it seems)

adamrk commentedon Nov 26, 2021
Sorry, just noticed this. Yeah, I'll take a look at it.
alexcrichton commentedon Dec 15, 2021
Oh nice, thanks for that! I was able to find the function-in-question with:
which prints out:
which points to function 1276 as the offender here.
Using
wasm-optI got:which I can confirm the extracted wasm file takes ~20s to compile, quite too long!
alexcrichton commentedon Apr 26, 2022
I'm going to close this given the discussion on #4060. The new regalloc probably improves things here but these sorts of outliers are inevitable with Cranelift's design goals.