-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
noalias on all parameters by default (with debug safety); ability to specify mayalias #1108
Comments
Related: #476 |
It is, but there have been some issues. It looks like rust currently has related optimizations disabled due to rust-lang/rust#54878 |
What if you could provide a list of variables that it might alias? |
This was essentially how GCC leveraged unions to specify semantics of aliased data operations, see https://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html#union_1 We could piggyback on union semantics for this by introducing a new kind of union, an |
I took the opportunity to translate the 'Benefits to The Strict Aliasing Rule' C example into Zig on Godbolt here, https://godbolt.org/z/r3dhdo. Notice that LLVM always reloads the memory at uniform.*.b with movzx ecx, word ptr [rsi + 2] inside the loop body instead of hoisting the load up out of the body to occur once. This is sub-optimal if values and uniform never overlap in memory in the loop. It's a bit harder to understand the output with --release-fast, although the compiler seems to generate a few different loop structures based on how many elements could be processed in wider SIMD units per iteration. This implies the aliasing semantics between --release-fast and --release-safe are currently different. |
The LLVM patch for full noalias optimization is a quite big one and not yet upstream. Also look at a Rust example. By the way, noalias might not be enough. But that is to come after noalias and restrict lands in LLVM. |
Would this apply to pointers inside structs? For example, if I pass a parameter of type |
@SpexGuy Two pointers pointing to the same object are by construction forbidden, but I dont understand how your struct is assumed to be used. An object means always a continuous chunk of stuff, so you will have the base address and length known at comptime in the typesystem. |
I'm extracting this (flawed) proposal out of #733.
mayalias
keywordnoalias
keywordmayalias
valid only on a pointer parameter. It means "may be aliased by global variables or other arguments to this function which are mutable pointers"*T
,*[N]T
, and[]T
) overlap with each other, or with any const pointers with known sizesunknown len ptr in the set do a safety check to find overlap
a scope unique to the function but shared by each other (the function's
const ptr alias scope)
a unique scope per var
function's noalias var scopes they are not based on, and the function's
const ptr alias scope
Depends on #561 so we can put llvm parameter attributes on slice pointers
This proposal needs work. Consider this example:
This would trigger the proposed debug safety but it does not actually represent problematic behavior, since the value is never accessed via the other pointer.
One proposal adjustment could be to do all the runtime safety only at store instructions for everything. I fear this would be too slow in practice, however it is worth experimenting with before shutting the idea down. I'll first verify that LLVM would be able to take advantage of these annotations though.
The text was updated successfully, but these errors were encountered: