Skip to content

Commit 4a57e83

Browse files
authored
[HLSL] Flesh out basic type typedefs (#104479)
We had a few missing typedefs that are supported by DXC. Specifically 1-element vectors, size-explicit 32-bit types and size-explicit floating point types. This adds the typedefs and a test file that just verifies the expected sizes and vector element counts. I needed to add some of these missing typedefs to address #102964, and thought instead I should try and be a bit comprehensive and put it in a separate PR. Nothing complicated here, just typedefs and static asserts to verify them.
1 parent 6fceb3e commit 4a57e83

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

clang/lib/Headers/hlsl/hlsl_basic_types.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,98 @@ namespace hlsl {
2323
// 16-bit integer.
2424
typedef unsigned short uint16_t;
2525
typedef short int16_t;
26+
27+
// 16-bit floating point.
28+
typedef half float16_t;
2629
#endif
2730

31+
// 32-bit integer.
32+
typedef int int32_t;
33+
2834
// unsigned 32-bit integer.
2935
typedef unsigned int uint;
36+
typedef unsigned int uint32_t;
37+
38+
// 32-bit floating point.
39+
typedef float float32_t;
3040

3141
// 64-bit integer.
3242
typedef unsigned long uint64_t;
3343
typedef long int64_t;
3444

45+
// 64-bit floating point
46+
typedef double float64_t;
47+
3548
// built-in vector data types:
3649

3750
#ifdef __HLSL_ENABLE_16_BIT
51+
typedef vector<int16_t, 1> int16_t1;
3852
typedef vector<int16_t, 2> int16_t2;
3953
typedef vector<int16_t, 3> int16_t3;
4054
typedef vector<int16_t, 4> int16_t4;
55+
typedef vector<uint16_t, 1> uint16_t1;
4156
typedef vector<uint16_t, 2> uint16_t2;
4257
typedef vector<uint16_t, 3> uint16_t3;
4358
typedef vector<uint16_t, 4> uint16_t4;
4459
#endif
60+
typedef vector<bool, 1> bool1;
4561
typedef vector<bool, 2> bool2;
4662
typedef vector<bool, 3> bool3;
4763
typedef vector<bool, 4> bool4;
64+
typedef vector<int, 1> int1;
4865
typedef vector<int, 2> int2;
4966
typedef vector<int, 3> int3;
5067
typedef vector<int, 4> int4;
68+
typedef vector<uint, 1> uint1;
5169
typedef vector<uint, 2> uint2;
5270
typedef vector<uint, 3> uint3;
5371
typedef vector<uint, 4> uint4;
72+
typedef vector<int32_t, 1> int32_t1;
73+
typedef vector<int32_t, 2> int32_t2;
74+
typedef vector<int32_t, 3> int32_t3;
75+
typedef vector<int32_t, 4> int32_t4;
76+
typedef vector<uint32_t, 1> uint32_t1;
77+
typedef vector<uint32_t, 2> uint32_t2;
78+
typedef vector<uint32_t, 3> uint32_t3;
79+
typedef vector<uint32_t, 4> uint32_t4;
80+
typedef vector<int64_t, 1> int64_t1;
5481
typedef vector<int64_t, 2> int64_t2;
5582
typedef vector<int64_t, 3> int64_t3;
5683
typedef vector<int64_t, 4> int64_t4;
84+
typedef vector<uint64_t, 1> uint64_t1;
5785
typedef vector<uint64_t, 2> uint64_t2;
5886
typedef vector<uint64_t, 3> uint64_t3;
5987
typedef vector<uint64_t, 4> uint64_t4;
6088

89+
typedef vector<half, 1> half1;
6190
typedef vector<half, 2> half2;
6291
typedef vector<half, 3> half3;
6392
typedef vector<half, 4> half4;
64-
93+
typedef vector<float, 1> float1;
6594
typedef vector<float, 2> float2;
6695
typedef vector<float, 3> float3;
6796
typedef vector<float, 4> float4;
97+
typedef vector<double, 1> double1;
6898
typedef vector<double, 2> double2;
6999
typedef vector<double, 3> double3;
70100
typedef vector<double, 4> double4;
71101

102+
#ifdef __HLSL_ENABLE_16_BIT
103+
typedef vector<float16_t, 1> float16_t1;
104+
typedef vector<float16_t, 2> float16_t2;
105+
typedef vector<float16_t, 3> float16_t3;
106+
typedef vector<float16_t, 4> float16_t4;
107+
#endif
108+
109+
typedef vector<float32_t, 1> float32_t1;
110+
typedef vector<float32_t, 2> float32_t2;
111+
typedef vector<float32_t, 3> float32_t3;
112+
typedef vector<float32_t, 4> float32_t4;
113+
typedef vector<float64_t, 1> float64_t1;
114+
typedef vector<float64_t, 2> float64_t2;
115+
typedef vector<float64_t, 3> float64_t3;
116+
typedef vector<float64_t, 4> float64_t4;
117+
72118
} // namespace hlsl
73119

74120
#endif //_HLSL_HLSL_BASIC_TYPES_H_
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.4-library -finclude-default-header -verify -fnative-half-type %s
2+
// RUN: %clang_cc1 -triple spirv-linux-vulkan-library -finclude-default-header -verify -fnative-half-type %s
3+
4+
// expected-no-diagnostics
5+
#define SizeCheck(Ty, SizeInBits) \
6+
_Static_assert(sizeof(Ty) == SizeInBits / 8, #Ty " is " #SizeInBits "-bit"); \
7+
_Static_assert(sizeof(Ty##1) == (SizeInBits * 1) / 8, #Ty "1 is 1x" #SizeInBits "-bit"); \
8+
_Static_assert(__builtin_vectorelements(Ty##1) == 1, #Ty "1 is has 1 " #SizeInBits "-bit element"); \
9+
_Static_assert(sizeof(Ty##2) == (SizeInBits * 2) / 8, #Ty "2 is 2x" #SizeInBits "-bit"); \
10+
_Static_assert(__builtin_vectorelements(Ty##2) == 2, #Ty "2 is has 2 " #SizeInBits "-bit element"); \
11+
_Static_assert(__builtin_vectorelements(Ty##3) == 3, #Ty "3 is has 3 " #SizeInBits "-bit element"); \
12+
_Static_assert(sizeof(Ty##4) == (SizeInBits * 4) / 8, #Ty "4 is 4x" #SizeInBits "-bit"); \
13+
_Static_assert(__builtin_vectorelements(Ty##4) == 4, #Ty "4 is has 4 " #SizeInBits "-bit element");
14+
15+
// FIXME: https://github.com/llvm/llvm-project/issues/104503 - 3 element vectors
16+
// should be the size of 3 elements not padded to 4.
17+
// _Static_assert(sizeof(Ty##3) == (SizeInBits * 3) / 8, #Ty "3 is 3x" #SizeInBits "-bit");
18+
19+
SizeCheck(int16_t, 16);
20+
SizeCheck(uint16_t, 16);
21+
SizeCheck(half, 16);
22+
SizeCheck(float16_t, 16);
23+
24+
SizeCheck(int, 32);
25+
SizeCheck(uint, 32);
26+
SizeCheck(int32_t, 32);
27+
SizeCheck(uint32_t, 32);
28+
SizeCheck(float, 32);
29+
SizeCheck(float32_t, 32);
30+
31+
SizeCheck(int64_t, 64);
32+
SizeCheck(uint64_t, 64);
33+
SizeCheck(double, 64);
34+
SizeCheck(float64_t, 64);

0 commit comments

Comments
 (0)