authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-07-07 19:19:30+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-07-08 02:50:15+02:00
log9e49652757da1946abd57a1a3af15a7bbcf8491c
treec5ed56602703d02fb27bf1abae9edf30572301a3
parent31e46be74360f7bf6509bd8826690318974175c0

llvm: Revert #17963 (workaround for #16392)


1 files changed, 25 insertions(+), 35 deletions(-)

src/codegen/llvm.zig+25-35
...@@ -5281,7 +5281,7 @@ pub const FuncGen = struct {...@@ -5281,7 +5281,7 @@ pub const FuncGen = struct {
5281 try attributes.addParamAttr(0, .{ .sret = llvm_ret_ty }, &o.builder);5281 try attributes.addParamAttr(0, .{ .sret = llvm_ret_ty }, &o.builder);
52825282
5283 const alignment = return_type.abiAlignment(zcu).toLlvm();5283 const alignment = return_type.abiAlignment(zcu).toLlvm();
5284 const ret_ptr = try self.buildAllocaWorkaround(return_type, alignment);5284 const ret_ptr = try self.buildAlloca(llvm_ret_ty, alignment);
5285 try llvm_args.append(ret_ptr);5285 try llvm_args.append(ret_ptr);
5286 break :blk ret_ptr;5286 break :blk ret_ptr;
5287 };5287 };
...@@ -5329,7 +5329,7 @@ pub const FuncGen = struct {...@@ -5329,7 +5329,7 @@ pub const FuncGen = struct {
53295329
5330 const alignment = param_ty.abiAlignment(zcu).toLlvm();5330 const alignment = param_ty.abiAlignment(zcu).toLlvm();
5331 const param_llvm_ty = try o.lowerType(param_ty);5331 const param_llvm_ty = try o.lowerType(param_ty);
5332 const arg_ptr = try self.buildAllocaWorkaround(param_ty, alignment);5332 const arg_ptr = try self.buildAlloca(param_llvm_ty, alignment);
5333 if (isByRef(param_ty, zcu)) {5333 if (isByRef(param_ty, zcu)) {
5334 const loaded = try self.wip.load(.normal, param_llvm_ty, llvm_arg, alignment, "");5334 const loaded = try self.wip.load(.normal, param_llvm_ty, llvm_arg, alignment, "");
5335 _ = try self.wip.store(.normal, loaded, arg_ptr, alignment);5335 _ = try self.wip.store(.normal, loaded, arg_ptr, alignment);
...@@ -5352,7 +5352,7 @@ pub const FuncGen = struct {...@@ -5352,7 +5352,7 @@ pub const FuncGen = struct {
5352 // LLVM does not allow bitcasting structs so we must allocate5352 // LLVM does not allow bitcasting structs so we must allocate
5353 // a local, store as one type, and then load as another type.5353 // a local, store as one type, and then load as another type.
5354 const alignment = param_ty.abiAlignment(zcu).toLlvm();5354 const alignment = param_ty.abiAlignment(zcu).toLlvm();
5355 const int_ptr = try self.buildAllocaWorkaround(param_ty, alignment);5355 const int_ptr = try self.buildAlloca(int_llvm_ty, alignment);
5356 _ = try self.wip.store(.normal, llvm_arg, int_ptr, alignment);5356 _ = try self.wip.store(.normal, llvm_arg, int_ptr, alignment);
5357 const loaded = try self.wip.load(.normal, int_llvm_ty, int_ptr, alignment, "");5357 const loaded = try self.wip.load(.normal, int_llvm_ty, int_ptr, alignment, "");
5358 try llvm_args.append(loaded);5358 try llvm_args.append(loaded);
...@@ -5727,7 +5727,7 @@ pub const FuncGen = struct {...@@ -5727,7 +5727,7 @@ pub const FuncGen = struct {
5727 const llvm_va_list_ty = try o.lowerType(va_list_ty);5727 const llvm_va_list_ty = try o.lowerType(va_list_ty);
57285728
5729 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();5729 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();
5730 const dest_list = try self.buildAllocaWorkaround(va_list_ty, result_alignment);5730 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
57315731
5732 _ = try self.wip.callIntrinsic(.normal, .none, .va_copy, &.{dest_list.typeOfWip(&self.wip)}, &.{ dest_list, src_list }, "");5732 _ = try self.wip.callIntrinsic(.normal, .none, .va_copy, &.{dest_list.typeOfWip(&self.wip)}, &.{ dest_list, src_list }, "");
5733 return if (isByRef(va_list_ty, zcu))5733 return if (isByRef(va_list_ty, zcu))
...@@ -5752,7 +5752,7 @@ pub const FuncGen = struct {...@@ -5752,7 +5752,7 @@ pub const FuncGen = struct {
5752 const llvm_va_list_ty = try o.lowerType(va_list_ty);5752 const llvm_va_list_ty = try o.lowerType(va_list_ty);
57535753
5754 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();5754 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();
5755 const dest_list = try self.buildAllocaWorkaround(va_list_ty, result_alignment);5755 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
57565756
5757 _ = try self.wip.callIntrinsic(.normal, .none, .va_start, &.{dest_list.typeOfWip(&self.wip)}, &.{dest_list}, "");5757 _ = try self.wip.callIntrinsic(.normal, .none, .va_start, &.{dest_list.typeOfWip(&self.wip)}, &.{dest_list}, "");
5758 return if (isByRef(va_list_ty, zcu))5758 return if (isByRef(va_list_ty, zcu))
...@@ -8030,7 +8030,7 @@ pub const FuncGen = struct {...@@ -8030,7 +8030,7 @@ pub const FuncGen = struct {
8030 self.ret_ptr8030 self.ret_ptr
8031 else brk: {8031 else brk: {
8032 const alignment = optional_ty.abiAlignment(zcu).toLlvm();8032 const alignment = optional_ty.abiAlignment(zcu).toLlvm();
8033 const optional_ptr = try self.buildAllocaWorkaround(optional_ty, alignment);8033 const optional_ptr = try self.buildAlloca(llvm_optional_ty, alignment);
8034 break :brk optional_ptr;8034 break :brk optional_ptr;
8035 };8035 };
80368036
...@@ -8067,7 +8067,7 @@ pub const FuncGen = struct {...@@ -8067,7 +8067,7 @@ pub const FuncGen = struct {
8067 self.ret_ptr8067 self.ret_ptr
8068 else brk: {8068 else brk: {
8069 const alignment = err_un_ty.abiAlignment(pt.zcu).toLlvm();8069 const alignment = err_un_ty.abiAlignment(pt.zcu).toLlvm();
8070 const result_ptr = try self.buildAllocaWorkaround(err_un_ty, alignment);8070 const result_ptr = try self.buildAlloca(err_un_llvm_ty, alignment);
8071 break :brk result_ptr;8071 break :brk result_ptr;
8072 };8072 };
80738073
...@@ -8106,7 +8106,7 @@ pub const FuncGen = struct {...@@ -8106,7 +8106,7 @@ pub const FuncGen = struct {
8106 self.ret_ptr8106 self.ret_ptr
8107 else brk: {8107 else brk: {
8108 const alignment = err_un_ty.abiAlignment(zcu).toLlvm();8108 const alignment = err_un_ty.abiAlignment(zcu).toLlvm();
8109 const result_ptr = try self.buildAllocaWorkaround(err_un_ty, alignment);8109 const result_ptr = try self.buildAlloca(err_un_llvm_ty, alignment);
8110 break :brk result_ptr;8110 break :brk result_ptr;
8111 };8111 };
81128112
...@@ -8640,7 +8640,7 @@ pub const FuncGen = struct {...@@ -8640,7 +8640,7 @@ pub const FuncGen = struct {
86408640
8641 if (isByRef(inst_ty, zcu)) {8641 if (isByRef(inst_ty, zcu)) {
8642 const result_alignment = inst_ty.abiAlignment(zcu).toLlvm();8642 const result_alignment = inst_ty.abiAlignment(zcu).toLlvm();
8643 const alloca_inst = try self.buildAllocaWorkaround(inst_ty, result_alignment);8643 const alloca_inst = try self.buildAlloca(llvm_inst_ty, result_alignment);
8644 {8644 {
8645 const field_ptr = try self.wip.gepStruct(llvm_inst_ty, alloca_inst, result_index, "");8645 const field_ptr = try self.wip.gepStruct(llvm_inst_ty, alloca_inst, result_index, "");
8646 _ = try self.wip.store(.normal, result_val, field_ptr, result_alignment);8646 _ = try self.wip.store(.normal, result_val, field_ptr, result_alignment);
...@@ -9000,7 +9000,7 @@ pub const FuncGen = struct {...@@ -9000,7 +9000,7 @@ pub const FuncGen = struct {
90009000
9001 if (isByRef(dest_ty, zcu)) {9001 if (isByRef(dest_ty, zcu)) {
9002 const result_alignment = dest_ty.abiAlignment(zcu).toLlvm();9002 const result_alignment = dest_ty.abiAlignment(zcu).toLlvm();
9003 const alloca_inst = try self.buildAllocaWorkaround(dest_ty, result_alignment);9003 const alloca_inst = try self.buildAlloca(llvm_dest_ty, result_alignment);
9004 {9004 {
9005 const field_ptr = try self.wip.gepStruct(llvm_dest_ty, alloca_inst, result_index, "");9005 const field_ptr = try self.wip.gepStruct(llvm_dest_ty, alloca_inst, result_index, "");
9006 _ = try self.wip.store(.normal, result, field_ptr, result_alignment);9006 _ = try self.wip.store(.normal, result, field_ptr, result_alignment);
...@@ -9425,7 +9425,7 @@ pub const FuncGen = struct {...@@ -9425,7 +9425,7 @@ pub const FuncGen = struct {
9425 return self.ng.todo("implement bitcast vector to non-ref array", .{});9425 return self.ng.todo("implement bitcast vector to non-ref array", .{});
9426 }9426 }
9427 const alignment = inst_ty.abiAlignment(zcu).toLlvm();9427 const alignment = inst_ty.abiAlignment(zcu).toLlvm();
9428 const array_ptr = try self.buildAllocaWorkaround(inst_ty, alignment);9428 const array_ptr = try self.buildAlloca(llvm_dest_ty, alignment);
9429 const bitcast_ok = elem_ty.bitSize(zcu) == elem_ty.abiSize(zcu) * 8;9429 const bitcast_ok = elem_ty.bitSize(zcu) == elem_ty.abiSize(zcu) * 8;
9430 if (bitcast_ok) {9430 if (bitcast_ok) {
9431 _ = try self.wip.store(.normal, operand, array_ptr, alignment);9431 _ = try self.wip.store(.normal, operand, array_ptr, alignment);
...@@ -9486,7 +9486,7 @@ pub const FuncGen = struct {...@@ -9486,7 +9486,7 @@ pub const FuncGen = struct {
94869486
9487 if (result_is_ref) {9487 if (result_is_ref) {
9488 const alignment = operand_ty.abiAlignment(zcu).max(inst_ty.abiAlignment(zcu)).toLlvm();9488 const alignment = operand_ty.abiAlignment(zcu).max(inst_ty.abiAlignment(zcu)).toLlvm();
9489 const result_ptr = try self.buildAllocaWorkaround(inst_ty, alignment);9489 const result_ptr = try self.buildAlloca(llvm_dest_ty, alignment);
9490 _ = try self.wip.store(.normal, operand, result_ptr, alignment);9490 _ = try self.wip.store(.normal, operand, result_ptr, alignment);
9491 return result_ptr;9491 return result_ptr;
9492 }9492 }
...@@ -9499,7 +9499,7 @@ pub const FuncGen = struct {...@@ -9499,7 +9499,7 @@ pub const FuncGen = struct {
9499 // but LLVM won't let us bitcast struct values or vectors with padding bits.9499 // but LLVM won't let us bitcast struct values or vectors with padding bits.
9500 // Therefore, we store operand to alloca, then load for result.9500 // Therefore, we store operand to alloca, then load for result.
9501 const alignment = operand_ty.abiAlignment(zcu).max(inst_ty.abiAlignment(zcu)).toLlvm();9501 const alignment = operand_ty.abiAlignment(zcu).max(inst_ty.abiAlignment(zcu)).toLlvm();
9502 const result_ptr = try self.buildAllocaWorkaround(inst_ty, alignment);9502 const result_ptr = try self.buildAlloca(llvm_dest_ty, alignment);
9503 _ = try self.wip.store(.normal, operand, result_ptr, alignment);9503 _ = try self.wip.store(.normal, operand, result_ptr, alignment);
9504 return self.wip.load(.normal, llvm_dest_ty, result_ptr, alignment, "");9504 return self.wip.load(.normal, llvm_dest_ty, result_ptr, alignment, "");
9505 }9505 }
...@@ -9608,9 +9608,9 @@ pub const FuncGen = struct {...@@ -9608,9 +9608,9 @@ pub const FuncGen = struct {
9608 if (!pointee_type.isFnOrHasRuntimeBitsIgnoreComptime(zcu))9608 if (!pointee_type.isFnOrHasRuntimeBitsIgnoreComptime(zcu))
9609 return (try o.lowerPtrToVoid(ptr_ty)).toValue();9609 return (try o.lowerPtrToVoid(ptr_ty)).toValue();
96109610
9611 //const pointee_llvm_ty = try o.lowerType(pointee_type);9611 const pointee_llvm_ty = try o.lowerType(pointee_type);
9612 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();9612 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
9613 return self.buildAllocaWorkaround(pointee_type, alignment);9613 return self.buildAlloca(pointee_llvm_ty, alignment);
9614 }9614 }
96159615
9616 fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {9616 fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value {
...@@ -9622,9 +9622,9 @@ pub const FuncGen = struct {...@@ -9622,9 +9622,9 @@ pub const FuncGen = struct {
9622 if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu))9622 if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu))
9623 return (try o.lowerPtrToVoid(ptr_ty)).toValue();9623 return (try o.lowerPtrToVoid(ptr_ty)).toValue();
9624 if (self.ret_ptr != .none) return self.ret_ptr;9624 if (self.ret_ptr != .none) return self.ret_ptr;
9625 //const ret_llvm_ty = try o.lowerType(ret_ty);9625 const ret_llvm_ty = try o.lowerType(ret_ty);
9626 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();9626 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
9627 return self.buildAllocaWorkaround(ret_ty, alignment);9627 return self.buildAlloca(ret_llvm_ty, alignment);
9628 }9628 }
96299629
9630 /// Use this instead of builder.buildAlloca, because this function makes sure to9630 /// Use this instead of builder.buildAlloca, because this function makes sure to
...@@ -9638,16 +9638,6 @@ pub const FuncGen = struct {...@@ -9638,16 +9638,6 @@ pub const FuncGen = struct {
9638 return buildAllocaInner(&self.wip, llvm_ty, alignment, target);9638 return buildAllocaInner(&self.wip, llvm_ty, alignment, target);
9639 }9639 }
96409640
9641 // Workaround for https://github.com/ziglang/zig/issues/16392
9642 fn buildAllocaWorkaround(
9643 self: *FuncGen,
9644 ty: Type,
9645 alignment: Builder.Alignment,
9646 ) Allocator.Error!Builder.Value {
9647 const o = self.ng.object;
9648 return self.buildAlloca(try o.builder.arrayType(ty.abiSize(o.pt.zcu), .i8), alignment);
9649 }
9650
9651 fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {9641 fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value {
9652 const o = self.ng.object;9642 const o = self.ng.object;
9653 const pt = o.pt;9643 const pt = o.pt;
...@@ -10686,7 +10676,7 @@ pub const FuncGen = struct {...@@ -10686,7 +10676,7 @@ pub const FuncGen = struct {
10686 const llvm_result_ty = accum_init.typeOfWip(&self.wip);10676 const llvm_result_ty = accum_init.typeOfWip(&self.wip);
1068710677
10688 // Allocate and initialize our mutable variables10678 // Allocate and initialize our mutable variables
10689 const i_ptr = try self.buildAllocaWorkaround(Type.usize, .default);10679 const i_ptr = try self.buildAlloca(usize_ty, .default);
10690 _ = try self.wip.store(.normal, try o.builder.intValue(usize_ty, 0), i_ptr, .default);10680 _ = try self.wip.store(.normal, try o.builder.intValue(usize_ty, 0), i_ptr, .default);
10691 const accum_ptr = try self.buildAlloca(llvm_result_ty, .default);10681 const accum_ptr = try self.buildAlloca(llvm_result_ty, .default);
10692 _ = try self.wip.store(.normal, accum_init, accum_ptr, .default);10682 _ = try self.wip.store(.normal, accum_init, accum_ptr, .default);
...@@ -10899,7 +10889,7 @@ pub const FuncGen = struct {...@@ -10899,7 +10889,7 @@ pub const FuncGen = struct {
10899 // TODO in debug builds init to undef so that the padding will be 0xaa10889 // TODO in debug builds init to undef so that the padding will be 0xaa
10900 // even if we fully populate the fields.10890 // even if we fully populate the fields.
10901 const alignment = result_ty.abiAlignment(zcu).toLlvm();10891 const alignment = result_ty.abiAlignment(zcu).toLlvm();
10902 const alloca_inst = try self.buildAllocaWorkaround(result_ty, alignment);10892 const alloca_inst = try self.buildAlloca(llvm_result_ty, alignment);
1090310893
10904 for (elements, 0..) |elem, i| {10894 for (elements, 0..) |elem, i| {
10905 if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue;10895 if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue;
...@@ -10936,7 +10926,7 @@ pub const FuncGen = struct {...@@ -10936,7 +10926,7 @@ pub const FuncGen = struct {
10936 const llvm_usize = try o.lowerType(Type.usize);10926 const llvm_usize = try o.lowerType(Type.usize);
10937 const usize_zero = try o.builder.intValue(llvm_usize, 0);10927 const usize_zero = try o.builder.intValue(llvm_usize, 0);
10938 const alignment = result_ty.abiAlignment(zcu).toLlvm();10928 const alignment = result_ty.abiAlignment(zcu).toLlvm();
10939 const alloca_inst = try self.buildAllocaWorkaround(result_ty, alignment);10929 const alloca_inst = try self.buildAlloca(llvm_result_ty, alignment);
1094010930
10941 const array_info = result_ty.arrayInfo(zcu);10931 const array_info = result_ty.arrayInfo(zcu);
10942 const elem_ptr_ty = try pt.ptrType(.{10932 const elem_ptr_ty = try pt.ptrType(.{
...@@ -11011,7 +11001,7 @@ pub const FuncGen = struct {...@@ -11011,7 +11001,7 @@ pub const FuncGen = struct {
11011 // We must construct the correct unnamed struct type here, in order to then set11001 // We must construct the correct unnamed struct type here, in order to then set
11012 // the fields appropriately.11002 // the fields appropriately.
11013 const alignment = layout.abi_align.toLlvm();11003 const alignment = layout.abi_align.toLlvm();
11014 const result_ptr = try self.buildAllocaWorkaround(union_ty, alignment);11004 const result_ptr = try self.buildAlloca(union_llvm_ty, alignment);
11015 const llvm_payload = try self.resolveInst(extra.init);11005 const llvm_payload = try self.resolveInst(extra.init);
11016 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]);11006 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]);
11017 const field_llvm_ty = try o.lowerType(field_ty);11007 const field_llvm_ty = try o.lowerType(field_ty);
...@@ -11308,7 +11298,7 @@ pub const FuncGen = struct {...@@ -11308,7 +11298,7 @@ pub const FuncGen = struct {
1130811298
11309 if (isByRef(optional_ty, zcu)) {11299 if (isByRef(optional_ty, zcu)) {
11310 const payload_alignment = optional_ty.abiAlignment(pt.zcu).toLlvm();11300 const payload_alignment = optional_ty.abiAlignment(pt.zcu).toLlvm();
11311 const alloca_inst = try self.buildAllocaWorkaround(optional_ty, payload_alignment);11301 const alloca_inst = try self.buildAlloca(optional_llvm_ty, payload_alignment);
1131211302
11313 {11303 {
11314 const field_ptr = try self.wip.gepStruct(optional_llvm_ty, alloca_inst, 0, "");11304 const field_ptr = try self.wip.gepStruct(optional_llvm_ty, alloca_inst, 0, "");
...@@ -11451,10 +11441,10 @@ pub const FuncGen = struct {...@@ -11451,10 +11441,10 @@ pub const FuncGen = struct {
11451 ) !Builder.Value {11441 ) !Builder.Value {
11452 const o = fg.ng.object;11442 const o = fg.ng.object;
11453 const pt = o.pt;11443 const pt = o.pt;
11454 //const pointee_llvm_ty = try o.lowerType(pointee_type);11444 const pointee_llvm_ty = try o.lowerType(pointee_type);
11455 const result_align = InternPool.Alignment.fromLlvm(ptr_alignment)11445 const result_align = InternPool.Alignment.fromLlvm(ptr_alignment)
11456 .max(pointee_type.abiAlignment(pt.zcu)).toLlvm();11446 .max(pointee_type.abiAlignment(pt.zcu)).toLlvm();
11457 const result_ptr = try fg.buildAllocaWorkaround(pointee_type, result_align);11447 const result_ptr = try fg.buildAlloca(pointee_llvm_ty, result_align);
11458 const size_bytes = pointee_type.abiSize(pt.zcu);11448 const size_bytes = pointee_type.abiSize(pt.zcu);
11459 _ = try fg.wip.callMemCpy(11449 _ = try fg.wip.callMemCpy(
11460 result_ptr,11450 result_ptr,
...@@ -11515,7 +11505,7 @@ pub const FuncGen = struct {...@@ -11515,7 +11505,7 @@ pub const FuncGen = struct {
1151511505
11516 if (isByRef(elem_ty, zcu)) {11506 if (isByRef(elem_ty, zcu)) {
11517 const result_align = elem_ty.abiAlignment(zcu).toLlvm();11507 const result_align = elem_ty.abiAlignment(zcu).toLlvm();
11518 const result_ptr = try self.buildAllocaWorkaround(elem_ty, result_align);11508 const result_ptr = try self.buildAlloca(elem_llvm_ty, result_align);
1151911509
11520 const same_size_int = try o.builder.intType(@intCast(elem_bits));11510 const same_size_int = try o.builder.intType(@intCast(elem_bits));
11521 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");11511 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");