llvm/llvm-project
libclc
libclc/ is libclc — an implementation of the OpenCL C standard library. It compiles OpenCL kernel source into bitcode that is later linked with user-written kernels to provide the OpenCL built-ins (get_global_id, barrier, mad, dot, math functions, atomics, etc.).
Purpose
OpenCL kernels rely on a large surface of host-provided built-ins. libclc supplies that surface as portable bitcode that can be combined with hardware-specific code-gen output. It targets AMDGPU, NVPTX, SPIR-V, and a "generic" software target. From libclc/README.md:
libclc is intended to be used with the Clang compiler's OpenCL frontend.
Directory layout
libclc/
├── clc/ # The CLC (Common Library for OpenCL) implementation — most of the math
├── opencl/ # OpenCL-specific wrappers and headers
├── amdgcn/, amdgcn-amdhsa/, ptx/, ptx-nvidiacl/, spirv/, spirv64/, generic/
│ # Per-target overlay directories
├── cmake/
├── test/
├── utils/ # gen_convert.py, gen_format.py, prepare-builtins
├── docs/
└── README.mdThe library is built by compiling OpenCL C source to bitcode using the just-built clang. The output is libclc-<target>-<vendor>.bc, suitable for linking into compiled OpenCL kernels.
How it works
graph LR
src["libclc OpenCL C source<br/>(libclc/clc/, opencl/)"] --> clang[clang -emit-llvm-bc<br/>--target=spirv64/...]
clang --> bc[Per-target bitcode]
bc --> link[Linked with user kernel]
link --> kernel[Final kernel module]The build invokes clang on each source file with the appropriate target triple, then concatenates and finalizes the bitcode. The result is shipped to OpenCL runtimes (driver-side) or to compilers that target SPIR-V.
What ships
A non-exhaustive list:
- Math (
sin,cos,pow,sqrt,mad,fma, etc.) — in IEEE-754-correct and "native_" fast variants - Vector arithmetic
- Synchronization (
barrier,mem_fence) - Atomics
- Conversion (
convert_int4, etc.) — extensively templated - Geometric, common, integer, relational built-ins
- Workgroup queries (
get_global_id,get_local_size, ...)
Integration points
- Clang — produces SPIR-V or per-target bitcode that consumes libclc's symbols.
- AMDGPU / NVPTX backends in
llvm/lib/Target/— the codegen targets. - OpenCL drivers consume the resulting bitcode at runtime or at JIT time.
Entry points for modification
- Adding a built-in: implement under
libclc/clc/(orlibclc/opencl/for OpenCL-specific shapes), update the per-target manifests, and add a test. - Updating math precision: math sources are in
libclc/clc/lib/math/. The precision policy is documented inline in each file.
Reference
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.
Previous
libsycl
Next
orc-rt