authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-07-12 13:06:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-12 21:34:20-04:00
log2eaef84ebe968224b0cf25206abf12ea1c5e0f5a
treee1fe357fb3dac1e21fd90c143d2ecc5396d3c470
parent7090f0471c0169c60a9476b537b09eebe1bdf6af

stage2 llvm: Use unpacked struct for unions and arrays

Our lowerings for various LLVM types assume that we can anticipate the alignment/layout that LLVM will generate. Among other things, this requires that we keep the alignment of our lowered LLVM types synchronized with their expected alignment in Zig. - Arrays were using packed struct types, which is seems to be incorrect since array elements are supposed to be self-aligned. - Unions were using packed struct types for their payload, which causes layout divergence between what stage2 expects and what LLVM generates Consider this lowered union type: ```llvm %Value = type { <{ i64, [8 x i8] }>, i1, [7 x i8] } ; 24 bytes, align(1) %ErrorUnion = type { %Value, i16 } ; 26 bytes, align(2) ``` Zig expects Value to be align(8) and, by extension, for ErrorUnion to be size 32.

1 files changed, 5 insertions(+), 5 deletions(-)

src/codegen/llvm.zig+5-5
......@@ -2689,7 +2689,7 @@ pub const DeclGen = struct {
26892689 llvm_aligned_field_ty,
26902690 dg.context.intType(8).arrayType(padding_len),
26912691 };
2692 break :t dg.context.structType(&fields, fields.len, .True);
2692 break :t dg.context.structType(&fields, fields.len, .False);
26932693 };
26942694
26952695 if (layout.tag_size == 0) {
......@@ -3002,7 +3002,7 @@ pub const DeclGen = struct {
30023002 return dg.context.constStruct(
30033003 llvm_elems.ptr,
30043004 @intCast(c_uint, llvm_elems.len),
3005 .True,
3005 .False,
30063006 );
30073007 } else {
30083008 const llvm_elem_ty = try dg.lowerType(elem_ty);
......@@ -3039,7 +3039,7 @@ pub const DeclGen = struct {
30393039 return dg.context.constStruct(
30403040 llvm_elems.ptr,
30413041 @intCast(c_uint, llvm_elems.len),
3042 .True,
3042 .False,
30433043 );
30443044 } else {
30453045 const llvm_elem_ty = try dg.lowerType(elem_ty);
......@@ -3056,7 +3056,7 @@ pub const DeclGen = struct {
30563056 const llvm_elems: [1]*const llvm.Value = .{sentinel};
30573057 const need_unnamed = dg.isUnnamedType(elem_ty, llvm_elems[0]);
30583058 if (need_unnamed) {
3059 return dg.context.constStruct(&llvm_elems, llvm_elems.len, .True);
3059 return dg.context.constStruct(&llvm_elems, llvm_elems.len, .False);
30603060 } else {
30613061 const llvm_elem_ty = try dg.lowerType(elem_ty);
30623062 return llvm_elem_ty.constArray(&llvm_elems, llvm_elems.len);
......@@ -3343,7 +3343,7 @@ pub const DeclGen = struct {
33433343 const fields: [2]*const llvm.Value = .{
33443344 field, dg.context.intType(8).arrayType(padding_len).getUndef(),
33453345 };
3346 break :p dg.context.constStruct(&fields, fields.len, .True);
3346 break :p dg.context.constStruct(&fields, fields.len, .False);
33473347 };
33483348
33493349 if (layout.tag_size == 0) {