From 0cc7e41c6f553bf7a8234bc3ed85b421d2a5e6c5 Mon Sep 17 00:00:00 2001 From: lyken Date: Sun, 14 Jul 2024 23:40:19 +0800 Subject: [PATCH] core: fix new irrt ndarray issues --- nac3core/src/codegen/irrt/numpy/ndarray.rs | 11 ++++------- nac3core/src/codegen/model/pointer.rs | 12 ++++++++++++ nac3core/src/codegen/numpy_new.rs | 13 ++++++++++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/nac3core/src/codegen/irrt/numpy/ndarray.rs b/nac3core/src/codegen/irrt/numpy/ndarray.rs index 2e510dce..7085cba4 100644 --- a/nac3core/src/codegen/irrt/numpy/ndarray.rs +++ b/nac3core/src/codegen/irrt/numpy/ndarray.rs @@ -204,7 +204,7 @@ fn call_nac3_ndarray_set_strides_by_shape<'ctx, G: CodeGenerator + ?Sized>( FunctionBuilder::begin( ctx, - &get_sized_dependent_function_name(sizet, "__nac3_ndarray_util_assert_shape_no_negative"), + &get_sized_dependent_function_name(sizet, "__nac3_ndarray_set_strides_by_shape"), ) .arg("ndarray", &PointerModel(StructModel(NpArray { sizet })), ndarray_ptr) .returning_void(); @@ -217,12 +217,9 @@ fn call_nac3_ndarray_nbytes<'ctx, G: CodeGenerator + ?Sized>( ) -> Int<'ctx> { let sizet = IntModel(generator.get_size_type(ctx.ctx)); - FunctionBuilder::begin( - ctx, - &get_sized_dependent_function_name(sizet, "__nac3_ndarray_util_assert_shape_no_negative"), - ) - .arg("ndarray", &PointerModel(StructModel(NpArray { sizet })), ndarray_ptr) - .returning("nbytes", &sizet) + FunctionBuilder::begin(ctx, &get_sized_dependent_function_name(sizet, "__nac3_ndarray_nbytes")) + .arg("ndarray", &PointerModel(StructModel(NpArray { sizet })), ndarray_ptr) + .returning("nbytes", &sizet) } pub fn call_nac3_ndarray_fill_generic<'ctx, G: CodeGenerator + ?Sized>( diff --git a/nac3core/src/codegen/model/pointer.rs b/nac3core/src/codegen/model/pointer.rs index bbe0bb93..974f952d 100644 --- a/nac3core/src/codegen/model/pointer.rs +++ b/nac3core/src/codegen/model/pointer.rs @@ -77,4 +77,16 @@ impl<'ctx> OpaquePointer<'ctx> { pub fn store(&self, ctx: &CodeGenContext<'ctx, '_>, value: BasicValueEnum<'ctx>) { ctx.builder.build_store(self.0, value).unwrap(); } + + pub fn from_ptr(ctx: &CodeGenContext<'ctx, '_>, ptr: PointerValue<'ctx>) -> Self { + let ptr = ctx + .builder + .build_pointer_cast( + ptr, + ctx.ctx.i8_type().ptr_type(AddressSpace::default()), + "opaque.from_ptr", + ) + .unwrap(); + OpaquePointer(ptr) + } } diff --git a/nac3core/src/codegen/numpy_new.rs b/nac3core/src/codegen/numpy_new.rs index f726d5f8..21520b65 100644 --- a/nac3core/src/codegen/numpy_new.rs +++ b/nac3core/src/codegen/numpy_new.rs @@ -55,9 +55,16 @@ where let ndarray_ptr = call_ndarray_empty_impl(generator, ctx, elem_ty, shape, shape_ty, name)?; // NOTE: fill_value's type is not checked!! so be careful with logics - let fill_value_ptr = - OpaquePointer(ctx.builder.build_alloca(fill_value.get_type(), "fill_value_ptr").unwrap()); - fill_value_ptr.store(ctx, fill_value); + + // Allocate fill_value on the stack and give the corresponding stack pointer + // to call_nac3_ndarray_fill_generic + let fill_value_ptr = ctx.builder.build_alloca(fill_value.get_type(), "fill_value_ptr").unwrap(); + ctx.builder.build_store(fill_value_ptr, fill_value).unwrap(); + + // Opaque-ize fill_value_ptr (turning it into `i8*`) before passing + // to call_nac3_ndarray_fill_generic + let fill_value_ptr = OpaquePointer::from_ptr(ctx, fill_value_ptr); + call_nac3_ndarray_fill_generic(generator, ctx, &ndarray_ptr, &fill_value_ptr); Ok(ndarray_ptr)