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 struct libfuncs to use the BlockExt trait #650

Merged
merged 3 commits into from
Jun 4, 2024
Merged
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
27 changes: 5 additions & 22 deletions src/libfuncs/struct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! # Struct-related libfuncs

use super::LibfuncHelper;
use crate::block_ext::BlockExt;
use crate::{
error::Result, metadata::MetadataStorage, types::TypeBuilder, utils::ProgramRegistryExt,
};
Expand All @@ -16,7 +17,7 @@ use cairo_lang_sierra::{
};
use melior::{
dialect::llvm,
ir::{attribute::DenseI64ArrayAttribute, Block, Location, Value},
ir::{Block, Location, Value},
Context,
};

Expand Down Expand Up @@ -87,18 +88,9 @@ pub fn build_struct_value<'ctx, 'this>(
) -> Result<Value<'ctx, 'this>> {
let struct_ty = registry.build_type(context, helper, registry, metadata, struct_type)?;

let mut acc = entry.append_operation(llvm::undef(struct_ty, location));
for (i, field) in fields.iter().enumerate() {
acc = entry.append_operation(llvm::insert_value(
context,
acc.result(0)?.into(),
DenseI64ArrayAttribute::new(context, &[i as _]),
*field,
location,
));
}
let acc = entry.append_operation(llvm::undef(struct_ty, location));

Ok(acc.result(0)?.into())
entry.insert_values(context, location, acc.result(0)?.into(), fields)
}

/// Generate MLIR operations for the `struct_deconstruct` libfunc.
Expand All @@ -118,16 +110,7 @@ pub fn build_deconstruct<'ctx, 'this>(
let type_info = registry.get_type(&var_info.ty)?;
let field_ty = type_info.build(context, helper, registry, metadata, &var_info.ty)?;

let value = entry
.append_operation(llvm::extract_value(
context,
container,
DenseI64ArrayAttribute::new(context, &[i.try_into()?]),
field_ty,
location,
))
.result(0)?
.into();
let value = entry.extract_value(context, location, container, field_ty, i)?;

fields.push(value);
}
Expand Down
Loading