[clang] 47889cd - [HLSL] Add empty struct test cases to `__builtin_hlsl_is_typed_resource_element_compatible` test file (#115045)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 14 17:01:16 PST 2024
Author: Joshua Batista
Date: 2024-11-14T17:01:13-08:00
New Revision: 47889cdd23e57c0acb68adff44ce5657dc86640e
URL: https://github.com/llvm/llvm-project/commit/47889cdd23e57c0acb68adff44ce5657dc86640e
DIFF: https://github.com/llvm/llvm-project/commit/47889cdd23e57c0acb68adff44ce5657dc86640e.diff
LOG: [HLSL] Add empty struct test cases to `__builtin_hlsl_is_typed_resource_element_compatible` test file (#115045)
This PR adds empty struct cases to the test file for the builtin.
Added:
Modified:
clang/lib/Sema/SemaHLSL.cpp
clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatible.hlsl
clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatibleErrors.hlsl
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 65b0d9cd65637f..79d0d7399fbe2f 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2225,47 +2225,40 @@ static void BuildFlattenedTypeList(QualType BaseTy,
}
bool SemaHLSL::IsTypedResourceElementCompatible(clang::QualType QT) {
- if (QT.isNull())
+ // null and array types are not allowed.
+ if (QT.isNull() || QT->isArrayType())
return false;
- // check if the outer type was an array type
- if (QT->isArrayType())
+ // UDT types are not allowed
+ if (QT->isRecordType())
return false;
- llvm::SmallVector<QualType, 4> QTTypes;
- BuildFlattenedTypeList(QT, QTTypes);
-
- assert(QTTypes.size() > 0 &&
- "expected at least one constituent type from non-null type");
- QualType FirstQT = SemaRef.Context.getCanonicalType(QTTypes[0]);
-
- // element count cannot exceed 4
- if (QTTypes.size() > 4)
+ if (QT->isBooleanType() || QT->isEnumeralType())
return false;
- for (QualType TempQT : QTTypes) {
- // ensure homogeneity
- if (!getASTContext().hasSameUnqualifiedType(FirstQT, TempQT))
+ // the only other valid builtin types are scalars or vectors
+ if (QT->isArithmeticType()) {
+ if (SemaRef.Context.getTypeSize(QT) / 8 > 16)
return false;
+ return true;
}
- if (const BuiltinType *BT = FirstQT->getAs<BuiltinType>()) {
- if (BT->isBooleanType() || BT->isEnumeralType())
+ if (const VectorType *VT = QT->getAs<VectorType>()) {
+ int ArraySize = VT->getNumElements();
+
+ if (ArraySize > 4)
return false;
- // Check if it is an array type.
- if (FirstQT->isArrayType())
+ QualType ElTy = VT->getElementType();
+ if (ElTy->isBooleanType())
return false;
- }
- // if the loop above completes without returning, then
- // we've guaranteed homogeneity
- int TotalSizeInBytes =
- (SemaRef.Context.getTypeSize(FirstQT) / 8) * QTTypes.size();
- if (TotalSizeInBytes > 16)
- return false;
+ if (SemaRef.Context.getTypeSize(QT) / 8 > 16)
+ return false;
+ return true;
+ }
- return true;
+ return false;
}
bool SemaHLSL::IsScalarizedLayoutCompatible(QualType T1, QualType T2) const {
diff --git a/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatible.hlsl b/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatible.hlsl
index acc1f281daddfc..0467844e1cab8c 100644
--- a/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatible.hlsl
+++ b/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatible.hlsl
@@ -1,109 +1,52 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -fnative-half-type -verify %s
// expected-no-diagnostics
-struct oneInt {
- int i;
-};
-
-struct twoInt {
- int aa;
- int ab;
-};
-
-struct threeInts {
- oneInt o;
- twoInt t;
-};
-
-struct oneFloat {
- float f;
-};
-struct depthDiff {
- int i;
- oneInt o;
- oneFloat f;
-};
-
-struct notHomogenous{
- int i;
- float f;
-};
-
-struct EightElements {
- twoInt x[2];
- twoInt y[2];
-};
-
-struct EightHalves {
-half x[8];
-};
-
-struct intVec {
- int2 i;
-};
-
-struct oneIntWithVec {
- int i;
- oneInt i2;
- int2 i3;
-};
-
-struct weirdStruct {
- int i;
- intVec iv;
-};
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(int), "");
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(float), "");
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(float4), "");
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(double2), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(oneInt), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(oneFloat), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(twoInt), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(threeInts), "");
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(notHomogenous), "");
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(depthDiff), "");
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(EightElements), "");
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(EightHalves), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(oneIntWithVec), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(weirdStruct), "");
+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(RWBuffer<int>), "");
+struct s {
+ int x;
+};
-// arrays not allowed
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(half[4]), "");
+struct Empty {};
template<typename T> struct TemplatedBuffer {
T a;
- __hlsl_resource_t h;
};
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(TemplatedBuffer<int>), "");
-struct MyStruct1 : TemplatedBuffer<float> {
- float x;
+template<typename T> struct TemplatedVector {
+ vector<T, 4> v;
};
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(MyStruct1), "");
-struct MyStruct2 {
- const TemplatedBuffer<float> TB[10];
-};
-_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(MyStruct2), "");
+// structs not allowed
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(s), "");
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(Empty), "");
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(TemplatedBuffer<int>), "");
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(TemplatedVector<int>), "");
-template<typename T> struct SimpleTemplate {
- T a;
-};
+// arrays not allowed
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(half[4]), "");
-// though the element type is incomplete, the type trait should still technically return true
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(SimpleTemplate<__hlsl_resource_t>), "");
+typedef vector<int, 8> int8;
+// too many elements
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(int8), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(SimpleTemplate<float>), "");
+typedef int MyInt;
+_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(MyInt), "");
+// bool and enums not allowed
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(bool), "");
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(vector<bool, 2>), "");
-typedef int myInt;
+enum numbers { one, two, three };
-struct TypeDefTest {
- int x;
- myInt y;
-};
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(numbers), "");
+
+// size exceeds 16 bytes, and exceeds element count limit after splitting 64 bit types into 32 bit types
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(double3), "");
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(TypeDefTest), "");
diff --git a/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatibleErrors.hlsl b/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatibleErrors.hlsl
index cb3e9ae7a61509..d3d79aa0499e54 100644
--- a/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatibleErrors.hlsl
+++ b/clang/test/SemaHLSL/Types/Traits/IsTypedResourceElementCompatibleErrors.hlsl
@@ -1,10 +1,9 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -fnative-half-type -verify %s
// types must be complete
-_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(__hlsl_resource_t), "");
+_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(__hlsl_resource_t), "");
// expected-note at +1{{forward declaration of 'notComplete'}}
struct notComplete;
// expected-error at +1{{incomplete type 'notComplete' where a complete type is required}}
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(notComplete), "");
-
More information about the cfe-commits
mailing list