Skip to content

Commit 5ec5319

Browse files
committed
Merge branch 'main' into felt-bigint
2 parents 1a24fb9 + c17aa23 commit 5ec5319

File tree

11 files changed

+128
-272
lines changed

11 files changed

+128
-272
lines changed

.github/workflows/hyperfine.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ jobs:
159159
hyperfine -N -r 10 --export-markdown "target_programs/{program}.md" \
160160
-L bin "$BINS" -n "{bin} {program}" \
161161
-s "cat ./bin/cairo-vm-cli-{bin} target_programs/{program}.json" \
162-
"./bin/cairo-vm-cli-{bin} --layout starknet_with_keccak \
162+
"./bin/cairo-vm-cli-{bin} --proof_mode --layout starknet_with_keccak \
163163
--memory_file /dev/null --trace_file /dev/null target_programs/{program}.json"
164164
echo "benchmark_count=$(ls target_programs/*.md | wc -l)" >> $GITHUB_OUTPUT
165165

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,10 @@
10061006
ids.remainder.d2 = remainder_split[2]
10071007
```
10081008

1009+
* BREAKING CHANGE: optimization for instruction decoding [#942](https://github.com/lambdaclass/cairo-rs/pull/942):
1010+
* Avoids copying immediate arguments to the `Instruction` structure, as they get inferred from the offset anyway
1011+
* Breaking: removal of the field `Instruction::imm`
1012+
10091013
* Add missing `\n` character in traceback string [#997](https://github.com/lambdaclass/cairo-rs/pull/997)
10101014
* BugFix: Add missing `\n` character after traceback lines when the filename is missing ("Unknown Location")
10111015

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ NORETROCOMPAT_DIR:=cairo_programs/noretrocompat
6565
NORETROCOMPAT_FILES:=$(wildcard $(NORETROCOMPAT_DIR)/*.cairo)
6666
COMPILED_NORETROCOMPAT_TESTS:=$(patsubst $(NORETROCOMPAT_DIR)/%.cairo, $(NORETROCOMPAT_DIR)/%.json, $(NORETROCOMPAT_FILES))
6767

68+
$(BENCH_DIR)/%.json: $(BENCH_DIR)/%.cairo
69+
cairo-compile --cairo_path="$(TEST_DIR):$(BENCH_DIR)" $< --output $@ --proof_mode
70+
6871
$(TEST_DIR)/%.json: $(TEST_DIR)/%.cairo
6972
cairo-compile --cairo_path="$(TEST_DIR):$(BENCH_DIR)" $< --output $@
7073

@@ -74,9 +77,6 @@ $(TEST_DIR)/%.rs.trace $(TEST_DIR)/%.rs.memory: $(TEST_DIR)/%.json $(RELBIN)
7477
$(TEST_DIR)/%.trace $(TEST_DIR)/%.memory: $(TEST_DIR)/%.json
7578
cairo-run --layout starknet_with_keccak --program $< --trace_file $@ --memory_file $(@D)/$(*F).memory
7679

77-
$(BENCH_DIR)/%.json: $(BENCH_DIR)/%.cairo
78-
cairo-compile --cairo_path="$(TEST_DIR):$(BENCH_DIR)" $< --output $@
79-
8080
$(NORETROCOMPAT_DIR)/%.json: $(NORETROCOMPAT_DIR)/%.cairo
8181
cairo-compile --cairo_path="$(TEST_DIR):$(BENCH_DIR):$(NORETROCOMPAT_DIR)" $< --output $@
8282

src/types/instruction.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub struct Instruction {
1515
pub off0: isize,
1616
pub off1: isize,
1717
pub off2: isize,
18-
pub imm: Option<Felt252>,
1918
pub dst_register: Register,
2019
pub op0_register: Register,
2120
pub op1_addr: Op1Addr,
@@ -75,20 +74,20 @@ pub enum Opcode {
7574

7675
impl Instruction {
7776
pub fn size(&self) -> usize {
78-
match self.imm {
79-
Some(_) => 2,
80-
None => 1,
77+
match self.op1_addr {
78+
Op1Addr::Imm => 2,
79+
_ => 1,
8180
}
8281
}
8382
}
8483

85-
// Returns True if the given instruction looks like a call instruction.
86-
pub(crate) fn is_call_instruction(encoded_instruction: &Felt252, imm: Option<&Felt252>) -> bool {
87-
let encoded_i64_instruction: i64 = match encoded_instruction.to_i64() {
84+
// Returns True if the given instruction looks like a call instruction
85+
pub(crate) fn is_call_instruction(encoded_instruction: &Felt252) -> bool {
86+
let encoded_i64_instruction = match encoded_instruction.to_u64() {
8887
Some(num) => num,
8988
None => return false,
9089
};
91-
let instruction = match decode_instruction(encoded_i64_instruction, imm) {
90+
let instruction = match decode_instruction(encoded_i64_instruction) {
9291
Ok(inst) => inst,
9392
Err(_) => return false,
9493
};
@@ -110,27 +109,28 @@ mod tests {
110109
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
111110
fn is_call_instruction_true() {
112111
let encoded_instruction = Felt252::new(1226245742482522112_i64);
113-
assert!(is_call_instruction(
114-
&encoded_instruction,
115-
Some(&Felt252::new(2))
116-
));
112+
assert!(is_call_instruction(&encoded_instruction));
117113
}
114+
118115
#[test]
119116
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
120117
fn is_call_instruction_false() {
121118
let encoded_instruction = Felt252::new(4612671187288031229_i64);
122-
assert!(!is_call_instruction(&encoded_instruction, None));
119+
assert!(!is_call_instruction(&encoded_instruction));
120+
}
121+
122+
#[test]
123+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
124+
fn is_call_instruction_invalid() {
125+
let encoded_instruction = Felt252::new(1u64 << 63);
126+
assert!(!is_call_instruction(&encoded_instruction));
123127
}
124128

125129
#[test]
126130
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
127131
fn instruction_size() {
128132
let encoded_instruction = Felt252::new(1226245742482522112_i64);
129-
let instruction = decode_instruction(
130-
encoded_instruction.to_i64().unwrap(),
131-
Some(&Felt252::new(2)),
132-
)
133-
.unwrap();
133+
let instruction = decode_instruction(encoded_instruction.to_u64().unwrap()).unwrap();
134134
assert_eq!(instruction.size(), 2);
135135
}
136136
}

src/utils.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub fn from_relocatable_to_indexes(relocatable: Relocatable) -> (usize, usize) {
5353
pub mod test_utils {
5454
use crate::types::exec_scope::ExecutionScopes;
5555
use crate::types::relocatable::MaybeRelocatable;
56+
use crate::vm::trace::trace_entry::TraceEntry;
5657

5758
#[macro_export]
5859
macro_rules! bigint {
@@ -354,23 +355,13 @@ pub mod test_utils {
354355
}
355356
pub(crate) use non_continuous_ids_data;
356357

357-
macro_rules! trace_check {
358-
( $trace: expr, [ $( ($off_pc:expr, $off_ap:expr, $off_fp:expr) ),+ ] ) => {
359-
let mut index = -1;
360-
$(
361-
index += 1;
362-
assert_eq!(
363-
$trace[index as usize],
364-
TraceEntry {
365-
pc: $off_pc,
366-
ap: $off_ap,
367-
fp: $off_fp,
368-
}
369-
);
370-
)*
371-
};
358+
#[track_caller]
359+
pub(crate) fn trace_check(actual: &[TraceEntry], expected: &[(usize, usize, usize)]) {
360+
assert_eq!(actual.len(), expected.len());
361+
for (entry, expected) in actual.iter().zip(expected.iter()) {
362+
assert_eq!(&(entry.pc, entry.ap, entry.fp), expected);
363+
}
372364
}
373-
pub(crate) use trace_check;
374365

375366
macro_rules! exec_scopes_ref {
376367
() => {
@@ -669,7 +660,7 @@ mod test {
669660
fp: 7,
670661
},
671662
];
672-
trace_check!(trace, [(2, 7, 1), (5, 1, 0), (9, 5, 7)]);
663+
trace_check(&trace, &[(2, 7, 1), (5, 1, 0), (9, 5, 7)]);
673664
}
674665

675666
#[test]

src/vm/context/run_context.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ mod tests {
118118
off0: 1,
119119
off1: 2,
120120
off2: 3,
121-
imm: None,
122121
dst_register: Register::AP,
123122
op0_register: Register::FP,
124123
op1_addr: Op1Addr::AP,
@@ -147,7 +146,6 @@ mod tests {
147146
off0: 1,
148147
off1: 2,
149148
off2: 3,
150-
imm: None,
151149
dst_register: Register::FP,
152150
op0_register: Register::AP,
153151
op1_addr: Op1Addr::AP,
@@ -177,7 +175,6 @@ mod tests {
177175
off0: 1,
178176
off1: 2,
179177
off2: 3,
180-
imm: None,
181178
dst_register: Register::AP,
182179
op0_register: Register::AP,
183180
op1_addr: Op1Addr::AP,
@@ -206,7 +203,6 @@ mod tests {
206203
off0: 1,
207204
off1: 2,
208205
off2: 3,
209-
imm: None,
210206
dst_register: Register::FP,
211207
op0_register: Register::FP,
212208
op1_addr: Op1Addr::AP,
@@ -235,7 +231,6 @@ mod tests {
235231
off0: 1,
236232
off1: 2,
237233
off2: 3,
238-
imm: None,
239234
dst_register: Register::FP,
240235
op0_register: Register::AP,
241236
op1_addr: Op1Addr::FP,
@@ -264,7 +259,6 @@ mod tests {
264259
off0: 1,
265260
off1: 2,
266261
off2: 3,
267-
imm: None,
268262
dst_register: Register::FP,
269263
op0_register: Register::AP,
270264
op1_addr: Op1Addr::AP,
@@ -293,7 +287,6 @@ mod tests {
293287
off0: 1,
294288
off1: 2,
295289
off2: 1,
296-
imm: None,
297290
dst_register: Register::FP,
298291
op0_register: Register::AP,
299292
op1_addr: Op1Addr::Imm,
@@ -322,7 +315,6 @@ mod tests {
322315
off0: 1,
323316
off1: 2,
324317
off2: 3,
325-
imm: None,
326318
dst_register: Register::FP,
327319
op0_register: Register::AP,
328320
op1_addr: Op1Addr::Imm,
@@ -354,7 +346,6 @@ mod tests {
354346
off0: 1,
355347
off1: 2,
356348
off2: 1,
357-
imm: None,
358349
dst_register: Register::FP,
359350
op0_register: Register::AP,
360351
op1_addr: Op1Addr::Op0,
@@ -385,7 +376,6 @@ mod tests {
385376
off0: 1,
386377
off1: 2,
387378
off2: 1,
388-
imm: None,
389379
dst_register: Register::FP,
390380
op0_register: Register::AP,
391381
op1_addr: Op1Addr::Op0,
@@ -418,7 +408,6 @@ mod tests {
418408
off0: 1,
419409
off1: 2,
420410
off2: 3,
421-
imm: None,
422411
dst_register: Register::FP,
423412
op0_register: Register::AP,
424413
op1_addr: Op1Addr::Op0,

0 commit comments

Comments
 (0)