Skip to content

Commit

Permalink
Refactored nullable types to use the BlockExt trait (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Jun 6, 2024
1 parent 9424e67 commit c540e07
Showing 1 changed file with 17 additions and 50 deletions.
67 changes: 17 additions & 50 deletions src/types/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! `Box<T>` we can reuse its pointer, just leaving it null when there's no value.
use super::{TypeBuilder, WithSelf};
use crate::block_ext::BlockExt;
use crate::{
error::Result,
libfuncs::LibfuncHelper,
Expand All @@ -22,13 +23,12 @@ use cairo_lang_sierra::{
};
use melior::{
dialect::{
arith::{self, CmpiPredicate},
llvm::{self, r#type::pointer},
ods, scf,
},
ir::{
attribute::IntegerAttribute, operation::OperationBuilder, r#type::IntegerType, Block,
Location, Module, Region, Type, Value,
attribute::IntegerAttribute, r#type::IntegerType, Block, Location, Module, Region, Type,
Value,
},
Context,
};
Expand Down Expand Up @@ -75,39 +75,20 @@ fn snapshot_take<'ctx, 'this>(

let elem_layout = registry.get_type(&info.ty)?.layout(registry)?;

let k0 = entry
.append_operation(arith::constant(
context,
IntegerAttribute::new(IntegerType::new(context, 64).into(), 0).into(),
location,
))
.result(0)?
.into();
let null_ptr = entry
.append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into())
.result(0)?
.into();
.append_op_result(ods::llvm::mlir_zero(context, pointer(context, 0), location).into())?;

let ptr_value = entry
.append_operation(
OperationBuilder::new("llvm.ptrtoint", location)
.add_operands(&[src_value])
.add_results(&[IntegerType::new(context, 64).into()])
.build()?,
)
.result(0)?
.into();

let is_null = entry
.append_operation(arith::cmpi(
let is_null = entry.append_op_result(
ods::llvm::icmp(
context,
CmpiPredicate::Eq,
ptr_value,
k0,
IntegerType::new(context, 1).into(),
src_value,
null_ptr,
IntegerAttribute::new(IntegerType::new(context, 64).into(), 0).into(),
location,
))
.result(0)?
.into();
)
.into(),
)?;

let value = entry
.append_operation(scf::r#if(
Expand All @@ -124,25 +105,11 @@ fn snapshot_take<'ctx, 'this>(
let region = Region::new();
let block = region.append_block(Block::new(&[]));

let alloc_len = block
.append_operation(arith::constant(
context,
IntegerAttribute::new(
IntegerType::new(context, 64).into(),
elem_layout.size() as i64,
)
.into(),
location,
))
.result(0)?
.into();
let alloc_len = block.const_int(context, location, elem_layout.size(), 64)?;

let cloned_ptr = block
.append_operation(ReallocBindingsMeta::realloc(
context, null_ptr, alloc_len, location,
))
.result(0)?
.into();
let cloned_ptr = block.append_op_result(ReallocBindingsMeta::realloc(
context, null_ptr, alloc_len, location,
))?;

block.append_operation(
ods::llvm::intr_memcpy(
Expand Down

0 comments on commit c540e07

Please sign in to comment.