Clspv is a prototype compiler for a subset of OpenCL C to Vulkan compute shaders
Switch branches/tags
Nothing to show
Clone or download
kpet and alan-baker Fix codegen for logical operations on bool vectors (#210)
Bitwise instructions were being emitted. The tests added
only cover And and Or as I couldn't find a way of
producing Xor on boolean vectors which seems to line up
with a few observations from the OpenCL specification:

- there is no xor logical operator (although ^^ has been
  reserved for that use)
- boolean vectors aren't explicitly specified (even
  though booln (2,3,4,8 and 16) is a reserved type)
- the result of relational operators applied to two
  vectors is guaranteed to be a vector of signed integers
  (this is the case that made me discover the issue)

Clang's omission of the sign extension to int vectors
when the result of multiple relational operators is
only used by a logical operation is an optimisation.
Since there is no logical xor operator it is
reasonable for Clang to always sign extend the result
of relational operators (according to §6.3d of the
OpenCL 1.2 specification) that feed into an xor
operator and perform the operation as bitwise.

An optimisation similar to that done for And and Or
could be made by clspv in cases where the results of
relational operators are only used by xor operators
but should IMHO be discussed separately from this
change.

Also tidy up a few related things:

- use isIntOrIntVectorTy(1) where it makes the code clearer
- tighten the check in SPIRVProducerPass::GenerateInstruction
  that special cases Instruction::Xor for booleans. It was
  allowing OpLogicalNot to be emitted in all cases where one
  of the operands was constant, which is correct only when the
  constrant is 1. The value of the constant is now checked.

Signed-off-by: Kévin Petit <kpet@free.fr>
Latest commit 24272b6 Oct 18, 2018

README.md

clspv

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:

Legal

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 CONTRIBUTING.md.

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

Clspv is not an official Google product.

Status

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.

Examples

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

clspv foo.cl -o foo.spv

Emit SPIR-V assembly:

clspv -S foo.cl -o foo.spvasm

Emit SPIR-V assembly to standard output:

clspv -S foo.cl -o -

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 foo.cl -o -

Predefine some preprocessor symbols:

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

Use OpenCL compiler options:

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

Show help:

clspv -help

Build

Tools

You will need:

  • CMake
  • Python
  • 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:

python utils/fetch_sources.py

Building

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>
ninja

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.

Test

To run the test suite from within the build directory:

cmake --build . --target check-spirv

Or if you are using Ninja:

ninja check-spirv