Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

contracts: Upgrade to wasmi 0.28 #13312

Merged
merged 6 commits into from
Mar 20, 2023
Merged

contracts: Upgrade to wasmi 0.28 #13312

merged 6 commits into from
Mar 20, 2023

Conversation

athei
Copy link
Member

@athei athei commented Feb 4, 2023

Also upgrading to newest wasmparser to keep up with wasmi.

Performance uplift for instructions execution looks pretty good from wasmi 0.20. As we expected.

Please note that this is without wasm-opt as applying this to the runtime didn't improve performance as a whole (#13408). It also did remove all the benefits of this new wasmi-version. Hence we just merge without wasm-opt.

This is how this PR performed with wasmt-opt vs. no wasm-opt. this is with -O2but -O4 performed the same:

wasm-opt vs no wasm opt

Cumulus companion: paritytech/cumulus#2350

@athei athei added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Feb 4, 2023
@athei athei requested a review from koute as a code owner February 5, 2023 03:21
frame/contracts/src/weights.rs Outdated Show resolved Hide resolved
@koute
Copy link
Contributor

koute commented Feb 6, 2023

Since this is enabling new optimizations in wasm-opt ideally I'd like to run a full burn-in before we consider merging this. Basically hack the WASM executor so that it dynamically runs wasm-opt on every runtime upgrade (so essentially apply it retroactively to every existing on-chain runtime) and see if everything successfully syncs.

I'll set it up on my GCP node.

@koute
Copy link
Contributor

koute commented Feb 6, 2023

Burn-in is running; will probably take a few days to finish:

2023-02-06 08:32:26 ⚙️  Syncing 362.0 bps, target=#16514561 (40 peers), best: #57137 (0x4f91…6757), finalized #57127 (0x560b…089b), ⬇ 268.6kiB/s ⬆ 142.6kiB/s    
2023-02-06 08:32:31 ⚙️  Syncing 353.6 bps, target=#16514561 (40 peers), best: #58905 (0x8eea…203a), finalized #58882 (0x4aeb…beb0), ⬇ 508.6kiB/s ⬆ 156.3kiB/s    
2023-02-06 08:32:36 ⚙️  Syncing 150.6 bps, target=#16514561 (40 peers), best: #59658 (0xeb84…bc63), finalized #59470 (0x7e2c…dfa6), ⬇ 182.1kiB/s ⬆ 133.4kiB/s    
2023-02-06 08:32:41 ⚙️  Syncing  0.0 bps, target=#16514561 (40 peers), best: #59658 (0xeb84…bc63), finalized #59470 (0x7e2c…dfa6), ⬇ 149.0kiB/s ⬆ 150.1kiB/s    
2023-02-06 08:32:46 ⚙️  Syncing  0.0 bps, target=#16514561 (40 peers), best: #59658 (0xeb84…bc63), finalized #59470 (0x7e2c…dfa6), ⬇ 167.2kiB/s ⬆ 168.6kiB/s    
2023-02-06 08:32:51 ⚙️  Syncing  0.0 bps, target=#16514561 (40 peers), best: #59658 (0xeb84…bc63), finalized #59470 (0x7e2c…dfa6), ⬇ 216.7kiB/s ⬆ 218.9kiB/s    
2023-02-06 08:32:56 ⚙️  Syncing  0.0 bps, target=#16514561 (40 peers), best: #59658 (0xeb84…bc63), finalized #59470 (0x7e2c…dfa6), ⬇ 179.8kiB/s ⬆ 181.1kiB/s    
2023-02-06 08:32:59 Optimized WASM blob with opt level 4 in 26.30s: 1177008 -> 1075157    
2023-02-06 08:33:01 ⚙️  Syncing 72.0 bps, target=#16514561 (40 peers), best: #60018 (0xf001…d268), finalized #59904 (0x782a…05ff), ⬇ 189.0kiB/s ⬆ 166.3kiB/s    
2023-02-06 08:33:06 ⚙️  Syncing 371.2 bps, target=#16514561 (40 peers), best: #61874 (0x5851…951a), finalized #61772 (0xd671…ebc5), ⬇ 336.8kiB/s ⬆ 218.3kiB/s    
2023-02-06 08:33:11 ⚙️  Syncing 384.8 bps, target=#16514561 (40 peers), best: #63798 (0xb7e7…abd8), finalized #63489 (0xb1bd…2d3c), ⬇ 326.4kiB/s ⬆ 197.6kiB/s    
2023-02-06 08:33:16 ⚙️  Syncing 348.6 bps, target=#16514561 (40 peers), best: #65541 (0x8caa…515b), finalized #65536 (0x3db2…1579), ⬇ 513.4kiB/s ⬆ 161.9kiB/s    
2023-02-06 08:33:21 ⚙️  Syncing 391.2 bps, target=#16514561 (40 peers), best: #67497 (0x94b8…f05f), finalized #67072 (0x9d70…4481), ⬇ 369.5kiB/s ⬆ 165.9kiB/s    

@Robbepop
Copy link
Contributor

Robbepop commented Feb 6, 2023

Since this is enabling new optimizations in wasm-opt ideally I'd like to run a full burn-in before we consider merging this. Basically hack the WASM executor so that it dynamically runs wasm-opt on every runtime upgrade (so essentially apply it retroactively to every existing on-chain runtime) and see if everything successfully syncs.

I'll set it up on my GCP node.

@koute Can you elaborate on why an update to wasmi enables new optimizations in wasm-opt?

@koute
Copy link
Contributor

koute commented Feb 6, 2023

@koute Can you elaborate on why an update to wasmi enables new optimizations in wasm-opt?

I'm not entirely sure I understand the question. (:

Here's the patch from this PR which changes wasm-opt to actually optimize the runtime blobs we produce:

https://github.com/paritytech/substrate/pull/13312/files#diff-90c7859f5054a6b99320e4e6a8391a6e7d7135e993a40ce451a68c44c471e171R694

@Robbepop
Copy link
Contributor

Robbepop commented Feb 6, 2023

I didn't notice that this PR was also updating wasm-opt besides wasmi. Maybe it would have been better to do both in separate steps to have a better understanding of what exactly caused performance differences in the contracts-pallet if there are any.

@koute
Copy link
Contributor

koute commented Feb 6, 2023

to have a better understanding of what exactly caused performance differences in the contracts-pallet if there are any.

Well, the last time we tried there was visible impact on performance when using wasm-opt, so I'm expecting that it's probably still there.

@athei
Copy link
Member Author

athei commented Feb 6, 2023

I just added it to this PR because I suspected the degradation was from not using wasm-opt (wasmi is sensitive to that). But it was about the benchmarking machine I guess.

Thanks for doing the burn in. If everything goes well I propose that we will remove the change from this PR and put it into a standalone PR.

@athei
Copy link
Member Author

athei commented Feb 7, 2023

I removed the wasm-opt changes from this PR. If your burn-in doesn't show any problems I will make a new PR with only the wasm-opt change and generate a new weight baseline in that PR. Then we can see the performance impact and can decide the next steps.

@koute
Copy link
Contributor

koute commented Feb 7, 2023

I removed the wasm-opt changes from this PR. If your burn-in doesn't show any problems I will make a new PR with only the wasm-opt change and generate a new weight baseline in that PR. Then we can see the performance impact and can decide the next steps.

Sounds good to me. Current progress:

2023-02-07 13:53:29 ⚙️  Syncing 21.6 bps, target=#16514571 (40 peers), best: #7999611 (0x9b7f…8efd), finalized #7999488 (0x8b28…9874), ⬇ 387.1kiB/s ⬆ 186.0kiB/s

@koute
Copy link
Contributor

koute commented Feb 13, 2023

@athei The burn-in finished successfully.

@Robbepop
Copy link
Contributor

In the meantime I released version 0.26.0 featuring fuel metering. So maybe you could insta upgrade in this PR.

@athei
Copy link
Member Author

athei commented Feb 13, 2023

Yeah will do that. There is a queue of PRs that need to go in before as soon as metering is fixed, though.

@athei athei changed the title contracts: Upgrade to wasmi 0.25 contracts: Upgrade to wasmi 0.28 Mar 14, 2023
@athei
Copy link
Member Author

athei commented Mar 18, 2023

Performance uplift without wasm-opt looks nice. Let's try with wasm-opt enabled.

Comment on lines -442 to -444
if matches!(determinism, Determinism::Deterministic) {
contract_module.ensure_no_floating_types()?;
}
Copy link
Member Author

@athei athei Mar 19, 2023

Choose a reason for hiding this comment

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

We no longer need this because the newest wasmparser rejects floating types for us if floats: false. Before it only rejected float instructions.

Copy link
Contributor

@pgherveou pgherveou Mar 20, 2023

Choose a reason for hiding this comment

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

wasmparser rejects floating types for us if floats: false

just curious, where do you define this setting?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just a bit above. Argument to Validator::new_with_features.

@athei
Copy link
Member Author

athei commented Mar 19, 2023

wasm-opt turned out to be regressing the performance of the runtime as a whole and of wasmi specifically (see PR description). Hence we just go ahead with only upgrading wasmi. I rebased the PR to remove all wasm-opt experiments. It is ready for review now.

Copy link
Contributor

@pgherveou pgherveou left a comment

Choose a reason for hiding this comment

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

lgtm

Comment on lines -442 to -444
if matches!(determinism, Determinism::Deterministic) {
contract_module.ensure_no_floating_types()?;
}
Copy link
Contributor

@pgherveou pgherveou Mar 20, 2023

Choose a reason for hiding this comment

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

wasmparser rejects floating types for us if floats: false

just curious, where do you define this setting?

@@ -1214,7 +1165,7 @@ mod tests {
(func (export "deploy"))
)
"#,
Err("use of floating point type in globals is forbidden")
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we want to keep the more specific error message?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't. The function returns a &'static str. We opted for logging the error message to the console instead of returning it. Not optimal. The error handling there is a mess. But the whole thing will become much simpler once we have #13639. Then we can have a hard look on the error handling there.

Copy link
Contributor

@agryaznov agryaznov left a comment

Choose a reason for hiding this comment

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

LGTM

frame/contracts/src/wasm/prepare.rs Outdated Show resolved Hide resolved
@athei
Copy link
Member Author

athei commented Mar 20, 2023

I am doing the update to wasmi 0.29 in a follow up PR. I don't want to re-run benchmarks.

@athei
Copy link
Member Author

athei commented Mar 20, 2023

Okay the rename needed a cumulus companion. Please also approve the trivial companion: paritytech/cumulus#2350

@athei
Copy link
Member Author

athei commented Mar 20, 2023

bot merge

@paritytech-processbot paritytech-processbot bot merged commit 8f0b0f6 into master Mar 20, 2023
@paritytech-processbot paritytech-processbot bot deleted the at/wasmi branch March 20, 2023 23:09
breathx pushed a commit to gear-tech/substrate that referenced this pull request Apr 22, 2023
* Upgrade to wasmi 0.28

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* Update stale comment

* Renamed variants of `Determinism`

* Compile fix

---------

Co-authored-by: command-bot <>
nathanwhit pushed a commit to nathanwhit/substrate that referenced this pull request Jul 19, 2023
* Upgrade to wasmi 0.28

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* Update stale comment

* Renamed variants of `Determinism`

* Compile fix

---------

Co-authored-by: command-bot <>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants