Skip to content

Commit bdbedff

Browse files
committed
stage2: LLVM backend: properly set module target data
Also fix tripping LLVM assert having to do with 0 bit integers. stage2 behavior tests now run clean in a debug build of llvm 12.
1 parent ea6706b commit bdbedff

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/codegen/llvm.zig

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ pub const Object = struct {
264264
);
265265
errdefer target_machine.dispose();
266266

267+
const target_data = target_machine.createTargetDataLayout();
268+
defer target_data.dispose();
269+
270+
llvm_module.setModuleDataLayout(target_data);
271+
267272
return Object{
268273
.llvm_module = llvm_module,
269274
.context = context,
@@ -1589,13 +1594,17 @@ pub const FuncGen = struct {
15891594
return null;
15901595

15911596
const ty_op = self.air.instructions.items(.data)[inst].ty_op;
1592-
const operand = try self.resolveInst(ty_op.operand);
1593-
const array_len = self.air.typeOf(ty_op.operand).elemType().arrayLen();
1594-
const usize_llvm_ty = try self.dg.llvmType(Type.initTag(.usize));
1595-
const len = usize_llvm_ty.constInt(array_len, .False);
1597+
const operand_ty = self.air.typeOf(ty_op.operand);
1598+
const array_ty = operand_ty.childType();
1599+
const llvm_usize = try self.dg.llvmType(Type.usize);
1600+
const len = llvm_usize.constInt(array_ty.arrayLen(), .False);
15961601
const slice_llvm_ty = try self.dg.llvmType(self.air.typeOfIndex(inst));
1602+
if (!array_ty.hasCodeGenBits()) {
1603+
return self.builder.buildInsertValue(slice_llvm_ty.getUndef(), len, 1, "");
1604+
}
1605+
const operand = try self.resolveInst(ty_op.operand);
15971606
const indices: [2]*const llvm.Value = .{
1598-
usize_llvm_ty.constNull(), usize_llvm_ty.constNull(),
1607+
llvm_usize.constNull(), llvm_usize.constNull(),
15991608
};
16001609
const ptr = self.builder.buildInBoundsGEP(operand, &indices, indices.len, "");
16011610
const partial = self.builder.buildInsertValue(slice_llvm_ty.getUndef(), ptr, 0, "");
@@ -2454,12 +2463,12 @@ pub const FuncGen = struct {
24542463
}
24552464

24562465
fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
2457-
if (self.liveness.isUnused(inst))
2458-
return null;
2466+
if (self.liveness.isUnused(inst)) return null;
24592467
// buildAlloca expects the pointee type, not the pointer type, so assert that
24602468
// a Payload.PointerSimple is passed to the alloc instruction.
24612469
const ptr_ty = self.air.typeOfIndex(inst);
24622470
const pointee_type = ptr_ty.elemType();
2471+
if (!pointee_type.hasCodeGenBits()) return null;
24632472
const pointee_llvm_ty = try self.dg.llvmType(pointee_type);
24642473
const target = self.dg.module.getTarget();
24652474
const alloca_inst = self.buildAlloca(pointee_llvm_ty);

src/codegen/llvm/bindings.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ pub const Module = opaque {
219219
pub const verify = LLVMVerifyModule;
220220
extern fn LLVMVerifyModule(*const Module, Action: VerifierFailureAction, OutMessage: *[*:0]const u8) Bool;
221221

222+
pub const setModuleDataLayout = LLVMSetModuleDataLayout;
223+
extern fn LLVMSetModuleDataLayout(*const Module, *const TargetData) void;
224+
222225
pub const addFunction = LLVMAddFunction;
223226
extern fn LLVMAddFunction(*const Module, Name: [*:0]const u8, FunctionTy: *const Type) *const Value;
224227

@@ -766,6 +769,14 @@ pub const TargetMachine = opaque {
766769
llvm_ir_filename: ?[*:0]const u8,
767770
bitcode_filename: ?[*:0]const u8,
768771
) bool;
772+
773+
pub const createTargetDataLayout = LLVMCreateTargetDataLayout;
774+
extern fn LLVMCreateTargetDataLayout(*const TargetMachine) *const TargetData;
775+
};
776+
777+
pub const TargetData = opaque {
778+
pub const dispose = LLVMDisposeTargetData;
779+
extern fn LLVMDisposeTargetData(*const TargetData) void;
769780
};
770781

771782
pub const CodeModel = enum(c_int) {

src/type.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3868,6 +3868,7 @@ pub const Type = extern union {
38683868
};
38693869

38703870
pub const @"bool" = initTag(.bool);
3871+
pub const @"usize" = initTag(.usize);
38713872
pub const @"comptime_int" = initTag(.comptime_int);
38723873

38733874
pub fn ptr(arena: *Allocator, d: Payload.Pointer.Data) !Type {

0 commit comments

Comments
 (0)