[cfe-dev] Transform (CUDA)-Attributes in the AST

Simeon Ehrig via cfe-dev cfe-dev at lists.llvm.org
Fri Jul 12 00:06:16 PDT 2019


Dear all,

I try to transform a variable declaration with the CUDA attribute 
__constant__ in a function. The complete code, which I want to transform 
is the following:

     void __cling_Un1Qu31(void* vpClingValue) {
       __constant__ int array[1024];
       ;
     }

I have to transform the AST before the transformation to a semantic 
Attribute take place, because the attribute __constant__ has the 
semantic property that it cannot be declared as a locale variable. 
That's also the reason why I want to transform the AST. Unfortunately 
the function Decl::hasAttr<T>() doesn't work without semantic 
transformation either.

I have already started writing an ASTTransformer:

     ASTTransformer::Result 
CUDAConstantMemoryTransformer::Transform(clang::Decl *D) {
       clang::FunctionDecl *F = llvm::dyn_cast<clang::FunctionDecl>(D);
       if (F && F->hasBody() && 
(F->getNameInfo().getName().getAsString().rfind("__cling_", 0) == 0)) {
         //F->dump();
       }
     }

At this point I don't know how to check if  a statement or declaration 
has the attribute __constant__ . When I run F->dump(), I get the 
following output:

     FunctionDecl 0x5fb1cf0 <input_line_3:1:1, line:4:1> line:1:6 
__cling_Un1Qu30 'void (void *)'
     |-ParmVarDecl 0x5fb1c50 <col:22, col:28> col:28 vpClingValue 'void *'
     `-CompoundStmt 0x5fb1eb8 <col:42, line:4:1>
       |-DeclStmt 0x5fb1e90 
</usr/local/cuda/include/host_defines.h:86:9, input_line_3:2:30>
       | `-VarDecl 0x5fb1e30 
</usr/local/cuda/include/host_defines.h:86:9, input_line_3:2:29> col:19 
array 'int [1024]'
       `-NullStmt 0x5fb1ea8 <line:3:1>

I tried different functions on different AST nodes (in special DeclStmt 
and VarDecl), but I didn't found a solution to detect the __constant__ 
attribute.

Can anyone help me to detect and change the __constant__ attribute in 
the AST?

Best regards,
Simeon




More information about the cfe-dev mailing list