Skip to content

[NFC][HLSL][DirectX] Move DXILRootSignature validations to RootSignatureValidations library #147110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ namespace llvm {
namespace hlsl {
namespace rootsig {

// Basic verification of RootElements

bool verifyRootFlag(uint32_t Flags);
bool verifyVersion(uint32_t Version);
bool verifyRegisterValue(uint32_t RegisterValue);
bool verifyRegisterSpace(uint32_t RegisterSpace);
bool verifyDescriptorFlag(uint32_t Flags);
bool verifyRangeType(uint32_t Type);
bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
uint32_t FlagsVal);
bool verifySamplerFilter(uint32_t Value);
bool verifyAddress(uint32_t Address);
bool verifyMipLODBias(float MipLODBias);
bool verifyMaxAnisotropy(uint32_t MaxAnisotropy);
bool verifyComparisonFunc(uint32_t ComparisonFunc);
bool verifyBorderColor(uint32_t BorderColor);
bool verifyLOD(float LOD);

struct RangeInfo {
const static uint32_t Unbounded = ~0u;

Expand Down
143 changes: 143 additions & 0 deletions llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,153 @@

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

#include <cmath>

namespace llvm {
namespace hlsl {
namespace rootsig {

bool verifyRootFlag(uint32_t Flags) { return (Flags & ~0xfff) == 0; }

bool verifyVersion(uint32_t Version) { return (Version == 1 || Version == 2); }

bool verifyRegisterValue(uint32_t RegisterValue) {
return RegisterValue != ~0U;
}

// This Range is reserverved, therefore invalid, according to the spec
// https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#all-the-values-should-be-legal
bool verifyRegisterSpace(uint32_t RegisterSpace) {
return !(RegisterSpace >= 0xFFFFFFF0 && RegisterSpace <= 0xFFFFFFFF);
}

bool verifyDescriptorFlag(uint32_t Flags) { return (Flags & ~0xE) == 0; }

bool verifyRangeType(uint32_t Type) {
switch (Type) {
case llvm::to_underlying(dxbc::DescriptorRangeType::CBV):
case llvm::to_underlying(dxbc::DescriptorRangeType::SRV):
case llvm::to_underlying(dxbc::DescriptorRangeType::UAV):
case llvm::to_underlying(dxbc::DescriptorRangeType::Sampler):
return true;
};

return false;
}

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

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

if (Version == 1) {
// Since the metadata is unversioned, we expect to explicitly see the values
// that map to the version 1 behaviour here.
if (IsSampler)
return Flags == FlagT::DescriptorsVolatile;
return Flags == (FlagT::DataVolatile | FlagT::DescriptorsVolatile);
}

// The data-specific flags are mutually exclusive.
FlagT DataFlags = FlagT::DataVolatile | FlagT::DataStatic |
FlagT::DataStaticWhileSetAtExecute;

if (popcount(llvm::to_underlying(Flags & DataFlags)) > 1)
return false;

// The descriptor-specific flags are mutually exclusive.
FlagT DescriptorFlags = FlagT::DescriptorsStaticKeepingBufferBoundsChecks |
FlagT::DescriptorsVolatile;
if (popcount(llvm::to_underlying(Flags & DescriptorFlags)) > 1)
return false;

// For volatile descriptors, DATA_is never valid.
if ((Flags & FlagT::DescriptorsVolatile) == FlagT::DescriptorsVolatile) {
FlagT Mask = FlagT::DescriptorsVolatile;
if (!IsSampler) {
Mask |= FlagT::DataVolatile;
Mask |= FlagT::DataStaticWhileSetAtExecute;
}
return (Flags & ~Mask) == FlagT::None;
}

// For "KEEPING_BUFFER_BOUNDS_CHECKS" descriptors,
// the other data-specific flags may all be set.
if ((Flags & FlagT::DescriptorsStaticKeepingBufferBoundsChecks) ==
FlagT::DescriptorsStaticKeepingBufferBoundsChecks) {
FlagT Mask = FlagT::DescriptorsStaticKeepingBufferBoundsChecks;
if (!IsSampler) {
Mask |= FlagT::DataVolatile;
Mask |= FlagT::DataStatic;
Mask |= FlagT::DataStaticWhileSetAtExecute;
}
return (Flags & ~Mask) == FlagT::None;
}

// When no descriptor flag is set, any data flag is allowed.
FlagT Mask = FlagT::None;
if (!IsSampler) {
Mask |= FlagT::DataVolatile;
Mask |= FlagT::DataStaticWhileSetAtExecute;
Mask |= FlagT::DataStatic;
}
return (Flags & ~Mask) == FlagT::None;
}

bool verifySamplerFilter(uint32_t Value) {
switch (Value) {
#define FILTER(Num, Val) case llvm::to_underlying(dxbc::SamplerFilter::Val):
#include "llvm/BinaryFormat/DXContainerConstants.def"
return true;
}
return false;
}

// Values allowed here:
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_texture_address_mode#syntax
bool verifyAddress(uint32_t Address) {
switch (Address) {
#define TEXTURE_ADDRESS_MODE(Num, Val) \
case llvm::to_underlying(dxbc::TextureAddressMode::Val):
#include "llvm/BinaryFormat/DXContainerConstants.def"
return true;
}
return false;
}

bool verifyMipLODBias(float MipLODBias) {
return MipLODBias >= -16.f && MipLODBias <= 15.99f;
}

bool verifyMaxAnisotropy(uint32_t MaxAnisotropy) {
return MaxAnisotropy <= 16u;
}

bool verifyComparisonFunc(uint32_t ComparisonFunc) {
switch (ComparisonFunc) {
#define COMPARISON_FUNC(Num, Val) \
case llvm::to_underlying(dxbc::ComparisonFunc::Val):
#include "llvm/BinaryFormat/DXContainerConstants.def"
return true;
}
return false;
}

bool verifyBorderColor(uint32_t BorderColor) {
switch (BorderColor) {
#define STATIC_BORDER_COLOR(Num, Val) \
case llvm::to_underlying(dxbc::StaticBorderColor::Val):
#include "llvm/BinaryFormat/DXContainerConstants.def"
return true;
}
return false;
}

bool verifyLOD(float LOD) { return !std::isnan(LOD); }

std::optional<const RangeInfo *>
ResourceRange::getOverlapping(const RangeInfo &Info) const {
MapT::const_iterator Interval = Intervals.find(Info.LowerBound);
Expand Down
Loading
Loading