Skip to content

Commit 47889cd

Browse files
authored
[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.
1 parent 478c24b commit 47889cd

File tree

3 files changed

+48
-113
lines changed

3 files changed

+48
-113
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,47 +2225,40 @@ static void BuildFlattenedTypeList(QualType BaseTy,
22252225
}
22262226

22272227
bool SemaHLSL::IsTypedResourceElementCompatible(clang::QualType QT) {
2228-
if (QT.isNull())
2228+
// null and array types are not allowed.
2229+
if (QT.isNull() || QT->isArrayType())
22292230
return false;
22302231

2231-
// check if the outer type was an array type
2232-
if (QT->isArrayType())
2232+
// UDT types are not allowed
2233+
if (QT->isRecordType())
22332234
return false;
22342235

2235-
llvm::SmallVector<QualType, 4> QTTypes;
2236-
BuildFlattenedTypeList(QT, QTTypes);
2237-
2238-
assert(QTTypes.size() > 0 &&
2239-
"expected at least one constituent type from non-null type");
2240-
QualType FirstQT = SemaRef.Context.getCanonicalType(QTTypes[0]);
2241-
2242-
// element count cannot exceed 4
2243-
if (QTTypes.size() > 4)
2236+
if (QT->isBooleanType() || QT->isEnumeralType())
22442237
return false;
22452238

2246-
for (QualType TempQT : QTTypes) {
2247-
// ensure homogeneity
2248-
if (!getASTContext().hasSameUnqualifiedType(FirstQT, TempQT))
2239+
// the only other valid builtin types are scalars or vectors
2240+
if (QT->isArithmeticType()) {
2241+
if (SemaRef.Context.getTypeSize(QT) / 8 > 16)
22492242
return false;
2243+
return true;
22502244
}
22512245

2252-
if (const BuiltinType *BT = FirstQT->getAs<BuiltinType>()) {
2253-
if (BT->isBooleanType() || BT->isEnumeralType())
2246+
if (const VectorType *VT = QT->getAs<VectorType>()) {
2247+
int ArraySize = VT->getNumElements();
2248+
2249+
if (ArraySize > 4)
22542250
return false;
22552251

2256-
// Check if it is an array type.
2257-
if (FirstQT->isArrayType())
2252+
QualType ElTy = VT->getElementType();
2253+
if (ElTy->isBooleanType())
22582254
return false;
2259-
}
22602255

2261-
// if the loop above completes without returning, then
2262-
// we've guaranteed homogeneity
2263-
int TotalSizeInBytes =
2264-
(SemaRef.Context.getTypeSize(FirstQT) / 8) * QTTypes.size();
2265-
if (TotalSizeInBytes > 16)
2266-
return false;
2256+
if (SemaRef.Context.getTypeSize(QT) / 8 > 16)
2257+
return false;
2258+
return true;
2259+
}
22672260

2268-
return true;
2261+
return false;
22692262
}
22702263

22712264
bool SemaHLSL::IsScalarizedLayoutCompatible(QualType T1, QualType T2) const {
Lines changed: 27 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,52 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -fnative-half-type -verify %s
22
// expected-no-diagnostics
33

4-
struct oneInt {
5-
int i;
6-
};
7-
8-
struct twoInt {
9-
int aa;
10-
int ab;
11-
};
12-
13-
struct threeInts {
14-
oneInt o;
15-
twoInt t;
16-
};
17-
18-
struct oneFloat {
19-
float f;
20-
};
21-
struct depthDiff {
22-
int i;
23-
oneInt o;
24-
oneFloat f;
25-
};
26-
27-
struct notHomogenous{
28-
int i;
29-
float f;
30-
};
31-
32-
struct EightElements {
33-
twoInt x[2];
34-
twoInt y[2];
35-
};
36-
37-
struct EightHalves {
38-
half x[8];
39-
};
40-
41-
struct intVec {
42-
int2 i;
43-
};
44-
45-
struct oneIntWithVec {
46-
int i;
47-
oneInt i2;
48-
int2 i3;
49-
};
50-
51-
struct weirdStruct {
52-
int i;
53-
intVec iv;
54-
};
554

565
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(int), "");
576
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(float), "");
587
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(float4), "");
598
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(double2), "");
60-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(oneInt), "");
61-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(oneFloat), "");
62-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(twoInt), "");
63-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(threeInts), "");
64-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(notHomogenous), "");
65-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(depthDiff), "");
66-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(EightElements), "");
67-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(EightHalves), "");
68-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(oneIntWithVec), "");
69-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(weirdStruct), "");
9+
7010
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(RWBuffer<int>), "");
7111

12+
struct s {
13+
int x;
14+
};
7215

73-
// arrays not allowed
74-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(half[4]), "");
16+
struct Empty {};
7517

7618
template<typename T> struct TemplatedBuffer {
7719
T a;
78-
__hlsl_resource_t h;
7920
};
80-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(TemplatedBuffer<int>), "");
8121

82-
struct MyStruct1 : TemplatedBuffer<float> {
83-
float x;
22+
template<typename T> struct TemplatedVector {
23+
vector<T, 4> v;
8424
};
85-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(MyStruct1), "");
8625

87-
struct MyStruct2 {
88-
const TemplatedBuffer<float> TB[10];
89-
};
90-
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(MyStruct2), "");
26+
// structs not allowed
27+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(s), "");
28+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(Empty), "");
29+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(TemplatedBuffer<int>), "");
30+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(TemplatedVector<int>), "");
9131

92-
template<typename T> struct SimpleTemplate {
93-
T a;
94-
};
32+
// arrays not allowed
33+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(half[4]), "");
9534

96-
// though the element type is incomplete, the type trait should still technically return true
97-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(SimpleTemplate<__hlsl_resource_t>), "");
35+
typedef vector<int, 8> int8;
36+
// too many elements
37+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(int8), "");
9838

99-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(SimpleTemplate<float>), "");
39+
typedef int MyInt;
40+
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(MyInt), "");
10041

42+
// bool and enums not allowed
43+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(bool), "");
44+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(vector<bool, 2>), "");
10145

102-
typedef int myInt;
46+
enum numbers { one, two, three };
10347

104-
struct TypeDefTest {
105-
int x;
106-
myInt y;
107-
};
48+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(numbers), "");
49+
50+
// size exceeds 16 bytes, and exceeds element count limit after splitting 64 bit types into 32 bit types
51+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(double3), "");
10852

109-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(TypeDefTest), "");
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-library -finclude-default-header -fnative-half-type -verify %s
22

33
// types must be complete
4-
_Static_assert(__builtin_hlsl_is_typed_resource_element_compatible(__hlsl_resource_t), "");
4+
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(__hlsl_resource_t), "");
55

66
// expected-note@+1{{forward declaration of 'notComplete'}}
77
struct notComplete;
88
// expected-error@+1{{incomplete type 'notComplete' where a complete type is required}}
99
_Static_assert(!__builtin_hlsl_is_typed_resource_element_compatible(notComplete), "");
10-

0 commit comments

Comments
 (0)