Skip to content

Error using <type_traits> header in OpenCL kernel compilation #140046

@davidrohr

Description

@davidrohr

The clang OpenCL documentation says I can use the <type_traits> header from the libcxx header in OpenCL. (https://clang.llvm.org/docs/OpenCLSupport.html)
I tried that with clang 19.1.7 and with 20.1.4, and in both cases I am facing 4 problems:

The kernel I am trying to compile is the example from the documentation:

#pragma OPENCL EXTENSION __cl_clang_function_pointers : enable
#pragma OPENCL EXTENSION __cl_clang_variadic_functions : enable
#include <type_traits>
#pragma OPENCL EXTENSION __cl_clang_function_pointers : disable
#pragma OPENCL EXTENSION __cl_clang_variadic_functions : disable

using sint_type = std::make_signed<unsigned int>::type;

__kernel void foo() {
  static_assert(!std::is_same<sint_type, unsigned int>::value);
}

I am getting from clang++ -x cl -cl-std=CLC++2021 -o test -I[libcxx_path]/include/c++/v1/ --target=spirv64v1.2 -c test.clcpp:

 
In file included from test.clcpp:3:
In file included from /usr/include/c++/v1/type_traits:420:
/usr/include/c++/v1/__config:829:8: error: "No thread API"
  829 | #      error "No thread API"
      |        ^

It seems it needs a thread API, which is not available for OpenCL.

I patched away that check in __config, and then it failed with

In file included from /usr/lib/llvm/20/bin/../../../../lib/clang/20/include/stdint.h:56:
/usr/include/stdint.h:82:15: error: typedef redefinition with different types ('int' vs 'long')
   82 | typedef int                     intptr_t;
      |                                 ^
/usr/lib/llvm/20/bin/../../../../lib/clang/20/include/opencl-c-base.h:158:25: note: previous definition is here
  158 | typedef __INTPTR_TYPE__ intptr_t;

So it seems it works, but the definitions in the headers for C and OpenCL do not match.
I could compile it in the end by adding the -cl-no-stdinc option, but I am wondering if this could not be fixed.

The documentation says I should use -I<path to libcxx checkout or installation>/include, but with that it doesn't even find <type_traits>, so By using the actual path to <type_traits> in my command line, it worked..

Finally compiling the llvm IR code to SPIR-V with the LLVM SPIR-V translator fails with

InvalidBitWidth: Invalid bit width in input: 128
clang: error: llvm-spirv command failed with exit code 9 (use -v to see invocation)

I believe the problem is that stdint.h defines float128 types, which are not supported with OpenCL, but not fully sure.

In any case, the <type_traits> header seems not really usable in OpenCL.

Metadata

Metadata

Assignees

Labels

OpenCLclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions