mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-06-14 15:41:04 +00:00
a07968205b
- Add the revive runtime function interface to minimize boiler plate code. - Outline heavily repeated code into dedicated functions to bring down code size. - The code size tests builds optimized for size. - Function attributes are passed as slices. This significantly brings down the code size for all OpenZeppelin wizard contracts (using all possible features) compiled against OpenZeppelin `v5.0.0` with size optimizations. |contract|| `-Oz` main | `-Oz` PR || `-O3` main | `-O3` PR | |-|-|-|-|-|-|-| |erc1155.sol||100K|67K||114K|147K| |erc20.sol||120K|90K||160K|191K| |erc721.sol||128K|101K||178K|214K| |governor.sol||226K|165K||293K|349K| |rwa.sol||116K|85K||154K|185K| |stable.sol||116K|86K||155K|192K| On the flip side this introduces a heavy penalty for cycle optimized builds. Setting the no-inline attributes for cycle optimized builds helps a lot but heavily penalizes runtime speed (LLVM does not yet inline everything properly - to be investigated later on). Next steps: - Modularize more functions - Refactor the YUL function arguments to use pointers instead of values - Afterwards check if LLVM still has trouble inline-ing properly on O3 or set the no-inline attribute if it does not penalize runtime performance too bad.
111 lines
3.3 KiB
Rust
111 lines
3.3 KiB
Rust
//! The revive simulated EVM linear memory pointer functions.
|
|
|
|
use inkwell::values::BasicValueEnum;
|
|
|
|
use crate::polkavm::context::runtime::RuntimeFunction;
|
|
use crate::polkavm::context::Context;
|
|
use crate::polkavm::Dependency;
|
|
use crate::polkavm::WriteLLVM;
|
|
|
|
/// Load a word size value from a heap pointer.
|
|
pub struct LoadWord;
|
|
|
|
impl<D> RuntimeFunction<D> for LoadWord
|
|
where
|
|
D: Dependency + Clone,
|
|
{
|
|
const NAME: &'static str = "__revive_load_heap_word";
|
|
|
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
|
context
|
|
.word_type()
|
|
.fn_type(&[context.xlen_type().into()], false)
|
|
}
|
|
|
|
fn emit_body<'ctx>(
|
|
&self,
|
|
context: &mut Context<'ctx, D>,
|
|
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
|
|
let offset = Self::paramater(context, 0).into_int_value();
|
|
let length = context
|
|
.xlen_type()
|
|
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false);
|
|
let pointer = context.build_heap_gep(offset, length)?;
|
|
let value = context
|
|
.builder()
|
|
.build_load(context.word_type(), pointer.value, "value")?;
|
|
context
|
|
.basic_block()
|
|
.get_last_instruction()
|
|
.expect("Always exists")
|
|
.set_alignment(revive_common::BYTE_LENGTH_BYTE as u32)
|
|
.expect("Alignment is valid");
|
|
|
|
let swapped_value = context.build_byte_swap(value)?;
|
|
Ok(Some(swapped_value))
|
|
}
|
|
}
|
|
|
|
impl<D> WriteLLVM<D> for LoadWord
|
|
where
|
|
D: Dependency + Clone,
|
|
{
|
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
|
}
|
|
|
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
|
}
|
|
}
|
|
|
|
/// Store a word size value through a heap pointer.
|
|
pub struct StoreWord;
|
|
|
|
impl<D> RuntimeFunction<D> for StoreWord
|
|
where
|
|
D: Dependency + Clone,
|
|
{
|
|
const NAME: &'static str = "__revive_store_heap_word";
|
|
|
|
fn r#type<'ctx>(context: &Context<'ctx, D>) -> inkwell::types::FunctionType<'ctx> {
|
|
context.void_type().fn_type(
|
|
&[context.xlen_type().into(), context.word_type().into()],
|
|
false,
|
|
)
|
|
}
|
|
|
|
fn emit_body<'ctx>(
|
|
&self,
|
|
context: &mut Context<'ctx, D>,
|
|
) -> anyhow::Result<Option<BasicValueEnum<'ctx>>> {
|
|
let offset = Self::paramater(context, 0).into_int_value();
|
|
let length = context
|
|
.xlen_type()
|
|
.const_int(revive_common::BYTE_LENGTH_WORD as u64, false);
|
|
let pointer = context.build_heap_gep(offset, length)?;
|
|
|
|
let value = context.build_byte_swap(Self::paramater(context, 1))?;
|
|
|
|
context
|
|
.builder()
|
|
.build_store(pointer.value, value)?
|
|
.set_alignment(revive_common::BYTE_LENGTH_BYTE as u32)
|
|
.expect("Alignment is valid");
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
impl<D> WriteLLVM<D> for StoreWord
|
|
where
|
|
D: Dependency + Clone,
|
|
{
|
|
fn declare(&mut self, context: &mut Context<D>) -> anyhow::Result<()> {
|
|
<Self as RuntimeFunction<_>>::declare(self, context)
|
|
}
|
|
|
|
fn into_llvm(self, context: &mut Context<D>) -> anyhow::Result<()> {
|
|
<Self as RuntimeFunction<_>>::emit(&self, context)
|
|
}
|
|
}
|