Add options for fp16 and fp64 and disable unsupported CL3 features (#722)

* Add options for fp16 and fp64 and disable unsupported CL3 features

Some OpenCL applications (e.g. clpeak) conditionally enable FP16/FP64 in
kernels based on the presence of the cl_khr_fp{16,64} macros. When using
the SPIR target Clang always enables all OpenCL extensions and features
potantially resulting in applications attempting to use features not
supported on the target device.

Since Clang always enables all features when using the SPIR target,
it means that clspv must either pass a list of features to be disabled
to Clang (not sure this would be accepted upstream) or disable features
after the target-specific defaults have been applied. The latter is the
approach proposed in this change: extensions and features are disabled
after the creation of the TargetInfo and before the CompilerInstance
is configured to use it.

The new options are enabled by default for backwards compatibility.

Signed-off-by: Kévin Petit <>
10 files changed
tree: fe2bcdc42a55890730f014575d0cff74c72dd03b
  1. amber/
  2. cmake/
  3. docs/
  4. include/
  5. kokoro/
  6. lib/
  7. test/
  8. tools/
  9. utils/
  10. .clang-format
  11. .gitignore
  13. CMakeLists.txt
  16. deps.json


Clspv is a prototype compiler for a subset of OpenCL C to Vulkan compute shaders.

It consists of:

  • A set of LLVM Module passes to transform a dialect of LLVM IR into a SPIR-V module containing Vulkan compute shaders.
  • A command line compiler tool called ‘clspv’ to compile a subset of OpenCL C into a Vulkan compute shader.

Clspv depends on external projects:


Clspv is licensed under the terms of the Apache 2.0 license. The AUTHORS file lists the copyright owners, while individual credit is given in the CONTRIBUTORS file. To contribute, see

Materials in projects Clspv depends on are licensed under their own terms.

Clspv is not an official Google product.


The compiler is an incomplete prototype, with many rough edges.

The input language is a subset of OpenCL C version 1.2. The OpenCL C on Vulkan Specification describes the specific subset, and also the mapping into Vulkan compute shaders.


Compile a set of kernels into a SPIR-V binary module:

clspv -o foo.spv

Emit the binary as a C initializer list, for easy embedding of a shader in in a C or C++ program source:

clspv -mfmt=c -o -

Predefine some preprocessor symbols:

clspv -DWIDTH=32 -DHEIGHT=64 -o foo.spv

Use OpenCL compiler options:

clspv -cl-fast-relaxed-math -cl-single-precision-constant -o foo.spv

Show help:

clspv -help



You will need:

  • CMake
  • Python3
  • A C++ compiler
  • git

Getting sources for dependencies

Clspv depends on the sources for other projects, at specific commits. Run the following command to download those dependencies, and place them in the third_party directory:

python3 utils/


Then, create a build directory:

cd <clspv-dir>
mkdir build
cd build

Then configure and build the code:

cmake <clspv-dir>
cmake --build .

This will build the clspv command line compiler and place it in location bin/clspv under the build directory.

Using Ninja to build, and other build options

We recommend you use the Ninja build tool if it's available. To do so, replace the last two commands with:

cmake -G Ninja <clspv-dir>

Other useful configuration options (the first cmake command):

  • -DCMAKE_BUILD_TYPE=RelWithDebInfo : Build in release mode, with debugging information. Default is a debug build.

See the CMake documentation for more generic options.


To run the test suite from within the build directory:

cmake --build . --target check-spirv

Or if you are using Ninja:

ninja check-spirv

Builtin Library

Clspv includes an LLVM IR library (cmake/clspv--.bc) containing implementations of some OpenCL builtin functions. This file is distributed under the LLVM license (included in LICENSE) as it is generated from the LLVM sub-project libclc. Refer to the source for the relevant copyrights.

Rebuilding the Library

Required Tools

You will need a pre-built version of LLVM for your system, CMake, and (optionally) Ninja.


To rebuild the library run the following commands:

cmake -GNinja -DLIBCLC_TARGETS_TO_BUILD=“clspv--” -DLLVM_CONFIG= ninja

Copy the resulting clspv--.bc into the cmake/ directory and rebuild clspv.