Skip to content

Commit

Permalink
Refactor poseidon libfuncs to use the BlockExt trait (#646)
Browse files Browse the repository at this point in the history
Co-authored-by: Iñaki Garay <[email protected]>
Co-authored-by: Edgar <[email protected]>
  • Loading branch information
3 people authored Jun 18, 2024
1 parent eb40ecd commit ddf395c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 118 deletions.
2 changes: 1 addition & 1 deletion src/block_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl<'ctx> BlockExt<'ctx> for Block<'ctx> {
if let Some(align) = align {
op.set_alignment(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
align as i64,
align.try_into()?,
));
}

Expand Down
171 changes: 54 additions & 117 deletions src/libfuncs/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ use cairo_lang_sierra::{
program_registry::ProgramRegistry,
};
use melior::{
dialect::{
arith,
llvm::{self, LoadStoreOptions},
ods,
},
ir::{attribute::IntegerAttribute, r#type::IntegerType, Block, Location},
dialect::{arith, llvm, ods},
ir::{r#type::IntegerType, Block, Location},
Context,
};

Expand Down Expand Up @@ -90,62 +86,39 @@ pub fn build_hades_permutation<'ctx>(
.init_block()
.alloca1(context, location, i256_ty, Some(layout_i256.align()))?;

let op0_i256 = entry
.append_operation(ods::arith::extui(context, i256_ty, op0, location).into())
.result(0)?
.into();
let op1_i256 = entry
.append_operation(ods::arith::extui(context, i256_ty, op1, location).into())
.result(0)?
.into();
let op2_i256 = entry
.append_operation(ods::arith::extui(context, i256_ty, op2, location).into())
.result(0)?
.into();
let op0_i256 =
entry.append_op_result(ods::arith::extui(context, i256_ty, op0, location).into())?;

let op1_i256 =
entry.append_op_result(ods::arith::extui(context, i256_ty, op1, location).into())?;
let op2_i256 =
entry.append_op_result(ods::arith::extui(context, i256_ty, op2, location).into())?;

let op0_be = entry
.append_operation(llvm::intr_bswap(op0_i256, i256_ty, location))
.result(0)?
.into();
let op1_be = entry
.append_operation(llvm::intr_bswap(op1_i256, i256_ty, location))
.result(0)?
.into();
let op2_be = entry
.append_operation(llvm::intr_bswap(op2_i256, i256_ty, location))
.result(0)?
.into();
let op0_be = entry.append_op_result(llvm::intr_bswap(op0_i256, i256_ty, location))?;
let op1_be = entry.append_op_result(llvm::intr_bswap(op1_i256, i256_ty, location))?;
let op2_be = entry.append_op_result(llvm::intr_bswap(op2_i256, i256_ty, location))?;

entry.append_operation(llvm::store(
entry.store(
context,
op0_be,
op0_ptr,
location,
LoadStoreOptions::default().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
layout_i256.align().try_into()?,
))),
));
entry.append_operation(llvm::store(
op0_ptr,
op0_be,
Some(layout_i256.align()),
)?;
entry.store(
context,
op1_be,
op1_ptr,
location,
LoadStoreOptions::default().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
layout_i256.align().try_into()?,
))),
));
entry.append_operation(llvm::store(
op1_ptr,
op1_be,
Some(layout_i256.align()),
)?;
entry.store(
context,
op2_be,
op2_ptr,
location,
LoadStoreOptions::default().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
layout_i256.align().try_into()?,
))),
));
op2_ptr,
op2_be,
Some(layout_i256.align()),
)?;

let runtime_bindings = metadata
.get_mut::<RuntimeBindingsMeta>()
Expand All @@ -154,71 +127,35 @@ pub fn build_hades_permutation<'ctx>(
runtime_bindings
.libfunc_hades_permutation(context, helper, entry, op0_ptr, op1_ptr, op2_ptr, location)?;

let op0_be = entry
.append_operation(llvm::load(
context,
op0_ptr,
i256_ty,
location,
LoadStoreOptions::default().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
layout_i256.align().try_into()?,
))),
))
.result(0)?
.into();
let op1_be = entry
.append_operation(llvm::load(
context,
op1_ptr,
i256_ty,
location,
LoadStoreOptions::default().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
layout_i256.align().try_into()?,
))),
))
.result(0)?
.into();
let op2_be = entry
.append_operation(llvm::load(
context,
op2_ptr,
i256_ty,
location,
LoadStoreOptions::default().align(Some(IntegerAttribute::new(
IntegerType::new(context, 64).into(),
layout_i256.align().try_into()?,
))),
))
.result(0)?
.into();
let op0_be = entry.load(
context,
location,
op0_ptr,
i256_ty,
Some(layout_i256.align()),
)?;
let op1_be = entry.load(
context,
location,
op1_ptr,
i256_ty,
Some(layout_i256.align()),
)?;
let op2_be = entry.load(
context,
location,
op2_ptr,
i256_ty,
Some(layout_i256.align()),
)?;

let op0_i256 = entry
.append_operation(llvm::intr_bswap(op0_be, i256_ty, location))
.result(0)?
.into();
let op1_i256 = entry
.append_operation(llvm::intr_bswap(op1_be, i256_ty, location))
.result(0)?
.into();
let op2_i256 = entry
.append_operation(llvm::intr_bswap(op2_be, i256_ty, location))
.result(0)?
.into();
let op0_i256 = entry.append_op_result(llvm::intr_bswap(op0_be, i256_ty, location))?;
let op1_i256 = entry.append_op_result(llvm::intr_bswap(op1_be, i256_ty, location))?;
let op2_i256 = entry.append_op_result(llvm::intr_bswap(op2_be, i256_ty, location))?;

let op0 = entry
.append_operation(arith::trunci(op0_i256, felt252_ty, location))
.result(0)?
.into();
let op1 = entry
.append_operation(arith::trunci(op1_i256, felt252_ty, location))
.result(0)?
.into();
let op2 = entry
.append_operation(arith::trunci(op2_i256, felt252_ty, location))
.result(0)?
.into();
let op0 = entry.append_op_result(arith::trunci(op0_i256, felt252_ty, location))?;
let op1 = entry.append_op_result(arith::trunci(op1_i256, felt252_ty, location))?;
let op2 = entry.append_op_result(arith::trunci(op2_i256, felt252_ty, location))?;

entry.append_operation(helper.br(0, &[poseidon_builtin, op0, op1, op2], location));

Expand Down

0 comments on commit ddf395c

Please sign in to comment.