From c248af3bdcd17c334e742d53a7ac7bda2422a688 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 29 Jun 2022 19:01:48 -0700 Subject: LLVM: fix lowering of untagged union types The LLVM backend was calculating the amount of padding solely based on the payload size. However, in the case where there is no union tag, this fails to take into account alignment. Closes #11857 --- src/codegen/llvm.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 2b320923ad..c62860c88d 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -2708,7 +2708,10 @@ pub const DeclGen = struct { if (layout.most_aligned_field_size == layout.payload_size) { break :t llvm_aligned_field_ty; } - const padding_len = @intCast(c_uint, layout.payload_size - layout.most_aligned_field_size); + const padding_len = if (layout.tag_size == 0) + @intCast(c_uint, layout.abi_size - layout.most_aligned_field_size) + else + @intCast(c_uint, layout.payload_size - layout.most_aligned_field_size); const fields: [2]*const llvm.Type = .{ llvm_aligned_field_ty, dg.context.intType(8).arrayType(padding_len), @@ -5756,7 +5759,7 @@ pub const FuncGen = struct { // First set the non-null bit. const indices: [2]*const llvm.Value = .{ index_type.constNull(), // dereference the pointer - index_type.constInt(1, .False), // second field is the payload + index_type.constInt(1, .False), // second field is the non-null bit }; const non_null_ptr = self.builder.buildInBoundsGEP(operand, &indices, indices.len, ""); _ = self.builder.buildStore(non_null_bit, non_null_ptr); -- cgit v1.2.3