[cfe-dev] Matching CUDA code

Luca Atzori luca.atzori at cern.ch
Tue Jul 14 02:22:32 PDT 2015


Il giorno ven, 10/07/2015 alle 17.54 +0200, Luca Atzori ha scritto:
> Hello everybody,
> 
> [...]
> 
> First question!
> 
> Let's match CUDA qualifiers, like for example __global__, in order to
> find kernel declarations.
> 
> If I try to use a Matcher defined within an ASTConsumer, it will not
> work. For example something like: 
> 
> class MyASTConsumer : public ASTConsumer {
> public:
>   MyASTConsumer(Rewriter &R) : THandler(R) {
> 	 
>  Matcher.addMatcher(functionDecl(hasAttr(clang::attr::CUDAGlobal)).bi
> nd
> ("cex"),
> 			  &THandler);
>   }
> 
>   void HandleTranslationUnit(ASTContext &Context) override {
>     Matcher.matchAST(Context);
> 
>   }
> 
> private:
>   MatchFinder Matcher;
>   ThreadHandler THandler;
> };
> 

Actually this solution works fine, the problem seems to be how do you
define attributes in the input file. The matcher is not capable to
expand macros, so it works only when we have something like 

__attribute__((global)) void kernel(...){...}

but not defining

#define __global__ __attribute__((global))

Probably we have to interact in some way with clang's preprocessor.

> 
> Otherwise, using a solution similar the one I found here 
> http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-June/015574.html it
> will work.
> 
> void HandleTranslationUnit(ASTContext &Context) override {
>   Matcher.matchAST(Context);
> 
>   for(DeclContext::decl_iterator D = Context.getTranslationUnitDecl()
> ->decls_begin(), DEnd = Context.getTranslationUnitDecl()
> ->decls_end();
> D!=DEnd; ++D){
>    FunctionDecl* FD;
>    if((FD=dyn_cast<FunctionDecl> (*D)) != 0){
>     if(FD->hasAttr<CUDAGlobalAttr>())
>     	std::cout << "found " << FD->getNameAsString() << " fun
> decl\n";
>    }
>   }
> 
> }
> 
> What do you think about this behaviour?  Am I putting the wrong value
> in hasAttr() in the first case?
> 
> Thanks,
> Luca
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list