[LLVMdev] LLVM and OpenMP
wmatyjewicz at fastmail.fm
Thu Nov 29 11:11:11 PST 2007
Pertti Kellomäki wrote:
> Since we are already using LLVM for other purposes, we thought
> about using LLVM for analysis and then OpenMP for compilation.
> The idea was to use the LLVM back end to spit out C code with
> OpenMP directives. However, looking at the C code that llc
> produces, it seems that this might be a non-starter, as loops
> have already been turned into gotos in the generated C.
> I suspect we may need to do something else, but if anyone has
> any bright ideas on how to use LLVM for this purpose, I'd be
> very grateful.
I have some ideas, but I'm not sure if they are bright...:)
As you have noticed, loops aren't represented directly in the LLVM IR.
However, there are analysis passes which may be helpful to "reconstruct"
them. For example: LoopInfo pass detects natural loops (as sets of basic
blocks) and ScalarEvolution pass finds loop induction variables (and
also does many other things). Using them and some own solutions it
should be possible to detect most loops that are potential candidates
for parallelization. Is this what you would like to achieve?
If you know a loop is parallelizable, it is possible, in general, in
LLVM to extract it (its blocks) into a new function. In the place of the
original loop a code spawning some threads calling this new function can
be inserted. Each function call gets different iteration range for the
loop. If you're interested I can send you a very simplified but working
example that demonstrates it (it's about 500 lines).
More information about the llvm-dev