Skip to content

Commit ad71318

Browse files
authored
[DirectX] Move DXIL ResourceKind and ElementType to DXILABI.h. NFC
Pull Request: #78225
1 parent 8c0a611 commit ad71318

File tree

4 files changed

+109
-104
lines changed

4 files changed

+109
-104
lines changed

llvm/include/llvm/Frontend/HLSL/HLSLResource.h

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_FRONTEND_HLSL_HLSLRESOURCE_H
1515

1616
#include "llvm/IR/Metadata.h"
17+
#include "llvm/Support/DXILABI.h"
1718

1819
namespace llvm {
1920
class GlobalVariable;
@@ -29,54 +30,9 @@ enum class ResourceClass : uint8_t {
2930
NumClasses = Invalid,
3031
};
3132

32-
// The value ordering of this enumeration is part of the DXIL ABI. Elements
33-
// can only be added to the end, and not removed.
34-
enum class ResourceKind : uint32_t {
35-
Invalid = 0,
36-
Texture1D,
37-
Texture2D,
38-
Texture2DMS,
39-
Texture3D,
40-
TextureCube,
41-
Texture1DArray,
42-
Texture2DArray,
43-
Texture2DMSArray,
44-
TextureCubeArray,
45-
TypedBuffer,
46-
RawBuffer,
47-
StructuredBuffer,
48-
CBuffer,
49-
Sampler,
50-
TBuffer,
51-
RTAccelerationStructure,
52-
FeedbackTexture2D,
53-
FeedbackTexture2DArray,
54-
NumEntries,
55-
};
56-
57-
// The value ordering of this enumeration is part of the DXIL ABI. Elements
58-
// can only be added to the end, and not removed.
59-
enum class ElementType : uint32_t {
60-
Invalid = 0,
61-
I1,
62-
I16,
63-
U16,
64-
I32,
65-
U32,
66-
I64,
67-
U64,
68-
F16,
69-
F32,
70-
F64,
71-
SNormF16,
72-
UNormF16,
73-
SNormF32,
74-
UNormF32,
75-
SNormF64,
76-
UNormF64,
77-
PackedS8x32,
78-
PackedU8x32,
79-
};
33+
// For now we use DXIL ABI enum values directly. This may change in the future.
34+
using dxil::ElementType;
35+
using dxil::ResourceKind;
8036

8137
class FrontendResource {
8238
MDNode *Entry;

llvm/include/llvm/Support/DXILABI.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,54 @@ enum class ParameterKind : uint8_t {
3939
DXIL_HANDLE,
4040
};
4141

42+
/// The kind of resource for an SRV or UAV resource. Sometimes referred to as
43+
/// "Shape" in the DXIL docs.
44+
enum class ResourceKind : uint32_t {
45+
Invalid = 0,
46+
Texture1D,
47+
Texture2D,
48+
Texture2DMS,
49+
Texture3D,
50+
TextureCube,
51+
Texture1DArray,
52+
Texture2DArray,
53+
Texture2DMSArray,
54+
TextureCubeArray,
55+
TypedBuffer,
56+
RawBuffer,
57+
StructuredBuffer,
58+
CBuffer,
59+
Sampler,
60+
TBuffer,
61+
RTAccelerationStructure,
62+
FeedbackTexture2D,
63+
FeedbackTexture2DArray,
64+
NumEntries,
65+
};
66+
67+
/// The element type of an SRV or UAV resource.
68+
enum class ElementType : uint32_t {
69+
Invalid = 0,
70+
I1,
71+
I16,
72+
U16,
73+
I32,
74+
U32,
75+
I64,
76+
U64,
77+
F16,
78+
F32,
79+
F64,
80+
SNormF16,
81+
UNormF16,
82+
SNormF32,
83+
UNormF32,
84+
SNormF64,
85+
UNormF64,
86+
PackedS8x32,
87+
PackedU8x32,
88+
};
89+
4290
} // namespace dxil
4391
} // namespace llvm
4492

llvm/lib/Target/DirectX/DXILResource.cpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
using namespace llvm;
2323
using namespace llvm::dxil;
24-
using namespace llvm::hlsl;
2524

2625
template <typename T> void ResourceTable<T>::collect(Module &M) {
2726
NamedMDNode *Entry = M.getNamedMetadata(MDName);
@@ -30,7 +29,7 @@ template <typename T> void ResourceTable<T>::collect(Module &M) {
3029

3130
uint32_t Counter = 0;
3231
for (auto *Res : Entry->operands()) {
33-
Data.push_back(T(Counter++, FrontendResource(cast<MDNode>(Res))));
32+
Data.push_back(T(Counter++, hlsl::FrontendResource(cast<MDNode>(Res))));
3433
}
3534
}
3635

@@ -42,7 +41,7 @@ template <> void ResourceTable<ConstantBuffer>::collect(Module &M) {
4241
uint32_t Counter = 0;
4342
for (auto *Res : Entry->operands()) {
4443
Data.push_back(
45-
ConstantBuffer(Counter++, FrontendResource(cast<MDNode>(Res))));
44+
ConstantBuffer(Counter++, hlsl::FrontendResource(cast<MDNode>(Res))));
4645
}
4746
// FIXME: share CBufferDataLayout with CBuffer load lowering.
4847
// See https://github.com/llvm/llvm-project/issues/58381
@@ -56,7 +55,7 @@ void Resources::collect(Module &M) {
5655
CBuffers.collect(M);
5756
}
5857

59-
ResourceBase::ResourceBase(uint32_t I, FrontendResource R)
58+
ResourceBase::ResourceBase(uint32_t I, hlsl::FrontendResource R)
6059
: ID(I), GV(R.getGlobalVariable()), Name(""), Space(R.getSpace()),
6160
LowerBound(R.getResourceIndex()), RangeSize(1) {
6261
if (auto *ArrTy = dyn_cast<ArrayType>(GV->getValueType()))
@@ -107,83 +106,84 @@ StringRef ResourceBase::getElementTypeName(ElementType ElTy) {
107106
llvm_unreachable("All ElementType enums are handled in switch");
108107
}
109108

110-
void ResourceBase::printElementType(Kinds Kind, ElementType ElTy,
109+
void ResourceBase::printElementType(ResourceKind Kind, ElementType ElTy,
111110
unsigned Alignment, raw_ostream &OS) {
112111
switch (Kind) {
113112
default:
114113
// TODO: add vector size.
115114
OS << right_justify(getElementTypeName(ElTy), Alignment);
116115
break;
117-
case Kinds::RawBuffer:
116+
case ResourceKind::RawBuffer:
118117
OS << right_justify("byte", Alignment);
119118
break;
120-
case Kinds::StructuredBuffer:
119+
case ResourceKind::StructuredBuffer:
121120
OS << right_justify("struct", Alignment);
122121
break;
123-
case Kinds::CBuffer:
124-
case Kinds::Sampler:
122+
case ResourceKind::CBuffer:
123+
case ResourceKind::Sampler:
125124
OS << right_justify("NA", Alignment);
126125
break;
127-
case Kinds::Invalid:
128-
case Kinds::NumEntries:
126+
case ResourceKind::Invalid:
127+
case ResourceKind::NumEntries:
129128
break;
130129
}
131130
}
132131

133-
StringRef ResourceBase::getKindName(Kinds Kind) {
132+
StringRef ResourceBase::getKindName(ResourceKind Kind) {
134133
switch (Kind) {
135-
case Kinds::NumEntries:
136-
case Kinds::Invalid:
134+
case ResourceKind::NumEntries:
135+
case ResourceKind::Invalid:
137136
return "invalid";
138-
case Kinds::Texture1D:
137+
case ResourceKind::Texture1D:
139138
return "1d";
140-
case Kinds::Texture2D:
139+
case ResourceKind::Texture2D:
141140
return "2d";
142-
case Kinds::Texture2DMS:
141+
case ResourceKind::Texture2DMS:
143142
return "2dMS";
144-
case Kinds::Texture3D:
143+
case ResourceKind::Texture3D:
145144
return "3d";
146-
case Kinds::TextureCube:
145+
case ResourceKind::TextureCube:
147146
return "cube";
148-
case Kinds::Texture1DArray:
147+
case ResourceKind::Texture1DArray:
149148
return "1darray";
150-
case Kinds::Texture2DArray:
149+
case ResourceKind::Texture2DArray:
151150
return "2darray";
152-
case Kinds::Texture2DMSArray:
151+
case ResourceKind::Texture2DMSArray:
153152
return "2darrayMS";
154-
case Kinds::TextureCubeArray:
153+
case ResourceKind::TextureCubeArray:
155154
return "cubearray";
156-
case Kinds::TypedBuffer:
155+
case ResourceKind::TypedBuffer:
157156
return "buf";
158-
case Kinds::RawBuffer:
157+
case ResourceKind::RawBuffer:
159158
return "rawbuf";
160-
case Kinds::StructuredBuffer:
159+
case ResourceKind::StructuredBuffer:
161160
return "structbuf";
162-
case Kinds::CBuffer:
161+
case ResourceKind::CBuffer:
163162
return "cbuffer";
164-
case Kinds::Sampler:
163+
case ResourceKind::Sampler:
165164
return "sampler";
166-
case Kinds::TBuffer:
165+
case ResourceKind::TBuffer:
167166
return "tbuffer";
168-
case Kinds::RTAccelerationStructure:
167+
case ResourceKind::RTAccelerationStructure:
169168
return "ras";
170-
case Kinds::FeedbackTexture2D:
169+
case ResourceKind::FeedbackTexture2D:
171170
return "fbtex2d";
172-
case Kinds::FeedbackTexture2DArray:
171+
case ResourceKind::FeedbackTexture2DArray:
173172
return "fbtex2darray";
174173
}
175-
llvm_unreachable("All Kinds enums are handled in switch");
174+
llvm_unreachable("All ResourceKind enums are handled in switch");
176175
}
177176

178-
void ResourceBase::printKind(Kinds Kind, unsigned Alignment, raw_ostream &OS,
179-
bool SRV, bool HasCounter, uint32_t SampleCount) {
177+
void ResourceBase::printKind(ResourceKind Kind, unsigned Alignment,
178+
raw_ostream &OS, bool SRV, bool HasCounter,
179+
uint32_t SampleCount) {
180180
switch (Kind) {
181181
default:
182182
OS << right_justify(getKindName(Kind), Alignment);
183183
break;
184184

185-
case Kinds::RawBuffer:
186-
case Kinds::StructuredBuffer:
185+
case ResourceKind::RawBuffer:
186+
case ResourceKind::StructuredBuffer:
187187
if (SRV)
188188
OS << right_justify("r/o", Alignment);
189189
else {
@@ -193,22 +193,22 @@ void ResourceBase::printKind(Kinds Kind, unsigned Alignment, raw_ostream &OS,
193193
OS << right_justify("r/w+cnt", Alignment);
194194
}
195195
break;
196-
case Kinds::TypedBuffer:
196+
case ResourceKind::TypedBuffer:
197197
OS << right_justify("buf", Alignment);
198198
break;
199-
case Kinds::Texture2DMS:
200-
case Kinds::Texture2DMSArray: {
199+
case ResourceKind::Texture2DMS:
200+
case ResourceKind::Texture2DMSArray: {
201201
std::string DimName = getKindName(Kind).str();
202202
if (SampleCount)
203203
DimName += std::to_string(SampleCount);
204204
OS << right_justify(DimName, Alignment);
205205
} break;
206-
case Kinds::CBuffer:
207-
case Kinds::Sampler:
206+
case ResourceKind::CBuffer:
207+
case ResourceKind::Sampler:
208208
OS << right_justify("NA", Alignment);
209209
break;
210-
case Kinds::Invalid:
211-
case Kinds::NumEntries:
210+
case ResourceKind::Invalid:
211+
case ResourceKind::NumEntries:
212212
break;
213213
}
214214
}
@@ -258,9 +258,9 @@ void ConstantBuffer::print(raw_ostream &OS) const {
258258

259259
OS << right_justify("cbuffer", 10);
260260

261-
printElementType(Kinds::CBuffer, ElementType::Invalid, 8, OS);
261+
printElementType(ResourceKind::CBuffer, ElementType::Invalid, 8, OS);
262262

263-
printKind(Kinds::CBuffer, 12, OS, /*SRV*/ false, /*HasCounter*/ false);
263+
printKind(ResourceKind::CBuffer, 12, OS, /*SRV*/ false, /*HasCounter*/ false);
264264
// Print the binding part.
265265
ResourceBase::print(OS, "CB", "cb");
266266
}

llvm/lib/Target/DirectX/DXILResource.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Frontend/HLSL/HLSLResource.h"
1919
#include "llvm/IR/Metadata.h"
2020
#include "llvm/Support/Compiler.h"
21+
#include "llvm/Support/DXILABI.h"
2122
#include <cstdint>
2223

2324
namespace llvm {
@@ -40,19 +41,19 @@ class ResourceBase {
4041
void write(LLVMContext &Ctx, MutableArrayRef<Metadata *> Entries) const;
4142

4243
void print(raw_ostream &O, StringRef IDPrefix, StringRef BindingPrefix) const;
43-
using Kinds = hlsl::ResourceKind;
44-
static StringRef getKindName(Kinds Kind);
45-
static void printKind(Kinds Kind, unsigned Alignment, raw_ostream &OS,
46-
bool SRV = false, bool HasCounter = false,
47-
uint32_t SampleCount = 0);
44+
static StringRef getKindName(dxil::ResourceKind Kind);
45+
static void printKind(dxil::ResourceKind Kind, unsigned Alignment,
46+
raw_ostream &OS, bool SRV = false,
47+
bool HasCounter = false, uint32_t SampleCount = 0);
4848

49-
static StringRef getElementTypeName(hlsl::ElementType CompType);
50-
static void printElementType(Kinds Kind, hlsl::ElementType CompType,
51-
unsigned Alignment, raw_ostream &OS);
49+
static StringRef getElementTypeName(dxil::ElementType CompType);
50+
static void printElementType(dxil::ResourceKind Kind,
51+
dxil::ElementType CompType, unsigned Alignment,
52+
raw_ostream &OS);
5253

5354
public:
5455
struct ExtendedProperties {
55-
std::optional<hlsl::ElementType> ElementType;
56+
std::optional<dxil::ElementType> ElementType;
5657

5758
// The value ordering of this enumeration is part of the DXIL ABI. Elements
5859
// can only be added to the end, and not removed.
@@ -68,7 +69,7 @@ class ResourceBase {
6869
};
6970

7071
class UAVResource : public ResourceBase {
71-
ResourceBase::Kinds Shape;
72+
dxil::ResourceKind Shape;
7273
bool GloballyCoherent;
7374
bool HasCounter;
7475
bool IsROV;

0 commit comments

Comments
 (0)