Compare commits

...

2 Commits

Author SHA1 Message Date
David Mak bd43e5eb35 [core] coregen/types: Implement StructFields for NDArray
Also rename some fields to better align with their naming in numpy.
2024-11-20 15:57:49 +08:00
David Mak 6094e721db [core] codegen/types: Implement NDArray in terms of i8*
Better aligns with the future implementation of ndstrides.
2024-11-20 15:57:46 +08:00
8 changed files with 520 additions and 293 deletions

View File

@ -461,8 +461,7 @@ fn format_rpc_arg<'ctx>(
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, arg_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, arg_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty); let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let llvm_arg_ty = NDArrayType::new(generator, ctx.ctx, llvm_elem_ty); let llvm_arg_ty = NDArrayType::new(generator, ctx.ctx, llvm_elem_ty);
let llvm_arg = let llvm_arg = llvm_arg_ty.map_value(arg.into_pointer_value(), None);
NDArrayValue::from_pointer_value(arg.into_pointer_value(), llvm_usize, None);
let llvm_usize_sizeof = ctx let llvm_usize_sizeof = ctx
.builder .builder
@ -499,7 +498,7 @@ fn format_rpc_arg<'ctx>(
call_memcpy_generic( call_memcpy_generic(
ctx, ctx,
pbuffer_dims_begin, pbuffer_dims_begin,
llvm_arg.dim_sizes().base_ptr(ctx, generator), llvm_arg.shape().base_ptr(ctx, generator),
dims_buf_sz, dims_buf_sz,
llvm_i1.const_zero(), llvm_i1.const_zero(),
); );
@ -613,7 +612,7 @@ fn format_rpc_ret<'ctx>(
// Set `ndarray.ndims` // Set `ndarray.ndims`
ndarray.store_ndims(ctx, generator, llvm_usize.const_int(ndims, false)); ndarray.store_ndims(ctx, generator, llvm_usize.const_int(ndims, false));
// Allocate `ndarray.shape` [size_t; ndims] // Allocate `ndarray.shape` [size_t; ndims]
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray.load_ndims(ctx)); ndarray.create_shape(ctx, llvm_usize, ndarray.load_ndims(ctx));
/* /*
ndarray now: ndarray now:
@ -703,7 +702,7 @@ fn format_rpc_ret<'ctx>(
call_memcpy_generic( call_memcpy_generic(
ctx, ctx,
ndarray.dim_sizes().base_ptr(ctx, generator), ndarray.shape().base_ptr(ctx, generator),
pbuffer_dims, pbuffer_dims,
sizeof_dims, sizeof_dims,
llvm_i1.const_zero(), llvm_i1.const_zero(),
@ -715,7 +714,7 @@ fn format_rpc_ret<'ctx>(
// `ndarray.shape` must be initialized beforehand in this implementation // `ndarray.shape` must be initialized beforehand in this implementation
// (for ndarray.create_data() to know how many elements to allocate) // (for ndarray.create_data() to know how many elements to allocate)
let num_elements = let num_elements =
call_ndarray_calc_size(generator, ctx, &ndarray.dim_sizes(), (None, None)); call_ndarray_calc_size(generator, ctx, &ndarray.shape(), (None, None));
// debug_assert(nelems * sizeof(T) >= ndarray_nbytes) // debug_assert(nelems * sizeof(T) >= ndarray_nbytes)
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None { if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
@ -1369,13 +1368,18 @@ fn polymorphic_print<'ctx>(
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => { TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
fmt.push_str("array(["); fmt.push_str("array([");
flush(ctx, generator, &mut fmt, &mut args); flush(ctx, generator, &mut fmt, &mut args);
let val = let val = NDArrayValue::from_pointer_value(
NDArrayValue::from_pointer_value(value.into_pointer_value(), llvm_usize, None); value.into_pointer_value(),
let len = call_ndarray_calc_size(generator, ctx, &val.dim_sizes(), (None, None)); llvm_elem_ty,
llvm_usize,
None,
);
let len = call_ndarray_calc_size(generator, ctx, &val.shape(), (None, None));
let last = let last =
ctx.builder.build_int_sub(len, llvm_usize.const_int(1, false), "").unwrap(); ctx.builder.build_int_sub(len, llvm_usize.const_int(1, false), "").unwrap();

View File

@ -21,7 +21,10 @@ use super::{
CodeGenContext, CodeGenerator, CodeGenContext, CodeGenerator,
}; };
use crate::{ use crate::{
toplevel::{helper::PrimDef, numpy::unpack_ndarray_var_tys}, toplevel::{
helper::{arraylike_flatten_element_type, PrimDef},
numpy::unpack_ndarray_var_tys,
},
typecheck::typedef::{Type, TypeEnum}, typecheck::typedef::{Type, TypeEnum},
}; };
@ -65,12 +68,17 @@ pub fn call_len<'ctx, G: CodeGenerator + ?Sized>(
ctx.builder.build_int_truncate_or_bit_cast(len, llvm_i32, "len").unwrap() ctx.builder.build_int_truncate_or_bit_cast(len, llvm_i32, "len").unwrap()
} }
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => { TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
let elem_ty = arraylike_flatten_element_type(&mut ctx.unifier, arg_ty);
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
let arg = let arg = NDArrayValue::from_pointer_value(
NDArrayValue::from_pointer_value(arg.into_pointer_value(), llvm_usize, None); arg.into_pointer_value(),
ctx.get_llvm_type(generator, elem_ty),
llvm_usize,
None,
);
let ndims = arg.dim_sizes().size(ctx, generator); let ndims = arg.shape().size(ctx, generator);
ctx.make_assert( ctx.make_assert(
generator, generator,
ctx.builder ctx.builder
@ -83,12 +91,7 @@ pub fn call_len<'ctx, G: CodeGenerator + ?Sized>(
); );
let len = unsafe { let len = unsafe {
arg.dim_sizes().get_typed_unchecked( arg.shape().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
ctx,
generator,
&llvm_usize.const_zero(),
None,
)
}; };
ctx.builder.build_int_truncate_or_bit_cast(len, llvm_i32, "len").unwrap() ctx.builder.build_int_truncate_or_bit_cast(len, llvm_i32, "len").unwrap()
@ -143,13 +146,14 @@ pub fn call_int32<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.int32, ctx.primitives.int32,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_int32(generator, ctx, (elem_ty, val)), |generator, ctx, val| call_int32(generator, ctx, (elem_ty, val)),
)?; )?;
@ -205,13 +209,14 @@ pub fn call_int64<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.int64, ctx.primitives.int64,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_int64(generator, ctx, (elem_ty, val)), |generator, ctx, val| call_int64(generator, ctx, (elem_ty, val)),
)?; )?;
@ -283,13 +288,14 @@ pub fn call_uint32<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.uint32, ctx.primitives.uint32,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_uint32(generator, ctx, (elem_ty, val)), |generator, ctx, val| call_uint32(generator, ctx, (elem_ty, val)),
)?; )?;
@ -350,13 +356,14 @@ pub fn call_uint64<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.uint64, ctx.primitives.uint64,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_uint64(generator, ctx, (elem_ty, val)), |generator, ctx, val| call_uint64(generator, ctx, (elem_ty, val)),
)?; )?;
@ -416,13 +423,14 @@ pub fn call_float<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.float, ctx.primitives.float,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_float(generator, ctx, (elem_ty, val)), |generator, ctx, val| call_float(generator, ctx, (elem_ty, val)),
)?; )?;
@ -462,13 +470,14 @@ pub fn call_round<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ret_elem_ty, ret_elem_ty,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_round(generator, ctx, (elem_ty, val), ret_elem_ty), |generator, ctx, val| call_round(generator, ctx, (elem_ty, val), ret_elem_ty),
)?; )?;
@ -502,13 +511,14 @@ pub fn call_numpy_round<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.float, ctx.primitives.float,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_numpy_round(generator, ctx, (elem_ty, val)), |generator, ctx, val| call_numpy_round(generator, ctx, (elem_ty, val)),
)?; )?;
@ -567,13 +577,14 @@ pub fn call_bool<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.bool, ctx.primitives.bool,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| { |generator, ctx, val| {
let elem = call_bool(generator, ctx, (elem_ty, val))?; let elem = call_bool(generator, ctx, (elem_ty, val))?;
@ -621,13 +632,14 @@ pub fn call_floor<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ret_elem_ty, ret_elem_ty,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_floor(generator, ctx, (elem_ty, val), ret_elem_ty), |generator, ctx, val| call_floor(generator, ctx, (elem_ty, val), ret_elem_ty),
)?; )?;
@ -671,13 +683,14 @@ pub fn call_ceil<'ctx, G: CodeGenerator + ?Sized>(
if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
generator, generator,
ctx, ctx,
ret_elem_ty, ret_elem_ty,
None, None,
NDArrayValue::from_pointer_value(n, llvm_usize, None), NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None),
|generator, ctx, val| call_ceil(generator, ctx, (elem_ty, val), ret_elem_ty), |generator, ctx, val| call_ceil(generator, ctx, (elem_ty, val), ret_elem_ty),
)?; )?;
@ -806,8 +819,8 @@ pub fn call_numpy_minimum<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_minimum(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_minimum(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -906,10 +919,10 @@ pub fn call_numpy_max_min<'ctx, G: CodeGenerator + ?Sized>(
if a_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) => if a_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
{ {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, a_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, a_ty);
let llvm_ndarray_ty = ctx.get_llvm_type(generator, elem_ty); let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let n = NDArrayValue::from_pointer_value(n, llvm_usize, None); let n = NDArrayValue::from_pointer_value(n, llvm_elem_ty, llvm_usize, None);
let n_sz = irrt::call_ndarray_calc_size(generator, ctx, &n.dim_sizes(), (None, None)); let n_sz = irrt::call_ndarray_calc_size(generator, ctx, &n.shape(), (None, None));
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None { if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
let n_sz_eqz = ctx let n_sz_eqz = ctx
.builder .builder
@ -926,7 +939,7 @@ pub fn call_numpy_max_min<'ctx, G: CodeGenerator + ?Sized>(
); );
} }
let accumulator_addr = generator.gen_var_alloc(ctx, llvm_ndarray_ty, None)?; let accumulator_addr = generator.gen_var_alloc(ctx, llvm_elem_ty, None)?;
let res_idx = generator.gen_var_alloc(ctx, llvm_int64.into(), None)?; let res_idx = generator.gen_var_alloc(ctx, llvm_int64.into(), None)?;
unsafe { unsafe {
@ -1068,8 +1081,8 @@ pub fn call_numpy_maximum<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_maximum(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_maximum(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1114,6 +1127,7 @@ where
{ {
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
let (arg_elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, arg_ty); let (arg_elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, arg_ty);
let llvm_arg_elem_ty = ctx.get_llvm_type(generator, arg_elem_ty);
let ret_elem_ty = get_ret_elem_type(ctx, arg_elem_ty); let ret_elem_ty = get_ret_elem_type(ctx, arg_elem_ty);
let ndarray = ndarray_elementwise_unaryop_impl( let ndarray = ndarray_elementwise_unaryop_impl(
@ -1121,7 +1135,7 @@ where
ctx, ctx,
ret_elem_ty, ret_elem_ty,
None, None,
NDArrayValue::from_pointer_value(x, llvm_usize, None), NDArrayValue::from_pointer_value(x, llvm_arg_elem_ty, llvm_usize, None),
|generator, ctx, elem_val| { |generator, ctx, elem_val| {
helper_call_numpy_unary_elementwise( helper_call_numpy_unary_elementwise(
generator, generator,
@ -1508,8 +1522,8 @@ pub fn call_numpy_arctan2<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_arctan2(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_arctan2(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1575,8 +1589,8 @@ pub fn call_numpy_copysign<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_copysign(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_copysign(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1642,8 +1656,8 @@ pub fn call_numpy_fmax<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_fmax(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_fmax(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1709,8 +1723,8 @@ pub fn call_numpy_fmin<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_fmin(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_fmin(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1765,8 +1779,8 @@ pub fn call_numpy_ldexp<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_ldexp(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_ldexp(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1832,8 +1846,8 @@ pub fn call_numpy_hypot<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_hypot(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_hypot(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1899,8 +1913,8 @@ pub fn call_numpy_nextafter<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
dtype, dtype,
None, None,
(x1, !is_ndarray1), (x1_ty, x1, !is_ndarray1),
(x2, !is_ndarray2), (x2_ty, x2, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
call_numpy_nextafter(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs)) call_numpy_nextafter(generator, ctx, (x1_scalar_ty, lhs), (x2_scalar_ty, rhs))
}, },
@ -1960,14 +1974,14 @@ pub fn call_np_linalg_cholesky<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
let dim1 = unsafe { let dim1 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None) .get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
.into_int_value() .into_int_value()
}; };
@ -2002,14 +2016,14 @@ pub fn call_np_linalg_qr<'ctx, G: CodeGenerator + ?Sized>(
unimplemented!("{FN_NAME} operates on float type NdArrays only"); unimplemented!("{FN_NAME} operates on float type NdArrays only");
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
let dim1 = unsafe { let dim1 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None) .get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
.into_int_value() .into_int_value()
}; };
@ -2052,15 +2066,15 @@ pub fn call_np_linalg_svd<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
let dim1 = unsafe { let dim1 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None) .get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
.into_int_value() .into_int_value()
}; };
@ -2107,14 +2121,14 @@ pub fn call_np_linalg_inv<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
let dim1 = unsafe { let dim1 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None) .get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
.into_int_value() .into_int_value()
}; };
@ -2149,15 +2163,15 @@ pub fn call_np_linalg_pinv<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
let dim1 = unsafe { let dim1 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None) .get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
.into_int_value() .into_int_value()
}; };
@ -2192,15 +2206,15 @@ pub fn call_sp_linalg_lu<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
let dim1 = unsafe { let dim1 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None) .get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
.into_int_value() .into_int_value()
}; };
@ -2245,7 +2259,7 @@ pub fn call_np_linalg_matrix_power<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty, x2_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty, x2_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
// Changing second parameter to a `NDArray` for uniformity in function call // Changing second parameter to a `NDArray` for uniformity in function call
let n2_array = numpy::create_ndarray_const_shape( let n2_array = numpy::create_ndarray_const_shape(
generator, generator,
@ -2265,12 +2279,12 @@ pub fn call_np_linalg_matrix_power<'ctx, G: CodeGenerator + ?Sized>(
let n2_array = n2_array.as_base_value().as_basic_value_enum(); let n2_array = n2_array.as_base_value().as_basic_value_enum();
let outdim0 = unsafe { let outdim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
let outdim1 = unsafe { let outdim1 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None) .get_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
.into_int_value() .into_int_value()
}; };
@ -2340,10 +2354,10 @@ pub fn call_sp_linalg_schur<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };
@ -2383,10 +2397,10 @@ pub fn call_sp_linalg_hessenberg<'ctx, G: CodeGenerator + ?Sized>(
unsupported_type(ctx, FN_NAME, &[x1_ty]); unsupported_type(ctx, FN_NAME, &[x1_ty]);
}; };
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1 = NDArrayValue::from_pointer_value(n1, n1_elem_ty, llvm_usize, None);
let dim0 = unsafe { let dim0 = unsafe {
n1.dim_sizes() n1.shape()
.get_unchecked(ctx, generator, &llvm_usize.const_zero(), None) .get_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
.into_int_value() .into_int_value()
}; };

View File

@ -1564,10 +1564,21 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2)); assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2));
let left_val = let llvm_ndarray_dtype1 = ctx.get_llvm_type(generator, ndarray_dtype1);
NDArrayValue::from_pointer_value(left_val.into_pointer_value(), llvm_usize, None); let llvm_ndarray_dtype2 = ctx.get_llvm_type(generator, ndarray_dtype2);
let right_val =
NDArrayValue::from_pointer_value(right_val.into_pointer_value(), llvm_usize, None); let left_val = NDArrayValue::from_pointer_value(
left_val.into_pointer_value(),
llvm_ndarray_dtype1,
llvm_usize,
None,
);
let right_val = NDArrayValue::from_pointer_value(
right_val.into_pointer_value(),
llvm_ndarray_dtype2,
llvm_usize,
None,
);
let res = if op.base == Operator::MatMult { let res = if op.base == Operator::MatMult {
// MatMult is the only binop which is not an elementwise op // MatMult is the only binop which is not an elementwise op
@ -1591,8 +1602,8 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
BinopVariant::Normal => None, BinopVariant::Normal => None,
BinopVariant::AugAssign => Some(left_val), BinopVariant::AugAssign => Some(left_val),
}, },
(left_val.as_base_value().into(), false), (ty1, left_val.as_base_value().into(), false),
(right_val.as_base_value().into(), false), (ty2, right_val.as_base_value().into(), false),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
gen_binop_expr_with_values( gen_binop_expr_with_values(
generator, generator,
@ -1616,8 +1627,10 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
} else { } else {
let (ndarray_dtype, _) = let (ndarray_dtype, _) =
unpack_ndarray_var_tys(&mut ctx.unifier, if is_ndarray1 { ty1 } else { ty2 }); unpack_ndarray_var_tys(&mut ctx.unifier, if is_ndarray1 { ty1 } else { ty2 });
let llvm_ndarray_dtype = ctx.get_llvm_type(generator, ndarray_dtype);
let ndarray_val = NDArrayValue::from_pointer_value( let ndarray_val = NDArrayValue::from_pointer_value(
if is_ndarray1 { left_val } else { right_val }.into_pointer_value(), if is_ndarray1 { left_val } else { right_val }.into_pointer_value(),
llvm_ndarray_dtype,
llvm_usize, llvm_usize,
None, None,
); );
@ -1629,8 +1642,8 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
BinopVariant::Normal => None, BinopVariant::Normal => None,
BinopVariant::AugAssign => Some(ndarray_val), BinopVariant::AugAssign => Some(ndarray_val),
}, },
(left_val, !is_ndarray1), (ty1, left_val, !is_ndarray1),
(right_val, !is_ndarray2), (ty2, right_val, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
gen_binop_expr_with_values( gen_binop_expr_with_values(
generator, generator,
@ -1810,8 +1823,14 @@ pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>(
} else if ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) { } else if ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) {
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
let (ndarray_dtype, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty); let (ndarray_dtype, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty);
let llvm_ndarray_dtype = ctx.get_llvm_type(generator, ndarray_dtype);
let val = NDArrayValue::from_pointer_value(val.into_pointer_value(), llvm_usize, None); let val = NDArrayValue::from_pointer_value(
val.into_pointer_value(),
llvm_ndarray_dtype,
llvm_usize,
None,
);
// ndarray uses `~` rather than `not` to perform elementwise inversion, convert it before // ndarray uses `~` rather than `not` to perform elementwise inversion, convert it before
// passing it to the elementwise codegen function // passing it to the elementwise codegen function
@ -1902,15 +1921,21 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2)); assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2));
let left_val = let llvm_ndarray_dtype1 = ctx.get_llvm_type(generator, ndarray_dtype1);
NDArrayValue::from_pointer_value(lhs.into_pointer_value(), llvm_usize, None);
let left_val = NDArrayValue::from_pointer_value(
lhs.into_pointer_value(),
llvm_ndarray_dtype1,
llvm_usize,
None,
);
let res = numpy::ndarray_elementwise_binop_impl( let res = numpy::ndarray_elementwise_binop_impl(
generator, generator,
ctx, ctx,
ctx.primitives.bool, ctx.primitives.bool,
None, None,
(left_val.as_base_value().into(), false), (left_ty, left_val.as_base_value().into(), false),
(rhs, false), (right_ty, rhs, false),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
let val = gen_cmpop_expr_with_values( let val = gen_cmpop_expr_with_values(
generator, generator,
@ -1941,8 +1966,8 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
ctx, ctx,
ctx.primitives.bool, ctx.primitives.bool,
None, None,
(lhs, !is_ndarray1), (left_ty, lhs, !is_ndarray1),
(rhs, !is_ndarray2), (right_ty, rhs, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { |generator, ctx, (lhs, rhs)| {
let val = gen_cmpop_expr_with_values( let val = gen_cmpop_expr_with_values(
generator, generator,
@ -2606,7 +2631,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
let llvm_i32 = ctx.ctx.i32_type(); let llvm_i32 = ctx.ctx.i32_type();
let len = unsafe { let len = unsafe {
v.dim_sizes().get_typed_unchecked( v.shape().get_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(dim, true), &llvm_usize.const_int(dim, true),
@ -2647,7 +2672,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
ExprKind::Slice { lower, upper, step } => { ExprKind::Slice { lower, upper, step } => {
let dim_sz = unsafe { let dim_sz = unsafe {
v.dim_sizes().get_typed_unchecked( v.shape().get_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(dim, false), &llvm_usize.const_int(dim, false),
@ -2771,8 +2796,12 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
// elements over // elements over
let subscripted_ndarray = let subscripted_ndarray =
generator.gen_var_alloc(ctx, llvm_ndarray_t.into(), None)?; generator.gen_var_alloc(ctx, llvm_ndarray_t.into(), None)?;
let ndarray = let ndarray = NDArrayValue::from_pointer_value(
NDArrayValue::from_pointer_value(subscripted_ndarray, llvm_usize, None); subscripted_ndarray,
llvm_ndarray_data_t,
llvm_usize,
None,
);
let num_dims = v.load_ndims(ctx); let num_dims = v.load_ndims(ctx);
ndarray.store_ndims( ndarray.store_ndims(
@ -2784,7 +2813,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
); );
let ndarray_num_dims = ndarray.load_ndims(ctx); let ndarray_num_dims = ndarray.load_ndims(ctx);
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims); ndarray.create_shape(ctx, llvm_usize, ndarray_num_dims);
let ndarray_num_dims = ctx let ndarray_num_dims = ctx
.builder .builder
@ -2795,7 +2824,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
) )
.unwrap(); .unwrap();
let v_dims_src_ptr = unsafe { let v_dims_src_ptr = unsafe {
v.dim_sizes().ptr_offset_unchecked( v.shape().ptr_offset_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(1, false), &llvm_usize.const_int(1, false),
@ -2804,7 +2833,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
}; };
call_memcpy_generic( call_memcpy_generic(
ctx, ctx,
ndarray.dim_sizes().base_ptr(ctx, generator), ndarray.shape().base_ptr(ctx, generator),
v_dims_src_ptr, v_dims_src_ptr,
ctx.builder ctx.builder
.build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "") .build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "")
@ -2816,7 +2845,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
&ndarray.dim_sizes().as_slice_value(ctx, generator), &ndarray.shape().as_slice_value(ctx, generator),
(None, None), (None, None),
); );
let ndarray_num_elems = ctx let ndarray_num_elems = ctx
@ -3510,6 +3539,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
} }
TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::NDArray.id() => { TypeEnum::TObj { obj_id, params, .. } if *obj_id == PrimDef::NDArray.id() => {
let (ty, ndims) = params.iter().map(|(_, ty)| ty).collect_tuple().unwrap(); let (ty, ndims) = params.iter().map(|(_, ty)| ty).collect_tuple().unwrap();
let llvm_ty = ctx.get_llvm_type(generator, *ty);
let v = if let Some(v) = generator.gen_expr(ctx, value)? { let v = if let Some(v) = generator.gen_expr(ctx, value)? {
v.to_basic_value_enum(ctx, generator, value.custom.unwrap())? v.to_basic_value_enum(ctx, generator, value.custom.unwrap())?
@ -3517,7 +3547,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
} else { } else {
return Ok(None); return Ok(None);
}; };
let v = NDArrayValue::from_pointer_value(v, usize, None); let v = NDArrayValue::from_pointer_value(v, llvm_ty, usize, None);
return gen_ndarray_subscript_expr(generator, ctx, *ty, *ndims, v, slice); return gen_ndarray_subscript_expr(generator, ctx, *ty, *ndims, v, slice);
} }

View File

@ -103,7 +103,7 @@ pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
}); });
let ndarray_num_dims = ndarray.load_ndims(ctx); let ndarray_num_dims = ndarray.load_ndims(ctx);
let ndarray_dims = ndarray.dim_sizes(); let ndarray_dims = ndarray.shape();
let indices = ctx.builder.build_array_alloca(llvm_i32, ndarray_num_dims, "").unwrap(); let indices = ctx.builder.build_array_alloca(llvm_i32, ndarray_num_dims, "").unwrap();
@ -172,7 +172,7 @@ where
}); });
let ndarray_num_dims = ndarray.load_ndims(ctx); let ndarray_num_dims = ndarray.load_ndims(ctx);
let ndarray_dims = ndarray.dim_sizes(); let ndarray_dims = ndarray.shape();
let index = ctx let index = ctx
.builder .builder
@ -259,8 +259,8 @@ pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>(
let idx = ctx.builder.build_int_sub(min_ndims, idx, "").unwrap(); let idx = ctx.builder.build_int_sub(min_ndims, idx, "").unwrap();
let (lhs_dim_sz, rhs_dim_sz) = unsafe { let (lhs_dim_sz, rhs_dim_sz) = unsafe {
( (
lhs.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None), lhs.shape().get_typed_unchecked(ctx, generator, &idx, None),
rhs.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None), rhs.shape().get_typed_unchecked(ctx, generator, &idx, None),
) )
}; };
@ -298,9 +298,9 @@ pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>(
.unwrap(); .unwrap();
let max_ndims = llvm_intrinsics::call_int_umax(ctx, lhs_ndims, rhs_ndims, None); let max_ndims = llvm_intrinsics::call_int_umax(ctx, lhs_ndims, rhs_ndims, None);
let lhs_dims = lhs.dim_sizes().base_ptr(ctx, generator); let lhs_dims = lhs.shape().base_ptr(ctx, generator);
let lhs_ndims = lhs.load_ndims(ctx); let lhs_ndims = lhs.load_ndims(ctx);
let rhs_dims = rhs.dim_sizes().base_ptr(ctx, generator); let rhs_dims = rhs.shape().base_ptr(ctx, generator);
let rhs_ndims = rhs.load_ndims(ctx); let rhs_ndims = rhs.load_ndims(ctx);
let out_dims = ctx.builder.build_array_alloca(llvm_usize, max_ndims, "").unwrap(); let out_dims = ctx.builder.build_array_alloca(llvm_usize, max_ndims, "").unwrap();
let out_dims = ArraySliceValue::from_ptr_val(out_dims, max_ndims, None); let out_dims = ArraySliceValue::from_ptr_val(out_dims, max_ndims, None);
@ -362,7 +362,7 @@ pub fn call_ndarray_calc_broadcast_index<
let broadcast_size = broadcast_idx.size(ctx, generator); let broadcast_size = broadcast_idx.size(ctx, generator);
let out_idx = ctx.builder.build_array_alloca(llvm_i32, broadcast_size, "").unwrap(); let out_idx = ctx.builder.build_array_alloca(llvm_i32, broadcast_size, "").unwrap();
let array_dims = array.dim_sizes().base_ptr(ctx, generator); let array_dims = array.shape().base_ptr(ctx, generator);
let array_ndims = array.load_ndims(ctx); let array_ndims = array.load_ndims(ctx);
let broadcast_idx_ptr = unsafe { let broadcast_idx_ptr = unsafe {
broadcast_idx.ptr_offset_unchecked(ctx, generator, &llvm_usize.const_zero(), None) broadcast_idx.ptr_offset_unchecked(ctx, generator, &llvm_usize.const_zero(), None)

View File

@ -26,7 +26,7 @@ use super::{
use crate::{ use crate::{
symbol_resolver::ValueEnum, symbol_resolver::ValueEnum,
toplevel::{ toplevel::{
helper::PrimDef, helper::{arraylike_flatten_element_type, PrimDef},
numpy::{make_ndarray_ty, unpack_ndarray_var_tys}, numpy::{make_ndarray_ty, unpack_ndarray_var_tys},
DefinitionId, DefinitionId,
}, },
@ -42,6 +42,7 @@ fn create_ndarray_uninitialized<'ctx, G: CodeGenerator + ?Sized>(
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
) -> Result<NDArrayValue<'ctx>, String> { ) -> Result<NDArrayValue<'ctx>, String> {
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None); let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None);
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
@ -54,7 +55,7 @@ fn create_ndarray_uninitialized<'ctx, G: CodeGenerator + ?Sized>(
let ndarray = generator.gen_var_alloc(ctx, llvm_ndarray_t.into(), None)?; let ndarray = generator.gen_var_alloc(ctx, llvm_ndarray_t.into(), None)?;
Ok(NDArrayValue::from_pointer_value(ndarray, llvm_usize, None)) Ok(NDArrayValue::from_pointer_value(ndarray, llvm_elem_ty, llvm_usize, None))
} }
/// Creates an `NDArray` instance from a dynamic shape. /// Creates an `NDArray` instance from a dynamic shape.
@ -127,7 +128,7 @@ where
ndarray.store_ndims(ctx, generator, num_dims); ndarray.store_ndims(ctx, generator, num_dims);
let ndarray_num_dims = ndarray.load_ndims(ctx); let ndarray_num_dims = ndarray.load_ndims(ctx);
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims); ndarray.create_shape(ctx, llvm_usize, ndarray_num_dims);
// Copy the dimension sizes from shape to ndarray.dims // Copy the dimension sizes from shape to ndarray.dims
let shape_len = shape_len_fn(generator, ctx, shape)?; let shape_len = shape_len_fn(generator, ctx, shape)?;
@ -143,7 +144,7 @@ where
let shape_dim = ctx.builder.build_int_z_extend(shape_dim, llvm_usize, "").unwrap(); let shape_dim = ctx.builder.build_int_z_extend(shape_dim, llvm_usize, "").unwrap();
let ndarray_pdim = let ndarray_pdim =
unsafe { ndarray.dim_sizes().ptr_offset_unchecked(ctx, generator, &i, None) }; unsafe { ndarray.shape().ptr_offset_unchecked(ctx, generator, &i, None) };
ctx.builder.build_store(ndarray_pdim, shape_dim).unwrap(); ctx.builder.build_store(ndarray_pdim, shape_dim).unwrap();
@ -194,12 +195,12 @@ pub fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>(
ndarray.store_ndims(ctx, generator, num_dims); ndarray.store_ndims(ctx, generator, num_dims);
let ndarray_num_dims = ndarray.load_ndims(ctx); let ndarray_num_dims = ndarray.load_ndims(ctx);
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims); ndarray.create_shape(ctx, llvm_usize, ndarray_num_dims);
for (i, &shape_dim) in shape.iter().enumerate() { for (i, &shape_dim) in shape.iter().enumerate() {
let shape_dim = ctx.builder.build_int_z_extend(shape_dim, llvm_usize, "").unwrap(); let shape_dim = ctx.builder.build_int_z_extend(shape_dim, llvm_usize, "").unwrap();
let ndarray_dim = unsafe { let ndarray_dim = unsafe {
ndarray.dim_sizes().ptr_offset_unchecked( ndarray.shape().ptr_offset_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(i as u64, true), &llvm_usize.const_int(i as u64, true),
@ -228,7 +229,7 @@ fn ndarray_init_data<'ctx, G: CodeGenerator + ?Sized>(
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
&ndarray.dim_sizes().as_slice_value(ctx, generator), &ndarray.shape().as_slice_value(ctx, generator),
(None, None), (None, None),
); );
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems); ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
@ -379,7 +380,7 @@ where
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
&ndarray.dim_sizes().as_slice_value(ctx, generator), &ndarray.shape().as_slice_value(ctx, generator),
(None, None), (None, None),
); );
@ -473,8 +474,8 @@ fn ndarray_broadcast_fill<'ctx, 'a, G, ValueFn>(
generator: &mut G, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, 'a>, ctx: &mut CodeGenContext<'ctx, 'a>,
res: NDArrayValue<'ctx>, res: NDArrayValue<'ctx>,
lhs: (BasicValueEnum<'ctx>, bool), lhs: (Type, BasicValueEnum<'ctx>, bool),
rhs: (BasicValueEnum<'ctx>, bool), rhs: (Type, BasicValueEnum<'ctx>, bool),
value_fn: ValueFn, value_fn: ValueFn,
) -> Result<NDArrayValue<'ctx>, String> ) -> Result<NDArrayValue<'ctx>, String>
where where
@ -487,8 +488,8 @@ where
{ {
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
let (lhs_val, lhs_scalar) = lhs; let (lhs_ty, lhs_val, lhs_scalar) = lhs;
let (rhs_val, rhs_scalar) = rhs; let (rhs_ty, rhs_val, rhs_scalar) = rhs;
assert!( assert!(
!(lhs_scalar && rhs_scalar), !(lhs_scalar && rhs_scalar),
@ -499,14 +500,26 @@ where
// Assert that all ndarray operands are broadcastable to the target size // Assert that all ndarray operands are broadcastable to the target size
if !lhs_scalar { if !lhs_scalar {
let lhs_val = let lhs_dtype = arraylike_flatten_element_type(&mut ctx.unifier, lhs_ty);
NDArrayValue::from_pointer_value(lhs_val.into_pointer_value(), llvm_usize, None); let llvm_lhs_elem_ty = ctx.get_llvm_type(generator, lhs_dtype);
let lhs_val = NDArrayValue::from_pointer_value(
lhs_val.into_pointer_value(),
llvm_lhs_elem_ty,
llvm_usize,
None,
);
ndarray_assert_is_broadcastable(generator, ctx, res, lhs_val); ndarray_assert_is_broadcastable(generator, ctx, res, lhs_val);
} }
if !rhs_scalar { if !rhs_scalar {
let rhs_val = let rhs_dtype = arraylike_flatten_element_type(&mut ctx.unifier, rhs_ty);
NDArrayValue::from_pointer_value(rhs_val.into_pointer_value(), llvm_usize, None); let llvm_rhs_elem_ty = ctx.get_llvm_type(generator, rhs_dtype);
let rhs_val = NDArrayValue::from_pointer_value(
rhs_val.into_pointer_value(),
llvm_rhs_elem_ty,
llvm_usize,
None,
);
ndarray_assert_is_broadcastable(generator, ctx, res, rhs_val); ndarray_assert_is_broadcastable(generator, ctx, res, rhs_val);
} }
@ -514,8 +527,14 @@ where
let lhs_elem = if lhs_scalar { let lhs_elem = if lhs_scalar {
lhs_val lhs_val
} else { } else {
let lhs = let lhs_dtype = arraylike_flatten_element_type(&mut ctx.unifier, lhs_ty);
NDArrayValue::from_pointer_value(lhs_val.into_pointer_value(), llvm_usize, None); let llvm_lhs_elem_ty = ctx.get_llvm_type(generator, lhs_dtype);
let lhs = NDArrayValue::from_pointer_value(
lhs_val.into_pointer_value(),
llvm_lhs_elem_ty,
llvm_usize,
None,
);
let lhs_idx = call_ndarray_calc_broadcast_index(generator, ctx, lhs, idx); let lhs_idx = call_ndarray_calc_broadcast_index(generator, ctx, lhs, idx);
unsafe { lhs.data().get_unchecked(ctx, generator, &lhs_idx, None) } unsafe { lhs.data().get_unchecked(ctx, generator, &lhs_idx, None) }
@ -524,8 +543,14 @@ where
let rhs_elem = if rhs_scalar { let rhs_elem = if rhs_scalar {
rhs_val rhs_val
} else { } else {
let rhs = let rhs_dtype = arraylike_flatten_element_type(&mut ctx.unifier, rhs_ty);
NDArrayValue::from_pointer_value(rhs_val.into_pointer_value(), llvm_usize, None); let llvm_rhs_elem_ty = ctx.get_llvm_type(generator, rhs_dtype);
let rhs = NDArrayValue::from_pointer_value(
rhs_val.into_pointer_value(),
llvm_rhs_elem_ty,
llvm_usize,
None,
);
let rhs_idx = call_ndarray_calc_broadcast_index(generator, ctx, rhs, idx); let rhs_idx = call_ndarray_calc_broadcast_index(generator, ctx, rhs, idx);
unsafe { rhs.data().get_unchecked(ctx, generator, &rhs_idx, None) } unsafe { rhs.data().get_unchecked(ctx, generator, &rhs_idx, None) }
@ -671,7 +696,7 @@ fn llvm_ndlist_get_ndims<'ctx, G: CodeGenerator + ?Sized>(
fn llvm_arraylike_get_ndims<'ctx, G: CodeGenerator + ?Sized>( fn llvm_arraylike_get_ndims<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
value: BasicValueEnum<'ctx>, (ty, value): (Type, BasicValueEnum<'ctx>),
) -> IntValue<'ctx> { ) -> IntValue<'ctx> {
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
@ -679,7 +704,9 @@ fn llvm_arraylike_get_ndims<'ctx, G: CodeGenerator + ?Sized>(
BasicValueEnum::PointerValue(v) BasicValueEnum::PointerValue(v)
if NDArrayValue::is_representable(v, llvm_usize).is_ok() => if NDArrayValue::is_representable(v, llvm_usize).is_ok() =>
{ {
NDArrayValue::from_pointer_value(v, llvm_usize, None).load_ndims(ctx) let dtype = arraylike_flatten_element_type(&mut ctx.unifier, ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, dtype);
NDArrayValue::from_pointer_value(v, llvm_elem_ty, llvm_usize, None).load_ndims(ctx)
} }
BasicValueEnum::PointerValue(v) if ListValue::is_representable(v, llvm_usize).is_ok() => { BasicValueEnum::PointerValue(v) if ListValue::is_representable(v, llvm_usize).is_ok() => {
@ -694,7 +721,6 @@ fn llvm_arraylike_get_ndims<'ctx, G: CodeGenerator + ?Sized>(
fn ndarray_from_ndlist_impl<'ctx, G: CodeGenerator + ?Sized>( fn ndarray_from_ndlist_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type,
(dst_arr, dst_slice_ptr): (NDArrayValue<'ctx>, PointerValue<'ctx>), (dst_arr, dst_slice_ptr): (NDArrayValue<'ctx>, PointerValue<'ctx>),
src_lst: ListValue<'ctx>, src_lst: ListValue<'ctx>,
dim: u64, dim: u64,
@ -713,7 +739,7 @@ fn ndarray_from_ndlist_impl<'ctx, G: CodeGenerator + ?Sized>(
let stride = call_ndarray_calc_size( let stride = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
&dst_arr.dim_sizes(), &dst_arr.shape(),
(Some(llvm_usize.const_int(dim + 1, false)), None), (Some(llvm_usize.const_int(dim + 1, false)), None),
); );
@ -727,6 +753,20 @@ fn ndarray_from_ndlist_impl<'ctx, G: CodeGenerator + ?Sized>(
|_, _| Ok(llvm_usize.const_int(1, false)), |_, _| Ok(llvm_usize.const_int(1, false)),
|generator, ctx, _, i| { |generator, ctx, _, i| {
let offset = ctx.builder.build_int_mul(stride, i, "").unwrap(); let offset = ctx.builder.build_int_mul(stride, i, "").unwrap();
let offset = ctx
.builder
.build_int_mul(
offset,
ctx.builder
.build_int_truncate_or_bit_cast(
dst_arr.get_type().element_type().size_of().unwrap(),
offset.get_type(),
"",
)
.unwrap(),
"",
)
.unwrap();
let dst_ptr = let dst_ptr =
unsafe { ctx.builder.build_gep(dst_slice_ptr, &[offset], "").unwrap() }; unsafe { ctx.builder.build_gep(dst_slice_ptr, &[offset], "").unwrap() };
@ -741,7 +781,6 @@ fn ndarray_from_ndlist_impl<'ctx, G: CodeGenerator + ?Sized>(
ndarray_from_ndlist_impl( ndarray_from_ndlist_impl(
generator, generator,
ctx, ctx,
elem_ty,
(dst_arr, dst_ptr), (dst_arr, dst_ptr),
nested_lst_elem, nested_lst_elem,
dim + 1, dim + 1,
@ -760,7 +799,7 @@ fn ndarray_from_ndlist_impl<'ctx, G: CodeGenerator + ?Sized>(
_ => { _ => {
let lst_len = src_lst.load_size(ctx, None); let lst_len = src_lst.load_size(ctx, None);
let sizeof_elem = ctx.get_llvm_type(generator, elem_ty).size_of().unwrap(); let sizeof_elem = dst_arr.get_type().element_type().size_of().unwrap();
let sizeof_elem = ctx.builder.build_int_cast(sizeof_elem, llvm_usize, "").unwrap(); let sizeof_elem = ctx.builder.build_int_cast(sizeof_elem, llvm_usize, "").unwrap();
let cpy_len = ctx let cpy_len = ctx
@ -816,7 +855,8 @@ fn call_ndarray_array_impl<'ctx, G: CodeGenerator + ?Sized>(
// object is an NDArray instance - copy object unless copy=0 && ndmin < object.ndims // object is an NDArray instance - copy object unless copy=0 && ndmin < object.ndims
if NDArrayValue::is_representable(object, llvm_usize).is_ok() { if NDArrayValue::is_representable(object, llvm_usize).is_ok() {
let object = NDArrayValue::from_pointer_value(object, llvm_usize, None); let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let object = NDArrayValue::from_pointer_value(object, llvm_elem_ty, llvm_usize, None);
let ndarray = gen_if_else_expr_callback( let ndarray = gen_if_else_expr_callback(
generator, generator,
@ -878,7 +918,6 @@ fn call_ndarray_array_impl<'ctx, G: CodeGenerator + ?Sized>(
ndarray_sliced_copyto_impl( ndarray_sliced_copyto_impl(
generator, generator,
ctx, ctx,
elem_ty,
(ndarray, ndarray.data().base_ptr(ctx, generator)), (ndarray, ndarray.data().base_ptr(ctx, generator)),
(object, object.data().base_ptr(ctx, generator)), (object, object.data().base_ptr(ctx, generator)),
0, 0,
@ -892,6 +931,7 @@ fn call_ndarray_array_impl<'ctx, G: CodeGenerator + ?Sized>(
return Ok(NDArrayValue::from_pointer_value( return Ok(NDArrayValue::from_pointer_value(
ndarray.map(BasicValueEnum::into_pointer_value).unwrap(), ndarray.map(BasicValueEnum::into_pointer_value).unwrap(),
llvm_elem_ty,
llvm_usize, llvm_usize,
None, None,
)); ));
@ -1026,7 +1066,6 @@ fn call_ndarray_array_impl<'ctx, G: CodeGenerator + ?Sized>(
ndarray_from_ndlist_impl( ndarray_from_ndlist_impl(
generator, generator,
ctx, ctx,
elem_ty,
(ndarray, ndarray.data().base_ptr(ctx, generator)), (ndarray, ndarray.data().base_ptr(ctx, generator)),
object, object,
0, 0,
@ -1099,7 +1138,6 @@ fn call_ndarray_eye_impl<'ctx, G: CodeGenerator + ?Sized>(
fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>( fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type,
(dst_arr, dst_slice_ptr): (NDArrayValue<'ctx>, PointerValue<'ctx>), (dst_arr, dst_slice_ptr): (NDArrayValue<'ctx>, PointerValue<'ctx>),
(src_arr, src_slice_ptr): (NDArrayValue<'ctx>, PointerValue<'ctx>), (src_arr, src_slice_ptr): (NDArrayValue<'ctx>, PointerValue<'ctx>),
dim: u64, dim: u64,
@ -1108,14 +1146,16 @@ fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>(
let llvm_i1 = ctx.ctx.bool_type(); let llvm_i1 = ctx.ctx.bool_type();
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
assert_eq!(dst_arr.get_type().element_type(), src_arr.get_type().element_type());
let sizeof_elem = dst_arr.get_type().element_type().size_of().unwrap();
// If there are no (remaining) slice expressions, memcpy the entire dimension // If there are no (remaining) slice expressions, memcpy the entire dimension
if slices.is_empty() { if slices.is_empty() {
let sizeof_elem = ctx.get_llvm_type(generator, elem_ty).size_of().unwrap();
let stride = call_ndarray_calc_size( let stride = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
&src_arr.dim_sizes(), &src_arr.shape(),
(Some(llvm_usize.const_int(dim, false)), None), (Some(llvm_usize.const_int(dim, false)), None),
); );
let stride = let stride =
@ -1133,13 +1173,13 @@ fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>(
let src_stride = call_ndarray_calc_size( let src_stride = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
&src_arr.dim_sizes(), &src_arr.shape(),
(Some(llvm_usize.const_int(dim + 1, false)), None), (Some(llvm_usize.const_int(dim + 1, false)), None),
); );
let dst_stride = call_ndarray_calc_size( let dst_stride = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
&dst_arr.dim_sizes(), &dst_arr.shape(),
(Some(llvm_usize.const_int(dim + 1, false)), None), (Some(llvm_usize.const_int(dim + 1, false)), None),
); );
@ -1162,9 +1202,29 @@ fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>(
|generator, ctx, _, src_i| { |generator, ctx, _, src_i| {
// Calculate the offset of the active slice // Calculate the offset of the active slice
let src_data_offset = ctx.builder.build_int_mul(src_stride, src_i, "").unwrap(); let src_data_offset = ctx.builder.build_int_mul(src_stride, src_i, "").unwrap();
let src_data_offset = ctx
.builder
.build_int_mul(
src_data_offset,
ctx.builder
.build_int_cast(sizeof_elem, src_data_offset.get_type(), "")
.unwrap(),
"",
)
.unwrap();
let dst_i = let dst_i =
ctx.builder.build_load(dst_i_addr, "").map(BasicValueEnum::into_int_value).unwrap(); ctx.builder.build_load(dst_i_addr, "").map(BasicValueEnum::into_int_value).unwrap();
let dst_data_offset = ctx.builder.build_int_mul(dst_stride, dst_i, "").unwrap(); let dst_data_offset = ctx.builder.build_int_mul(dst_stride, dst_i, "").unwrap();
let dst_data_offset = ctx
.builder
.build_int_mul(
dst_data_offset,
ctx.builder
.build_int_cast(sizeof_elem, dst_data_offset.get_type(), "")
.unwrap(),
"",
)
.unwrap();
let (src_ptr, dst_ptr) = unsafe { let (src_ptr, dst_ptr) = unsafe {
( (
@ -1176,7 +1236,6 @@ fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>(
ndarray_sliced_copyto_impl( ndarray_sliced_copyto_impl(
generator, generator,
ctx, ctx,
elem_ty,
(dst_arr, dst_ptr), (dst_arr, dst_ptr),
(src_arr, src_ptr), (src_arr, src_ptr),
dim + 1, dim + 1,
@ -1219,7 +1278,7 @@ pub fn ndarray_sliced_copy<'ctx, G: CodeGenerator + ?Sized>(
&this, &this,
|_, ctx, shape| Ok(shape.load_ndims(ctx)), |_, ctx, shape| Ok(shape.load_ndims(ctx)),
|generator, ctx, shape, idx| unsafe { |generator, ctx, shape, idx| unsafe {
Ok(shape.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None)) Ok(shape.shape().get_typed_unchecked(ctx, generator, &idx, None))
}, },
)? )?
} else { } else {
@ -1227,7 +1286,7 @@ pub fn ndarray_sliced_copy<'ctx, G: CodeGenerator + ?Sized>(
ndarray.store_ndims(ctx, generator, this.load_ndims(ctx)); ndarray.store_ndims(ctx, generator, this.load_ndims(ctx));
let ndims = this.load_ndims(ctx); let ndims = this.load_ndims(ctx);
ndarray.create_dim_sizes(ctx, llvm_usize, ndims); ndarray.create_shape(ctx, llvm_usize, ndims);
// Populate the first slices.len() dimensions by computing the size of each dim slice // Populate the first slices.len() dimensions by computing the size of each dim slice
for (i, (start, stop, step)) in slices.iter().enumerate() { for (i, (start, stop, step)) in slices.iter().enumerate() {
@ -1259,7 +1318,7 @@ pub fn ndarray_sliced_copy<'ctx, G: CodeGenerator + ?Sized>(
ctx.builder.build_int_z_extend_or_bit_cast(slice_len, llvm_usize, "").unwrap(); ctx.builder.build_int_z_extend_or_bit_cast(slice_len, llvm_usize, "").unwrap();
unsafe { unsafe {
ndarray.dim_sizes().set_typed_unchecked( ndarray.shape().set_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(i as u64, false), &llvm_usize.const_int(i as u64, false),
@ -1277,8 +1336,8 @@ pub fn ndarray_sliced_copy<'ctx, G: CodeGenerator + ?Sized>(
(this.load_ndims(ctx), false), (this.load_ndims(ctx), false),
|generator, ctx, _, idx| { |generator, ctx, _, idx| {
unsafe { unsafe {
let dim_sz = this.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None); let dim_sz = this.shape().get_typed_unchecked(ctx, generator, &idx, None);
ndarray.dim_sizes().set_typed_unchecked(ctx, generator, &idx, dim_sz); ndarray.shape().set_typed_unchecked(ctx, generator, &idx, dim_sz);
} }
Ok(()) Ok(())
@ -1293,7 +1352,6 @@ pub fn ndarray_sliced_copy<'ctx, G: CodeGenerator + ?Sized>(
ndarray_sliced_copyto_impl( ndarray_sliced_copyto_impl(
generator, generator,
ctx, ctx,
elem_ty,
(ndarray, ndarray.data().base_ptr(ctx, generator)), (ndarray, ndarray.data().base_ptr(ctx, generator)),
(this, this.data().base_ptr(ctx, generator)), (this, this.data().base_ptr(ctx, generator)),
0, 0,
@ -1339,7 +1397,7 @@ where
&operand, &operand,
|_, ctx, v| Ok(v.load_ndims(ctx)), |_, ctx, v| Ok(v.load_ndims(ctx)),
|generator, ctx, v, idx| unsafe { |generator, ctx, v, idx| unsafe {
Ok(v.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None)) Ok(v.shape().get_typed_unchecked(ctx, generator, &idx, None))
}, },
) )
.unwrap() .unwrap()
@ -1376,8 +1434,8 @@ pub fn ndarray_elementwise_binop_impl<'ctx, 'a, G, ValueFn>(
ctx: &mut CodeGenContext<'ctx, 'a>, ctx: &mut CodeGenContext<'ctx, 'a>,
elem_ty: Type, elem_ty: Type,
res: Option<NDArrayValue<'ctx>>, res: Option<NDArrayValue<'ctx>>,
lhs: (BasicValueEnum<'ctx>, bool), lhs: (Type, BasicValueEnum<'ctx>, bool),
rhs: (BasicValueEnum<'ctx>, bool), rhs: (Type, BasicValueEnum<'ctx>, bool),
value_fn: ValueFn, value_fn: ValueFn,
) -> Result<NDArrayValue<'ctx>, String> ) -> Result<NDArrayValue<'ctx>, String>
where where
@ -1390,8 +1448,8 @@ where
{ {
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
let (lhs_val, lhs_scalar) = lhs; let (lhs_ty, lhs_val, lhs_scalar) = lhs;
let (rhs_val, rhs_scalar) = rhs; let (rhs_ty, rhs_val, rhs_scalar) = rhs;
assert!( assert!(
!(lhs_scalar && rhs_scalar), !(lhs_scalar && rhs_scalar),
@ -1402,10 +1460,22 @@ where
let ndarray = res.unwrap_or_else(|| { let ndarray = res.unwrap_or_else(|| {
if lhs_scalar && rhs_scalar { if lhs_scalar && rhs_scalar {
let lhs_val = let lhs_dtype = arraylike_flatten_element_type(&mut ctx.unifier, lhs_ty);
NDArrayValue::from_pointer_value(lhs_val.into_pointer_value(), llvm_usize, None); let llvm_lhs_elem_ty = ctx.get_llvm_type(generator, lhs_dtype);
let rhs_val = let lhs_val = NDArrayValue::from_pointer_value(
NDArrayValue::from_pointer_value(rhs_val.into_pointer_value(), llvm_usize, None); lhs_val.into_pointer_value(),
llvm_lhs_elem_ty,
llvm_usize,
None,
);
let rhs_dtype = arraylike_flatten_element_type(&mut ctx.unifier, rhs_ty);
let llvm_rhs_elem_ty = ctx.get_llvm_type(generator, rhs_dtype);
let rhs_val = NDArrayValue::from_pointer_value(
rhs_val.into_pointer_value(),
llvm_rhs_elem_ty,
llvm_usize,
None,
);
let ndarray_dims = call_ndarray_calc_broadcast(generator, ctx, lhs_val, rhs_val); let ndarray_dims = call_ndarray_calc_broadcast(generator, ctx, lhs_val, rhs_val);
@ -1421,8 +1491,14 @@ where
) )
.unwrap() .unwrap()
} else { } else {
let dtype = arraylike_flatten_element_type(
&mut ctx.unifier,
if lhs_scalar { rhs_ty } else { lhs_ty },
);
let llvm_elem_ty = ctx.get_llvm_type(generator, dtype);
let ndarray = NDArrayValue::from_pointer_value( let ndarray = NDArrayValue::from_pointer_value(
if lhs_scalar { rhs_val } else { lhs_val }.into_pointer_value(), if lhs_scalar { rhs_val } else { lhs_val }.into_pointer_value(),
llvm_elem_ty,
llvm_usize, llvm_usize,
None, None,
); );
@ -1434,7 +1510,7 @@ where
&ndarray, &ndarray,
|_, ctx, v| Ok(v.load_ndims(ctx)), |_, ctx, v| Ok(v.load_ndims(ctx)),
|generator, ctx, v, idx| unsafe { |generator, ctx, v, idx| unsafe {
Ok(v.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None)) Ok(v.shape().get_typed_unchecked(ctx, generator, &idx, None))
}, },
) )
.unwrap() .unwrap()
@ -1495,10 +1571,10 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
if let Some(res) = res { if let Some(res) = res {
let res_ndims = res.load_ndims(ctx); let res_ndims = res.load_ndims(ctx);
let res_dim0 = unsafe { let res_dim0 = unsafe {
res.dim_sizes().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None) res.shape().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
}; };
let res_dim1 = unsafe { let res_dim1 = unsafe {
res.dim_sizes().get_typed_unchecked( res.shape().get_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(1, false), &llvm_usize.const_int(1, false),
@ -1506,10 +1582,10 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
) )
}; };
let lhs_dim0 = unsafe { let lhs_dim0 = unsafe {
lhs.dim_sizes().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None) lhs.shape().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
}; };
let rhs_dim1 = unsafe { let rhs_dim1 = unsafe {
rhs.dim_sizes().get_typed_unchecked( rhs.shape().get_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(1, false), &llvm_usize.const_int(1, false),
@ -1558,15 +1634,10 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None { if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
let lhs_dim1 = unsafe { let lhs_dim1 = unsafe {
lhs.dim_sizes().get_typed_unchecked( lhs.shape().get_typed_unchecked(ctx, generator, &llvm_usize.const_int(1, false), None)
ctx,
generator,
&llvm_usize.const_int(1, false),
None,
)
}; };
let rhs_dim0 = unsafe { let rhs_dim0 = unsafe {
rhs.dim_sizes().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None) rhs.shape().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
}; };
// lhs.dims[1] == rhs.dims[0] // lhs.dims[1] == rhs.dims[0]
@ -1605,7 +1676,7 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
}, },
|generator, ctx| { |generator, ctx| {
Ok(Some(unsafe { Ok(Some(unsafe {
lhs.dim_sizes().get_typed_unchecked( lhs.shape().get_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_zero(), &llvm_usize.const_zero(),
@ -1615,7 +1686,7 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
}, },
|generator, ctx| { |generator, ctx| {
Ok(Some(unsafe { Ok(Some(unsafe {
rhs.dim_sizes().get_typed_unchecked( rhs.shape().get_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(1, false), &llvm_usize.const_int(1, false),
@ -1642,7 +1713,7 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
let common_dim = { let common_dim = {
let lhs_idx1 = unsafe { let lhs_idx1 = unsafe {
lhs.dim_sizes().get_typed_unchecked( lhs.shape().get_typed_unchecked(
ctx, ctx,
generator, generator,
&llvm_usize.const_int(1, false), &llvm_usize.const_int(1, false),
@ -1650,7 +1721,7 @@ pub fn ndarray_matmul_2d<'ctx, G: CodeGenerator>(
) )
}; };
let rhs_idx0 = unsafe { let rhs_idx0 = unsafe {
rhs.dim_sizes().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None) rhs.shape().get_typed_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
}; };
let idx = llvm_intrinsics::call_expect(ctx, rhs_idx0, lhs_idx1, None); let idx = llvm_intrinsics::call_expect(ctx, rhs_idx0, lhs_idx1, None);
@ -1981,11 +2052,18 @@ pub fn gen_ndarray_copy<'ctx>(
let this_arg = let this_arg =
obj.as_ref().unwrap().1.clone().to_basic_value_enum(context, generator, this_ty)?; obj.as_ref().unwrap().1.clone().to_basic_value_enum(context, generator, this_ty)?;
let llvm_elem_ty = context.get_llvm_type(generator, this_elem_ty);
ndarray_copy_impl( ndarray_copy_impl(
generator, generator,
context, context,
this_elem_ty, this_elem_ty,
NDArrayValue::from_pointer_value(this_arg.into_pointer_value(), llvm_usize, None), NDArrayValue::from_pointer_value(
this_arg.into_pointer_value(),
llvm_elem_ty,
llvm_usize,
None,
),
) )
.map(NDArrayValue::into) .map(NDArrayValue::into)
} }
@ -2004,6 +2082,7 @@ pub fn gen_ndarray_fill<'ctx>(
let llvm_usize = generator.get_size_type(context.ctx); let llvm_usize = generator.get_size_type(context.ctx);
let this_ty = obj.as_ref().unwrap().0; let this_ty = obj.as_ref().unwrap().0;
let this_elem_ty = arraylike_flatten_element_type(&mut context.unifier, this_ty);
let this_arg = obj let this_arg = obj
.as_ref() .as_ref()
.unwrap() .unwrap()
@ -2014,10 +2093,12 @@ pub fn gen_ndarray_fill<'ctx>(
let value_ty = fun.0.args[0].ty; let value_ty = fun.0.args[0].ty;
let value_arg = args[0].1.clone().to_basic_value_enum(context, generator, value_ty)?; let value_arg = args[0].1.clone().to_basic_value_enum(context, generator, value_ty)?;
let llvm_elem_ty = context.get_llvm_type(generator, this_elem_ty);
ndarray_fill_flattened( ndarray_fill_flattened(
generator, generator,
context, context,
NDArrayValue::from_pointer_value(this_arg, llvm_usize, None), NDArrayValue::from_pointer_value(this_arg, llvm_elem_ty, llvm_usize, None),
|generator, ctx, _| { |generator, ctx, _| {
let value = if value_arg.is_pointer_value() { let value = if value_arg.is_pointer_value() {
let llvm_i1 = ctx.ctx.bool_type(); let llvm_i1 = ctx.ctx.bool_type();
@ -2058,8 +2139,9 @@ pub fn ndarray_transpose<'ctx, G: CodeGenerator + ?Sized>(
if let BasicValueEnum::PointerValue(n1) = x1 { if let BasicValueEnum::PointerValue(n1) = x1 {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, x1_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, x1_ty);
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let n_sz = call_ndarray_calc_size(generator, ctx, &n1.dim_sizes(), (None, None)); let n1 = NDArrayValue::from_pointer_value(n1, llvm_elem_ty, llvm_usize, None);
let n_sz = call_ndarray_calc_size(generator, ctx, &n1.shape(), (None, None));
// Dimensions are reversed in the transposed array // Dimensions are reversed in the transposed array
let out = create_ndarray_dyn_shape( let out = create_ndarray_dyn_shape(
@ -2074,7 +2156,7 @@ pub fn ndarray_transpose<'ctx, G: CodeGenerator + ?Sized>(
.builder .builder
.build_int_sub(new_idx, new_idx.get_type().const_int(1, false), "") .build_int_sub(new_idx, new_idx.get_type().const_int(1, false), "")
.unwrap(); .unwrap();
unsafe { Ok(n.dim_sizes().get_typed_unchecked(ctx, generator, &new_idx, None)) } unsafe { Ok(n.shape().get_typed_unchecked(ctx, generator, &new_idx, None)) }
}, },
) )
.unwrap(); .unwrap();
@ -2111,7 +2193,7 @@ pub fn ndarray_transpose<'ctx, G: CodeGenerator + ?Sized>(
.build_int_sub(ndim_rev, llvm_usize.const_int(1, false), "") .build_int_sub(ndim_rev, llvm_usize.const_int(1, false), "")
.unwrap(); .unwrap();
let dim = unsafe { let dim = unsafe {
n1.dim_sizes().get_typed_unchecked(ctx, generator, &ndim_rev, None) n1.shape().get_typed_unchecked(ctx, generator, &ndim_rev, None)
}; };
let rem_idx_val = let rem_idx_val =
@ -2177,8 +2259,9 @@ pub fn ndarray_reshape<'ctx, G: CodeGenerator + ?Sized>(
if let BasicValueEnum::PointerValue(n1) = x1 { if let BasicValueEnum::PointerValue(n1) = x1 {
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, x1_ty); let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, x1_ty);
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let n_sz = call_ndarray_calc_size(generator, ctx, &n1.dim_sizes(), (None, None)); let n1 = NDArrayValue::from_pointer_value(n1, llvm_elem_ty, llvm_usize, None);
let n_sz = call_ndarray_calc_size(generator, ctx, &n1.shape(), (None, None));
let acc = generator.gen_var_alloc(ctx, llvm_usize.into(), None)?; let acc = generator.gen_var_alloc(ctx, llvm_usize.into(), None)?;
let num_neg = generator.gen_var_alloc(ctx, llvm_usize.into(), None)?; let num_neg = generator.gen_var_alloc(ctx, llvm_usize.into(), None)?;
@ -2406,7 +2489,7 @@ pub fn ndarray_reshape<'ctx, G: CodeGenerator + ?Sized>(
); );
// The new shape must be compatible with the old shape // The new shape must be compatible with the old shape
let out_sz = call_ndarray_calc_size(generator, ctx, &out.dim_sizes(), (None, None)); let out_sz = call_ndarray_calc_size(generator, ctx, &out.shape(), (None, None));
ctx.make_assert( ctx.make_assert(
generator, generator,
ctx.builder.build_int_compare(IntPredicate::EQ, out_sz, n_sz, "").unwrap(), ctx.builder.build_int_compare(IntPredicate::EQ, out_sz, n_sz, "").unwrap(),
@ -2454,17 +2537,22 @@ pub fn ndarray_dot<'ctx, G: CodeGenerator + ?Sized>(
) -> Result<BasicValueEnum<'ctx>, String> { ) -> Result<BasicValueEnum<'ctx>, String> {
const FN_NAME: &str = "ndarray_dot"; const FN_NAME: &str = "ndarray_dot";
let (x1_ty, x1) = x1; let (x1_ty, x1) = x1;
let (_, x2) = x2; let (x2_ty, x2) = x2;
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
match (x1, x2) { match (x1, x2) {
(BasicValueEnum::PointerValue(n1), BasicValueEnum::PointerValue(n2)) => { (BasicValueEnum::PointerValue(n1), BasicValueEnum::PointerValue(n2)) => {
let n1 = NDArrayValue::from_pointer_value(n1, llvm_usize, None); let n1_dtype = arraylike_flatten_element_type(&mut ctx.unifier, x1_ty);
let n2 = NDArrayValue::from_pointer_value(n2, llvm_usize, None); let n2_dtype = arraylike_flatten_element_type(&mut ctx.unifier, x2_ty);
let llvm_n1_data_ty = ctx.get_llvm_type(generator, n1_dtype);
let llvm_n2_data_ty = ctx.get_llvm_type(generator, n2_dtype);
let n1_sz = call_ndarray_calc_size(generator, ctx, &n1.dim_sizes(), (None, None)); let n1 = NDArrayValue::from_pointer_value(n1, llvm_n1_data_ty, llvm_usize, None);
let n2_sz = call_ndarray_calc_size(generator, ctx, &n1.dim_sizes(), (None, None)); let n2 = NDArrayValue::from_pointer_value(n2, llvm_n2_data_ty, llvm_usize, None);
let n1_sz = call_ndarray_calc_size(generator, ctx, &n1.shape(), (None, None));
let n2_sz = call_ndarray_calc_size(generator, ctx, &n1.shape(), (None, None));
ctx.make_assert( ctx.make_assert(
generator, generator,
@ -2501,7 +2589,7 @@ pub fn ndarray_dot<'ctx, G: CodeGenerator + ?Sized>(
.build_float_mul(e1, elem2.into_float_value(), "") .build_float_mul(e1, elem2.into_float_value(), "")
.unwrap() .unwrap()
.as_basic_value_enum(), .as_basic_value_enum(),
_ => codegen_unreachable!(ctx), _ => codegen_unreachable!(ctx, "product: {}", elem1.get_type()),
}; };
let acc_val = ctx.builder.build_load(acc, "").unwrap(); let acc_val = ctx.builder.build_load(acc, "").unwrap();
let acc_val = match acc_val { let acc_val = match acc_val {
@ -2515,7 +2603,7 @@ pub fn ndarray_dot<'ctx, G: CodeGenerator + ?Sized>(
.build_float_add(e1, product.into_float_value(), "") .build_float_add(e1, product.into_float_value(), "")
.unwrap() .unwrap()
.as_basic_value_enum(), .as_basic_value_enum(),
_ => codegen_unreachable!(ctx), _ => codegen_unreachable!(ctx, "acc_val: {}", acc_val.get_type()),
}; };
ctx.builder.build_store(acc, acc_val).unwrap(); ctx.builder.build_store(acc, acc_val).unwrap();

View File

@ -1,11 +1,17 @@
use inkwell::{ use inkwell::{
context::Context, context::Context,
types::{AnyTypeEnum, BasicType, BasicTypeEnum, IntType, PointerType}, types::{AnyTypeEnum, BasicType, BasicTypeEnum, IntType, PointerType},
values::IntValue, values::{IntValue, PointerValue},
AddressSpace, AddressSpace,
}; };
use itertools::Itertools;
use super::ProxyType; use nac3core_derive::StructFields;
use super::{
structure::{StructField, StructFields},
ProxyType,
};
use crate::codegen::{ use crate::codegen::{
values::{ArraySliceValue, NDArrayValue, ProxyValue}, values::{ArraySliceValue, NDArrayValue, ProxyValue},
{CodeGenContext, CodeGenerator}, {CodeGenContext, CodeGenerator},
@ -19,6 +25,16 @@ pub struct NDArrayType<'ctx> {
llvm_usize: IntType<'ctx>, llvm_usize: IntType<'ctx>,
} }
#[derive(PartialEq, Eq, Clone, Copy, StructFields)]
pub struct NDArrayStructFields<'ctx> {
#[value_type(usize)]
pub ndims: StructField<'ctx, IntValue<'ctx>>,
#[value_type(usize.ptr_type(AddressSpace::default()))]
pub shape: StructField<'ctx, PointerValue<'ctx>>,
#[value_type(i8_type().ptr_type(AddressSpace::default()))]
pub data: StructField<'ctx, PointerValue<'ctx>>,
}
impl<'ctx> NDArrayType<'ctx> { impl<'ctx> NDArrayType<'ctx> {
/// Checks whether `llvm_ty` represents a `ndarray` type, returning [Err] if it does not. /// Checks whether `llvm_ty` represents a `ndarray` type, returning [Err] if it does not.
pub fn is_representable( pub fn is_representable(
@ -67,35 +83,58 @@ impl<'ctx> NDArrayType<'ctx> {
} }
let ndarray_data_ty = llvm_ndarray_ty.get_field_type_at_index(2).unwrap(); let ndarray_data_ty = llvm_ndarray_ty.get_field_type_at_index(2).unwrap();
let Ok(_) = PointerType::try_from(ndarray_data_ty) else { let Ok(ndarray_pdata) = PointerType::try_from(ndarray_data_ty) else {
return Err(format!("Expected pointer type for `ndarray.2`, got {ndarray_data_ty}")); return Err(format!("Expected pointer type for `ndarray.2`, got {ndarray_data_ty}"));
}; };
let ndarray_data = ndarray_pdata.get_element_type();
let Ok(ndarray_data) = IntType::try_from(ndarray_data) else {
return Err(format!(
"Expected pointer-to-int type for `ndarray.2`, got pointer-to-{ndarray_data}"
));
};
if ndarray_data.get_bit_width() != 8 {
return Err(format!(
"Expected pointer-to-8-bit int type for `ndarray.1`, got pointer-to-{}-bit int",
ndarray_data.get_bit_width()
));
}
Ok(()) Ok(())
} }
// TODO: Move this into e.g. StructProxyType
#[must_use]
fn fields(ctx: &'ctx Context, llvm_usize: IntType<'ctx>) -> NDArrayStructFields<'ctx> {
NDArrayStructFields::new(ctx, llvm_usize)
}
// TODO: Move this into e.g. StructProxyType
#[must_use]
pub fn get_fields(
&self,
ctx: &'ctx Context,
llvm_usize: IntType<'ctx>,
) -> NDArrayStructFields<'ctx> {
Self::fields(ctx, llvm_usize)
}
/// Creates an LLVM type corresponding to the expected structure of an `NDArray`. /// Creates an LLVM type corresponding to the expected structure of an `NDArray`.
#[must_use] #[must_use]
fn llvm_type( fn llvm_type(ctx: &'ctx Context, llvm_usize: IntType<'ctx>) -> PointerType<'ctx> {
ctx: &'ctx Context, // struct NDArray { num_dims: size_t, dims: size_t*, data: i8* }
dtype: BasicTypeEnum<'ctx>,
llvm_usize: IntType<'ctx>,
) -> PointerType<'ctx> {
// struct NDArray { num_dims: size_t, dims: size_t*, data: T* }
// //
// * num_dims: Number of dimensions in the array
// * dims: Pointer to an array containing the size of each dimension
// * data : Pointer to an array containing the array data // * data : Pointer to an array containing the array data
let field_tys = [ // * itemsize: The size of each NDArray elements in bytes
llvm_usize.into(), // * ndims : Number of dimensions in the array
llvm_usize.ptr_type(AddressSpace::default()).into(), // * shape : Pointer to an array containing the shape of the NDArray
dtype.ptr_type(AddressSpace::default()).into(), // * strides : Pointer to an array indicating the number of bytes between each element at a dimension
]; let field_tys =
Self::fields(ctx, llvm_usize).into_iter().map(|field| field.1).collect_vec();
ctx.struct_type(&field_tys, false).ptr_type(AddressSpace::default()) ctx.struct_type(&field_tys, false).ptr_type(AddressSpace::default())
} }
/// Creates an instance of [`ListType`]. /// Creates an instance of [`NDArrayType`].
#[must_use] #[must_use]
pub fn new<G: CodeGenerator + ?Sized>( pub fn new<G: CodeGenerator + ?Sized>(
generator: &G, generator: &G,
@ -103,24 +142,21 @@ impl<'ctx> NDArrayType<'ctx> {
dtype: BasicTypeEnum<'ctx>, dtype: BasicTypeEnum<'ctx>,
) -> Self { ) -> Self {
let llvm_usize = generator.get_size_type(ctx); let llvm_usize = generator.get_size_type(ctx);
let llvm_ndarray = Self::llvm_type(ctx, dtype, llvm_usize); let llvm_ndarray = Self::llvm_type(ctx, llvm_usize);
NDArrayType::from_type(llvm_ndarray, llvm_usize) NDArrayType { ty: llvm_ndarray, dtype, llvm_usize }
} }
/// Creates an [`NDArrayType`] from a [`PointerType`]. /// Creates an [`NDArrayType`] from a [`PointerType`] representing an `NDArray`.
#[must_use] #[must_use]
pub fn from_type(ptr_ty: PointerType<'ctx>, llvm_usize: IntType<'ctx>) -> Self { pub fn from_type(
ptr_ty: PointerType<'ctx>,
dtype: BasicTypeEnum<'ctx>,
llvm_usize: IntType<'ctx>,
) -> Self {
debug_assert!(Self::is_representable(ptr_ty, llvm_usize).is_ok()); debug_assert!(Self::is_representable(ptr_ty, llvm_usize).is_ok());
NDArrayType { NDArrayType { ty: ptr_ty, dtype, llvm_usize }
ty: ptr_ty,
dtype: ptr_ty
.get_element_type()
.try_into()
.expect("Expected BasicTypeEnum for dtype of NDArray"),
llvm_usize,
}
} }
/// Returns the type of the `size` field of this `ndarray` type. /// Returns the type of the `size` field of this `ndarray` type.
@ -207,7 +243,7 @@ impl<'ctx> ProxyType<'ctx> for NDArrayType<'ctx> {
) -> Self::Value { ) -> Self::Value {
debug_assert_eq!(value.get_type(), self.as_base_type()); debug_assert_eq!(value.get_type(), self.as_base_type());
NDArrayValue::from_pointer_value(value, self.llvm_usize, name) NDArrayValue::from_pointer_value(value, self.dtype, self.llvm_usize, name)
} }
fn as_base_type(&self) -> Self::Base { fn as_base_type(&self) -> Self::Base {

View File

@ -1,7 +1,7 @@
use inkwell::{ use inkwell::{
types::{AnyTypeEnum, BasicTypeEnum, IntType}, types::{AnyType, AnyTypeEnum, BasicType, BasicTypeEnum, IntType},
values::{BasicValueEnum, IntValue, PointerValue}, values::{BasicValueEnum, IntValue, PointerValue},
IntPredicate, AddressSpace, IntPredicate,
}; };
use super::{ use super::{
@ -20,6 +20,7 @@ use crate::codegen::{
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct NDArrayValue<'ctx> { pub struct NDArrayValue<'ctx> {
value: PointerValue<'ctx>, value: PointerValue<'ctx>,
dtype: BasicTypeEnum<'ctx>,
llvm_usize: IntType<'ctx>, llvm_usize: IntType<'ctx>,
name: Option<&'ctx str>, name: Option<&'ctx str>,
} }
@ -38,28 +39,21 @@ impl<'ctx> NDArrayValue<'ctx> {
#[must_use] #[must_use]
pub fn from_pointer_value( pub fn from_pointer_value(
ptr: PointerValue<'ctx>, ptr: PointerValue<'ctx>,
dtype: BasicTypeEnum<'ctx>,
llvm_usize: IntType<'ctx>, llvm_usize: IntType<'ctx>,
name: Option<&'ctx str>, name: Option<&'ctx str>,
) -> Self { ) -> Self {
debug_assert!(Self::is_representable(ptr, llvm_usize).is_ok()); debug_assert!(Self::is_representable(ptr, llvm_usize).is_ok());
NDArrayValue { value: ptr, llvm_usize, name } NDArrayValue { value: ptr, dtype, llvm_usize, name }
} }
/// Returns the pointer to the field storing the number of dimensions of this `NDArray`. /// Returns the pointer to the field storing the number of dimensions of this `NDArray`.
fn ptr_to_ndims(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> { fn ptr_to_ndims(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
let llvm_i32 = ctx.ctx.i32_type(); self.get_type()
let var_name = self.name.map(|v| format!("{v}.ndims.addr")).unwrap_or_default(); .get_fields(ctx.ctx, self.llvm_usize)
.ndims
unsafe { .ptr_by_gep(ctx, self.value, self.name)
ctx.builder
.build_in_bounds_gep(
self.as_base_value(),
&[llvm_i32.const_zero(), llvm_i32.const_zero()],
var_name.as_str(),
)
.unwrap()
}
} }
/// Stores the number of dimensions `ndims` into this instance. /// Stores the number of dimensions `ndims` into this instance.
@ -81,63 +75,51 @@ impl<'ctx> NDArrayValue<'ctx> {
ctx.builder.build_load(pndims, "").map(BasicValueEnum::into_int_value).unwrap() ctx.builder.build_load(pndims, "").map(BasicValueEnum::into_int_value).unwrap()
} }
/// Returns the double-indirection pointer to the `dims` array, as if by calling `getelementptr` /// Returns the double-indirection pointer to the `shape` array, as if by calling
/// on the field. /// `getelementptr` on the field.
fn ptr_to_dims(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> { fn ptr_to_shape(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
let llvm_i32 = ctx.ctx.i32_type(); self.get_type()
let var_name = self.name.map(|v| format!("{v}.dims.addr")).unwrap_or_default(); .get_fields(ctx.ctx, self.llvm_usize)
.shape
unsafe { .ptr_by_gep(ctx, self.value, self.name)
ctx.builder
.build_in_bounds_gep(
self.as_base_value(),
&[llvm_i32.const_zero(), llvm_i32.const_int(1, true)],
var_name.as_str(),
)
.unwrap()
}
} }
/// Stores the array of dimension sizes `dims` into this instance. /// Stores the array of dimension sizes `dims` into this instance.
fn store_dim_sizes(&self, ctx: &CodeGenContext<'ctx, '_>, dims: PointerValue<'ctx>) { fn store_shape(&self, ctx: &CodeGenContext<'ctx, '_>, dims: PointerValue<'ctx>) {
ctx.builder.build_store(self.ptr_to_dims(ctx), dims).unwrap(); ctx.builder.build_store(self.ptr_to_shape(ctx), dims).unwrap();
} }
/// Convenience method for creating a new array storing dimension sizes with the given `size`. /// Convenience method for creating a new array storing dimension sizes with the given `size`.
pub fn create_dim_sizes( pub fn create_shape(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
llvm_usize: IntType<'ctx>, llvm_usize: IntType<'ctx>,
size: IntValue<'ctx>, size: IntValue<'ctx>,
) { ) {
self.store_dim_sizes(ctx, ctx.builder.build_array_alloca(llvm_usize, size, "").unwrap()); self.store_shape(ctx, ctx.builder.build_array_alloca(llvm_usize, size, "").unwrap());
} }
/// Returns a proxy object to the field storing the size of each dimension of this `NDArray`. /// Returns a proxy object to the field storing the size of each dimension of this `NDArray`.
#[must_use] #[must_use]
pub fn dim_sizes(&self) -> NDArrayDimsProxy<'ctx, '_> { pub fn shape(&self) -> NDArrayShapeProxy<'ctx, '_> {
NDArrayDimsProxy(self) NDArrayShapeProxy(self)
} }
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr` /// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
/// on the field. /// on the field.
pub fn ptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> { pub fn ptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
let llvm_i32 = ctx.ctx.i32_type(); self.get_type()
let var_name = self.name.map(|v| format!("{v}.data.addr")).unwrap_or_default(); .get_fields(ctx.ctx, self.llvm_usize)
.data
unsafe { .ptr_by_gep(ctx, self.value, self.name)
ctx.builder
.build_in_bounds_gep(
self.as_base_value(),
&[llvm_i32.const_zero(), llvm_i32.const_int(2, true)],
var_name.as_str(),
)
.unwrap()
}
} }
/// Stores the array of data elements `data` into this instance. /// Stores the array of data elements `data` into this instance.
fn store_data(&self, ctx: &CodeGenContext<'ctx, '_>, data: PointerValue<'ctx>) { fn store_data(&self, ctx: &CodeGenContext<'ctx, '_>, data: PointerValue<'ctx>) {
let data = ctx
.builder
.build_bit_cast(data, ctx.ctx.i8_type().ptr_type(AddressSpace::default()), "")
.unwrap();
ctx.builder.build_store(self.ptr_to_data(ctx), data).unwrap(); ctx.builder.build_store(self.ptr_to_data(ctx), data).unwrap();
} }
@ -149,7 +131,15 @@ impl<'ctx> NDArrayValue<'ctx> {
elem_ty: BasicTypeEnum<'ctx>, elem_ty: BasicTypeEnum<'ctx>,
size: IntValue<'ctx>, size: IntValue<'ctx>,
) { ) {
self.store_data(ctx, ctx.builder.build_array_alloca(elem_ty, size, "").unwrap()); let itemsize =
ctx.builder.build_int_cast(elem_ty.size_of().unwrap(), size.get_type(), "").unwrap();
let nbytes = ctx.builder.build_int_mul(size, itemsize, "").unwrap();
// TODO: What about alignment?
self.store_data(
ctx,
ctx.builder.build_array_alloca(ctx.ctx.i8_type(), nbytes, "").unwrap(),
);
} }
/// Returns a proxy object to the field storing the data of this `NDArray`. /// Returns a proxy object to the field storing the data of this `NDArray`.
@ -164,7 +154,7 @@ impl<'ctx> ProxyValue<'ctx> for NDArrayValue<'ctx> {
type Type = NDArrayType<'ctx>; type Type = NDArrayType<'ctx>;
fn get_type(&self) -> Self::Type { fn get_type(&self) -> Self::Type {
NDArrayType::from_type(self.as_base_value().get_type(), self.llvm_usize) NDArrayType::from_type(self.as_base_value().get_type(), self.dtype, self.llvm_usize)
} }
fn as_base_value(&self) -> Self::Base { fn as_base_value(&self) -> Self::Base {
@ -180,15 +170,15 @@ impl<'ctx> From<NDArrayValue<'ctx>> for PointerValue<'ctx> {
/// Proxy type for accessing the `dims` array of an `NDArray` instance in LLVM. /// Proxy type for accessing the `dims` array of an `NDArray` instance in LLVM.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct NDArrayDimsProxy<'ctx, 'a>(&'a NDArrayValue<'ctx>); pub struct NDArrayShapeProxy<'ctx, 'a>(&'a NDArrayValue<'ctx>);
impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDimsProxy<'ctx, '_> { impl<'ctx> ArrayLikeValue<'ctx> for NDArrayShapeProxy<'ctx, '_> {
fn element_type<G: CodeGenerator + ?Sized>( fn element_type<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
generator: &G, generator: &G,
) -> AnyTypeEnum<'ctx> { ) -> AnyTypeEnum<'ctx> {
self.0.dim_sizes().base_ptr(ctx, generator).get_type().get_element_type() self.0.shape().base_ptr(ctx, generator).get_type().get_element_type()
} }
fn base_ptr<G: CodeGenerator + ?Sized>( fn base_ptr<G: CodeGenerator + ?Sized>(
@ -199,7 +189,7 @@ impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDimsProxy<'ctx, '_> {
let var_name = self.0.name.map(|v| format!("{v}.data")).unwrap_or_default(); let var_name = self.0.name.map(|v| format!("{v}.data")).unwrap_or_default();
ctx.builder ctx.builder
.build_load(self.0.ptr_to_dims(ctx), var_name.as_str()) .build_load(self.0.ptr_to_shape(ctx), var_name.as_str())
.map(BasicValueEnum::into_pointer_value) .map(BasicValueEnum::into_pointer_value)
.unwrap() .unwrap()
} }
@ -213,7 +203,7 @@ impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDimsProxy<'ctx, '_> {
} }
} }
impl<'ctx> ArrayLikeIndexer<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> { impl<'ctx> ArrayLikeIndexer<'ctx, IntValue<'ctx>> for NDArrayShapeProxy<'ctx, '_> {
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>( unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
@ -252,10 +242,10 @@ impl<'ctx> ArrayLikeIndexer<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_>
} }
} }
impl<'ctx> UntypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {} impl<'ctx> UntypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayShapeProxy<'ctx, '_> {}
impl<'ctx> UntypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {} impl<'ctx> UntypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayShapeProxy<'ctx, '_> {}
impl<'ctx> TypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> { impl<'ctx> TypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayShapeProxy<'ctx, '_> {
fn downcast_to_type( fn downcast_to_type(
&self, &self,
_: &mut CodeGenContext<'ctx, '_>, _: &mut CodeGenContext<'ctx, '_>,
@ -265,7 +255,7 @@ impl<'ctx> TypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ct
} }
} }
impl<'ctx> TypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> { impl<'ctx> TypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayShapeProxy<'ctx, '_> {
fn upcast_from_type( fn upcast_from_type(
&self, &self,
_: &mut CodeGenContext<'ctx, '_>, _: &mut CodeGenContext<'ctx, '_>,
@ -282,10 +272,10 @@ pub struct NDArrayDataProxy<'ctx, 'a>(&'a NDArrayValue<'ctx>);
impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDataProxy<'ctx, '_> { impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDataProxy<'ctx, '_> {
fn element_type<G: CodeGenerator + ?Sized>( fn element_type<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, _: &CodeGenContext<'ctx, '_>,
generator: &G, _: &G,
) -> AnyTypeEnum<'ctx> { ) -> AnyTypeEnum<'ctx> {
self.0.data().base_ptr(ctx, generator).get_type().get_element_type() self.0.dtype.as_any_type_enum()
} }
fn base_ptr<G: CodeGenerator + ?Sized>( fn base_ptr<G: CodeGenerator + ?Sized>(
@ -318,15 +308,37 @@ impl<'ctx> ArrayLikeIndexer<'ctx> for NDArrayDataProxy<'ctx, '_> {
idx: &IntValue<'ctx>, idx: &IntValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
unsafe { let sizeof_elem = ctx
.builder
.build_int_truncate_or_bit_cast(
self.element_type(ctx, generator).size_of().unwrap(),
idx.get_type(),
"",
)
.unwrap();
let idx = ctx.builder.build_int_mul(*idx, sizeof_elem, "").unwrap();
let ptr = unsafe {
ctx.builder ctx.builder
.build_in_bounds_gep( .build_in_bounds_gep(
self.base_ptr(ctx, generator), self.base_ptr(ctx, generator),
&[*idx], &[idx],
name.unwrap_or_default(), name.unwrap_or_default(),
) )
.unwrap() .unwrap()
} };
// Current implementation is transparent - The returned pointer type is
// already cast into the expected type, allowing for immediately
// load/store.
ctx.builder
.build_pointer_cast(
ptr,
BasicTypeEnum::try_from(self.element_type(ctx, generator))
.unwrap()
.ptr_type(AddressSpace::default()),
"",
)
.unwrap()
} }
fn ptr_offset<G: CodeGenerator + ?Sized>( fn ptr_offset<G: CodeGenerator + ?Sized>(
@ -347,7 +359,20 @@ impl<'ctx> ArrayLikeIndexer<'ctx> for NDArrayDataProxy<'ctx, '_> {
ctx.current_loc, ctx.current_loc,
); );
unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) } let ptr = unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) };
// Current implementation is transparent - The returned pointer type is
// already cast into the expected type, allowing for immediately
// load/store.
ctx.builder
.build_pointer_cast(
ptr,
BasicTypeEnum::try_from(self.element_type(ctx, generator))
.unwrap()
.ptr_type(AddressSpace::default()),
"",
)
.unwrap()
} }
} }
@ -381,8 +406,17 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
); );
let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices); let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices);
let sizeof_elem = ctx
.builder
.build_int_truncate_or_bit_cast(
self.element_type(ctx, generator).size_of().unwrap(),
index.get_type(),
"",
)
.unwrap();
let index = ctx.builder.build_int_mul(index, sizeof_elem, "").unwrap();
unsafe { let ptr = unsafe {
ctx.builder ctx.builder
.build_in_bounds_gep( .build_in_bounds_gep(
self.base_ptr(ctx, generator), self.base_ptr(ctx, generator),
@ -390,7 +424,17 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
name.unwrap_or_default(), name.unwrap_or_default(),
) )
.unwrap() .unwrap()
} };
// TODO: Current implementation is transparent
ctx.builder
.build_pointer_cast(
ptr,
BasicTypeEnum::try_from(self.element_type(ctx, generator))
.unwrap()
.ptr_type(AddressSpace::default()),
"",
)
.unwrap()
} }
fn ptr_offset<G: CodeGenerator + ?Sized>( fn ptr_offset<G: CodeGenerator + ?Sized>(
@ -429,7 +473,7 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
let (dim_idx, dim_sz) = unsafe { let (dim_idx, dim_sz) = unsafe {
( (
indices.get_unchecked(ctx, generator, &i, None).into_int_value(), indices.get_unchecked(ctx, generator, &i, None).into_int_value(),
self.0.dim_sizes().get_typed_unchecked(ctx, generator, &i, None), self.0.shape().get_typed_unchecked(ctx, generator, &i, None),
) )
}; };
let dim_idx = ctx let dim_idx = ctx
@ -455,7 +499,17 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
) )
.unwrap(); .unwrap();
unsafe { self.ptr_offset_unchecked(ctx, generator, indices, name) } let ptr = unsafe { self.ptr_offset_unchecked(ctx, generator, indices, name) };
// TODO: Current implementation is transparent
ctx.builder
.build_pointer_cast(
ptr,
BasicTypeEnum::try_from(self.element_type(ctx, generator))
.unwrap()
.ptr_type(AddressSpace::default()),
"",
)
.unwrap()
} }
} }

View File

@ -144,6 +144,7 @@ def test_ndarray_array():
# Copy # Copy
n2_cpy: ndarray[float, 2] = np_array(n2, copy=False) n2_cpy: ndarray[float, 2] = np_array(n2, copy=False)
output_ndarray_float_2(n2_cpy)
n2_cpy.fill(0.0) n2_cpy.fill(0.0)
output_ndarray_float_2(n2_cpy) output_ndarray_float_2(n2_cpy)