-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[DirectX] Add Range Overlap validation #152229
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
Changes from 4 commits
c95ce68
af6aa53
29eb893
ed4c553
28fb609
403972d
0f0435d
e841a98
ae6d67a
41f32bd
6f3d019
971ad57
db73d71
3b04c2d
1ddffc3
db0008e
b4e5fb4
4a655a5
cc94561
98f48d2
eb334b8
06c0da4
d58606f
74980c8
d376abf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a file header. |
||
#include "llvm/Support/DXILABI.h" | ||
#include "llvm/Support/ErrorHandling.h" | ||
|
||
|
||
using namespace llvm; | ||
namespace llvm { | ||
namespace dxil { | ||
StringRef getResourceClassName(dxil::ResourceClass RC) { | ||
|
||
switch (RC) { | ||
case dxil::ResourceClass::SRV: | ||
return "SRV"; | ||
case dxil::ResourceClass::UAV: | ||
return "UAV"; | ||
case dxil::ResourceClass::CBuffer: | ||
return "CBuffer"; | ||
case dxil::ResourceClass::Sampler: | ||
return "Sampler"; | ||
} | ||
llvm_unreachable("Unhandled ResourceClass"); | ||
} | ||
} // namespace dxil | ||
} // namespace llvm | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -18,16 +18,16 @@ | |||||||
#include "llvm/IR/IntrinsicsDirectX.h" | ||||||||
#include "llvm/IR/Module.h" | ||||||||
#include "llvm/InitializePasses.h" | ||||||||
#include "llvm/Support/DXILABI.h" | ||||||||
|
||||||||
#define DEBUG_TYPE "dxil-post-optimization-validation" | ||||||||
|
||||||||
using namespace llvm; | ||||||||
using namespace llvm::dxil; | ||||||||
|
||||||||
namespace { | ||||||||
static ResourceClass RangeToResourceClass(uint32_t RangeType) { | ||||||||
static ResourceClass toResourceClass(dxbc::DescriptorRangeType RangeType) { | ||||||||
using namespace dxbc; | ||||||||
switch (static_cast<DescriptorRangeType>(RangeType)) { | ||||||||
switch (RangeType) { | ||||||||
case DescriptorRangeType::SRV: | ||||||||
return ResourceClass::SRV; | ||||||||
case DescriptorRangeType::UAV: | ||||||||
|
@@ -39,20 +39,21 @@ static ResourceClass RangeToResourceClass(uint32_t RangeType) { | |||||||
} | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need an |
||||||||
|
||||||||
ResourceClass ParameterToResourceClass(uint32_t Type) { | ||||||||
static ResourceClass toResourceClass(dxbc::RootParameterType Type) { | ||||||||
using namespace dxbc; | ||||||||
switch (Type) { | ||||||||
case llvm::to_underlying(RootParameterType::Constants32Bit): | ||||||||
case RootParameterType::Constants32Bit: | ||||||||
return ResourceClass::CBuffer; | ||||||||
case llvm::to_underlying(RootParameterType::SRV): | ||||||||
case RootParameterType::SRV: | ||||||||
return ResourceClass::SRV; | ||||||||
case llvm::to_underlying(RootParameterType::UAV): | ||||||||
case RootParameterType::UAV: | ||||||||
return ResourceClass::UAV; | ||||||||
case llvm::to_underlying(RootParameterType::CBV): | ||||||||
case RootParameterType::CBV: | ||||||||
return ResourceClass::CBuffer; | ||||||||
default: | ||||||||
llvm_unreachable("Unknown RootParameterType"); | ||||||||
case dxbc::RootParameterType::DescriptorTable: | ||||||||
inbelic marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
break; | ||||||||
|
||||||||
} | ||||||||
llvm_unreachable("Unconvertible RootParameterType"); | ||||||||
|
||||||||
} | ||||||||
|
||||||||
static void reportInvalidDirection(Module &M, DXILResourceMap &DRM) { | ||||||||
|
@@ -131,12 +132,6 @@ static void reportOverlappingRegisters( | |||||||
|
||||||||
static dxbc::ShaderVisibility | ||||||||
tripleToVisibility(llvm::Triple::EnvironmentType ET) { | ||||||||
assert((ET == Triple::Pixel || ET == Triple::Vertex || | ||||||||
ET == Triple::Geometry || ET == Triple::Hull || | ||||||||
ET == Triple::Domain || ET == Triple::Mesh || | ||||||||
ET == Triple::Compute) && | ||||||||
"Invalid Triple to shader stage conversion"); | ||||||||
|
||||||||
switch (ET) { | ||||||||
case Triple::Pixel: | ||||||||
return dxbc::ShaderVisibility::Pixel; | ||||||||
|
@@ -157,73 +152,80 @@ tripleToVisibility(llvm::Triple::EnvironmentType ET) { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
static void trackRootSigDescBinding(hlsl::BindingInfoBuilder &Builder, | ||||||||
const mcdxbc::RootSignatureDesc &RSD, | ||||||||
dxbc::ShaderVisibility Visibility) { | ||||||||
for (size_t I = 0; I < RSD.ParametersContainer.size(); I++) { | ||||||||
const auto &[Type, Loc] = | ||||||||
RSD.ParametersContainer.getTypeAndLocForParameter(I); | ||||||||
|
||||||||
const auto &Header = RSD.ParametersContainer.getHeader(I); | ||||||||
if (Header.ShaderVisibility != | ||||||||
llvm::to_underlying(dxbc::ShaderVisibility::All) && | ||||||||
Header.ShaderVisibility != llvm::to_underlying(Visibility)) | ||||||||
continue; | ||||||||
static void validateRootSignature(Module &M, | ||||||||
const mcdxbc::RootSignatureDesc &RSD, | ||||||||
dxil::ModuleMetadataInfo &MMI) { | ||||||||
|
||||||||
switch (Type) { | ||||||||
case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): { | ||||||||
hlsl::BindingInfoBuilder Builder; | ||||||||
dxbc::ShaderVisibility Visibility = tripleToVisibility(MMI.ShaderProfile); | ||||||||
|
||||||||
for (const mcdxbc::RootParameterInfo &ParamInfo : RSD.ParametersContainer) { | ||||||||
dxbc::ShaderVisibility ParamVisibility = | ||||||||
static_cast<dxbc::ShaderVisibility>(ParamInfo.Header.ShaderVisibility); | ||||||||
if (ParamVisibility != dxbc::ShaderVisibility::All && | ||||||||
ParamVisibility != Visibility) | ||||||||
continue; | ||||||||
dxbc::RootParameterType ParamType = | ||||||||
static_cast<dxbc::RootParameterType>(ParamInfo.Header.ParameterType); | ||||||||
switch (ParamType) { | ||||||||
case dxbc::RootParameterType::Constants32Bit: { | ||||||||
dxbc::RTS0::v1::RootConstants Const = | ||||||||
RSD.ParametersContainer.getConstant(Loc); | ||||||||
RSD.ParametersContainer.getConstant(ParamInfo.Location); | ||||||||
Builder.trackBinding(dxil::ResourceClass::CBuffer, Const.RegisterSpace, | ||||||||
Const.ShaderRegister, | ||||||||
Const.ShaderRegister + Const.Num32BitValues, &Const); | ||||||||
Const.ShaderRegister, Const.ShaderRegister, nullptr); | ||||||||
break; | ||||||||
} | ||||||||
|
||||||||
case llvm::to_underlying(dxbc::RootParameterType::SRV): | ||||||||
case llvm::to_underlying(dxbc::RootParameterType::UAV): | ||||||||
case llvm::to_underlying(dxbc::RootParameterType::CBV): { | ||||||||
case dxbc::RootParameterType::SRV: | ||||||||
case dxbc::RootParameterType::UAV: | ||||||||
case dxbc::RootParameterType::CBV: { | ||||||||
dxbc::RTS0::v2::RootDescriptor Desc = | ||||||||
RSD.ParametersContainer.getRootDescriptor(Loc); | ||||||||
Builder.trackBinding(ParameterToResourceClass(Type), Desc.RegisterSpace, | ||||||||
Desc.ShaderRegister, Desc.ShaderRegister, &Desc); | ||||||||
RSD.ParametersContainer.getRootDescriptor(ParamInfo.Location); | ||||||||
Builder.trackBinding(toResourceClass(static_cast<dxbc::RootParameterType>( | ||||||||
ParamInfo.Header.ParameterType)), | ||||||||
Desc.RegisterSpace, Desc.ShaderRegister, | ||||||||
Desc.ShaderRegister, nullptr); | ||||||||
|
||||||||
break; | ||||||||
} | ||||||||
case llvm::to_underlying(dxbc::RootParameterType::DescriptorTable): { | ||||||||
case dxbc::RootParameterType::DescriptorTable: { | ||||||||
const mcdxbc::DescriptorTable &Table = | ||||||||
RSD.ParametersContainer.getDescriptorTable(Loc); | ||||||||
RSD.ParametersContainer.getDescriptorTable(ParamInfo.Location); | ||||||||
|
||||||||
for (const dxbc::RTS0::v2::DescriptorRange &Range : Table.Ranges) { | ||||||||
Builder.trackBinding(RangeToResourceClass(Range.RangeType), | ||||||||
Range.RegisterSpace, Range.BaseShaderRegister, | ||||||||
Range.NumDescriptors == ~0U | ||||||||
? Range.NumDescriptors | ||||||||
: Range.BaseShaderRegister + | ||||||||
Range.NumDescriptors, | ||||||||
&Range); | ||||||||
uint32_t UpperBound = | ||||||||
Range.NumDescriptors == ~0U | ||||||||
? Range.BaseShaderRegister | ||||||||
: Range.BaseShaderRegister + Range.NumDescriptors - 1; | ||||||||
Builder.trackBinding( | ||||||||
toResourceClass( | ||||||||
static_cast<dxbc::DescriptorRangeType>(Range.RangeType)), | ||||||||
Range.RegisterSpace, Range.BaseShaderRegister, UpperBound, nullptr); | ||||||||
} | ||||||||
break; | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
for (auto &S : RSD.StaticSamplers) { | ||||||||
for (const dxbc::RTS0::v1::StaticSampler &S : RSD.StaticSamplers) | ||||||||
Builder.trackBinding(dxil::ResourceClass::Sampler, S.RegisterSpace, | ||||||||
S.ShaderRegister, S.ShaderRegister, &S); | ||||||||
} | ||||||||
S.ShaderRegister, S.ShaderRegister, nullptr); | ||||||||
inbelic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
|
||||||||
hlsl::BindingInfo Info = Builder.calculateBindingInfo( | ||||||||
inbelic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
[&M](const llvm::hlsl::BindingInfoBuilder &Builder, | ||||||||
const llvm::hlsl::BindingInfoBuilder::Binding &ReportedBinding) { | ||||||||
const llvm::hlsl::BindingInfoBuilder::Binding &Overlaping = | ||||||||
Builder.findOverlapping(ReportedBinding); | ||||||||
reportOverlappingRegisters(M, ReportedBinding, Overlaping); | ||||||||
}); | ||||||||
} | ||||||||
|
||||||||
std::optional<mcdxbc::RootSignatureDesc> | ||||||||
static mcdxbc::RootSignatureDesc * | ||||||||
getRootSignature(RootSignatureBindingInfo &RSBI, | ||||||||
dxil::ModuleMetadataInfo &MMI) { | ||||||||
if (MMI.EntryPropertyVec.size() == 0) | ||||||||
return std::nullopt; | ||||||||
std::optional<mcdxbc::RootSignatureDesc> RootSigDesc = | ||||||||
RSBI.getDescForFunction(MMI.EntryPropertyVec[0].Entry); | ||||||||
if (!RootSigDesc) | ||||||||
return std::nullopt; | ||||||||
return RootSigDesc; | ||||||||
return nullptr; | ||||||||
return RSBI.getDescForFunction(MMI.EntryPropertyVec[0].Entry); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Is that true? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, there are a lot of tests that compile without an entry point, mainly scenarios for libraries. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But if it isn't 0, is it then always 1? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, HLSL can have multiple entry point, for multiple shader stages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, what makes it always be the first one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For non library shader compilation, only one entry point is expected. If we have more than one entry point, we are compiling a library shader. Library don't compile root signature to dxcontainer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really need to clean up the places that make this assumption and grab the first entry of the entry point vector explicitly, either by wrapping it with an API with an explicit name or by more explicitly handling library profiles. That said, this is a pretty ubiquitous problem already I don't think we need to fix it in this PR. |
||||||||
} | ||||||||
|
||||||||
static void reportErrors(Module &M, DXILResourceMap &DRM, | ||||||||
|
@@ -239,21 +241,9 @@ static void reportErrors(Module &M, DXILResourceMap &DRM, | |||||||
assert(!DRBI.hasImplicitBinding() && "implicit bindings should be handled in " | ||||||||
"DXILResourceImplicitBinding pass"); | ||||||||
|
||||||||
if (auto RSD = getRootSignature(RSBI, MMI)) { | ||||||||
|
||||||||
hlsl::BindingInfoBuilder Builder; | ||||||||
dxbc::ShaderVisibility Visibility = tripleToVisibility(MMI.ShaderProfile); | ||||||||
trackRootSigDescBinding(Builder, *RSD, Visibility); | ||||||||
hlsl::BindingInfo Info = Builder.calculateBindingInfo( | ||||||||
[&M](const llvm::hlsl::BindingInfoBuilder &Builder, | ||||||||
const llvm::hlsl::BindingInfoBuilder::Binding &ReportedBinding) { | ||||||||
const llvm::hlsl::BindingInfoBuilder::Binding &Overlaping = | ||||||||
Builder.findOverlapping(ReportedBinding); | ||||||||
reportOverlappingRegisters(M, ReportedBinding, Overlaping); | ||||||||
}); | ||||||||
} | ||||||||
if (mcdxbc::RootSignatureDesc *RSD = getRootSignature(RSBI, MMI)) | ||||||||
validateRootSignature(M, *RSD, MMI); | ||||||||
} | ||||||||
} // namespace | ||||||||
|
||||||||
PreservedAnalyses | ||||||||
DXILPostOptimizationValidation::run(Module &M, ModuleAnalysisManager &MAM) { | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to use the recently landed function defined here in tangent with
enumToStringRef
: #152213There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check
llvm/lib/Support/DXILABI.cpp
,getResourceClassName
callsenumToStringRef
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, I think we could probably just do that inline? Then we don't need to define
DXILABI.cpp
at all, since this file is defined to just contain "definitions of constants and enums" to be stable in the DXILThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check #152229 (comment), bogner explains why we created
DXILABI.cpp
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, in that case I think we need to move the
getResourceClasses
from here toDXILABI
. Otherwise, we have a circular dependency betweenDXILABI
andBinaryFormat/DXContainer.h
.Either that or inline this function where it is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The circular dependency shouldn't be an issue, since we use the
#ifndef
in all files.I will fix this, but in a follow-up PR, since this change is already out of scope for this patch.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the PR fixing it: #153490