Skip to content

Commit

Permalink
Merge branch 'main' into cl/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
xermicus committed Nov 29, 2024
2 parents 8177821 + 423a494 commit 1fcd5a4
Show file tree
Hide file tree
Showing 22 changed files with 623 additions and 690 deletions.
1,057 changes: 525 additions & 532 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ path-slash = "0.2"
rayon = "1.8"
clap = { version = "4", default-features = false, features = ["derive"] }
rand = "0.8"
polkavm-common = "0.14"
polkavm-linker = "0.14"
polkavm-disassembler = "0.14"
polkavm = "0.14"
polkavm-common = "0.17"
polkavm-linker = "0.17"
polkavm-disassembler = "0.17"
polkavm = "0.17"
alloy-primitives = { version = "0.8", features = ["serde"] }
alloy-sol-types = "0.8"
alloy-genesis = "0.3"
Expand All @@ -67,7 +67,7 @@ log = { version = "0.4" }
# polkadot-sdk and friends
codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" }
scale-info = { version = "2.11.1", default-features = false }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "0449b214accd0f0fbf7ea3e8f3a8d8b7f99445e4" }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "447902eff4a574e66894ad60cb41999b05bf5e84" }

# llvm
[workspace.dependencies.inkwell]
Expand Down
56 changes: 28 additions & 28 deletions crates/benchmarks/BENCHMARKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,56 @@

| | `EVM` | `PVMInterpreter` |
|:--------|:------------------------|:-------------------------------- |
| **`0`** | `5.97 us` (✅ **1.00x**) | `27.04 us` (❌ *4.53x slower*) |
| **`0`** | `3.36 us` (✅ **1.00x**) | `11.84 us` (❌ *3.52x slower*) |

### OddPorduct

| | `EVM` | `PVMInterpreter` |
|:-------------|:--------------------------|:-------------------------------- |
| **`10000`** | `4.26 ms` (✅ **1.00x**) | `2.88 ms` ( **1.48x faster**) |
| **`100000`** | `42.37 ms` (✅ **1.00x**) | `28.35 ms` ( **1.49x faster**) |
| **`300000`** | `127.88 ms` (✅ **1.00x**) | `88.43 ms` ( **1.45x faster**) |
| | `EVM` | `PVMInterpreter` |
|:-------------|:-------------------------|:-------------------------------- |
| **`10000`** | `3.11 ms` (✅ **1.00x**) | `1.53 ms` (🚀 **2.03x faster**) |
| **`100000`** | `30.70 ms` (✅ **1.00x**) | `15.54 ms` (🚀 **1.98x faster**) |
| **`300000`** | `92.68 ms` (✅ **1.00x**) | `45.47 ms` (🚀 **2.04x faster**) |

### TriangleNumber

| | `EVM` | `PVMInterpreter` |
|:-------------|:--------------------------|:-------------------------------- |
| **`10000`** | `2.85 ms` (✅ **1.00x**) | `2.37 ms` ( **1.20x faster**) |
| **`100000`** | `27.85 ms` (✅ **1.00x**) | `23.01 ms` ( **1.21x faster**) |
| **`360000`** | `103.01 ms` (✅ **1.00x**) | `83.66 ms` ( **1.23x faster**) |
| | `EVM` | `PVMInterpreter` |
|:-------------|:-------------------------|:-------------------------------- |
| **`10000`** | `2.29 ms` (✅ **1.00x**) | `1.09 ms` (🚀 **2.11x faster**) |
| **`100000`** | `22.84 ms` (✅ **1.00x**) | `10.66 ms` (🚀 **2.14x faster**) |
| **`360000`** | `82.29 ms` (✅ **1.00x**) | `37.01 ms` (🚀 **2.22x faster**) |

### FibonacciRecursive

| | `EVM` | `PVMInterpreter` |
|:---------|:--------------------------|:--------------------------------- |
| **`12`** | `195.19 us` (✅ **1.00x**) | `333.53 us` (*1.71x slower*) |
| **`16`** | `1.22 ms` (✅ **1.00x**) | `1.97 ms` (*1.62x slower*) |
| **`20`** | `8.14 ms` (✅ **1.00x**) | `13.20 ms` (*1.62x slower*) |
| **`24`** | `55.09 ms` (✅ **1.00x**) | `88.56 ms` (*1.61x slower*) |
| **`12`** | `135.67 us` (✅ **1.00x**) | `125.02 us` (**1.09x faster**) |
| **`16`** | `903.75 us` (✅ **1.00x**) | `762.79 us` (**1.18x faster**) |
| **`20`** | `6.12 ms` (✅ **1.00x**) | `4.96 ms` (**1.23x faster**) |
| **`24`** | `42.05 ms` (✅ **1.00x**) | `33.86 ms` (**1.24x faster**) |

### FibonacciIterative

| | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:--------------------------------- |
| **`64`** | `33.39 us` (✅ **1.00x**) | `86.02 us` (❌ *2.58x slower*) |
| **`128`** | `52.91 us` (✅ **1.00x**) | `126.38 us` (❌ *2.39x slower*) |
| **`256`** | `82.33 us` (✅ **1.00x**) | `208.74 us` (❌ *2.54x slower*) |
| | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:-------------------------------- |
| **`64`** | `15.04 us` (✅ **1.00x**) | `29.45 us` (❌ *1.96x slower*) |
| **`128`** | `26.36 us` (✅ **1.00x**) | `42.19 us` (❌ *1.60x slower*) |
| **`256`** | `48.61 us` (✅ **1.00x**) | `65.71 us` (❌ *1.35x slower*) |

### FibonacciBinet

| | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:--------------------------------- |
| **`64`** | `32.29 us` (✅ **1.00x**) | `161.75 us` (❌ *5.01x slower*) |
| **`128`** | `36.02 us` (✅ **1.00x**) | `172.59 us` (❌ *4.79x slower*) |
| **`256`** | `41.21 us` (✅ **1.00x**) | `185.30 us` (❌ *4.50x slower*) |
| | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:-------------------------------- |
| **`64`** | `15.22 us` (✅ **1.00x**) | `41.46 us` (❌ *2.72x slower*) |
| **`128`** | `17.05 us` (✅ **1.00x**) | `42.84 us` (❌ *2.51x slower*) |
| **`256`** | `19.00 us` (✅ **1.00x**) | `44.36 us` (❌ *2.34x slower*) |

### SHA1

| | `EVM` | `PVMInterpreter` |
|:----------|:--------------------------|:--------------------------------- |
| **`1`** | `160.17 us` (✅ **1.00x**) | `403.46 us` (❌ *2.52x slower*) |
| **`64`** | `286.69 us` (✅ **1.00x**) | `479.79 us` (❌ *1.67x slower*) |
| **`512`** | `1.18 ms` (✅ **1.00x**) | `1.37 ms` (*1.16x slower*) |
| **`1`** | `110.04 us` (✅ **1.00x**) | `216.11 us` (❌ *1.96x slower*) |
| **`64`** | `209.04 us` (✅ **1.00x**) | `309.48 us` (❌ *1.48x slower*) |
| **`512`** | `903.65 us` (✅ **1.00x**) | `980.49 us` (**1.09x slower**) |

---
Made with [criterion-table](https://github.com/nu11ptr/criterion-table)
Expand Down
3 changes: 0 additions & 3 deletions crates/builtins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ authors.workspace = true
build = "build.rs"
description = "compiler builtins for the revive compiler"

[features]
riscv-64 = []

[dependencies]
3 changes: 0 additions & 3 deletions crates/builtins/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::{env, fs, io::Read, path::Path, process::Command};

#[cfg(not(feature = "riscv-64"))]
pub const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv32.a";
#[cfg(feature = "riscv-64")]
pub const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv64.a";

fn main() {
Expand Down
3 changes: 0 additions & 3 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ description = "Shared constants of the revive compiler"
[lib]
doctest = false

[features]
riscv-64 = []

[dependencies]
anyhow = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
16 changes: 8 additions & 8 deletions crates/integration/codesize.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"Baseline": 961,
"Computation": 4024,
"DivisionArithmetics": 31789,
"ERC20": 44214,
"Events": 1737,
"FibonacciIterative": 2929,
"Flipper": 3402,
"SHA1": 26003
"Baseline": 1073,
"Computation": 2469,
"DivisionArithmetics": 15041,
"ERC20": 23282,
"Events": 1615,
"FibonacciIterative": 1676,
"Flipper": 2378,
"SHA1": 17076
}
3 changes: 0 additions & 3 deletions crates/linker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ repository.workspace = true
authors.workspace = true
description = "revive compiler linker utils"

[features]
riscv-64 = []

[dependencies]
inkwell = { workspace = true }
tempfile = { workspace = true }
Expand Down
7 changes: 0 additions & 7 deletions crates/linker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ SECTIONS {
.text : { KEEP(*(.text.polkavm_export)) *(.text .text.*) }
}"#;

#[cfg(not(feature = "riscv-64"))]
const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv32.a";
#[cfg(feature = "riscv-64")]
const BUILTINS_ARCHIVE_FILE: &str = "libclang_rt.builtins-riscv64.a";

#[cfg(not(feature = "riscv-64"))]
const BUILTINS_LIB_NAME: &str = "clang_rt.builtins-riscv32";
#[cfg(feature = "riscv-64")]
const BUILTINS_LIB_NAME: &str = "clang_rt.builtins-riscv64";

fn invoke_lld(cmd_args: &[&str]) -> bool {
Expand Down
9 changes: 0 additions & 9 deletions crates/llvm-context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ description = "Shared front end code of the revive PolkaVM compilers"
[lib]
doctest = false

[features]
riscv-zbb = []
riscv-64 = [
"revive-linker/riscv-64",
"revive-builtins/riscv-64",
"revive-runtime-api/riscv-64",
"revive-common/riscv-64",
]

[dependencies]
anyhow = { workspace = true }
semver = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/llvm-context/src/polkavm/const/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// The LLVM framework version.
pub const LLVM_VERSION: semver::Version = semver::Version::new(18, 1, 4);

/// The register width sized type
/// The pointer width sized type.
pub static XLEN: usize = revive_common::BIT_LENGTH_X32;

/// The heap memory pointer pointer global variable name.
Expand Down
18 changes: 15 additions & 3 deletions crates/llvm-context/src/polkavm/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ where

fn link_immutable_data(&self, contract_path: &str) -> anyhow::Result<()> {
let size = self.solidity().immutables_size() as u32;
let exports = revive_runtime_api::immutable_data::module(self.llvm(), size);
self.module.link_in_module(exports).map_err(|error| {
let immutables = revive_runtime_api::immutable_data::module(self.llvm(), size);

self.module.link_in_module(immutables).map_err(|error| {
anyhow::anyhow!(
"The contract `{}` immutable data module linking error: {}",
contract_path,
Expand Down Expand Up @@ -202,6 +203,16 @@ where
);
}

/// Configure the revive datalayout.
fn set_data_layout(
llvm: &'ctx inkwell::context::Context,
module: &inkwell::module::Module<'ctx>,
) {
let source_module = revive_stdlib::module(llvm, "revive_stdlib").unwrap();
let data_layout = source_module.get_data_layout();
module.set_data_layout(&data_layout);
}

/// Initializes a new LLVM context.
pub fn new(
llvm: &'ctx inkwell::context::Context,
Expand All @@ -211,6 +222,7 @@ where
include_metadata_hash: bool,
debug_config: DebugConfig,
) -> Self {
Self::set_data_layout(llvm, &module);
Self::link_stdlib_module(llvm, &module);
Self::link_polkavm_imports(llvm, &module);
Self::set_polkavm_stack_size(llvm, &module, Self::POLKAVM_STACK_SIZE);
Expand Down Expand Up @@ -261,7 +273,7 @@ where
self.link_immutable_data(contract_path)?;

let target_machine = TargetMachine::new(Target::PVM, self.optimizer.settings())?;
target_machine.set_target_data(self.module());
self.module().set_triple(&target_machine.get_triple());

self.debug_config
.dump_llvm_ir_unoptimized(contract_path, self.module())?;
Expand Down
12 changes: 8 additions & 4 deletions crates/llvm-context/src/polkavm/evm/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where
let is_success = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
success,
context.xlen_type().const_zero(),
context.integer_const(revive_common::BIT_LENGTH_X64, 0),
"is_success",
)?;

Expand Down Expand Up @@ -139,8 +139,12 @@ where
let arguments = &[
flags.as_basic_value_enum(),
address_pointer.value.as_basic_value_enum(),
context.integer_const(64, 0).as_basic_value_enum(),
context.integer_const(64, 0).as_basic_value_enum(),
context
.integer_const(revive_common::BIT_LENGTH_X64, 0)
.as_basic_value_enum(),
context
.integer_const(revive_common::BIT_LENGTH_X64, 0)
.as_basic_value_enum(),
context.sentinel_pointer().value.as_basic_value_enum(),
input_pointer.value.as_basic_value_enum(),
input_length.as_basic_value_enum(),
Expand Down Expand Up @@ -168,7 +172,7 @@ where
let is_success = context.builder().build_int_compare(
inkwell::IntPredicate::EQ,
success,
context.xlen_type().const_zero(),
context.integer_const(revive_common::BIT_LENGTH_X64, 0),
"is_success",
)?;

Expand Down
22 changes: 2 additions & 20 deletions crates/llvm-context/src/target_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,16 @@ pub struct TargetMachine {

impl TargetMachine {
/// The LLVM target name.
#[cfg(not(feature = "riscv-64"))]
pub const VM_TARGET_NAME: &'static str = "riscv32";
#[cfg(feature = "riscv-64")]
pub const VM_TARGET_NAME: &'static str = "riscv64";

/// The LLVM target triple.
#[cfg(not(feature = "riscv-64"))]
pub const VM_TARGET_TRIPLE: &'static str = "riscv32-unknown-unknown-elf";
#[cfg(feature = "riscv-64")]
pub const VM_TARGET_TRIPLE: &'static str = "riscv64-unknown-unknown-elf";

/// The LLVM target cpu
#[cfg(not(feature = "riscv-64"))]
pub const VM_TARGET_CPU: &'static str = "generic-rv32";
#[cfg(feature = "riscv-64")]
pub const VM_TARGET_CPU: &'static str = "generic-rv64";

/// LLVM target features.
#[cfg(feature = "riscv-zbb")]
pub const VM_FEATURES: &'static str = "+zbb,+a,+e,+m,+c,+fast-unaligned-access,+xtheadcondmov";
#[cfg(not(feature = "riscv-zbb"))]
pub const VM_FEATURES: &'static str = "+a,+e,+m,+c,+fast-unaligned-access,+xtheadcondmov";
pub const VM_FEATURES: &'static str =
"+e,+m,+a,+c,+zbb,+auipc-addi-fusion,+ld-add-fusion,+lui-addi-fusion,+xtheadcondmov";

/// A shortcut constructor.
/// A separate instance for every optimization level is created.
Expand Down Expand Up @@ -70,12 +58,6 @@ impl TargetMachine {
})
}

/// Sets the target-specific data in the module.
pub fn set_target_data(&self, module: &inkwell::module::Module) {
module.set_triple(&self.target_machine.get_triple());
module.set_data_layout(&self.target_machine.get_target_data().get_data_layout());
}

/// Writes the LLVM module to a memory buffer.
pub fn write_to_memory_buffer(
&self,
Expand Down
12 changes: 0 additions & 12 deletions crates/llvm-context/src/target_machine/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@ impl Target {
/// Returns the target name.
pub fn name(&self) -> &str {
match self {
#[cfg(not(feature = "riscv-64"))]
Self::PVM => "riscv32",
#[cfg(feature = "riscv-64")]
Self::PVM => "riscv64",
}
}

/// Returns the target triple.
pub fn triple(&self) -> &str {
match self {
#[cfg(not(feature = "riscv-64"))]
Self::PVM => "riscv32-unknown-unknown-elf",
#[cfg(feature = "riscv-64")]
Self::PVM => "riscv64-unknown-unknown-elf",
}
}
Expand All @@ -43,9 +37,6 @@ impl FromStr for Target {

fn from_str(string: &str) -> Result<Self, Self::Err> {
match string {
#[cfg(not(feature = "riscv-64"))]
"riscv32" => Ok(Self::PVM),
#[cfg(feature = "riscv-64")]
"riscv64" => Ok(Self::PVM),
_ => Err(anyhow::anyhow!(
"Unknown target `{}`. Supported targets: {:?}",
Expand All @@ -59,9 +50,6 @@ impl FromStr for Target {
impl std::fmt::Display for Target {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(not(feature = "riscv-64"))]
Target::PVM => write!(f, "riscv32"),
#[cfg(feature = "riscv-64")]
Target::PVM => write!(f, "riscv64"),
}
}
Expand Down
Binary file modified crates/runner/fixtures/Baseline.pvm
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub const BOB: H160 = H160([2u8; 20]);
/// The charlie test account
pub const CHARLIE: H160 = H160([3u8; 20]);
/// Default gas limit
pub const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024);
pub const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000_000, 3 * 1024 * 1024 * 1024);
/// Default deposit limit
pub const DEPOSIT_LIMIT: Balance = 10_000_000;

Expand Down
3 changes: 0 additions & 3 deletions crates/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ repository.workspace = true
authors.workspace = true
description = "Implements the low level runtime API bindings with pallet contracts"

[features]
riscv-64 = []

[dependencies]
anyhow = { workspace = true }
inkwell = { workspace = true, features = ["target-riscv", "no-libffi-linking", "llvm18-0"] }
Expand Down
Loading

0 comments on commit 1fcd5a4

Please sign in to comment.