authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-26 13:04:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-28 13:24:42-07:00
log51adbf472bcf9eacc0099e39778a6f9177fea023
tree6371f7a0419042c3721bfcbfc1e594fdaafdcad6
parent00b690540e561391d17c65e45f818db6be8fecec

llvm backend: fix memset with byref element value


1 files changed, 19 insertions(+), 4 deletions(-)

src/codegen/llvm.zig+19-4
...@@ -8429,6 +8429,7 @@ pub const FuncGen = struct {...@@ -8429,6 +8429,7 @@ pub const FuncGen = struct {
8429 const dest_ptr_align = ptr_ty.ptrAlignment(target);8429 const dest_ptr_align = ptr_ty.ptrAlignment(target);
8430 const u8_llvm_ty = self.context.intType(8);8430 const u8_llvm_ty = self.context.intType(8);
8431 const dest_ptr = self.sliceOrArrayPtr(dest_slice, ptr_ty);8431 const dest_ptr = self.sliceOrArrayPtr(dest_slice, ptr_ty);
8432 const is_volatile = ptr_ty.isVolatilePtr();
84328433
8433 if (val_is_undef) {8434 if (val_is_undef) {
8434 // Even if safety is disabled, we still emit a memset to undefined since it conveys8435 // Even if safety is disabled, we still emit a memset to undefined since it conveys
...@@ -8439,7 +8440,7 @@ pub const FuncGen = struct {...@@ -8439,7 +8440,7 @@ pub const FuncGen = struct {
8439 else8440 else
8440 u8_llvm_ty.getUndef();8441 u8_llvm_ty.getUndef();
8441 const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);8442 const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
8442 _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, ptr_ty.isVolatilePtr());8443 _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile);
84438444
8444 if (safety and self.dg.module.comp.bin_file.options.valgrind) {8445 if (safety and self.dg.module.comp.bin_file.options.valgrind) {
8445 self.valgrindMarkUndef(dest_ptr, len);8446 self.valgrindMarkUndef(dest_ptr, len);
...@@ -8454,7 +8455,7 @@ pub const FuncGen = struct {...@@ -8454,7 +8455,7 @@ pub const FuncGen = struct {
8454 // In this case we can take advantage of LLVM's intrinsic.8455 // In this case we can take advantage of LLVM's intrinsic.
8455 const fill_byte = try self.bitCast(value, elem_ty, Type.u8);8456 const fill_byte = try self.bitCast(value, elem_ty, Type.u8);
8456 const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);8457 const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
8457 _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, ptr_ty.isVolatilePtr());8458 _ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, is_volatile);
8458 return null;8459 return null;
8459 }8460 }
84608461
...@@ -8496,8 +8497,22 @@ pub const FuncGen = struct {...@@ -8496,8 +8497,22 @@ pub const FuncGen = struct {
8496 _ = self.builder.buildCondBr(end, body_block, end_block);8497 _ = self.builder.buildCondBr(end, body_block, end_block);
84978498
8498 self.builder.positionBuilderAtEnd(body_block);8499 self.builder.positionBuilderAtEnd(body_block);
8499 const store_inst = self.builder.buildStore(value, it_ptr);8500 const elem_abi_alignment = elem_ty.abiAlignment(target);
8500 store_inst.setAlignment(@min(elem_ty.abiAlignment(target), dest_ptr_align));8501 const it_ptr_alignment = @min(elem_abi_alignment, dest_ptr_align);
8502 if (isByRef(elem_ty)) {
8503 _ = self.builder.buildMemCpy(
8504 it_ptr,
8505 it_ptr_alignment,
8506 value,
8507 elem_abi_alignment,
8508 llvm_usize_ty.constInt(elem_abi_size, .False),
8509 is_volatile,
8510 );
8511 } else {
8512 const store_inst = self.builder.buildStore(value, it_ptr);
8513 store_inst.setAlignment(it_ptr_alignment);
8514 store_inst.setVolatile(llvm.Bool.fromBool(is_volatile));
8515 }
8501 const one_gep = [_]*llvm.Value{llvm_usize_ty.constInt(1, .False)};8516 const one_gep = [_]*llvm.Value{llvm_usize_ty.constInt(1, .False)};
8502 const next_ptr = self.builder.buildInBoundsGEP(elem_llvm_ty, it_ptr, &one_gep, one_gep.len, "");8517 const next_ptr = self.builder.buildInBoundsGEP(elem_llvm_ty, it_ptr, &one_gep, one_gep.len, "");
8503 _ = self.builder.buildBr(loop_block);8518 _ = self.builder.buildBr(loop_block);