-
-
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
Proposal add @compilerInternal and move most intrinsics to libraries #4466
Comments
Here's a list of builtins that could potential be moved to libraries with this change:
Choosing these based on one/both of these criteria:
It might be a good idea to handle |
Funny that you mention this. I was just thinking about this last night. My question was: "Can't we just 'cImport' (or something similar) all the intrinsics for each platform and make them available in a specific Your proposal is very much in the same direction and obviously more thought out. So I'm supportive of this idea. Not sure if it should be compiler internal or something else as what is available depends more on the target than on the compiler I guess. I imagine this can also make the compiler simpler, because it will have way less buildins and instead most if not all can move to library code. In any case it would be good to have access to all intrinsics one way or another. |
The separation between standards and implementation in C++ has lead to great difficulty when porting codebases between compilers. Something I really like about Zig is that intrinsics are mandated as part of the language spec and not compiler extensions. This puts the implementation burden on the compiler implementer, instead of every programmer using the compiler. I think there's room in Zig for something like a |
The proposal as written is not sufficient to replace all the intrinsic it claims. Specifically, the intrinsics that accept integers accept any llvm integer, from 1 bit to 65565 bits. |
Hardware details are totally irrelevant to a language. What should be guaranteed is IEEE-754 (modulo subnormals, and <fenv.h>). |
Yes, I agree compiler specific behavior is bad and should be avoided as much as possible. I've dealt with the crazy macro hackery to workaround different compilers before and it's no fun. The intent of this proposal is to help with this. This feature would primarily be used by compiler authors inside the standard library and should ideally never be used in 99.9% of user code (I hoped the name However, new compiler implementations likely need to implement new ISA specific behavior (besides perfomance, what's the point of writing a new compiler otherwise?). |
I'm interested in having a built-in that provides intristics. There are some great x86 instructions that I want to use, but which aren't supported by Zig. I'm currently experimenting with fast ways to decode variable-length integers, and most of my solutions benefit from certain unsupported instructions like However, I'm not the greatest fan of tying this to the compiler. I'd be more interesting in tying it to things like instruction sets and OS-specific interrupts. If a compiler has some special features, they could be tied to this as well, but in general, if a Instead of tying Here's an example of what I want to be able to do (some safety checks omitted):
|
Thanks for the proposal. I'm rejecting it as I don't see it as beneficial.
I consider this to be a downside. We don't want to expose LLVM intrinsics directly.
Always use
It's not magic, it's the same thing as Using
You wouldn't want to do this for the same reason that you wouldn't want to swap out the implementation of
It is more common for the intent to be "perform the mathematical sin function" than "use a hardware sin instruction or emit a compile error". In the rarer latter case the programmer is free to use inline assembly.
This just moves the burden to the programmer who now has to add a dependency on a specific compiler implementation in order to use a builtin.
Anyway, none of this really matters because this proposal is just arguing for namespacing differently, and having one namespace in the language and one namespace outside the language. Namespacing with a prefix such as |
The idea is to add a
@compilerInternal
builtin to implement compiler specific features in a standardized way. Here's an example of what it might look like:The behavior of
@compilerInternal
is compiler dependent, the only requirements are:Here's why I think this feature will be useful:
Allow code to take advantage of LLVM intrinsics when needed
Provides a way to use LLVM intrinsics #2291 without making LLVM a core part of the language
Allows intrinsics to be implemented in a separate library
With
@compilerInternal
it should be possible to move most builtin intrinsics to a separateintrinsic
library. Then instead of using something like@sin
, usestd.math.sin
. Here's why I think this is a good idea:I don't want to have the option to choose between
@sin()
orstd.math.sin()
, I just want to usestd.math.sin
and get the best implementation.I think having builtins in the default namespace makes them a core part of the language, but a lot of them are highly ISA specific. For example, most embedded systems are lucky to have hardware floating point, so stuff like
@sin
seems out of place as a core part of the language.One design goal of zig is to make function calls obvious in code, but
@sin
is magic. Does it use hardware instruction? Does it call a software implementation? Which one (std.math.sin
,libc sin
)? Were is the source code? Usingstd.math.sin
is less magical.Another reason to move intrinsics to libraries is it's easier to switch out the implementation. For example, if I use
const sin = std.math.sin;
it's much easier to later change the implementation toconst sin = mySin;
. I can't do this as easily with@sin
.The intrinsics library would always resolve to the hardware implementation and produce a compiler error for unsupported architectures. This makes the programmers intent more clear.
Makes alternative implementations of zig easier to implement since there's less builtins in the language.
ISA specific extensions
Gives alternative compiler implementations/backends an official way to extended the language without breaking standard zig code. The keil 8051 C compiler is an example of how ISA specific extensions can be done wrong. They added new keywords to support 8051, but they choose words like
code
,data
. If you used those as variable names, the official "workaround" is to rename all your variables.So for example with compiler internal this might look something like:
(This is just an example, in reality zig would probably handle this use case with: address spaces and/or link sections)
The text was updated successfully, but these errors were encountered: