Skip to content

Commit

Permalink
Fix snapshot_take for non-trivially-copyable types (ex. Array<T>). (
Browse files Browse the repository at this point in the history
#338)

* Add `snapshot_take` metadata.

* Override `take_snapshot` for `Array<T>`.

* Fix `memcpy` intrinsic.

* Apply formatting.

---------

Co-authored-by: Esteve Soler Arderiu <[email protected]>
  • Loading branch information
azteca1998 and azteca1998 authored Oct 31, 2023
1 parent 113be0b commit 4012a10
Show file tree
Hide file tree
Showing 59 changed files with 1,220 additions and 412 deletions.
5 changes: 3 additions & 2 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ where
&param.id,
registry
.get_type(ty)?
.build(context, module, registry, metadata_storage)
.build(context, module, registry, metadata_storage, ty)
.map_err(make_type_builder_error(ty))?,
))
})
Expand Down Expand Up @@ -574,6 +574,7 @@ where
module,
registry,
metadata_storage,
&var_info.ty,
)
.map_err(make_type_builder_error(&var_info.ty))
},
Expand Down Expand Up @@ -701,7 +702,7 @@ where
type_ids.iter().map(|id| {
registry
.get_type(id)?
.build(context, module, registry, metadata_storage)
.build(context, module, registry, metadata_storage, id)
.map_err(make_type_builder_error(id))
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/libfuncs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ where

impl<TType, TLibfunc> LibfuncBuilder<TType, TLibfunc> for CoreConcreteLibfunc
where
TType: GenericType,
TLibfunc: GenericLibfunc<Concrete = Self>,
TType: 'static + GenericType,
TLibfunc: 'static + GenericLibfunc<Concrete = Self>,
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
{
type Error = CoreLibfuncBuilderError;
Expand Down
109 changes: 67 additions & 42 deletions src/libfuncs/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
},
metadata::{realloc_bindings::ReallocBindingsMeta, MetadataStorage},
types::TypeBuilder,
utils::ProgramRegistryExt,
};
use cairo_lang_sierra::{
extensions::{
Expand Down Expand Up @@ -101,9 +102,13 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
let array_ty = registry
.get_type(&info.branch_signatures()[0].vars[0].ty)?
.build(context, helper, registry, metadata)?;
let array_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;

let op0 = entry.append_operation(
OperationBuilder::new("llvm.mlir.null", location)
Expand Down Expand Up @@ -164,22 +169,21 @@ where
metadata.insert(ReallocBindingsMeta::new(context, helper));
}

let array_ty = registry
.get_type(&info.param_signatures()[0].ty)?
.build(context, helper, registry, metadata)?;
let array_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.param_signatures()[0].ty,
)?;

let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0);
let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1);
let opaque_ptr_ty = llvm::r#type::opaque_pointer(context);

let elem_concrete_ty = registry.get_type(&info.ty)?;
let elem_ty = elem_concrete_ty.build(context, helper, registry, metadata)?;

let elem_stride = registry
.get_type(&info.ty)?
.layout(registry)?
.pad_to_align()
.size();
let (elem_ty, elem_layout) =
registry.build_type_with_layout(context, helper, registry, metadata, &info.ty)?;
let elem_stride = elem_layout.pad_to_align().size();

let op_ptr = entry.append_operation(llvm::extract_value(
context,
Expand Down Expand Up @@ -373,9 +377,13 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
let array_ty = registry
.get_type(&info.param_signatures()[0].ty)?
.build(context, helper, registry, metadata)?;
let array_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.param_signatures()[0].ty,
)?;

let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1);

Expand Down Expand Up @@ -413,13 +421,16 @@ where
metadata.insert(ReallocBindingsMeta::new(context, helper));
}

let array_ty = registry
.get_type(&info.param_signatures()[1].ty)?
.build(context, helper, registry, metadata)?;
let array_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.param_signatures()[1].ty,
)?;

let elem_concrete_ty = registry.get_type(&info.ty)?;
let elem_layout = elem_concrete_ty.layout(registry)?;
let elem_ty = elem_concrete_ty.build(context, helper, registry, metadata)?;
let (elem_ty, elem_layout) =
registry.build_type_with_layout(context, helper, registry, metadata, &info.ty)?;

let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0);
let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1);
Expand Down Expand Up @@ -557,13 +568,16 @@ where
metadata.insert(ReallocBindingsMeta::new(context, helper));
}

let array_ty = registry
.get_type(&info.param_signatures()[0].ty)?
.build(context, helper, registry, metadata)?;
let array_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.param_signatures()[0].ty,
)?;

let elem_concrete_ty = registry.get_type(&info.ty)?;
let elem_layout = elem_concrete_ty.layout(registry)?;
let elem_ty = elem_concrete_ty.build(context, helper, registry, metadata)?;
let (elem_ty, elem_layout) =
registry.build_type_with_layout(context, helper, registry, metadata, &info.ty)?;

let elem_stride = registry
.get_type(&info.ty)?
Expand Down Expand Up @@ -834,13 +848,21 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
let array_ty = registry
.get_type(&info.param_signatures()[0].ty)?
.build(context, helper, registry, metadata)?;
let array_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.param_signatures()[0].ty,
)?;

let elem_concrete_ty = registry.get_type(&info.branch_signatures()[0].vars[1].ty)?;
let elem_layout = elem_concrete_ty.layout(registry)?;
let elem_ty = elem_concrete_ty.build(context, helper, registry, metadata)?;
let (elem_ty, elem_layout) = registry.build_type_with_layout(
context,
helper,
registry,
metadata,
&info.branch_signatures()[0].vars[1].ty,
)?;

let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0);
let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1);
Expand Down Expand Up @@ -977,13 +999,16 @@ where
metadata.insert(ReallocBindingsMeta::new(context, helper));
}

let array_ty = registry
.get_type(&info.param_signatures()[1].ty)?
.build(context, helper, registry, metadata)?;

let elem_concrete_ty = registry.get_type(&info.ty)?;
let elem_layout = elem_concrete_ty.layout(registry)?;
let elem_ty = elem_concrete_ty.build(context, helper, registry, metadata)?;
let array_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.param_signatures()[1].ty,
)?;

let (elem_ty, elem_layout) =
registry.build_type_with_layout(context, helper, registry, metadata, &info.ty)?;

let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0);
let len_ty = crate::ffi::get_struct_field_type_at(&array_ty, 1);
Expand Down
11 changes: 8 additions & 3 deletions src/libfuncs/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
},
metadata::MetadataStorage,
types::TypeBuilder,
utils::ProgramRegistryExt,
};
use cairo_lang_sierra::{
extensions::{
Expand Down Expand Up @@ -228,9 +229,13 @@ where
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
let enum_ty = registry.get_type(&info.param_signatures()[0].ty)?;
let felt252_ty = registry
.get_type(&info.branch_signatures()[0].vars[0].ty)?
.build(context, helper, registry, metadata)?;
let felt252_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;

let tag_bits = enum_ty
.variants()
Expand Down
6 changes: 3 additions & 3 deletions src/libfuncs/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
},
metadata::{realloc_bindings::ReallocBindingsMeta, MetadataStorage},
types::TypeBuilder,
utils::ProgramRegistryExt,
};
use cairo_lang_sierra::{
extensions::{
Expand Down Expand Up @@ -125,9 +126,8 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
let inner_type = registry.get_type(&info.ty)?;
let inner_layout = inner_type.layout(registry)?;
let inner_ty = inner_type.build(context, helper, registry, metadata)?;
let (inner_ty, inner_layout) =
registry.build_type_with_layout(context, helper, registry, metadata, &info.ty)?;

let op = entry.append_operation(llvm::load(
context,
Expand Down
9 changes: 3 additions & 6 deletions src/libfuncs/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
},
metadata::MetadataStorage,
types::TypeBuilder,
utils::ProgramRegistryExt,
};
use cairo_lang_sierra::{
extensions::{
Expand Down Expand Up @@ -67,12 +68,8 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
let src_ty = registry
.get_type(&info.from_ty)?
.build(context, helper, registry, metadata)?;
let dst_ty = registry
.get_type(&info.to_ty)?
.build(context, helper, registry, metadata)?;
let src_ty = registry.build_type(context, helper, registry, metadata, &info.from_ty)?;
let dst_ty = registry.build_type(context, helper, registry, metadata, &info.to_ty)?;
assert!(info.from_nbits >= info.to_nbits);

if info.from_nbits == info.to_nbits {
Expand Down
32 changes: 22 additions & 10 deletions src/libfuncs/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
felt252::{register_prime_modulo_meta, Felt252},
TypeBuilder,
},
utils::get_integer_layout,
utils::{get_integer_layout, ProgramRegistryExt},
};
use cairo_lang_sierra::{
extensions::{
Expand Down Expand Up @@ -904,9 +904,13 @@ where
context,
entry.argument(0)?.into(),
DenseI64ArrayAttribute::new(context, &[0]),
registry
.get_type(&info.branch_signatures()[0].vars[0].ty)?
.build(context, helper, registry, metadata)?,
registry.build_type(
context,
helper,
registry,
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?,
location,
))
.result(0)?
Expand All @@ -916,9 +920,13 @@ where
context,
entry.argument(0)?.into(),
DenseI64ArrayAttribute::new(context, &[1]),
registry
.get_type(&info.branch_signatures()[0].vars[1].ty)?
.build(context, helper, registry, metadata)?,
registry.build_type(
context,
helper,
registry,
metadata,
&info.branch_signatures()[0].vars[1].ty,
)?,
location,
))
.result(0)?
Expand All @@ -944,9 +952,13 @@ where
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
let ec_point_ty = registry
.get_type(&info.branch_signatures()[0].vars[0].ty)?
.build(context, helper, registry, metadata)?;
let ec_point_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;

let point = entry
.append_operation(llvm::undef(ec_point_ty, location))
Expand Down
22 changes: 15 additions & 7 deletions src/libfuncs/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
metadata::MetadataStorage,
types::TypeBuilder,
utils::padding_needed_for,
utils::{padding_needed_for, ProgramRegistryExt},
};
use cairo_lang_sierra::{
extensions::{
Expand Down Expand Up @@ -89,9 +89,13 @@ where
.unwrap(),
)?;

let enum_ty = registry
.get_type(&info.branch_signatures()[0].vars[0].ty)?
.build(context, helper, registry, metadata)?;
let enum_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.branch_signatures()[0].vars[0].ty,
)?;

let op0 = entry.append_operation(arith::constant(
context,
Expand Down Expand Up @@ -208,9 +212,13 @@ where
.unwrap(),
)?;

let enum_ty = registry
.get_type(&info.param_signatures()[0].ty)?
.build(context, helper, registry, metadata)?;
let enum_ty = registry.build_type(
context,
helper,
registry,
metadata,
&info.param_signatures()[0].ty,
)?;

let op0 = helper.init_block().append_operation(arith::constant(
context,
Expand Down
Loading

0 comments on commit 4012a10

Please sign in to comment.