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

Aaron Ballman via cfe-dev cfe-dev at lists.llvm.org
Fri Jul 12 04:54:41 PDT 2019


On Fri, Jul 12, 2019 at 3:06 AM Simeon Ehrig via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
>
> 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?

Unfortunately, you won't be able to detect that attribute in the AST
because it won't exist in the AST. When we ignore an attribute that
we've parsed (because it's unknown, doesn't apply to that construct,
etc), we do not create an AST node for it in the AST so that the
faulty attribute doesn't cause issues with invariants elsewhere in the
compiler or confuse third-party tools.

One possible solution is to locally relax the restriction on the
attribute so that it no longer is ignored, but that would likely not
be acceptable for upstreaming because we want to keep that restriction
(I presume; I know very little about CUDA specifically). See
handleConstantAttr() in SemaDeclAttr.cpp for where this particular
constraint is enforced.

HTH!

~Aaron

>
> Best regards,
> Simeon
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list