authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-04-25 09:11:18-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-04-25 12:01:14-04:00
logcb1c7319b575372009185c25eac7b29b93ae83f4
tree9e965db8fa3e0e19c50f2776c13b82c717c937fb
parent23bcb8148fb12d86b003708b6410d98a986d2d9f

llvm: fix aarch64 c abi HFA detection

Aggregate types do not count as Homogeneous Aggregates if they have padding gaps between fields or at the end due to field alignments.

4 files changed, 227 insertions(+), 64 deletions(-)

src/codegen/aarch64/abi.zig+49-43
......@@ -1,5 +1,6 @@
11const assert = @import("std").debug.assert;
22const std = @import("std");
3const InternPool = @import("../../InternPool.zig");
34const Type = @import("../../Type.zig");
45const Zcu = @import("../../Zcu.zig");
56
......@@ -15,12 +16,10 @@ pub const Class = union(enum) {
1516pub fn classifyType(ty: Type, zcu: *Zcu) Class {
1617 assert(ty.hasRuntimeBits(zcu));
1718
18 var maybe_float_bits: ?u16 = null;
1919 switch (ty.zigTypeTag(zcu)) {
2020 .@"struct" => {
2121 if (ty.containerLayout(zcu) == .@"packed") return .byval;
22 const float_count = countFloats(ty, zcu, &maybe_float_bits);
23 if (float_count <= sret_float_count) return .{ .float_array = float_count };
22 if (countFloats(ty, zcu)) |float| return .{ .float_array = float.count };
2423
2524 const bit_size = ty.bitSize(zcu);
2625 if (bit_size > 128) return .memory;
......@@ -29,8 +28,7 @@ pub fn classifyType(ty: Type, zcu: *Zcu) Class {
2928 },
3029 .@"union" => {
3130 if (ty.containerLayout(zcu) == .@"packed") return .byval;
32 const float_count = countFloats(ty, zcu, &maybe_float_bits);
33 if (float_count <= sret_float_count) return .{ .float_array = float_count };
31 if (countFloats(ty, zcu)) |float| return .{ .float_array = float.count };
3432
3533 const bit_size = ty.bitSize(zcu);
3634 if (bit_size > 128) return .memory;
......@@ -70,46 +68,52 @@ pub fn classifyType(ty: Type, zcu: *Zcu) Class {
7068 }
7169}
7270
73const sret_float_count = 4;
74fn countFloats(ty: Type, zcu: *Zcu, maybe_float_bits: *?u16) u8 {
71const CountFloatsResult = struct {
72 ty: Type,
73 count: std.math.IntFittingRange(0, max_count),
74
75 const none: CountFloatsResult = .{ .ty = .void, .count = 0 };
76
77 const max_count = 4;
78};
79fn countFloats(ty: Type, zcu: *Zcu) ?CountFloatsResult {
7580 const ip = &zcu.intern_pool;
76 const target = zcu.getTarget();
77 const invalid = std.math.maxInt(u8);
81 if (!ty.hasRuntimeBits(zcu)) return .none;
7882 switch (ty.zigTypeTag(zcu)) {
7983 .@"union" => {
80 const union_obj = zcu.typeToUnion(ty).?;
81 var max_count: u8 = 0;
82 for (union_obj.field_types.get(ip)) |field_ty| {
83 const field_count = countFloats(Type.fromInterned(field_ty), zcu, maybe_float_bits);
84 if (field_count == invalid) return invalid;
85 if (field_count > max_count) max_count = field_count;
86 if (max_count > sret_float_count) return invalid;
84 const loaded_union = zcu.typeToUnion(ty).?;
85 var result: CountFloatsResult = .none;
86 for (loaded_union.field_types.get(ip)) |field_ty| {
87 const float = countFloats(Type.fromInterned(field_ty), zcu) orelse return null;
88 if (result.ty.toIntern() == .void_type) {
89 result.ty = float.ty;
90 } else if (result.ty.bitSize(zcu) != float.ty.bitSize(zcu)) return null;
91 result.count = @max(result.count, float.count);
8792 }
88 return max_count;
93 if (ty.abiSize(zcu) != result.ty.abiSize(zcu) * result.count) return null;
94 return result;
8995 },
9096 .@"struct" => {
91 const fields_len = ty.structFieldCount(zcu);
92 var count: u8 = 0;
93 var i: u32 = 0;
94 while (i < fields_len) : (i += 1) {
95 const field_ty = ty.fieldType(i, zcu);
96 const field_count = countFloats(field_ty, zcu, maybe_float_bits);
97 if (field_count == invalid) return invalid;
98 count += field_count;
99 if (count > sret_float_count) return invalid;
97 var result: CountFloatsResult = .none;
98 var field_it: InternPool.LoadedStructType.RuntimeOrderIterator = if (zcu.typeToStruct(ty)) |loaded_struct|
99 loaded_struct.iterateRuntimeOrder(ip)
100 else
101 .{ .runtime_order = null, .fields_len = ty.structFieldCount(zcu), .next_index = 0 };
102 while (field_it.next()) |field_index| {
103 if (ty.structFieldOffset(field_index, zcu) != result.ty.abiSize(zcu) * result.count) return null;
104 const field_ty = ty.fieldType(field_index, zcu);
105 const float = countFloats(field_ty, zcu) orelse return null;
106 if (result.ty.toIntern() == .void_type) {
107 result.ty = float.ty;
108 } else if (result.ty.bitSize(zcu) != float.ty.bitSize(zcu)) return null;
109 if (float.count > CountFloatsResult.max_count - result.count) return null;
110 result.count += float.count;
100111 }
101 return count;
112 if (ty.abiSize(zcu) != result.ty.abiSize(zcu) * result.count) return null;
113 return result;
102114 },
103 .float => {
104 const float_bits = maybe_float_bits.* orelse {
105 maybe_float_bits.* = ty.floatBits(target);
106 return 1;
107 };
108 if (ty.floatBits(target) == float_bits) return 1;
109 return invalid;
110 },
111 .void => return 0,
112 else => return invalid,
115 .float => return .{ .ty = ty, .count = 1 },
116 else => return null,
113117 }
114118}
115119
......@@ -117,17 +121,19 @@ pub fn getFloatArrayType(ty: Type, zcu: *Zcu) ?Type {
117121 const ip = &zcu.intern_pool;
118122 switch (ty.zigTypeTag(zcu)) {
119123 .@"union" => {
120 const union_obj = zcu.typeToUnion(ty).?;
121 for (union_obj.field_types.get(ip)) |field_ty| {
124 const loaded_union = zcu.typeToUnion(ty).?;
125 for (loaded_union.field_types.get(ip)) |field_ty| {
122126 if (getFloatArrayType(Type.fromInterned(field_ty), zcu)) |some| return some;
123127 }
124128 return null;
125129 },
126130 .@"struct" => {
127 const fields_len = ty.structFieldCount(zcu);
128 var i: u32 = 0;
129 while (i < fields_len) : (i += 1) {
130 const field_ty = ty.fieldType(i, zcu);
131 var field_it: InternPool.LoadedStructType.RuntimeOrderIterator = if (zcu.typeToStruct(ty)) |loaded_struct|
132 loaded_struct.iterateRuntimeOrder(ip)
133 else
134 .{ .runtime_order = null, .fields_len = ty.structFieldCount(zcu), .next_index = 0 };
135 while (field_it.next()) |field_index| {
136 const field_ty = ty.fieldType(field_index, zcu);
131137 if (getFloatArrayType(field_ty, zcu)) |some| return some;
132138 }
133139 return null;
src/codegen/llvm/FuncGen.zig+7-6
......@@ -6707,17 +6707,18 @@ const ParamTypeIterator = struct {
67076707 .double_integer => return Lowering{ .i64_array = 2 },
67086708 .fields => {
67096709 it.types_len = 0;
6710 var offset: u64 = 0;
6711 for (0..ty.structFieldCount(zcu)) |field_index| {
6710 var field_it: InternPool.LoadedStructType.RuntimeOrderIterator = if (zcu.typeToStruct(ty)) |loaded_struct|
6711 loaded_struct.iterateRuntimeOrder(&zcu.intern_pool)
6712 else
6713 .{ .runtime_order = null, .fields_len = ty.structFieldCount(zcu), .next_index = 0 };
6714 while (field_it.next()) |field_index| {
67126715 const field_ty = ty.fieldType(field_index, zcu);
67136716 if (!field_ty.hasRuntimeBits(zcu)) continue;
6714 offset = field_ty.abiAlignment(zcu).forward(offset);
67156717 it.types_buffer[it.types_len] = try it.object.lowerType(field_ty);
6716 it.offsets_buffer[it.types_len] = offset;
6718 it.offsets_buffer[it.types_len] = ty.structFieldOffset(field_index, zcu);
67176719 it.types_len += 1;
6718 offset += field_ty.abiSize(zcu);
67196720 }
6720 it.offsets_buffer[it.types_len] = offset;
6721 it.offsets_buffer[it.types_len] = ty.abiSize(zcu);
67216722 it.llvm_index += it.types_len - 1;
67226723 return .multiple_llvm_types;
67236724 },
test/c_abi/cfuncs.c+95-13
......@@ -1,5 +1,6 @@
11#include <complex.h>
22#include <inttypes.h>
3#include <stdalign.h>
34#include <stdbool.h>
45#include <stdlib.h>
56#include <string.h>
......@@ -209,7 +210,7 @@ struct Struct_u8 zig_ret_struct_u8(void);
209210void zig_struct_u8(struct Struct_u8, size_t);
210211
211212struct Struct_u8 c_ret_struct_u8(void) {
212 return (struct Struct_u8){ 4 };
213 return (struct Struct_u8){ .a = 4 };
213214}
214215
215216void c_struct_u8(struct Struct_u8 s, size_t i) {
......@@ -226,7 +227,7 @@ struct Struct_u16 zig_ret_struct_u16(void);
226227void zig_struct_u16(struct Struct_u16, size_t);
227228
228229struct Struct_u16 c_ret_struct_u16(void) {
229 return (struct Struct_u16){ 10 };
230 return (struct Struct_u16){ .a = 10 };
230231}
231232
232233void c_struct_u16(struct Struct_u16 s, size_t i) {
......@@ -243,7 +244,7 @@ struct Struct_u32 zig_ret_struct_u32(void);
243244void zig_struct_u32(struct Struct_u32, size_t);
244245
245246struct Struct_u32 c_ret_struct_u32(void) {
246 return (struct Struct_u32){ 16 };
247 return (struct Struct_u32){ .a = 16 };
247248}
248249
249250void c_struct_u32(struct Struct_u32 s, size_t i) {
......@@ -260,7 +261,7 @@ struct Struct_u64 zig_ret_struct_u64(void);
260261void zig_struct_u64(struct Struct_u64, size_t);
261262
262263struct Struct_u64 c_ret_struct_u64(void) {
263 return (struct Struct_u64){ 22 };
264 return (struct Struct_u64){ .a = 22 };
264265}
265266
266267void c_struct_u64(struct Struct_u64 s, size_t i) {
......@@ -286,7 +287,7 @@ void zig_struct_u64_u64_7(size_t, size_t, size_t, size_t, size_t, size_t, size_t
286287void zig_struct_u64_u64_8(size_t, size_t, size_t, size_t, size_t, size_t, size_t, size_t, struct Struct_u64_u64, size_t);
287288
288289struct Struct_u64_u64 c_ret_struct_u64_u64(void) {
289 return (struct Struct_u64_u64){ 21, 22 };
290 return (struct Struct_u64_u64){ .a = 21, .b = 22 };
290291}
291292
292293void c_struct_u64_u64_0(struct Struct_u64_u64 s, size_t i) {
......@@ -344,7 +345,7 @@ struct Struct_f32 zig_ret_struct_f32(void);
344345void zig_struct_f32(struct Struct_f32);
345346
346347struct Struct_f32 c_ret_struct_f32(void) {
347 return (struct Struct_f32){ 2.5f };
348 return (struct Struct_f32){ .a = 2.5f };
348349}
349350
350351void c_struct_f32(struct Struct_f32 s) {
......@@ -360,13 +361,49 @@ struct Struct_f64 zig_ret_struct_f64(void);
360361void zig_struct_f64(struct Struct_f64);
361362
362363struct Struct_f64 c_ret_struct_f64(void) {
363 return (struct Struct_f64){ 2.5 };
364 return (struct Struct_f64){ .a = 2.5 };
364365}
365366
366367void c_struct_f64(struct Struct_f64 s) {
367368 assert_or_panic(s.a == 2.5);
368369}
369370
371struct Struct_f32a8 {
372 alignas(8) float a;
373};
374
375struct Struct_f32a8 zig_ret_struct_f32a8(void);
376
377void zig_struct_f32a8(struct Struct_f32a8, float);
378
379struct Struct_f32a8 c_ret_struct_f32a8(void) {
380 return (struct Struct_f32a8){ .a = 4.125f };
381}
382
383void c_struct_f32a8(struct Struct_f32a8 s, float f) {
384 assert_or_panic(s.a == 5.375f);
385 assert_or_panic(f == 6.5f);
386}
387
388struct Struct_f32a8_f32a8 {
389 alignas(8) float a;
390 alignas(8) float b;
391};
392
393struct Struct_f32a8_f32a8 zig_ret_struct_f32a8_f32a8(void);
394
395void zig_struct_f32a8_f32a8(struct Struct_f32a8_f32a8, float);
396
397struct Struct_f32a8_f32a8 c_ret_struct_f32a8_f32a8(void) {
398 return (struct Struct_f32a8_f32a8){ .a = 6.625f, .b = 7.875f };
399}
400
401void c_struct_f32a8_f32a8(struct Struct_f32a8_f32a8 s, float f) {
402 assert_or_panic(s.a == 8.0625f);
403 assert_or_panic(s.b == 9.1875f);
404 assert_or_panic(f == 10.5f);
405}
406
370407struct Struct_f32f32_f32 {
371408 struct {
372409 float b, c;
......@@ -379,7 +416,7 @@ struct Struct_f32f32_f32 zig_ret_struct_f32f32_f32(void);
379416void zig_struct_f32f32_f32(struct Struct_f32f32_f32);
380417
381418struct Struct_f32f32_f32 c_ret_struct_f32f32_f32(void) {
382 return (struct Struct_f32f32_f32){ { 1.0f, 2.0f }, 3.0f };
419 return (struct Struct_f32f32_f32){ .a = { .b = 1.0f, .c = 2.0f }, .d = 3.0f };
383420}
384421
385422void c_struct_f32f32_f32(struct Struct_f32f32_f32 s) {
......@@ -400,7 +437,7 @@ struct Struct_f32_f32f32 zig_ret_struct_f32_f32f32(void);
400437void zig_struct_f32_f32f32(struct Struct_f32_f32f32);
401438
402439struct Struct_f32_f32f32 c_ret_struct_f32_f32f32(void) {
403 return (struct Struct_f32_f32f32){ 1.0f, { 2.0f, 3.0f } };
440 return (struct Struct_f32_f32f32){ .a = 1.0f, .b = { .c = 2.0f, .d = 3.0f } };
404441}
405442
406443void c_struct_f32_f32f32(struct Struct_f32_f32f32 s) {
......@@ -2870,7 +2907,7 @@ void run_c_tests(void) {
28702907 {
28712908 struct Struct_f32 s = zig_ret_struct_f32();
28722909 assert_or_panic(s.a == 2.5f);
2873 zig_struct_f32((struct Struct_f32){ 2.5f });
2910 zig_struct_f32((struct Struct_f32){ .a = 2.5f });
28742911 }
28752912#endif
28762913
......@@ -2879,17 +2916,62 @@ void run_c_tests(void) {
28792916 {
28802917 struct Struct_f64 s = zig_ret_struct_f64();
28812918 assert_or_panic(s.a == 2.5);
2882 zig_struct_f64((struct Struct_f64){ 2.5 });
2919 zig_struct_f64((struct Struct_f64){ .a = 2.5 });
2920 }
2921#endif
2922#endif
2923
2924#if !defined(__arm__)
2925#if !defined(__loongarch__)
2926#if !defined(__mips64__)
2927#if !defined(__powerpc__)
2928#if !defined(ZIG_RISCV32)
2929#if !defined(__s390x__)
2930#if !defined(__i386__)
2931 {
2932 struct Struct_f32a8 s = zig_ret_struct_f32a8();
2933 assert_or_panic(s.a == 1.25f);
2934 zig_struct_f32a8((struct Struct_f32a8){ .a = 2.75f }, 3.5f);
2935 }
2936#endif
2937#endif
2938#endif
2939#endif
2940#endif
2941#endif
2942#endif
2943
2944#if !defined(__arm__)
2945#if !defined(__loongarch__)
2946#if !defined(__mips64__)
2947#if !defined(__powerpc__)
2948#if !defined(__riscv)
2949#if !defined(__s390x__)
2950#if !defined(__i386__)
2951#if !defined(__x86_64__)
2952 {
2953 struct Struct_f32a8_f32a8 s = zig_ret_struct_f32a8_f32a8();
2954 assert_or_panic(s.a == 1.25f);
2955 assert_or_panic(s.b == 2.75f);
2956 zig_struct_f32a8_f32a8((struct Struct_f32a8_f32a8){ .a = 3.125f, .b = 4.375f }, 5.5f);
28832957 }
28842958#endif
2959#endif
2960#endif
2961#endif
2962#endif
2963#endif
2964#endif
2965#endif
28852966
2967#if !(defined(__arm__) && defined(__SOFTFP__))
28862968#if !defined(__loongarch__) && !defined(__mips64__)
28872969 {
28882970 struct Struct_f32f32_f32 s = zig_ret_struct_f32f32_f32();
28892971 assert_or_panic(s.a.b == 1.0f);
28902972 assert_or_panic(s.a.c == 2.0f);
28912973 assert_or_panic(s.d == 3.0f);
2892 zig_struct_f32f32_f32((struct Struct_f32f32_f32){ { 1.0f, 2.0f }, 3.0f });
2974 zig_struct_f32f32_f32((struct Struct_f32f32_f32){ .a = { .b = 1.0f, .c = 2.0f }, .d = 3.0f });
28932975 }
28942976
28952977 {
......@@ -2897,7 +2979,7 @@ void run_c_tests(void) {
28972979 assert_or_panic(s.a == 1.0f);
28982980 assert_or_panic(s.b.c == 2.0f);
28992981 assert_or_panic(s.b.d == 3.0f);
2900 zig_struct_f32_f32f32((struct Struct_f32_f32f32){ 1.0f, { 2.0f, 3.0f } });
2982 zig_struct_f32_f32f32((struct Struct_f32_f32f32){ .a = 1.0f, .b = { .c = 2.0f, .d = 3.0f } });
29012983 }
29022984#endif
29032985#endif
test/c_abi/main.zig+76-2
......@@ -444,7 +444,7 @@ extern fn c_struct_u64_u64_6(usize, usize, usize, usize, usize, usize, Struct_u6
444444extern fn c_struct_u64_u64_7(usize, usize, usize, usize, usize, usize, usize, Struct_u64_u64, usize) void;
445445extern fn c_struct_u64_u64_8(usize, usize, usize, usize, usize, usize, usize, usize, Struct_u64_u64, usize) void;
446446
447test "C ABI struct u64 u64" {
447test "C ABI struct u64, u64" {
448448 if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest;
449449 if (builtin.cpu.arch.isPowerPC32()) return error.SkipZigTest;
450450 if (builtin.cpu.arch == .hexagon) return error.SkipZigTest;
......@@ -518,6 +518,80 @@ test "C ABI struct f64" {
518518 c_struct_f64(.{ .a = 2.5 });
519519}
520520
521const Struct_f32a8 = extern struct {
522 a: f32 align(8),
523};
524
525export fn zig_ret_struct_f32a8() Struct_f32a8 {
526 return .{ .a = 1.25 };
527}
528
529export fn zig_struct_f32a8(s: Struct_f32a8, f: f32) void {
530 expect(s.a == 2.75) catch @panic("test failure");
531 expect(f == 3.5) catch @panic("test failure");
532}
533
534extern fn c_ret_struct_f32a8() Struct_f32a8;
535
536extern fn c_struct_f32a8(Struct_f32a8, f32) void;
537
538test "C ABI struct f32 align(8)" {
539 if (builtin.cpu.arch.isArm()) return error.SkipZigTest;
540 if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest;
541 if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest;
542 if (builtin.cpu.arch.isPowerPC()) return error.SkipZigTest;
543 if (builtin.cpu.arch == .riscv32) return error.SkipZigTest;
544 if (builtin.cpu.arch == .s390x) return error.SkipZigTest;
545 if (builtin.cpu.arch == .x86) return error.SkipZigTest;
546
547 const s = c_ret_struct_f32a8();
548 try expect(s.a == 4.125);
549 c_struct_f32a8(.{ .a = 5.375 }, 6.5);
550}
551
552const Struct_f32a8_f32a8 = extern struct {
553 a: f32 align(8),
554 b: f32 align(8),
555};
556
557comptime {
558 skip: {
559 if (builtin.zig_backend == .stage2_x86_64) break :skip;
560
561 _ = struct {
562 export fn zig_ret_struct_f32a8_f32a8() Struct_f32a8_f32a8 {
563 return .{ .a = 1.25, .b = 2.75 };
564 }
565
566 export fn zig_struct_f32a8_f32a8(s: Struct_f32a8_f32a8, f: f32) void {
567 expect(s.a == 3.125) catch @panic("test failure");
568 expect(s.b == 4.375) catch @panic("test failure");
569 expect(f == 5.5) catch @panic("test failure");
570 }
571 };
572 }
573}
574
575extern fn c_ret_struct_f32a8_f32a8() Struct_f32a8_f32a8;
576
577extern fn c_struct_f32a8_f32a8(Struct_f32a8_f32a8, f32) void;
578
579test "C ABI struct f32 align(8), f32 align(8)" {
580 if (builtin.cpu.arch.isArm()) return error.SkipZigTest;
581 if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest;
582 if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest;
583 if (builtin.cpu.arch.isPowerPC()) return error.SkipZigTest;
584 if (builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
585 if (builtin.cpu.arch == .s390x) return error.SkipZigTest;
586 if (builtin.cpu.arch == .x86) return error.SkipZigTest;
587 if (builtin.cpu.arch == .x86_64) return error.SkipZigTest;
588
589 const s = c_ret_struct_f32a8_f32a8();
590 try expect(s.a == 6.625);
591 try expect(s.b == 7.875);
592 c_struct_f32a8_f32a8(.{ .a = 8.0625, .b = 9.1875 }, 10.5);
593}
594
521595const Struct_f32f32_f32 = extern struct {
522596 a: extern struct { b: f32, c: f32 },
523597 d: f32,
......@@ -537,7 +611,7 @@ extern fn c_ret_struct_f32f32_f32() Struct_f32f32_f32;
537611
538612extern fn c_struct_f32f32_f32(Struct_f32f32_f32) void;
539613
540test "C ABI struct {f32,f32} f32" {
614test "C ABI struct {f32, f32}, f32" {
541615 if (builtin.cpu.arch.isMIPS64()) return error.SkipZigTest;
542616 if (builtin.cpu.arch.isPowerPC32()) return error.SkipZigTest;
543617 if (builtin.cpu.arch.isArm() and builtin.abi.float() == .soft) return error.SkipZigTest;