Clang error: attribute A can only be applied to an OpenCL kernel function (err_opencl_kernel_attr)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: attribute A can only be applied to an OpenCL kernel function (since 7.0)
error: attribute A can only be applied to a kernel function (until 6.0)
Type Error
Category Semantic Issue
Internal Id err_opencl_kernel_attr
Internal Message attribute %0 can only be applied to an OpenCL kernel function (since 7.0)
attribute %0 can only be applied to a kernel function (until 6.0)
Regular Expression (?:error|fatal error)\: attribute (.*?) can only be applied to an OpenCL kernel function
First Commit 2013-12-13 2cd9db1cefbb [OpenCL] Produce an error when the work group and vec type hint attributes

Description

The error is issued by the Clang compiler when an attribute that is only valid for OpenCL kernel functions is applied to any other type of function. OpenCL attributes provide important hints for the compiler that affect the code generation phase, optimizing execution on parallel computing architectures. Attributes such as vec_type_hint, reqd_work_group_size, work_group_size_hint, and intel_reqd_sub_group_size are specific to OpenCL kernel functions, which are functions executed on OpenCL devices. Each attribute imparts specific instructions to the compiler related to the execution characteristics of the kernel, such as suggesting vector types for arguments or specifying workgroup sizes for execution. When these attributes are incorrectly applied to non-kernel functions, the compiler generates this error to indicate the misusage. Correct application of these attributes is crucial for leveraging the specific hardware optimizations they enable in OpenCL applications.  
AI Generated

Example

In the following example, an OpenCL-specific attribute, vec_type_hint, is applied to a function that is not an OpenCL kernel function. The attribute vec_type_hint is intended to provide hints to the compiler about the vector type that the kernel function can benefit from for optimization purposes. However, when applied to a non-kernel function, the Clang compiler correctly identifies this misuse and emits an error message indicating that the vec_type_hint attribute can only be applied to an OpenCL kernel function. This underscores the importance of correctly applying OpenCL-specific attributes to the appropriate contexts to avoid compilation errors.  
AI Generated


Flags -xcl -cl-std=CL1.2

[Try out in Compiler Explorer]

Source
__attribute__((vec_type_hint(float))) void f() {}

// Applied vec_type_hint to a non-kernel function.
Compiler Output
<source>:1:44: error: attribute 'vec_type_hint' can only be applied to an OpenCL kernel function


Clang Internals (17.0.6)

Git Commit Message

[OpenCL] Produce an error when the work group and vec type hint attributes
are used on non-kernel functions.

Reviewed by Aaron over IRC!

llvm-svn: 197243

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDeclAttr.cpp (line 9530)

/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
/// attribute list to the specified decl, ignoring any type attributes.
void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const ParsedAttributesView &AttrList, const ProcessDeclAttributeOptions &Options) {
  // ...
  // FIXME: We should be able to handle this in TableGen as well. It would be
  // good to have a way to specify "these attributes must appear as a group",
  // for these. Additionally, it would be good to have a way to specify "these
  // attribute must never appear as a group" for attributes like cold and hot.
  if (!D->hasAttr<OpenCLKernelAttr>()) {
    // These attributes cannot be applied to a non-kernel function.
    if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
      // ...
      Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;

clang/lib/Sema/SemaDeclAttr.cpp (line 9533)

/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
/// attribute list to the specified decl, ignoring any type attributes.
void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const ParsedAttributesView &AttrList, const ProcessDeclAttributeOptions &Options) {
  // ...
  // FIXME: We should be able to handle this in TableGen as well. It would be
  // good to have a way to specify "these attributes must appear as a group",
  // for these. Additionally, it would be good to have a way to specify "these
  // attribute must never appear as a group" for attributes like cold and hot.
  if (!D->hasAttr<OpenCLKernelAttr>()) {
    // These attributes cannot be applied to a non-kernel function.
    if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
    // ...
    } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
      Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;

clang/lib/Sema/SemaDeclAttr.cpp (line 9536)

/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
/// attribute list to the specified decl, ignoring any type attributes.
void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const ParsedAttributesView &AttrList, const ProcessDeclAttributeOptions &Options) {
  // ...
  // FIXME: We should be able to handle this in TableGen as well. It would be
  // good to have a way to specify "these attributes must appear as a group",
  // for these. Additionally, it would be good to have a way to specify "these
  // attribute must never appear as a group" for attributes like cold and hot.
  if (!D->hasAttr<OpenCLKernelAttr>()) {
    // These attributes cannot be applied to a non-kernel function.
    if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
    // ...
    } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
    // ...
    } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
      Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;

clang/lib/Sema/SemaDeclAttr.cpp (line 9539)

/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
/// attribute list to the specified decl, ignoring any type attributes.
void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const ParsedAttributesView &AttrList, const ProcessDeclAttributeOptions &Options) {
  // ...
  // FIXME: We should be able to handle this in TableGen as well. It would be
  // good to have a way to specify "these attributes must appear as a group",
  // for these. Additionally, it would be good to have a way to specify "these
  // attribute must never appear as a group" for attributes like cold and hot.
  if (!D->hasAttr<OpenCLKernelAttr>()) {
    // These attributes cannot be applied to a non-kernel function.
    if (const auto *A = D->getAttr<ReqdWorkGroupSizeAttr>()) {
    // ...
    } else if (const auto *A = D->getAttr<WorkGroupSizeHintAttr>()) {
    // ...
    } else if (const auto *A = D->getAttr<VecTypeHintAttr>()) {
    // ...
    } else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
      Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaCUDA/amdgpu-attrs.cu

  • clang/test/SemaCUDA/amdgpu-attrs.cu:68:17: error: attribute 'reqd_work_group_size' can only be applied to an OpenCL kernel function
  • clang/test/SemaCUDA/amdgpu-attrs.cu:72:17: error: attribute 'work_group_size_hint' can only be applied to an OpenCL kernel function
  • clang/test/SemaCUDA/amdgpu-attrs.cu:76:17: error: attribute 'vec_type_hint' can only be applied to an OpenCL kernel function
  • clang/test/SemaCUDA/amdgpu-attrs.cu:80:17: error: attribute 'intel_reqd_sub_group_size' can only be applied to an OpenCL kernel function