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(box libfuncs): use BlockExt trait #545

Closed
wants to merge 9 commits into from
96 changes: 27 additions & 69 deletions src/libfuncs/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use super::LibfuncHelper;
use crate::{
block_ext::BlockExt,
error::Result,
metadata::{realloc_bindings::ReallocBindingsMeta, MetadataStorage},
types::TypeBuilder,
Expand All @@ -21,15 +22,10 @@ use cairo_lang_sierra::{
};
use melior::{
dialect::{
arith,
llvm::{self, r#type::opaque_pointer, AllocaOptions, LoadStoreOptions},
llvm::{self, r#type::opaque_pointer},
ods,
},
ir::{
attribute::{IntegerAttribute, TypeAttribute},
r#type::IntegerType,
Block, Location,
},
ir::{attribute::IntegerAttribute, r#type::IntegerType, Block, Location},
Context,
};

Expand Down Expand Up @@ -73,18 +69,7 @@ pub fn build_into_box<'ctx, 'this>(
let inner_type = registry.get_type(&info.ty)?;
let inner_layout = inner_type.layout(registry)?;

let value_len = entry
.append_operation(arith::constant(
context,
IntegerAttribute::new(
IntegerType::new(context, 64).into(),
inner_layout.pad_to_align().size().try_into()?,
)
.into(),
location,
))
.result(0)?
.into();
let value_len = entry.const_int(context, location, inner_layout.size(), 64)?;
FeliGR marked this conversation as resolved.
Show resolved Hide resolved

let ptr = entry
.append_operation(llvm::nullptr(opaque_pointer(context), location))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here this should be refactored using append_op_result no?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and the next operation too (the ReallocBindingsMeta::realloc).

Expand Down Expand Up @@ -117,16 +102,13 @@ pub fn build_into_box<'ctx, 'this>(
);
}
_ => {
entry.append_operation(llvm::store(
entry.store(
context,
entry.argument(0)?.into(),
ptr,
location,
LoadStoreOptions::new().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
inner_layout.align() as i64,
))),
));
ptr,
entry.argument(0)?.into(),
Some(inner_layout.align()),
);
}
}

Expand Down Expand Up @@ -155,35 +137,17 @@ pub fn build_unbox<'ctx, 'this>(
.iter()
.all(|type_id| registry.get_type(type_id).unwrap().is_zst(registry)) =>
{
let value_len = helper
.init_block()
.append_operation(arith::constant(
context,
IntegerAttribute::new(
IntegerType::new(context, 64).into(),
inner_layout.size() as i64,
)
.into(),
location,
))
.result(0)?
.into();
let stack_ptr = helper
.init_block()
.append_operation(llvm::alloca(
context,
value_len,
llvm::r#type::opaque_pointer(context),
location,
AllocaOptions::new()
.align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
inner_layout.align() as i64,
)))
.elem_type(Some(TypeAttribute::new(inner_ty))),
))
.result(0)?
.into();
let value_len =
helper
.init_block()
.const_int(context, location, inner_layout.size() as i64, 64)?;
let stack_ptr = helper.init_block().alloca(
context,
location,
inner_ty,
value_len,
Some(inner_layout.align()),
)?;

entry.append_operation(
ods::llvm::intr_memcpy(
Expand All @@ -199,19 +163,13 @@ pub fn build_unbox<'ctx, 'this>(

stack_ptr
}
_ => entry
.append_operation(llvm::load(
context,
entry.argument(0)?.into(),
inner_ty,
location,
LoadStoreOptions::new().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
inner_layout.align() as i64,
))),
))
.result(0)?
.into(),
_ => entry.load(
context,
location,
entry.argument(0)?.into(),
inner_ty,
Some(inner_layout.align()),
)?,
};

entry.append_operation(ReallocBindingsMeta::free(
Expand Down
Loading