-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[DirectX] Validating Root flags are denying shader stage #153287
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
joaosaffran
merged 30 commits into
llvm:users/joaosaffran/153276
from
joaosaffran:validation/root-flags
Sep 26, 2025
Merged
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b1e34ff
adding validaiton and tests
90c2578
fix?
6e20bdf
Merge branch 'users/joaosaffran/153276' into validation/root-flags
joaosaffran 84a4c4b
format
4400e2e
format
eb425c5
making validation function according to spec
ffcff83
merge
51ff280
format
e5812ce
clean up
d186ebd
clean up
5c35c32
clean up
9c09f21
Merge branch 'validation/descriptor-tables' into validation/root-flags
joaosaffran 9169be0
formatting
joaosaffran 65089ce
clean up
joaosaffran 5910271
clean up
joaosaffran 311a2e5
clean up
joaosaffran 901bd1d
clean up
joaosaffran aba77f9
fix test
joaosaffran 22319f9
rename test
joaosaffran 4c86232
rename test
joaosaffran 6641d66
rename test
joaosaffran c9986ed
refactoring follow inbelic suggestion
joaosaffran a0916e1
formating
joaosaffran 1f8e5b6
adding getEnvironmentDenyFlagMask
joaosaffran 1c2a864
format
joaosaffran 8cccaf3
making getEnvironmentDenyFlagMask return optional
joaosaffran b3bc8b8
renaming SRVorUAV flag
joaosaffran 18f9e9c
Merge branch 'validation/descriptor-tables' into validation/root-flags
joaosaffran 83ee5fe
addressing comments from bogner
joaosaffran fdcd3e3
removing consts
joaosaffran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,41 @@ tripleToVisibility(llvm::Triple::EnvironmentType ET) { | |
} | ||
} | ||
|
||
static void reportIfDeniedShaderStageAccess(Module &M, | ||
const dxbc::RootFlags &Flags, | ||
const dxbc::RootFlags &Mask) { | ||
if ((Flags & Mask) != Mask) | ||
return; | ||
|
||
SmallString<128> Message; | ||
raw_svector_ostream OS(Message); | ||
OS << "Shader has root bindings but root signature uses a DENY flag to " | ||
"disallow root binding access to the shader stage."; | ||
M.getContext().diagnose(DiagnosticInfoGeneric(Message)); | ||
} | ||
|
||
static std::optional<dxbc::RootFlags> | ||
getEnvironmentDenyFlagMask(Triple::EnvironmentType ShaderProfile) { | ||
switch (ShaderProfile) { | ||
case Triple::Pixel: | ||
return dxbc::RootFlags::DenyPixelShaderRootAccess; | ||
case Triple::Vertex: | ||
return dxbc::RootFlags::DenyVertexShaderRootAccess; | ||
case Triple::Geometry: | ||
return dxbc::RootFlags::DenyGeometryShaderRootAccess; | ||
case Triple::Hull: | ||
return dxbc::RootFlags::DenyHullShaderRootAccess; | ||
case Triple::Domain: | ||
return dxbc::RootFlags::DenyDomainShaderRootAccess; | ||
case Triple::Mesh: | ||
return dxbc::RootFlags::DenyMeshShaderRootAccess; | ||
case Triple::Amplification: | ||
return dxbc::RootFlags::DenyAmplificationShaderRootAccess; | ||
default: | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
static void validateRootSignature(Module &M, | ||
const mcdxbc::RootSignatureDesc &RSD, | ||
dxil::ModuleMetadataInfo &MMI, | ||
|
@@ -225,7 +260,9 @@ static void validateRootSignature(Module &M, | |
Builder.findOverlapping(ReportedBinding); | ||
reportOverlappingRegisters(M, ReportedBinding, Overlaping); | ||
}); | ||
|
||
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. That is intentional, I think it improves readability. |
||
const hlsl::BoundRegs &BoundRegs = Builder.takeBoundRegs(); | ||
bool HasBindings = false; | ||
inbelic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (const ResourceInfo &RI : DRM) { | ||
const ResourceInfo::ResourceBinding &Binding = RI.getBinding(); | ||
const dxil::ResourceTypeInfo &RTI = DRTM[RI.getHandleTy()]; | ||
|
@@ -236,22 +273,34 @@ static void validateRootSignature(Module &M, | |
BoundRegs.findBoundReg(RC, Binding.Space, Binding.LowerBound, | ||
Binding.LowerBound + Binding.Size - 1); | ||
|
||
if (Reg != nullptr) { | ||
const auto *ParamInfo = | ||
static_cast<const mcdxbc::RootParameterInfo *>(Reg->Cookie); | ||
|
||
if (RC != ResourceClass::SRV && RC != ResourceClass::UAV) | ||
continue; | ||
|
||
if (ParamInfo->Type == dxbc::RootParameterType::DescriptorTable) | ||
continue; | ||
|
||
if (RK != ResourceKind::RawBuffer && RK != ResourceKind::StructuredBuffer) | ||
reportInvalidHandleTyError(M, RC, Binding); | ||
} else { | ||
if (!Reg) { | ||
reportRegNotBound(M, RC, Binding); | ||
continue; | ||
} | ||
|
||
const auto *ParamInfo = | ||
static_cast<const mcdxbc::RootParameterInfo *>(Reg->Cookie); | ||
|
||
const bool IsSRVOrUAV = | ||
RC == ResourceClass::SRV || RC == ResourceClass::UAV; | ||
const bool IsDescriptorTable = | ||
ParamInfo->Type == dxbc::RootParameterType::DescriptorTable; | ||
const bool IsRawOrStructuredBuffer = | ||
RK != ResourceKind::RawBuffer && RK != ResourceKind::StructuredBuffer; | ||
if (IsSRVOrUAV && !IsDescriptorTable && IsRawOrStructuredBuffer) { | ||
reportInvalidHandleTyError(M, RC, Binding); | ||
continue; | ||
} | ||
|
||
HasBindings = true; | ||
} | ||
|
||
if (!HasBindings) | ||
return; | ||
|
||
if (std::optional<dxbc::RootFlags> Mask = | ||
getEnvironmentDenyFlagMask(MMI.ShaderProfile)) | ||
reportIfDeniedShaderStageAccess(M, dxbc::RootFlags(RSD.Flags), *Mask); | ||
} | ||
|
||
static mcdxbc::RootSignatureDesc * | ||
|
17 changes: 17 additions & 0 deletions
17
llvm/test/CodeGen/DirectX/rootsignature-validation-deny-no-binding.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
; RUN: opt -S -passes='dxil-post-optimization-validation' %s | ||
; This is a valid case where no resource is being used | ||
target triple = "dxil-pc-shadermodel6.6-pixel" | ||
|
||
define void @CSMain() #0 { | ||
entry: | ||
ret void | ||
} | ||
attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="geometry" } | ||
|
||
!dx.rootsignatures = !{!0} | ||
|
||
!0 = !{ptr @CSMain, !1, i32 2} | ||
!1 = !{!2, !3, !4} | ||
!2 = !{!"RootConstants", i32 0, i32 2, i32 0, i32 4} | ||
!3 = !{ !"RootFlags", i32 294 } ; 294 = deny_pixel/hull/vertex/amplification_shader_root_access | ||
!4 = !{ !"RootSRV", i32 0, i32 1, i32 0, i32 0 } |
20 changes: 20 additions & 0 deletions
20
llvm/test/CodeGen/DirectX/rootsignature-validation-fail-deny-multiple-shader.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
; RUN: not opt -S -passes='dxil-post-optimization-validation' %s 2>&1 | FileCheck %s | ||
inbelic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
; CHECK: error: Shader has root bindings but root signature uses a DENY flag to disallow root binding access to the shader stage. | ||
target triple = "dxil-pc-shadermodel6.6-pixel" | ||
|
||
%__cblayout_CB = type <{ float }> | ||
|
||
@CB.str = private unnamed_addr constant [3 x i8] c"CB\00", align 1 | ||
|
||
define void @CSMain() "hlsl.shader"="compute" { | ||
entry: | ||
%CB = tail call target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 4, 0)) @llvm.dx.resource.handlefrombinding(i32 0, i32 2, i32 1, i32 0, ptr nonnull @CB.str) | ||
ret void | ||
} | ||
|
||
!dx.rootsignatures = !{!0} | ||
|
||
!0 = !{ptr @CSMain, !1, i32 2} | ||
!1 = !{!2, !3} | ||
!2 = !{!"RootConstants", i32 0, i32 2, i32 0, i32 4} | ||
!3 = !{!"RootFlags", i32 294} ; 294 = deny_pixel/hull/vertex/amplification_shader_root_access |
20 changes: 20 additions & 0 deletions
20
llvm/test/CodeGen/DirectX/rootsignature-validation-fail-deny-root-descriptor.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
; RUN: not opt -S -passes='dxil-post-optimization-validation' %s 2>&1 | FileCheck %s | ||
|
||
; CHECK: error: Shader has root bindings but root signature uses a DENY flag to disallow root binding access to the shader stage. | ||
target triple = "dxil-pc-shadermodel6.6-pixel" | ||
|
||
@SB.str = private unnamed_addr constant [3 x i8] c"SB\00", align 1 | ||
|
||
define void @CSMain() "hlsl.shader"="pixel" { | ||
entry: | ||
%SB = tail call target("dx.RawBuffer", i32, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr nonnull @SB.str) | ||
ret void | ||
} | ||
|
||
!dx.rootsignatures = !{!0} | ||
|
||
!0 = !{ptr @CSMain, !1, i32 2} | ||
!1 = !{!2, !3} | ||
!2 = !{!"DescriptorTable", i32 0, !4} | ||
!4 = !{!"SRV", i32 1, i32 0, i32 0, i32 -1, i32 4} | ||
!3 = !{!"RootFlags", i32 32} ; 32 = deny_pixel_shader_root_access |
19 changes: 19 additions & 0 deletions
19
llvm/test/CodeGen/DirectX/rootsignature-validation-fail-deny-single-shader.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
; RUN: not opt -S -passes='dxil-post-optimization-validation' %s 2>&1 | FileCheck %s | ||
|
||
; CHECK: error: Shader has root bindings but root signature uses a DENY flag to disallow root binding access to the shader stage. | ||
target triple = "dxil-pc-shadermodel6.6-pixel" | ||
|
||
@SB.str = private unnamed_addr constant [3 x i8] c"SB\00", align 1 | ||
|
||
define void @CSMain() "hlsl.shader"="pixel" { | ||
entry: | ||
%SB = tail call target("dx.RawBuffer", i32, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, ptr nonnull @SB.str) | ||
ret void | ||
} | ||
|
||
!dx.rootsignatures = !{!0} | ||
|
||
!0 = !{ptr @CSMain, !1, i32 2} | ||
!1 = !{!2, !3} | ||
!2 = !{!"RootSRV", i32 0, i32 0, i32 0, i32 4} | ||
!3 = !{!"RootFlags", i32 32} ; 32 = deny_pixel_shader_root_access |
21 changes: 21 additions & 0 deletions
21
llvm/test/CodeGen/DirectX/rootsignature-validation-not-dening-shader.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
; RUN: opt -S -passes='dxil-post-optimization-validation' %s | ||
; Valid scenario where shader stage is not blocked from accessing root bindings | ||
target triple = "dxil-pc-shadermodel6.6-geometry" | ||
|
||
%__cblayout_CB = type <{ float }> | ||
|
||
@CB.str = private unnamed_addr constant [3 x i8] c"CB\00", align 1 | ||
|
||
define void @CSMain() "hlsl.shader"="geometry" { | ||
entry: | ||
%CB = tail call target("dx.CBuffer", target("dx.Layout", %__cblayout_CB, 4, 0)) @llvm.dx.resource.handlefrombinding(i32 0, i32 2, i32 1, i32 0, ptr nonnull @CB.str) | ||
ret void | ||
} | ||
attributes #0 = { noinline nounwind "exp-shader"="cs" "hlsl.numthreads"="1,2,1" "hlsl.shader"="geometry" } | ||
|
||
!dx.rootsignatures = !{!0} | ||
|
||
!0 = !{ptr @CSMain, !1, i32 2} | ||
!1 = !{!2, !3} | ||
!2 = !{ !"RootFlags", i32 294 } ; 294 = deny_pixel/hull/vertex/amplification_shader_root_access | ||
!3 = !{ !"RootCBV", i32 0, i32 2, i32 0, i32 0 } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 added this check here, because I think it simplifies the code. If this is not here, I need to make the same check in all statements of the switch with slightly different masks.