forked from M-Labs/nac3
1
0
Fork 0

core: Remove ArrayValue variants of functions

These will be lowered and optimized away later anyways, and we have
ArrayLikeAccessor now.
This commit is contained in:
David Mak 2024-03-22 16:57:36 +08:00
parent 26a01b14d5
commit 5ba8601b39
3 changed files with 27 additions and 83 deletions

View File

@ -5,6 +5,7 @@ use crate::{
classes::{ classes::{
ArrayLikeIndexer, ArrayLikeIndexer,
ArrayLikeValue, ArrayLikeValue,
ArraySliceValue,
ListValue, ListValue,
NDArrayValue, NDArrayValue,
RangeValue, RangeValue,
@ -1265,12 +1266,14 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
} else { } else {
return Ok(None) return Ok(None)
}; };
let index_addr = generator.gen_var_alloc(ctx, index.get_type().into(), None)?;
ctx.builder.build_store(index_addr, index).unwrap();
Ok(Some(v.data() Ok(Some(v.data()
.get( .get(
ctx, ctx,
generator, generator,
ctx.ctx.i32_type().const_array(&[index]), ArraySliceValue::from_ptr_val(index_addr, llvm_usize.const_int(1, false), None),
None, None,
) )
.into())) .into()))
@ -1286,6 +1289,8 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
} else { } else {
return Ok(None) return Ok(None)
}; };
let index_addr = generator.gen_var_alloc(ctx, index.get_type().into(), None)?;
ctx.builder.build_store(index_addr, index).unwrap();
// Create a new array, remove the top dimension from the dimension-size-list, and copy the // Create a new array, remove the top dimension from the dimension-size-list, and copy the
// elements over // elements over
@ -1340,7 +1345,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
let v_data_src_ptr = v.data().ptr_offset( let v_data_src_ptr = v.data().ptr_offset(
ctx, ctx,
generator, generator,
ctx.ctx.i32_type().const_array(&[index]), ArraySliceValue::from_ptr_val(index_addr, llvm_usize.const_int(1, false), None),
None None
); );
call_memcpy_generic( call_memcpy_generic(

View File

@ -8,7 +8,6 @@ use super::{
ListValue, ListValue,
NDArrayValue, NDArrayValue,
TypedArrayLikeAdapter, TypedArrayLikeAdapter,
UntypedArrayLikeMutator,
}, },
CodeGenContext, CodeGenContext,
CodeGenerator, CodeGenerator,
@ -19,7 +18,7 @@ use inkwell::{
memory_buffer::MemoryBuffer, memory_buffer::MemoryBuffer,
module::Module, module::Module,
types::{BasicTypeEnum, IntType}, types::{BasicTypeEnum, IntType},
values::{ArrayValue, BasicValueEnum, CallSiteValue, FloatValue, IntValue}, values::{BasicValueEnum, CallSiteValue, FloatValue, IntValue},
AddressSpace, IntPredicate, AddressSpace, IntPredicate,
}; };
use itertools::Either; use itertools::Either;
@ -785,46 +784,3 @@ pub fn call_ndarray_flatten_index<'ctx, G, Index>(
indices, indices,
) )
} }
/// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the
/// multidimensional index.
///
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
/// `NDArray`.
/// * `indices` - The multidimensional index to compute the flattened index for.
pub fn call_ndarray_flatten_index_const<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>,
indices: ArrayValue<'ctx>,
) -> IntValue<'ctx> {
let llvm_usize = generator.get_size_type(ctx.ctx);
let indices_size = indices.get_type().len();
let indices_alloca = generator.gen_array_var_alloc(
ctx,
indices.get_type().get_element_type(),
llvm_usize.const_int(indices_size as u64, false),
None,
).unwrap();
for i in 0..indices_size {
let v = ctx.builder.build_extract_value(indices, i, "")
.unwrap()
.into_int_value();
unsafe {
indices_alloca.set_unchecked(
ctx,
generator,
ctx.ctx.i32_type().const_int(i as u64, false),
v.into(),
);
}
}
call_ndarray_flatten_index_impl(
generator,
ctx,
ndarray,
&indices_alloca,
)
}

View File

@ -1,7 +1,7 @@
use inkwell::{ use inkwell::{
IntPredicate, IntPredicate,
types::BasicType, types::BasicType,
values::{AggregateValueEnum, ArrayValue, BasicValueEnum, IntValue, PointerValue} values::{BasicValueEnum, IntValue, PointerValue}
}; };
use nac3parser::ast::StrRef; use nac3parser::ast::StrRef;
use crate::{ use crate::{
@ -140,12 +140,12 @@ fn create_ndarray_dyn_shape<'ctx, 'a, G, V, LenFn, DataFn>(
/// Creates an `NDArray` instance from a constant shape. /// Creates an `NDArray` instance from a constant shape.
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
/// * `shape` - The shape of the `NDArray`, represented as an LLVM [`ArrayValue`]. /// * `shape` - The shape of the `NDArray`, represented am array of [`IntValue`]s.
fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>( fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
shape: ArrayValue<'ctx> shape: &[IntValue<'ctx>],
) -> Result<NDArrayValue<'ctx>, String> { ) -> Result<NDArrayValue<'ctx>, String> {
let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None); let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None);
@ -156,14 +156,9 @@ fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>(
let llvm_ndarray_data_t = ctx.get_llvm_type(generator, elem_ty).as_basic_type_enum(); let llvm_ndarray_data_t = ctx.get_llvm_type(generator, elem_ty).as_basic_type_enum();
assert!(llvm_ndarray_data_t.is_sized()); assert!(llvm_ndarray_data_t.is_sized());
for i in 0..shape.get_type().len() { for shape_dim in shape {
let shape_dim = ctx.builder
.build_extract_value(shape, i, "")
.map(BasicValueEnum::into_int_value)
.unwrap();
let shape_dim_gez = ctx.builder let shape_dim_gez = ctx.builder
.build_int_compare(IntPredicate::SGE, shape_dim, llvm_usize.const_zero(), "") .build_int_compare(IntPredicate::SGE, *shape_dim, llvm_usize.const_zero(), "")
.unwrap(); .unwrap();
ctx.make_assert( ctx.make_assert(
@ -183,21 +178,20 @@ fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>(
)?; )?;
let ndarray = NDArrayValue::from_ptr_val(ndarray, llvm_usize, None); let ndarray = NDArrayValue::from_ptr_val(ndarray, llvm_usize, None);
let num_dims = llvm_usize.const_int(shape.get_type().len() as u64, false); let num_dims = llvm_usize.const_int(shape.len() as u64, false);
ndarray.store_ndims(ctx, generator, num_dims); ndarray.store_ndims(ctx, generator, num_dims);
let ndarray_num_dims = ndarray.load_ndims(ctx); let ndarray_num_dims = ndarray.load_ndims(ctx);
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims); ndarray.create_dim_sizes(ctx, llvm_usize, ndarray_num_dims);
for i in 0..shape.get_type().len() { for (i, shape_dim) in shape.iter().enumerate() {
let ndarray_dim = ndarray let ndarray_dim = unsafe {
ndarray
.dim_sizes() .dim_sizes()
.ptr_offset(ctx, generator, llvm_usize.const_int(i as u64, true), None); .ptr_offset_unchecked(ctx, generator, llvm_usize.const_int(i as u64, true), None)
let shape_dim = ctx.builder.build_extract_value(shape, i, "") };
.map(BasicValueEnum::into_int_value)
.unwrap();
ctx.builder.build_store(ndarray_dim, shape_dim).unwrap(); ctx.builder.build_store(ndarray_dim, *shape_dim).unwrap();
} }
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
@ -473,27 +467,16 @@ fn call_ndarray_eye_impl<'ctx, G: CodeGenerator + ?Sized>(
) -> Result<NDArrayValue<'ctx>, String> { ) -> Result<NDArrayValue<'ctx>, String> {
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_usize_2 = llvm_usize.array_type(2);
let shape_addr = generator.gen_var_alloc(ctx, llvm_usize_2.into(), None)?;
let shape = ctx.builder.build_load(shape_addr, "")
.map(BasicValueEnum::into_array_value)
.unwrap();
let nrows = ctx.builder.build_int_z_extend_or_bit_cast(nrows, llvm_usize, "").unwrap(); let nrows = ctx.builder.build_int_z_extend_or_bit_cast(nrows, llvm_usize, "").unwrap();
let shape = ctx.builder
.build_insert_value(shape, nrows, 0, "")
.map(AggregateValueEnum::into_array_value)
.unwrap();
let ncols = ctx.builder.build_int_z_extend_or_bit_cast(ncols, llvm_usize, "").unwrap(); let ncols = ctx.builder.build_int_z_extend_or_bit_cast(ncols, llvm_usize, "").unwrap();
let shape = ctx.builder
.build_insert_value(shape, ncols, 1, "")
.map(AggregateValueEnum::into_array_value)
.unwrap();
let ndarray = create_ndarray_const_shape(generator, ctx, elem_ty, shape)?; let ndarray = create_ndarray_const_shape(
generator,
ctx,
elem_ty,
&[nrows, ncols],
)?;
ndarray_fill_indexed( ndarray_fill_indexed(
generator, generator,