forked from M-Labs/nac3
1
0
Fork 0

core: reap numpy

This commit is contained in:
lyken 2024-07-08 10:19:29 +08:00
parent eeb474f9e6
commit 6233f84ee9
6 changed files with 2908 additions and 2848 deletions

File diff suppressed because it is too large Load Diff

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

@ -2,8 +2,7 @@ use crate::typecheck::typedef::Type;
use super::{ use super::{
classes::{ classes::{
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue, ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue, NpArrayValue, TypedArrayLikeAdapter, UntypedArrayLikeAccessor
TypedArrayLikeAdapter, UntypedArrayLikeAccessor,
}, },
llvm_intrinsics, CodeGenContext, CodeGenerator, llvm_intrinsics, CodeGenContext, CodeGenerator,
}; };
@ -15,7 +14,7 @@ use inkwell::{
memory_buffer::MemoryBuffer, memory_buffer::MemoryBuffer,
module::Module, module::Module,
types::{BasicTypeEnum, IntType}, types::{BasicTypeEnum, IntType},
values::{BasicValueEnum, CallSiteValue, FloatValue, IntValue}, values::{BasicValueEnum, CallSiteValue, FloatValue, IntValue, PointerValue},
AddressSpace, IntPredicate, AddressSpace, IntPredicate,
}; };
use itertools::Either; use itertools::Either;
@ -927,3 +926,13 @@ pub fn call_ndarray_calc_broadcast_index<
Box::new(|_, v| v.into()), Box::new(|_, v| v.into()),
) )
} }
fn call_ndarray_strides_from_shape<'ctx, G, Index>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
ndims: IntValue<'ctx>,
shape: PointerValue<'ctx>,
dst_strides: PointerValue<'ctx>,
) -> IntValue<'ctx> {
todo!()
}

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!(),