authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2024-02-27 18:42:12+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-27 18:42:12+01:00
log27f589dea1dae6ec0033e1ad2902fb5dadfa562b
tree1869f9c6fb16320d7558bcae43463c621067a694
parent085bde6889925b486291ddf1450b6bb6c8562a8f
parent885f40520e64b8a433ff437ba48ef7e87ad78e1b
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #18538 from Pangoraw/wasm_vector_abi

wasm: allow non-int vectors

3 files changed, 70 insertions(+), 6 deletions(-)

src/arch/wasm/abi.zig+2-1
......@@ -45,7 +45,7 @@ pub fn classifyType(ty: Type, mod: *Module) [2]Class {
4545 }
4646 return classifyType(field_ty, mod);
4747 },
48 .Int, .Enum, .ErrorSet, .Vector => {
48 .Int, .Enum, .ErrorSet => {
4949 const int_bits = ty.intInfo(mod).bits;
5050 if (int_bits <= 64) return direct;
5151 if (int_bits <= 128) return .{ .direct, .direct };
......@@ -58,6 +58,7 @@ pub fn classifyType(ty: Type, mod: *Module) [2]Class {
5858 return memory;
5959 },
6060 .Bool => return direct,
61 .Vector => return direct,
6162 .Array => return memory,
6263 .Optional => {
6364 assert(ty.isPtrLikeOptional(mod));
test/c_abi/cfuncs.c+36-1
......@@ -294,7 +294,37 @@ struct SplitStructMixed zig_ret_split_struct_mixed();
294294
295295struct BigStruct zig_big_struct_both(struct BigStruct);
296296
297#if defined(ZIG_BACKEND_STAGE2_X86_64) || defined(ZIG_PPC32)
297typedef float Vector2Float __attribute__((ext_vector_type(2)));
298typedef float Vector4Float __attribute__((ext_vector_type(4)));
299
300void c_vector_2_float(Vector2Float vec) {
301 assert_or_panic(vec[0] == 1.0);
302 assert_or_panic(vec[1] == 2.0);
303}
304
305void c_vector_4_float(Vector4Float vec) {
306 assert_or_panic(vec[0] == 1.0);
307 assert_or_panic(vec[1] == 2.0);
308 assert_or_panic(vec[2] == 3.0);
309 assert_or_panic(vec[3] == 4.0);
310}
311
312Vector2Float c_ret_vector_2_float(void) {
313 return (Vector2Float){
314 1.0,
315 2.0,
316 };
317}
318Vector4Float c_ret_vector_4_float(void) {
319 return (Vector4Float){
320 1.0,
321 2.0,
322 3.0,
323 4.0,
324 };
325}
326
327#if defined(ZIG_BACKEND_STAGE2_X86_64) || defined(ZIG_PPC32) || defined(__wasm__)
298328
299329typedef bool Vector2Bool __attribute__((ext_vector_type(2)));
300330typedef bool Vector4Bool __attribute__((ext_vector_type(4)));
......@@ -581,6 +611,9 @@ void c_vector_128_bool(Vector128Bool vec) {
581611 assert_or_panic(vec[127] == true);
582612}
583613
614// WASM: The following vector functions define too many Wasm locals for wasmtime in debug mode and are therefore disabled for the wasm target.
615#if !defined(__wasm__)
616
584617void c_vector_256_bool(Vector256Bool vec) {
585618 assert_or_panic(vec[0] == false);
586619 assert_or_panic(vec[1] == true);
......@@ -1355,6 +1388,8 @@ void c_vector_512_bool(Vector512Bool vec) {
13551388 assert_or_panic(vec[511] == true);
13561389}
13571390
1391#endif
1392
13581393Vector2Bool c_ret_vector_2_bool(void) {
13591394 return (Vector2Bool){
13601395 true,
test/c_abi/main.zig+32-4
......@@ -967,6 +967,34 @@ test "big simd vector" {
967967 try expect(x[7] == 16);
968968}
969969
970const Vector2Float = @Vector(2, f32);
971const Vector4Float = @Vector(4, f32);
972
973extern fn c_vector_2_float(Vector2Float) void;
974extern fn c_vector_4_float(Vector4Float) void;
975
976extern fn c_ret_vector_2_float() Vector2Float;
977extern fn c_ret_vector_4_float() Vector4Float;
978
979test "float simd vectors" {
980 if (builtin.cpu.arch == .powerpc or builtin.cpu.arch == .powerpc64le) return error.SkipZigTest;
981
982 {
983 c_vector_2_float(.{ 1.0, 2.0 });
984 const vec = c_ret_vector_2_float();
985 try expect(vec[0] == 1.0);
986 try expect(vec[1] == 2.0);
987 }
988 {
989 c_vector_4_float(.{ 1.0, 2.0, 3.0, 4.0 });
990 const vec = c_ret_vector_4_float();
991 try expect(vec[0] == 1.0);
992 try expect(vec[1] == 2.0);
993 try expect(vec[2] == 3.0);
994 try expect(vec[3] == 4.0);
995 }
996}
997
970998const Vector2Bool = @Vector(2, bool);
971999const Vector4Bool = @Vector(4, bool);
9721000const Vector8Bool = @Vector(8, bool);
......@@ -998,7 +1026,7 @@ extern fn c_ret_vector_256_bool() Vector256Bool;
9981026extern fn c_ret_vector_512_bool() Vector512Bool;
9991027
10001028test "bool simd vector" {
1001 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch != .powerpc) return error.SkipZigTest;
1029 if (builtin.zig_backend == .stage2_llvm and (builtin.cpu.arch != .powerpc and builtin.cpu.arch != .wasm32)) return error.SkipZigTest;
10021030
10031031 {
10041032 c_vector_2_bool(.{
......@@ -1550,8 +1578,9 @@ test "bool simd vector" {
15501578 try expect(vec[126] == false);
15511579 try expect(vec[127] == true);
15521580 }
1581
15531582 {
1554 c_vector_256_bool(.{
1583 if (builtin.target.cpu.arch != .wasm32) c_vector_256_bool(.{
15551584 false,
15561585 true,
15571586 true,
......@@ -2069,7 +2098,7 @@ test "bool simd vector" {
20692098 try expect(vec[255] == false);
20702099 }
20712100 {
2072 c_vector_512_bool(.{
2101 if (builtin.target.cpu.arch != .wasm32) c_vector_512_bool(.{
20732102 true,
20742103 true,
20752104 true,
......@@ -3102,7 +3131,6 @@ test "bool simd vector" {
31023131
31033132comptime {
31043133 skip: {
3105 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .wasm32) break :skip;
31063134 if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .x86_64) break :skip;
31073135
31083136 _ = struct {