-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[proposal] Mark external function as pure #5838
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
Comments
I think this would fit nicely into the callconvention configuration proposed in #21209, for example like |
No, function purity is not related to calling convention. |
It would also be nice if additionally only specific calls of an external function could be marked as pure, for example if you don't care whether there are side effects (maybe because it doesn't affect you) or if a function will not have side effects with specific input (e.g. This proposal doesn't propose specific syntax for Zig. If Zig had tags/annotations/attributes it might use those but because it doesn't have them for now I would propose adding a extern fn "library" pure square(num: c_int) callconv(.C) c_int; Alternatively it could go after the parameters, before the return type. To mark only specific calls of an external function as pure I propose: @call(.pure, square, .{ 3 }); Calling non-external functions this way would be a compile error. This could for example be used in the standard library to perhaps allow the compiler to optimize away unused memory allocations by marking call sites like this as pure: zig/lib/std/heap/PageAllocator.zig Line 39 in 218cf05
Status quo:
|
Ehhh, I don't think reserving |
I completely agree, it's just that I can't really think of any other way to specify it considering that you can't combine it with any existing constructs such as |
Right now, there is no way to tell llvm to optimize external functions as if they were pure
godbolt example: https://godbolt.org/z/7xbo8r
It would be useful if there was a way to mark external functions as pure to allow repeated calls to be optimized away
The text was updated successfully, but these errors were encountered: