Thanks everybody for your replies,<br><br>Guoping,<br>as Tobias has pointed out many of the LLVM analysis and transformation <br>passes are designed to work on the IR. So I think this is the most natural choice.<br><br>Justin,<br>
I followed the development of the PTX backend and the upcoming full support<br>is great news.<br><br>Tobias,<br>I don't know yet what kind of optimizations I will work on.<br>It will depend also on the target architecture.<br>
Polyhedral transformations are a big candidate though (I am reading your PhD thesis...).<br>Thank you very much for the runtime trick, I will try it.<br>As you said one possible drawback is related to the fact that the<br>
specific ABI of the different vendors must be taken into considerations.<br>Eg. how the kernels in the module are represented (with metadata or name<br>mangling), address spaces, etc<br>My goal is to manage this aspects on my own in the simplest possible way with the <br>
OpenCL->LLVM->OpenCL loop. Taking also into consideration that, as far as I<br>can see, there is no agreement on how to manage address spaces.<br><br>I will keep you updated with the development of the work.<br><br>
Bye,<br><br>Alberto<br><br><div class="gmail_quote">2011/10/5 Tobias Grosser <span dir="ltr"><<a href="mailto:tobias@grosser.es">tobias@grosser.es</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 10/05/2011 06:53 PM, Alberto Magni wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi everybody,<br>
<br>
for a research project I would like to use LLVM to optimize<br>
OpenCL programs for GPUs.<br>
<br>
Due to the lack of open-source back-ends and runtimes for<br>
GPUs my idea is the following:<br>
1) compile OpenCL C into LLVM-IR (for what I read on the ML<br>
full support is close, at least foreseeable),<br>
2) apply LLVM transformations to the bitcode,<br>
3) generate the OpenCL C code from the optimized bitcode,<br>
4) use the official (Nvidia, AMD, Intel, ....) OpenCL compilers<br>
and runtimes for the actual execution of the optimized code<br>
<br>
I know that the C backend is buggy and it is no more<br>
supported but it still works with simple C programs.<br>
Remeber that OpenCL programs are usually quite simple<br>
(no function pointers, etc...)<br>
<br>
The main features to be added to the backend are:<br>
1) the "__kernel" keyword,<br>
2) the four address spaces keywords<br>
3) vector data types<br>
4) the half keyword<br>
<br>
My idea is to extensively verify the functionality the C-backend for<br>
C programs (similar to OpenCL-C ones) and possibly add the listed features.<br>
<br>
What do you think of this ? Is it feasible ?<br>
</blockquote>
<br></div></div>
Hi Alberto,<br>
<br>
this depends what you want to achieve and what kind of optimizations you want to apply.<br>
<br>
Your proposal suggests you want to transform OpenCL-C programs into LLVM-IR to apply transformations on LLVM-IR level. What kind of LLVM-IR transformations are you planning to run? To my knowledge at least the AMD, Intel and Apple OpenCL implementations use LLVM internally, so existing LLVM optimizations will not give you any benefits as they are already run in the OpenCL compilers.<br>
<br>
Depending on what kind of optimizations you want to perform, several approaches are possible. As Guoping Long suggested, you can<br>
use clang to create an OpenCL AST and use its rewriter capabilities to<br>
perform source to source transformations. This will be more a pattern match approach, but for research or for projects where you can educate<br>
people to write canonical code this might be a good choice. It is definitely an approach where it is easy to get access to higher level constructs like for-loops. On the other hand, analysis like scalar evolution are not available at this layer.<br>
<br>
Another possibility is to take an approach similar to Polly[1] (a project I work on). Here we use LLVM analysis passes to recover high level constructs from LLVM-IR and to subsequently apply higher level<br>
transformations on the LLVM-IR.<br>
<br>
If you want to apply your optimizations on LLVM-IR the translation OpenCL -> LLVM-IR should be straightforward. clang's OpenCL<br>
support is pretty good and people continue to improve it. The way back from LLVM-IR to OpenCL is more difficult. One approach you pointed out is to generate OpenCL-C with the C backend. I must admit I never looked into the C-backend, but from my knowledge it seems to work OK for selected examples, but has some problems that are regarded unsolvable. People propose to rewrite it as a regular LLVM backend. You probably need to investigate yourself how much work it is to make it useable.<br>
<br>
Another option is to pass LLVM-IR directly into the backends without ever regenerating OpenCL-C. This is a very interesting approach as most (all?) OpenCL implementations use LLVM and so you could skip the useless<br>
LLVM-IR -> OpenCL-C -> LLVM-IR conversion.<br>
As Justin pointed out LLVM includes a PTX backend that could be used to directly target NVIDIA hardware. Micah Villmow, from AMD recently submitted the first patches to open source the AMD-IL backend. As getting open source support for AMD will take some time and it is unknown how complete it will be, I will point you to another option. OpenCL has support to export and reimport implementation specific binaries through clGetProgramInfo(CL_PROGRAM_<u></u>BINARIES) and createProgramWithBinary(). The binary of the AMD OpenCL SDK is a normal elf file, that contains a section called .llvmir. You should be able to insert an optimized OpenCL program into the AMD SDK by exporting the binary of the OpenCL-C program, replacing the elf '.llvmir' section with your optimized LLVM-IR and by finally reimporting the changed binary (Let me know if you need help here). On the Euro-LLVM meeting it was also discussed if it is worthwhile to standardize the injection of LLVM-IR into OpenCL backends. Several people seemed to be interested, but also some problems regarding non target agnostic LLVM-IR were raised.<br>
<br>
Cheers<br>
Tobi<br>
<br>
P.S.: I am very interested in OpenCL optimizations. If possible, it would be nice if you could point me information about the stuff you plan to work on.<br>
</blockquote></div><br>