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

Refactor arrays to use a reference-counted clone-on-write implementation. #876

Closed
wants to merge 6 commits into from
Closed
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 env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case $(uname) in
MLIR_SYS_190_PREFIX="$(brew --prefix llvm@19)"
LLVM_SYS_191_PREFIX="$(brew --prefix llvm@19)"
TABLEGEN_190_PREFIX="$(brew --prefix llvm@19)"
CAIRO_NATIVE_RUNTIME_LIBRARY="$(pwd)/target/debug/libcairo_native_runtime.a"
CAIRO_NATIVE_RUNTIME_LIBRARY="$(pwd)/target/release/libcairo_native_runtime.a"

export LIBRARY_PATH
export MLIR_SYS_190_PREFIX
Expand All @@ -24,7 +24,7 @@ case $(uname) in
MLIR_SYS_190_PREFIX=/usr/lib/llvm-19
LLVM_SYS_191_PREFIX=/usr/lib/llvm-19
TABLEGEN_190_PREFIX=/usr/lib/llvm-19
CAIRO_NATIVE_RUNTIME_LIBRARY="$(pwd)/target/debug/libcairo_native_runtime.a"
CAIRO_NATIVE_RUNTIME_LIBRARY="$(pwd)/target/release/libcairo_native_runtime.a"

export MLIR_SYS_190_PREFIX
export LLVM_SYS_191_PREFIX
Expand Down
37 changes: 25 additions & 12 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,24 +336,32 @@ fn parse_result(
}

match type_info {
CoreTypeConcrete::Array(_) => Ok(Value::from_ptr(return_ptr.unwrap(), type_id, registry)?),
CoreTypeConcrete::Array(_) => Ok(Value::from_ptr(
return_ptr.unwrap(),
type_id,
registry,
true,
)?),
CoreTypeConcrete::Box(info) => unsafe {
let ptr = return_ptr.unwrap_or(NonNull::new_unchecked(ret_registers[0] as *mut ()));
let value = Value::from_ptr(ptr, &info.ty, registry)?;
let value = Value::from_ptr(ptr, &info.ty, registry, true)?;
libc_free(ptr.cast().as_ptr());
Ok(value)
},
CoreTypeConcrete::EcPoint(_) | CoreTypeConcrete::EcState(_) => {
Ok(Value::from_ptr(return_ptr.unwrap(), type_id, registry)?)
}
CoreTypeConcrete::EcPoint(_) | CoreTypeConcrete::EcState(_) => Ok(Value::from_ptr(
return_ptr.unwrap(),
type_id,
registry,
true,
)?),
CoreTypeConcrete::Felt252(_)
| CoreTypeConcrete::StarkNet(
StarkNetTypeConcrete::ClassHash(_)
| StarkNetTypeConcrete::ContractAddress(_)
| StarkNetTypeConcrete::StorageAddress(_)
| StarkNetTypeConcrete::StorageBaseAddress(_),
) => match return_ptr {
Some(return_ptr) => Ok(Value::from_ptr(return_ptr, type_id, registry)?),
Some(return_ptr) => Ok(Value::from_ptr(return_ptr, type_id, registry, true)?),
None => {
#[cfg(target_arch = "x86_64")]
// Since x86_64's return values hold at most two different 64bit registers,
Expand All @@ -372,7 +380,7 @@ fn parse_result(
}
},
CoreTypeConcrete::Bytes31(_) => match return_ptr {
Some(return_ptr) => Ok(Value::from_ptr(return_ptr, type_id, registry)?),
Some(return_ptr) => Ok(Value::from_ptr(return_ptr, type_id, registry, true)?),
None => {
#[cfg(target_arch = "x86_64")]
// Since x86_64's return values hold at most two different 64bit registers,
Expand All @@ -387,7 +395,7 @@ fn parse_result(
}
},
CoreTypeConcrete::BoundedInt(info) => match return_ptr {
Some(return_ptr) => Ok(Value::from_ptr(return_ptr, type_id, registry)?),
Some(return_ptr) => Ok(Value::from_ptr(return_ptr, type_id, registry, true)?),
None => {
let mut data = if info.range.offset_bit_width() <= 64 {
BigInt::from(ret_registers[0])
Expand Down Expand Up @@ -459,7 +467,7 @@ fn parse_result(
Ok(Value::Null)
} else {
let ptr = NonNull::new_unchecked(ptr);
let value = Value::from_ptr(ptr, &info.ty, registry)?;
let value = Value::from_ptr(ptr, &info.ty, registry, true)?;
libc_free(ptr.as_ptr().cast());
Ok(value)
}
Expand Down Expand Up @@ -515,7 +523,7 @@ fn parse_result(
}
};
let value = match ptr {
Ok(ptr) => Box::new(Value::from_ptr(ptr, &info.variants[tag], registry)?),
Ok(ptr) => Box::new(Value::from_ptr(ptr, &info.variants[tag], registry, true)?),
Err(offset) => {
ret_registers.copy_within(offset.., 0);
Box::new(parse_result(
Expand All @@ -540,14 +548,19 @@ fn parse_result(
debug_name: Some(debug_name),
})
} else {
Ok(Value::from_ptr(return_ptr.unwrap(), type_id, registry)?)
Ok(Value::from_ptr(
return_ptr.unwrap(),
type_id,
registry,
true,
)?)
}
}
CoreTypeConcrete::Felt252Dict(_) | CoreTypeConcrete::SquashedFelt252Dict(_) => unsafe {
let ptr = return_ptr.unwrap_or(NonNull::new_unchecked(
addr_of_mut!(ret_registers[0]) as *mut ()
));
Ok(Value::from_ptr(ptr, type_id, registry)?)
Ok(Value::from_ptr(ptr, type_id, registry, true)?)
},

CoreTypeConcrete::Snapshot(info) => {
Expand Down
20 changes: 18 additions & 2 deletions src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,20 @@ impl AotContractExecutor {
}

let felt_layout = get_integer_layout(252).pad_to_align();
let ptr: *mut () = unsafe { libc_malloc(felt_layout.size() * args.len()).cast() };
let refcount_offset = get_integer_layout(32)
.align_to(felt_layout.align())
.unwrap()
.pad_to_align()
.size();

let ptr: *mut () =
unsafe { libc_malloc(felt_layout.size() * args.len() + refcount_offset).cast() };
let len: u32 = args.len().try_into().unwrap();

// Write reference count.
unsafe { ptr.cast::<u32>().write(1) };
let ptr = unsafe { ptr.byte_add(refcount_offset) };

ptr.to_bytes(&mut invoke_data)?;
0u32.to_bytes(&mut invoke_data)?; // start
len.to_bytes(&mut invoke_data)?; // end
Expand Down Expand Up @@ -392,7 +403,12 @@ impl AotContractExecutor {
}

if !array_ptr.is_null() {
unsafe { libc_free(array_ptr.cast()) };
unsafe {
let ptr = array_ptr.byte_sub(refcount_offset);
assert_eq!(ptr.cast::<u32>().read(), 1);

libc_free(ptr.cast());
};
}

let mut error_msg = None;
Expand Down
Loading
Loading