From 76220781279a2dc42572b7819a834aebb85eb354 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 21 Oct 2022 17:51:54 +0300 Subject: aarc64 C ABI: fix handling of packed structs and unions * Packed unions were not handled at all previously. * Packed unions and structs are integers so their floats should not be counted. --- src/arch/aarch64/abi.zig | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/arch/aarch64/abi.zig b/src/arch/aarch64/abi.zig index 7c92d4e91c..57343ee4b1 100644 --- a/src/arch/aarch64/abi.zig +++ b/src/arch/aarch64/abi.zig @@ -9,23 +9,24 @@ pub const Class = enum(u8) { memory, integer, none, float_array, _ }; /// For `float_array` the second element will be the amount of floats. pub fn classifyType(ty: Type, target: std.Target) [2]Class { - var maybe_float_bits: ?u16 = null; - const float_count = countFloats(ty, target, &maybe_float_bits); - if (float_count <= sret_float_count) return .{ .float_array, @intToEnum(Class, float_count) }; - return classifyTypeInner(ty, target); -} - -fn classifyTypeInner(ty: Type, target: std.Target) [2]Class { if (!ty.hasRuntimeBitsIgnoreComptime()) return .{ .none, .none }; + var maybe_float_bits: ?u16 = null; switch (ty.zigTypeTag()) { .Struct => { if (ty.containerLayout() == .Packed) return .{ .integer, .none }; + const float_count = countFloats(ty, target, &maybe_float_bits); + if (float_count <= sret_float_count) return .{ .float_array, @intToEnum(Class, float_count) }; + const bit_size = ty.bitSize(target); if (bit_size > 128) return .{ .memory, .none }; if (bit_size > 64) return .{ .integer, .integer }; return .{ .integer, .none }; }, .Union => { + if (ty.containerLayout() == .Packed) return .{ .integer, .none }; + const float_count = countFloats(ty, target, &maybe_float_bits); + if (float_count <= sret_float_count) return .{ .float_array, @intToEnum(Class, float_count) }; + const bit_size = ty.bitSize(target); if (bit_size > 128) return .{ .memory, .none }; if (bit_size > 64) return .{ .integer, .integer }; -- cgit v1.2.3 From 9ae78a5890602b89113e53884039ba537c7ef6f9 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 21 Oct 2022 17:53:29 +0300 Subject: stage2: implement ARM C ABI Six new passing tests and the previously incorrectly passing complex tests are now skipped. --- src/arch/arm/abi.zig | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/codegen/llvm.zig | 79 ++++++++++++++++++++++++++++ test/c_abi/cfuncs.c | 4 ++ test/c_abi/main.zig | 9 +--- 4 files changed, 228 insertions(+), 7 deletions(-) diff --git a/src/arch/arm/abi.zig b/src/arch/arm/abi.zig index 3fcfb0e561..df3794d6ed 100644 --- a/src/arch/arm/abi.zig +++ b/src/arch/arm/abi.zig @@ -2,6 +2,149 @@ const std = @import("std"); const bits = @import("bits.zig"); const Register = bits.Register; const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; +const Type = @import("../../type.zig").Type; + +pub const Class = union(enum) { + memory, + byval, + none, + i32_array: u8, + i64_array: u8, + + fn arrSize(total_size: u64, arr_size: u64) Class { + const count = @intCast(u8, std.mem.alignForward(total_size, arr_size) / arr_size); + if (arr_size == 32) { + return .{ .i32_array = count }; + } else { + return .{ .i64_array = count }; + } + } +}; + +pub fn classifyType(ty: Type, target: std.Target) Class { + if (!ty.hasRuntimeBitsIgnoreComptime()) return .none; + + var maybe_float_bits: ?u16 = null; + const max_byval_size = 512; + switch (ty.zigTypeTag()) { + .Struct => { + const bit_size = ty.bitSize(target); + if (ty.containerLayout() == .Packed) { + if (bit_size > 64) return .memory; + return .byval; + } + if (bit_size > max_byval_size) return .memory; + const float_count = countFloats(ty, target, &maybe_float_bits); + if (float_count <= byval_float_count) return .byval; + + const fields = ty.structFieldCount(); + var i: u32 = 0; + while (i < fields) : (i += 1) { + const field_ty = ty.structFieldType(i); + const field_alignment = ty.structFieldAlign(i, target); + const field_size = field_ty.bitSize(target); + if (field_size > 32 or field_alignment > 32) { + return Class.arrSize(bit_size, 64); + } + } + return Class.arrSize(bit_size, 32); + }, + .Union => { + const bit_size = ty.bitSize(target); + if (ty.containerLayout() == .Packed) { + if (bit_size > 64) return .memory; + return .byval; + } + if (bit_size > max_byval_size) return .memory; + const float_count = countFloats(ty, target, &maybe_float_bits); + if (float_count <= byval_float_count) return .byval; + + for (ty.unionFields().values()) |field| { + if (field.ty.bitSize(target) > 32 or field.normalAlignment(target) > 32) { + return Class.arrSize(bit_size, 64); + } + } + return Class.arrSize(bit_size, 32); + }, + .Int, .Enum => { + const bit_size = ty.bitSize(target); + if (bit_size > 64) return .memory; + return .byval; + }, + .ErrorSet, .Vector, .Float, .Bool => { + const bit_size = ty.bitSize(target); + if (bit_size > 128) return .memory; + return .byval; + }, + .Optional => { + std.debug.assert(ty.isPtrLikeOptional()); + return .byval; + }, + .Pointer => { + std.debug.assert(!ty.isSlice()); + return .byval; + }, + .ErrorUnion, + .Frame, + .AnyFrame, + .NoReturn, + .Void, + .Type, + .ComptimeFloat, + .ComptimeInt, + .Undefined, + .Null, + .BoundFn, + .Fn, + .Opaque, + .EnumLiteral, + .Array, + => unreachable, + } +} + +const byval_float_count = 4; +fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u32 { + const invalid = std.math.maxInt(u32); + switch (ty.zigTypeTag()) { + .Union => { + const fields = ty.unionFields(); + var max_count: u32 = 0; + for (fields.values()) |field| { + const field_count = countFloats(field.ty, target, maybe_float_bits); + if (field_count == invalid) return invalid; + if (field_count > max_count) max_count = field_count; + if (max_count > byval_float_count) return invalid; + } + return max_count; + }, + .Struct => { + const fields_len = ty.structFieldCount(); + var count: u32 = 0; + var i: u32 = 0; + while (i < fields_len) : (i += 1) { + const field_ty = ty.structFieldType(i); + const field_count = countFloats(field_ty, target, maybe_float_bits); + if (field_count == invalid) return invalid; + count += field_count; + if (count > byval_float_count) return invalid; + } + return count; + }, + .Float => { + const float_bits = maybe_float_bits.* orelse { + const float_bits = ty.floatBits(target); + if (float_bits != 32 and float_bits != 64) return invalid; + maybe_float_bits.* = float_bits; + return 1; + }; + if (ty.floatBits(target) == float_bits) return 1; + return invalid; + }, + .Void => return 0, + else => return invalid, + } +} pub const callee_preserved_regs = [_]Register{ .r4, .r5, .r6, .r7, .r8, .r10 }; pub const caller_preserved_regs = [_]Register{ .r0, .r1, .r2, .r3 }; diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index b0d1588007..84b2145893 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -24,6 +24,7 @@ const CType = @import("../type.zig").CType; const x86_64_abi = @import("../arch/x86_64/abi.zig"); const wasm_c_abi = @import("../arch/wasm/abi.zig"); const aarch64_c_abi = @import("../arch/aarch64/abi.zig"); +const arm_c_abi = @import("../arch/arm/abi.zig"); const Error = error{ OutOfMemory, CodegenFail }; @@ -1130,6 +1131,25 @@ pub const Object = struct { const casted_ptr = builder.buildBitCast(arg_ptr, param.typeOf().pointerType(0), ""); _ = builder.buildStore(param, casted_ptr); + if (isByRef(param_ty)) { + try args.append(arg_ptr); + } else { + const load_inst = builder.buildLoad(param_llvm_ty, arg_ptr, ""); + load_inst.setAlignment(alignment); + try args.append(load_inst); + } + }, + .i32_array, .i64_array => { + const param_ty = fn_info.param_types[it.zig_index - 1]; + const param_llvm_ty = try dg.lowerType(param_ty); + const param = llvm_func.getParam(llvm_arg_i); + llvm_arg_i += 1; + + const alignment = param_ty.abiAlignment(target); + const arg_ptr = buildAllocaInner(builder, llvm_func, false, param_llvm_ty, alignment, target); + const casted_ptr = builder.buildBitCast(arg_ptr, param.typeOf().pointerType(0), ""); + _ = builder.buildStore(param, casted_ptr); + if (isByRef(param_ty)) { try args.append(arg_ptr); } else { @@ -2578,6 +2598,8 @@ pub const DeclGen = struct { .multiple_llvm_float, .as_u16, .float_array, + .i32_array, + .i64_array, => continue, .slice => unreachable, // extern functions do not support slice types. @@ -3132,6 +3154,11 @@ pub const DeclGen = struct { const arr_ty = float_ty.arrayType(field_count); try llvm_params.append(arr_ty); }, + .i32_array, .i64_array => |arr_len| { + const elem_size: u8 = if (lowering == .i32_array) 32 else 64; + const arr_ty = dg.context.intType(elem_size).arrayType(arr_len); + try llvm_params.append(arr_ty); + }, }; return llvm.functionType( @@ -4821,6 +4848,25 @@ pub const FuncGen = struct { load_inst.setAlignment(alignment); try llvm_args.append(load_inst); }, + .i32_array, .i64_array => |arr_len| { + const elem_size: u8 = if (lowering == .i32_array) 32 else 64; + const arg = args[it.zig_index - 1]; + const arg_ty = self.air.typeOf(arg); + var llvm_arg = try self.resolveInst(arg); + if (!isByRef(arg_ty)) { + const p = self.buildAlloca(llvm_arg.typeOf(), null); + const store_inst = self.builder.buildStore(llvm_arg, p); + store_inst.setAlignment(arg_ty.abiAlignment(target)); + llvm_arg = store_inst; + } + + const array_llvm_ty = self.dg.context.intType(elem_size).arrayType(arr_len); + const casted = self.builder.buildBitCast(llvm_arg, array_llvm_ty.pointerType(0), ""); + const alignment = arg_ty.abiAlignment(target); + const load_inst = self.builder.buildLoad(array_llvm_ty, casted, ""); + load_inst.setAlignment(alignment); + try llvm_args.append(load_inst); + }, }; const call = self.builder.buildCall( @@ -10068,6 +10114,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool }, .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect, .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target)[0] == .memory, + .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target)) { + .memory, .i64_array => return true, + .i32_array => |size| return size != 1, + .none, .byval => return false, + }, else => return false, // TODO investigate C ABI for other architectures }, else => return false, @@ -10195,6 +10246,18 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { return dg.context.intType(64).arrayType(2); }, + .arm, .armeb => { + switch (arm_c_abi.classifyType(fn_info.return_type, target)) { + .memory, .i64_array => return dg.context.voidType(), + .i32_array => |len| if (len == 1) { + return dg.context.intType(32); + } else { + return dg.context.voidType(); + }, + .byval => return dg.lowerType(fn_info.return_type), + .none => unreachable, + } + }, // TODO investigate C ABI for other architectures else => return dg.lowerType(fn_info.return_type), } @@ -10223,6 +10286,8 @@ const ParamTypeIterator = struct { slice, as_u16, float_array: u8, + i32_array: u8, + i64_array: u8, }; pub fn next(it: *ParamTypeIterator) ?Lowering { @@ -10410,6 +10475,20 @@ const ParamTypeIterator = struct { it.llvm_types_buffer[1] = 64; return .multiple_llvm_ints; }, + .arm, .armeb => { + it.zig_index += 1; + it.llvm_index += 1; + switch (arm_c_abi.classifyType(ty, it.target)) { + .none => unreachable, + .memory => { + it.byval_attr = true; + return .byref; + }, + .byval => return .byval, + .i32_array => |size| return Lowering{ .i32_array = size }, + .i64_array => |size| return Lowering{ .i64_array = size }, + } + }, // TODO investigate C ABI for other architectures else => { it.zig_index += 1; diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index 3d169badc2..434b506153 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -32,6 +32,10 @@ static void assert_or_panic(bool ok) { # define ZIG_NO_COMPLEX #endif +#ifdef __arm__ +# define ZIG_NO_COMPLEX +#endif + #ifndef ZIG_NO_I128 struct i128 { __int128 value; diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index b239fd6ce3..baa43689b4 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -167,7 +167,8 @@ extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble; extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat; extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble; -const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS(); +const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS() and + !builtin.cpu.arch.isARM(); test "C ABI complex float" { if (!complex_abi_compatible) return error.SkipZigTest; @@ -320,7 +321,6 @@ extern fn c_ret_med_struct_mixed() MedStructMixed; test "C ABI medium struct of ints and floats" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; @@ -353,7 +353,6 @@ extern fn c_ret_small_struct_ints() SmallStructInts; test "C ABI small struct of ints" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; @@ -435,7 +434,6 @@ extern fn c_split_struct_ints(SplitStructInt) void; test "C ABI split struct of ints" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; @@ -463,7 +461,6 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed; test "C ABI split struct of ints and floats" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; @@ -543,7 +540,6 @@ const Vector5 = extern struct { extern fn c_big_struct_floats(Vector5) void; test "C ABI structs of floats as parameter" { - if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; @@ -725,7 +721,6 @@ extern fn c_ret_struct_with_array() StructWithArray; test "Struct with array as padding." { if (builtin.cpu.arch == .i386) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; -- cgit v1.2.3 From 3981250b84b4eb4a34832e3fa5888aa3442e8a74 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 21 Oct 2022 19:20:48 +0300 Subject: aarch64 C ABI: return union instead of array of two enums The result is much cleaner and the second element was unused most of the time. --- src/arch/aarch64/abi.zig | 42 +++++++++++++++++++++--------------------- src/codegen/llvm.zig | 46 +++++++++++++++++++--------------------------- 2 files changed, 40 insertions(+), 48 deletions(-) diff --git a/src/arch/aarch64/abi.zig b/src/arch/aarch64/abi.zig index 57343ee4b1..58b769ef52 100644 --- a/src/arch/aarch64/abi.zig +++ b/src/arch/aarch64/abi.zig @@ -5,42 +5,41 @@ const Register = bits.Register; const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; const Type = @import("../../type.zig").Type; -pub const Class = enum(u8) { memory, integer, none, float_array, _ }; +pub const Class = union(enum) { memory, integer, double_integer, none, float_array: u8 }; /// For `float_array` the second element will be the amount of floats. -pub fn classifyType(ty: Type, target: std.Target) [2]Class { - if (!ty.hasRuntimeBitsIgnoreComptime()) return .{ .none, .none }; +pub fn classifyType(ty: Type, target: std.Target) Class { + if (!ty.hasRuntimeBitsIgnoreComptime()) return .none; var maybe_float_bits: ?u16 = null; switch (ty.zigTypeTag()) { .Struct => { - if (ty.containerLayout() == .Packed) return .{ .integer, .none }; + if (ty.containerLayout() == .Packed) return .integer; const float_count = countFloats(ty, target, &maybe_float_bits); - if (float_count <= sret_float_count) return .{ .float_array, @intToEnum(Class, float_count) }; + if (float_count <= sret_float_count) return .{ .float_array = float_count }; const bit_size = ty.bitSize(target); - if (bit_size > 128) return .{ .memory, .none }; - if (bit_size > 64) return .{ .integer, .integer }; - return .{ .integer, .none }; + if (bit_size > 128) return .memory; + if (bit_size > 64) return .double_integer; + return .integer; }, .Union => { - if (ty.containerLayout() == .Packed) return .{ .integer, .none }; + if (ty.containerLayout() == .Packed) return .integer; const float_count = countFloats(ty, target, &maybe_float_bits); - if (float_count <= sret_float_count) return .{ .float_array, @intToEnum(Class, float_count) }; + if (float_count <= sret_float_count) return .{ .float_array = float_count }; const bit_size = ty.bitSize(target); - if (bit_size > 128) return .{ .memory, .none }; - if (bit_size > 64) return .{ .integer, .integer }; - return .{ .integer, .none }; + if (bit_size > 128) return .memory; + if (bit_size > 64) return .double_integer; + return .integer; }, - .Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .{ .integer, .none }, - .Array => return .{ .memory, .none }, + .Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .integer, .Optional => { std.debug.assert(ty.isPtrLikeOptional()); - return .{ .integer, .none }; + return .integer; }, .Pointer => { std.debug.assert(!ty.isSlice()); - return .{ .integer, .none }; + return .integer; }, .ErrorUnion, .Frame, @@ -56,17 +55,18 @@ pub fn classifyType(ty: Type, target: std.Target) [2]Class { .Fn, .Opaque, .EnumLiteral, + .Array, => unreachable, } } const sret_float_count = 4; -fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u32 { - const invalid = std.math.maxInt(u32); +fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u8 { + const invalid = std.math.maxInt(u8); switch (ty.zigTypeTag()) { .Union => { const fields = ty.unionFields(); - var max_count: u32 = 0; + var max_count: u8 = 0; for (fields.values()) |field| { const field_count = countFloats(field.ty, target, maybe_float_bits); if (field_count == invalid) return invalid; @@ -77,7 +77,7 @@ fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u32 { }, .Struct => { const fields_len = ty.structFieldCount(); - var count: u32 = 0; + var count: u8 = 0; var i: u32 = 0; while (i < fields_len) : (i += 1) { const field_ty = ty.structFieldType(i); diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 84b2145893..b3c8e225dc 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -10113,7 +10113,7 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory, }, .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect, - .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target)[0] == .memory, + .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target) == .memory, .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target)) { .memory, .i64_array => return true, .i32_array => |size| return size != 1, @@ -10232,19 +10232,15 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { if (is_scalar) { return dg.lowerType(fn_info.return_type); } - const classes = aarch64_c_abi.classifyType(fn_info.return_type, target); - if (classes[0] == .memory or classes[0] == .none) { - return dg.context.voidType(); - } - if (classes[0] == .float_array) { - return dg.lowerType(fn_info.return_type); - } - if (classes[1] == .none) { - const bit_size = fn_info.return_type.bitSize(target); - return dg.context.intType(@intCast(c_uint, bit_size)); + switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) { + .memory, .none => return dg.context.voidType(), + .float_array => return dg.lowerType(fn_info.return_type), + .integer => { + const bit_size = fn_info.return_type.bitSize(target); + return dg.context.intType(@intCast(c_uint, bit_size)); + }, + .double_integer => return dg.context.intType(64).arrayType(2), } - - return dg.context.intType(64).arrayType(2); }, .arm, .armeb => { switch (arm_c_abi.classifyType(fn_info.return_type, target)) { @@ -10459,21 +10455,17 @@ const ParamTypeIterator = struct { if (is_scalar) { return .byval; } - const classes = aarch64_c_abi.classifyType(ty, it.target); - if (classes[0] == .memory) { - return .byref; - } - if (classes[0] == .float_array) { - return Lowering{ .float_array = @enumToInt(classes[1]) }; - } - if (classes[1] == .none) { - it.llvm_types_len = 1; - } else { - it.llvm_types_len = 2; + switch (aarch64_c_abi.classifyType(ty, it.target)) { + .none => unreachable, + .memory => return .byref, + .float_array => |len| return Lowering{ .float_array = len }, + .integer => { + it.llvm_types_len = 1; + it.llvm_types_buffer[0] = 64; + return .multiple_llvm_ints; + }, + .double_integer => return Lowering{ .i64_array = 2 }, } - it.llvm_types_buffer[0] = 64; - it.llvm_types_buffer[1] = 64; - return .multiple_llvm_ints; }, .arm, .armeb => { it.zig_index += 1; -- cgit v1.2.3 From 031c768cc8399ccdf5440df87d37c03a238315d5 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 21 Oct 2022 21:44:52 +0300 Subject: add C ABI tests for simd vectors --- src/arch/aarch64/abi.zig | 25 +++++++++++++++++++------ src/arch/arm/abi.zig | 13 +++++++++---- src/arch/x86_64/CodeGen.zig | 2 +- src/arch/x86_64/abi.zig | 24 +++++++++++++++++++++--- src/codegen/llvm.zig | 30 +++++++++++++----------------- test/c_abi/cfuncs.c | 38 +++++++++++++++++++++++++++++++++----- test/c_abi/main.zig | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 131 insertions(+), 36 deletions(-) diff --git a/src/arch/aarch64/abi.zig b/src/arch/aarch64/abi.zig index 58b769ef52..9471bb57f9 100644 --- a/src/arch/aarch64/abi.zig +++ b/src/arch/aarch64/abi.zig @@ -5,7 +5,14 @@ const Register = bits.Register; const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; const Type = @import("../../type.zig").Type; -pub const Class = union(enum) { memory, integer, double_integer, none, float_array: u8 }; +pub const Class = union(enum) { + memory, + byval, + integer, + double_integer, + none, + float_array: u8, +}; /// For `float_array` the second element will be the amount of floats. pub fn classifyType(ty: Type, target: std.Target) Class { @@ -13,7 +20,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class { var maybe_float_bits: ?u16 = null; switch (ty.zigTypeTag()) { .Struct => { - if (ty.containerLayout() == .Packed) return .integer; + if (ty.containerLayout() == .Packed) return .byval; const float_count = countFloats(ty, target, &maybe_float_bits); if (float_count <= sret_float_count) return .{ .float_array = float_count }; @@ -23,7 +30,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class { return .integer; }, .Union => { - if (ty.containerLayout() == .Packed) return .integer; + if (ty.containerLayout() == .Packed) return .byval; const float_count = countFloats(ty, target, &maybe_float_bits); if (float_count <= sret_float_count) return .{ .float_array = float_count }; @@ -32,14 +39,20 @@ pub fn classifyType(ty: Type, target: std.Target) Class { if (bit_size > 64) return .double_integer; return .integer; }, - .Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .integer, + .Int, .Enum, .ErrorSet, .Float, .Bool => return .byval, + .Vector => { + const bit_size = ty.bitSize(target); + // TODO is this controlled by a cpu feature? + if (bit_size > 128) return .memory; + return .byval; + }, .Optional => { std.debug.assert(ty.isPtrLikeOptional()); - return .integer; + return .byval; }, .Pointer => { std.debug.assert(!ty.isSlice()); - return .integer; + return .byval; }, .ErrorUnion, .Frame, diff --git a/src/arch/arm/abi.zig b/src/arch/arm/abi.zig index df3794d6ed..2ffca4e848 100644 --- a/src/arch/arm/abi.zig +++ b/src/arch/arm/abi.zig @@ -21,7 +21,9 @@ pub const Class = union(enum) { } }; -pub fn classifyType(ty: Type, target: std.Target) Class { +pub const Context = enum { ret, arg }; + +pub fn classifyType(ty: Type, target: std.Target, ctx: Context) Class { if (!ty.hasRuntimeBitsIgnoreComptime()) return .none; var maybe_float_bits: ?u16 = null; @@ -66,14 +68,17 @@ pub fn classifyType(ty: Type, target: std.Target) Class { } return Class.arrSize(bit_size, 32); }, - .Int, .Enum => { + .Bool, .Float => return .byval, + .Int, .Enum, .ErrorSet => { const bit_size = ty.bitSize(target); if (bit_size > 64) return .memory; return .byval; }, - .ErrorSet, .Vector, .Float, .Bool => { + .Vector => { const bit_size = ty.bitSize(target); - if (bit_size > 128) return .memory; + // TODO is this controlled by a cpu feature? + if (ctx == .ret and bit_size > 128) return .memory; + if (bit_size > 512) return .memory; return .byval; }, .Optional => { diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index a3888b4173..9a5696c6d7 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -7143,7 +7143,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { const classes: []const abi.Class = switch (self.target.os.tag) { .windows => &[1]abi.Class{abi.classifyWindows(ty, self.target.*)}, - else => mem.sliceTo(&abi.classifySystemV(ty, self.target.*), .none), + else => mem.sliceTo(&abi.classifySystemV(ty, self.target.*, .arg), .none), }; if (classes.len > 1) { return self.fail("TODO handle multiple classes per type", .{}); diff --git a/src/arch/x86_64/abi.zig b/src/arch/x86_64/abi.zig index 45c5760540..a428bcacdd 100644 --- a/src/arch/x86_64/abi.zig +++ b/src/arch/x86_64/abi.zig @@ -60,9 +60,11 @@ pub fn classifyWindows(ty: Type, target: Target) Class { } } +pub const Context = enum { ret, arg }; + /// There are a maximum of 8 possible return slots. Returned values are in /// the beginning of the array; unused slots are filled with .none. -pub fn classifySystemV(ty: Type, target: Target) [8]Class { +pub fn classifySystemV(ty: Type, target: Target, ctx: Context) [8]Class { const memory_class = [_]Class{ .memory, .none, .none, .none, .none, .none, .none, .none, @@ -134,6 +136,22 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class { }, .Vector => { const elem_ty = ty.childType(); + if (ctx == .arg) { + const bit_size = ty.bitSize(target); + if (bit_size > 128) return memory_class; + if (bit_size > 80) return .{ + .integer, .integer, .none, .none, + .none, .none, .none, .none, + }; + if (bit_size > 64) return .{ + .x87, .none, .none, .none, + .none, .none, .none, .none, + }; + return .{ + .integer, .none, .none, .none, + .none, .none, .none, .none, + }; + } const bits = elem_ty.bitSize(target) * ty.arrayLen(); if (bits <= 64) return .{ .sse, .none, .none, .none, @@ -201,7 +219,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class { } } const field_size = field.ty.abiSize(target); - const field_class_array = classifySystemV(field.ty, target); + const field_class_array = classifySystemV(field.ty, target, .arg); const field_class = std.mem.sliceTo(&field_class_array, .none); if (byte_i + field_size <= 8) { // Combine this field with the previous one. @@ -315,7 +333,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class { } } // Combine this field with the previous one. - const field_class = classifySystemV(field.ty, target); + const field_class = classifySystemV(field.ty, target, .arg); for (result) |*result_item, i| { const field_item = field_class[i]; // "If both classes are equal, this is the resulting class." diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index b3c8e225dc..cc8ab10e4b 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -10110,11 +10110,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool .mips, .mipsel => return false, .x86_64 => switch (target.os.tag) { .windows => return x86_64_abi.classifyWindows(fn_info.return_type, target) == .memory, - else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory, + else => return x86_64_abi.classifySystemV(fn_info.return_type, target, .ret)[0] == .memory, }, .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect, .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target) == .memory, - .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target)) { + .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) { .memory, .i64_array => return true, .i32_array => |size| return size != 1, .none, .byval => return false, @@ -10171,7 +10171,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { if (is_scalar) { return dg.lowerType(fn_info.return_type); } - const classes = x86_64_abi.classifySystemV(fn_info.return_type, target); + const classes = x86_64_abi.classifySystemV(fn_info.return_type, target, .ret); if (classes[0] == .memory) { return dg.context.voidType(); } @@ -10229,12 +10229,10 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { return dg.context.intType(@intCast(c_uint, abi_size * 8)); }, .aarch64, .aarch64_be => { - if (is_scalar) { - return dg.lowerType(fn_info.return_type); - } switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) { .memory, .none => return dg.context.voidType(), .float_array => return dg.lowerType(fn_info.return_type), + .byval => return dg.lowerType(fn_info.return_type), .integer => { const bit_size = fn_info.return_type.bitSize(target); return dg.context.intType(@intCast(c_uint, bit_size)); @@ -10243,7 +10241,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { } }, .arm, .armeb => { - switch (arm_c_abi.classifyType(fn_info.return_type, target)) { + switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) { .memory, .i64_array => return dg.context.voidType(), .i32_array => |len| if (len == 1) { return dg.context.intType(32); @@ -10376,18 +10374,18 @@ const ParamTypeIterator = struct { else => unreachable, }, else => { - if (is_scalar) { - it.zig_index += 1; - it.llvm_index += 1; - return .byval; - } - const classes = x86_64_abi.classifySystemV(ty, it.target); + const classes = x86_64_abi.classifySystemV(ty, it.target, .arg); if (classes[0] == .memory) { it.zig_index += 1; it.llvm_index += 1; it.byval_attr = true; return .byref; } + if (is_scalar) { + it.zig_index += 1; + it.llvm_index += 1; + return .byval; + } var llvm_types_buffer: [8]u16 = undefined; var llvm_types_index: u32 = 0; for (classes) |class| { @@ -10452,13 +10450,11 @@ const ParamTypeIterator = struct { .aarch64, .aarch64_be => { it.zig_index += 1; it.llvm_index += 1; - if (is_scalar) { - return .byval; - } switch (aarch64_c_abi.classifyType(ty, it.target)) { .none => unreachable, .memory => return .byref, .float_array => |len| return Lowering{ .float_array = len }, + .byval => return .byval, .integer => { it.llvm_types_len = 1; it.llvm_types_buffer[0] = 64; @@ -10470,7 +10466,7 @@ const ParamTypeIterator = struct { .arm, .armeb => { it.zig_index += 1; it.llvm_index += 1; - switch (arm_c_abi.classifyType(ty, it.target)) { + switch (arm_c_abi.classifyType(ty, it.target, .arg)) { .none => unreachable, .memory => { it.byval_attr = true; diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index 434b506153..b6a2007a9f 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -1,8 +1,8 @@ +#include #include -#include #include +#include #include -#include void zig_panic(); @@ -210,7 +210,7 @@ void run_c_tests(void) { zig_longdouble(12.34l); zig_five_floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f); - zig_ptr((void*)0xdeadbeefL); + zig_ptr((void *)0xdeadbeefL); zig_bool(true); @@ -408,7 +408,7 @@ void c_long_double(long double x) { } void c_ptr(void *x) { - assert_or_panic(x == (void*)0xdeadbeefL); + assert_or_panic(x == (void *)0xdeadbeefL); } void c_bool(bool x) { @@ -676,7 +676,7 @@ void c_struct_with_array(StructWithArray x) { } StructWithArray c_ret_struct_with_array() { - return (StructWithArray) { 4, {}, 155 }; + return (StructWithArray){4, {}, 155}; } typedef struct { @@ -705,3 +705,31 @@ FloatArrayStruct c_ret_float_array_struct() { x.size.height = 4; return x; } + +typedef uint32_t SmallVec __attribute__((vector_size(2 * sizeof(uint32_t)))); + +void c_small_vec(SmallVec vec) { + assert_or_panic(vec[0] == 1); + assert_or_panic(vec[1] == 2); +} + +SmallVec c_ret_small_vec(void) { + return (SmallVec){3, 4}; +} + +typedef size_t BigVec __attribute__((vector_size(8 * sizeof(size_t)))); + +void c_big_vec(BigVec vec) { + assert_or_panic(vec[0] == 1); + assert_or_panic(vec[1] == 2); + assert_or_panic(vec[2] == 3); + assert_or_panic(vec[3] == 4); + assert_or_panic(vec[4] == 5); + assert_or_panic(vec[5] == 6); + assert_or_panic(vec[6] == 7); + assert_or_panic(vec[7] == 8); +} + +BigVec c_ret_big_vec(void) { + return (BigVec){9, 10, 11, 12, 13, 14, 15, 16}; +} diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index baa43689b4..96d8753e0a 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -766,3 +766,38 @@ test "Float array like struct" { try std.testing.expect(x.size.width == 3); try std.testing.expect(x.size.height == 4); } + +const SmallVec = @Vector(2, u32); + +extern fn c_small_vec(SmallVec) void; +extern fn c_ret_small_vec() SmallVec; + +test "small simd vector" { + if (builtin.cpu.arch == .i386) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + + c_small_vec(.{ 1, 2 }); + + var x = c_ret_small_vec(); + try std.testing.expect(x[0] == 3); + try std.testing.expect(x[1] == 4); +} + +const BigVec = @Vector(8, usize); + +extern fn c_big_vec(BigVec) void; +extern fn c_ret_big_vec() BigVec; + +test "big simd vector" { + c_big_vec(.{ 1, 2, 3, 4, 5, 6, 7, 8 }); + + var x = c_ret_big_vec(); + try std.testing.expect(x[0] == 9); + try std.testing.expect(x[1] == 10); + try std.testing.expect(x[2] == 11); + try std.testing.expect(x[3] == 12); + try std.testing.expect(x[4] == 13); + try std.testing.expect(x[5] == 14); + try std.testing.expect(x[6] == 15); + try std.testing.expect(x[7] == 16); +} -- cgit v1.2.3 From 12a2ccfb459e89f6a59c14de36bf80bf51a0a517 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 21 Oct 2022 23:00:49 +0300 Subject: make C ABI tests compile on powerpc --- test/c_abi/cfuncs.c | 31 +++++++++++++++++++++++-------- test/c_abi/main.zig | 29 +++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index b6a2007a9f..a6457a1f15 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -12,6 +12,10 @@ static void assert_or_panic(bool ok) { } } +#if defined __powerpc__ && !defined _ARCH_PPC64 +# define ZIG_PPC32 +#endif + #ifdef __i386__ # define ZIG_NO_I128 #endif @@ -24,6 +28,10 @@ static void assert_or_panic(bool ok) { # define ZIG_NO_I128 #endif +#ifdef ZIG_PPC32 +# define ZIG_NO_I128 +#endif + #ifdef __i386__ # define ZIG_NO_COMPLEX #endif @@ -36,6 +44,10 @@ static void assert_or_panic(bool ok) { # define ZIG_NO_COMPLEX #endif +#ifdef __powerpc__ +# define ZIG_NO_COMPLEX +#endif + #ifndef ZIG_NO_I128 struct i128 { __int128 value; @@ -253,14 +265,15 @@ void run_c_tests(void) { } #endif -#if !defined __mips__ && !defined __riscv +#if !defined __mips__ && !defined __riscv && !defined ZIG_PPC32 { struct BigStruct s = {1, 2, 3, 4, 5}; zig_big_struct(s); } #endif -#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv +#if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ + !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct SmallStructInts s = {1, 2, 3, 4}; zig_small_struct_ints(s); @@ -285,28 +298,30 @@ void run_c_tests(void) { zig_small_packed_struct(s); } -#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv +#if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ + !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct SplitStructInts s = {1234, 100, 1337}; zig_split_struct_ints(s); } #endif -#if !defined __arm__ && !defined __riscv +#if !defined __arm__ && !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct MedStructMixed s = {1234, 100.0f, 1337.0f}; zig_med_struct_mixed(s); } #endif -#if !defined __i386__ && !defined __arm__ && !defined __mips__ && !defined __riscv +#if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ + !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct SplitStructMixed s = {1234, 100, 1337.0f}; zig_split_struct_mixed(s); } #endif -#if !defined __mips__ && !defined __riscv +#if !defined __mips__ && !defined __riscv && !defined ZIG_PPC32 { struct BigStruct s = {30, 31, 32, 33, 34}; struct BigStruct res = zig_big_struct_both(s); @@ -318,7 +333,7 @@ void run_c_tests(void) { } #endif -#ifndef __riscv +#if !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct Rect r1 = {1, 21, 16, 4}; struct Rect r2 = {178, 189, 21, 15}; @@ -326,7 +341,7 @@ void run_c_tests(void) { } #endif -#if !defined __mips__ && !defined __riscv +#if !defined __mips__ && !defined __riscv && !defined ZIG_PPC32 { struct FloatRect r1 = {1, 21, 16, 4}; struct FloatRect r2 = {178, 189, 21, 15}; diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index 96d8753e0a..14364d34f8 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -3,7 +3,7 @@ const builtin = @import("builtin"); const print = std.debug.print; const expect = std.testing.expect; const has_i128 = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isARM() and - !builtin.cpu.arch.isMIPS(); + !builtin.cpu.arch.isMIPS() and !builtin.cpu.arch.isPPC(); extern fn run_c_tests() void; @@ -112,6 +112,9 @@ test "C ABI floats" { } test "C ABI long double" { + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; + c_long_double(12.34); } @@ -168,7 +171,7 @@ extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat; extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble; const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS() and - !builtin.cpu.arch.isARM(); + !builtin.cpu.arch.isARM() and !builtin.cpu.arch.isPPC(); test "C ABI complex float" { if (!complex_abi_compatible) return error.SkipZigTest; @@ -263,6 +266,7 @@ extern fn c_big_struct(BigStruct) void; test "C ABI big struct" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var s = BigStruct{ .a = 1, @@ -289,6 +293,7 @@ extern fn c_big_union(BigUnion) void; test "C ABI big union" { if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var x = BigUnion{ .a = BigStruct{ @@ -323,6 +328,8 @@ test "C ABI medium struct of ints and floats" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; var s = MedStructMixed{ .a = 1234, @@ -355,6 +362,8 @@ test "C ABI small struct of ints" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; var s = SmallStructInts{ .a = 1, @@ -436,6 +445,8 @@ test "C ABI split struct of ints" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; var s = SplitStructInt{ .a = 1234, @@ -463,6 +474,8 @@ test "C ABI split struct of ints and floats" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; var s = SplitStructMixed{ .a = 1234, @@ -490,6 +503,7 @@ extern fn c_multiple_struct_floats(FloatRect, FloatRect) void; test "C ABI sret and byval together" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var s = BigStruct{ .a = 1, @@ -542,6 +556,8 @@ extern fn c_big_struct_floats(Vector5) void; test "C ABI structs of floats as parameter" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; var v3 = Vector3{ .x = 3.0, @@ -581,6 +597,8 @@ export fn zig_multiple_struct_ints(x: Rect, y: Rect) void { test "C ABI structs of ints as multiple parameters" { if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; var r1 = Rect{ .left = 1, @@ -618,6 +636,7 @@ export fn zig_multiple_struct_floats(x: FloatRect, y: FloatRect) void { test "C ABI structs of floats as multiple parameters" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var r1 = FloatRect{ .left = 1, @@ -723,6 +742,8 @@ test "Struct with array as padding." { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 }); @@ -748,6 +769,7 @@ extern fn c_ret_float_array_struct() FloatArrayStruct; test "Float array like struct" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; c_float_array_struct(.{ .origin = .{ @@ -775,6 +797,7 @@ extern fn c_ret_small_vec() SmallVec; test "small simd vector" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; c_small_vec(.{ 1, 2 }); @@ -789,6 +812,8 @@ extern fn c_big_vec(BigVec) void; extern fn c_ret_big_vec() BigVec; test "big simd vector" { + if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; + c_big_vec(.{ 1, 2, 3, 4, 5, 6, 7, 8 }); var x = c_ret_big_vec(); -- cgit v1.2.3 From 8fa91939a880ac5394aaac457238251d2c937059 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 21 Oct 2022 23:01:49 +0300 Subject: build.zig: separate C ABI tests from standalone tests --- build.zig | 1 + test/c_abi/build.zig | 22 ------------- test/c_abi/build_wasm.zig | 24 -------------- test/standalone.zig | 17 ---------- test/tests.zig | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 63 deletions(-) delete mode 100644 test/c_abi/build.zig delete mode 100644 test/c_abi/build_wasm.zig diff --git a/build.zig b/build.zig index 2dd047a1e3..a1ba2b20bc 100644 --- a/build.zig +++ b/build.zig @@ -508,6 +508,7 @@ pub fn build(b: *Builder) !void { b.enable_wasmtime, b.enable_wine, )); + test_step.dependOn(tests.addCAbiTests(b, skip_non_native)); test_step.dependOn(tests.addLinkTests(b, test_filter, modes, enable_macos_sdk, skip_stage2_tests)); test_step.dependOn(tests.addStackTraceTests(b, test_filter, modes)); test_step.dependOn(tests.addCliTests(b, test_filter, modes)); diff --git a/test/c_abi/build.zig b/test/c_abi/build.zig deleted file mode 100644 index b9151f6dda..0000000000 --- a/test/c_abi/build.zig +++ /dev/null @@ -1,22 +0,0 @@ -const Builder = @import("std").build.Builder; - -pub fn build(b: *Builder) void { - const rel_opts = b.standardReleaseOptions(); - const target = b.standardTargetOptions(.{}); - - const c_obj = b.addObject("cfuncs", null); - c_obj.addCSourceFile("cfuncs.c", &[_][]const u8{"-std=c99"}); - c_obj.setBuildMode(rel_opts); - c_obj.linkSystemLibrary("c"); - c_obj.target = target; - - const main = b.addTest("main.zig"); - main.setBuildMode(rel_opts); - main.addObject(c_obj); - main.target = target; - - const test_step = b.step("test", "Test the program"); - test_step.dependOn(&main.step); - - b.default_step.dependOn(test_step); -} diff --git a/test/c_abi/build_wasm.zig b/test/c_abi/build_wasm.zig deleted file mode 100644 index b288650855..0000000000 --- a/test/c_abi/build_wasm.zig +++ /dev/null @@ -1,24 +0,0 @@ -const std = @import("std"); -const Builder = std.build.Builder; - -pub fn build(b: *Builder) void { - const rel_opts = b.standardReleaseOptions(); - const target: std.zig.CrossTarget = .{ .cpu_arch = .wasm32, .os_tag = .wasi }; - b.use_stage1 = false; - - const c_obj = b.addObject("cfuncs", null); - c_obj.addCSourceFile("cfuncs.c", &[_][]const u8{"-std=c99"}); - c_obj.setBuildMode(rel_opts); - c_obj.linkSystemLibrary("c"); - c_obj.setTarget(target); - - const main = b.addTest("main.zig"); - main.setBuildMode(rel_opts); - main.addObject(c_obj); - main.setTarget(target); - - const test_step = b.step("test", "Test the program"); - test_step.dependOn(&main.step); - - b.default_step.dependOn(test_step); -} diff --git a/test/standalone.zig b/test/standalone.zig index 16cb61a85d..f0567943b6 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -44,23 +44,6 @@ pub fn addCases(cases: *tests.StandaloneContext) void { if (builtin.os.tag != .wasi) { cases.addBuildFile("test/standalone/load_dynamic_library/build.zig", .{}); } - // C ABI compatibility issue: https://github.com/ziglang/zig/issues/1481 - if (builtin.cpu.arch == .x86_64) { - if (builtin.zig_backend == .stage1 or builtin.zig_backend == .stage2_llvm) { - cases.addBuildFile("test/c_abi/build.zig", .{}); - } - } - if (builtin.cpu.arch.isAARCH64() and builtin.zig_backend == .stage2_llvm) { - cases.addBuildFile("test/c_abi/build.zig", .{}); - } - if (builtin.cpu.arch == .i386 and builtin.zig_backend == .stage2_llvm) { - cases.addBuildFile("test/c_abi/build.zig", .{}); - } - // C ABI tests only pass for the Wasm target when using stage2 - cases.addBuildFile("test/c_abi/build_wasm.zig", .{ - .requires_stage2 = true, - .use_emulation = true, - }); cases.addBuildFile("test/standalone/c_compiler/build.zig", .{ .build_modes = true, diff --git a/test/tests.zig b/test/tests.zig index 4dec71bc52..f204a683fb 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1268,3 +1268,82 @@ fn printInvocation(args: []const []const u8) void { } std.debug.print("\n", .{}); } + +const c_abi_targets = [_]CrossTarget{ + .{}, + .{ + .cpu_arch = .x86_64, + .os_tag = .linux, + .abi = .musl, + }, + .{ + .cpu_arch = .i386, + .os_tag = .linux, + .abi = .musl, + }, + .{ + .cpu_arch = .aarch64, + .os_tag = .linux, + .abi = .musl, + }, + .{ + .cpu_arch = .arm, + .os_tag = .linux, + .abi = .musleabihf, + }, + .{ + .cpu_arch = .mips, + .os_tag = .linux, + .abi = .musl, + }, + .{ + .cpu_arch = .riscv64, + .os_tag = .linux, + .abi = .musl, + }, + .{ + .cpu_arch = .wasm32, + .os_tag = .wasi, + .abi = .musl, + }, + .{ + .cpu_arch = .powerpc, + .os_tag = .linux, + .abi = .musl, + }, + .{ + .cpu_arch = .powerpc64le, + .os_tag = .linux, + .abi = .none, + }, +}; + +pub fn addCAbiTests(b: *build.Builder, skip_non_native: bool) *build.Step { + const step = b.step("test-c-abi", "Run the C ABI tests"); + + for (c_abi_targets) |c_abi_target| { + if (skip_non_native and !c_abi_target.isNative()) + continue; + + const test_step = b.addTest("test/c_abi/main.zig"); + test_step.setTarget(c_abi_target); + if (c_abi_target.abi != null and c_abi_target.abi.?.isMusl()) { + // TODO NativeTargetInfo insists on dynamically linking musl + // for some reason? + test_step.target_info.dynamic_linker.max_byte = null; + } + test_step.linkLibC(); + test_step.addCSourceFile("test/c_abi/cfuncs.c", &.{"-std=c99"}); + + const triple_prefix = c_abi_target.zigTriple(b.allocator) catch unreachable; + test_step.setNamePrefix(b.fmt("{s}-{s} ", .{ + "test-c-abi", + triple_prefix, + })); + + test_step.use_stage1 = false; + + step.dependOn(&test_step.step); + } + return step; +} -- cgit v1.2.3 From 5e0b4836a1f48181b0d6df2b272add5aa79654c3 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 22 Oct 2022 13:05:28 +0300 Subject: stage2: implement RISCV C ABI --- src/arch/aarch64/abi.zig | 4 +-- src/arch/arm/abi.zig | 3 +-- src/arch/riscv64/abi.zig | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ src/codegen/llvm.zig | 60 ++++++++++++++++++++++++++++++----------- test/c_abi/cfuncs.c | 30 ++++++++++++++------- test/c_abi/main.zig | 15 +---------- 6 files changed, 138 insertions(+), 43 deletions(-) diff --git a/src/arch/aarch64/abi.zig b/src/arch/aarch64/abi.zig index 9471bb57f9..e83cc0444a 100644 --- a/src/arch/aarch64/abi.zig +++ b/src/arch/aarch64/abi.zig @@ -10,13 +10,13 @@ pub const Class = union(enum) { byval, integer, double_integer, - none, float_array: u8, }; /// For `float_array` the second element will be the amount of floats. pub fn classifyType(ty: Type, target: std.Target) Class { - if (!ty.hasRuntimeBitsIgnoreComptime()) return .none; + std.debug.assert(ty.hasRuntimeBitsIgnoreComptime()); + var maybe_float_bits: ?u16 = null; switch (ty.zigTypeTag()) { .Struct => { diff --git a/src/arch/arm/abi.zig b/src/arch/arm/abi.zig index 2ffca4e848..d8bbc5d1d1 100644 --- a/src/arch/arm/abi.zig +++ b/src/arch/arm/abi.zig @@ -7,7 +7,6 @@ const Type = @import("../../type.zig").Type; pub const Class = union(enum) { memory, byval, - none, i32_array: u8, i64_array: u8, @@ -24,7 +23,7 @@ pub const Class = union(enum) { pub const Context = enum { ret, arg }; pub fn classifyType(ty: Type, target: std.Target, ctx: Context) Class { - if (!ty.hasRuntimeBitsIgnoreComptime()) return .none; + std.debug.assert(ty.hasRuntimeBitsIgnoreComptime()); var maybe_float_bits: ?u16 = null; const max_byval_size = 512; diff --git a/src/arch/riscv64/abi.zig b/src/arch/riscv64/abi.zig index 3792c4ab18..8a560f4596 100644 --- a/src/arch/riscv64/abi.zig +++ b/src/arch/riscv64/abi.zig @@ -2,6 +2,75 @@ const std = @import("std"); const bits = @import("bits.zig"); const Register = bits.Register; const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; +const Type = @import("../../type.zig").Type; + +pub const Class = enum { memory, byval, integer, double_integer }; + +pub fn classifyType(ty: Type, target: std.Target) Class { + std.debug.assert(ty.hasRuntimeBitsIgnoreComptime()); + + const max_byval_size = target.cpu.arch.ptrBitWidth() * 2; + switch (ty.zigTypeTag()) { + .Struct => { + const bit_size = ty.bitSize(target); + if (ty.containerLayout() == .Packed) { + if (bit_size > max_byval_size) return .memory; + return .byval; + } + // TODO this doesn't exactly match what clang produces but its better than nothing + if (bit_size > max_byval_size) return .memory; + if (bit_size > max_byval_size / 2) return .double_integer; + return .integer; + }, + .Union => { + const bit_size = ty.bitSize(target); + if (ty.containerLayout() == .Packed) { + if (bit_size > max_byval_size) return .memory; + return .byval; + } + // TODO this doesn't exactly match what clang produces but its better than nothing + if (bit_size > max_byval_size) return .memory; + if (bit_size > max_byval_size / 2) return .double_integer; + return .integer; + }, + .Bool => return .integer, + .Float => return .byval, + .Int, .Enum, .ErrorSet => { + const bit_size = ty.bitSize(target); + if (bit_size > max_byval_size) return .memory; + return .byval; + }, + .Vector => { + const bit_size = ty.bitSize(target); + if (bit_size > max_byval_size) return .memory; + return .integer; + }, + .Optional => { + std.debug.assert(ty.isPtrLikeOptional()); + return .byval; + }, + .Pointer => { + std.debug.assert(!ty.isSlice()); + return .byval; + }, + .ErrorUnion, + .Frame, + .AnyFrame, + .NoReturn, + .Void, + .Type, + .ComptimeFloat, + .ComptimeInt, + .Undefined, + .Null, + .BoundFn, + .Fn, + .Opaque, + .EnumLiteral, + .Array, + => unreachable, + } +} pub const callee_preserved_regs = [_]Register{ .s0, .s1, .s2, .s3, .s4, .s5, .s6, .s7, .s8, .s9, .s10, .s11, diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index cc8ab10e4b..8b10895068 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -25,6 +25,7 @@ const x86_64_abi = @import("../arch/x86_64/abi.zig"); const wasm_c_abi = @import("../arch/wasm/abi.zig"); const aarch64_c_abi = @import("../arch/aarch64/abi.zig"); const arm_c_abi = @import("../arch/arm/abi.zig"); +const riscv_c_abi = @import("../arch/riscv64/abi.zig"); const Error = error{ OutOfMemory, CodegenFail }; @@ -10117,8 +10118,9 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) { .memory, .i64_array => return true, .i32_array => |size| return size != 1, - .none, .byval => return false, + .byval => return false, }, + .riscv32, .riscv64 => return riscv_c_abi.classifyType(fn_info.return_type, target) == .memory, else => return false, // TODO investigate C ABI for other architectures }, else => return false, @@ -10230,7 +10232,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { }, .aarch64, .aarch64_be => { switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) { - .memory, .none => return dg.context.voidType(), + .memory => return dg.context.voidType(), .float_array => return dg.lowerType(fn_info.return_type), .byval => return dg.lowerType(fn_info.return_type), .integer => { @@ -10249,7 +10251,23 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { return dg.context.voidType(); }, .byval => return dg.lowerType(fn_info.return_type), - .none => unreachable, + } + }, + .riscv32, .riscv64 => { + switch (riscv_c_abi.classifyType(fn_info.return_type, target)) { + .memory => return dg.context.voidType(), + .integer => { + const bit_size = fn_info.return_type.bitSize(target); + return dg.context.intType(@intCast(c_uint, bit_size)); + }, + .double_integer => { + var llvm_types_buffer: [2]*llvm.Type = .{ + dg.context.intType(64), + dg.context.intType(64), + }; + return dg.context.structType(&llvm_types_buffer, 2, .False); + }, + .byval => return dg.lowerType(fn_info.return_type), } }, // TODO investigate C ABI for other architectures @@ -10328,15 +10346,6 @@ const ParamTypeIterator = struct { .C => { const is_scalar = isScalar(ty); switch (it.target.cpu.arch) { - .riscv32, .riscv64 => { - it.zig_index += 1; - it.llvm_index += 1; - if (ty.tag() == .f16) { - return .as_u16; - } else { - return .byval; - } - }, .mips, .mipsel => { it.zig_index += 1; it.llvm_index += 1; @@ -10451,7 +10460,6 @@ const ParamTypeIterator = struct { it.zig_index += 1; it.llvm_index += 1; switch (aarch64_c_abi.classifyType(ty, it.target)) { - .none => unreachable, .memory => return .byref, .float_array => |len| return Lowering{ .float_array = len }, .byval => return .byval, @@ -10467,7 +10475,6 @@ const ParamTypeIterator = struct { it.zig_index += 1; it.llvm_index += 1; switch (arm_c_abi.classifyType(ty, it.target, .arg)) { - .none => unreachable, .memory => { it.byval_attr = true; return .byref; @@ -10477,6 +10484,21 @@ const ParamTypeIterator = struct { .i64_array => |size| return Lowering{ .i64_array = size }, } }, + .riscv32, .riscv64 => { + it.zig_index += 1; + it.llvm_index += 1; + if (ty.tag() == .f16) { + return .as_u16; + } + switch (riscv_c_abi.classifyType(ty, it.target)) { + .memory => { + return .byref; + }, + .byval => return .byval, + .integer => return .abi_sized_int, + .double_integer => return Lowering{ .i64_array = 2 }, + } + }, // TODO investigate C ABI for other architectures else => { it.zig_index += 1; @@ -10523,8 +10545,16 @@ fn ccAbiPromoteInt( }; if (int_info.bits <= 16) return int_info.signedness; switch (target.cpu.arch) { + .riscv64 => { + if (int_info.bits == 32) { + // LLVM always signextends 32 bit ints, unsure if bug. + return .signed; + } + if (int_info.bits < 64) { + return int_info.signedness; + } + }, .sparc64, - .riscv64, .powerpc64, .powerpc64le, => { diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index a6457a1f15..c47f89ebaf 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -16,6 +16,10 @@ static void assert_or_panic(bool ok) { # define ZIG_PPC32 #endif +#if defined __riscv && defined _ILP32 +# define ZIG_RISCV32 +#endif + #ifdef __i386__ # define ZIG_NO_I128 #endif @@ -32,6 +36,10 @@ static void assert_or_panic(bool ok) { # define ZIG_NO_I128 #endif +#ifdef ZIG_RISCV32 +# define ZIG_NO_I128 +#endif + #ifdef __i386__ # define ZIG_NO_COMPLEX #endif @@ -48,6 +56,10 @@ static void assert_or_panic(bool ok) { # define ZIG_NO_COMPLEX #endif +#ifdef __riscv +# define ZIG_NO_COMPLEX +#endif + #ifndef ZIG_NO_I128 struct i128 { __int128 value; @@ -265,7 +277,7 @@ void run_c_tests(void) { } #endif -#if !defined __mips__ && !defined __riscv && !defined ZIG_PPC32 +#if !defined __mips__ && !defined ZIG_PPC32 { struct BigStruct s = {1, 2, 3, 4, 5}; zig_big_struct(s); @@ -273,7 +285,7 @@ void run_c_tests(void) { #endif #if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ - !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 + !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct SmallStructInts s = {1, 2, 3, 4}; zig_small_struct_ints(s); @@ -299,14 +311,14 @@ void run_c_tests(void) { } #if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ - !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 + !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct SplitStructInts s = {1234, 100, 1337}; zig_split_struct_ints(s); } #endif -#if !defined __arm__ && !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 +#if !defined __arm__ && !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct MedStructMixed s = {1234, 100.0f, 1337.0f}; zig_med_struct_mixed(s); @@ -314,14 +326,14 @@ void run_c_tests(void) { #endif #if !defined __i386__ && !defined __arm__ && !defined __mips__ && \ - !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 + !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct SplitStructMixed s = {1234, 100, 1337.0f}; zig_split_struct_mixed(s); } #endif -#if !defined __mips__ && !defined __riscv && !defined ZIG_PPC32 +#if !defined __mips__ && !defined ZIG_PPC32 { struct BigStruct s = {30, 31, 32, 33, 34}; struct BigStruct res = zig_big_struct_both(s); @@ -333,7 +345,7 @@ void run_c_tests(void) { } #endif -#if !defined __riscv && !defined ZIG_PPC32 && !defined _ARCH_PPC64 +#if !defined ZIG_PPC32 && !defined _ARCH_PPC64 { struct Rect r1 = {1, 21, 16, 4}; struct Rect r2 = {178, 189, 21, 15}; @@ -341,7 +353,7 @@ void run_c_tests(void) { } #endif -#if !defined __mips__ && !defined __riscv && !defined ZIG_PPC32 +#if !defined __mips__ && !defined ZIG_PPC32 { struct FloatRect r1 = {1, 21, 16, 4}; struct FloatRect r2 = {178, 189, 21, 15}; @@ -354,9 +366,7 @@ void run_c_tests(void) { assert_or_panic(zig_ret_u8() == 0xff); assert_or_panic(zig_ret_u16() == 0xffff); -#ifndef __riscv assert_or_panic(zig_ret_u32() == 0xffffffff); -#endif assert_or_panic(zig_ret_u64() == 0xffffffffffffffff); assert_or_panic(zig_ret_i8() == -1); diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index 14364d34f8..bd2b057c38 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -171,7 +171,7 @@ extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat; extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble; const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS() and - !builtin.cpu.arch.isARM() and !builtin.cpu.arch.isPPC(); + !builtin.cpu.arch.isARM() and !builtin.cpu.arch.isPPC() and !builtin.cpu.arch.isRISCV(); test "C ABI complex float" { if (!complex_abi_compatible) return error.SkipZigTest; @@ -265,7 +265,6 @@ extern fn c_big_struct(BigStruct) void; test "C ABI big struct" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var s = BigStruct{ @@ -292,7 +291,6 @@ const BigUnion = extern union { extern fn c_big_union(BigUnion) void; test "C ABI big union" { - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var x = BigUnion{ @@ -327,7 +325,6 @@ extern fn c_ret_med_struct_mixed() MedStructMixed; test "C ABI medium struct of ints and floats" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; @@ -361,7 +358,6 @@ extern fn c_ret_small_struct_ints() SmallStructInts; test "C ABI small struct of ints" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; @@ -444,7 +440,6 @@ extern fn c_split_struct_ints(SplitStructInt) void; test "C ABI split struct of ints" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; @@ -473,7 +468,6 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed; test "C ABI split struct of ints and floats" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; @@ -502,7 +496,6 @@ extern fn c_multiple_struct_floats(FloatRect, FloatRect) void; test "C ABI sret and byval together" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var s = BigStruct{ @@ -555,7 +548,6 @@ extern fn c_big_struct_floats(Vector5) void; test "C ABI structs of floats as parameter" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; @@ -596,7 +588,6 @@ export fn zig_multiple_struct_ints(x: Rect, y: Rect) void { } test "C ABI structs of ints as multiple parameters" { - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; @@ -635,7 +626,6 @@ export fn zig_multiple_struct_floats(x: FloatRect, y: FloatRect) void { test "C ABI structs of floats as multiple parameters" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; var r1 = FloatRect{ @@ -741,7 +731,6 @@ extern fn c_ret_struct_with_array() StructWithArray; test "Struct with array as padding." { if (builtin.cpu.arch == .i386) return error.SkipZigTest; if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; @@ -768,7 +757,6 @@ extern fn c_ret_float_array_struct() FloatArrayStruct; test "Float array like struct" { if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; c_float_array_struct(.{ @@ -796,7 +784,6 @@ extern fn c_ret_small_vec() SmallVec; test "small simd vector" { if (builtin.cpu.arch == .i386) return error.SkipZigTest; - if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; if (comptime builtin.cpu.arch.isPPC64()) return error.SkipZigTest; c_small_vec(.{ 1, 2 }); -- cgit v1.2.3 From 3f4197906190fe39d75f5e9bee2d9a7e5813f800 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 22 Oct 2022 14:50:31 +0300 Subject: replace some panics with try in C ABI tests --- test/c_abi/main.zig | 68 ++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index bd2b057c38..c9f697dfb0 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -181,8 +181,8 @@ test "C ABI complex float" { const b = ComplexFloat{ .real = 11.3, .imag = -1.5 }; const z = c_cmultf(a, b); - expect(z.real == 1.5) catch @panic("test failure: zig_complex_float 1"); - expect(z.imag == 13.5) catch @panic("test failure: zig_complex_float 2"); + try expect(z.real == 1.5); + try expect(z.imag == 13.5); } test "C ABI complex float by component" { @@ -192,8 +192,8 @@ test "C ABI complex float by component" { const b = ComplexFloat{ .real = 11.3, .imag = -1.5 }; const z2 = c_cmultf_comp(a.real, a.imag, b.real, b.imag); - expect(z2.real == 1.5) catch @panic("test failure: zig_complex_float 3"); - expect(z2.imag == 13.5) catch @panic("test failure: zig_complex_float 4"); + try expect(z2.real == 1.5); + try expect(z2.imag == 13.5); } test "C ABI complex double" { @@ -203,8 +203,8 @@ test "C ABI complex double" { const b = ComplexDouble{ .real = 11.3, .imag = -1.5 }; const z = c_cmultd(a, b); - expect(z.real == 1.5) catch @panic("test failure: zig_complex_double 1"); - expect(z.imag == 13.5) catch @panic("test failure: zig_complex_double 2"); + try expect(z.real == 1.5); + try expect(z.imag == 13.5); } test "C ABI complex double by component" { @@ -214,8 +214,8 @@ test "C ABI complex double by component" { const b = ComplexDouble{ .real = 11.3, .imag = -1.5 }; const z = c_cmultd_comp(a.real, a.imag, b.real, b.imag); - expect(z.real == 1.5) catch @panic("test failure: zig_complex_double 3"); - expect(z.imag == 13.5) catch @panic("test failure: zig_complex_double 4"); + try expect(z.real == 1.5); + try expect(z.imag == 13.5); } export fn zig_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat { @@ -335,9 +335,9 @@ test "C ABI medium struct of ints and floats" { }; c_med_struct_mixed(s); var s2 = c_ret_med_struct_mixed(); - expect(s2.a == 1234) catch @panic("test failure"); - expect(s2.b == 100.0) catch @panic("test failure"); - expect(s2.c == 1337.0) catch @panic("test failure"); + try expect(s2.a == 1234); + try expect(s2.b == 100.0); + try expect(s2.c == 1337.0); } export fn zig_med_struct_mixed(x: MedStructMixed) void { @@ -369,10 +369,10 @@ test "C ABI small struct of ints" { }; c_small_struct_ints(s); var s2 = c_ret_small_struct_ints(); - expect(s2.a == 1) catch @panic("test failure"); - expect(s2.b == 2) catch @panic("test failure"); - expect(s2.c == 3) catch @panic("test failure"); - expect(s2.d == 4) catch @panic("test failure"); + try expect(s2.a == 1); + try expect(s2.b == 2); + try expect(s2.c == 3); + try expect(s2.d == 4); } export fn zig_small_struct_ints(x: SmallStructInts) void { @@ -478,9 +478,9 @@ test "C ABI split struct of ints and floats" { }; c_split_struct_mixed(s); var s2 = c_ret_split_struct_mixed(); - expect(s2.a == 1234) catch @panic("test failure"); - expect(s2.b == 100) catch @panic("test failure"); - expect(s2.c == 1337.0) catch @panic("test failure"); + try expect(s2.a == 1234); + try expect(s2.b == 100); + try expect(s2.c == 1337.0); } export fn zig_split_struct_mixed(x: SplitStructMixed) void { @@ -737,8 +737,8 @@ test "Struct with array as padding." { c_struct_with_array(.{ .a = 1, .padding = undefined, .b = 2 }); var x = c_ret_struct_with_array(); - try std.testing.expect(x.a == 4); - try std.testing.expect(x.b == 155); + try expect(x.a == 4); + try expect(x.b == 155); } const FloatArrayStruct = extern struct { @@ -771,10 +771,10 @@ test "Float array like struct" { }); var x = c_ret_float_array_struct(); - try std.testing.expect(x.origin.x == 1); - try std.testing.expect(x.origin.y == 2); - try std.testing.expect(x.size.width == 3); - try std.testing.expect(x.size.height == 4); + try expect(x.origin.x == 1); + try expect(x.origin.y == 2); + try expect(x.size.width == 3); + try expect(x.size.height == 4); } const SmallVec = @Vector(2, u32); @@ -789,8 +789,8 @@ test "small simd vector" { c_small_vec(.{ 1, 2 }); var x = c_ret_small_vec(); - try std.testing.expect(x[0] == 3); - try std.testing.expect(x[1] == 4); + try expect(x[0] == 3); + try expect(x[1] == 4); } const BigVec = @Vector(8, usize); @@ -804,12 +804,12 @@ test "big simd vector" { c_big_vec(.{ 1, 2, 3, 4, 5, 6, 7, 8 }); var x = c_ret_big_vec(); - try std.testing.expect(x[0] == 9); - try std.testing.expect(x[1] == 10); - try std.testing.expect(x[2] == 11); - try std.testing.expect(x[3] == 12); - try std.testing.expect(x[4] == 13); - try std.testing.expect(x[5] == 14); - try std.testing.expect(x[6] == 15); - try std.testing.expect(x[7] == 16); + try expect(x[0] == 9); + try expect(x[1] == 10); + try expect(x[2] == 11); + try expect(x[3] == 12); + try expect(x[4] == 13); + try expect(x[5] == 14); + try expect(x[6] == 15); + try expect(x[7] == 16); } -- cgit v1.2.3 From be10f95994d077f9b8137bb58d0349f78f408219 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 22 Oct 2022 19:32:56 +0300 Subject: ARM C ABI: workaround i128 issues --- src/arch/arm/abi.zig | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/arch/arm/abi.zig b/src/arch/arm/abi.zig index d8bbc5d1d1..9659ca13d7 100644 --- a/src/arch/arm/abi.zig +++ b/src/arch/arm/abi.zig @@ -68,7 +68,14 @@ pub fn classifyType(ty: Type, target: std.Target, ctx: Context) Class { return Class.arrSize(bit_size, 32); }, .Bool, .Float => return .byval, - .Int, .Enum, .ErrorSet => { + .Int => { + // TODO this is incorrect for _BitInt(128) but implementing + // this correctly makes implementing compiler-rt impossible. + // const bit_size = ty.bitSize(target); + // if (bit_size > 64) return .memory; + return .byval; + }, + .Enum, .ErrorSet => { const bit_size = ty.bitSize(target); if (bit_size > 64) return .memory; return .byval; -- cgit v1.2.3 From f2a7aba586157482131611c57a283f3b4cfee98d Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 22 Oct 2022 22:00:59 +0300 Subject: x86_64 llvm: correct lowering of ptr sized float struct Closes #13211 --- src/codegen/llvm.zig | 5 ----- test/c_abi/cfuncs.c | 12 ++++++++++++ test/c_abi/main.zig | 18 ++++++++++++++++++ test/tests.zig | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 8b10895068..129d4581b7 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -10432,11 +10432,6 @@ const ParamTypeIterator = struct { it.llvm_index += 1; return .abi_sized_int; } - if (classes[0] == .sse and classes[1] == .none) { - it.zig_index += 1; - it.llvm_index += 1; - return .byval; - } it.llvm_types_buffer = llvm_types_buffer; it.llvm_types_len = llvm_types_index; it.llvm_index += llvm_types_index; diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index c47f89ebaf..5891ee29d7 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -758,3 +758,15 @@ void c_big_vec(BigVec vec) { BigVec c_ret_big_vec(void) { return (BigVec){9, 10, 11, 12, 13, 14, 15, 16}; } + +typedef struct { + float x, y; +} Vector2; + +void c_ptr_size_float_struct(Vector2 vec) { + assert_or_panic(vec.x == 1); + assert_or_panic(vec.y == 2); +} +Vector2 c_ret_ptr_size_float_struct(void) { + return (Vector2){3, 4}; +} diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index c9f697dfb0..d9fb25f4b3 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -813,3 +813,21 @@ test "big simd vector" { try expect(x[6] == 15); try expect(x[7] == 16); } + +const Vector2 = extern struct { x: f32, y: f32 }; + +extern fn c_ptr_size_float_struct(Vector2) void; +extern fn c_ret_ptr_size_float_struct() Vector2; + +test "C ABI pointer sized float struct" { + if (builtin.cpu.arch == .i386) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; + if (comptime builtin.cpu.arch.isPPC()) return error.SkipZigTest; + + c_ptr_size_float_struct(.{ .x = 1, .y = 2 }); + + var x = c_ret_ptr_size_float_struct(); + try expect(x.x == 3); + try expect(x.y == 4); +} diff --git a/test/tests.zig b/test/tests.zig index f204a683fb..266d2a37d0 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -1314,7 +1314,7 @@ const c_abi_targets = [_]CrossTarget{ .{ .cpu_arch = .powerpc64le, .os_tag = .linux, - .abi = .none, + .abi = .musl, }, }; -- cgit v1.2.3