Skip to content

Commit 9e778f6

Browse files
authored
[DirectX] Removing dxbc DescriptorRange from mcbxdc (#154629)
MC Descriptor Range Representation currently depend on Object structures. This PR removes that dependency and in order to facilitate removing to_underlying usage in follow-up PRs.
1 parent 11a4f5b commit 9e778f6

File tree

13 files changed

+52
-89
lines changed

13 files changed

+52
-89
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,9 +1295,8 @@ bool SemaHLSL::handleRootSignatureElements(
12951295
ReportError(Loc, 1, 0xfffffffe);
12961296
}
12971297

1298-
if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag(
1299-
Version, llvm::to_underlying(Clause->Type),
1300-
llvm::to_underlying(Clause->Flags)))
1298+
if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag(Version, Clause->Type,
1299+
Clause->Flags))
13011300
ReportFlagError(Loc);
13021301
}
13031302
}

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,6 @@ enum class RootParameterType : uint32_t {
192192

193193
LLVM_ABI ArrayRef<EnumEntry<RootParameterType>> getRootParameterTypes();
194194

195-
#define DESCRIPTOR_RANGE(Val, Enum) Enum = Val,
196-
enum class DescriptorRangeType : uint32_t {
197-
#include "DXContainerConstants.def"
198-
};
199-
200-
LLVM_ABI ArrayRef<EnumEntry<DescriptorRangeType>> getDescriptorRangeTypes();
201-
202195
#define ROOT_PARAMETER(Val, Enum) \
203196
case Val: \
204197
return true;
@@ -209,6 +202,10 @@ inline bool isValidParameterType(uint32_t V) {
209202
return false;
210203
}
211204

205+
inline bool isValidRangeType(uint32_t V) {
206+
return V <= llvm::to_underlying(dxil::ResourceClass::LastEntry);
207+
}
208+
212209
#define SHADER_VISIBILITY(Val, Enum) Enum = Val,
213210
enum class ShaderVisibility : uint32_t {
214211
#include "DXContainerConstants.def"

llvm/include/llvm/BinaryFormat/DXContainerConstants.def

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@ DESCRIPTOR_RANGE_FLAG(0x10000, DescriptorsStaticKeepingBufferBoundsChecks, DESCR
104104
#undef DESCRIPTOR_RANGE_FLAG
105105
#endif // DESCRIPTOR_RANGE_FLAG
106106

107-
// DESCRIPTOR_RANGE(value, name).
108-
#ifdef DESCRIPTOR_RANGE
109-
110-
DESCRIPTOR_RANGE(0, SRV)
111-
DESCRIPTOR_RANGE(1, UAV)
112-
DESCRIPTOR_RANGE(2, CBV)
113-
DESCRIPTOR_RANGE(3, Sampler)
114-
#undef DESCRIPTOR_RANGE
115-
#endif // DESCRIPTOR_RANGE
116-
117107
#ifdef ROOT_PARAMETER
118108

119109
ROOT_PARAMETER(0, DescriptorTable)

llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue);
3030
LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace);
3131
LLVM_ABI bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal);
3232
LLVM_ABI bool verifyRangeType(uint32_t Type);
33-
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
34-
uint32_t FlagsVal);
33+
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version,
34+
dxil::ResourceClass Type,
35+
dxbc::DescriptorRangeFlags FlagsVal);
3536
LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors);
3637
LLVM_ABI bool verifySamplerFilter(uint32_t Value);
3738
LLVM_ABI bool verifyAddress(uint32_t Address);

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ struct RootDescriptor {
3131
uint32_t Flags;
3232
};
3333

34+
struct DescriptorRange {
35+
dxil::ResourceClass RangeType;
36+
uint32_t NumDescriptors;
37+
uint32_t BaseShaderRegister;
38+
uint32_t RegisterSpace;
39+
uint32_t Flags;
40+
uint32_t OffsetInDescriptorsFromTableStart;
41+
};
42+
3443
struct RootParameterInfo {
3544
dxbc::RootParameterType Type;
3645
dxbc::ShaderVisibility Visibility;
@@ -42,11 +51,11 @@ struct RootParameterInfo {
4251
};
4352

4453
struct DescriptorTable {
45-
SmallVector<dxbc::RTS0::v2::DescriptorRange> Ranges;
46-
SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator begin() const {
54+
SmallVector<DescriptorRange> Ranges;
55+
SmallVector<DescriptorRange>::const_iterator begin() const {
4756
return Ranges.begin();
4857
}
49-
SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator end() const {
58+
SmallVector<DescriptorRange>::const_iterator end() const {
5059
return Ranges.end();
5160
}
5261
};

llvm/include/llvm/Support/DXILABI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum class ResourceClass : uint8_t {
2828
UAV,
2929
CBuffer,
3030
Sampler,
31+
LastEntry = Sampler,
3132
};
3233

3334
/// The kind of resource for an SRV or UAV resource. Sometimes referred to as

llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,23 @@ Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table,
322322
if (RangeDescriptorNode->getNumOperands() != 6)
323323
return make_error<InvalidRSMetadataFormat>("Descriptor Range");
324324

325-
dxbc::RTS0::v2::DescriptorRange Range;
325+
mcdxbc::DescriptorRange Range;
326326

327327
std::optional<StringRef> ElementText =
328328
extractMdStringValue(RangeDescriptorNode, 0);
329329

330330
if (!ElementText.has_value())
331331
return make_error<InvalidRSMetadataFormat>("Descriptor Range");
332332

333-
Range.RangeType =
334-
StringSwitch<uint32_t>(*ElementText)
335-
.Case("CBV", to_underlying(dxbc::DescriptorRangeType::CBV))
336-
.Case("SRV", to_underlying(dxbc::DescriptorRangeType::SRV))
337-
.Case("UAV", to_underlying(dxbc::DescriptorRangeType::UAV))
338-
.Case("Sampler", to_underlying(dxbc::DescriptorRangeType::Sampler))
339-
.Default(~0U);
340-
341-
if (Range.RangeType == ~0U)
333+
if (*ElementText == "CBV")
334+
Range.RangeType = dxil::ResourceClass::CBuffer;
335+
else if (*ElementText == "SRV")
336+
Range.RangeType = dxil::ResourceClass::SRV;
337+
else if (*ElementText == "UAV")
338+
Range.RangeType = dxil::ResourceClass::UAV;
339+
else if (*ElementText == "Sampler")
340+
Range.RangeType = dxil::ResourceClass::Sampler;
341+
else
342342
return make_error<GenericRSMetadataError>("Invalid Descriptor Range type.",
343343
RangeDescriptorNode);
344344

@@ -568,13 +568,7 @@ Error MetadataParser::validateRootSignature(
568568
case dxbc::RootParameterType::DescriptorTable: {
569569
const mcdxbc::DescriptorTable &Table =
570570
RSD.ParametersContainer.getDescriptorTable(Info.Location);
571-
for (const dxbc::RTS0::v2::DescriptorRange &Range : Table) {
572-
if (!hlsl::rootsig::verifyRangeType(Range.RangeType))
573-
DeferredErrs =
574-
joinErrors(std::move(DeferredErrs),
575-
make_error<RootSignatureValidationError<uint32_t>>(
576-
"RangeType", Range.RangeType));
577-
571+
for (const mcdxbc::DescriptorRange &Range : Table) {
578572
if (!hlsl::rootsig::verifyRegisterSpace(Range.RegisterSpace))
579573
DeferredErrs =
580574
joinErrors(std::move(DeferredErrs),
@@ -588,7 +582,8 @@ Error MetadataParser::validateRootSignature(
588582
"NumDescriptors", Range.NumDescriptors));
589583

590584
if (!hlsl::rootsig::verifyDescriptorRangeFlag(
591-
RSD.Version, Range.RangeType, Range.Flags))
585+
RSD.Version, Range.RangeType,
586+
dxbc::DescriptorRangeFlags(Range.Flags)))
592587
DeferredErrs =
593588
joinErrors(std::move(DeferredErrs),
594589
make_error<RootSignatureValidationError<uint32_t>>(

llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,11 @@ bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal) {
5151
return (Flags | DataFlags) == DataFlags;
5252
}
5353

54-
bool verifyRangeType(uint32_t Type) {
55-
switch (Type) {
56-
case llvm::to_underlying(dxbc::DescriptorRangeType::CBV):
57-
case llvm::to_underlying(dxbc::DescriptorRangeType::SRV):
58-
case llvm::to_underlying(dxbc::DescriptorRangeType::UAV):
59-
case llvm::to_underlying(dxbc::DescriptorRangeType::Sampler):
60-
return true;
61-
};
62-
63-
return false;
64-
}
65-
66-
bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
67-
uint32_t FlagsVal) {
54+
bool verifyDescriptorRangeFlag(uint32_t Version, dxil::ResourceClass Type,
55+
dxbc::DescriptorRangeFlags Flags) {
6856
using FlagT = dxbc::DescriptorRangeFlags;
69-
FlagT Flags = FlagT(FlagsVal);
7057

71-
const bool IsSampler =
72-
(Type == llvm::to_underlying(dxbc::DescriptorRangeType::Sampler));
58+
const bool IsSampler = (Type == dxil::ResourceClass::Sampler);
7359

7460
if (Version == 1) {
7561
// Since the metadata is unversioned, we expect to explicitly see the values

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ void RootSignatureDesc::write(raw_ostream &OS) const {
137137
llvm::endianness::little);
138138
rewriteOffsetToCurrentByte(BOS, writePlaceholder(BOS));
139139
for (const auto &Range : Table) {
140-
support::endian::write(BOS, Range.RangeType, llvm::endianness::little);
140+
support::endian::write(BOS, static_cast<uint32_t>(Range.RangeType),
141+
llvm::endianness::little);
141142
support::endian::write(BOS, Range.NumDescriptors,
142143
llvm::endianness::little);
143144
support::endian::write(BOS, Range.BaseShaderRegister,

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@ Error DXContainerWriter::writeParts(raw_ostream &OS) {
313313
P.RootSignature->Parameters.getOrInsertTable(L);
314314
mcdxbc::DescriptorTable Table;
315315
for (const auto &R : TableYaml.Ranges) {
316-
317-
dxbc::RTS0::v2::DescriptorRange Range;
318-
Range.RangeType = R.RangeType;
316+
assert(dxbc::isValidRangeType(R.RangeType) &&
317+
"Invalid Descriptor Range Type");
318+
mcdxbc::DescriptorRange Range;
319+
Range.RangeType = dxil::ResourceClass(R.RangeType);
319320
Range.NumDescriptors = R.NumDescriptors;
320321
Range.BaseShaderRegister = R.BaseShaderRegister;
321322
Range.RegisterSpace = R.RegisterSpace;

0 commit comments

Comments
 (0)