Skip to content

Commit

Permalink
start porting gas to u64
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Oct 29, 2024
1 parent ab47832 commit 3f2b65c
Show file tree
Hide file tree
Showing 30 changed files with 256 additions and 165 deletions.
12 changes: 6 additions & 6 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ fn criterion_benchmark(c: &mut Criterion) {
find_function_id(&logistic_map, "logistic_map::logistic_map::main").unwrap();

c.bench_function("Cached JIT factorial_2M", |b| {
b.iter(|| jit_factorial.invoke_dynamic(factorial_function_id, &[], Some(u128::MAX)));
b.iter(|| jit_factorial.invoke_dynamic(factorial_function_id, &[], Some(u64::MAX)));
});
c.bench_function("Cached JIT fib_2M", |b| {
b.iter(|| jit_fibonacci.invoke_dynamic(fibonacci_function_id, &[], Some(u128::MAX)));
b.iter(|| jit_fibonacci.invoke_dynamic(fibonacci_function_id, &[], Some(u64::MAX)));
});
c.bench_function("Cached JIT logistic_map", |b| {
b.iter(|| jit_logistic_map.invoke_dynamic(logistic_map_function_id, &[], Some(u128::MAX)));
b.iter(|| jit_logistic_map.invoke_dynamic(logistic_map_function_id, &[], Some(u64::MAX)));
});

c.bench_function("Cached AOT factorial_2M", |b| {
b.iter(|| aot_factorial.invoke_dynamic(factorial_function_id, &[], Some(u128::MAX)));
b.iter(|| aot_factorial.invoke_dynamic(factorial_function_id, &[], Some(u64::MAX)));
});
c.bench_function("Cached AOT fib_2M", |b| {
b.iter(|| aot_fibonacci.invoke_dynamic(fibonacci_function_id, &[], Some(u128::MAX)));
b.iter(|| aot_fibonacci.invoke_dynamic(fibonacci_function_id, &[], Some(u64::MAX)));
});
c.bench_function("Cached AOT logistic_map", |b| {
b.iter(|| aot_logistic_map.invoke_dynamic(logistic_map_function_id, &[], Some(u128::MAX)));
b.iter(|| aot_logistic_map.invoke_dynamic(logistic_map_function_id, &[], Some(u64::MAX)));
});
}

Expand Down
12 changes: 6 additions & 6 deletions benches/libfuncs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn bench_libfuncs(c: &mut Criterion) {

// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX as u128))
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
Expand All @@ -81,14 +81,14 @@ pub fn bench_libfuncs(c: &mut Criterion) {
// warmup
for _ in 0..5 {
native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX as u128))
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
}

b.iter(|| {
// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX as u128))
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
Expand All @@ -108,7 +108,7 @@ pub fn bench_libfuncs(c: &mut Criterion) {

// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX as u128))
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
Expand All @@ -128,14 +128,14 @@ pub fn bench_libfuncs(c: &mut Criterion) {
// warmup
for _ in 0..5 {
native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX as u128))
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
}

b.iter(|| {
// Execute the program.
let result = native_executor
.invoke_dynamic(&entry.id, &[], Some(u64::MAX as u128))
.invoke_dynamic(&entry.id, &[], Some(u64::MAX))
.unwrap();
black_box(result)
})
Expand Down
3 changes: 1 addition & 2 deletions docs/gas_builtin_accounting.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ and greged’s about
### Gas builtin
The gas builtin is used in Sierra in order to perform gas accounting. It is
passed as an input to all function calls and holds the current remaining
gas. It is represented in MLIR by a simple `u128`.
gas. It is represented in MLIR by a simple `u64`.

### Gas metadata
The process of calculating gas begins at the very outset of the compilation
Expand Down Expand Up @@ -236,4 +236,3 @@ constant 1.
When this compiled MLIR code is called, the initial value of all builtin
counters is set to `0` as can be seen in the
[`invoke_dynamic` function](../src/executor.rs#L240).

2 changes: 1 addition & 1 deletion examples/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ fn main() {
Felt::from(4),
Felt::from(6),
],
Some(u128::MAX),
Some(u64::MAX),
SyscallHandler,
)
.expect("failed to execute the given contract");
Expand Down
2 changes: 1 addition & 1 deletion examples/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ fn main() {
let native_executor = JitNativeExecutor::from_native_module(native_program, Default::default());

let result = native_executor
.invoke_contract_dynamic(fn_id, &[Felt::ONE], Some(u128::MAX), SyscallHandler::new())
.invoke_contract_dynamic(fn_id, &[Felt::ONE], Some(u64::MAX), SyscallHandler::new())
.expect("failed to execute the given contract");

println!();
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cairo-native-run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Args {
allow_warnings: bool,
/// In cases where gas is available, the amount of provided gas.
#[arg(long)]
available_gas: Option<u128>,
available_gas: Option<u64>,
/// Run with JIT or AOT (compiled).
#[arg(long, value_enum, default_value_t = RunMode::Jit)]
run_mode: RunMode,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cairo-native-stress/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn main() {
let execution_result = {
let now = Instant::now();
let execution_result = executor
.invoke_contract_dynamic(&entry_point, &[], Some(u128::MAX), DummySyscallHandler)
.invoke_contract_dynamic(&entry_point, &[], Some(u64::MAX), DummySyscallHandler)
.expect("failed to execute contract");
let elapsed = now.elapsed().as_millis();
let result = execution_result.return_values[0];
Expand Down
2 changes: 1 addition & 1 deletion src/cache/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mod tests {
let function_id = &program.funcs.first().expect("should have a function").id;
let executor = cache.compile_and_insert((), &program, OptLevel::default());
let res = executor
.invoke_dynamic(function_id, &[], Some(u128::MAX))
.invoke_dynamic(function_id, &[], Some(u64::MAX))
.expect("should run");

// After compiling and inserting the program, we should be able to run it.
Expand Down
4 changes: 2 additions & 2 deletions src/execution_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct BuiltinStats {
/// The result of the JIT execution.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ExecutionResult {
pub remaining_gas: Option<u128>,
pub remaining_gas: Option<u64>,
pub return_value: Value,
pub builtin_stats: BuiltinStats,
}
Expand All @@ -52,7 +52,7 @@ pub struct ExecutionResult {
serde::Deserialize,
)]
pub struct ContractExecutionResult {
pub remaining_gas: u128,
pub remaining_gas: u64,
pub failure_flag: bool,
pub return_values: Vec<Felt>,
pub error_msg: Option<String>,
Expand Down
14 changes: 7 additions & 7 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn invoke_dynamic(
function_ptr: *const c_void,
function_signature: &FunctionSignature,
args: &[Value],
gas: u128,
gas: u64,
mut syscall_handler: Option<impl StarknetSyscallHandler>,
) -> Result<ExecutionResult, Error> {
tracing::info!("Invoking function with signature: {function_signature:?}.");
Expand Down Expand Up @@ -232,11 +232,11 @@ fn invoke_dynamic(
match type_info {
CoreTypeConcrete::GasBuiltin(_) => {
remaining_gas = Some(match &mut return_ptr {
Some(return_ptr) => unsafe { *read_value::<u128>(return_ptr) },
Some(return_ptr) => unsafe { *read_value::<u64>(return_ptr) },
None => {
// If there's no return ptr then the function only returned the gas. We don't
// need to bother with the syscall handler builtin.
((ret_registers[1] as u128) << 64) | ret_registers[0] as u128
ret_registers[0]
}
});
}
Expand Down Expand Up @@ -641,7 +641,7 @@ mod tests {
let entrypoint_function_id = &program.funcs.first().expect("should have a function").id;

let result = executor
.invoke_dynamic(entrypoint_function_id, &[], Some(u128::MAX))
.invoke_dynamic(entrypoint_function_id, &[], Some(u64::MAX))
.unwrap();

assert_eq!(result.return_value, Value::Felt252(Felt::from(42)));
Expand All @@ -659,7 +659,7 @@ mod tests {
let entrypoint_function_id = &program.funcs.first().expect("should have a function").id;

let result = executor
.invoke_dynamic(entrypoint_function_id, &[], Some(u128::MAX))
.invoke_dynamic(entrypoint_function_id, &[], Some(u64::MAX))
.unwrap();

assert_eq!(result.return_value, Value::Felt252(Felt::from(42)));
Expand All @@ -684,7 +684,7 @@ mod tests {
.invoke_contract_dynamic(
entrypoint_function_id,
&[],
Some(u128::MAX),
Some(u64::MAX),
&mut StubSyscallHandler::default(),
)
.unwrap();
Expand All @@ -711,7 +711,7 @@ mod tests {
.invoke_contract_dynamic(
entrypoint_function_id,
&[],
Some(u128::MAX),
Some(u64::MAX),
&mut StubSyscallHandler::default(),
)
.unwrap();
Expand Down
12 changes: 6 additions & 6 deletions src/executor/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl AotNativeExecutor {
&self,
function_id: &FunctionId,
args: &[Value],
gas: Option<u128>,
gas: Option<u64>,
) -> Result<ExecutionResult, Error> {
let available_gas = self
.gas_metadata
Expand All @@ -92,7 +92,7 @@ impl AotNativeExecutor {
&self,
function_id: &FunctionId,
args: &[Value],
gas: Option<u128>,
gas: Option<u64>,
syscall_handler: impl StarknetSyscallHandler,
) -> Result<ExecutionResult, Error> {
let available_gas = self
Expand All @@ -114,7 +114,7 @@ impl AotNativeExecutor {
&self,
function_id: &FunctionId,
args: &[Felt],
gas: Option<u128>,
gas: Option<u64>,
syscall_handler: impl StarknetSyscallHandler,
) -> Result<ContractExecutionResult, Error> {
let available_gas = self
Expand Down Expand Up @@ -223,7 +223,7 @@ mod tests {
let entrypoint_function_id = &program.funcs.first().expect("should have a function").id;

let result = executor
.invoke_dynamic(entrypoint_function_id, &[], Some(u128::MAX))
.invoke_dynamic(entrypoint_function_id, &[], Some(u64::MAX))
.unwrap();

assert_eq!(result.return_value, Value::Felt252(Felt::from(42)));
Expand Down Expand Up @@ -251,7 +251,7 @@ mod tests {
.invoke_dynamic_with_syscall_handler(
entrypoint_function_id,
&[],
Some(u128::MAX),
Some(u64::MAX),
syscall_handler,
)
.unwrap();
Expand Down Expand Up @@ -290,7 +290,7 @@ mod tests {
.invoke_contract_dynamic(
entrypoint_function_id,
&[],
Some(u128::MAX),
Some(u64::MAX),
&mut StubSyscallHandler::default(),
)
.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl AotContractExecutor {
&self,
function_id: &FunctionId,
args: &[Felt],
gas: Option<u128>,
gas: Option<u64>,
mut syscall_handler: impl StarknetSyscallHandler,
) -> Result<ContractExecutionResult> {
let arena = Bump::new();
Expand All @@ -235,7 +235,7 @@ impl AotContractExecutor {
match b {
BuiltinType::Gas => {
let gas = gas.unwrap_or(0);
gas.to_bytes(&mut invoke_data)?;
(gas as u128).to_bytes(&mut invoke_data)?;
}
BuiltinType::System => {
(&mut syscall_handler as *mut StarknetSyscallHandlerCallbacks<_>)
Expand Down Expand Up @@ -314,7 +314,7 @@ impl AotContractExecutor {
for b in &self.entry_points_info[&function_id.id].builtins {
match b {
BuiltinType::Gas => {
remaining_gas = unsafe { *read_value::<u128>(return_ptr) };
remaining_gas = unsafe { *read_value::<u64>(return_ptr) };
}
BuiltinType::System => {
let ptr = return_ptr.cast::<*mut ()>();
Expand Down Expand Up @@ -518,7 +518,7 @@ mod tests {
.run(
entrypoint_function_id,
&[2.into()],
Some(u64::MAX as u128),
Some(u64::MAX),
&mut StubSyscallHandler::default(),
)
.unwrap();
Expand All @@ -543,7 +543,7 @@ mod tests {
.run(
entrypoint_function_id,
&[],
Some(u64::MAX as u128),
Some(u64::MAX),
&mut StubSyscallHandler::default(),
)
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/executor/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'m> JitNativeExecutor<'m> {
&self,
function_id: &FunctionId,
args: &[Value],
gas: Option<u128>,
gas: Option<u64>,
) -> Result<ExecutionResult, Error> {
let available_gas = self
.gas_metadata
Expand All @@ -91,7 +91,7 @@ impl<'m> JitNativeExecutor<'m> {
&self,
function_id: &FunctionId,
args: &[Value],
gas: Option<u128>,
gas: Option<u64>,
syscall_handler: impl StarknetSyscallHandler,
) -> Result<ExecutionResult, Error> {
let available_gas = self
Expand All @@ -113,7 +113,7 @@ impl<'m> JitNativeExecutor<'m> {
&self,
function_id: &FunctionId,
args: &[Felt],
gas: Option<u128>,
gas: Option<u64>,
syscall_handler: impl StarknetSyscallHandler,
) -> Result<ContractExecutionResult, Error> {
let available_gas = self
Expand Down
Loading

0 comments on commit 3f2b65c

Please sign in to comment.