Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SignextLowering pass to wasm-opt #1280

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions crates/build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,6 @@ fn exec_cargo_for_onchain_target(
env.push(("RUSTC_BOOTSTRAP", Some("1".to_string())))
}

// newer rustc versions might emit unsupported instructions
if rustc_version::version_meta()?.semver >= Version::new(1, 70, 0) {
eprintln!(
"{} {}",
"warning:".yellow().bold(),
"You are using a rustc version that might emit unsupported wasm instructions. Build using a version lower than 1.70.0 to make sure your contract will deploy."
.bold()
);
}

// merge target specific flags with the common flags (defined here)
// We want to disable warnings here as they will be duplicates of the clippy pass.
// However, if we want to do so with either `--cap-lints allow` or `-A
Expand Down
8 changes: 7 additions & 1 deletion crates/build/src/wasm_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use anyhow::Result;
use wasm_opt::{
Feature,
OptimizationOptions,
Pass,
};

use std::{
Expand Down Expand Up @@ -68,9 +69,14 @@ impl WasmOptHandler {
);

OptimizationOptions::from(self.optimization_level)
// Since rustc 1.70 `SignExt` can't be disabled anymore. Hence we have to allow it.
.mvp_features_only()
// Since rustc 1.70 `SignExt` can't be disabled anymore. Hence we have to allow it,
// in order that the Wasm binary containing these instructions can be loaded.
.enable_feature(Feature::SignExt)
// This pass will then remove any `signext` instructions in order that the resulting
// Wasm binary is compatible with older versions of `pallet-contracts` which do not
// support the `signext` instruction.
.add_pass(Pass::SignextLowering)
Copy link
Contributor

Choose a reason for hiding this comment

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

Since paritytech/substrate#14565 we do allow sign_extension in smart contracts on the pallet side. Shouldn't we make this pass optional for the cargo-contract so that the users of the newer versions of pallet-contracts could still opt in to have signext instructions in their contracts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am aware, and I did consider making this optional. I decided against it because removing them by default means that both old and new versions of pallet_contracts will be supported with this, so will make users and our lives easier if the resulting wasm is compatible with as wide a range of target nodes as possible.

I assume the majority of node implementations currently are using an older version of pallet_contracts which does not support signext.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Could adding smth like --allow-signext to the cargo-contract contract build so that wasm_opt runs without the pass, be an option then?

Copy link
Collaborator Author

@ascjones ascjones Aug 17, 2023

Choose a reason for hiding this comment

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

Yes, it would be preferable to do that if the user really does want signext instructions.

It would be interesting to do some analysis to the benefits or otherwise of contracts with or without the signext lowering pass. Could check for differences in contract sizes and gas efficiency.

// the memory in our module is imported, `wasm-opt` needs to be told that
// the memory is initialized to zeroes, otherwise it won't run the
// memory-packing pre-pass.
Expand Down