Skip to content

Commit

Permalink
chore: update revm gas
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Apr 28, 2024
1 parent 36988a0 commit bdb30cd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions crates/revm-jit-builtins/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ pub(crate) fn resize_memory(
) -> InstructionResult {
let size = offset.saturating_add(len);
if size > ecx.memory.len() {
let rounded_size = revm_interpreter::interpreter::next_multiple_of_32(size);

// TODO: Memory limit

// TODO: try_resize
if !ecx.gas.record_memory(rgas::memory_gas(rounded_size / 32)) {
return InstructionResult::MemoryLimitOOG;
if !revm_interpreter::interpreter::resize_memory(ecx.memory, ecx.gas, size) {
return InstructionResult::MemoryOOG;
}
ecx.memory.resize(rounded_size);
}
InstructionResult::Continue
}
Expand Down
8 changes: 8 additions & 0 deletions crates/revm-jit/src/bytecode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ impl<'a> Bytecode<'a> {
false
}

/// Returns `true` if the bytecode is small.
///
/// This is arbitarily chosen to speed up compilation for larger contracts.
#[allow(dead_code)]
pub(crate) fn is_small(&self) -> bool {
self.insts.len() < 2000
}

/// Returns `true` if the instruction is diverging.
pub(crate) fn is_instr_diverging(&self, inst: Inst) -> bool {
self.insts[inst].is_diverging(self.is_eof())
Expand Down
33 changes: 12 additions & 21 deletions crates/revm-jit/src/compiler/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ pub(super) struct FunctionCx<'a, B: Backend> {
stack: Pointer<B::Builder<'a>>,
/// The amount of gas remaining. `i64`. See `Gas`.
gas_remaining: Pointer<B::Builder<'a>>,
/// The amount of gas remaining, without accounting for memory expansion. `i64`. See `Gas`.
gas_remaining_nomem: Pointer<B::Builder<'a>>,
/// The environment. Constant throughout the function.
env: B::Value,
/// The contract. Constant throughout the function.
Expand Down Expand Up @@ -186,11 +184,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> {
let name = "gas.remaining.addr";
Pointer::new_address(i64_type, bcx.gep(i8_type, gas_ptr, &[offset], name))
};
let gas_remaining_nomem = {
let offset = bcx.iconst(i64_type, mem::offset_of!(pf::Gas, remaining_nomem) as i64);
let name = "gas.remaining_nomem.addr";
Pointer::new_address(i64_type, bcx.gep(i8_type, gas_ptr, &[offset], name))
};

let sp_arg = bcx.fn_param(1);
let stack = if config.local_stack {
Expand Down Expand Up @@ -236,7 +229,6 @@ impl<'a, B: Backend> FunctionCx<'a, B> {
stack_len,
stack,
gas_remaining,
gas_remaining_nomem,
env,
contract,
ecx,
Expand Down Expand Up @@ -1325,18 +1317,19 @@ impl<'a, B: Backend> FunctionCx<'a, B> {
}

// Modified from `Gas::record_cost`.
// Group operations to allow for vectorization.
// This can overflow the gas counters, which has to be adjusted for after the call.
let gas_remaining = self.load_gas_remaining();
let nomem = self.gas_remaining_nomem.load(&mut self.bcx, "gas_remaining_nomem");

let (res, overflow) = self.bcx.usub_overflow(gas_remaining, cost);
let nomem = self.bcx.isub(nomem, cost);

self.store_gas_remaining(res);
self.gas_remaining_nomem.store(&mut self.bcx, nomem);

self.build_check(overflow, InstructionResult::OutOfGas);
if self.bytecode.is_small() {
// Storing the result before the check significantly increases time spent in
// `llvm::MemoryDependenceResults::getNonLocalPointerDependency`, but it might produce
// slightly better code.
self.store_gas_remaining(res);
self.build_check(overflow, InstructionResult::OutOfGas);
} else {
self.build_check(overflow, InstructionResult::OutOfGas);
self.store_gas_remaining(res);
}
}

/*
Expand Down Expand Up @@ -1540,6 +1533,7 @@ mod pf {
data: AtomicPtr<()>,
vtable: &'static Vtable,
}
const _: [(); mem::size_of::<revm_primitives::Bytes>()] = [(); mem::size_of::<Bytes>()];
struct Vtable {
/// fn(data, ptr, len)
clone: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> Bytes,
Expand All @@ -1562,13 +1556,10 @@ mod pf {
pub(super) limit: u64,
/// The remaining gas.
pub(super) remaining: u64,
/// The remaining gas, without memory expansion.
pub(super) remaining_nomem: u64,
/// The **last** memory expansion cost.
memory: u64,
/// Refunded gas. This is used only at the end of execution.
refunded: i64,
}
const _: [(); mem::size_of::<revm_interpreter::Gas>()] = [(); mem::size_of::<Gas>()];
}

fn op_block_name_with(op: Inst, data: &InstData, with: &str) -> String {
Expand Down
6 changes: 2 additions & 4 deletions crates/revm-jit/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,8 +826,7 @@ tests! {
output: Bytes::copy_from_slice(&0x69_U256.to_be_bytes::<32>()),
gas: {
let mut gas = Gas::new(DEF_GAS_LIMIT);
assert!(gas.record_cost(3 + 2 + 3 + 3 + 2));
assert!(gas.record_memory(gas::memory_gas(1)));
assert!(gas.record_cost(3 + 2 + (3 + gas::memory_gas(1)) + 3 + 2));
gas
},
},
Expand All @@ -844,8 +843,7 @@ tests! {
output: Bytes::copy_from_slice(&0x69_U256.to_be_bytes::<32>()),
gas: {
let mut gas = Gas::new(DEF_GAS_LIMIT);
assert!(gas.record_cost(3 + 2 + 3 + 3 + 2));
assert!(gas.record_memory(gas::memory_gas(1)));
assert!(gas.record_cost(3 + 2 + (3 + gas::memory_gas(1)) + 3 + 2));
gas
},
},
Expand Down

0 comments on commit bdb30cd

Please sign in to comment.