Open
Description
I'm extracting this (flawed) proposal out of #733.
- Add
mayalias
keyword - Remove
noalias
keyword mayalias
valid only on a pointer parameter. It means "may be aliased by global variables or other arguments to this function which are mutable pointers"- Debug safety
- At the beginning of each function, verify that no mutable pointers with known sizes (
*T
,*[N]T
, and[]T
) overlap with each other, or with any const pointers with known sizes
- At the beginning of each function, verify that no mutable pointers with known sizes (
- In Zig IR, pointer values track the set of noalias parameters and global variables possibly referenced
- slice and getelementptr instructions that include a noalias var of
unknown len ptr in the set do a safety check to find overlap
- slice and getelementptr instructions that include a noalias var of
- When generating LLVM,
- load instructions based on const ptr noalias variables !alias.scope
a scope unique to the function but shared by each other (the function's
const ptr alias scope) - load instructions based on mut ptr noalias variables !alias.scope
a unique scope per var - Store instructions based on noalias variables !noalias all the
function's noalias var scopes they are not based on, and the function's
const ptr alias scope
- load instructions based on const ptr noalias variables !alias.scope
- Verify that LLVM can take advantage of these annotations
Depends on #561 so we can put llvm parameter attributes on slice pointers
This proposal needs work. Consider this example:
const Context = struct {
// some useful fields, and then
preallocated_point: Point,
};
const Point = struct {
x: i32,
y: i32,
}
fn incrementXAndPrint(self: *Context, pt: *Point) {
pt.x += 1;
self.log("point: {v}\n", pt);
}
test "aoeu" {
var c = Context {
.preallocated_point = Point { .x = 0, .y = 0 },
};
incrementXAndPrint(&c, &c.preallocated_point);
}
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
To do
Milestone
Relationships
Development
No branches or pull requests
Activity
andrewrk commentedon Sep 26, 2018
Related: #476
daurnimator commentedon Nov 28, 2018
It is, but there have been some issues. It looks like rust currently has related optimizations disabled due to rust-lang/rust#54878
@fieldParentPtr
violates type based aliasing guarantees #1644daurnimator commentedon Jul 20, 2019
What if you could provide a list of variables that it might alias?
16 remaining items
return
statement #2765