[cfe-dev] OpenCL/CUDA Interop with PTX Back-End

Speziale Ettore speziale.ettore at gmail.com
Tue Oct 4 23:28:41 PDT 2011


I can report my experience with my OpenCL runtime. First of all consider
I'm developing it in spare time.

> The built-in functions would be declared by a header file that belongs
> to an OpenCL C runtime library (not to be confused with the OpenCL
> Platform Layer or OpenCL Runtime defined by sections 4 and 5 of the
> OpenCL specification).  The runtime library in this case would consist
> of a set of header files and (optionally) a static or shared library
> file which together implement section 6.11 of the OpenCL specification.
> The runtime library as a project would be a separate project from Clang
> (but it may be a potential LLVM sub-project).

The first observation is that many OpenCL C library functions are
overloaded for vector types. The semantic of those functions is simply
to apply the scalar version of the function to all elements of the
vector. Thus, I described the runtime functions I need through TableGen
files, then I wrote a couple of TableGen backends. The first generates
the implementations of vector versions using scalar versions. Scalar
versions are coded by hand, but now I have only used functions that
easely map on a mathematic builting (e.g. cos*, sin*, ...). The second
generates an "ocldef.h" header files, that have been added to clang
Header lib. That file is automatically included by clang every time it
compiles OpenCL C sources.

> The driver would be extended to support locating the runtime
> library's main header file, which could be installed in a known
> location, pre-including it using the -include command line option
> to the frontend (so that the functions declared by the header file
> are available to every OpenCL program), and setting linker options
> so that the runtime library is linked into the final executable.

Linking is performed at codegen time inside the OpenCL runtime. Now I
support only CPU devices, so I use the llvm JIT to generate the kernel
and link it with the OpenCL C runtime library. It must be in the
standard llvm bitcode library path -- it is distributed in bitcode

To support my runtime, I have to generate some metadata about kernels
when compiling OpenCL C. Thus I have subclassed the CGOpenCLRuntime
class provided by Peter, by adding my extra functionalities. The last
thing I have added to clang is fixing the size of data types -- e.g. int
must be 32 bits on all targets.

The runtime will be released as soon as it will reach a minimum level of
quality. For the clang patches, I can send them to the ML if anyone are

Best regards,
speziale.ettore at gmail.com

More information about the cfe-dev mailing list