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;...@@ -5,7 +5,14 @@ const Register = bits.Register;
5const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager;5const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager;
6const Type = @import("../../type.zig").Type;6const 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
10/// For `float_array` the second element will be the amount of floats.17/// For `float_array` the second element will be the amount of floats.
11pub fn classifyType(ty: Type, target: std.Target) Class {18pub fn classifyType(ty: Type, target: std.Target) Class {
...@@ -13,7 +20,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class {...@@ -13,7 +20,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
13 var maybe_float_bits: ?u16 = null;20 var maybe_float_bits: ?u16 = null;
14 switch (ty.zigTypeTag()) {21 switch (ty.zigTypeTag()) {
15 .Struct => {22 .Struct => {
16 if (ty.containerLayout() == .Packed) return .integer;23 if (ty.containerLayout() == .Packed) return .byval;
17 const float_count = countFloats(ty, target, &maybe_float_bits);24 const float_count = countFloats(ty, target, &maybe_float_bits);
18 if (float_count <= sret_float_count) return .{ .float_array = float_count };25 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 {...@@ -23,7 +30,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
23 return .integer;30 return .integer;
24 },31 },
25 .Union => {32 .Union => {
26 if (ty.containerLayout() == .Packed) return .integer;33 if (ty.containerLayout() == .Packed) return .byval;
27 const float_count = countFloats(ty, target, &maybe_float_bits);34 const float_count = countFloats(ty, target, &maybe_float_bits);
28 if (float_count <= sret_float_count) return .{ .float_array = float_count };35 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 {...@@ -32,14 +39,20 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
32 if (bit_size > 64) return .double_integer;39 if (bit_size > 64) return .double_integer;
33 return .integer;40 return .integer;
34 },41 },
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 },
36 .Optional => {49 .Optional => {
37 std.debug.assert(ty.isPtrLikeOptional());50 std.debug.assert(ty.isPtrLikeOptional());
38 return .integer;51 return .byval;
39 },52 },
40 .Pointer => {53 .Pointer => {
41 std.debug.assert(!ty.isSlice());54 std.debug.assert(!ty.isSlice());
42 return .integer;55 return .byval;
43 },56 },
44 .ErrorUnion,57 .ErrorUnion,
45 .Frame,58 .Frame,
src/arch/arm/abi.zig+9-4
...@@ -21,7 +21,9 @@ pub const Class = union(enum) {...@@ -21,7 +21,9 @@ pub const Class = union(enum) {
21 }21 }
22};22};
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 {
25 if (!ty.hasRuntimeBitsIgnoreComptime()) return .none;27 if (!ty.hasRuntimeBitsIgnoreComptime()) return .none;
2628
27 var maybe_float_bits: ?u16 = null;29 var maybe_float_bits: ?u16 = null;
...@@ -66,14 +68,17 @@ pub fn classifyType(ty: Type, target: std.Target) Class {...@@ -66,14 +68,17 @@ pub fn classifyType(ty: Type, target: std.Target) Class {
66 }68 }
67 return Class.arrSize(bit_size, 32);69 return Class.arrSize(bit_size, 32);
68 },70 },
69 .Int, .Enum => {71 .Bool, .Float => return .byval,
72 .Int, .Enum, .ErrorSet => {
70 const bit_size = ty.bitSize(target);73 const bit_size = ty.bitSize(target);
71 if (bit_size > 64) return .memory;74 if (bit_size > 64) return .memory;
72 return .byval;75 return .byval;
73 },76 },
74 .ErrorSet, .Vector, .Float, .Bool => {77 .Vector => {
75 const bit_size = ty.bitSize(target);78 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;
77 return .byval;82 return .byval;
78 },83 },
79 .Optional => {84 .Optional => {
src/arch/x86_64/CodeGen.zig+1-1
...@@ -7143,7 +7143,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues {...@@ -7143,7 +7143,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues {
71437143
7144 const classes: []const abi.Class = switch (self.target.os.tag) {7144 const classes: []const abi.Class = switch (self.target.os.tag) {
7145 .windows => &[1]abi.Class{abi.classifyWindows(ty, self.target.*)},7145 .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),
7147 };7147 };
7148 if (classes.len > 1) {7148 if (classes.len > 1) {
7149 return self.fail("TODO handle multiple classes per type", .{});7149 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 {...@@ -60,9 +60,11 @@ pub fn classifyWindows(ty: Type, target: Target) Class {
60 }60 }
61}61}
6262
63pub const Context = enum { ret, arg };
64
63/// There are a maximum of 8 possible return slots. Returned values are in65/// There are a maximum of 8 possible return slots. Returned values are in
64/// the beginning of the array; unused slots are filled with .none.66/// 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 {
66 const memory_class = [_]Class{68 const memory_class = [_]Class{
67 .memory, .none, .none, .none,69 .memory, .none, .none, .none,
68 .none, .none, .none, .none,70 .none, .none, .none, .none,
...@@ -134,6 +136,22 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {...@@ -134,6 +136,22 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
134 },136 },
135 .Vector => {137 .Vector => {
136 const elem_ty = ty.childType();138 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 }
137 const bits = elem_ty.bitSize(target) * ty.arrayLen();155 const bits = elem_ty.bitSize(target) * ty.arrayLen();
138 if (bits <= 64) return .{156 if (bits <= 64) return .{
139 .sse, .none, .none, .none,157 .sse, .none, .none, .none,
...@@ -201,7 +219,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {...@@ -201,7 +219,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
201 }219 }
202 }220 }
203 const field_size = field.ty.abiSize(target);221 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);
205 const field_class = std.mem.sliceTo(&field_class_array, .none);223 const field_class = std.mem.sliceTo(&field_class_array, .none);
206 if (byte_i + field_size <= 8) {224 if (byte_i + field_size <= 8) {
207 // Combine this field with the previous one.225 // Combine this field with the previous one.
...@@ -315,7 +333,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {...@@ -315,7 +333,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
315 }333 }
316 }334 }
317 // Combine this field with the previous one.335 // Combine this field with the previous one.
318 const field_class = classifySystemV(field.ty, target);336 const field_class = classifySystemV(field.ty, target, .arg);
319 for (result) |*result_item, i| {337 for (result) |*result_item, i| {
320 const field_item = field_class[i];338 const field_item = field_class[i];
321 // "If both classes are equal, this is the resulting class."339 // "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...@@ -10110,11 +10110,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool
10110 .mips, .mipsel => return false,10110 .mips, .mipsel => return false,
10111 .x86_64 => switch (target.os.tag) {10111 .x86_64 => switch (target.os.tag) {
10112 .windows => return x86_64_abi.classifyWindows(fn_info.return_type, target) == .memory,10112 .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,
10114 },10114 },
10115 .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect,10115 .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect,
10116 .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target) == .memory,10116 .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)) {
10118 .memory, .i64_array => return true,10118 .memory, .i64_array => return true,
10119 .i32_array => |size| return size != 1,10119 .i32_array => |size| return size != 1,
10120 .none, .byval => return false,10120 .none, .byval => return false,
...@@ -10171,7 +10171,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {...@@ -10171,7 +10171,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
10171 if (is_scalar) {10171 if (is_scalar) {
10172 return dg.lowerType(fn_info.return_type);10172 return dg.lowerType(fn_info.return_type);
10173 }10173 }
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);
10175 if (classes[0] == .memory) {10175 if (classes[0] == .memory) {
10176 return dg.context.voidType();10176 return dg.context.voidType();
10177 }10177 }
...@@ -10229,12 +10229,10 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {...@@ -10229,12 +10229,10 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
10229 return dg.context.intType(@intCast(c_uint, abi_size * 8));10229 return dg.context.intType(@intCast(c_uint, abi_size * 8));
10230 },10230 },
10231 .aarch64, .aarch64_be => {10231 .aarch64, .aarch64_be => {
10232 if (is_scalar) {
10233 return dg.lowerType(fn_info.return_type);
10234 }
10235 switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) {10232 switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) {
10236 .memory, .none => return dg.context.voidType(),10233 .memory, .none => return dg.context.voidType(),
10237 .float_array => return dg.lowerType(fn_info.return_type),10234 .float_array => return dg.lowerType(fn_info.return_type),
10235 .byval => return dg.lowerType(fn_info.return_type),
10238 .integer => {10236 .integer => {
10239 const bit_size = fn_info.return_type.bitSize(target);10237 const bit_size = fn_info.return_type.bitSize(target);
10240 return dg.context.intType(@intCast(c_uint, bit_size));10238 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 {...@@ -10243,7 +10241,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
10243 }10241 }
10244 },10242 },
10245 .arm, .armeb => {10243 .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)) {
10247 .memory, .i64_array => return dg.context.voidType(),10245 .memory, .i64_array => return dg.context.voidType(),
10248 .i32_array => |len| if (len == 1) {10246 .i32_array => |len| if (len == 1) {
10249 return dg.context.intType(32);10247 return dg.context.intType(32);
...@@ -10376,18 +10374,18 @@ const ParamTypeIterator = struct {...@@ -10376,18 +10374,18 @@ const ParamTypeIterator = struct {
10376 else => unreachable,10374 else => unreachable,
10377 },10375 },
10378 else => {10376 else => {
10379 if (is_scalar) {10377 const classes = x86_64_abi.classifySystemV(ty, it.target, .arg);
10380 it.zig_index += 1;
10381 it.llvm_index += 1;
10382 return .byval;
10383 }
10384 const classes = x86_64_abi.classifySystemV(ty, it.target);
10385 if (classes[0] == .memory) {10378 if (classes[0] == .memory) {
10386 it.zig_index += 1;10379 it.zig_index += 1;
10387 it.llvm_index += 1;10380 it.llvm_index += 1;
10388 it.byval_attr = true;10381 it.byval_attr = true;
10389 return .byref;10382 return .byref;
10390 }10383 }
10384 if (is_scalar) {
10385 it.zig_index += 1;
10386 it.llvm_index += 1;
10387 return .byval;
10388 }
10391 var llvm_types_buffer: [8]u16 = undefined;10389 var llvm_types_buffer: [8]u16 = undefined;
10392 var llvm_types_index: u32 = 0;10390 var llvm_types_index: u32 = 0;
10393 for (classes) |class| {10391 for (classes) |class| {
...@@ -10452,13 +10450,11 @@ const ParamTypeIterator = struct {...@@ -10452,13 +10450,11 @@ const ParamTypeIterator = struct {
10452 .aarch64, .aarch64_be => {10450 .aarch64, .aarch64_be => {
10453 it.zig_index += 1;10451 it.zig_index += 1;
10454 it.llvm_index += 1;10452 it.llvm_index += 1;
10455 if (is_scalar) {
10456 return .byval;
10457 }
10458 switch (aarch64_c_abi.classifyType(ty, it.target)) {10453 switch (aarch64_c_abi.classifyType(ty, it.target)) {
10459 .none => unreachable,10454 .none => unreachable,
10460 .memory => return .byref,10455 .memory => return .byref,
10461 .float_array => |len| return Lowering{ .float_array = len },10456 .float_array => |len| return Lowering{ .float_array = len },
10457 .byval => return .byval,
10462 .integer => {10458 .integer => {
10463 it.llvm_types_len = 1;10459 it.llvm_types_len = 1;
10464 it.llvm_types_buffer[0] = 64;10460 it.llvm_types_buffer[0] = 64;
...@@ -10470,7 +10466,7 @@ const ParamTypeIterator = struct {...@@ -10470,7 +10466,7 @@ const ParamTypeIterator = struct {
10470 .arm, .armeb => {10466 .arm, .armeb => {
10471 it.zig_index += 1;10467 it.zig_index += 1;
10472 it.llvm_index += 1;10468 it.llvm_index += 1;
10473 switch (arm_c_abi.classifyType(ty, it.target)) {10469 switch (arm_c_abi.classifyType(ty, it.target, .arg)) {
10474 .none => unreachable,10470 .none => unreachable,
10475 .memory => {10471 .memory => {
10476 it.byval_attr = true;10472 it.byval_attr = true;
test/c_abi/cfuncs.c+33-5
...@@ -1,8 +1,8 @@...@@ -1,8 +1,8 @@
1#include <complex.h>
1#include <inttypes.h>2#include <inttypes.h>
2#include <stdlib.h>
3#include <stdbool.h>3#include <stdbool.h>
4#include <stdlib.h>
4#include <string.h>5#include <string.h>
5#include <complex.h>
66
7void zig_panic();7void zig_panic();
88
...@@ -210,7 +210,7 @@ void run_c_tests(void) {...@@ -210,7 +210,7 @@ void run_c_tests(void) {
210 zig_longdouble(12.34l);210 zig_longdouble(12.34l);
211 zig_five_floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f);211 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
215 zig_bool(true);215 zig_bool(true);
216216
...@@ -408,7 +408,7 @@ void c_long_double(long double x) {...@@ -408,7 +408,7 @@ void c_long_double(long double x) {
408}408}
409409
410void c_ptr(void *x) {410void c_ptr(void *x) {
411 assert_or_panic(x == (void*)0xdeadbeefL);411 assert_or_panic(x == (void *)0xdeadbeefL);
412}412}
413413
414void c_bool(bool x) {414void c_bool(bool x) {
...@@ -676,7 +676,7 @@ void c_struct_with_array(StructWithArray x) {...@@ -676,7 +676,7 @@ void c_struct_with_array(StructWithArray x) {
676}676}
677677
678StructWithArray c_ret_struct_with_array() {678StructWithArray c_ret_struct_with_array() {
679 return (StructWithArray) { 4, {}, 155 };679 return (StructWithArray){4, {}, 155};
680}680}
681681
682typedef struct {682typedef struct {
...@@ -705,3 +705,31 @@ FloatArrayStruct c_ret_float_array_struct() {...@@ -705,3 +705,31 @@ FloatArrayStruct c_ret_float_array_struct() {
705 x.size.height = 4;705 x.size.height = 4;
706 return x;706 return x;
707}707}
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" {...@@ -766,3 +766,38 @@ test "Float array like struct" {
766 try std.testing.expect(x.size.width == 3);766 try std.testing.expect(x.size.width == 3);
767 try std.testing.expect(x.size.height == 4);767 try std.testing.expect(x.size.height == 4);
768}768}
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}