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

[C++4OpenCL] Cannot use struct as kernel parameter when accessing through templated struct #57881

Closed
biggysmith opened this issue Sep 21, 2022 · 6 comments
Labels

Comments

@biggysmith
Copy link

Hi,

I'm having some issues when using certain types as kernel parameters. Whenever I try to use a struct defined in template type I get compile errors:

struct test_param0{
public:
    struct constants_t{
        int size;
    };
};

template<typename T>
struct test_param1{
public:
    struct constants_t{
        int size;
    };
};

// this compiles fine
kernel void test_kernel0(               
    __read_only image3d_t src_volume, 
    __write_only image3d_t dst_img,  
    __constant test_param0::constants_t& c
)              
{        
} 

// this does not compile
// error: '__constant test_param1<int>::constants_t &__private' cannot be used as the type of a kernel parameter'
kernel void test_kernel1(               
    __read_only image3d_t src_volume, 
    __write_only image3d_t dst_img,  
    __constant test_param1<int>::constants_t& c
)              
{        
} 

Is this some sort of OpenCL C++ limitation? It feels like it should be ok.

--
cmd: clang -target spirv64 -emit-llvm -cl-std=clc++2021 -O0 -c -o output.bc input.clcpp
using llvm 15

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 21, 2022

@llvm/issue-subscribers-opencl

@AnastasiaStulova
Copy link
Contributor

That's right, I can't see anything in the documentation stating that your example should be disallowed. The only relevant restrictions I can see from here https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html#kernel_function are:

Moreover the types used in parameters of the kernel functions must be:

Trivial and standard-layout types C++17 [basic.types] (plain old data types) for parameters passed by value;

Standard-layout types for pointer parameters. The same applies to references[[2](https://www.khronos.org/opencl/assets/CXX_for_OpenCL.html#_footnotedef_2)] if an implementation supports them in kernel parameters.

None of which corresponds to your case. This seems like a clang bug indeed.

@AnastasiaStulova
Copy link
Contributor

This patch seems to be fixing the reported test case but unfortunately it doesn't unlock general use of templates in kernel argument yet: https://reviews.llvm.org/D134445.

FYI as a workaround there is the following extension: https://clang.llvm.org/docs/LanguageExtensions.html#cl-clang-non-portable-kernel-param-types that can be used to enable/disable such diagnostics While I don't advertise using this extension widely it might be useful as s temporary solution while the proper fix of the diagnostics is in progress.

This example compiles with clang-15:

#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : enable
kernel void test_kernel1(               
    __read_only image3d_t src_volume, 
    __write_only image3d_t dst_img,  
    __constant test_param1<int>::constants_t& c
)              
{        
}
#pragma OPENCL EXTENSION __cl_clang_non_portable_kernel_param_types : disable

@biggysmith
Copy link
Author

I just tried this out in my more complicated example code and it works! I check manually that the struct is standard_layout, so I think this will be safe for me to use for now.

Thanks for investigating this for me.

@AnastasiaStulova
Copy link
Contributor

@AnastasiaStulova
Copy link
Contributor

Opened related issue to fix general problem: #58590

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants