From 536ed2146c0d1196bc3900c30be61b53306d44e8 Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 29 Nov 2024 17:19:43 +0800 Subject: [PATCH] [meta] Remove all mentions of build_int_cast build_int_cast performs signed extension or truncation depending on the source and target int lengths. This is usually not what we want - We want zero-extension instead. Replace all instances of build_int_cast with build_int_z_extend_or_bit_cast to fix this issue. --- nac3core/src/codegen/numpy.rs | 7 ++++--- nac3core/src/codegen/values/ndarray.rs | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/nac3core/src/codegen/numpy.rs b/nac3core/src/codegen/numpy.rs index 58869f2c4..c3631f564 100644 --- a/nac3core/src/codegen/numpy.rs +++ b/nac3core/src/codegen/numpy.rs @@ -800,7 +800,8 @@ fn ndarray_from_ndlist_impl<'ctx, G: CodeGenerator + ?Sized>( _ => { let lst_len = src_lst.load_size(ctx, None); let sizeof_elem = dst_arr.get_type().element_type().size_of().unwrap(); - let sizeof_elem = ctx.builder.build_int_cast(sizeof_elem, llvm_usize, "").unwrap(); + let sizeof_elem = + ctx.builder.build_int_z_extend_or_bit_cast(sizeof_elem, llvm_usize, "").unwrap(); let cpy_len = ctx .builder @@ -1207,7 +1208,7 @@ fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>( .build_int_mul( src_data_offset, ctx.builder - .build_int_cast(sizeof_elem, src_data_offset.get_type(), "") + .build_int_z_extend_or_bit_cast(sizeof_elem, src_data_offset.get_type(), "") .unwrap(), "", ) @@ -1220,7 +1221,7 @@ fn ndarray_sliced_copyto_impl<'ctx, G: CodeGenerator + ?Sized>( .build_int_mul( dst_data_offset, ctx.builder - .build_int_cast(sizeof_elem, dst_data_offset.get_type(), "") + .build_int_z_extend_or_bit_cast(sizeof_elem, dst_data_offset.get_type(), "") .unwrap(), "", ) diff --git a/nac3core/src/codegen/values/ndarray.rs b/nac3core/src/codegen/values/ndarray.rs index 38c6a9804..e3e225640 100644 --- a/nac3core/src/codegen/values/ndarray.rs +++ b/nac3core/src/codegen/values/ndarray.rs @@ -131,8 +131,10 @@ impl<'ctx> NDArrayValue<'ctx> { elem_ty: BasicTypeEnum<'ctx>, size: IntValue<'ctx>, ) { - let itemsize = - ctx.builder.build_int_cast(elem_ty.size_of().unwrap(), size.get_type(), "").unwrap(); + let itemsize = ctx + .builder + .build_int_z_extend_or_bit_cast(elem_ty.size_of().unwrap(), size.get_type(), "") + .unwrap(); let nbytes = ctx.builder.build_int_mul(size, itemsize, "").unwrap(); // TODO: What about alignment?