TreeTransform: QualType for OpenCL float4

Hello list,

I am currently prototyping source-to-source transformation using
TreeTransform and would like to replace an OpenCL float variable type
by a vector variant like float4 (i.e. vectorize code) [0].

Background

OpenCL vector types are represented as “extended vectors” in Clang
AstContext provides a method to construct these as a QualType: QualType getExtVectorType (QualType VectorType, unsigned NumElts) const Return the unique reference to an extended vector type of the specified element type and size. Is this what you need? Sameer.

Hi Sameer,

thank you very much for your answer. It indeed works fine, altough it seems that
pointer types can not be converted by
  SemaRef.getASTContext().getExtVectorType(Param->getType(), 4);
because assert(vecType->isBuiltinType() || vecType->isDependentType()); is
failing in ASTContext.cpp.

The print method returns for such a transformed ParmVarDecl:

  float data __attribute__((ext_vector_type(4)))

which is also reflected in the AST dump:

  ParmVarDecl 0x2aa64f0 <line:2:5, col:11> col:11 data 'float
__attribute__((ext_vector_type(4)))'

If I declare a float16 variable in the program that's parsed the original type
name is preserved (float16):
  'float16':'float __attribute__((ext_vector_type(16)))'

as you can see here:

DeclStmt 0x2aa63e0 <line:4:5, col:26>
    `-VarDecl 0x2aa6350 <col:5,
../libclc/generic/include/clc/float/definitions.h:1:25>
demoMinReductionKernel.cl:4:13 agg 'float16':'float
__attribute__((ext_vector_type(16)))' cinit
      `-ImplicitCastExpr 0x2aa63c8
<../libclc/generic/include/clc/float/definitions.h:1:25> 'float16':'float
__attribute__((ext_vector_type(16)))' <VectorSplat>
        `-FloatingLiteral 0x2aa63a8 <col:25> 'float' 3.402823e+38

Do you now if there is some sort of alias that needs to be modified or set as
well?

Best regards
Christoph

Sema::addImplicitTypedef() seems to be meant for registering a "name" for a type, although I have never used it myself! Can't make out if this will help your purpose, which is to print "float16" instead of the entire extended vector syntax.

Sameer.

Hi Sameer,

thanks again for your answer :slight_smile:
Unfortunately it turns out that this doesn't work.
So the print out is still: float data __attribute__((ext_vector_type(4)))
But I could imagine from the use of the method that it can be used to register
type aliases like in C++ typedef. But I might be wrong though.

I will try to look on it later today again. Maybe I'll find something in the
Parser or so.

Best
Christoph

Hi Sameer, hi list,

you were right with your hint. OpenCL vector types (float2,float4,..)
are (at least in libclc) TypedefTypes. So pointing me to
Sema::addImplicitTypedef() was good. The types are declared for example
in libclc/generic/include/clc/clctypes.h:

  typedef __attribute__((ext_vector_type(4))) float float4;

Known identifiers are stored in ASTContext's IdentifierTable.
Having this it's possible to ask Sema for the QualType of a known
identifier using it's IdentifierInfo. The following snippet assumes
a reference to an instance of Sema in the variable SemaRef and shows
how you can retrieve the QualType of a previously declared type:

  IdentifierInfo& VectorII = SemaRef.getASTContext().Idents.get("float4");
  ParsedType PT = SemaRef.getTypeName(VectorII, SourceLocation(),
SemaRef.getCurScope());
  QualType Q = PT.get();

Best regards
Christoph