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 +++++++++++++----------------- 5 files changed, 63 insertions(+), 31 deletions(-) (limited to 'src') 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; -- cgit v1.2.3