Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update revm gas #17

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 7 additions & 9 deletions crates/revm-jit-cli/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const SPEC_ID: SpecId = SpecId::CANCUN;

fn bench(c: &mut Criterion) {
for bench in &revm_jit_cli::get_benches() {
if matches!(bench.name, "push0_proxy" | "weth") {
continue;
}
run_bench(c, bench);
if matches!(bench.name, "hash_20k") {
break;
}
}
}

Expand Down Expand Up @@ -68,16 +68,14 @@ fn run_bench(c: &mut Criterion, bench: &Bench) {
host.clear();
let mut ecx = EvmContext::from_interpreter(&mut interpreter, &mut host);

let r = unsafe { f.call(Some(&mut stack), Some(&mut stack_len), &mut ecx) };
assert!(r.is_ok(), "JIT failed with {r:?}");
r
unsafe { f.call(Some(&mut stack), Some(&mut stack_len), &mut ecx) }
};

let jit_matrix = [
("default", (true, true)),
("no_gas", (false, true)),
("no_stack", (true, false)),
("no_gas_no_stack", (false, false)),
// ("no_stack", (true, false)),
// ("no_gas_no_stack", (false, false)),
];
let jit_ids = jit_matrix.map(|(name, (gas, stack))| {
compiler.gas_metering(gas);
Expand Down Expand Up @@ -112,7 +110,7 @@ fn run_bench(c: &mut Criterion, bench: &Bench) {

fn mk_group<'a>(c: &'a mut Criterion, name: &str) -> BenchmarkGroup<'a, WallTime> {
let mut g = c.benchmark_group(name);
g.sample_size(20);
g.sample_size(50);
g.warm_up_time(Duration::from_secs(5));
g.measurement_time(Duration::from_secs(15));
g
Expand Down
2 changes: 1 addition & 1 deletion crates/revm-jit-cli/benches/iai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn setup_group(group: &mut BinaryBenchmarkGroup, is_ct: bool) {
}
run.regression(regression);

if small && !is_ci() {
if small && !is_ci() && false { // Uses an insane amount of memory (???)
let flamegraph = FlamegraphConfig::default();
run.flamegraph(flamegraph);
}
Expand Down
14 changes: 7 additions & 7 deletions crates/revm-jit-cli/src/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ pub fn get_benches() -> Vec<Bench> {
calldata: hex!("30627b7c").to_vec(),
..Default::default()
},
Bench {
name: "push0_proxy",
bytecode: hex::decode(include_str!("../../../data/push0_proxy.rt.hex")).unwrap(),
..Default::default()
},
Bench {
name: "weth",
bytecode: hex::decode(include_str!("../../../data/weth.rt.hex")).unwrap(),
Expand All @@ -57,15 +52,20 @@ pub fn get_benches() -> Vec<Bench> {
..Default::default()
},
Bench {
name: "fiat_token",
bytecode: hex::decode(include_str!("../../../data/fiat_token.rt.hex")).unwrap(),
name: "push0_proxy",
bytecode: hex::decode(include_str!("../../../data/push0_proxy.rt.hex")).unwrap(),
..Default::default()
},
Bench {
name: "usdc_proxy",
bytecode: hex::decode(include_str!("../../../data/usdc_proxy.rt.hex")).unwrap(),
..Default::default()
},
Bench {
name: "fiat_token",
bytecode: hex::decode(include_str!("../../../data/fiat_token.rt.hex")).unwrap(),
..Default::default()
},
Bench {
name: "uniswap_v2_pair",
bytecode: hex::decode(include_str!("../../../data/uniswap_v2_pair.rt.hex")).unwrap(),
Expand Down
7 changes: 7 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,13 @@ impl<'a> Bytecode<'a> {
false
}

/// Returns `true` if the bytecode is small.
///
/// This is arbitarily chosen to speed up compilation for larger contracts.
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
34 changes: 13 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 @@ -1556,19 +1550,17 @@ mod pf {
pub(super) ptr: *const u8,
pub(super) len: usize,
}
const _: [(); mem::size_of::<&'static [u8]>()] = [(); mem::size_of::<Slice>()];

pub(super) struct Gas {
/// The initial gas limit. This is constant throughout execution.
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
Loading