Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
31ec5e5
making parameter type and shader visibility use enums
joaosaffran Aug 19, 2025
1690a9c
clean up
joaosaffran Aug 19, 2025
f6f2e61
removing root parameter header from MC
joaosaffran Aug 19, 2025
6539364
clean up
joaosaffran Aug 19, 2025
1d29111
fix whitespace in test
joaosaffran Aug 19, 2025
8eb82fd
adding missing import
joaosaffran Aug 19, 2025
d38c00d
remove unused
joaosaffran Aug 19, 2025
3b25b34
save a copy
joaosaffran Aug 19, 2025
567a3d4
remove default constructor
joaosaffran Aug 19, 2025
fb248da
rename visibility
joaosaffran Aug 19, 2025
dc436d5
remove cstdint
joaosaffran Aug 19, 2025
8353fe0
remove Loc
joaosaffran Aug 19, 2025
f3ecd8a
removing dependency of Object
joaosaffran Aug 20, 2025
8c143ba
removing binary format descriptor range dependency
joaosaffran Aug 20, 2025
a4d77d7
Revert "removing binary format descriptor range dependency"
joaosaffran Aug 20, 2025
19ec1c3
Merge branch 'main' into refactoring/updating-descriptor-range
joaosaffran Aug 29, 2025
182c817
removing binary format descriptor range dependency
joaosaffran Aug 20, 2025
f9d16d2
creating toDescriptorRange and change verifyDescriptorRangeFlag signa…
joaosaffran Aug 29, 2025
42f8f11
adding test and removing string switch
joaosaffran Aug 29, 2025
7ada31b
removing copy
joaosaffran Aug 29, 2025
fa60959
clean up
joaosaffran Aug 30, 2025
e065a82
removing function I thought I needed
joaosaffran Sep 6, 2025
17dbe9f
refactoring to use dxil::ResourceClass for range type
joaosaffran Sep 8, 2025
5c23b7e
adding assert
joaosaffran Sep 8, 2025
1c539f0
changing casting to use proper llvm-casting
joaosaffran Sep 8, 2025
4074ceb
removing unused code
joaosaffran Sep 9, 2025
36afa22
removing assert and adding lastEntry
joaosaffran Sep 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,21 @@ bool SemaHLSL::handleRootSignatureElements(
<< /*version minor*/ VersionEnum;
};

auto toDescriptorRangeType = [](llvm::dxil::ResourceClass Type) {
switch (Type) {
case llvm::dxil::ResourceClass::SRV:
return llvm::dxbc::DescriptorRangeType::SRV;
case llvm::dxil::ResourceClass::UAV:
return llvm::dxbc::DescriptorRangeType::UAV;
case llvm::dxil::ResourceClass::CBuffer:
return llvm::dxbc::DescriptorRangeType::CBV;
case llvm::dxil::ResourceClass::Sampler:
return llvm::dxbc::DescriptorRangeType::Sampler;
}

llvm_unreachable("Unhandled Resource Class");
};

// Iterate through the elements and do basic validations
for (const hlsl::RootSignatureElement &RootSigElem : Elements) {
SourceLocation Loc = RootSigElem.getLocation();
Expand Down Expand Up @@ -1282,10 +1297,8 @@ bool SemaHLSL::handleRootSignatureElements(
// value
ReportError(Loc, 1, 0xfffffffe);
}

if (!llvm::hlsl::rootsig::verifyDescriptorRangeFlag(
Version, llvm::to_underlying(Clause->Type),
llvm::to_underlying(Clause->Flags)))
Version, toDescriptorRangeType(Clause->Type), Clause->Flags))
ReportFlagError(Loc);
}
}
Expand Down
10 changes: 10 additions & 0 deletions llvm/include/llvm/BinaryFormat/DXContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ inline bool isValidParameterType(uint32_t V) {
return false;
}

#define DESCRIPTOR_RANGE(Val, Enum) \
case Val: \
return true;
inline bool isValidRangeType(uint32_t V) {
switch (V) {
#include "DXContainerConstants.def"
}
return false;
}

#define SHADER_VISIBILITY(Val, Enum) Enum = Val,
enum class ShaderVisibility : uint32_t {
#include "DXContainerConstants.def"
Expand Down
6 changes: 5 additions & 1 deletion llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue);
LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace);
LLVM_ABI bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal);
LLVM_ABI bool verifyRangeType(uint32_t Type);
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version,
dxbc::DescriptorRangeType Type,
dxbc::DescriptorRangeFlags FlagsVal);
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version,
dxbc::DescriptorRangeType Type,
uint32_t FlagsVal);
LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors);
LLVM_ABI bool verifySamplerFilter(uint32_t Value);
Expand Down
15 changes: 12 additions & 3 deletions llvm/include/llvm/MC/DXContainerRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ struct RootDescriptor {
uint32_t Flags;
};

struct DescriptorRange {
dxbc::DescriptorRangeType RangeType;
uint32_t NumDescriptors;
uint32_t BaseShaderRegister;
uint32_t RegisterSpace;
uint32_t Flags;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to keep the flags as uint32_t, since the flags are not mutually exclusive.

uint32_t OffsetInDescriptorsFromTableStart;
};

struct RootParameterInfo {
dxbc::RootParameterType Type;
dxbc::ShaderVisibility Visibility;
Expand All @@ -42,11 +51,11 @@ struct RootParameterInfo {
};

struct DescriptorTable {
SmallVector<dxbc::RTS0::v2::DescriptorRange> Ranges;
SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator begin() const {
SmallVector<DescriptorRange> Ranges;
SmallVector<DescriptorRange>::const_iterator begin() const {
return Ranges.begin();
}
SmallVector<dxbc::RTS0::v2::DescriptorRange>::const_iterator end() const {
SmallVector<DescriptorRange>::const_iterator end() const {
return Ranges.end();
}
};
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/BinaryFormat/DXContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ ArrayRef<EnumEntry<RootParameterType>> dxbc::getRootParameterTypes() {
return ArrayRef(RootParameterTypes);
}

#define DESCRIPTOR_RANGE(Val, Enum) {#Enum, DescriptorRangeType::Enum},

static const EnumEntry<DescriptorRangeType> DescriptorRangeTypes[] = {
#include "llvm/BinaryFormat/DXContainerConstants.def"
};

ArrayRef<EnumEntry<DescriptorRangeType>> dxbc::getDescriptorRangeTypes() {
Copy link
Contributor Author

@joaosaffran joaosaffran Aug 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replicating the pattern to use with enumToStringRef

return ArrayRef(DescriptorRangeTypes);
}

#define SEMANTIC_KIND(Val, Enum) {#Enum, PSV::SemanticKind::Enum},

static const EnumEntry<PSV::SemanticKind> SemanticKindNames[] = {
Expand Down
27 changes: 11 additions & 16 deletions llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,23 @@ Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table,
if (RangeDescriptorNode->getNumOperands() != 6)
return make_error<InvalidRSMetadataFormat>("Descriptor Range");

dxbc::RTS0::v2::DescriptorRange Range;
mcdxbc::DescriptorRange Range;

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

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

Range.RangeType =
StringSwitch<uint32_t>(*ElementText)
.Case("CBV", to_underlying(dxbc::DescriptorRangeType::CBV))
.Case("SRV", to_underlying(dxbc::DescriptorRangeType::SRV))
.Case("UAV", to_underlying(dxbc::DescriptorRangeType::UAV))
.Case("Sampler", to_underlying(dxbc::DescriptorRangeType::Sampler))
.Default(~0U);

if (Range.RangeType == ~0U)
if (*ElementText == "CBV")
Range.RangeType = dxbc::DescriptorRangeType::CBV;
else if (*ElementText == "SRV")
Range.RangeType = dxbc::DescriptorRangeType::SRV;
else if (*ElementText == "UAV")
Range.RangeType = dxbc::DescriptorRangeType::UAV;
else if (*ElementText == "Sampler")
Range.RangeType = dxbc::DescriptorRangeType::Sampler;
else
return make_error<GenericRSMetadataError>("Invalid Descriptor Range type.",
RangeDescriptorNode);

Expand Down Expand Up @@ -568,12 +568,7 @@ Error MetadataParser::validateRootSignature(
case dxbc::RootParameterType::DescriptorTable: {
const mcdxbc::DescriptorTable &Table =
RSD.ParametersContainer.getDescriptorTable(Info.Location);
for (const dxbc::RTS0::v2::DescriptorRange &Range : Table) {
if (!hlsl::rootsig::verifyRangeType(Range.RangeType))
DeferredErrs =
joinErrors(std::move(DeferredErrs),
make_error<RootSignatureValidationError<uint32_t>>(
"RangeType", Range.RangeType));
Comment on lines -571 to -576
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Range type should've been verified and converted to an enum by this point, so this check don't make sense anymore.

for (const mcdxbc::DescriptorRange &Range : Table) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove whitespace below this line


if (!hlsl::rootsig::verifyRegisterSpace(Range.RegisterSpace))
DeferredErrs =
Expand Down
16 changes: 11 additions & 5 deletions llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ bool verifyRangeType(uint32_t Type) {
return false;
}

bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
uint32_t FlagsVal) {
bool verifyDescriptorRangeFlag(uint32_t Version, dxbc::DescriptorRangeType Type,
dxbc::DescriptorRangeFlags Flags) {
using FlagT = dxbc::DescriptorRangeFlags;
FlagT Flags = FlagT(FlagsVal);

const bool IsSampler =
(Type == llvm::to_underlying(dxbc::DescriptorRangeType::Sampler));
const bool IsSampler = (Type == dxbc::DescriptorRangeType::Sampler);

if (Version == 1) {
// Since the metadata is unversioned, we expect to explicitly see the values
Expand Down Expand Up @@ -125,6 +123,14 @@ bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
return (Flags & ~Mask) == FlagT::None;
}

// This is included to avoid including BinaryFormat/DXContainer to make the
// flags conversion, since this might cause compilation times to increase.
bool verifyDescriptorRangeFlag(uint32_t Version, dxbc::DescriptorRangeType Type,
uint32_t Flags) {
return verifyDescriptorRangeFlag(Version, Type,
dxbc::DescriptorRangeFlags(Flags));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated before, this is being added to avoid adding a header include, which is also recommended by llvm coding styles https://llvm.org/docs/CodingStandards.html#include-as-little-as-possible


bool verifyNumDescriptors(uint32_t NumDescriptors) {
return NumDescriptors > 0;
}
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/ObjectYAML/DXContainerEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,10 @@ Error DXContainerWriter::writeParts(raw_ostream &OS) {
P.RootSignature->Parameters.getOrInsertTable(L);
mcdxbc::DescriptorTable Table;
for (const auto &R : TableYaml.Ranges) {

dxbc::RTS0::v2::DescriptorRange Range;
Range.RangeType = R.RangeType;
assert(dxbc::isValidRangeType(R.RangeType) &&
"Invalid Descriptor Range Type");
Comment on lines +316 to +317
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we use enums in the yaml represention this should be impossible to trigger. But I am making it clear here, by adding this assert.

mcdxbc::DescriptorRange Range;
Range.RangeType = dxbc::DescriptorRangeType(R.RangeType);
Range.NumDescriptors = R.NumDescriptors;
Range.BaseShaderRegister = R.BaseShaderRegister;
Range.RegisterSpace = R.RegisterSpace;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static void validateRootSignature(Module &M,
const mcdxbc::DescriptorTable &Table =
RSD.ParametersContainer.getDescriptorTable(ParamInfo.Location);

for (const dxbc::RTS0::v2::DescriptorRange &Range : Table.Ranges) {
for (const mcdxbc::DescriptorRange &Range : Table.Ranges) {
uint32_t UpperBound =
Range.NumDescriptors == ~0U
? Range.BaseShaderRegister
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Target/DirectX/DXILRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
RS.ParametersContainer.getDescriptorTable(Info.Location);
OS << " NumRanges: " << Table.Ranges.size() << "\n";

for (const dxbc::RTS0::v2::DescriptorRange Range : Table) {
OS << " - Range Type: " << Range.RangeType << "\n"
for (const mcdxbc::DescriptorRange &Range : Table) {
OS << " - Range Type: "
<< enumToStringRef(Range.RangeType,
dxbc::getDescriptorRangeTypes())
<< "\n"
<< " Register Space: " << Range.RegisterSpace << "\n"
<< " Base Shader Register: " << Range.BaseShaderRegister << "\n"
<< " Num Descriptors: " << Range.NumDescriptors << "\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
;CHECK-NEXT: - Parameter Type: DescriptorTable
;CHECK-NEXT: Shader Visibility: All
;CHECK-NEXT: NumRanges: 2
;CHECK-NEXT: - Range Type: 0
;CHECK-NEXT: - Range Type: SRV
;CHECK-NEXT: Register Space: 0
;CHECK-NEXT: Base Shader Register: 1
;CHECK-NEXT: Num Descriptors: 1
;CHECK-NEXT: Offset In Descriptors From Table Start: 4294967295
;CHECK-NEXT: Flags: 4
;CHECK-NEXT: - Range Type: 1
;CHECK-NEXT: - Range Type: UAV
;CHECK-NEXT: Register Space: 10
;CHECK-NEXT: Base Shader Register: 1
;CHECK-NEXT: Num Descriptors: 5
Expand Down