-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
translate-c: Add support for vector expressions #8389
Conversation
Open questions:
|
Includes vector types, __builtin_shufflevector, and __builtin_convertvector
7cc1157
to
5937a33
Compare
There is not. However, all Zig vector types have alignment 16, so I think we can safely ignore that value if it is <= 16. And I suspect that we will not encounter a higher alignment in practice (please correct me if I'm wrong).
I think we should be able to translate these function calls into zig SIMD expressions on vectors, right? I'm not sure exactly what that function does but if it was for example vector addition then we could translate to e.g. I see that you are already doing that in this PR with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty slick. Nice work!
I found an issue talking about intrinsics: #7702
There are a lot of them - just for x86 we have: https://gcc.gnu.org/onlinedocs/gcc-10.2.0/gcc/x86-Built-in-Functions.html#x86-Built-in-Functions In theory all of them could be emulated in pure Zig but that would be a lot of work and pretty error prone and possibly miss out on the ability to compile them down to a single instruction depending on how smart the backend is. The proposal I linked talked about possibly having a way to forward them to the backend but I have no idea how difficult that would be to implement. I think in general user code would not directly use these builtins; using our example they would call |
The plan for these things is to have builtin functions for all of them. We have an entire |
Cool, so if I'm understanding correctly, for the purposes of pub fn __builtin_ia32_packsswb(a: Vector(4, i16), b: Vector(4, i16)) callconv(.Inline) Vector(8, i8) {
return @"intrinsic.mmx.packsswb"(a, b);
} and then translate-c would not have to do any special work, it would just notice that the function is defined and emit a call to it. Alternatively On the topic of alignment - some AVX stuff has higher than 16 byte alignment. |
@ehaas I think I'd rather see it somewhere in the zig standard library as conditional comptime assembly, e.g. fn saturatingTruncate(comptime destType: T, src: anytype) T {
if (!@isComptime() // see https://github.com/ziglang/zig/issues/868
and destType == Vector(i8,8)
and @TypeOf(src) == Vector(i16,8)
and std.builtin.cpu.arch == .x86
and std.builtin.cpu.features.has(.mmx)
) {
return asm("packsswb ...." : src); // or alternatively, this could be the LLVM intrinsic? see https://github.com/ziglang/zig/issues/4466 or https://github.com/ziglang/zig/issues/2291
}
// implementation using primitive addition/etc.
} |
Yep, looks like we're on the same page. Although I'd probably want to name it something less fancy, such as
Interesting- I'm glad you brought this up. I think we might need to consider adjusting zig's type system to take into account vector alignment. Would you mind opening a new issue for this topic? |
Includes vector types, __builtin_shufflevector, and __builtin_convertvector