[clang] [HLSL] Add implicit resource element type concepts to AST (PR #112600)
Helena Kotas via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 31 11:31:28 PDT 2024
================
@@ -483,10 +582,101 @@ static BuiltinTypeDeclBuilder setupBufferType(CXXRecordDecl *Decl, Sema &S,
.addDefaultHandleConstructor(S, RC);
}
+BinaryOperator *constructSizeOfLEQ16Expr(ASTContext &Context,
+ SourceLocation NameLoc,
+ TemplateTypeParmDecl *T) {
+ // Obtain the QualType for 'unsigned long'
+ QualType UnsignedLongType = Context.UnsignedLongTy;
+
+ // Create a QualType that points to this TemplateTypeParmDecl
+ QualType TType = Context.getTypeDeclType(T);
+
+ // Create a TypeSourceInfo for the template type parameter 'T'
+ TypeSourceInfo *TTypeSourceInfo =
+ Context.getTrivialTypeSourceInfo(TType, NameLoc);
+
+ UnaryExprOrTypeTraitExpr *sizeOfExpr = new (Context) UnaryExprOrTypeTraitExpr(
+ UETT_SizeOf, TTypeSourceInfo, UnsignedLongType, NameLoc, NameLoc);
+
+ // Create an IntegerLiteral for the value '16' with size type
+ QualType SizeType = Context.getSizeType();
+ llvm::APInt SizeValue = llvm::APInt(Context.getTypeSize(SizeType), 16);
+ IntegerLiteral *SizeLiteral =
+ new (Context) IntegerLiteral(Context, SizeValue, SizeType, NameLoc);
+
+ QualType BoolTy = Context.BoolTy;
+
+ BinaryOperator *binaryOperator =
+ BinaryOperator::Create(Context, sizeOfExpr, // Left-hand side expression
+ SizeLiteral, // Right-hand side expression
+ BO_LE, // Binary operator kind (<=)
+ BoolTy, // Result type (bool)
+ VK_LValue, // Value kind
+ OK_Ordinary, // Object kind
+ NameLoc, // Source location of operator
+ FPOptionsOverride());
+
+ return binaryOperator;
+}
+
+Expr *constructTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc,
+ TemplateTypeParmDecl *T) {
+ ASTContext &Context = S.getASTContext();
+
+ // first get the "sizeof(T) <= 16" expression, as a binary operator
+ BinaryOperator *SizeOfLEQ16 = constructSizeOfLEQ16Expr(Context, NameLoc, T);
+ // TODO: add the '__builtin_hlsl_is_line_vector_layout_compatible' builtin
+ // and return a binary operator that evaluates the builtin on the given
+ // template type parameter 'T'
+ return SizeOfLEQ16;
+}
+
+ConceptDecl *constructTypedBufferConceptDecl(Sema &S) {
+ DeclContext *DC = S.CurContext;
+ ASTContext &Context = S.getASTContext();
+ SourceLocation DeclLoc = SourceLocation();
+
+ IdentifierInfo &IsValidLineVectorII =
+ Context.Idents.get("is_valid_line_vector");
+ IdentifierInfo &ElementTypeII = Context.Idents.get("element_type");
+ TemplateTypeParmDecl *T = TemplateTypeParmDecl::Create(
+ Context, Context.getTranslationUnitDecl(), DeclLoc, DeclLoc,
+ /*depth=*/0,
+ /*position=*/0,
+ /*id=*/&ElementTypeII,
+ /*Typename=*/true,
+ /*ParameterPack=*/false);
+
+ T->setDeclContext(DC);
+ T->setReferenced();
+
+ // Create and Attach Template Parameter List to ConceptDecl
+ TemplateParameterList *ConceptParams = TemplateParameterList::Create(
+ Context, DeclLoc, DeclLoc, {T}, DeclLoc, nullptr);
+
+ DeclarationName DeclName = DeclarationName(&IsValidLineVectorII);
+ Expr *ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T);
+
+ // Create a ConceptDecl
+ ConceptDecl *CD =
+ ConceptDecl::Create(Context, Context.getTranslationUnitDecl(), DeclLoc,
+ DeclName, ConceptParams, ConstraintExpr);
+
+ // Attach the template parameter list to the ConceptDecl
+ CD->setTemplateParameters(ConceptParams);
+
+ // Add the concept declaration to the Translation Unit Decl
+ Context.getTranslationUnitDecl()->addDecl(CD);
+
+ return CD;
+}
+
void HLSLExternalSemaSource::defineHLSLTypesWithForwardDeclarations() {
CXXRecordDecl *Decl;
+ ConceptDecl *CD = constructTypedBufferConceptDecl(*SemaPtr);
----------------
hekota wrote:
I'd probably use a more descriptive name here, like `TypeBufferConcept` since as we'll need to have one for RawBuffers as well, right?
https://github.com/llvm/llvm-project/pull/112600
More information about the cfe-commits
mailing list