[LLVMdev] Example for usage of LLVM/Clang/libclc

Dan Liew dan at su-root.co.uk
Sat Feb 7 05:55:16 PST 2015


Hi Ahmed,

> @Dan:
>
>> Mark all functions apart from the kernel entry points with the internal
>> attribute and then run global dead code elimination (it should remove most
>> of the unused functions).
>> You can use the opt tool to do this.
>> e.g. if you had kernel entry points foo and bar you could run the
>> following
>> $ opt -internalize-public-api-list= foo,bar -globaldce your_program.bc
>> transformed_program.bc
>
> That would probably help, but I was aiming for a solution that doesn't
> involve knowing the name of kernel entry points, but I can definitely write
> a small script that would figure out the kernel entry points for me.

The kernel entry points are encoded in the LLVM IR as metadata when
clang generates the module. If you take a look you'll see there's a
named metadata entry called "opencl.kernels". This contains a list of
numbered metadata where each numbered metadata is kernel entry point,
e.g.

```
!opencl.kernels = !{!21}
...
!21 = metadata !{void (i32 addrspace(1)*)* @foo}
```

Using this it should be fairly straight forward to write your own C++
application that uses the llvm libraries to do the following

1. To load the bitcode into memory
2. Find the kernel entry points by reading the metadata
3. Run the internalise pass specifiying the kernel entry points
4. Run the globaldce pass to remove unused functions
5. Output the resulting bitcode.

Here's example code that tries to find kernel entry points [1].
Writing a simple tool that builds against the LLVM libraries is fairly
straight forward and I have an example here [2]. Note the example code
written is here for LLVM3.5 and I don't know about LLVM 3.6
compatibility. If you go down this route I advise you also have a read
of [3] to help you understand how to use CMake to build against LLVM
easily.


[1] https://github.com/mc-imperial/bugle/blob/master/lib/Translator/TranslateModule.cpp#L552
[2] https://github.com/delcypher/srg-llvm-pass-tutorial
[3] http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project

Hope that helps,

Dan.



More information about the llvm-dev mailing list