Skip to content

WGSL pointer aliasing rules #1457

@litherum

Description

@litherum

Consider the following snippet of GLSL:

void myFunction(inout int x, inout int y) {
    x = 17;
}
...
int a = 2;
int b = 3;
myFunction(a, b);

I believe GLSL allows compilers to implement these inout parameters with real pointers, but it also alternatively allows compilers to rewrite this code to become something like:

struct Output {
    int m;
    int n;
}
Output myFunction(int x, int y) {
    Output output;
    // Copy in
    output.m = x;
    output.n = y;
    // Run the body
    output.m = 17;
    // Return the new values of the parameters
    return output;
}
...
int a = 2;
int b = 3;
Output temp = myFunction(a, b);
// Copy out
a = temp.m; // 17
b = temp.n; // 3

That's fine, except what happens when the user calls it like this:

int c = 4;
myFunction(c, c);

If the compiler uses pointers to implement the inout parameters, then c will equal 17 after the call. However, if the compiler naively makes the above transformation, c would equal 4 after the call.

During today's F2F, @dneto0 said that this kind of aliasing is forbidden (in SPIR-V?). If such aliasing is forbidden in one of the backend languages, I think that means it has to be forbidden in all of them, and probably has to be forbidden in WGSL itself.

It would be good to get details about exactly what is forbidden, so we can make sure to add text in the spec describing what's legal and what isn't.

Starter questions: Is this aliasing forbidden at the site the reference is created? Or is it forbidden at the call site? Do references have to be globally unique throughout the whole program?

Metadata

Metadata

Assignees

No one assigned

    Labels

    wgslWebGPU Shading Language Issues

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions