WIP
This commit is contained in:
parent
a370a52658
commit
b461cdccb6
|
@ -735,7 +735,9 @@ fn format_rpc_ret<'ctx>(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ndarray.create_data(ctx, llvm_elem_ty, num_elements);
|
unsafe {
|
||||||
|
ndarray.create_data(generator, ctx, llvm_elem_ty, num_elements);
|
||||||
|
}
|
||||||
|
|
||||||
let ndarray_data = ndarray.data().base_ptr(ctx, generator);
|
let ndarray_data = ndarray.data().base_ptr(ctx, generator);
|
||||||
let ndarray_data_i8 =
|
let ndarray_data_i8 =
|
||||||
|
|
|
@ -2858,7 +2858,9 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
|
||||||
.builder
|
.builder
|
||||||
.build_int_z_extend_or_bit_cast(ndarray_num_elems, sizeof_elem.get_type(), "")
|
.build_int_z_extend_or_bit_cast(ndarray_num_elems, sizeof_elem.get_type(), "")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
unsafe {
|
||||||
|
ndarray.create_data(generator, ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
||||||
|
}
|
||||||
|
|
||||||
let v_data_src_ptr = v.data().ptr_offset(ctx, generator, &index_addr, None);
|
let v_data_src_ptr = v.data().ptr_offset(ctx, generator, &index_addr, None);
|
||||||
call_memcpy_generic(
|
call_memcpy_generic(
|
||||||
|
@ -3604,3 +3606,97 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a function in the current module and inserts a `call` instruction into the LLVM IR.
|
||||||
|
pub fn create_fn_and_call<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
fn_name: &str,
|
||||||
|
ret_type: Option<BasicTypeEnum<'ctx>>,
|
||||||
|
(params, is_var_args): (&[BasicTypeEnum<'ctx>], bool),
|
||||||
|
args: &[BasicValueEnum<'ctx>],
|
||||||
|
call_value_name: Option<&str>,
|
||||||
|
configure: Option<&dyn Fn(&FunctionValue<'ctx>)>,
|
||||||
|
) -> Option<BasicValueEnum<'ctx>> {
|
||||||
|
let intrinsic_fn = ctx.module.get_function(fn_name).unwrap_or_else(|| {
|
||||||
|
let params = params.iter()
|
||||||
|
.copied()
|
||||||
|
.map(BasicTypeEnum::into)
|
||||||
|
.collect_vec();
|
||||||
|
let fn_type = if let Some(ret_type) = ret_type {
|
||||||
|
ret_type.fn_type(params.as_slice(), is_var_args)
|
||||||
|
} else {
|
||||||
|
ctx.ctx.void_type().fn_type(params.as_slice(), is_var_args)
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.module.add_function(fn_name, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(configure) = configure {
|
||||||
|
configure(&intrinsic_fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
let args = args.iter()
|
||||||
|
.copied()
|
||||||
|
.map(BasicValueEnum::into)
|
||||||
|
.collect_vec();
|
||||||
|
ctx.builder
|
||||||
|
.build_call(intrinsic_fn, args.as_slice(), call_value_name.unwrap_or_default())
|
||||||
|
.map(CallSiteValue::try_as_basic_value)
|
||||||
|
.map(Either::left)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a function in the current module and inserts a `call` instruction into the LLVM IR.
|
||||||
|
///
|
||||||
|
/// This is a wrapper around [`create_fn_and_call`] for non-vararg function. This function allows
|
||||||
|
/// parameters and arguments to be specified as tuples to better indicate the expected type and
|
||||||
|
/// actual value of each parameter-argument pair of the call.
|
||||||
|
pub fn create_and_call_function<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
fn_name: &str,
|
||||||
|
ret_type: Option<BasicTypeEnum<'ctx>>,
|
||||||
|
params: &[(BasicTypeEnum<'ctx>, BasicValueEnum<'ctx>)],
|
||||||
|
value_name: Option<&str>,
|
||||||
|
configure: Option<&dyn Fn(&FunctionValue<'ctx>)>,
|
||||||
|
) -> Option<BasicValueEnum<'ctx>> {
|
||||||
|
let param_tys = params.iter().map(|(ty, _)| ty).copied().map(BasicTypeEnum::into).collect_vec();
|
||||||
|
let arg_values = params.iter().map(|(_, value)| value).copied().map(BasicValueEnum::into).collect_vec();
|
||||||
|
|
||||||
|
create_fn_and_call(
|
||||||
|
ctx,
|
||||||
|
fn_name,
|
||||||
|
ret_type,
|
||||||
|
(param_tys.as_slice(), false),
|
||||||
|
arg_values.as_slice(),
|
||||||
|
value_name,
|
||||||
|
configure,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a function in the current module and inserts a `call` instruction into the LLVM IR.
|
||||||
|
///
|
||||||
|
/// This is a wrapper around [`create_fn_and_call`] for non-vararg function. This function allows
|
||||||
|
/// only arguments to be specified and performs inference for the parameter types using
|
||||||
|
/// [`BasicValueEnum::get_type`].
|
||||||
|
pub fn infer_and_call_function<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
fn_name: &str,
|
||||||
|
ret_type: Option<BasicTypeEnum<'ctx>>,
|
||||||
|
args: &[BasicValueEnum<'ctx>],
|
||||||
|
value_name: Option<&str>,
|
||||||
|
configure: Option<&dyn Fn(&FunctionValue<'ctx>)>,
|
||||||
|
) -> Option<BasicValueEnum<'ctx>> {
|
||||||
|
let param_tys = args.iter()
|
||||||
|
.map(BasicValueEnum::get_type)
|
||||||
|
.collect_vec();
|
||||||
|
|
||||||
|
create_fn_and_call(
|
||||||
|
ctx,
|
||||||
|
fn_name,
|
||||||
|
ret_type,
|
||||||
|
(param_tys.as_slice(), false),
|
||||||
|
args,
|
||||||
|
value_name,
|
||||||
|
configure,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -60,6 +60,27 @@ pub fn load_irrt<'ctx>(ctx: &'ctx Context, symbol_resolver: &dyn SymbolResolver)
|
||||||
irrt_mod
|
irrt_mod
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the name of a function which contains variants for 32-bit and 64-bit `size_t`.
|
||||||
|
///
|
||||||
|
/// - When [`TypeContext::size_type`] is 32-bits, the function name is `fn_name}`.
|
||||||
|
/// - When [`TypeContext::size_type`] is 64-bits, the function name is `{fn_name}64`.
|
||||||
|
#[must_use]
|
||||||
|
pub fn get_usize_dependent_function_name<G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &CodeGenContext<'_, '_>,
|
||||||
|
name: &str,
|
||||||
|
) -> String {
|
||||||
|
let mut name = name.to_owned();
|
||||||
|
match generator.get_size_type(ctx.ctx).get_bit_width() {
|
||||||
|
32 => {}
|
||||||
|
64 => name.push_str("64"),
|
||||||
|
bit_width => {
|
||||||
|
panic!("Unsupported int type bit width {bit_width}, must be either 32-bits or 64-bits")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
name
|
||||||
|
}
|
||||||
|
|
||||||
/// NOTE: the output value of the end index of this function should be compared ***inclusively***,
|
/// NOTE: the output value of the end index of this function should be compared ***inclusively***,
|
||||||
/// because python allows `a[2::-1]`, whose semantic is `[a[2], a[1], a[0]]`, which is equivalent to
|
/// because python allows `a[2::-1]`, whose semantic is `[a[2], a[1], a[0]]`, which is equivalent to
|
||||||
/// NO numeric slice in python.
|
/// NO numeric slice in python.
|
||||||
|
|
|
@ -1,134 +1,258 @@
|
||||||
use crate::codegen::{CodeGenContext, CodeGenerator};
|
use inkwell::{
|
||||||
|
values::{BasicValueEnum, IntValue, PointerValue},
|
||||||
|
AddressSpace,
|
||||||
|
};
|
||||||
|
|
||||||
/// Returns the name of a function which contains variants for 32-bit and 64-bit `size_t`.
|
use crate::codegen::{
|
||||||
///
|
expr::create_and_call_function,
|
||||||
/// - When [`TypeContext::size_type`] is 32-bits, the function name is `fn_name}`.
|
irrt::get_usize_dependent_function_name,
|
||||||
/// - When [`TypeContext::size_type`] is 64-bits, the function name is `{fn_name}64`.
|
types::NDArrayType,
|
||||||
#[must_use]
|
values::{NDArrayValue, ProxyValue},
|
||||||
pub fn get_usize_dependent_function_name<G: CodeGenerator + ?Sized>(
|
CodeGenContext, CodeGenerator,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_util_assert_shape_no_negative<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
ctx: &CodeGenContext<'_, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
name: &str,
|
ndims: IntValue<'ctx>,
|
||||||
) -> String {
|
shape: PointerValue<'ctx>,
|
||||||
let mut name = name.to_owned();
|
) {
|
||||||
match generator.get_size_type(ctx.ctx).get_bit_width() {
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
32 => {}
|
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
||||||
64 => name.push_str("64"),
|
|
||||||
bit_width => {
|
let name = get_usize_dependent_function_name(
|
||||||
panic!("Unsupported int type bit width {bit_width}, must be either 32-bits or 64-bits")
|
generator,
|
||||||
}
|
ctx,
|
||||||
}
|
"__nac3_ndarray_util_assert_shape_no_negative",
|
||||||
name
|
);
|
||||||
|
|
||||||
|
create_and_call_function(
|
||||||
|
ctx,
|
||||||
|
&name,
|
||||||
|
Some(llvm_usize.into()),
|
||||||
|
&[(llvm_usize.into(), ndims.into()), (llvm_pusize.into(), shape.into())],
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn call_nac3_ndarray_util_assert_shape_no_negative<'ctx, G: CodeGenerator + ?Sized>(
|
pub fn call_nac3_ndarray_util_assert_output_shape_same<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
// generator: &mut G,
|
generator: &mut G,
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
// ndims: Instance<'ctx, Int<SizeT>>,
|
ndarray_ndims: IntValue<'ctx>,
|
||||||
// shape: Instance<'ctx, Ptr<Int<SizeT>>>,
|
ndarray_shape: PointerValue<'ctx>,
|
||||||
// ) {
|
output_ndims: IntValue<'ctx>,
|
||||||
// let name = get_usize_dependent_function_name(
|
output_shape: IntValue<'ctx>,
|
||||||
// generator,
|
) {
|
||||||
// ctx,
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
// "__nac3_ndarray_util_assert_shape_no_negative",
|
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
||||||
// );
|
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndims).arg(shape).returning_void();
|
let name = get_usize_dependent_function_name(
|
||||||
// }
|
generator,
|
||||||
//
|
ctx,
|
||||||
// pub fn call_nac3_ndarray_util_assert_output_shape_same<'ctx, G: CodeGenerator + ?Sized>(
|
"__nac3_ndarray_util_assert_output_shape_same",
|
||||||
// generator: &mut G,
|
);
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
// ndarray_ndims: Instance<'ctx, Int<SizeT>>,
|
create_and_call_function(
|
||||||
// ndarray_shape: Instance<'ctx, Ptr<Int<SizeT>>>,
|
ctx,
|
||||||
// output_ndims: Instance<'ctx, Int<SizeT>>,
|
&name,
|
||||||
// output_shape: Instance<'ctx, Ptr<Int<SizeT>>>,
|
Some(llvm_usize.into()),
|
||||||
// ) {
|
&[
|
||||||
// let name = get_usize_dependent_function_name(
|
(llvm_usize.into(), ndarray_ndims.into()),
|
||||||
// generator,
|
(llvm_pusize.into(), ndarray_shape.into()),
|
||||||
// ctx,
|
(llvm_usize.into(), output_ndims.into()),
|
||||||
// "__nac3_ndarray_util_assert_output_shape_same",
|
(llvm_pusize.into(), output_shape.into()),
|
||||||
// );
|
],
|
||||||
// FnCall::builder(generator, ctx, &name)
|
None,
|
||||||
// .arg(ndarray_ndims)
|
None,
|
||||||
// .arg(ndarray_shape)
|
);
|
||||||
// .arg(output_ndims)
|
}
|
||||||
// .arg(output_shape)
|
|
||||||
// .returning_void();
|
pub fn call_nac3_ndarray_size<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
// }
|
generator: &mut G,
|
||||||
//
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
// pub fn call_nac3_ndarray_size<'ctx, G: CodeGenerator + ?Sized>(
|
ndarray: NDArrayValue<'ctx>,
|
||||||
// generator: &mut G,
|
) -> IntValue<'ctx> {
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
// ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
// ) -> Instance<'ctx, Int<SizeT>> {
|
|
||||||
// let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_size");
|
let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_size");
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndarray).returning_auto("size")
|
|
||||||
// }
|
create_and_call_function(
|
||||||
//
|
ctx,
|
||||||
// pub fn call_nac3_ndarray_nbytes<'ctx, G: CodeGenerator + ?Sized>(
|
&name,
|
||||||
// generator: &mut G,
|
Some(llvm_usize.into()),
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
&[(llvm_ndarray.into(), ndarray.as_base_value().into())],
|
||||||
// ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
Some("size"),
|
||||||
// ) -> Instance<'ctx, Int<SizeT>> {
|
None,
|
||||||
// let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_nbytes");
|
)
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndarray).returning_auto("nbytes")
|
.map(BasicValueEnum::into_int_value)
|
||||||
// }
|
.unwrap()
|
||||||
//
|
}
|
||||||
// pub fn call_nac3_ndarray_len<'ctx, G: CodeGenerator + ?Sized>(
|
|
||||||
// generator: &mut G,
|
pub fn call_nac3_ndarray_nbytes<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
generator: &mut G,
|
||||||
// ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
// ) -> Instance<'ctx, Int<SizeT>> {
|
ndarray: NDArrayValue<'ctx>,
|
||||||
// let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_len");
|
) -> IntValue<'ctx> {
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndarray).returning_auto("len")
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
// }
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
//
|
|
||||||
// pub fn call_nac3_ndarray_is_c_contiguous<'ctx, G: CodeGenerator + ?Sized>(
|
let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_nbytes");
|
||||||
// generator: &mut G,
|
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
create_and_call_function(
|
||||||
// ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
ctx,
|
||||||
// ) -> Instance<'ctx, Int<Bool>> {
|
&name,
|
||||||
// let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_is_c_contiguous");
|
Some(llvm_usize.into()),
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndarray).returning_auto("is_c_contiguous")
|
&[(llvm_ndarray.into(), ndarray.as_base_value().into())],
|
||||||
// }
|
Some("nbytes"),
|
||||||
//
|
None,
|
||||||
// pub fn call_nac3_ndarray_get_nth_pelement<'ctx, G: CodeGenerator + ?Sized>(
|
)
|
||||||
// generator: &mut G,
|
.map(BasicValueEnum::into_int_value)
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
.unwrap()
|
||||||
// ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
}
|
||||||
// index: Instance<'ctx, Int<SizeT>>,
|
|
||||||
// ) -> Instance<'ctx, Ptr<Int<Byte>>> {
|
pub fn call_nac3_ndarray_len<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
// let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_get_nth_pelement");
|
generator: &mut G,
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndarray).arg(index).returning_auto("pelement")
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
// }
|
ndarray: NDArrayValue<'ctx>,
|
||||||
//
|
) -> IntValue<'ctx> {
|
||||||
// pub fn call_nac3_ndarray_get_pelement_by_indices<'ctx, G: CodeGenerator + ?Sized>(
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
// generator: &mut G,
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
// ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_len");
|
||||||
// indices: Instance<'ctx, Ptr<Int<SizeT>>>,
|
|
||||||
// ) -> Instance<'ctx, Ptr<Int<Byte>>> {
|
create_and_call_function(
|
||||||
// let name =
|
ctx,
|
||||||
// get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_get_pelement_by_indices");
|
&name,
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndarray).arg(indices).returning_auto("pelement")
|
Some(llvm_usize.into()),
|
||||||
// }
|
&[(llvm_ndarray.into(), ndarray.as_base_value().into())],
|
||||||
//
|
Some("len"),
|
||||||
// pub fn call_nac3_ndarray_set_strides_by_shape<'ctx, G: CodeGenerator + ?Sized>(
|
None,
|
||||||
// generator: &mut G,
|
)
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
.map(BasicValueEnum::into_int_value)
|
||||||
// ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
.unwrap()
|
||||||
// ) {
|
}
|
||||||
// let name =
|
|
||||||
// get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_set_strides_by_shape");
|
pub fn call_nac3_ndarray_is_c_contiguous<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
// FnCall::builder(generator, ctx, &name).arg(ndarray).returning_void();
|
generator: &mut G,
|
||||||
// }
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
//
|
ndarray: NDArrayValue<'ctx>,
|
||||||
// pub fn call_nac3_ndarray_copy_data<'ctx, G: CodeGenerator + ?Sized>(
|
) -> IntValue<'ctx> {
|
||||||
// generator: &mut G,
|
let llvm_i1 = ctx.ctx.bool_type();
|
||||||
// ctx: &mut CodeGenContext<'ctx, '_>,
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
// src_ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
// dst_ndarray: Instance<'ctx, Ptr<Struct<NDArray>>>,
|
|
||||||
// ) {
|
let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_is_c_contiguous");
|
||||||
// let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_copy_data");
|
|
||||||
// FnCall::builder(generator, ctx, &name).arg(src_ndarray).arg(dst_ndarray).returning_void();
|
create_and_call_function(
|
||||||
// }
|
ctx,
|
||||||
|
&name,
|
||||||
|
Some(llvm_i1.into()),
|
||||||
|
&[(llvm_ndarray.into(), ndarray.as_base_value().into())],
|
||||||
|
Some("is_c_contiguous"),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.map(BasicValueEnum::into_int_value)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_get_nth_pelement<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray: NDArrayValue<'ctx>,
|
||||||
|
index: IntValue<'ctx>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
let llvm_i8 = ctx.ctx.i8_type();
|
||||||
|
let llvm_pi8 = llvm_i8.ptr_type(AddressSpace::default());
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
|
|
||||||
|
let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_get_nth_pelement");
|
||||||
|
|
||||||
|
create_and_call_function(
|
||||||
|
ctx,
|
||||||
|
&name,
|
||||||
|
Some(llvm_pi8.into()),
|
||||||
|
&[(llvm_ndarray.into(), ndarray.as_base_value().into()), (llvm_usize.into(), index.into())],
|
||||||
|
Some("pelement"),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.map(BasicValueEnum::into_pointer_value)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_get_pelement_by_indices<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray: NDArrayValue<'ctx>,
|
||||||
|
indices: PointerValue<'ctx>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
let llvm_i8 = ctx.ctx.i8_type();
|
||||||
|
let llvm_pi8 = llvm_i8.ptr_type(AddressSpace::default());
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
||||||
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
|
|
||||||
|
let name =
|
||||||
|
get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_get_pelement_by_indices");
|
||||||
|
|
||||||
|
create_and_call_function(
|
||||||
|
ctx,
|
||||||
|
&name,
|
||||||
|
Some(llvm_pi8.into()),
|
||||||
|
&[
|
||||||
|
(llvm_ndarray.into(), ndarray.as_base_value().into()),
|
||||||
|
(llvm_pusize.into(), indices.into()),
|
||||||
|
],
|
||||||
|
Some("pelement"),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.map(BasicValueEnum::into_pointer_value)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_set_strides_by_shape<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray: NDArrayValue<'ctx>,
|
||||||
|
) {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
|
|
||||||
|
let name =
|
||||||
|
get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_set_strides_by_shape");
|
||||||
|
|
||||||
|
create_and_call_function(
|
||||||
|
ctx,
|
||||||
|
&name,
|
||||||
|
None,
|
||||||
|
&[(llvm_ndarray.into(), ndarray.as_base_value().into())],
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_copy_data<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
src_ndarray: NDArrayValue<'ctx>,
|
||||||
|
dst_ndarray: NDArrayValue<'ctx>,
|
||||||
|
) {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
let llvm_ndarray = NDArrayType::llvm_type(ctx.ctx, llvm_usize);
|
||||||
|
|
||||||
|
let name = get_usize_dependent_function_name(generator, ctx, "__nac3_ndarray_copy_data");
|
||||||
|
|
||||||
|
create_and_call_function(
|
||||||
|
ctx,
|
||||||
|
&name,
|
||||||
|
None,
|
||||||
|
&[
|
||||||
|
(llvm_ndarray.into(), src_ndarray.as_base_value().into()),
|
||||||
|
(llvm_ndarray.into(), dst_ndarray.as_base_value().into()),
|
||||||
|
],
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -212,7 +212,9 @@ fn ndarray_init_data<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
&ndarray.shape().as_slice_value(ctx, generator),
|
&ndarray.shape().as_slice_value(ctx, generator),
|
||||||
(None, None),
|
(None, None),
|
||||||
);
|
);
|
||||||
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
unsafe {
|
||||||
|
ndarray.create_data(generator, ctx, llvm_ndarray_data_t, ndarray_num_elems);
|
||||||
|
}
|
||||||
|
|
||||||
ndarray
|
ndarray
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,7 +103,7 @@ impl<'ctx> NDArrayType<'ctx> {
|
||||||
|
|
||||||
/// Creates an LLVM type corresponding to the expected structure of an `NDArray`.
|
/// Creates an LLVM type corresponding to the expected structure of an `NDArray`.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn llvm_type(ctx: &'ctx Context, llvm_usize: IntType<'ctx>) -> PointerType<'ctx> {
|
pub fn llvm_type(ctx: &'ctx Context, llvm_usize: IntType<'ctx>) -> PointerType<'ctx> {
|
||||||
// struct NDArray { data: i8*, itemsize: size_t, ndims: size_t, shape: size_t*, strides: size_t* }
|
// struct NDArray { data: i8*, itemsize: size_t, ndims: size_t, shape: size_t*, strides: size_t* }
|
||||||
//
|
//
|
||||||
// * data : Pointer to an array containing the array data
|
// * data : Pointer to an array containing the array data
|
||||||
|
@ -142,11 +142,7 @@ impl<'ctx> NDArrayType<'ctx> {
|
||||||
let llvm_dtype = ctx.get_llvm_type(generator, dtype);
|
let llvm_dtype = ctx.get_llvm_type(generator, dtype);
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
NDArrayType {
|
NDArrayType { ty: Self::llvm_type(ctx.ctx, llvm_usize), dtype: llvm_dtype, llvm_usize }
|
||||||
ty: Self::llvm_type(ctx.ctx, llvm_usize),
|
|
||||||
dtype: llvm_dtype,
|
|
||||||
llvm_usize,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an [`NDArrayType`] from a [`PointerType`] representing an `NDArray`.
|
/// Creates an [`NDArrayType`] from a [`PointerType`] representing an `NDArray`.
|
||||||
|
|
|
@ -184,8 +184,15 @@ impl<'ctx> NDArrayValue<'ctx> {
|
||||||
|
|
||||||
/// Convenience method for creating a new array storing data elements with the given element
|
/// Convenience method for creating a new array storing data elements with the given element
|
||||||
/// type `elem_ty` and `size`.
|
/// type `elem_ty` and `size`.
|
||||||
pub fn create_data(
|
///
|
||||||
|
/// The data buffer will be allocated on the stack, and is considered to be owned by this ndarray instance.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// `shape` and `itemsize` of the ndarray must be initialized.
|
||||||
|
pub unsafe fn create_data<G: CodeGenerator + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
elem_ty: BasicTypeEnum<'ctx>,
|
elem_ty: BasicTypeEnum<'ctx>,
|
||||||
size: IntValue<'ctx>,
|
size: IntValue<'ctx>,
|
||||||
|
@ -195,10 +202,10 @@ impl<'ctx> NDArrayValue<'ctx> {
|
||||||
let nbytes = ctx.builder.build_int_mul(size, itemsize, "").unwrap();
|
let nbytes = ctx.builder.build_int_mul(size, itemsize, "").unwrap();
|
||||||
|
|
||||||
// TODO: What about alignment?
|
// TODO: What about alignment?
|
||||||
self.store_data(
|
let data = ctx.builder.build_array_alloca(ctx.ctx.i8_type(), nbytes, "").unwrap();
|
||||||
ctx,
|
self.store_data(ctx, data);
|
||||||
ctx.builder.build_array_alloca(ctx.ctx.i8_type(), nbytes, "").unwrap(),
|
|
||||||
);
|
// self.set_strides_contiguous(generator, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a proxy object to the field storing the data of this `NDArray`.
|
/// Returns a proxy object to the field storing the data of this `NDArray`.
|
||||||
|
@ -206,6 +213,112 @@ impl<'ctx> NDArrayValue<'ctx> {
|
||||||
pub fn data(&self) -> NDArrayDataProxy<'ctx, '_> {
|
pub fn data(&self) -> NDArrayDataProxy<'ctx, '_> {
|
||||||
NDArrayDataProxy(self)
|
NDArrayDataProxy(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Copy shape dimensions from an array.
|
||||||
|
pub fn copy_shape_from_array<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
shape: PointerValue<'ctx>,
|
||||||
|
) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copy shape dimensions from an ndarray.
|
||||||
|
/// Panics if `ndims` mismatches.
|
||||||
|
pub fn copy_shape_from_ndarray<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
src_ndarray: NDArrayValue<'ctx>,
|
||||||
|
) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copy strides dimensions from an array.
|
||||||
|
pub fn copy_strides_from_array<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
strides: PointerValue<'ctx>,
|
||||||
|
) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copy strides dimensions from an ndarray.
|
||||||
|
/// Panics if `ndims` mismatches.
|
||||||
|
pub fn copy_strides_from_ndarray<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
src_ndarray: NDArrayValue<'ctx>,
|
||||||
|
) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the `np.size()` of this ndarray.
|
||||||
|
pub fn size<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the `ndarray.nbytes` of this ndarray.
|
||||||
|
pub fn nbytes<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the `len()` of this ndarray.
|
||||||
|
pub fn len<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if this ndarray is C-contiguous.
|
||||||
|
///
|
||||||
|
/// See NumPy's `flags["C_CONTIGUOUS"]`: <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flags.html#numpy.ndarray.flags>
|
||||||
|
pub fn is_c_contiguous<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Call [`call_nac3_ndarray_set_strides_by_shape`] on this ndarray to update `strides`.
|
||||||
|
///
|
||||||
|
/// Update the ndarray's strides to make the ndarray contiguous.
|
||||||
|
pub fn set_strides_contiguous<G: CodeGenerator + ?Sized>(
|
||||||
|
self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copy data from another ndarray.
|
||||||
|
///
|
||||||
|
/// This ndarray and `src` is that their `np.size()` should be the same. Their shapes
|
||||||
|
/// do not matter. The copying order is determined by how their flattened views look.
|
||||||
|
///
|
||||||
|
/// Panics if the `dtype`s of ndarrays are different.
|
||||||
|
pub fn copy_data_from<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
src: NDArrayValue<'ctx>,
|
||||||
|
) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ctx> ProxyValue<'ctx> for NDArrayValue<'ctx> {
|
impl<'ctx> ProxyValue<'ctx> for NDArrayValue<'ctx> {
|
||||||
|
|
Loading…
Reference in New Issue