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] Adding const to function does not work #4706

Closed
zmarlon opened this issue Oct 4, 2022 · 1 comment
Closed

[HLSL 2021] Adding const to function does not work #4706

zmarlon opened this issue Oct 4, 2022 · 1 comment

Comments

@zmarlon
Copy link

zmarlon commented Oct 4, 2022

I have the following structure, which is an abstraction to load data from a spirv physical storage buffer.

template<typename T, u32 Alignment = 8>
struct Ptr {
    u64 address;

    static Ptr<T, Alignment> from(u64 address) {
        Ptr<T, Alignment> result;
        result.address = address;
        return result;
    }
	
    T operator [](u64 index) {
        return vk::RawBufferLoad<T>(address + detail::get_stride<T>(Alignment) * index, Alignment);
    }
};

Lets say I create a Ptr<i32> and call the [] operator to retrieve an element from the physical address buffer:

#include "ptr.hlsl"

[[vk::binding(0)]] RWStructuredBuffer<int> output_buffer;

[numthreads(8, 8, 1)]
void main() {
    kamel::Ptr<i32> ptr = kamel::Ptr<i32>::from(0);
    output_buffer[0] = ptr[0];
}

This code works as expected. If I have a kamel::Ptr<i32>, I can call the [] operator. But If I change the kamel::Ptr<i32> to be a const kamel::Ptr<i32, it does not work anymore:

example.hlsl:8:27: error: no viable overloaded operator[] for type 'const kamel::Ptr<i32>'
    output_buffer[0] = ptr[0];
                       ~~~^~
./ptr.hlsl:27:11: note: candidate function not viable: 'this' argument has type 'const kamel::Ptr<i32>', but method is not marked const
        T operator [](u64 index) {
          ^

It makes sense, since T operator [](u64 address) is not marked as const und therefore cannot be called! But adding const to the function like you usually do in C++ does not solve this issue:

    T operator [](u64 index) const {
        return vk::RawBufferLoad<T>(address + detail::get_stride<T>(Alignment) * index, Alignment);
    }

It complains that const is not valid:

./ptr.hlsl:27:33: error: expected ';' at end of declaration list
        T operator [](u64 index) const {
                                ^
                                ;

I don't know if const is supported with HLSL 2021 or if it is a bug, but If not, the compiler message that the method is not marked const is missleading as it implies that there is a way to add const to the function.

@llvm-beanz
Copy link
Collaborator

This is a duplicate of #4340.

The underlying issue is that HLSL doesn't support constant instance methods. We are aware of the issues caused by the missing feature and we are looking at adding the feature to a future version of HLSL.

@llvm-beanz llvm-beanz closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants