llvm backend: fix lowering of memset

The bitcast of ABI size 1 elements was problematic for some types.
This commit is contained in:
Andrew Kelley
2023-04-26 12:49:32 -07:00
parent fd6200eda6
commit 00b690540e
4 changed files with 141 additions and 92 deletions

View File

@@ -7939,11 +7939,15 @@ pub const FuncGen = struct {
return self.builder.buildPtrToInt(operand_ptr, dest_llvm_ty, "");
}
fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !?*llvm.Value {
fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) !*llvm.Value {
const ty_op = self.air.instructions.items(.data)[inst].ty_op;
const operand_ty = self.air.typeOf(ty_op.operand);
const inst_ty = self.air.typeOfIndex(inst);
const operand = try self.resolveInst(ty_op.operand);
return self.bitCast(operand, operand_ty, inst_ty);
}
fn bitCast(self: *FuncGen, operand: *llvm.Value, operand_ty: Type, inst_ty: Type) !*llvm.Value {
const operand_is_ref = isByRef(operand_ty);
const result_is_ref = isByRef(inst_ty);
const llvm_dest_ty = try self.dg.lowerType(inst_ty);
@@ -7954,6 +7958,12 @@ pub const FuncGen = struct {
return operand;
}
if (llvm_dest_ty.getTypeKind() == .Integer and
operand.typeOf().getTypeKind() == .Integer)
{
return self.builder.buildZExtOrBitCast(operand, llvm_dest_ty, "");
}
if (operand_ty.zigTypeTag() == .Int and inst_ty.isPtrAtRuntime()) {
return self.builder.buildIntToPtr(operand, llvm_dest_ty, "");
}
@@ -8442,7 +8452,7 @@ pub const FuncGen = struct {
if (elem_abi_size == 1) {
// In this case we can take advantage of LLVM's intrinsic.
const fill_byte = self.builder.buildBitCast(value, u8_llvm_ty, "");
const fill_byte = try self.bitCast(value, elem_ty, Type.u8);
const len = self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
_ = self.builder.buildMemSet(dest_ptr, fill_byte, len, dest_ptr_align, ptr_ty.isVolatilePtr());
return null;