llvm-context: lazy handling of function arguments and immutable data (#282)

- Lazily load function arguments so that they can be passed as pointers.
- Lazily call the immutable store function to avoid storing zero sized
immutable data.

---------

Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
xermicus
2025-04-14 15:54:59 +02:00
committed by GitHub
parent ad3315346c
commit 431b5a2ce5
18 changed files with 268 additions and 177 deletions
+9
View File
@@ -4,6 +4,15 @@
This is a development pre-release. This is a development pre-release.
Supported `polkadot-sdk` rev:`c29e72a8628835e34deb6aa7db9a78a2e4eabcee`
### Added
### Changed
### Fixed
- Constructors avoid storing zero sized immutable data on exit.
## v0.1.0-dev.13 ## v0.1.0-dev.13
This is a development pre-release. This is a development pre-release.
+24 -24
View File
@@ -15,58 +15,58 @@
### Baseline ### Baseline
| | `EVM` | `PVMInterpreter` | | | `EVM` | `PVMInterpreter` |
|:--------|:------------------------|:-------------------------------- | |:--------|:-------------------------|:-------------------------------- |
| **`0`** | `3.36 us` (✅ **1.00x**) | `11.84 us` (*3.52x slower*) | | **`0`** | `10.63 us` (✅ **1.00x**) | `10.35 us` (**1.03x faster**) |
### OddPorduct ### OddPorduct
| | `EVM` | `PVMInterpreter` | | | `EVM` | `PVMInterpreter` |
|:-------------|:-------------------------|:-------------------------------- | |:-------------|:--------------------------|:-------------------------------- |
| **`10000`** | `3.11 ms` (✅ **1.00x**) | `1.53 ms` (🚀 **2.03x faster**) | | **`10000`** | `3.63 ms` (✅ **1.00x**) | `1.66 ms` (🚀 **2.19x faster**) |
| **`100000`** | `30.70 ms` (✅ **1.00x**) | `15.54 ms` (🚀 **1.98x faster**) | | **`100000`** | `36.66 ms` (✅ **1.00x**) | `16.39 ms` (🚀 **2.24x faster**) |
| **`300000`** | `92.68 ms` (✅ **1.00x**) | `45.47 ms` (🚀 **2.04x faster**) | | **`300000`** | `108.64 ms` (✅ **1.00x**) | `50.48 ms` (🚀 **2.15x faster**) |
### TriangleNumber ### TriangleNumber
| | `EVM` | `PVMInterpreter` | | | `EVM` | `PVMInterpreter` |
|:-------------|:-------------------------|:-------------------------------- | |:-------------|:-------------------------|:-------------------------------- |
| **`10000`** | `2.29 ms` (✅ **1.00x**) | `1.09 ms` (🚀 **2.11x faster**) | | **`10000`** | `2.59 ms` (✅ **1.00x**) | `1.20 ms` (🚀 **2.17x faster**) |
| **`100000`** | `22.84 ms` (✅ **1.00x**) | `10.66 ms` (🚀 **2.14x faster**) | | **`100000`** | `25.50 ms` (✅ **1.00x**) | `11.82 ms` (🚀 **2.16x faster**) |
| **`360000`** | `82.29 ms` (✅ **1.00x**) | `37.01 ms` (🚀 **2.22x faster**) | | **`360000`** | `91.57 ms` (✅ **1.00x**) | `42.11 ms` (🚀 **2.17x faster**) |
### FibonacciRecursive ### FibonacciRecursive
| | `EVM` | `PVMInterpreter` | | | `EVM` | `PVMInterpreter` |
|:---------|:--------------------------|:--------------------------------- | |:---------|:--------------------------|:--------------------------------- |
| **`12`** | `135.67 us` (✅ **1.00x**) | `125.02 us` (✅ **1.09x faster**) | | **`12`** | `149.13 us` (✅ **1.00x**) | `154.35 us` (✅ **1.04x slower**) |
| **`16`** | `903.75 us` (✅ **1.00x**) | `762.79 us` (✅ **1.18x faster**) | | **`16`** | `972.01 us` (✅ **1.00x**) | `924.33 us` (✅ **1.05x faster**) |
| **`20`** | `6.12 ms` (✅ **1.00x**) | `4.96 ms` (✅ **1.23x faster**) | | **`20`** | `6.62 ms` (✅ **1.00x**) | `6.23 ms` (✅ **1.06x faster**) |
| **`24`** | `42.05 ms` (✅ **1.00x**) | `33.86 ms` (✅ **1.24x faster**) | | **`24`** | `45.25 ms` (✅ **1.00x**) | `43.44 ms` (✅ **1.04x faster**) |
### FibonacciIterative ### FibonacciIterative
| | `EVM` | `PVMInterpreter` | | | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:-------------------------------- | |:----------|:-------------------------|:-------------------------------- |
| **`64`** | `15.04 us` (✅ **1.00x**) | `29.45 us` (❌ *1.96x slower*) | | **`64`** | `22.71 us` (✅ **1.00x**) | `31.48 us` (❌ *1.39x slower*) |
| **`128`** | `26.36 us` (✅ **1.00x**) | `42.19 us` (❌ *1.60x slower*) | | **`128`** | `35.32 us` (✅ **1.00x**) | `41.87 us` (❌ *1.19x slower*) |
| **`256`** | `48.61 us` (✅ **1.00x**) | `65.71 us` (*1.35x slower*) | | **`256`** | `59.58 us` (✅ **1.00x**) | `63.43 us` (**1.06x slower**) |
### FibonacciBinet ### FibonacciBinet
| | `EVM` | `PVMInterpreter` | | | `EVM` | `PVMInterpreter` |
|:----------|:-------------------------|:-------------------------------- | |:----------|:-------------------------|:-------------------------------- |
| **`64`** | `15.22 us` (✅ **1.00x**) | `41.46 us` (❌ *2.72x slower*) | | **`64`** | `23.18 us` (✅ **1.00x**) | `47.33 us` (❌ *2.04x slower*) |
| **`128`** | `17.05 us` (✅ **1.00x**) | `42.84 us` (❌ *2.51x slower*) | | **`128`** | `24.97 us` (✅ **1.00x**) | `50.37 us` (❌ *2.02x slower*) |
| **`256`** | `19.00 us` (✅ **1.00x**) | `44.36 us` (❌ *2.34x slower*) | | **`256`** | `28.25 us` (✅ **1.00x**) | `53.69 us` (❌ *1.90x slower*) |
### SHA1 ### SHA1
| | `EVM` | `PVMInterpreter` | | | `EVM` | `PVMInterpreter` |
|:----------|:--------------------------|:--------------------------------- | |:----------|:--------------------------|:--------------------------------- |
| **`1`** | `110.04 us` (✅ **1.00x**) | `216.11 us` (❌ *1.96x slower*) | | **`1`** | `132.75 us` (✅ **1.00x**) | `232.17 us` (❌ *1.75x slower*) |
| **`64`** | `209.04 us` (✅ **1.00x**) | `309.48 us` (❌ *1.48x slower*) | | **`64`** | `240.82 us` (✅ **1.00x**) | `328.19 us` (❌ *1.36x slower*) |
| **`512`** | `903.65 us` (✅ **1.00x**) | `980.49 us` (✅ **1.09x slower**) | | **`512`** | `1.03 ms` (✅ **1.00x**) | `1.03 ms` (✅ **1.01x faster**) |
--- ---
Made with [criterion-table](https://github.com/nu11ptr/criterion-table) Made with [criterion-table](https://github.com/nu11ptr/criterion-table)
+8 -8
View File
@@ -1,10 +1,10 @@
{ {
"Baseline": 1443, "Baseline": 1365,
"Computation": 2788, "Computation": 2710,
"DivisionArithmetics": 9748, "DivisionArithmetics": 9672,
"ERC20": 19150, "ERC20": 19131,
"Events": 2201, "Events": 2123,
"FibonacciIterative": 2041, "FibonacciIterative": 1964,
"Flipper": 2691, "Flipper": 2590,
"SHA1": 8997 "SHA1": 8918
} }
@@ -4,61 +4,98 @@
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Argument<'ctx> { pub struct Argument<'ctx> {
/// The actual LLVM operand. /// The actual LLVM operand.
pub value: inkwell::values::BasicValueEnum<'ctx>, pub value: Value<'ctx>,
/// The original AST value. Used mostly for string literals. /// The original AST value. Used mostly for string literals.
pub original: Option<String>, pub original: Option<String>,
/// The preserved constant value, if available. /// The preserved constant value, if available.
pub constant: Option<num::BigUint>, pub constant: Option<num::BigUint>,
} }
/// The function argument can be either a pointer or a integer value.
/// This disambiguation allows for lazy loading of variables.
#[derive(Clone, Debug)]
pub enum Value<'ctx> {
Register(inkwell::values::BasicValueEnum<'ctx>),
Pointer {
pointer: crate::polkavm::context::Pointer<'ctx>,
id: String,
},
}
impl<'ctx> Argument<'ctx> { impl<'ctx> Argument<'ctx> {
/// The calldata offset argument index. /// A shortcut constructor for register arguments.
pub const ARGUMENT_INDEX_CALLDATA_OFFSET: usize = 0; pub fn value(value: inkwell::values::BasicValueEnum<'ctx>) -> Self {
/// The calldata length argument index.
pub const ARGUMENT_INDEX_CALLDATA_LENGTH: usize = 1;
/// A shortcut constructor.
pub fn new(value: inkwell::values::BasicValueEnum<'ctx>) -> Self {
Self { Self {
value, value: Value::Register(value),
original: None, original: None,
constant: None, constant: None,
} }
} }
/// A shortcut constructor. /// A shortcut constructor for stack arguments.
pub fn new_with_original( pub fn pointer(pointer: crate::polkavm::context::Pointer<'ctx>, id: String) -> Self {
value: inkwell::values::BasicValueEnum<'ctx>,
original: String,
) -> Self {
Self { Self {
value, value: Value::Pointer { pointer, id },
original: Some(original), original: None,
constant: None, constant: None,
} }
} }
/// A shortcut constructor. /// Set the original decleratation value.
pub fn new_with_constant( pub fn with_original(mut self, original: String) -> Self {
value: inkwell::values::BasicValueEnum<'ctx>, self.original = Some(original);
constant: num::BigUint, self
) -> Self { }
Self {
value, /// Set the constant value.
original: None, pub fn with_constant(mut self, constant: num::BigUint) -> Self {
constant: Some(constant), self.constant = Some(constant);
} self
} }
/// Returns the inner LLVM value. /// Returns the inner LLVM value.
pub fn to_llvm(&self) -> inkwell::values::BasicValueEnum<'ctx> { ///
self.value /// Panics if `self` is a pointer argument.
pub fn _to_llvm_value(&self) -> inkwell::values::BasicValueEnum<'ctx> {
match &self.value {
Value::Register(value) => *value,
Value::Pointer { .. } => unreachable!("invalid register value access"),
}
}
/// Access the underlying value.
///
/// Will emit a stack load if `self` is a pointer argument.
pub fn access<D: crate::polkavm::Dependency + Clone>(
&self,
context: &crate::polkavm::context::Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> {
match &self.value {
Value::Register(value) => Ok(*value),
Value::Pointer { pointer, id } => context.build_load(*pointer, id),
}
}
/// Access the underlying value.
///
/// Will emit a stack load if `self` is a pointer argument.
pub fn as_pointer<D: crate::polkavm::Dependency + Clone>(
&self,
context: &crate::polkavm::context::Context<'ctx, D>,
) -> anyhow::Result<crate::polkavm::context::Pointer<'ctx>> {
match &self.value {
Value::Register(value) => {
let pointer = context.build_alloca_at_entry(context.word_type(), "pvm_arg");
context.build_store(pointer, *value)?;
Ok(pointer)
}
Value::Pointer { pointer, .. } => Ok(*pointer),
}
} }
} }
impl<'ctx> From<inkwell::values::BasicValueEnum<'ctx>> for Argument<'ctx> { impl<'ctx> From<inkwell::values::BasicValueEnum<'ctx>> for Argument<'ctx> {
fn from(value: inkwell::values::BasicValueEnum<'ctx>) -> Self { fn from(value: inkwell::values::BasicValueEnum<'ctx>) -> Self {
Self::new(value) Self::value(value)
} }
} }
@@ -101,6 +101,9 @@ where
solidity_data: Option<SolidityData>, solidity_data: Option<SolidityData>,
/// The Yul data. /// The Yul data.
yul_data: Option<YulData>, yul_data: Option<YulData>,
/// Hints whether the contracts deploy function stores immutables.
immutables: bool,
} }
impl<'ctx, D> Context<'ctx, D> impl<'ctx, D> Context<'ctx, D>
@@ -263,6 +266,8 @@ where
solidity_data: None, solidity_data: None,
yul_data: None, yul_data: None,
immutables: false,
} }
} }
@@ -1426,4 +1431,14 @@ where
pub fn optimizer_settings(&self) -> &OptimizerSettings { pub fn optimizer_settings(&self) -> &OptimizerSettings {
self.optimizer.settings() self.optimizer.settings()
} }
/// Hint the deploy code exit routine to emit storing the immutables.
pub fn enable_immutables(&mut self) {
self.immutables = true;
}
/// Returns if the contract stores or loads immutables.
pub fn has_immutables(&self) -> bool {
self.immutables
}
} }
@@ -19,7 +19,7 @@ where
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> { fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context context
.word_type() .word_type()
.fn_type(&[context.word_type().into()], false) .fn_type(&[context.llvm().ptr_type(Default::default()).into()], false)
} }
fn emit_body<'ctx>( fn emit_body<'ctx>(
@@ -59,7 +59,7 @@ where
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> { fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context context
.word_type() .word_type()
.fn_type(&[context.word_type().into()], false) .fn_type(&[context.llvm().ptr_type(Default::default()).into()], false)
} }
fn emit_body<'ctx>( fn emit_body<'ctx>(
@@ -94,7 +94,10 @@ where
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> { fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context.void_type().fn_type( context.void_type().fn_type(
&[context.word_type().into(), context.word_type().into()], &[
context.llvm().ptr_type(Default::default()).into(),
context.llvm().ptr_type(Default::default()).into(),
],
false, false,
) )
} }
@@ -138,7 +141,10 @@ where
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> { fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
context.void_type().fn_type( context.void_type().fn_type(
&[context.word_type().into(), context.word_type().into()], &[
context.llvm().ptr_type(Default::default()).into(),
context.llvm().ptr_type(Default::default()).into(),
],
false, false,
) )
} }
@@ -173,9 +179,17 @@ where
fn emit_load<'ctx, D: Dependency + Clone>( fn emit_load<'ctx, D: Dependency + Clone>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
mut key: BasicValueEnum<'ctx>, key: BasicValueEnum<'ctx>,
transient: bool, transient: bool,
) -> anyhow::Result<BasicValueEnum<'ctx>> { ) -> anyhow::Result<BasicValueEnum<'ctx>> {
let mut key = context.build_load(
super::Pointer::new(
context.word_type(),
Default::default(),
key.into_pointer_value(),
),
"key",
)?;
if !transient { if !transient {
key = context.build_byte_swap(key)?; key = context.build_byte_swap(key)?;
} }
@@ -217,10 +231,26 @@ fn emit_load<'ctx, D: Dependency + Clone>(
fn emit_store<'ctx, D: Dependency + Clone>( fn emit_store<'ctx, D: Dependency + Clone>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
mut key: BasicValueEnum<'ctx>, key: BasicValueEnum<'ctx>,
mut value: BasicValueEnum<'ctx>, value: BasicValueEnum<'ctx>,
transient: bool, transient: bool,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut key = context.build_load(
super::Pointer::new(
context.word_type(),
Default::default(),
key.into_pointer_value(),
),
"key",
)?;
let mut value = context.build_load(
super::Pointer::new(
context.word_type(),
Default::default(),
value.into_pointer_value(),
),
"key",
)?;
if !transient { if !transient {
key = context.build_byte_swap(key)?; key = context.build_byte_swap(key)?;
value = context.build_byte_swap(value)?; value = context.build_byte_swap(value)?;
+6 -10
View File
@@ -119,10 +119,8 @@ where
_ => error, _ => error,
})?; })?;
if contract_path.as_str() == parent { if contract_path.as_str() == parent {
return Ok(Argument::new_with_constant( return Ok(Argument::value(context.word_const(0).as_basic_value_enum())
context.word_const(0).as_basic_value_enum(), .with_constant(num::BigUint::zero()));
num::BigUint::zero(),
));
} else if identifier.ends_with("_deployed") && code_type == CodeType::Runtime { } else if identifier.ends_with("_deployed") && code_type == CodeType::Runtime {
anyhow::bail!("type({}).runtimeCode is not supported", identifier); anyhow::bail!("type({}).runtimeCode is not supported", identifier);
} }
@@ -131,7 +129,7 @@ where
let hash_value = context let hash_value = context
.word_const_str_hex(hash_string.as_str()) .word_const_str_hex(hash_string.as_str())
.as_basic_value_enum(); .as_basic_value_enum();
Ok(Argument::new_with_original(hash_value, hash_string)) Ok(Argument::value(hash_value).with_original(hash_string))
} }
/// Translates the deploy call header size instruction. the header consists of /// Translates the deploy call header size instruction. the header consists of
@@ -160,10 +158,8 @@ where
_ => error, _ => error,
})?; })?;
if contract_path.as_str() == parent { if contract_path.as_str() == parent {
return Ok(Argument::new_with_constant( return Ok(Argument::value(context.word_const(0).as_basic_value_enum())
context.word_const(0).as_basic_value_enum(), .with_constant(num::BigUint::zero()));
num::BigUint::zero(),
));
} else if identifier.ends_with("_deployed") && code_type == CodeType::Runtime { } else if identifier.ends_with("_deployed") && code_type == CodeType::Runtime {
anyhow::bail!("type({}).runtimeCode is not supported", identifier); anyhow::bail!("type({}).runtimeCode is not supported", identifier);
} }
@@ -172,5 +168,5 @@ where
let size_value = context let size_value = context
.word_const(crate::polkavm::DEPLOYER_CALL_HEADER_SIZE as u64) .word_const(crate::polkavm::DEPLOYER_CALL_HEADER_SIZE as u64)
.as_basic_value_enum(); .as_basic_value_enum();
Ok(Argument::new_with_constant(size_value, size_bigint)) Ok(Argument::value(size_value).with_constant(size_bigint))
} }
@@ -257,6 +257,7 @@ where
anyhow::bail!("Immutables are not available if the contract part is undefined"); anyhow::bail!("Immutables are not available if the contract part is undefined");
} }
Some(CodeType::Deploy) => { Some(CodeType::Deploy) => {
context.enable_immutables();
let immutable_data_pointer = context let immutable_data_pointer = context
.get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)? .get_global(revive_runtime_api::immutable_data::GLOBAL_IMMUTABLE_DATA_POINTER)?
.value .value
@@ -18,11 +18,13 @@ where
match context.code_type() { match context.code_type() {
None => anyhow::bail!("Return is not available if the contract part is undefined"), None => anyhow::bail!("Return is not available if the contract part is undefined"),
Some(CodeType::Deploy) => { Some(CodeType::Deploy) => {
context.build_call( if context.has_immutables() {
<Store as RuntimeFunction<D>>::declaration(context), context.build_call(
Default::default(), <Store as RuntimeFunction<D>>::declaration(context),
"store_immutable_data", Default::default(),
); "store_immutable_data",
);
}
} }
Some(CodeType::Runtime) => {} Some(CodeType::Runtime) => {}
} }
+17 -10
View File
@@ -3,6 +3,7 @@
use crate::polkavm::context::runtime::RuntimeFunction; use crate::polkavm::context::runtime::RuntimeFunction;
use crate::polkavm::context::Context; use crate::polkavm::context::Context;
use crate::polkavm::Dependency; use crate::polkavm::Dependency;
use crate::PolkaVMArgument;
use crate::PolkaVMLoadStorageWordFunction; use crate::PolkaVMLoadStorageWordFunction;
use crate::PolkaVMLoadTransientStorageWordFunction; use crate::PolkaVMLoadTransientStorageWordFunction;
use crate::PolkaVMStoreStorageWordFunction; use crate::PolkaVMStoreStorageWordFunction;
@@ -11,14 +12,14 @@ use crate::PolkaVMStoreTransientStorageWordFunction;
/// Translates the storage load. /// Translates the storage load.
pub fn load<'ctx, D>( pub fn load<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
position: inkwell::values::IntValue<'ctx>, position: &PolkaVMArgument<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> ) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME; let name = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::NAME;
let declaration = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::declaration(context); let declaration = <PolkaVMLoadStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [position.into()]; let arguments = [position.as_pointer(context)?.value.into()];
Ok(context Ok(context
.build_call(declaration, &arguments, "storage_load") .build_call(declaration, &arguments, "storage_load")
.unwrap_or_else(|| panic!("runtime function {name} should return a value"))) .unwrap_or_else(|| panic!("runtime function {name} should return a value")))
@@ -27,14 +28,17 @@ where
/// Translates the storage store. /// Translates the storage store.
pub fn store<'ctx, D>( pub fn store<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
position: inkwell::values::IntValue<'ctx>, position: &PolkaVMArgument<'ctx>,
value: inkwell::values::IntValue<'ctx>, value: &PolkaVMArgument<'ctx>,
) -> anyhow::Result<()> ) -> anyhow::Result<()>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context); let declaration = <PolkaVMStoreStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [position.into(), value.into()]; let arguments = [
position.as_pointer(context)?.value.into(),
value.as_pointer(context)?.value.into(),
];
context.build_call(declaration, &arguments, "storage_store"); context.build_call(declaration, &arguments, "storage_store");
Ok(()) Ok(())
} }
@@ -42,13 +46,13 @@ where
/// Translates the transient storage load. /// Translates the transient storage load.
pub fn transient_load<'ctx, D>( pub fn transient_load<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
position: inkwell::values::IntValue<'ctx>, position: &PolkaVMArgument<'ctx>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>> ) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let name = <PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::NAME; let name = <PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::NAME;
let arguments = [position.into()]; let arguments = [position.as_pointer(context)?.value.into()];
let declaration = let declaration =
<PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context); <PolkaVMLoadTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
Ok(context Ok(context
@@ -59,15 +63,18 @@ where
/// Translates the transient storage store. /// Translates the transient storage store.
pub fn transient_store<'ctx, D>( pub fn transient_store<'ctx, D>(
context: &mut Context<'ctx, D>, context: &mut Context<'ctx, D>,
position: inkwell::values::IntValue<'ctx>, position: &PolkaVMArgument<'ctx>,
value: inkwell::values::IntValue<'ctx>, value: &PolkaVMArgument<'ctx>,
) -> anyhow::Result<()> ) -> anyhow::Result<()>
where where
D: Dependency + Clone, D: Dependency + Clone,
{ {
let declaration = let declaration =
<PolkaVMStoreTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context); <PolkaVMStoreTransientStorageWordFunction as RuntimeFunction<D>>::declaration(context);
let arguments = [position.into(), value.into()]; let arguments = [
position.as_pointer(context)?.value.into(),
value.as_pointer(context)?.value.into(),
];
context.build_call(declaration, &arguments, "transient_storage_store"); context.build_call(declaration, &arguments, "transient_storage_store");
Ok(()) Ok(())
} }
@@ -139,13 +139,14 @@ where
identifier.inner, identifier.inner,
) )
})?; })?;
context.build_store(pointer, value.to_llvm())?; context.build_store(pointer, value.access(context)?)?;
return Ok(()); return Ok(());
} }
let llvm_type = value.to_llvm().into_struct_value().get_type(); let value = value.access(context)?;
let llvm_type = value.into_struct_value().get_type();
let tuple_pointer = context.build_alloca(llvm_type, "assignment_pointer"); let tuple_pointer = context.build_alloca(llvm_type, "assignment_pointer");
context.build_store(tuple_pointer, value.to_llvm())?; context.build_store(tuple_pointer, value)?;
for (index, binding) in self.bindings.into_iter().enumerate() { for (index, binding) in self.bindings.into_iter().enumerate() {
context.set_debug_location(self.location.line, 0, None)?; context.set_debug_location(self.location.line, 0, None)?;
@@ -128,7 +128,10 @@ impl FunctionCall {
Name::UserDefined(name) => { Name::UserDefined(name) => {
let mut values = Vec::with_capacity(self.arguments.len()); let mut values = Vec::with_capacity(self.arguments.len());
for argument in self.arguments.into_iter().rev() { for argument in self.arguments.into_iter().rev() {
let value = argument.into_llvm(context)?.expect("Always exists").value; let value = argument
.into_llvm(context)?
.expect("Always exists")
.access(context)?;
values.push(value); values.push(value);
} }
values.reverse(); values.reverse();
@@ -461,36 +464,29 @@ impl FunctionCall {
} }
Name::SLoad => { Name::SLoad => {
let arguments = self.pop_arguments_llvm::<D, 1>(context)?; let arguments = self.pop_arguments::<D, 1>(context)?;
revive_llvm_context::polkavm_evm_storage::load( revive_llvm_context::polkavm_evm_storage::load(context, &arguments[0]).map(Some)
context,
arguments[0].into_int_value(),
)
.map(Some)
} }
Name::SStore => { Name::SStore => {
let arguments = self.pop_arguments_llvm::<D, 2>(context)?; let arguments = self.pop_arguments::<D, 2>(context)?;
revive_llvm_context::polkavm_evm_storage::store( revive_llvm_context::polkavm_evm_storage::store(
context, context,
arguments[0].into_int_value(), &arguments[0],
arguments[1].into_int_value(), &arguments[1],
) )
.map(|_| None) .map(|_| None)
} }
Name::TLoad => { Name::TLoad => {
let arguments = self.pop_arguments_llvm::<D, 1>(context)?; let arguments = self.pop_arguments::<D, 1>(context)?;
revive_llvm_context::polkavm_evm_storage::transient_load( revive_llvm_context::polkavm_evm_storage::transient_load(context, &arguments[0])
context, .map(Some)
arguments[0].into_int_value(),
)
.map(Some)
} }
Name::TStore => { Name::TStore => {
let arguments = self.pop_arguments_llvm::<D, 2>(context)?; let arguments = self.pop_arguments::<D, 2>(context)?;
revive_llvm_context::polkavm_evm_storage::transient_store( revive_llvm_context::polkavm_evm_storage::transient_store(
context, context,
arguments[0].into_int_value(), &arguments[0],
arguments[1].into_int_value(), &arguments[1],
) )
.map(|_| None) .map(|_| None)
} }
@@ -514,7 +510,7 @@ impl FunctionCall {
let offset = context.solidity_mut().allocate_immutable(key.as_str()) let offset = context.solidity_mut().allocate_immutable(key.as_str())
/ revive_common::BYTE_LENGTH_WORD; / revive_common::BYTE_LENGTH_WORD;
let index = context.xlen_type().const_int(offset as u64, false); let index = context.xlen_type().const_int(offset as u64, false);
let value = arguments[2].value.into_int_value(); let value = arguments[2].access(context)?.into_int_value();
revive_llvm_context::polkavm_evm_immutable::store(context, index, value) revive_llvm_context::polkavm_evm_immutable::store(context, index, value)
.map(|_| None) .map(|_| None)
} }
@@ -720,13 +716,13 @@ impl FunctionCall {
Name::Call => { Name::Call => {
let arguments = self.pop_arguments::<D, 7>(context)?; let arguments = self.pop_arguments::<D, 7>(context)?;
let gas = arguments[0].value.into_int_value(); let gas = arguments[0].access(context)?.into_int_value();
let address = arguments[1].value.into_int_value(); let address = arguments[1].access(context)?.into_int_value();
let value = arguments[2].value.into_int_value(); let value = arguments[2].access(context)?.into_int_value();
let input_offset = arguments[3].value.into_int_value(); let input_offset = arguments[3].access(context)?.into_int_value();
let input_size = arguments[4].value.into_int_value(); let input_size = arguments[4].access(context)?.into_int_value();
let output_offset = arguments[5].value.into_int_value(); let output_offset = arguments[5].access(context)?.into_int_value();
let output_size = arguments[6].value.into_int_value(); let output_size = arguments[6].access(context)?.into_int_value();
let simulation_address: Vec<Option<num::BigUint>> = arguments let simulation_address: Vec<Option<num::BigUint>> = arguments
.into_iter() .into_iter()
@@ -750,12 +746,12 @@ impl FunctionCall {
Name::StaticCall => { Name::StaticCall => {
let arguments = self.pop_arguments::<D, 6>(context)?; let arguments = self.pop_arguments::<D, 6>(context)?;
let gas = arguments[0].value.into_int_value(); let gas = arguments[0].access(context)?.into_int_value();
let address = arguments[1].value.into_int_value(); let address = arguments[1].access(context)?.into_int_value();
let input_offset = arguments[2].value.into_int_value(); let input_offset = arguments[2].access(context)?.into_int_value();
let input_size = arguments[3].value.into_int_value(); let input_size = arguments[3].access(context)?.into_int_value();
let output_offset = arguments[4].value.into_int_value(); let output_offset = arguments[4].access(context)?.into_int_value();
let output_size = arguments[5].value.into_int_value(); let output_size = arguments[5].access(context)?.into_int_value();
let simulation_address: Vec<Option<num::BigUint>> = arguments let simulation_address: Vec<Option<num::BigUint>> = arguments
.into_iter() .into_iter()
@@ -779,12 +775,12 @@ impl FunctionCall {
Name::DelegateCall => { Name::DelegateCall => {
let arguments = self.pop_arguments::<D, 6>(context)?; let arguments = self.pop_arguments::<D, 6>(context)?;
let gas = arguments[0].value.into_int_value(); let gas = arguments[0].access(context)?.into_int_value();
let address = arguments[1].value.into_int_value(); let address = arguments[1].access(context)?.into_int_value();
let input_offset = arguments[2].value.into_int_value(); let input_offset = arguments[2].access(context)?.into_int_value();
let input_size = arguments[3].value.into_int_value(); let input_size = arguments[3].access(context)?.into_int_value();
let output_offset = arguments[4].value.into_int_value(); let output_offset = arguments[4].access(context)?.into_int_value();
let output_size = arguments[5].value.into_int_value(); let output_size = arguments[5].access(context)?.into_int_value();
let simulation_address: Vec<Option<num::BigUint>> = arguments let simulation_address: Vec<Option<num::BigUint>> = arguments
.into_iter() .into_iter()
@@ -845,7 +841,8 @@ impl FunctionCall {
})?; })?;
revive_llvm_context::polkavm_evm_create::contract_hash(context, identifier) revive_llvm_context::polkavm_evm_create::contract_hash(context, identifier)
.map(|argument| Some(argument.value)) .and_then(|argument| argument.access(context))
.map(Some)
} }
Name::DataSize => { Name::DataSize => {
let mut arguments = self.pop_arguments::<D, 1>(context)?; let mut arguments = self.pop_arguments::<D, 1>(context)?;
@@ -855,7 +852,8 @@ impl FunctionCall {
})?; })?;
revive_llvm_context::polkavm_evm_create::header_size(context, identifier) revive_llvm_context::polkavm_evm_create::header_size(context, identifier)
.map(|argument| Some(argument.value)) .and_then(|argument| argument.access(context))
.map(Some)
} }
Name::DataCopy => { Name::DataCopy => {
let arguments = self.pop_arguments_llvm::<D, 3>(context)?; let arguments = self.pop_arguments_llvm::<D, 3>(context)?;
@@ -989,7 +987,12 @@ impl FunctionCall {
{ {
let mut arguments = Vec::with_capacity(N); let mut arguments = Vec::with_capacity(N);
for expression in self.arguments.drain(0..N).rev() { for expression in self.arguments.drain(0..N).rev() {
arguments.push(expression.into_llvm(context)?.expect("Always exists").value); arguments.push(
expression
.into_llvm(context)?
.expect("Always exists")
.access(context)?,
);
} }
arguments.reverse(); arguments.reverse();
@@ -97,9 +97,7 @@ impl Literal {
BooleanLiteral::True => num::BigUint::one(), BooleanLiteral::True => num::BigUint::one(),
}; };
Ok(revive_llvm_context::PolkaVMArgument::new_with_constant( Ok(revive_llvm_context::PolkaVMArgument::value(value).with_constant(constant))
value, constant,
))
} }
LexicalLiteral::Integer(inner) => { LexicalLiteral::Integer(inner) => {
let r#type = self.yul_type.unwrap_or_default().into_llvm(context); let r#type = self.yul_type.unwrap_or_default().into_llvm(context);
@@ -127,9 +125,7 @@ impl Literal {
} }
.expect("Always valid"); .expect("Always valid");
Ok(revive_llvm_context::PolkaVMArgument::new_with_constant( Ok(revive_llvm_context::PolkaVMArgument::value(value).with_constant(constant))
value, constant,
))
} }
LexicalLiteral::String(inner) => { LexicalLiteral::String(inner) => {
let string = inner.inner; let string = inner.inner;
@@ -200,10 +196,10 @@ impl Literal {
}; };
if hex_string.len() > revive_common::BYTE_LENGTH_WORD * 2 { if hex_string.len() > revive_common::BYTE_LENGTH_WORD * 2 {
return Ok(revive_llvm_context::PolkaVMArgument::new_with_original( return Ok(revive_llvm_context::PolkaVMArgument::value(
r#type.const_zero().as_basic_value_enum(), r#type.const_zero().as_basic_value_enum(),
string, )
)); .with_original(string));
} }
if hex_string.len() < revive_common::BYTE_LENGTH_WORD * 2 { if hex_string.len() < revive_common::BYTE_LENGTH_WORD * 2 {
@@ -220,9 +216,7 @@ impl Literal {
) )
.expect("The value is valid") .expect("The value is valid")
.as_basic_value_enum(); .as_basic_value_enum();
Ok(revive_llvm_context::PolkaVMArgument::new_with_original( Ok(revive_llvm_context::PolkaVMArgument::value(value).with_original(string))
value, string,
))
} }
} }
} }
@@ -119,36 +119,28 @@ impl Expression {
}) })
.map(Some), .map(Some),
Self::Identifier(identifier) => { Self::Identifier(identifier) => {
let id = identifier.inner;
let pointer = context let pointer = context
.current_function() .current_function()
.borrow() .borrow()
.get_stack_pointer(identifier.inner.as_str()) .get_stack_pointer(&id)
.ok_or_else(|| { .ok_or_else(|| {
anyhow::anyhow!( anyhow::anyhow!("{} Undeclared variable `{}`", identifier.location, id)
"{} Undeclared variable `{}`",
identifier.location,
identifier.inner,
)
})?; })?;
let constant = context let constant = context.current_function().borrow().yul().get_constant(&id);
.current_function()
.borrow()
.yul()
.get_constant(identifier.inner.as_str());
let value = context.build_load(pointer, identifier.inner.as_str())?; let argument = revive_llvm_context::PolkaVMArgument::pointer(pointer, id);
match constant { Ok(Some(match constant {
Some(constant) => Ok(Some( Some(constant) => argument.with_constant(constant),
revive_llvm_context::PolkaVMArgument::new_with_constant(value, constant), _ => argument,
)), }))
None => Ok(Some(value.into())),
}
} }
Self::FunctionCall(call) => Ok(call Self::FunctionCall(call) => Ok(call
.into_llvm(context)? .into_llvm(context)?
.map(revive_llvm_context::PolkaVMArgument::new)), .map(revive_llvm_context::PolkaVMArgument::value)),
} }
} }
} }
@@ -78,7 +78,7 @@ where
.condition .condition
.into_llvm(context)? .into_llvm(context)?
.expect("Always exists") .expect("Always exists")
.to_llvm() .access(context)?
.into_int_value(); .into_int_value();
let condition = context.builder().build_int_z_extend_or_bit_cast( let condition = context.builder().build_int_z_extend_or_bit_cast(
condition, condition,
@@ -57,7 +57,7 @@ where
.condition .condition
.into_llvm(context)? .into_llvm(context)?
.expect("Always exists") .expect("Always exists")
.to_llvm() .access(context)?
.into_int_value(); .into_int_value();
let condition = context.builder().build_int_z_extend_or_bit_cast( let condition = context.builder().build_int_z_extend_or_bit_cast(
condition, condition,
@@ -137,7 +137,7 @@ where
let mut branches = Vec::with_capacity(self.cases.len()); let mut branches = Vec::with_capacity(self.cases.len());
for (index, case) in self.cases.into_iter().enumerate() { for (index, case) in self.cases.into_iter().enumerate() {
let constant = case.literal.into_llvm(context)?.to_llvm(); let constant = case.literal.into_llvm(context)?.access(context)?;
let expression_block = context let expression_block = context
.append_basic_block(format!("switch_case_branch_{}_block", index + 1).as_str()); .append_basic_block(format!("switch_case_branch_{}_block", index + 1).as_str());
@@ -161,7 +161,10 @@ where
context.set_basic_block(current_block); context.set_basic_block(current_block);
context.builder().build_switch( context.builder().build_switch(
scrutinee.expect("Always exists").to_llvm().into_int_value(), scrutinee
.expect("Always exists")
.access(context)?
.into_int_value(),
default_block, default_block,
branches.as_slice(), branches.as_slice(),
)?; )?;
@@ -121,7 +121,7 @@ where
.insert_constant(identifier.inner.clone(), constant); .insert_constant(identifier.inner.clone(), constant);
} }
value.to_llvm() value.access(context)?
} }
None => r#type.const_zero().as_basic_value_enum(), None => r#type.const_zero().as_basic_value_enum(),
} }
@@ -175,7 +175,8 @@ where
.collect::<Vec<inkwell::types::BasicTypeEnum<'ctx>>>() .collect::<Vec<inkwell::types::BasicTypeEnum<'ctx>>>()
.as_slice(), .as_slice(),
); );
if expression.value.get_type() != llvm_type.as_basic_type_enum() { let value = expression.access(context)?;
if value.get_type() != llvm_type.as_basic_type_enum() {
anyhow::bail!( anyhow::bail!(
"{} Assignment to {:?} received an invalid number of arguments", "{} Assignment to {:?} received an invalid number of arguments",
location, location,
@@ -183,7 +184,7 @@ where
); );
} }
let pointer = context.build_alloca(llvm_type, "bindings_pointer"); let pointer = context.build_alloca(llvm_type, "bindings_pointer");
context.build_store(pointer, expression.to_llvm())?; context.build_store(pointer, value)?;
for (index, binding) in self.bindings.into_iter().enumerate() { for (index, binding) in self.bindings.into_iter().enumerate() {
let pointer = context.build_gep( let pointer = context.build_gep(