mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 00:01:09 +00:00
Initial support for building RISC-V runtimes targeting PolkaVM (#3179)
This PR adds initial support for building RISC-V runtimes targeting PolkaVM. - Setting the `SUBSTRATE_RUNTIME_TARGET=riscv` environment variable will now build a RISC-V runtime instead of a WASM runtime. - This only adds support for *building* runtimes; running them will need a PolkaVM-based executor, which I will add in a future PR. - Only building the minimal runtime is supported (building the Polkadot runtime doesn't work *yet* due to one of the dependencies). - The builder now sets a `substrate_runtime` cfg flag when building the runtimes, with the idea being that instead of doing `#[cfg(not(feature = "std"))]` or `#[cfg(target_arch = "wasm32")]` to detect that we're building a runtime you'll do `#[cfg(substrate_runtime)]`. (Switching the whole codebase to use this will be done in a future PR; I deliberately didn't do this here to keep this PR minimal and reviewable.) - Further renaming of things (e.g. types, environment variables and proc macro attributes having "wasm" in their name) to be target-agnostic will also be done in a future refactoring PR (while keeping backwards compatibility where it makes sense; I don't intend to break anyone's workflow or create unnecessary churn). - This PR also fixes two bugs in the `wasm-builder` crate: * The `RUSTC` environment variable is now removed when invoking the compiler. This prevents the toolchain version from being overridden when called from a `build.rs` script. * When parsing the `rustup toolchain list` output the `(default)` is now properly stripped and not treated as part of the version. - I've also added a minimal CI job that makes sure this doesn't break in the future. (cc @paritytech/ci) cc @athei ------ Also, just a fun little tidbit: quickly comparing the size of the built runtimes it seems that the PolkaVM runtime is slightly smaller than the WASM one. (`production` build, with the `names` section substracted from the WASM's size to keep things fair, since for the PolkaVM runtime we're currently stripping out everything) - `.wasm`: 625505 bytes - `.wasm` (after wasm-opt -O3): 563205 bytes - `.wasm` (after wasm-opt -Os): 562987 bytes - `.wasm` (after wasm-opt -Oz): 536852 bytes - `.polkavm`: ~~580338 bytes~~ 550476 bytes (after enabling extra target features; I'll add those in another PR once we have an executor working) --------- Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
@@ -1737,20 +1737,20 @@ mod tracing_setup {
|
||||
|
||||
pub use tracing_setup::init_tracing;
|
||||
|
||||
/// Allocator used by Substrate when executing the Wasm runtime.
|
||||
#[cfg(all(target_arch = "wasm32", not(feature = "std")))]
|
||||
struct WasmAllocator;
|
||||
/// Allocator used by Substrate from within the runtime.
|
||||
#[cfg(substrate_runtime)]
|
||||
struct RuntimeAllocator;
|
||||
|
||||
#[cfg(all(target_arch = "wasm32", not(feature = "disable_allocator"), not(feature = "std")))]
|
||||
#[cfg(all(not(feature = "disable_allocator"), substrate_runtime))]
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: WasmAllocator = WasmAllocator;
|
||||
static ALLOCATOR: RuntimeAllocator = RuntimeAllocator;
|
||||
|
||||
#[cfg(all(target_arch = "wasm32", not(feature = "std")))]
|
||||
#[cfg(substrate_runtime)]
|
||||
mod allocator_impl {
|
||||
use super::*;
|
||||
use core::alloc::{GlobalAlloc, Layout};
|
||||
|
||||
unsafe impl GlobalAlloc for WasmAllocator {
|
||||
unsafe impl GlobalAlloc for RuntimeAllocator {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
allocator::malloc(layout.size() as u32)
|
||||
}
|
||||
@@ -1761,8 +1761,27 @@ mod allocator_impl {
|
||||
}
|
||||
}
|
||||
|
||||
/// A default panic handler for WASM environment.
|
||||
#[cfg(all(not(feature = "disable_panic_handler"), not(feature = "std")))]
|
||||
/// Crashes the execution of the program.
|
||||
///
|
||||
/// Equivalent to the WASM `unreachable` instruction, RISC-V `unimp` instruction,
|
||||
/// or just the `unreachable!()` macro everywhere else.
|
||||
pub fn unreachable() -> ! {
|
||||
#[cfg(target_family = "wasm")]
|
||||
{
|
||||
core::arch::wasm32::unreachable();
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
|
||||
unsafe {
|
||||
core::arch::asm!("unimp", options(noreturn));
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_arch = "riscv32", target_arch = "riscv64", target_family = "wasm")))]
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
/// A default panic handler for the runtime environment.
|
||||
#[cfg(all(not(feature = "disable_panic_handler"), substrate_runtime))]
|
||||
#[panic_handler]
|
||||
#[no_mangle]
|
||||
pub fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||
@@ -1774,11 +1793,11 @@ pub fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||
#[cfg(not(feature = "improved_panic_error_reporting"))]
|
||||
{
|
||||
logging::log(LogLevel::Error, "runtime", message.as_bytes());
|
||||
core::arch::wasm32::unreachable();
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
/// A default OOM handler for WASM environment.
|
||||
/// A default OOM handler for the runtime environment.
|
||||
#[cfg(all(not(feature = "disable_oom"), enable_alloc_error_handler))]
|
||||
#[alloc_error_handler]
|
||||
pub fn oom(_: core::alloc::Layout) -> ! {
|
||||
@@ -1789,7 +1808,7 @@ pub fn oom(_: core::alloc::Layout) -> ! {
|
||||
#[cfg(not(feature = "improved_panic_error_reporting"))]
|
||||
{
|
||||
logging::log(LogLevel::Error, "runtime", b"Runtime memory exhausted. Aborting");
|
||||
core::arch::wasm32::unreachable();
|
||||
unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user