Skip to content
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

[HLSL 2021] Overloaded array operator error about constness #4340

Closed
tomjohnstone opened this issue Mar 21, 2022 · 2 comments · Fixed by #5018
Closed

[HLSL 2021] Overloaded array operator error about constness #4340

tomjohnstone opened this issue Mar 21, 2022 · 2 comments · Fixed by #5018
Labels
bug Bug, regression, crash hlsl2021 Pertaining to HLSL2021 features

Comments

@tomjohnstone
Copy link

tomjohnstone commented Mar 21, 2022

The following snippet illustrates an error I get when trying to overload the array operator. The error output is:

error: no viable overloaded operator[] for type 'const BindlessTexture2D'
note: candidate function not viable: 'this' argument has type 'const BindlessTexture2D', but method is not marked const

Regards,
Tom

Texture2D Textures2D[] : register(t0, space10);

struct BindlessTexture2D
{
    uint Idx;

    float4 operator[](in uint2 pos)
    {
        return Textures2D[Idx][pos];
    }
};

struct MyConstantBuffer
{
    BindlessTexture2D MyTexture2D;
};

ConstantBuffer<MyConstantBuffer> CB : register(b1);

[numthreads(1, 1, 1)]
void CSMain()
{
    float4 value = CB.MyTexture2D[uint2(0, 1)];
}
@tomjohnstone tomjohnstone changed the title Overloaded array operator throwing error about constness Overloaded array operator throwing error about constness using HLSL2021 Mar 21, 2022
@tomjohnstone tomjohnstone changed the title Overloaded array operator throwing error about constness using HLSL2021 [HLSL 2021] Overloaded array operator error about constness Mar 21, 2022
@tex3d tex3d added the bug Bug, regression, crash label Mar 25, 2022
@tex3d
Copy link
Contributor

tex3d commented Mar 25, 2022

It would appear we support a normal named method this way without const, which HLSL does not allow on methods. Seems like a bug we should fix one way or another.

Incidentally, if you try to write to Idx in the method, you'll get another bug:

Internal Compiler error: llvm::cast<X>() argument of incompatible type!

@llvm-beanz
Copy link
Collaborator

#5018 should address the initial usability issue of the subscript operator on const variables by the same mechanism HLSL supports other methods on const instances (by ignoring the const-ness, which is gross).

We also have a proposal to add const instance methods to HLSL which we are tracking here:

https://github.com/microsoft/hlsl-specs/blob/main/proposals/0007-const-instance-methods.md

And a PR on that proposal I wrote yesterday extends it to more broadly adopt const-correctness for HLSL data types:
microsoft/hlsl-specs#34

llvm-beanz added a commit that referenced this issue Feb 13, 2023
This change should have no functional impact on compilation. The goal is
to remove cases where we rely on the subscript operator's name for
special resolution rules and instead key the behavior off an attribute
on the method declaration.

The added test cases are not exhaustive but try to cover at least one
object type exercising each code path modified in this change.

Also added a test case for HLSL 2021 user defined array operator. This
change partially addresses #4340. The remaining parts of that issue
will be addressed in a future HLSL language revision.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug, regression, crash hlsl2021 Pertaining to HLSL2021 features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants