WIP - [artiq] Test force zeroing

This commit is contained in:
David Mak 2024-08-15 10:38:09 +08:00
parent b91eaea4e9
commit 60605578ca
2 changed files with 47 additions and 93 deletions

View File

@ -41,6 +41,7 @@ use std::{
mem, mem,
sync::Arc, sync::Arc,
}; };
use nac3core::codegen::llvm_intrinsics::call_memset;
/// The parallelism mode within a block. /// The parallelism mode within a block.
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Copy, Clone, Eq, PartialEq)]
@ -648,6 +649,17 @@ fn format_rpc_ret<'ctx>(
llvm_pdata_sizeof, llvm_pdata_sizeof,
llvm_i1.const_zero(), llvm_i1.const_zero(),
); );
call_memset(
ctx,
ctx.builder.build_bitcast(
ndarray.data().base_ptr(ctx, generator),
llvm_pi8,
"",
).unwrap().into_pointer_value(),
llvm_i8.const_zero(),
llvm_usize.const_int(4, false),
llvm_i1.const_zero(),
);
let pbuffer_dims_begin = unsafe { let pbuffer_dims_begin = unsafe {
buffer.ptr_offset_unchecked(ctx, generator, &llvm_pdata_sizeof, None) buffer.ptr_offset_unchecked(ctx, generator, &llvm_pdata_sizeof, None)
@ -660,98 +672,6 @@ fn format_rpc_ret<'ctx>(
llvm_i1.const_zero(), llvm_i1.const_zero(),
); );
// let pbuffer_dims_begin = unsafe {
// buffer.ptr_offset_unchecked(ctx, generator, &llvm_usize.const_int(4, false), None)
// };
// call_memcpy_generic(
// ctx,
// ndarray.dim_sizes().base_ptr(ctx, generator),
// pbuffer_dims_begin,
// dims_buf_sz,
// llvm_i1.const_zero(),
// );
// dims=(10): bufsize = 32
// let sz = call_ndarray_calc_size(generator, ctx, &ndarray.dim_sizes(), (None, None));
//
// let ndims_addr = ndarray.ptr_to_ndims(ctx);
// let ndarray_pdata = ndarray.data().base_ptr(ctx, generator);
// call_memcpy_generic(
// ctx,
// ndarray_pdata,
// ndims_addr,
// llvm_usize_sizeof,
// llvm_i1.const_zero(),
// );
//
// let dim_sz0_addr = unsafe { ndarray.dim_sizes().ptr_offset_unchecked(ctx, generator, &llvm_usize.const_zero(), None) };
// let ndarray_pdata = unsafe {
// ndarray.data().ptr_offset_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
// };
// call_memcpy_generic(
// ctx,
// ndarray_pdata,
// dim_sz0_addr,
// llvm_usize_sizeof,
// llvm_i1.const_zero(),
// );
//
// let sz_addr = ctx.builder.build_alloca(llvm_usize, "").unwrap();
// ctx.builder.build_store(sz_addr, sz).unwrap();
// let ndarray_pdata = unsafe {
// ndarray.data().ptr_offset_unchecked(ctx, generator, &llvm_usize.const_int(2, false), None)
// };
// call_memcpy_generic(
// ctx,
// ndarray_pdata,
// sz_addr,
// llvm_usize_sizeof,
// llvm_i1.const_zero(),
// );
//
// let alloc_size_addr = ctx.builder.build_alloca(llvm_usize, "").unwrap();
// ctx.builder.build_store(alloc_size_addr, alloc_size).unwrap();
// let ndarray_pdata = unsafe {
// ndarray.data().ptr_offset_unchecked(ctx, generator, &llvm_usize.const_int(3, false), None)
// };
// call_memcpy_generic(
// ctx,
// ndarray_pdata,
// alloc_size_addr,
// llvm_usize_sizeof,
// llvm_i1.const_zero(),
// );
// // // This works (with uninitialized data)
// unsafe {
// ndarray.dim_sizes().set_typed_unchecked(
// ctx,
// generator,
// &llvm_usize.const_zero(),
// llvm_usize.const_int(3, false),
// );
// }
// ndarray.create_data(
// ctx,
// llvm_elem_ty,
// llvm_usize.const_int(3, false),
// );
//
// let ndarray_pdata = unsafe {
// ndarray.data().base_ptr(ctx, generator)
// };
// call_memcpy_generic(
// ctx,
// ndarray_pdata,
// buffer.base_ptr(ctx, generator),
// buffer_size,
// llvm_i1.const_zero()
// );
Ok(()) Ok(())
}, },
|_, _| Ok(()), |_, _| Ok(()),

View File

@ -199,6 +199,40 @@ pub fn call_memcpy_generic<'ctx>(
call_memcpy(ctx, dest, src, len, is_volatile); call_memcpy(ctx, dest, src, len, is_volatile);
} }
/// Invokes the [`llvm.memset`](https://llvm.org/docs/LangRef.html#llvm-memset-intrinsic) intrinsic.
pub fn call_memset<'ctx>(
ctx: &CodeGenContext<'ctx, '_>,
dest: PointerValue<'ctx>,
val: IntValue<'ctx>,
len: IntValue<'ctx>,
is_volatile: IntValue<'ctx>,
) {
const FN_NAME: &str = "llvm.memset";
debug_assert!(dest.get_type().get_element_type().is_int_type());
debug_assert_eq!(dest.get_type().get_element_type().into_int_type().get_bit_width(), 8);
debug_assert_eq!(val.get_type().get_bit_width(), 8);
debug_assert!(matches!(len.get_type().get_bit_width(), 32 | 64));
debug_assert_eq!(is_volatile.get_type().get_bit_width(), 1);
let llvm_dest_t = dest.get_type();
let llvm_src_t = val.get_type();
let llvm_len_t = len.get_type();
let intrinsic_fn = Intrinsic::find(FN_NAME)
.and_then(|intrinsic| {
intrinsic.get_declaration(
&ctx.module,
&[llvm_dest_t.into(), llvm_len_t.into()],
)
})
.unwrap();
ctx.builder
.build_call(intrinsic_fn, &[dest.into(), val.into(), len.into(), is_volatile.into()], "")
.unwrap();
}
/// Macro to find and generate build call for llvm intrinsic (body of llvm intrinsic function) /// Macro to find and generate build call for llvm intrinsic (body of llvm intrinsic function)
/// ///
/// Arguments: /// Arguments: