Skip to content

[HLSL] Implement '__builtin_hlsl_is_intangible' type trait #104544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/TokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ KEYWORD(out , KEYHLSL)
#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) KEYWORD(Name, KEYHLSL)
#include "clang/Basic/HLSLIntangibleTypes.def"

// HLSL Type traits
TYPE_TRAIT_1(__builtin_is_intangible, IsIntangibleType, KEYHLSL)

// OpenMP Type Traits
UNARY_EXPR_OR_TYPE_TRAIT(__builtin_omp_required_simd_align, OpenMPRequiredSimdAlign, KEYALL)

Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Sema/SemaHLSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class SemaHLSL : public SemaBase {
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);

bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);

// HLSL Type trait implementations
bool IsIntangibleType(const QualType T1);

private:
llvm::DenseMap<const Type *, bool> IsIntangibleTypeCache;
};

} // namespace clang
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Sema/SemaExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "clang/Sema/Scope.h"
#include "clang/Sema/ScopeInfo.h"
#include "clang/Sema/SemaCUDA.h"
#include "clang/Sema/SemaHLSL.h"
#include "clang/Sema/SemaInternal.h"
#include "clang/Sema/SemaLambda.h"
#include "clang/Sema/SemaObjC.h"
Expand Down Expand Up @@ -5098,6 +5099,7 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
case UTT_IsDestructible:
case UTT_IsNothrowDestructible:
case UTT_IsTriviallyDestructible:
case UTT_IsIntangibleType:
if (ArgTy->isIncompleteArrayType() || ArgTy->isVoidType())
return true;

Expand Down Expand Up @@ -5683,6 +5685,13 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
return true;
return false;
}
case UTT_IsIntangibleType:
if (!T->isVoidType() && !T->isIncompleteArrayType())
if (Self.RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), T,
diag::err_incomplete_type))
return false;
DiagnoseVLAInCXXTypeTrait(Self, TInfo, tok::kw___builtin_is_intangible);
return Self.HLSL().IsIntangibleType(T);
}
}

Expand Down
71 changes: 70 additions & 1 deletion clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

#include "clang/Sema/SemaHLSL.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Type.h"
#include "clang/Basic/DiagnosticSema.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/TargetInfo.h"
Expand All @@ -27,7 +29,7 @@

using namespace clang;

SemaHLSL::SemaHLSL(Sema &S) : SemaBase(S) {}
SemaHLSL::SemaHLSL(Sema &S) : SemaBase(S), IsIntangibleTypeCache(8) {}

Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer,
SourceLocation KwLoc, IdentifierInfo *Ident,
Expand Down Expand Up @@ -1154,3 +1156,70 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
}
return false;
}

static bool calculateIsIntangibleType(QualType Ty) {
Ty = Ty->getCanonicalTypeUnqualified();
if (Ty->isBuiltinType())
return Ty->isHLSLSpecificType();

llvm::SmallVector<QualType, 8> TypesToScan;
TypesToScan.push_back(Ty);
while (!TypesToScan.empty()) {
QualType T = TypesToScan.pop_back_val();
assert(T == T->getCanonicalTypeUnqualified() && "expected sugar-free type");
assert(!isa<MatrixType>(T) && "Matrix types not yet supported in HLSL");

if (const auto *AT = dyn_cast<ConstantArrayType>(T)) {
QualType ElTy = AT->getElementType()->getCanonicalTypeUnqualified();
if (ElTy->isBuiltinType())
return ElTy->isHLSLSpecificType();
TypesToScan.push_back(ElTy);
continue;
}

if (const auto *VT = dyn_cast<VectorType>(T)) {
QualType ElTy = VT->getElementType()->getCanonicalTypeUnqualified();
assert(ElTy->isBuiltinType() && "vectors can only contain builtin types");
if (ElTy->isHLSLSpecificType())
return true;
continue;
}

if (const auto *RT = dyn_cast<RecordType>(T)) {
const RecordDecl *RD = RT->getDecl();
for (const auto *FD : RD->fields()) {
QualType FieldTy = FD->getType()->getCanonicalTypeUnqualified();
if (FieldTy->isBuiltinType()) {
if (FieldTy->isHLSLSpecificType())
return true;
} else {
TypesToScan.push_back(FieldTy);
}
}

if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
for (const CXXBaseSpecifier &B : CXXRD->bases()) {
TypesToScan.push_back(B.getType()->getCanonicalTypeUnqualified());
}
}
continue;
}
}
return false;
}

bool SemaHLSL::IsIntangibleType(const clang::QualType Ty) {
if (Ty.isNull())
return false;

const auto CachedEntry = IsIntangibleTypeCache.find(Ty.getTypePtr());
if (CachedEntry != IsIntangibleTypeCache.end()) {
assert(CachedEntry->second == calculateIsIntangibleType(Ty) &&
"IsIntangibleType mismatch");
return CachedEntry->second;
}

bool IsIntangible = calculateIsIntangibleType(Ty);
IsIntangibleTypeCache[Ty.getTypePtr()] = IsIntangible;
return IsIntangible;
}
61 changes: 61 additions & 0 deletions clang/test/SemaHLSL/Types/Traits/IsIntangibleType.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -verify %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -fnative-half-type -verify %s
// expected-no-diagnostics

_Static_assert(__builtin_is_intangible(__hlsl_resource_t), "");
// no need to check array of __hlsl_resource_t, arrays of sizeless types are not supported

_Static_assert(!__builtin_is_intangible(int), "");
_Static_assert(!__builtin_is_intangible(float3), "");
_Static_assert(!__builtin_is_intangible(half[4]), "");

typedef __hlsl_resource_t Res;
_Static_assert(__builtin_is_intangible(const Res), "");
// no need to check array of Res, arrays of sizeless types are not supported

struct ABuffer {
const int i[10];
__hlsl_resource_t h;
};
_Static_assert(__builtin_is_intangible(ABuffer), "");
_Static_assert(__builtin_is_intangible(ABuffer[10]), "");

struct MyStruct {
half2 h2;
int3 i3;
};
_Static_assert(!__builtin_is_intangible(MyStruct), "");
_Static_assert(!__builtin_is_intangible(MyStruct[10]), "");

class MyClass {
int3 ivec;
float farray[12];
MyStruct ms;
ABuffer buf;
};
_Static_assert(__builtin_is_intangible(MyClass), "");
_Static_assert(__builtin_is_intangible(MyClass[2]), "");

union U {
double d[4];
Res buf;
};
_Static_assert(__builtin_is_intangible(U), "");
_Static_assert(__builtin_is_intangible(U[100]), "");

class MyClass2 {
int3 ivec;
float farray[12];
U u;
};
_Static_assert(__builtin_is_intangible(MyClass2), "");
_Static_assert(__builtin_is_intangible(MyClass2[5]), "");

class Simple {
int a;
};

class MyClass3 : MyClass2, Simple {
half h;
};
_Static_assert(__builtin_is_intangible(MyClass3), "");
11 changes: 11 additions & 0 deletions clang/test/SemaHLSL/Types/Traits/IsIntangibleTypeErrors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -verify %s

struct Undefined; // expected-note {{forward declaration of 'Undefined'}}
_Static_assert(!__builtin_is_intangible(Undefined), ""); // expected-error{{incomplete type 'Undefined' used in type trait expression}}

void fn(int X) {
// expected-error@#vla {{variable length arrays are not supported for the current target}}
// expected-error@#vla {{variable length arrays are not supported in '__builtin_is_intangible'}}
// expected-warning@#vla {{variable length arrays in C++ are a Clang extension}}
_Static_assert(!__builtin_is_intangible(int[X]), ""); // #vla
}
Loading