Skip to content

Commit

Permalink
fix mrg
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick committed May 8, 2024
1 parent 5bd26a9 commit 607e60e
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 33 deletions.
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
.single_threaded = true,
});
exe.linkLibC();
// Add dependency modules to the executable.
for (deps) |mod| exe.root_module.addImport(
mod.name,
Expand Down
6 changes: 3 additions & 3 deletions cairo_programs/benchmarks/compare_arrays_200000.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"end_col": 38,
"end_line": 3,
"input_file": {
"filename": "/Users/nikita/Projects/hobby/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo"
"filename": "/home/nikita/Projects/starknet/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo"
},
"start_col": 5,
"start_line": 3
Expand All @@ -198,7 +198,7 @@
"end_col": 12,
"end_line": 4,
"input_file": {
"filename": "/Users/nikita/Projects/hobby/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo"
"filename": "/home/nikita/Projects/starknet/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo"
},
"start_col": 5,
"start_line": 4
Expand All @@ -221,7 +221,7 @@
"end_col": 40,
"end_line": 5,
"input_file": {
"filename": "/Users/nikita/Projects/hobby/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo"
"filename": "/home/nikita/Projects/starknet/ziggy-starkdust/cairo-vm-env/lib/python3.12/site-packages/starkware/cairo/common/alloc.cairo"
},
"start_col": 5,
"start_line": 5
Expand Down
2 changes: 1 addition & 1 deletion scripts/benchmarks.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

set -e
source ../cairo-vm-env/bin/activate
. ../cairo-vm-env/bin/activate

BENCH_DIR=../cairo_programs/benchmarks
CAIRO_DIR=../cairo_programs/
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const CairoRunner = cairo_runner.CairoRunner;
const ProgramJson = @import("../vm/types/programjson.zig").ProgramJson;
const cairo_run = @import("../vm/cairo_run.zig");

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const gpa_allocator = gpa.allocator();
// var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const gpa_allocator = std.heap.c_allocator;

// Configuration settings for the CLI.
var config = Config{
Expand Down
2 changes: 1 addition & 1 deletion src/vm/cairo_run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn runConfig(allocator: Allocator, config: Config) !void {
try runner.runUntilPC(end, false, &hint_processor);
try runner.endRun(
allocator,
true,
false,
false,
&hint_processor,
false,
Expand Down
22 changes: 7 additions & 15 deletions src/vm/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ pub const CairoVM = struct {

pub fn stepHintExtensive(
self: *Self,
allocator: Allocator,
hint_processor: HintProcessor,
exec_scopes: *ExecutionScopes,
hint_datas: *std.ArrayList(HintData),
Expand All @@ -275,7 +274,7 @@ pub const CairoVM = struct {
for (hint_range.start..hint_range.start + hint_range.length) |idx| {
const hint_data = if (idx < hint_datas.items.len) &hint_datas.items[idx] else return CairoVMError.Unexpected;

var hint_extension = try hint_processor.executeHintExtensive(allocator, self, hint_data, constants, exec_scopes);
var hint_extension = try hint_processor.executeHintExtensive(self.allocator, self, hint_data, constants, exec_scopes);
defer hint_extension.deinit();

var it = hint_extension.iterator();
Expand All @@ -291,14 +290,13 @@ pub const CairoVM = struct {

pub fn stepHintNotExtensive(
self: *Self,
allocator: Allocator,
hint_processor: HintProcessor,
exec_scopes: *ExecutionScopes,
hint_datas: []HintData,
constants: *std.StringHashMap(Felt252),
) !void {
for (hint_datas) |*hint_data| {
try hint_processor.executeHint(allocator, self, hint_data, constants, exec_scopes);
try hint_processor.executeHint(self.allocator, self, hint_data, constants, exec_scopes);
}
}

Expand All @@ -315,7 +313,7 @@ pub const CairoVM = struct {
/// - `allocator`: The allocator used for memory operations.
/// # Errors
/// - If accessing an unknown memory cell occurs.
pub fn stepInstruction(self: *Self, allocator: Allocator) !void {
pub fn stepInstruction(self: *Self) !void {
const inst = if (self.run_context.pc.segment_index == 0) value: {
// Run instructions from the program segment, using the instruction cache.
const pc = self.run_context.pc.offset;
Expand Down Expand Up @@ -344,7 +342,6 @@ pub const CairoVM = struct {
// Execute the instruction if skip_instruction_execution is false.
if (!self.skip_instruction_execution) {
try self.runInstruction(
allocator,
inst,
);
} else {
Expand All @@ -358,42 +355,38 @@ pub const CairoVM = struct {
/// Process an instruction cycle using the typical fetch-decode-execute cycle.
pub fn stepNotExtensive(
self: *Self,
allocator: Allocator,
hint_processor: HintProcessor,
exec_scopes: *ExecutionScopes,
hint_datas: []HintData,
constants: *std.StringHashMap(Felt252),
) !void {
try self.stepHintNotExtensive(
allocator,
hint_processor,
exec_scopes,
hint_datas,
constants,
);
try self.stepInstruction(allocator);
try self.stepInstruction();
}

/// Do a single step of the VM with extensive hints.
/// Process an instruction cycle using the typical fetch-decode-execute cycle.
pub fn stepExtensive(
self: *Self,
allocator: Allocator,
hint_processor: HintProcessor,
exec_scopes: *ExecutionScopes,
hint_datas: *std.ArrayList(HintData),
hint_ranges: *std.AutoHashMap(Relocatable, HintRange),
constants: *std.StringHashMap(Felt252),
) !void {
try self.stepHintExtensive(
allocator,
hint_processor,
exec_scopes,
hint_datas,
hint_ranges,
constants,
);
try self.stepInstruction(allocator);
try self.stepInstruction();
}

/// Insert Operands only after checking if they were deduced.
Expand Down Expand Up @@ -463,7 +456,6 @@ pub const CairoVM = struct {
/// a controlled environment to ensure the correct execution of instructions and memory operations.
pub fn runInstruction(
self: *Self,
allocator: Allocator,
instruction: Instruction,
) !void {
// Check if tracing is disabled and log the current state if not.
Expand All @@ -478,10 +470,10 @@ pub const CairoVM = struct {
}

// Compute operands for the instruction.
const operands_result = try self.computeOperands(allocator, instruction);
const operands_result = try self.computeOperands(self.allocator, instruction);

// Insert deduced operands into memory.
try self.insertDeducedOperands(allocator, operands_result);
try self.insertDeducedOperands(self.allocator, operands_result);

// Perform opcode-specific assertions on operands using the `opcodeAssertions` function.
try self.opcodeAssertions(instruction, operands_result);
Expand Down
11 changes: 5 additions & 6 deletions src/vm/memory/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pub const Memory = struct {
/// # Returns
///
/// Returns a reference to the data in the form of `std.ArrayList(std.ArrayListUnmanaged(?MemoryCell))`.
pub inline fn getDataFromSegmentIndex(
pub fn getDataFromSegmentIndex(
self: *const Self,
segment_index: i64,
) []std.ArrayListUnmanaged(?MemoryCell) {
Expand Down Expand Up @@ -408,16 +408,15 @@ pub const Memory = struct {
const segment_index = address.getAdjustedSegmentIndex();

// Check if the segment index is valid within the data structure.
const isSegmentIndexValid = address.segment_index < data.len;
if (segment_index >= data.len) return null;
const segment_data = data[segment_index].items;

// Check if the offset is valid within the specified segment.
const isOffsetValid = isSegmentIndexValid and (address.offset < data[segment_index].items.len);
if (address.offset >= segment_data.len) return null;

// Return null if either the segment index or offset is not valid.
// Otherwise, return the maybe_relocatable value at the specified address.
return if (!isSegmentIndexValid or !isOffsetValid)
null
else if (data[segment_index].items[address.offset]) |val|
return if (segment_data[address.offset]) |val|
switch (val.maybe_relocatable) {
.relocatable => |addr| Self.relocateAddress(addr, self.relocation_rules) catch unreachable,
else => |_| val.maybe_relocatable,
Expand Down
2 changes: 1 addition & 1 deletion src/vm/memory/relocatable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub const Relocatable = struct {
///
/// # Returns
/// The adjusted `usize` value representing the segment index.
pub inline fn getAdjustedSegmentIndex(self: *const Self) usize {
pub inline fn getAdjustedSegmentIndex(self: Self) usize {
return @intCast(if (self.segment_index < 0)
-(self.segment_index + 1)
else
Expand Down
4 changes: 0 additions & 4 deletions src/vm/runners/cairo_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ pub const CairoRunner = struct {
while (!end.eq(self.vm.run_context.pc) and !hint_processor.run_resources.consumed()) {
if (extensive_hints) {
try self.vm.stepExtensive(
self.allocator,
hint_processor.*,
&self.execution_scopes,
&hint_datas,
Expand All @@ -576,7 +575,6 @@ pub const CairoRunner = struct {
);
} else {
try self.vm.stepNotExtensive(
self.allocator,
hint_processor.*,
&self.execution_scopes,
if (self.program
Expand Down Expand Up @@ -713,7 +711,6 @@ pub const CairoRunner = struct {
// Execute a single step of the program, considering extensive or non-extensive hints
if (extensive_hints) {
try self.vm.stepExtensive(
self.allocator,
hint_processor.*,
&self.execution_scopes,
&hint_datas,
Expand All @@ -722,7 +719,6 @@ pub const CairoRunner = struct {
);
} else {
try self.vm.stepNotExtensive(
self.allocator,
hint_processor.*,
&self.execution_scopes,
hint_data_final,
Expand Down

0 comments on commit 607e60e

Please sign in to comment.