llvm: fix SysV C abi for structs smaller than two eightbytes

Closes #16038
Closes #16288
This commit is contained in:
Jacob Young 2023-07-28 12:48:01 -04:00
parent c80609dfec
commit 125b453c58
7 changed files with 126 additions and 15 deletions

View File

@ -5421,10 +5421,13 @@ pub const FuncGen = struct {
// In this case the function return type is honoring the calling convention by having // In this case the function return type is honoring the calling convention by having
// a different LLVM type than the usual one. We solve this here at the callsite // a different LLVM type than the usual one. We solve this here at the callsite
// by using our canonical type, then loading it if necessary. // by using our canonical type, then loading it if necessary.
const alignment = Builder.Alignment.fromByteUnits( const alignment = Builder.Alignment.fromByteUnits(@max(
o.target_data.abiAlignmentOfType(abi_ret_ty.toLlvm(&o.builder)), o.target_data.abiAlignmentOfType(abi_ret_ty.toLlvm(&o.builder)),
); return_type.abiAlignment(mod),
const rp = try self.buildAlloca(llvm_ret_ty, alignment); ));
assert(o.target_data.abiSizeOfType(abi_ret_ty.toLlvm(&o.builder)) >=
o.target_data.abiSizeOfType(llvm_ret_ty.toLlvm(&o.builder)));
const rp = try self.buildAlloca(abi_ret_ty, alignment);
_ = try self.wip.store(.normal, call, rp, alignment); _ = try self.wip.store(.normal, call, rp, alignment);
return if (isByRef(return_type, mod)) return if (isByRef(return_type, mod))
rp rp
@ -11555,8 +11558,17 @@ fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.E
.none => break, .none => break,
} }
} }
if (classes[0] == .integer and classes[1] == .none) { const first_non_integer = std.mem.indexOfNone(x86_64_abi.Class, &classes, &.{.integer});
return o.builder.intType(@intCast(return_type.abiSize(mod) * 8)); if (first_non_integer == null or classes[first_non_integer.?] == .none) {
assert(first_non_integer orelse classes.len == types_index);
if (mod.intern_pool.indexToKey(return_type.toIntern()) == .struct_type) {
var struct_it = return_type.iterateStructOffsets(mod);
while (struct_it.next()) |_| {}
assert((std.math.divCeil(u64, struct_it.offset, 8) catch unreachable) == types_index);
if (struct_it.offset % 8 > 0) types_buffer[types_index - 1] =
try o.builder.intType(@intCast(struct_it.offset % 8 * 8));
}
if (types_index == 1) return types_buffer[0];
} }
return o.builder.structType(.normal, types_buffer[0..types_index]); return o.builder.structType(.normal, types_buffer[0..types_index]);
} }
@ -11807,10 +11819,21 @@ const ParamTypeIterator = struct {
.none => break, .none => break,
} }
} }
if (classes[0] == .integer and classes[1] == .none) { const first_non_integer = std.mem.indexOfNone(x86_64_abi.Class, &classes, &.{.integer});
it.zig_index += 1; if (first_non_integer == null or classes[first_non_integer.?] == .none) {
it.llvm_index += 1; assert(first_non_integer orelse classes.len == types_index);
return .abi_sized_int; if (types_index == 1) {
it.zig_index += 1;
it.llvm_index += 1;
return .abi_sized_int;
}
if (mod.intern_pool.indexToKey(ty.toIntern()) == .struct_type) {
var struct_it = ty.iterateStructOffsets(mod);
while (struct_it.next()) |_| {}
assert((std.math.divCeil(u64, struct_it.offset, 8) catch unreachable) == types_index);
if (struct_it.offset % 8 > 0) types_buffer[types_index - 1] =
try it.object.builder.intType(@intCast(struct_it.offset % 8 * 8));
}
} }
it.types_len = types_index; it.types_len = types_index;
it.types_buffer = types_buffer; it.types_buffer = types_buffer;

View File

@ -572,7 +572,9 @@ pub const Type = enum(u32) {
pub fn isSized(self: Type, builder: *const Builder) Allocator.Error!bool { pub fn isSized(self: Type, builder: *const Builder) Allocator.Error!bool {
var visited: IsSizedVisited = .{}; var visited: IsSizedVisited = .{};
return self.isSizedVisited(&visited, builder); const result = try self.isSizedVisited(&visited, builder);
if (builder.useLibLlvm()) assert(result == self.toLlvm(builder).isSized().toBool());
return result;
} }
const FormatData = struct { const FormatData = struct {

View File

@ -434,6 +434,9 @@ pub const Type = opaque {
Packed: Bool, Packed: Bool,
) void; ) void;
pub const isSized = LLVMTypeIsSized;
extern fn LLVMTypeIsSized(Ty: *Type) Bool;
pub const constGEP = LLVMConstGEP2; pub const constGEP = LLVMConstGEP2;
extern fn LLVMConstGEP2( extern fn LLVMConstGEP2(
Ty: *Type, Ty: *Type,

View File

@ -459,6 +459,7 @@ test "coerce anon tuple to tuple" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
var x: u8 = 1; var x: u8 = 1;
var y: u16 = 2; var y: u16 = 2;

View File

@ -16,8 +16,12 @@ static void assert_or_panic(bool ok) {
# define ZIG_PPC32 # define ZIG_PPC32
#endif #endif
#if defined __riscv && defined _ILP32 #ifdef __riscv
# define ZIG_RISCV32 # ifdef _ILP32
# define ZIG_RISCV32
# else
# define ZIG_RISCV64
# endif
#endif #endif
#if defined(__aarch64__) && defined(__linux__) #if defined(__aarch64__) && defined(__linux__)
@ -191,6 +195,15 @@ struct SmallStructInts {
void zig_small_struct_ints(struct SmallStructInts); void zig_small_struct_ints(struct SmallStructInts);
struct SmallStructInts zig_ret_small_struct_ints(); struct SmallStructInts zig_ret_small_struct_ints();
struct MedStructInts {
int32_t x;
int32_t y;
int32_t z;
};
void zig_med_struct_ints(struct MedStructInts);
struct MedStructInts zig_ret_med_struct_ints();
struct MedStructMixed { struct MedStructMixed {
uint32_t a; uint32_t a;
float b; float b;
@ -339,14 +352,22 @@ void run_c_tests(void) {
} }
#endif #endif
#if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ #if !defined __i386__ && !defined __arm__ && !defined __aarch64__ && \
!defined ZIG_PPC32 && !defined _ARCH_PPC64 !defined __mips__ && !defined __powerpc__ && !defined ZIG_RISCV64
{ {
struct SmallStructInts s = {1, 2, 3, 4}; struct SmallStructInts s = {1, 2, 3, 4};
zig_small_struct_ints(s); zig_small_struct_ints(s);
} }
#endif #endif
#if !defined __i386__ && !defined __arm__ && !defined __aarch64__ && \
!defined __mips__ && !defined __powerpc__ && !defined ZIG_RISCV64
{
struct MedStructInts s = {1, 2, 3};
zig_med_struct_ints(s);
}
#endif
#ifndef ZIG_NO_I128 #ifndef ZIG_NO_I128
{ {
__int128 s = 0; __int128 s = 0;
@ -586,6 +607,27 @@ struct SmallStructInts c_ret_small_struct_ints() {
return s; return s;
} }
void c_med_struct_ints(struct MedStructInts s) {
assert_or_panic(s.x == 1);
assert_or_panic(s.y == 2);
assert_or_panic(s.z == 3);
struct MedStructInts s2 = zig_ret_med_struct_ints();
assert_or_panic(s2.x == 1);
assert_or_panic(s2.y == 2);
assert_or_panic(s2.z == 3);
}
struct MedStructInts c_ret_med_struct_ints() {
struct MedStructInts s = {
.x = 1,
.y = 2,
.z = 3,
};
return s;
}
void c_med_struct_mixed(struct MedStructMixed x) { void c_med_struct_mixed(struct MedStructMixed x) {
assert_or_panic(x.a == 1234); assert_or_panic(x.a == 1234);
assert_or_panic(x.b == 100.0f); assert_or_panic(x.b == 100.0f);

View File

@ -393,6 +393,38 @@ export fn zig_small_struct_ints(x: SmallStructInts) void {
expect(x.d == 4) catch @panic("test failure"); expect(x.d == 4) catch @panic("test failure");
} }
const MedStructInts = extern struct {
x: i32,
y: i32,
z: i32,
};
extern fn c_med_struct_ints(MedStructInts) void;
extern fn c_ret_med_struct_ints() MedStructInts;
test "C ABI medium struct of ints" {
if (builtin.cpu.arch == .x86) return error.SkipZigTest;
if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest;
if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest;
var s = MedStructInts{
.x = 1,
.y = 2,
.z = 3,
};
c_med_struct_ints(s);
var s2 = c_ret_med_struct_ints();
try expect(s2.x == 1);
try expect(s2.y == 2);
try expect(s2.z == 3);
}
export fn zig_med_struct_ints(s: MedStructInts) void {
expect(s.x == 1) catch @panic("test failure");
expect(s.y == 2) catch @panic("test failure");
expect(s.z == 3) catch @panic("test failure");
}
const SmallPackedStruct = packed struct { const SmallPackedStruct = packed struct {
a: u2, a: u2,
b: u2, b: u2,
@ -691,6 +723,14 @@ export fn zig_ret_small_struct_ints() SmallStructInts {
}; };
} }
export fn zig_ret_med_struct_ints() MedStructInts {
return .{
.x = 1,
.y = 2,
.z = 3,
};
}
export fn zig_ret_med_struct_mixed() MedStructMixed { export fn zig_ret_med_struct_mixed() MedStructMixed {
return .{ return .{
.a = 1234, .a = 1234,

View File

@ -1102,7 +1102,7 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *Step { pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *Step {
const step = b.step("test-c-abi", "Run the C ABI tests"); const step = b.step("test-c-abi", "Run the C ABI tests");
const optimize_modes: [2]OptimizeMode = .{ .Debug, .ReleaseFast }; const optimize_modes: [3]OptimizeMode = .{ .Debug, .ReleaseSafe, .ReleaseFast };
for (optimize_modes) |optimize_mode| { for (optimize_modes) |optimize_mode| {
if (optimize_mode != .Debug and skip_release) continue; if (optimize_mode != .Debug and skip_release) continue;