add C ABI tests for simd vectors

This commit is contained in:
Veikka Tuominen 2022-10-21 21:44:52 +03:00
parent 3981250b84
commit 031c768cc8
7 changed files with 133 additions and 38 deletions

View File

@ -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,

View File

@ -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 => {

View File

@ -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", .{});

View File

@ -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."

View File

@ -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;

View File

@ -1,8 +1,8 @@
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <complex.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
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};
}

View File

@ -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);
}