Hello,
I am quite new in clang and I am intrested in OpenACC support.
My goal is to write a tool to transform C code with OpenACC directives to OpenCL code, so I am just focusing at the parsing
phase and AST creation for now.
I keep an eye in a thread about OpenMP "OpenMP support in CLANG: A proposal" as both cases are quite similar. I have a concern
about the structure of my implementation so far.
As far as I can tell, any #pragma directives must be handled by the Preprocessor (that's where they belong). But in the case of
OpenMP and OpenACC directives I think a Parser handler is more appropriate, because we need some semantic information about
their clauses not available in the Preprocessor.
I will give a random example in OpenMP of what I mean, as OpenACC may not be so popular yet!
#pragma omp parallel private(tid, i) shared(n,A,B,C,D)
{
tid = omp_get_thread_num();
if (tid == 0)
nthreads = omp_get_num_threads();
#pragma omp for nowait
for (i=0; i<n; i++) {
B[i] = 2.0*A[i];
}
#pragma omp for
for (i=0; i<n; i++) {
D[i] = 1.0/C[i];
}
}
In the above code the first OpenMP directive has a 'shared' clause with a parameter list of elements n,A,B,C,D.
The Preprocessor cannot say whether these identifiers are actually declared previously as variables or not, but the Parser can
with help from Sema actions and produce better diagnostics. Also already implemented methods from Parser can be used to parse
this clause e.g. a modified version of ParseExpressionList(). The OpenACC specification has almost identical structure for its
clauses.
As I found, at the end of a directive in general, the Preprocessor returns the eod token to Parser. Is this the only visible
change in Preprocessors behavior from the Parser's perspective?
In my code I split the parsing in a Preprocessor (PP) Handler and a Parser Handler.
1. The PP Handler recognizes the start point of a OpenACC directive ('#' 'pragma' 'acc') and replaces it with an annotated token
(tok::annot_pragma_acc).
2. The Parser Handler consumes the body of the directive until eod using the available semantic information from Sema.
I have made a patch (attached) that lets these types of "Extension" pragma directives to be handled by the Parser instead of the
Preprocessor. The patch actually adds some bits in the PragmaHandler class in "include/clang/Lex/Pragma.h", hopefully not too
much!
I also send my initial implementation of the Preprocessor and Parser Handlers (just the intresting parts).
I don't know if this is a right approach so please take a look at it and let me know.
for more information about OpenACC
http://www.openacc-standard.org/
Thanks,
Maroudas Manolis
pragma-handlers-extension.patch (4.28 KB)
openacc-preprocessor-handler.h (317 Bytes)
openacc-preprocessor-handler.cpp (767 Bytes)
openacc-parser-handler.cpp (2.15 KB)