core: comment out numpy

This commit is contained in:
lyken 2024-07-08 10:19:29 +08:00
parent 0300c81bb7
commit 851cef57aa
7 changed files with 3373 additions and 3318 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
use crate::codegen::{ use crate::codegen::{
irrt::{call_ndarray_calc_size, call_ndarray_flatten_index}, // irrt::{call_ndarray_calc_size, call_ndarray_flatten_index},
llvm_intrinsics::call_int_umin, llvm_intrinsics::call_int_umin,
stmt::gen_for_callback_incrementing, stmt::gen_for_callback_incrementing,
CodeGenContext, CodeGenerator, CodeGenContext, CodeGenerator,
@ -1601,7 +1601,8 @@ impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDataProxy<'ctx, '_> {
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
generator: &G, generator: &G,
) -> IntValue<'ctx> { ) -> IntValue<'ctx> {
call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator), (None, None)) todo!()
// call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator), (None, None))
} }
} }
@ -1659,33 +1660,34 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
indices: &Index, indices: &Index,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
let llvm_usize = generator.get_size_type(ctx.ctx); todo!()
// let llvm_usize = generator.get_size_type(ctx.ctx);
let indices_elem_ty = indices // let indices_elem_ty = indices
.ptr_offset(ctx, generator, &llvm_usize.const_zero(), None) // .ptr_offset(ctx, generator, &llvm_usize.const_zero(), None)
.get_type() // .get_type()
.get_element_type(); // .get_element_type();
let Ok(indices_elem_ty) = IntType::try_from(indices_elem_ty) else { // let Ok(indices_elem_ty) = IntType::try_from(indices_elem_ty) else {
panic!("Expected list[int32] but got {indices_elem_ty}") // panic!("Expected list[int32] but got {indices_elem_ty}")
}; // };
assert_eq!( // assert_eq!(
indices_elem_ty.get_bit_width(), // indices_elem_ty.get_bit_width(),
32, // 32,
"Expected list[int32] but got list[int{}]", // "Expected list[int32] but got list[int{}]",
indices_elem_ty.get_bit_width() // indices_elem_ty.get_bit_width()
); // );
let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices); // let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices);
unsafe { // unsafe {
ctx.builder // ctx.builder
.build_in_bounds_gep( // .build_in_bounds_gep(
self.base_ptr(ctx, generator), // self.base_ptr(ctx, generator),
&[index], // &[index],
name.unwrap_or_default(), // name.unwrap_or_default(),
) // )
.unwrap() // .unwrap()
} // }
} }
fn ptr_offset<G: CodeGenerator + ?Sized>( fn ptr_offset<G: CodeGenerator + ?Sized>(

View File

@ -1362,100 +1362,101 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
} else if ty1.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) } else if ty1.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|| ty2.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) || ty2.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
{ {
let llvm_usize = generator.get_size_type(ctx.ctx); todo!()
// let llvm_usize = generator.get_size_type(ctx.ctx);
let is_ndarray1 = ty1.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()); // let is_ndarray1 = ty1.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id());
let is_ndarray2 = ty2.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()); // let is_ndarray2 = ty2.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id());
if is_ndarray1 && is_ndarray2 { // if is_ndarray1 && is_ndarray2 {
let (ndarray_dtype1, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty1); // let (ndarray_dtype1, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty1);
let (ndarray_dtype2, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty2); // let (ndarray_dtype2, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty2);
assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2)); // assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2));
let left_val = // let left_val =
NDArrayValue::from_ptr_val(left_val.into_pointer_value(), llvm_usize, None); // NDArrayValue::from_ptr_val(left_val.into_pointer_value(), llvm_usize, None);
let right_val = // let right_val =
NDArrayValue::from_ptr_val(right_val.into_pointer_value(), llvm_usize, None); // NDArrayValue::from_ptr_val(right_val.into_pointer_value(), 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
numpy::ndarray_matmul_2d( // numpy::ndarray_matmul_2d(
generator, // generator,
ctx, // ctx,
ndarray_dtype1, // ndarray_dtype1,
match op.variant { // match op.variant {
BinopVariant::Normal => None, // BinopVariant::Normal => None,
BinopVariant::AugAssign => Some(left_val), // BinopVariant::AugAssign => Some(left_val),
}, // },
left_val, // left_val,
right_val, // right_val,
)? // )?
} else { // } else {
numpy::ndarray_elementwise_binop_impl( // numpy::ndarray_elementwise_binop_impl(
generator, // generator,
ctx, // ctx,
ndarray_dtype1, // ndarray_dtype1,
match op.variant { // match op.variant {
BinopVariant::Normal => None, // BinopVariant::Normal => None,
BinopVariant::AugAssign => Some(left_val), // BinopVariant::AugAssign => Some(left_val),
}, // },
(left_val.as_base_value().into(), false), // (left_val.as_base_value().into(), false),
(right_val.as_base_value().into(), false), // (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,
ctx, // ctx,
(&Some(ndarray_dtype1), lhs), // (&Some(ndarray_dtype1), lhs),
op, // op,
(&Some(ndarray_dtype2), rhs), // (&Some(ndarray_dtype2), rhs),
ctx.current_loc, // ctx.current_loc,
)? // )?
.unwrap() // .unwrap()
.to_basic_value_enum( // .to_basic_value_enum(
ctx, // ctx,
generator, // generator,
ndarray_dtype1, // ndarray_dtype1,
) // )
}, // },
)? // )?
}; // };
Ok(Some(res.as_base_value().into())) // Ok(Some(res.as_base_value().into()))
} 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 ndarray_val = NDArrayValue::from_ptr_val( // let ndarray_val = NDArrayValue::from_ptr_val(
if is_ndarray1 { left_val } else { right_val }.into_pointer_value(), // if is_ndarray1 { left_val } else { right_val }.into_pointer_value(),
llvm_usize, // llvm_usize,
None, // None,
); // );
let res = numpy::ndarray_elementwise_binop_impl( // let res = numpy::ndarray_elementwise_binop_impl(
generator, // generator,
ctx, // ctx,
ndarray_dtype, // ndarray_dtype,
match op.variant { // match op.variant {
BinopVariant::Normal => None, // BinopVariant::Normal => None,
BinopVariant::AugAssign => Some(ndarray_val), // BinopVariant::AugAssign => Some(ndarray_val),
}, // },
(left_val, !is_ndarray1), // (left_val, !is_ndarray1),
(right_val, !is_ndarray2), // (right_val, !is_ndarray2),
|generator, ctx, (lhs, rhs)| { // |generator, ctx, (lhs, rhs)| {
gen_binop_expr_with_values( // gen_binop_expr_with_values(
generator, // generator,
ctx, // ctx,
(&Some(ndarray_dtype), lhs), // (&Some(ndarray_dtype), lhs),
op, // op,
(&Some(ndarray_dtype), rhs), // (&Some(ndarray_dtype), rhs),
ctx.current_loc, // ctx.current_loc,
)? // )?
.unwrap() // .unwrap()
.to_basic_value_enum(ctx, generator, ndarray_dtype) // .to_basic_value_enum(ctx, generator, ndarray_dtype)
}, // },
)?; // )?;
Ok(Some(res.as_base_value().into())) // Ok(Some(res.as_base_value().into()))
} // }
} else { } else {
let left_ty_enum = ctx.unifier.get_ty_immutable(left_ty.unwrap()); let left_ty_enum = ctx.unifier.get_ty_immutable(left_ty.unwrap());
let TypeEnum::TObj { fields, obj_id, .. } = left_ty_enum.as_ref() else { let TypeEnum::TObj { fields, obj_id, .. } = left_ty_enum.as_ref() else {
@ -1612,40 +1613,41 @@ pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>(
_ => val.into(), _ => val.into(),
} }
} 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); todo!()
let (ndarray_dtype, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty); // let llvm_usize = generator.get_size_type(ctx.ctx);
// let (ndarray_dtype, _) = unpack_ndarray_var_tys(&mut ctx.unifier, ty);
let val = NDArrayValue::from_ptr_val(val.into_pointer_value(), llvm_usize, None); // let val = NDArrayValue::from_ptr_val(val.into_pointer_value(), 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
let op = if ndarray_dtype.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::Bool.id()) { // let op = if ndarray_dtype.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::Bool.id()) {
if op == ast::Unaryop::Invert { // if op == ast::Unaryop::Invert {
ast::Unaryop::Not // ast::Unaryop::Not
} else { // } else {
unreachable!( // unreachable!(
"ufunc {} not supported for ndarray[bool, N]", // "ufunc {} not supported for ndarray[bool, N]",
op.op_info().method_name, // op.op_info().method_name,
) // )
} // }
} else { // } else {
op // op
}; // };
let res = numpy::ndarray_elementwise_unaryop_impl( // let res = numpy::ndarray_elementwise_unaryop_impl(
generator, // generator,
ctx, // ctx,
ndarray_dtype, // ndarray_dtype,
None, // None,
val, // val,
|generator, ctx, val| { // |generator, ctx, val| {
gen_unaryop_expr_with_values(generator, ctx, op, (&Some(ndarray_dtype), val))? // gen_unaryop_expr_with_values(generator, ctx, op, (&Some(ndarray_dtype), val))?
.unwrap() // .unwrap()
.to_basic_value_enum(ctx, generator, ndarray_dtype) // .to_basic_value_enum(ctx, generator, ndarray_dtype)
}, // },
)?; // )?;
res.as_base_value().into() // res.as_base_value().into()
} else { } else {
unimplemented!() unimplemented!()
})) }))
@ -1688,85 +1690,86 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
if left_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) if left_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
|| right_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) || right_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id())
{ {
let llvm_usize = generator.get_size_type(ctx.ctx); todo!()
// let llvm_usize = generator.get_size_type(ctx.ctx);
let (Some(left_ty), lhs) = left else { unreachable!() }; // let (Some(left_ty), lhs) = left else { unreachable!() };
let (Some(right_ty), rhs) = comparators[0] else { unreachable!() }; // let (Some(right_ty), rhs) = comparators[0] else { unreachable!() };
let op = ops[0]; // let op = ops[0];
let is_ndarray1 = // let is_ndarray1 =
left_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()); // left_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id());
let is_ndarray2 = // let is_ndarray2 =
right_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()); // right_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id());
return if is_ndarray1 && is_ndarray2 { // return if is_ndarray1 && is_ndarray2 {
let (ndarray_dtype1, _) = unpack_ndarray_var_tys(&mut ctx.unifier, left_ty); // let (ndarray_dtype1, _) = unpack_ndarray_var_tys(&mut ctx.unifier, left_ty);
let (ndarray_dtype2, _) = unpack_ndarray_var_tys(&mut ctx.unifier, right_ty); // let (ndarray_dtype2, _) = unpack_ndarray_var_tys(&mut ctx.unifier, right_ty);
assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2)); // assert!(ctx.unifier.unioned(ndarray_dtype1, ndarray_dtype2));
let left_val = // let left_val =
NDArrayValue::from_ptr_val(lhs.into_pointer_value(), llvm_usize, None); // NDArrayValue::from_ptr_val(lhs.into_pointer_value(), 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_val.as_base_value().into(), false),
(rhs, false), // (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,
ctx, // ctx,
(Some(ndarray_dtype1), lhs), // (Some(ndarray_dtype1), lhs),
&[op], // &[op],
&[(Some(ndarray_dtype2), rhs)], // &[(Some(ndarray_dtype2), rhs)],
)? // )?
.unwrap() // .unwrap()
.to_basic_value_enum( // .to_basic_value_enum(
ctx, // ctx,
generator, // generator,
ctx.primitives.bool, // ctx.primitives.bool,
)?; // )?;
Ok(generator.bool_to_i8(ctx, val.into_int_value()).into()) // Ok(generator.bool_to_i8(ctx, val.into_int_value()).into())
}, // },
)?; // )?;
Ok(Some(res.as_base_value().into())) // Ok(Some(res.as_base_value().into()))
} else { // } else {
let (ndarray_dtype, _) = unpack_ndarray_var_tys( // let (ndarray_dtype, _) = unpack_ndarray_var_tys(
&mut ctx.unifier, // &mut ctx.unifier,
if is_ndarray1 { left_ty } else { right_ty }, // if is_ndarray1 { left_ty } else { right_ty },
); // );
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,
(lhs, !is_ndarray1), // (lhs, !is_ndarray1),
(rhs, !is_ndarray2), // (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,
ctx, // ctx,
(Some(ndarray_dtype), lhs), // (Some(ndarray_dtype), lhs),
&[op], // &[op],
&[(Some(ndarray_dtype), rhs)], // &[(Some(ndarray_dtype), rhs)],
)? // )?
.unwrap() // .unwrap()
.to_basic_value_enum( // .to_basic_value_enum(
ctx, // ctx,
generator, // generator,
ctx.primitives.bool, // ctx.primitives.bool,
)?; // )?;
Ok(generator.bool_to_i8(ctx, val.into_int_value()).into()) // Ok(generator.bool_to_i8(ctx, val.into_int_value()).into())
}, // },
)?; // )?;
Ok(Some(res.as_base_value().into())) // Ok(Some(res.as_base_value().into()))
}; // };
} }
} }
@ -2102,310 +2105,312 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
v: NDArrayValue<'ctx>, v: NDArrayValue<'ctx>,
slice: &Expr<Option<Type>>, slice: &Expr<Option<Type>>,
) -> Result<Option<ValueEnum<'ctx>>, String> { ) -> Result<Option<ValueEnum<'ctx>>, String> {
let llvm_i1 = ctx.ctx.bool_type(); todo!()
let llvm_i32 = ctx.ctx.i32_type();
let llvm_usize = generator.get_size_type(ctx.ctx);
let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) else { // let llvm_i1 = ctx.ctx.bool_type();
unreachable!() // let llvm_i32 = ctx.ctx.i32_type();
}; // let llvm_usize = generator.get_size_type(ctx.ctx);
let ndims = values // let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) else {
.iter() // unreachable!()
.map(|ndim| u64::try_from(ndim.clone()).map_err(|()| ndim.clone())) // };
.collect::<Result<Vec<_>, _>>()
.map_err(|val| {
format!(
"Expected non-negative literal for ndarray.ndims, got {}",
i128::try_from(val).unwrap()
)
})?;
assert!(!ndims.is_empty()); // let ndims = values
// .iter()
// .map(|ndim| u64::try_from(ndim.clone()).map_err(|()| ndim.clone()))
// .collect::<Result<Vec<_>, _>>()
// .map_err(|val| {
// format!(
// "Expected non-negative literal for ndarray.ndims, got {}",
// i128::try_from(val).unwrap()
// )
// })?;
// The number of dimensions subscripted by the index expression. // assert!(!ndims.is_empty());
// Slicing a ndarray will yield the same number of dimensions, whereas indexing into a
// dimension will remove a dimension.
let subscripted_dims = match &slice.node {
ExprKind::Tuple { elts, .. } => elts.iter().fold(0, |acc, value_subexpr| {
if let ExprKind::Slice { .. } = &value_subexpr.node {
acc
} else {
acc + 1
}
}),
ExprKind::Slice { .. } => 0, // // The number of dimensions subscripted by the index expression.
_ => 1, // // Slicing a ndarray will yield the same number of dimensions, whereas indexing into a
}; // // dimension will remove a dimension.
// let subscripted_dims = match &slice.node {
// ExprKind::Tuple { elts, .. } => elts.iter().fold(0, |acc, value_subexpr| {
// if let ExprKind::Slice { .. } = &value_subexpr.node {
// acc
// } else {
// acc + 1
// }
// }),
let ndarray_ndims_ty = ctx.unifier.get_fresh_literal( // ExprKind::Slice { .. } => 0,
ndims.iter().map(|v| SymbolValue::U64(v - subscripted_dims)).collect(), // _ => 1,
None, // };
);
let ndarray_ty =
make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(ty), Some(ndarray_ndims_ty));
let llvm_pndarray_t = ctx.get_llvm_type(generator, ndarray_ty).into_pointer_type();
let llvm_ndarray_t = llvm_pndarray_t.get_element_type().into_struct_type();
let llvm_ndarray_data_t = ctx.get_llvm_type(generator, ty).as_basic_type_enum();
// Check that len is non-zero // let ndarray_ndims_ty = ctx.unifier.get_fresh_literal(
let len = v.load_ndims(ctx); // ndims.iter().map(|v| SymbolValue::U64(v - subscripted_dims)).collect(),
ctx.make_assert( // None,
generator, // );
ctx.builder.build_int_compare(IntPredicate::SGT, len, llvm_usize.const_zero(), "").unwrap(), // let ndarray_ty =
"0:IndexError", // make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(ty), Some(ndarray_ndims_ty));
"too many indices for array: array is {0}-dimensional but 1 were indexed", // let llvm_pndarray_t = ctx.get_llvm_type(generator, ndarray_ty).into_pointer_type();
[Some(len), None, None], // let llvm_ndarray_t = llvm_pndarray_t.get_element_type().into_struct_type();
slice.location, // let llvm_ndarray_data_t = ctx.get_llvm_type(generator, ty).as_basic_type_enum();
);
// Normalizes a possibly-negative index to its corresponding positive index // // Check that len is non-zero
let normalize_index = |generator: &mut G, // let len = v.load_ndims(ctx);
ctx: &mut CodeGenContext<'ctx, '_>, // ctx.make_assert(
index: IntValue<'ctx>, // generator,
dim: u64| { // ctx.builder.build_int_compare(IntPredicate::SGT, len, llvm_usize.const_zero(), "").unwrap(),
gen_if_else_expr_callback( // "0:IndexError",
generator, // "too many indices for array: array is {0}-dimensional but 1 were indexed",
ctx, // [Some(len), None, None],
|_, ctx| { // slice.location,
Ok(ctx // );
.builder
.build_int_compare(IntPredicate::SGE, index, index.get_type().const_zero(), "")
.unwrap())
},
|_, _| Ok(Some(index)),
|generator, ctx| {
let llvm_i32 = ctx.ctx.i32_type();
let len = unsafe { // // Normalizes a possibly-negative index to its corresponding positive index
v.dim_sizes().get_typed_unchecked( // let normalize_index = |generator: &mut G,
ctx, // ctx: &mut CodeGenContext<'ctx, '_>,
generator, // index: IntValue<'ctx>,
&llvm_usize.const_int(dim, true), // dim: u64| {
None, // gen_if_else_expr_callback(
) // generator,
}; // ctx,
// |_, ctx| {
// Ok(ctx
// .builder
// .build_int_compare(IntPredicate::SGE, index, index.get_type().const_zero(), "")
// .unwrap())
// },
// |_, _| Ok(Some(index)),
// |generator, ctx| {
// let llvm_i32 = ctx.ctx.i32_type();
let index = ctx // let len = unsafe {
.builder // v.dim_sizes().get_typed_unchecked(
.build_int_add( // ctx,
len, // generator,
ctx.builder.build_int_s_extend(index, llvm_usize, "").unwrap(), // &llvm_usize.const_int(dim, true),
"", // None,
) // )
.unwrap(); // };
Ok(Some(ctx.builder.build_int_truncate(index, llvm_i32, "").unwrap())) // let index = ctx
}, // .builder
) // .build_int_add(
.map(|v| v.map(BasicValueEnum::into_int_value)) // len,
}; // ctx.builder.build_int_s_extend(index, llvm_usize, "").unwrap(),
// "",
// )
// .unwrap();
// Converts a slice expression into a slice-range tuple // Ok(Some(ctx.builder.build_int_truncate(index, llvm_i32, "").unwrap()))
let expr_to_slice = |generator: &mut G, // },
ctx: &mut CodeGenContext<'ctx, '_>, // )
node: &ExprKind<Option<Type>>, // .map(|v| v.map(BasicValueEnum::into_int_value))
dim: u64| { // };
match node {
ExprKind::Constant { value: Constant::Int(v), .. } => {
let Some(index) =
normalize_index(generator, ctx, llvm_i32.const_int(*v as u64, true), dim)?
else {
return Ok(None);
};
Ok(Some((index, index, llvm_i32.const_int(1, true)))) // // Converts a slice expression into a slice-range tuple
} // let expr_to_slice = |generator: &mut G,
// ctx: &mut CodeGenContext<'ctx, '_>,
// node: &ExprKind<Option<Type>>,
// dim: u64| {
// match node {
// ExprKind::Constant { value: Constant::Int(v), .. } => {
// let Some(index) =
// normalize_index(generator, ctx, llvm_i32.const_int(*v as u64, true), dim)?
// else {
// return Ok(None);
// };
ExprKind::Slice { lower, upper, step } => { // Ok(Some((index, index, llvm_i32.const_int(1, true))))
let dim_sz = unsafe { // }
v.dim_sizes().get_typed_unchecked(
ctx,
generator,
&llvm_usize.const_int(dim, false),
None,
)
};
handle_slice_indices(lower, upper, step, ctx, generator, dim_sz) // ExprKind::Slice { lower, upper, step } => {
} // let dim_sz = unsafe {
// v.dim_sizes().get_typed_unchecked(
// ctx,
// generator,
// &llvm_usize.const_int(dim, false),
// None,
// )
// };
_ => { // handle_slice_indices(lower, upper, step, ctx, generator, dim_sz)
let Some(index) = generator.gen_expr(ctx, slice)? else { return Ok(None) }; // }
let index = index
.to_basic_value_enum(ctx, generator, slice.custom.unwrap())?
.into_int_value();
let Some(index) = normalize_index(generator, ctx, index, dim)? else {
return Ok(None);
};
Ok(Some((index, index, llvm_i32.const_int(1, true)))) // _ => {
} // let Some(index) = generator.gen_expr(ctx, slice)? else { return Ok(None) };
} // let index = index
}; // .to_basic_value_enum(ctx, generator, slice.custom.unwrap())?
// .into_int_value();
// let Some(index) = normalize_index(generator, ctx, index, dim)? else {
// return Ok(None);
// };
let make_indices_arr = |generator: &mut G, // Ok(Some((index, index, llvm_i32.const_int(1, true))))
ctx: &mut CodeGenContext<'ctx, '_>| // }
-> Result<_, String> { // }
Ok(if let ExprKind::Tuple { elts, .. } = &slice.node { // };
let llvm_int_ty = ctx.get_llvm_type(generator, elts[0].custom.unwrap());
let index_addr = generator.gen_array_var_alloc(
ctx,
llvm_int_ty,
llvm_usize.const_int(elts.len() as u64, false),
None,
)?;
for (i, elt) in elts.iter().enumerate() { // let make_indices_arr = |generator: &mut G,
let Some(index) = generator.gen_expr(ctx, elt)? else { // ctx: &mut CodeGenContext<'ctx, '_>|
return Ok(None); // -> Result<_, String> {
}; // Ok(if let ExprKind::Tuple { elts, .. } = &slice.node {
// let llvm_int_ty = ctx.get_llvm_type(generator, elts[0].custom.unwrap());
// let index_addr = generator.gen_array_var_alloc(
// ctx,
// llvm_int_ty,
// llvm_usize.const_int(elts.len() as u64, false),
// None,
// )?;
let index = index // for (i, elt) in elts.iter().enumerate() {
.to_basic_value_enum(ctx, generator, elt.custom.unwrap())? // let Some(index) = generator.gen_expr(ctx, elt)? else {
.into_int_value(); // return Ok(None);
let Some(index) = normalize_index(generator, ctx, index, 0)? else { // };
return Ok(None);
};
let store_ptr = unsafe { // let index = index
index_addr.ptr_offset_unchecked( // .to_basic_value_enum(ctx, generator, elt.custom.unwrap())?
ctx, // .into_int_value();
generator, // let Some(index) = normalize_index(generator, ctx, index, 0)? else {
&llvm_usize.const_int(i as u64, false), // return Ok(None);
None, // };
)
};
ctx.builder.build_store(store_ptr, index).unwrap();
}
Some(index_addr) // let store_ptr = unsafe {
} else if let Some(index) = generator.gen_expr(ctx, slice)? { // index_addr.ptr_offset_unchecked(
let llvm_int_ty = ctx.get_llvm_type(generator, slice.custom.unwrap()); // ctx,
let index_addr = generator.gen_array_var_alloc( // generator,
ctx, // &llvm_usize.const_int(i as u64, false),
llvm_int_ty, // None,
llvm_usize.const_int(1u64, false), // )
None, // };
)?; // ctx.builder.build_store(store_ptr, index).unwrap();
// }
let index = // Some(index_addr)
index.to_basic_value_enum(ctx, generator, slice.custom.unwrap())?.into_int_value(); // } else if let Some(index) = generator.gen_expr(ctx, slice)? {
let Some(index) = normalize_index(generator, ctx, index, 0)? else { return Ok(None) }; // let llvm_int_ty = ctx.get_llvm_type(generator, slice.custom.unwrap());
// let index_addr = generator.gen_array_var_alloc(
// ctx,
// llvm_int_ty,
// llvm_usize.const_int(1u64, false),
// None,
// )?;
let store_ptr = unsafe { // let index =
index_addr.ptr_offset_unchecked(ctx, generator, &llvm_usize.const_zero(), None) // index.to_basic_value_enum(ctx, generator, slice.custom.unwrap())?.into_int_value();
}; // let Some(index) = normalize_index(generator, ctx, index, 0)? else { return Ok(None) };
ctx.builder.build_store(store_ptr, index).unwrap();
Some(index_addr) // let store_ptr = unsafe {
} else { // index_addr.ptr_offset_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
None // };
}) // ctx.builder.build_store(store_ptr, index).unwrap();
};
Ok(Some(if ndims.len() == 1 && ndims[0] - subscripted_dims == 0 { // Some(index_addr)
let Some(index_addr) = make_indices_arr(generator, ctx)? else { return Ok(None) }; // } else {
// None
// })
// };
v.data().get(ctx, generator, &index_addr, None).into() // Ok(Some(if ndims.len() == 1 && ndims[0] - subscripted_dims == 0 {
} else { // let Some(index_addr) = make_indices_arr(generator, ctx)? else { return Ok(None) };
match &slice.node {
ExprKind::Tuple { elts, .. } => {
let slices = elts
.iter()
.enumerate()
.map(|(dim, elt)| expr_to_slice(generator, ctx, &elt.node, dim as u64))
.take_while_inclusive(|slice| slice.as_ref().is_ok_and(Option::is_some))
.collect::<Result<Vec<_>, _>>()?;
if slices.len() < elts.len() {
return Ok(None);
}
let slices = slices.into_iter().map(Option::unwrap).collect_vec(); // v.data().get(ctx, generator, &index_addr, None).into()
// } else {
// match &slice.node {
// ExprKind::Tuple { elts, .. } => {
// let slices = elts
// .iter()
// .enumerate()
// .map(|(dim, elt)| expr_to_slice(generator, ctx, &elt.node, dim as u64))
// .take_while_inclusive(|slice| slice.as_ref().is_ok_and(Option::is_some))
// .collect::<Result<Vec<_>, _>>()?;
// if slices.len() < elts.len() {
// return Ok(None);
// }
numpy::ndarray_sliced_copy(generator, ctx, ty, v, &slices)?.as_base_value().into() // let slices = slices.into_iter().map(Option::unwrap).collect_vec();
}
ExprKind::Slice { .. } => { // numpy::ndarray_sliced_copy(generator, ctx, ty, v, &slices)?.as_base_value().into()
let Some(slice) = expr_to_slice(generator, ctx, &slice.node, 0)? else { // }
return Ok(None);
};
numpy::ndarray_sliced_copy(generator, ctx, ty, v, &[slice])?.as_base_value().into() // ExprKind::Slice { .. } => {
} // let Some(slice) = expr_to_slice(generator, ctx, &slice.node, 0)? else {
// return Ok(None);
// };
_ => { // numpy::ndarray_sliced_copy(generator, ctx, ty, v, &[slice])?.as_base_value().into()
// Accessing an element from a multi-dimensional `ndarray` // }
let Some(index_addr) = make_indices_arr(generator, ctx)? else { return Ok(None) }; // _ => {
// // Accessing an element from a multi-dimensional `ndarray`
// Create a new array, remove the top dimension from the dimension-size-list, and copy the // let Some(index_addr) = make_indices_arr(generator, ctx)? else { return Ok(None) };
// elements over
let subscripted_ndarray =
generator.gen_var_alloc(ctx, llvm_ndarray_t.into(), None)?;
let ndarray = NDArrayValue::from_ptr_val(subscripted_ndarray, llvm_usize, None);
let num_dims = v.load_ndims(ctx); // // Create a new array, remove the top dimension from the dimension-size-list, and copy the
ndarray.store_ndims( // // elements over
ctx, // let subscripted_ndarray =
generator, // generator.gen_var_alloc(ctx, llvm_ndarray_t.into(), None)?;
ctx.builder // let ndarray = NDArrayValue::from_ptr_val(subscripted_ndarray, llvm_usize, None);
.build_int_sub(num_dims, llvm_usize.const_int(1, false), "")
.unwrap(),
);
let ndarray_num_dims = ndarray.load_ndims(ctx); // let num_dims = v.load_ndims(ctx);
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims); // ndarray.store_ndims(
// ctx,
// generator,
// ctx.builder
// .build_int_sub(num_dims, llvm_usize.const_int(1, false), "")
// .unwrap(),
// );
let ndarray_num_dims = ndarray.load_ndims(ctx); // let ndarray_num_dims = ndarray.load_ndims(ctx);
let v_dims_src_ptr = unsafe { // ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims);
v.dim_sizes().ptr_offset_unchecked(
ctx,
generator,
&llvm_usize.const_int(1, false),
None,
)
};
call_memcpy_generic(
ctx,
ndarray.dim_sizes().base_ptr(ctx, generator),
v_dims_src_ptr,
ctx.builder
.build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "")
.map(Into::into)
.unwrap(),
llvm_i1.const_zero(),
);
let ndarray_num_elems = call_ndarray_calc_size( // let ndarray_num_dims = ndarray.load_ndims(ctx);
generator, // let v_dims_src_ptr = unsafe {
ctx, // v.dim_sizes().ptr_offset_unchecked(
&ndarray.dim_sizes().as_slice_value(ctx, generator), // ctx,
(None, None), // generator,
); // &llvm_usize.const_int(1, false),
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems); // None,
// )
// };
// call_memcpy_generic(
// ctx,
// ndarray.dim_sizes().base_ptr(ctx, generator),
// v_dims_src_ptr,
// ctx.builder
// .build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "")
// .map(Into::into)
// .unwrap(),
// llvm_i1.const_zero(),
// );
let v_data_src_ptr = v.data().ptr_offset(ctx, generator, &index_addr, None); // let ndarray_num_elems = call_ndarray_calc_size(
call_memcpy_generic( // generator,
ctx, // ctx,
ndarray.data().base_ptr(ctx, generator), // &ndarray.dim_sizes().as_slice_value(ctx, generator),
v_data_src_ptr, // (None, None),
ctx.builder // );
.build_int_mul( // ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
ndarray_num_elems,
llvm_ndarray_data_t.size_of().unwrap(),
"",
)
.map(Into::into)
.unwrap(),
llvm_i1.const_zero(),
);
ndarray.as_base_value().into() // let v_data_src_ptr = v.data().ptr_offset(ctx, generator, &index_addr, None);
} // call_memcpy_generic(
} // ctx,
})) // ndarray.data().base_ptr(ctx, generator),
// v_data_src_ptr,
// ctx.builder
// .build_int_mul(
// ndarray_num_elems,
// llvm_ndarray_data_t.size_of().unwrap(),
// "",
// )
// .map(Into::into)
// .unwrap(),
// llvm_i1.const_zero(),
// );
// ndarray.as_base_value().into()
// }
// }
// }))
} }
/// See [`CodeGenerator::gen_expr`]. /// See [`CodeGenerator::gen_expr`].

View File

@ -137,6 +137,20 @@ void __nac3_ndarray_calc_broadcast_idx_impl(
} }
} // namespace } // namespace
template<typename SizeT>
static void __nac3_ndarray_strides_from_shape_impl(
SizeT ndims,
SizeT *shape,
SizeT *dst_strides
) {
SizeT stride_product = 1;
for (SizeT i = 0; i < ndims; i++) {
int dim_i = ndims - i - 1;
dst_strides[dim_i] = stride_product;
stride_product *= shape[dim_i];
}
}
extern "C" { extern "C" {
#define DEF_nac3_int_exp_(T) \ #define DEF_nac3_int_exp_(T) \
T __nac3_int_exp_##T(T base, T exp) {\ T __nac3_int_exp_##T(T base, T exp) {\

View File

@ -563,367 +563,367 @@ pub fn call_j0<'ctx>(ctx: &CodeGenContext<'ctx, '_>, v: FloatValue<'ctx>) -> Flo
.unwrap() .unwrap()
} }
/// Generates a call to `__nac3_ndarray_calc_size`. Returns an [`IntValue`] representing the // /// Generates a call to `__nac3_ndarray_calc_size`. Returns an [`IntValue`] representing the
/// calculated total size. // /// calculated total size.
/// // ///
/// * `dims` - An [`ArrayLikeIndexer`] containing the size of each dimension. // /// * `dims` - An [`ArrayLikeIndexer`] containing the size of each dimension.
/// * `range` - The dimension index to begin and end (exclusively) calculating the dimensions for, // /// * `range` - The dimension index to begin and end (exclusively) calculating the dimensions for,
/// or [`None`] if starting from the first dimension and ending at the last dimension respectively. // /// or [`None`] if starting from the first dimension and ending at the last dimension respectively.
pub fn call_ndarray_calc_size<'ctx, G, Dims>( // pub fn call_ndarray_calc_size<'ctx, G, Dims>(
generator: &G, // generator: &G,
ctx: &CodeGenContext<'ctx, '_>, // ctx: &CodeGenContext<'ctx, '_>,
dims: &Dims, // dims: &Dims,
(begin, end): (Option<IntValue<'ctx>>, Option<IntValue<'ctx>>), // (begin, end): (Option<IntValue<'ctx>>, Option<IntValue<'ctx>>),
) -> IntValue<'ctx> // ) -> IntValue<'ctx>
where // where
G: CodeGenerator + ?Sized, // G: CodeGenerator + ?Sized,
Dims: ArrayLikeIndexer<'ctx>, // Dims: ArrayLikeIndexer<'ctx>,
{ // {
let llvm_usize = generator.get_size_type(ctx.ctx); // let llvm_usize = generator.get_size_type(ctx.ctx);
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default()); // let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
//
let ndarray_calc_size_fn_name = match llvm_usize.get_bit_width() { // let ndarray_calc_size_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_size", // 32 => "__nac3_ndarray_calc_size",
64 => "__nac3_ndarray_calc_size64", // 64 => "__nac3_ndarray_calc_size64",
bw => unreachable!("Unsupported size type bit width: {}", bw), // bw => unreachable!("Unsupported size type bit width: {}", bw),
}; // };
let ndarray_calc_size_fn_t = llvm_usize.fn_type( // let ndarray_calc_size_fn_t = llvm_usize.fn_type(
&[llvm_pusize.into(), llvm_usize.into(), llvm_usize.into(), llvm_usize.into()], // &[llvm_pusize.into(), llvm_usize.into(), llvm_usize.into(), llvm_usize.into()],
false, // false,
); // );
let ndarray_calc_size_fn = // let ndarray_calc_size_fn =
ctx.module.get_function(ndarray_calc_size_fn_name).unwrap_or_else(|| { // ctx.module.get_function(ndarray_calc_size_fn_name).unwrap_or_else(|| {
ctx.module.add_function(ndarray_calc_size_fn_name, ndarray_calc_size_fn_t, None) // ctx.module.add_function(ndarray_calc_size_fn_name, ndarray_calc_size_fn_t, None)
}); // });
//
let begin = begin.unwrap_or_else(|| llvm_usize.const_zero()); // let begin = begin.unwrap_or_else(|| llvm_usize.const_zero());
let end = end.unwrap_or_else(|| dims.size(ctx, generator)); // let end = end.unwrap_or_else(|| dims.size(ctx, generator));
ctx.builder // ctx.builder
.build_call( // .build_call(
ndarray_calc_size_fn, // ndarray_calc_size_fn,
&[ // &[
dims.base_ptr(ctx, generator).into(), // dims.base_ptr(ctx, generator).into(),
dims.size(ctx, generator).into(), // dims.size(ctx, generator).into(),
begin.into(), // begin.into(),
end.into(), // end.into(),
], // ],
"", // "",
) // )
.map(CallSiteValue::try_as_basic_value) // .map(CallSiteValue::try_as_basic_value)
.map(|v| v.map_left(BasicValueEnum::into_int_value)) // .map(|v| v.map_left(BasicValueEnum::into_int_value))
.map(Either::unwrap_left) // .map(Either::unwrap_left)
.unwrap() // .unwrap()
} // }
//
/// Generates a call to `__nac3_ndarray_calc_nd_indices`. Returns a [`TypeArrayLikeAdpater`] // /// Generates a call to `__nac3_ndarray_calc_nd_indices`. Returns a [`TypeArrayLikeAdpater`]
/// containing `i32` indices of the flattened index. // /// containing `i32` indices of the flattened index.
/// // ///
/// * `index` - The index to compute the multidimensional index for. // /// * `index` - The index to compute the multidimensional index for.
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an // /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
/// `NDArray`. // /// `NDArray`.
pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>( // pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
generator: &G, // generator: &G,
ctx: &mut CodeGenContext<'ctx, '_>, // ctx: &mut CodeGenContext<'ctx, '_>,
index: IntValue<'ctx>, // index: IntValue<'ctx>,
ndarray: NDArrayValue<'ctx>, // ndarray: NDArrayValue<'ctx>,
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> { // ) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
let llvm_void = ctx.ctx.void_type(); // let llvm_void = ctx.ctx.void_type();
let llvm_i32 = ctx.ctx.i32_type(); // let llvm_i32 = ctx.ctx.i32_type();
let llvm_usize = generator.get_size_type(ctx.ctx); // let llvm_usize = generator.get_size_type(ctx.ctx);
let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default()); // let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default());
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default()); // let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
//
let ndarray_calc_nd_indices_fn_name = match llvm_usize.get_bit_width() { // let ndarray_calc_nd_indices_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_nd_indices", // 32 => "__nac3_ndarray_calc_nd_indices",
64 => "__nac3_ndarray_calc_nd_indices64", // 64 => "__nac3_ndarray_calc_nd_indices64",
bw => unreachable!("Unsupported size type bit width: {}", bw), // bw => unreachable!("Unsupported size type bit width: {}", bw),
}; // };
let ndarray_calc_nd_indices_fn = // let ndarray_calc_nd_indices_fn =
ctx.module.get_function(ndarray_calc_nd_indices_fn_name).unwrap_or_else(|| { // ctx.module.get_function(ndarray_calc_nd_indices_fn_name).unwrap_or_else(|| {
let fn_type = llvm_void.fn_type( // let fn_type = llvm_void.fn_type(
&[llvm_usize.into(), llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into()], // &[llvm_usize.into(), llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into()],
false, // false,
); // );
//
ctx.module.add_function(ndarray_calc_nd_indices_fn_name, fn_type, None) // ctx.module.add_function(ndarray_calc_nd_indices_fn_name, fn_type, None)
}); // });
//
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.dim_sizes();
//
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();
//
ctx.builder // ctx.builder
.build_call( // .build_call(
ndarray_calc_nd_indices_fn, // ndarray_calc_nd_indices_fn,
&[ // &[
index.into(), // index.into(),
ndarray_dims.base_ptr(ctx, generator).into(), // ndarray_dims.base_ptr(ctx, generator).into(),
ndarray_num_dims.into(), // ndarray_num_dims.into(),
indices.into(), // indices.into(),
], // ],
"", // "",
) // )
.unwrap(); // .unwrap();
//
TypedArrayLikeAdapter::from( // TypedArrayLikeAdapter::from(
ArraySliceValue::from_ptr_val(indices, ndarray_num_dims, None), // ArraySliceValue::from_ptr_val(indices, ndarray_num_dims, None),
Box::new(|_, v| v.into_int_value()), // Box::new(|_, v| v.into_int_value()),
Box::new(|_, v| v.into()), // Box::new(|_, v| v.into()),
) // )
} // }
//
fn call_ndarray_flatten_index_impl<'ctx, G, Indices>( // fn call_ndarray_flatten_index_impl<'ctx, G, Indices>(
generator: &G, // generator: &G,
ctx: &CodeGenContext<'ctx, '_>, // ctx: &CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, // ndarray: NDArrayValue<'ctx>,
indices: &Indices, // indices: &Indices,
) -> IntValue<'ctx> // ) -> IntValue<'ctx>
where // where
G: CodeGenerator + ?Sized, // G: CodeGenerator + ?Sized,
Indices: ArrayLikeIndexer<'ctx>, // Indices: ArrayLikeIndexer<'ctx>,
{ // {
let llvm_i32 = ctx.ctx.i32_type(); // let llvm_i32 = ctx.ctx.i32_type();
let llvm_usize = generator.get_size_type(ctx.ctx); // let llvm_usize = generator.get_size_type(ctx.ctx);
//
let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default()); // let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default());
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default()); // let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
//
debug_assert_eq!( // debug_assert_eq!(
IntType::try_from(indices.element_type(ctx, generator)) // IntType::try_from(indices.element_type(ctx, generator))
.map(IntType::get_bit_width) // .map(IntType::get_bit_width)
.unwrap_or_default(), // .unwrap_or_default(),
llvm_i32.get_bit_width(), // llvm_i32.get_bit_width(),
"Expected i32 value for argument `indices` to `call_ndarray_flatten_index_impl`" // "Expected i32 value for argument `indices` to `call_ndarray_flatten_index_impl`"
); // );
debug_assert_eq!( // debug_assert_eq!(
indices.size(ctx, generator).get_type().get_bit_width(), // indices.size(ctx, generator).get_type().get_bit_width(),
llvm_usize.get_bit_width(), // llvm_usize.get_bit_width(),
"Expected usize integer value for argument `indices_size` to `call_ndarray_flatten_index_impl`" // "Expected usize integer value for argument `indices_size` to `call_ndarray_flatten_index_impl`"
); // );
//
let ndarray_flatten_index_fn_name = match llvm_usize.get_bit_width() { // let ndarray_flatten_index_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_flatten_index", // 32 => "__nac3_ndarray_flatten_index",
64 => "__nac3_ndarray_flatten_index64", // 64 => "__nac3_ndarray_flatten_index64",
bw => unreachable!("Unsupported size type bit width: {}", bw), // bw => unreachable!("Unsupported size type bit width: {}", bw),
}; // };
let ndarray_flatten_index_fn = // let ndarray_flatten_index_fn =
ctx.module.get_function(ndarray_flatten_index_fn_name).unwrap_or_else(|| { // ctx.module.get_function(ndarray_flatten_index_fn_name).unwrap_or_else(|| {
let fn_type = llvm_usize.fn_type( // let fn_type = llvm_usize.fn_type(
&[llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into(), llvm_usize.into()], // &[llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into(), llvm_usize.into()],
false, // false,
); // );
//
ctx.module.add_function(ndarray_flatten_index_fn_name, fn_type, None) // ctx.module.add_function(ndarray_flatten_index_fn_name, fn_type, None)
}); // });
//
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.dim_sizes();
//
let index = ctx // let index = ctx
.builder // .builder
.build_call( // .build_call(
ndarray_flatten_index_fn, // ndarray_flatten_index_fn,
&[ // &[
ndarray_dims.base_ptr(ctx, generator).into(), // ndarray_dims.base_ptr(ctx, generator).into(),
ndarray_num_dims.into(), // ndarray_num_dims.into(),
indices.base_ptr(ctx, generator).into(), // indices.base_ptr(ctx, generator).into(),
indices.size(ctx, generator).into(), // indices.size(ctx, generator).into(),
], // ],
"", // "",
) // )
.map(CallSiteValue::try_as_basic_value) // .map(CallSiteValue::try_as_basic_value)
.map(|v| v.map_left(BasicValueEnum::into_int_value)) // .map(|v| v.map_left(BasicValueEnum::into_int_value))
.map(Either::unwrap_left) // .map(Either::unwrap_left)
.unwrap(); // .unwrap();
//
index // index
} // }
//
/// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the // /// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the
/// multidimensional index. // /// multidimensional index.
/// // ///
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an // /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
/// `NDArray`. // /// `NDArray`.
/// * `indices` - The multidimensional index to compute the flattened index for. // /// * `indices` - The multidimensional index to compute the flattened index for.
pub fn call_ndarray_flatten_index<'ctx, G, Index>( // pub fn call_ndarray_flatten_index<'ctx, G, Index>(
generator: &mut G, // generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, // ctx: &mut CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, // ndarray: NDArrayValue<'ctx>,
indices: &Index, // indices: &Index,
) -> IntValue<'ctx> // ) -> IntValue<'ctx>
where // where
G: CodeGenerator + ?Sized, // G: CodeGenerator + ?Sized,
Index: ArrayLikeIndexer<'ctx>, // Index: ArrayLikeIndexer<'ctx>,
{ // {
call_ndarray_flatten_index_impl(generator, ctx, ndarray, indices) // call_ndarray_flatten_index_impl(generator, ctx, ndarray, indices)
} // }
//
/// Generates a call to `__nac3_ndarray_calc_broadcast`. Returns a tuple containing the number of // /// Generates a call to `__nac3_ndarray_calc_broadcast`. Returns a tuple containing the number of
/// dimension and size of each dimension of the resultant `ndarray`. // /// dimension and size of each dimension of the resultant `ndarray`.
pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>( // pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G, // generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, // ctx: &mut CodeGenContext<'ctx, '_>,
lhs: NDArrayValue<'ctx>, // lhs: NDArrayValue<'ctx>,
rhs: NDArrayValue<'ctx>, // rhs: NDArrayValue<'ctx>,
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> { // ) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
let llvm_usize = generator.get_size_type(ctx.ctx); // let llvm_usize = generator.get_size_type(ctx.ctx);
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default()); // let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
//
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() { // let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_broadcast", // 32 => "__nac3_ndarray_calc_broadcast",
64 => "__nac3_ndarray_calc_broadcast64", // 64 => "__nac3_ndarray_calc_broadcast64",
bw => unreachable!("Unsupported size type bit width: {}", bw), // bw => unreachable!("Unsupported size type bit width: {}", bw),
}; // };
let ndarray_calc_broadcast_fn = // let ndarray_calc_broadcast_fn =
ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| { // ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {
let fn_type = llvm_usize.fn_type( // let fn_type = llvm_usize.fn_type(
&[ // &[
llvm_pusize.into(), // llvm_pusize.into(),
llvm_usize.into(), // llvm_usize.into(),
llvm_pusize.into(), // llvm_pusize.into(),
llvm_usize.into(), // llvm_usize.into(),
llvm_pusize.into(), // llvm_pusize.into(),
], // ],
false, // false,
); // );
//
ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None) // ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None)
}); // });
//
let lhs_ndims = lhs.load_ndims(ctx); // let lhs_ndims = lhs.load_ndims(ctx);
let rhs_ndims = rhs.load_ndims(ctx); // let rhs_ndims = rhs.load_ndims(ctx);
let min_ndims = llvm_intrinsics::call_int_umin(ctx, lhs_ndims, rhs_ndims, None); // let min_ndims = llvm_intrinsics::call_int_umin(ctx, lhs_ndims, rhs_ndims, None);
//
gen_for_callback_incrementing( // gen_for_callback_incrementing(
generator, // generator,
ctx, // ctx,
llvm_usize.const_zero(), // llvm_usize.const_zero(),
(min_ndims, false), // (min_ndims, false),
|generator, ctx, _, idx| { // |generator, ctx, _, idx| {
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.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None),
rhs.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None), // rhs.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None),
) // )
}; // };
//
let llvm_usize_const_one = llvm_usize.const_int(1, false); // let llvm_usize_const_one = llvm_usize.const_int(1, false);
let lhs_eqz = ctx // let lhs_eqz = ctx
.builder // .builder
.build_int_compare(IntPredicate::EQ, lhs_dim_sz, llvm_usize_const_one, "") // .build_int_compare(IntPredicate::EQ, lhs_dim_sz, llvm_usize_const_one, "")
.unwrap(); // .unwrap();
let rhs_eqz = ctx // let rhs_eqz = ctx
.builder // .builder
.build_int_compare(IntPredicate::EQ, rhs_dim_sz, llvm_usize_const_one, "") // .build_int_compare(IntPredicate::EQ, rhs_dim_sz, llvm_usize_const_one, "")
.unwrap(); // .unwrap();
let lhs_or_rhs_eqz = ctx.builder.build_or(lhs_eqz, rhs_eqz, "").unwrap(); // let lhs_or_rhs_eqz = ctx.builder.build_or(lhs_eqz, rhs_eqz, "").unwrap();
//
let lhs_eq_rhs = ctx // let lhs_eq_rhs = ctx
.builder // .builder
.build_int_compare(IntPredicate::EQ, lhs_dim_sz, rhs_dim_sz, "") // .build_int_compare(IntPredicate::EQ, lhs_dim_sz, rhs_dim_sz, "")
.unwrap(); // .unwrap();
//
let is_compatible = ctx.builder.build_or(lhs_or_rhs_eqz, lhs_eq_rhs, "").unwrap(); // let is_compatible = ctx.builder.build_or(lhs_or_rhs_eqz, lhs_eq_rhs, "").unwrap();
//
ctx.make_assert( // ctx.make_assert(
generator, // generator,
is_compatible, // is_compatible,
"0:ValueError", // "0:ValueError",
"operands could not be broadcast together", // "operands could not be broadcast together",
[None, None, None], // [None, None, None],
ctx.current_loc, // ctx.current_loc,
); // );
//
Ok(()) // Ok(())
}, // },
llvm_usize.const_int(1, false), // llvm_usize.const_int(1, false),
) // )
.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.dim_sizes().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.dim_sizes().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);
//
ctx.builder // ctx.builder
.build_call( // .build_call(
ndarray_calc_broadcast_fn, // ndarray_calc_broadcast_fn,
&[ // &[
lhs_dims.into(), // lhs_dims.into(),
lhs_ndims.into(), // lhs_ndims.into(),
rhs_dims.into(), // rhs_dims.into(),
rhs_ndims.into(), // rhs_ndims.into(),
out_dims.base_ptr(ctx, generator).into(), // out_dims.base_ptr(ctx, generator).into(),
], // ],
"", // "",
) // )
.unwrap(); // .unwrap();
//
TypedArrayLikeAdapter::from( // TypedArrayLikeAdapter::from(
out_dims, // out_dims,
Box::new(|_, v| v.into_int_value()), // Box::new(|_, v| v.into_int_value()),
Box::new(|_, v| v.into()), // Box::new(|_, v| v.into()),
) // )
} // }
//
/// Generates a call to `__nac3_ndarray_calc_broadcast_idx`. Returns an [`ArrayAllocaValue`] // /// Generates a call to `__nac3_ndarray_calc_broadcast_idx`. Returns an [`ArrayAllocaValue`]
/// containing the indices used for accessing `array` corresponding to the index of the broadcasted // /// containing the indices used for accessing `array` corresponding to the index of the broadcasted
/// array `broadcast_idx`. // /// array `broadcast_idx`.
pub fn call_ndarray_calc_broadcast_index< // pub fn call_ndarray_calc_broadcast_index<
'ctx, // 'ctx,
G: CodeGenerator + ?Sized, // G: CodeGenerator + ?Sized,
BroadcastIdx: UntypedArrayLikeAccessor<'ctx>, // BroadcastIdx: UntypedArrayLikeAccessor<'ctx>,
>( // >(
generator: &mut G, // generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, // ctx: &mut CodeGenContext<'ctx, '_>,
array: NDArrayValue<'ctx>, // array: NDArrayValue<'ctx>,
broadcast_idx: &BroadcastIdx, // broadcast_idx: &BroadcastIdx,
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> { // ) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
let llvm_i32 = ctx.ctx.i32_type(); // let llvm_i32 = ctx.ctx.i32_type();
let llvm_usize = generator.get_size_type(ctx.ctx); // let llvm_usize = generator.get_size_type(ctx.ctx);
let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default()); // let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default());
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default()); // let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
//
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() { // let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_broadcast_idx", // 32 => "__nac3_ndarray_calc_broadcast_idx",
64 => "__nac3_ndarray_calc_broadcast_idx64", // 64 => "__nac3_ndarray_calc_broadcast_idx64",
bw => unreachable!("Unsupported size type bit width: {}", bw), // bw => unreachable!("Unsupported size type bit width: {}", bw),
}; // };
let ndarray_calc_broadcast_fn = // let ndarray_calc_broadcast_fn =
ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| { // ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {
let fn_type = llvm_usize.fn_type( // let fn_type = llvm_usize.fn_type(
&[llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into(), llvm_pi32.into()], // &[llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into(), llvm_pi32.into()],
false, // false,
); // );
//
ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None) // ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None)
}); // });
//
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.dim_sizes().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)
}; // };
//
ctx.builder // ctx.builder
.build_call( // .build_call(
ndarray_calc_broadcast_fn, // ndarray_calc_broadcast_fn,
&[array_dims.into(), array_ndims.into(), broadcast_idx_ptr.into(), out_idx.into()], // &[array_dims.into(), array_ndims.into(), broadcast_idx_ptr.into(), out_idx.into()],
"", // "",
) // )
.unwrap(); // .unwrap();
//
TypedArrayLikeAdapter::from( // TypedArrayLikeAdapter::from(
ArraySliceValue::from_ptr_val(out_idx, broadcast_size, None), // ArraySliceValue::from_ptr_val(out_idx, broadcast_size, None),
Box::new(|_, v| v.into_int_value()), // Box::new(|_, v| v.into_int_value()),
Box::new(|_, v| v.into()), // Box::new(|_, v| v.into()),
) // )
} // }

File diff suppressed because it is too large Load Diff

View File

@ -961,8 +961,9 @@ impl<'a> BuiltinBuilder<'a> {
resolver: None, resolver: None,
codegen_callback: Some(Arc::new(GenCall::new(Box::new( codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|ctx, obj, fun, args, generator| { |ctx, obj, fun, args, generator| {
gen_ndarray_copy(ctx, &obj, fun, &args, generator) todo!()
.map(|val| Some(val.as_basic_value_enum())) // gen_ndarray_copy(ctx, &obj, fun, &args, generator)
// .map(|val| Some(val.as_basic_value_enum()))
}, },
)))), )))),
loc: None, loc: None,
@ -978,8 +979,9 @@ impl<'a> BuiltinBuilder<'a> {
resolver: None, resolver: None,
codegen_callback: Some(Arc::new(GenCall::new(Box::new( codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|ctx, obj, fun, args, generator| { |ctx, obj, fun, args, generator| {
gen_ndarray_fill(ctx, &obj, fun, &args, generator)?; todo!()
Ok(None) // gen_ndarray_fill(ctx, &obj, fun, &args, generator)?;
// Ok(None)
}, },
)))), )))),
loc: None, loc: None,
@ -1199,13 +1201,14 @@ impl<'a> BuiltinBuilder<'a> {
self.ndarray_float, self.ndarray_float,
&[(self.ndarray_factory_fn_shape_arg_tvar.ty, "shape")], &[(self.ndarray_factory_fn_shape_arg_tvar.ty, "shape")],
Box::new(move |ctx, obj, fun, args, generator| { Box::new(move |ctx, obj, fun, args, generator| {
let func = match prim { todo!()
PrimDef::FunNpNDArray | PrimDef::FunNpEmpty => gen_ndarray_empty, // let func = match prim {
PrimDef::FunNpZeros => gen_ndarray_zeros, // PrimDef::FunNpNDArray | PrimDef::FunNpEmpty => gen_ndarray_empty,
PrimDef::FunNpOnes => gen_ndarray_ones, // PrimDef::FunNpZeros => gen_ndarray_zeros,
_ => unreachable!(), // PrimDef::FunNpOnes => gen_ndarray_ones,
}; // _ => unreachable!(),
func(ctx, &obj, fun, &args, generator).map(|val| Some(val.as_basic_value_enum())) // };
// func(ctx, &obj, fun, &args, generator).map(|val| Some(val.as_basic_value_enum()))
}), }),
) )
} }
@ -1251,8 +1254,9 @@ impl<'a> BuiltinBuilder<'a> {
resolver: None, resolver: None,
codegen_callback: Some(Arc::new(GenCall::new(Box::new( codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|ctx, obj, fun, args, generator| { |ctx, obj, fun, args, generator| {
gen_ndarray_array(ctx, &obj, fun, &args, generator) todo!()
.map(|val| Some(val.as_basic_value_enum())) // gen_ndarray_array(ctx, &obj, fun, &args, generator)
// .map(|val| Some(val.as_basic_value_enum()))
}, },
)))), )))),
loc: None, loc: None,
@ -1270,8 +1274,9 @@ impl<'a> BuiltinBuilder<'a> {
// type variable // type variable
&[(self.list_int32, "shape"), (tv.ty, "fill_value")], &[(self.list_int32, "shape"), (tv.ty, "fill_value")],
Box::new(move |ctx, obj, fun, args, generator| { Box::new(move |ctx, obj, fun, args, generator| {
gen_ndarray_full(ctx, &obj, fun, &args, generator) todo!()
.map(|val| Some(val.as_basic_value_enum())) // gen_ndarray_full(ctx, &obj, fun, &args, generator)
// .map(|val| Some(val.as_basic_value_enum()))
}), }),
) )
} }
@ -1303,8 +1308,9 @@ impl<'a> BuiltinBuilder<'a> {
resolver: None, resolver: None,
codegen_callback: Some(Arc::new(GenCall::new(Box::new( codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|ctx, obj, fun, args, generator| { |ctx, obj, fun, args, generator| {
gen_ndarray_eye(ctx, &obj, fun, &args, generator) todo!()
.map(|val| Some(val.as_basic_value_enum())) // gen_ndarray_eye(ctx, &obj, fun, &args, generator)
// .map(|val| Some(val.as_basic_value_enum()))
}, },
)))), )))),
loc: None, loc: None,
@ -1317,8 +1323,9 @@ impl<'a> BuiltinBuilder<'a> {
self.ndarray_float_2d, self.ndarray_float_2d,
&[(int32, "n")], &[(int32, "n")],
Box::new(|ctx, obj, fun, args, generator| { Box::new(|ctx, obj, fun, args, generator| {
gen_ndarray_identity(ctx, &obj, fun, &args, generator) todo!()
.map(|val| Some(val.as_basic_value_enum())) // gen_ndarray_identity(ctx, &obj, fun, &args, generator)
// .map(|val| Some(val.as_basic_value_enum()))
}), }),
), ),
_ => unreachable!(), _ => unreachable!(),