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
54 changes: 16 additions & 38 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 @@ -20,11 +21,8 @@ use cairo_lang_sierra::{
program_registry::ProgramRegistry,
};
use melior::{
dialect::{
arith,
llvm::{self, r#type::opaque_pointer, LoadStoreOptions},
},
ir::{attribute::IntegerAttribute, r#type::IntegerType, Block, Location},
dialect::llvm::{self, r#type::opaque_pointer},
ir::{Block, Location},
Context,
};

Expand Down Expand Up @@ -68,18 +66,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.pad_to_align().size(), 64)?;

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 All @@ -92,16 +79,13 @@ pub fn build_into_box<'ctx, 'this>(
.result(0)?
.into();

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()),
);

entry.append_operation(helper.br(0, &[ptr], location));
Ok(())
Expand All @@ -122,19 +106,13 @@ pub fn build_unbox<'ctx, 'this>(
let inner_layout = inner_type.layout(registry)?;

// Load the boxed value from memory.
let value = 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();
let value = entry.load(
context,
location,
entry.argument(0)?.into(),
inner_ty,
Some(inner_layout.align()),
)?;

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