authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-04 17:32:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-04 17:32:46-07:00
logac5c6b6061c6454a891c22a8258069370b57c05b
tree703979074915bba1b28d2ecf4b742df2fa9a1031
parent169ad1aac745857965db8fe7565aa45b8d8cea0f

stage2 LLVM backend: opaque pointer fixes


2 files changed, 80 insertions(+), 43 deletions(-)

src/codegen/llvm.zig+75-43
......@@ -2523,16 +2523,7 @@ pub const DeclGen = struct {
25232523 if (ptr_info.host_size != 0) {
25242524 return dg.context.intType(ptr_info.host_size * 8).pointerType(llvm_addrspace);
25252525 }
2526 const elem_ty = ptr_info.pointee_type;
2527 const lower_elem_ty = switch (elem_ty.zigTypeTag()) {
2528 .Opaque, .Fn => true,
2529 .Array => elem_ty.childType().hasRuntimeBitsIgnoreComptime(),
2530 else => elem_ty.hasRuntimeBitsIgnoreComptime(),
2531 };
2532 const llvm_elem_ty = if (lower_elem_ty)
2533 try dg.lowerType(elem_ty)
2534 else
2535 dg.context.intType(8);
2526 const llvm_elem_ty = try dg.lowerPtrElemTy(ptr_info.pointee_type);
25362527 return llvm_elem_ty.pointerType(llvm_addrspace);
25372528 },
25382529 .Opaque => switch (t.tag()) {
......@@ -2917,6 +2908,23 @@ pub const DeclGen = struct {
29172908 );
29182909 }
29192910
2911 /// Use this instead of lowerType when you want to handle correctly the case of elem_ty
2912 /// being a zero bit type, but it should still be lowered as an i8 in such case.
2913 /// There are other similar cases handled here as well.
2914 fn lowerPtrElemTy(dg: *DeclGen, elem_ty: Type) Allocator.Error!*const llvm.Type {
2915 const lower_elem_ty = switch (elem_ty.zigTypeTag()) {
2916 .Opaque, .Fn => true,
2917 .Array => elem_ty.childType().hasRuntimeBitsIgnoreComptime(),
2918 else => elem_ty.hasRuntimeBitsIgnoreComptime(),
2919 };
2920 const llvm_elem_ty = if (lower_elem_ty)
2921 try dg.lowerType(elem_ty)
2922 else
2923 dg.context.intType(8);
2924
2925 return llvm_elem_ty;
2926 }
2927
29202928 fn lowerValue(dg: *DeclGen, tv: TypedValue) Error!*const llvm.Value {
29212929 if (tv.val.isUndef()) {
29222930 const llvm_type = try dg.lowerType(tv.ty);
......@@ -4674,8 +4682,8 @@ pub const FuncGen = struct {
46744682 _ = self.builder.buildBr(end_block);
46754683
46764684 self.builder.positionBuilderAtEnd(both_pl_block);
4677 const lhs_payload = self.optPayloadHandle(opt_llvm_ty, lhs, scalar_ty);
4678 const rhs_payload = self.optPayloadHandle(opt_llvm_ty, rhs, scalar_ty);
4685 const lhs_payload = try self.optPayloadHandle(opt_llvm_ty, lhs, scalar_ty);
4686 const rhs_payload = try self.optPayloadHandle(opt_llvm_ty, rhs, scalar_ty);
46794687 const payload_cmp = try self.cmp(lhs_payload, rhs_payload, payload_ty, op);
46804688 _ = self.builder.buildBr(end_block);
46814689 const both_pl_block_end = self.builder.getInsertBlock();
......@@ -5133,7 +5141,8 @@ pub const FuncGen = struct {
51335141
51345142 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
51355143 const slice_ptr = try self.resolveInst(ty_op.operand);
5136 const slice_llvm_ty = try self.dg.lowerType(self.air.typeOf(ty_op.operand).childType());
5144 const slice_ptr_ty = self.air.typeOf(ty_op.operand);
5145 const slice_llvm_ty = try self.dg.lowerPtrElemTy(slice_ptr_ty.childType());
51375146
51385147 return self.builder.buildStructGEP(slice_llvm_ty, slice_ptr, index, "");
51395148 }
......@@ -5145,7 +5154,7 @@ pub const FuncGen = struct {
51455154
51465155 const slice = try self.resolveInst(bin_op.lhs);
51475156 const index = try self.resolveInst(bin_op.rhs);
5148 const llvm_elem_ty = try self.dg.lowerType(slice_ty.childType());
5157 const llvm_elem_ty = try self.dg.lowerPtrElemTy(slice_ty.childType());
51495158 const base_ptr = self.builder.buildExtractValue(slice, 0, "");
51505159 const indices: [1]*const llvm.Value = .{index};
51515160 const ptr = self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, "");
......@@ -5160,7 +5169,7 @@ pub const FuncGen = struct {
51605169
51615170 const slice = try self.resolveInst(bin_op.lhs);
51625171 const index = try self.resolveInst(bin_op.rhs);
5163 const llvm_elem_ty = try self.dg.lowerType(slice_ty.childType());
5172 const llvm_elem_ty = try self.dg.lowerPtrElemTy(slice_ty.childType());
51645173 const base_ptr = self.builder.buildExtractValue(slice, 0, "");
51655174 const indices: [1]*const llvm.Value = .{index};
51665175 return self.builder.buildInBoundsGEP(llvm_elem_ty, base_ptr, &indices, indices.len, "");
......@@ -5195,9 +5204,10 @@ pub const FuncGen = struct {
51955204 const ptr_ty = self.air.typeOf(bin_op.lhs);
51965205 if (!ptr_ty.isVolatilePtr() and self.liveness.isUnused(inst)) return null;
51975206
5198 const llvm_elem_ty = try self.dg.lowerType(ptr_ty.childType());
5207 const llvm_elem_ty = try self.dg.lowerPtrElemTy(ptr_ty.childType());
51995208 const base_ptr = try self.resolveInst(bin_op.lhs);
52005209 const rhs = try self.resolveInst(bin_op.rhs);
5210 // TODO: when we go fully opaque pointers in LLVM 16 we can remove this branch
52015211 const ptr = if (ptr_ty.isSinglePointer()) ptr: {
52025212 // If this is a single-item pointer to an array, we need another index in the GEP.
52035213 const indices: [2]*const llvm.Value = .{ self.context.intType(32).constNull(), rhs };
......@@ -5220,7 +5230,7 @@ pub const FuncGen = struct {
52205230
52215231 const base_ptr = try self.resolveInst(bin_op.lhs);
52225232 const rhs = try self.resolveInst(bin_op.rhs);
5223 const llvm_elem_ty = try self.dg.lowerType(elem_ty);
5233 const llvm_elem_ty = try self.dg.lowerPtrElemTy(elem_ty);
52245234 if (ptr_ty.isSinglePointer()) {
52255235 // If this is a single-item pointer to an array, we need another index in the GEP.
52265236 const indices: [2]*const llvm.Value = .{ self.context.intType(32).constNull(), rhs };
......@@ -5555,7 +5565,9 @@ pub const FuncGen = struct {
55555565 const max_param_count = inputs.len + outputs.len;
55565566 const llvm_param_types = try arena.alloc(*const llvm.Type, max_param_count);
55575567 const llvm_param_values = try arena.alloc(*const llvm.Value, max_param_count);
5558 const llvm_param_attrs = try arena.alloc(bool, max_param_count);
5568 // This stores whether we need to add an elementtype attribute and
5569 // if so, the element type itself.
5570 const llvm_param_attrs = try arena.alloc(?*const llvm.Type, max_param_count);
55595571 const target = self.dg.module.getTarget();
55605572
55615573 var llvm_ret_i: usize = 0;
......@@ -5573,7 +5585,7 @@ pub const FuncGen = struct {
55735585 // for the string, we still use the next u32 for the null terminator.
55745586 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
55755587
5576 try llvm_constraints.ensureUnusedCapacity(self.gpa, constraint.len + 1);
5588 try llvm_constraints.ensureUnusedCapacity(self.gpa, constraint.len + 3);
55775589 if (total_i != 0) {
55785590 llvm_constraints.appendAssumeCapacity(',');
55795591 }
......@@ -5582,8 +5594,10 @@ pub const FuncGen = struct {
55825594 // Pass any non-return outputs indirectly, if the constraint accepts a memory location
55835595 llvm_ret_indirect[i] = (output != .none) and constraintAllowsMemory(constraint);
55845596 if (output != .none) {
5585 try llvm_constraints.ensureUnusedCapacity(self.gpa, llvm_constraints.capacity + 1);
55865597 const output_inst = try self.resolveInst(output);
5598 const output_ty = self.air.typeOf(output);
5599 assert(output_ty.zigTypeTag() == .Pointer);
5600 const elem_llvm_ty = try self.dg.lowerPtrElemTy(output_ty.childType());
55875601
55885602 if (llvm_ret_indirect[i]) {
55895603 // Pass the result by reference as an indirect output (e.g. "=*m")
......@@ -5591,11 +5605,11 @@ pub const FuncGen = struct {
55915605
55925606 llvm_param_values[llvm_param_i] = output_inst;
55935607 llvm_param_types[llvm_param_i] = output_inst.typeOf();
5594 llvm_param_attrs[llvm_param_i] = true;
5608 llvm_param_attrs[llvm_param_i] = elem_llvm_ty;
55955609 llvm_param_i += 1;
55965610 } else {
55975611 // Pass the result directly (e.g. "=r")
5598 llvm_ret_types[llvm_ret_i] = output_inst.typeOf().getElementType();
5612 llvm_ret_types[llvm_ret_i] = elem_llvm_ty;
55995613 llvm_ret_i += 1;
56005614 }
56015615 } else {
......@@ -5633,7 +5647,9 @@ pub const FuncGen = struct {
56335647
56345648 const arg_llvm_value = try self.resolveInst(input);
56355649 const arg_ty = self.air.typeOf(input);
5650 var llvm_elem_ty: ?*const llvm.Type = null;
56365651 if (isByRef(arg_ty)) {
5652 llvm_elem_ty = try self.dg.lowerPtrElemTy(arg_ty);
56375653 if (constraintAllowsMemory(constraint)) {
56385654 llvm_param_values[llvm_param_i] = arg_llvm_value;
56395655 llvm_param_types[llvm_param_i] = arg_llvm_value.typeOf();
......@@ -5677,7 +5693,12 @@ pub const FuncGen = struct {
56775693
56785694 // In the case of indirect inputs, LLVM requires the callsite to have
56795695 // an elementtype(<ty>) attribute.
5680 llvm_param_attrs[llvm_param_i] = constraint[0] == '*';
5696 if (constraint[0] == '*') {
5697 llvm_param_attrs[llvm_param_i] = llvm_elem_ty orelse
5698 try self.dg.lowerPtrElemTy(arg_ty.childType());
5699 } else {
5700 llvm_param_attrs[llvm_param_i] = null;
5701 }
56815702
56825703 llvm_param_i += 1;
56835704 total_i += 1;
......@@ -5812,10 +5833,9 @@ pub const FuncGen = struct {
58125833 .Auto,
58135834 "",
58145835 );
5815 for (llvm_param_attrs[0..param_count]) |need_elem_ty, i| {
5816 if (need_elem_ty) {
5817 const elem_ty = llvm_param_types[i].getElementType();
5818 llvm.setCallElemTypeAttr(call, i, elem_ty);
5836 for (llvm_param_attrs[0..param_count]) |llvm_elem_ty, i| {
5837 if (llvm_elem_ty) |llvm_ty| {
5838 llvm.setCallElemTypeAttr(call, i, llvm_ty);
58195839 }
58205840 }
58215841
......@@ -6558,7 +6578,7 @@ pub const FuncGen = struct {
65586578 const base_ptr = try self.resolveInst(bin_op.lhs);
65596579 const offset = try self.resolveInst(bin_op.rhs);
65606580 const ptr_ty = self.air.typeOf(bin_op.lhs);
6561 const llvm_elem_ty = try self.dg.lowerType(ptr_ty.childType());
6581 const llvm_elem_ty = try self.dg.lowerPtrElemTy(ptr_ty.childType());
65626582 if (ptr_ty.ptrSize() == .One) {
65636583 // It's a pointer to an array, so according to LLVM we need an extra GEP index.
65646584 const indices: [2]*const llvm.Value = .{
......@@ -6580,7 +6600,7 @@ pub const FuncGen = struct {
65806600 const offset = try self.resolveInst(bin_op.rhs);
65816601 const negative_offset = self.builder.buildNeg(offset, "");
65826602 const ptr_ty = self.air.typeOf(bin_op.lhs);
6583 const llvm_elem_ty = try self.dg.lowerType(ptr_ty.childType());
6603 const llvm_elem_ty = try self.dg.lowerPtrElemTy(ptr_ty.childType());
65846604 if (ptr_ty.ptrSize() == .One) {
65856605 // It's a pointer to an array, so according to LLVM we need an extra GEP index.
65866606 const indices: [2]*const llvm.Value = .{
......@@ -7564,7 +7584,7 @@ pub const FuncGen = struct {
75647584 if (self.liveness.isUnused(inst)) return null;
75657585
75667586 const llvm_i32 = self.context.intType(32);
7567 const llvm_fn_name = "llvm.frameaddress.p0i8";
7587 const llvm_fn_name = "llvm.frameaddress.p0";
75687588 const llvm_fn = self.dg.object.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: {
75697589 const llvm_p0i8 = self.context.intType(8).pointerType(0);
75707590 const param_types = [_]*const llvm.Type{llvm_i32};
......@@ -7694,20 +7714,31 @@ pub const FuncGen = struct {
76947714 const atomic_load = self.air.instructions.items(.data)[inst].atomic_load;
76957715 const ptr = try self.resolveInst(atomic_load.ptr);
76967716 const ptr_ty = self.air.typeOf(atomic_load.ptr);
7697 if (!ptr_ty.isVolatilePtr() and self.liveness.isUnused(inst))
7717 const ptr_info = ptr_ty.ptrInfo().data;
7718 if (!ptr_info.@"volatile" and self.liveness.isUnused(inst))
7719 return null;
7720 const elem_ty = ptr_info.pointee_type;
7721 if (!elem_ty.hasRuntimeBitsIgnoreComptime())
76987722 return null;
76997723 const ordering = toLlvmAtomicOrdering(atomic_load.order);
7700 const operand_ty = ptr_ty.elemType();
7701 const opt_abi_ty = self.dg.getAtomicAbiType(operand_ty, false);
7724 const opt_abi_llvm_ty = self.dg.getAtomicAbiType(elem_ty, false);
7725 const target = self.dg.module.getTarget();
7726 const ptr_alignment = ptr_info.alignment(target);
7727 const ptr_volatile = llvm.Bool.fromBool(ptr_info.@"volatile");
7728 const elem_llvm_ty = try self.dg.lowerType(elem_ty);
77027729
7703 if (opt_abi_ty) |abi_ty| {
7730 if (opt_abi_llvm_ty) |abi_llvm_ty| {
77047731 // operand needs widening and truncating
7705 const casted_ptr = self.builder.buildBitCast(ptr, abi_ty.pointerType(0), "");
7706 const load_inst = (try self.load(casted_ptr, ptr_ty)).?;
7732 const casted_ptr = self.builder.buildBitCast(ptr, abi_llvm_ty.pointerType(0), "");
7733 const load_inst = self.builder.buildLoad(abi_llvm_ty, casted_ptr, "");
7734 load_inst.setAlignment(ptr_alignment);
7735 load_inst.setVolatile(ptr_volatile);
77077736 load_inst.setOrdering(ordering);
7708 return self.builder.buildTrunc(load_inst, try self.dg.lowerType(operand_ty), "");
7737 return self.builder.buildTrunc(load_inst, elem_llvm_ty, "");
77097738 }
7710 const load_inst = (try self.load(ptr, ptr_ty)).?;
7739 const load_inst = self.builder.buildLoad(elem_llvm_ty, ptr, "");
7740 load_inst.setAlignment(ptr_alignment);
7741 load_inst.setVolatile(ptr_volatile);
77117742 load_inst.setOrdering(ordering);
77127743 return load_inst;
77137744 }
......@@ -8508,7 +8539,7 @@ pub const FuncGen = struct {
85088539 const llvm_ptr_u8 = llvm_u8.pointerType(0);
85098540 const llvm_u32 = self.context.intType(32);
85108541
8511 const llvm_fn_name = "llvm.prefetch.p0i8";
8542 const llvm_fn_name = "llvm.prefetch.p0";
85128543 const fn_val = self.dg.object.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: {
85138544 // declare void @llvm.prefetch(i8*, i32, i32, i32)
85148545 const llvm_void = self.context.voidType();
......@@ -8660,7 +8691,7 @@ pub const FuncGen = struct {
86608691 opt_llvm_ty: *const llvm.Type,
86618692 opt_handle: *const llvm.Value,
86628693 opt_ty: Type,
8663 ) *const llvm.Value {
8694 ) !*const llvm.Value {
86648695 var buf: Type.Payload.ElemType = undefined;
86658696 const payload_ty = opt_ty.optionalChild(&buf);
86668697
......@@ -8673,7 +8704,8 @@ pub const FuncGen = struct {
86738704 }
86748705 const target = fg.dg.module.getTarget();
86758706 const payload_alignment = payload_ty.abiAlignment(target);
8676 const load_inst = fg.builder.buildLoad(payload_ptr.getGEPResultElementType(), payload_ptr, "");
8707 const payload_llvm_ty = try fg.dg.lowerType(payload_ty);
8708 const load_inst = fg.builder.buildLoad(payload_llvm_ty, payload_ptr, "");
86778709 load_inst.setAlignment(payload_alignment);
86788710 return load_inst;
86798711 }
......@@ -8758,7 +8790,7 @@ pub const FuncGen = struct {
87588790 return self.builder.buildBitCast(new_ptr, result_llvm_ty, "");
87598791 },
87608792 else => {
8761 const struct_llvm_ty = try self.dg.lowerType(struct_ty);
8793 const struct_llvm_ty = try self.dg.lowerPtrElemTy(struct_ty);
87628794
87638795 var ty_buf: Type.Payload.Pointer = undefined;
87648796 if (llvmFieldIndex(struct_ty, field_index, target, &ty_buf)) |llvm_field_index| {
......@@ -8815,7 +8847,7 @@ pub const FuncGen = struct {
88158847 if (!info.pointee_type.hasRuntimeBitsIgnoreComptime()) return null;
88168848
88178849 const target = self.dg.module.getTarget();
8818 const ptr_alignment = ptr_ty.ptrAlignment(target);
8850 const ptr_alignment = info.alignment(target);
88198851 const ptr_volatile = llvm.Bool.fromBool(ptr_ty.isVolatilePtr());
88208852 if (info.host_size == 0) {
88218853 const elem_llvm_ty = try self.dg.lowerType(info.pointee_type);
src/type.zig+5
......@@ -6229,6 +6229,11 @@ pub const Type = extern union {
62296229 mutable: bool = true, // TODO rename this to const, not mutable
62306230 @"volatile": bool = false,
62316231 size: std.builtin.Type.Pointer.Size = .One,
6232
6233 pub fn alignment(data: Data, target: Target) u32 {
6234 if (data.@"align" != 0) return data.@"align";
6235 return abiAlignment(data.pointee_type, target);
6236 }
62326237 };
62336238 };
62346239