Skip to content

Commit 951ac4c

Browse files
fix implementation with mlir
1 parent 09ec4d1 commit 951ac4c

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

src/compiler.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -953,14 +953,6 @@ fn compile_func(
953953
},
954954
)?;
955955

956-
#[cfg(feature = "with-libfunc-counter")]
957-
{
958-
use crate::metadata::libfunc_counter::LibfuncCounterMeta;
959-
960-
let libfunc_counter = metadata.get_mut::<LibfuncCounterMeta>().unwrap();
961-
libfunc_counter.store_array_counter(context, module, &entry_block, Location::unknown(context), libfunc_indexes.len() as u32)?;
962-
}
963-
964956
// Load arguments and jump to the entry block.
965957
{
966958
let mut arg_values = Vec::with_capacity(function.signature.param_types.len());
@@ -1070,6 +1062,10 @@ fn compile_func(
10701062
sierra_stmt_start_offset + function.entry_point.0,
10711063
0,
10721064
),
1065+
#[cfg(feature = "with-libfunc-counter")]
1066+
libfunc_indexes,
1067+
#[cfg(feature = "with-libfunc-counter")]
1068+
metadata,
10731069
)?;
10741070

10751071
tracing::debug!("Done generating function {}.", function.id);
@@ -1451,6 +1447,8 @@ fn generate_entry_point_wrapper<'c>(
14511447
arg_types: &[(Type<'c>, Location<'c>)],
14521448
ret_types: &[Type<'c>],
14531449
location: Location<'c>,
1450+
#[cfg(feature = "with-libfunc-counter")] libfunc_indexes: &HashMap<ConcreteLibfuncId, usize>,
1451+
#[cfg(feature = "with-libfunc-counter")] metadata: &mut MetadataStorage,
14541452
) -> Result<(), Error> {
14551453
let region = Region::new();
14561454
let block = region.append_block(Block::new(arg_types));
@@ -1483,6 +1481,20 @@ fn generate_entry_point_wrapper<'c>(
14831481
returns.push(block.extract_value(context, location, result, *ty, i)?);
14841482
}
14851483

1484+
#[cfg(feature = "with-libfunc-counter")]
1485+
{
1486+
use crate::metadata::libfunc_counter::LibfuncCounterMeta;
1487+
1488+
let libfunc_counter = metadata.get_mut::<LibfuncCounterMeta>().unwrap();
1489+
libfunc_counter.store_array_counter(
1490+
context,
1491+
module,
1492+
&block,
1493+
location,
1494+
libfunc_indexes.len() as u32,
1495+
)?;
1496+
}
1497+
14861498
block.append_operation(func::r#return(&returns, location));
14871499

14881500
module.body().append_operation(func::func(

src/metadata/libfunc_counter.rs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use melior::{
1515

1616
use crate::{
1717
error::{Error, Result},
18-
utils::{BlockExt, GepIndex},
18+
utils::BlockExt,
1919
};
2020

2121
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
@@ -142,7 +142,7 @@ impl LibfuncCounterMeta {
142142
module: &Module,
143143
block: &'a Block<'c>,
144144
location: Location,
145-
libfunc_amount: u32
145+
libfunc_amount: u32,
146146
) -> Result<()> {
147147
let array_ty = llvm::r#type::array(IntegerType::new(context, 32).into(), libfunc_amount);
148148

@@ -166,12 +166,10 @@ impl LibfuncCounterMeta {
166166
.into(),
167167
)?;
168168

169-
let array_counter_ptr = block.load(context, location, global_address, array_ty)?;
170-
171169
block.append_operation(
172170
OperationBuilder::new("llvm.call", location)
173171
.add_operands(&[function_ptr])
174-
.add_operands(&[counter_id, array_counter_ptr, lifuncs_amount])
172+
.add_operands(&[counter_id, global_address, lifuncs_amount])
175173
.build()?,
176174
);
177175

@@ -212,15 +210,6 @@ impl LibfuncCounterMeta {
212210
.into(),
213211
)?;
214212

215-
let array_counter_ptr = block.load(context, location, global_address, array_ty)?;
216-
217-
block.insert_values(
218-
context,
219-
location,
220-
array_counter_ptr,
221-
&vec![k0; libfunc_amount as usize],
222-
)?;
223-
224213
Ok(())
225214
}
226215

@@ -241,7 +230,7 @@ impl LibfuncCounterMeta {
241230
let array_ty = llvm::r#type::array(u32_ty, libfuncs_amount);
242231
let k1 = block.const_int(context, location, 1, 32)?;
243232

244-
let global_address = block.append_op_result(
233+
let array_counter_ptr = block.append_op_result(
245234
ods::llvm::mlir_addressof(
246235
context,
247236
llvm::r#type::pointer(context, 0),
@@ -251,25 +240,21 @@ impl LibfuncCounterMeta {
251240
.into(),
252241
)?;
253242

254-
let array_counter_ptr = block.load(context, location, global_address, array_ty)?;
243+
let array_counter = block.load(context, location, array_counter_ptr, array_ty)?;
255244

256-
let value_counter = block.gep(
257-
context,
258-
location,
259-
array_counter_ptr,
260-
&[GepIndex::Const(libfunc_idx as i32)],
261-
u32_ty,
262-
)?;
245+
let value_counter = block.extract_value(context, location, array_counter, u32_ty, libfunc_idx)?;
263246
let value_incremented = block.addi(value_counter, k1, location)?;
264-
265-
block.insert_value(
247+
248+
let array_counter = block.insert_value(
266249
context,
267250
location,
268-
array_counter_ptr,
251+
array_counter,
269252
value_incremented,
270253
libfunc_idx,
271254
)?;
272255

256+
block.store(context, location, array_counter_ptr, array_counter)?;
257+
273258
Ok(())
274259
}
275260
}
@@ -328,11 +313,12 @@ pub mod libfunc_counter_runtime {
328313

329314
pub unsafe extern "C" fn store_array_counter(
330315
counter_id: u64,
331-
array_counter: &[u32],
316+
array_counter: *const u32,
332317
libfuncs_amount: u32,
333318
) {
334319
let mut libfunc_counter = LIBFUNC_COUNTER.lock().unwrap();
335-
336-
libfunc_counter.insert(counter_id, array_counter.to_vec());
320+
let vec = (0..libfuncs_amount).map(|i| dbg!(*array_counter.add(i as usize))).collect_vec();
321+
322+
libfunc_counter.insert(counter_id, vec);
337323
}
338324
}

0 commit comments

Comments
 (0)