authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-21 21:44:52+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-22 11:31:41+03:00
log031c768cc8399ccdf5440df87d37c03a238315d5
treed9299c0405ae7beb0633b406aa99f473f94bf26d
parent3981250b84b4eb4a34832e3fa5888aa3442e8a74

add C ABI tests for simd vectors


7 files changed, 131 insertions(+), 36 deletions(-)

src/arch/aarch64/abi.zig+19-6
......@@ -5,7 +5,14 @@ const Register = bits.Register;
55const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager;
66const Type = @import("../../type.zig").Type;
77
8pub const Class = union(enum) { memory, integer, double_integer, none, float_array: u8 };
8pub const Class = union(enum) {
9 memory,
10 byval,
11 integer,
12 double_integer,
13 none,
14 float_array: u8,
15};
916
1017/// For `float_array` the second element will be the amount of floats.
1118pub fn classifyType(ty: Type, target: std.Target) Class {
......@@ -13,7 +20,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
1320 var maybe_float_bits: ?u16 = null;
1421 switch (ty.zigTypeTag()) {
1522 .Struct => {
16 if (ty.containerLayout() == .Packed) return .integer;
23 if (ty.containerLayout() == .Packed) return .byval;
1724 const float_count = countFloats(ty, target, &maybe_float_bits);
1825 if (float_count <= sret_float_count) return .{ .float_array = float_count };
1926
......@@ -23,7 +30,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
2330 return .integer;
2431 },
2532 .Union => {
26 if (ty.containerLayout() == .Packed) return .integer;
33 if (ty.containerLayout() == .Packed) return .byval;
2734 const float_count = countFloats(ty, target, &maybe_float_bits);
2835 if (float_count <= sret_float_count) return .{ .float_array = float_count };
2936
......@@ -32,14 +39,20 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
3239 if (bit_size > 64) return .double_integer;
3340 return .integer;
3441 },
35 .Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .integer,
42 .Int, .Enum, .ErrorSet, .Float, .Bool => return .byval,
43 .Vector => {
44 const bit_size = ty.bitSize(target);
45 // TODO is this controlled by a cpu feature?
46 if (bit_size > 128) return .memory;
47 return .byval;
48 },
3649 .Optional => {
3750 std.debug.assert(ty.isPtrLikeOptional());
38 return .integer;
51 return .byval;
3952 },
4053 .Pointer => {
4154 std.debug.assert(!ty.isSlice());
42 return .integer;
55 return .byval;
4356 },
4457 .ErrorUnion,
4558 .Frame,
src/arch/arm/abi.zig+9-4
......@@ -21,7 +21,9 @@ pub const Class = union(enum) {
2121 }
2222};
2323
24pub fn classifyType(ty: Type, target: std.Target) Class {
24pub const Context = enum { ret, arg };
25
26pub fn classifyType(ty: Type, target: std.Target, ctx: Context) Class {
2527 if (!ty.hasRuntimeBitsIgnoreComptime()) return .none;
2628
2729 var maybe_float_bits: ?u16 = null;
......@@ -66,14 +68,17 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
6668 }
6769 return Class.arrSize(bit_size, 32);
6870 },
69 .Int, .Enum => {
71 .Bool, .Float => return .byval,
72 .Int, .Enum, .ErrorSet => {
7073 const bit_size = ty.bitSize(target);
7174 if (bit_size > 64) return .memory;
7275 return .byval;
7376 },
74 .ErrorSet, .Vector, .Float, .Bool => {
77 .Vector => {
7578 const bit_size = ty.bitSize(target);
76 if (bit_size > 128) return .memory;
79 // TODO is this controlled by a cpu feature?
80 if (ctx == .ret and bit_size > 128) return .memory;
81 if (bit_size > 512) return .memory;
7782 return .byval;
7883 },
7984 .Optional => {
src/arch/x86_64/CodeGen.zig+1-1
......@@ -7143,7 +7143,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues {
71437143
71447144 const classes: []const abi.Class = switch (self.target.os.tag) {
71457145 .windows => &[1]abi.Class{abi.classifyWindows(ty, self.target.*)},
7146 else => mem.sliceTo(&abi.classifySystemV(ty, self.target.*), .none),
7146 else => mem.sliceTo(&abi.classifySystemV(ty, self.target.*, .arg), .none),
71477147 };
71487148 if (classes.len > 1) {
71497149 return self.fail("TODO handle multiple classes per type", .{});
src/arch/x86_64/abi.zig+21-3
......@@ -60,9 +60,11 @@ pub fn classifyWindows(ty: Type, target: Target) Class {
6060 }
6161}
6262
63pub const Context = enum { ret, arg };
64
6365/// There are a maximum of 8 possible return slots. Returned values are in
6466/// the beginning of the array; unused slots are filled with .none.
65pub fn classifySystemV(ty: Type, target: Target) [8]Class {
67pub fn classifySystemV(ty: Type, target: Target, ctx: Context) [8]Class {
6668 const memory_class = [_]Class{
6769 .memory, .none, .none, .none,
6870 .none, .none, .none, .none,
......@@ -134,6 +136,22 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
134136 },
135137 .Vector => {
136138 const elem_ty = ty.childType();
139 if (ctx == .arg) {
140 const bit_size = ty.bitSize(target);
141 if (bit_size > 128) return memory_class;
142 if (bit_size > 80) return .{
143 .integer, .integer, .none, .none,
144 .none, .none, .none, .none,
145 };
146 if (bit_size > 64) return .{
147 .x87, .none, .none, .none,
148 .none, .none, .none, .none,
149 };
150 return .{
151 .integer, .none, .none, .none,
152 .none, .none, .none, .none,
153 };
154 }
137155 const bits = elem_ty.bitSize(target) * ty.arrayLen();
138156 if (bits <= 64) return .{
139157 .sse, .none, .none, .none,
......@@ -201,7 +219,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
201219 }
202220 }
203221 const field_size = field.ty.abiSize(target);
204 const field_class_array = classifySystemV(field.ty, target);
222 const field_class_array = classifySystemV(field.ty, target, .arg);
205223 const field_class = std.mem.sliceTo(&field_class_array, .none);
206224 if (byte_i + field_size <= 8) {
207225 // Combine this field with the previous one.
......@@ -315,7 +333,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
315333 }
316334 }
317335 // Combine this field with the previous one.
318 const field_class = classifySystemV(field.ty, target);
336 const field_class = classifySystemV(field.ty, target, .arg);
319337 for (result) |*result_item, i| {
320338 const field_item = field_class[i];
321339 // "If both classes are equal, this is the resulting class."
src/codegen/llvm.zig+13-17
......@@ -10110,11 +10110,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool
1011010110 .mips, .mipsel => return false,
1011110111 .x86_64 => switch (target.os.tag) {
1011210112 .windows => return x86_64_abi.classifyWindows(fn_info.return_type, target) == .memory,
10113 else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory,
10113 else => return x86_64_abi.classifySystemV(fn_info.return_type, target, .ret)[0] == .memory,
1011410114 },
1011510115 .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect,
1011610116 .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target) == .memory,
10117 .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target)) {
10117 .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) {
1011810118 .memory, .i64_array => return true,
1011910119 .i32_array => |size| return size != 1,
1012010120 .none, .byval => return false,
......@@ -10171,7 +10171,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
1017110171 if (is_scalar) {
1017210172 return dg.lowerType(fn_info.return_type);
1017310173 }
10174 const classes = x86_64_abi.classifySystemV(fn_info.return_type, target);
10174 const classes = x86_64_abi.classifySystemV(fn_info.return_type, target, .ret);
1017510175 if (classes[0] == .memory) {
1017610176 return dg.context.voidType();
1017710177 }
......@@ -10229,12 +10229,10 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
1022910229 return dg.context.intType(@intCast(c_uint, abi_size * 8));
1023010230 },
1023110231 .aarch64, .aarch64_be => {
10232 if (is_scalar) {
10233 return dg.lowerType(fn_info.return_type);
10234 }
1023510232 switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) {
1023610233 .memory, .none => return dg.context.voidType(),
1023710234 .float_array => return dg.lowerType(fn_info.return_type),
10235 .byval => return dg.lowerType(fn_info.return_type),
1023810236 .integer => {
1023910237 const bit_size = fn_info.return_type.bitSize(target);
1024010238 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 {
1024310241 }
1024410242 },
1024510243 .arm, .armeb => {
10246 switch (arm_c_abi.classifyType(fn_info.return_type, target)) {
10244 switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) {
1024710245 .memory, .i64_array => return dg.context.voidType(),
1024810246 .i32_array => |len| if (len == 1) {
1024910247 return dg.context.intType(32);
......@@ -10376,18 +10374,18 @@ const ParamTypeIterator = struct {
1037610374 else => unreachable,
1037710375 },
1037810376 else => {
10379 if (is_scalar) {
10380 it.zig_index += 1;
10381 it.llvm_index += 1;
10382 return .byval;
10383 }
10384 const classes = x86_64_abi.classifySystemV(ty, it.target);
10377 const classes = x86_64_abi.classifySystemV(ty, it.target, .arg);
1038510378 if (classes[0] == .memory) {
1038610379 it.zig_index += 1;
1038710380 it.llvm_index += 1;
1038810381 it.byval_attr = true;
1038910382 return .byref;
1039010383 }
10384 if (is_scalar) {
10385 it.zig_index += 1;
10386 it.llvm_index += 1;
10387 return .byval;
10388 }
1039110389 var llvm_types_buffer: [8]u16 = undefined;
1039210390 var llvm_types_index: u32 = 0;
1039310391 for (classes) |class| {
......@@ -10452,13 +10450,11 @@ const ParamTypeIterator = struct {
1045210450 .aarch64, .aarch64_be => {
1045310451 it.zig_index += 1;
1045410452 it.llvm_index += 1;
10455 if (is_scalar) {
10456 return .byval;
10457 }
1045810453 switch (aarch64_c_abi.classifyType(ty, it.target)) {
1045910454 .none => unreachable,
1046010455 .memory => return .byref,
1046110456 .float_array => |len| return Lowering{ .float_array = len },
10457 .byval => return .byval,
1046210458 .integer => {
1046310459 it.llvm_types_len = 1;
1046410460 it.llvm_types_buffer[0] = 64;
......@@ -10470,7 +10466,7 @@ const ParamTypeIterator = struct {
1047010466 .arm, .armeb => {
1047110467 it.zig_index += 1;
1047210468 it.llvm_index += 1;
10473 switch (arm_c_abi.classifyType(ty, it.target)) {
10469 switch (arm_c_abi.classifyType(ty, it.target, .arg)) {
1047410470 .none => unreachable,
1047510471 .memory => {
1047610472 it.byval_attr = true;
test/c_abi/cfuncs.c+33-5
......@@ -1,8 +1,8 @@
1#include <complex.h>
12#include <inttypes.h>
2#include <stdlib.h>
33#include <stdbool.h>
4#include <stdlib.h>
45#include <string.h>
5#include <complex.h>
66
77void zig_panic();
88
......@@ -210,7 +210,7 @@ void run_c_tests(void) {
210210 zig_longdouble(12.34l);
211211 zig_five_floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f);
212212
213 zig_ptr((void*)0xdeadbeefL);
213 zig_ptr((void *)0xdeadbeefL);
214214
215215 zig_bool(true);
216216
......@@ -408,7 +408,7 @@ void c_long_double(long double x) {
408408}
409409
410410void c_ptr(void *x) {
411 assert_or_panic(x == (void*)0xdeadbeefL);
411 assert_or_panic(x == (void *)0xdeadbeefL);
412412}
413413
414414void c_bool(bool x) {
......@@ -676,7 +676,7 @@ void c_struct_with_array(StructWithArray x) {
676676}
677677
678678StructWithArray c_ret_struct_with_array() {
679 return (StructWithArray) { 4, {}, 155 };
679 return (StructWithArray){4, {}, 155};
680680}
681681
682682typedef struct {
......@@ -705,3 +705,31 @@ FloatArrayStruct c_ret_float_array_struct() {
705705 x.size.height = 4;
706706 return x;
707707}
708
709typedef uint32_t SmallVec __attribute__((vector_size(2 * sizeof(uint32_t))));
710
711void c_small_vec(SmallVec vec) {
712 assert_or_panic(vec[0] == 1);
713 assert_or_panic(vec[1] == 2);
714}
715
716SmallVec c_ret_small_vec(void) {
717 return (SmallVec){3, 4};
718}
719
720typedef size_t BigVec __attribute__((vector_size(8 * sizeof(size_t))));
721
722void c_big_vec(BigVec vec) {
723 assert_or_panic(vec[0] == 1);
724 assert_or_panic(vec[1] == 2);
725 assert_or_panic(vec[2] == 3);
726 assert_or_panic(vec[3] == 4);
727 assert_or_panic(vec[4] == 5);
728 assert_or_panic(vec[5] == 6);
729 assert_or_panic(vec[6] == 7);
730 assert_or_panic(vec[7] == 8);
731}
732
733BigVec c_ret_big_vec(void) {
734 return (BigVec){9, 10, 11, 12, 13, 14, 15, 16};
735}
test/c_abi/main.zig+35
......@@ -766,3 +766,38 @@ test "Float array like struct" {
766766 try std.testing.expect(x.size.width == 3);
767767 try std.testing.expect(x.size.height == 4);
768768}
769
770const SmallVec = @Vector(2, u32);
771
772extern fn c_small_vec(SmallVec) void;
773extern fn c_ret_small_vec() SmallVec;
774
775test "small simd vector" {
776 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
777 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
778
779 c_small_vec(.{ 1, 2 });
780
781 var x = c_ret_small_vec();
782 try std.testing.expect(x[0] == 3);
783 try std.testing.expect(x[1] == 4);
784}
785
786const BigVec = @Vector(8, usize);
787
788extern fn c_big_vec(BigVec) void;
789extern fn c_ret_big_vec() BigVec;
790
791test "big simd vector" {
792 c_big_vec(.{ 1, 2, 3, 4, 5, 6, 7, 8 });
793
794 var x = c_ret_big_vec();
795 try std.testing.expect(x[0] == 9);
796 try std.testing.expect(x[1] == 10);
797 try std.testing.expect(x[2] == 11);
798 try std.testing.expect(x[3] == 12);
799 try std.testing.expect(x[4] == 13);
800 try std.testing.expect(x[5] == 14);
801 try std.testing.expect(x[6] == 15);
802 try std.testing.expect(x[7] == 16);
803}