Skip to content

[DirectX] Implement Shader Flag Analysis for UAVsAtEveryStage #137085

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 8 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions llvm/lib/Target/DirectX/DXILShaderFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,26 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
NumUAVs += UAV.getBinding().Size;
if (NumUAVs > 8)
CombinedSFMask.Max64UAVs = true;

// Set UAVsAtEveryStage flag based on the presence of UAVs, the shader
// model version, and the shader environment
if (!DRM.uavs().empty()) {
if (MMDI.ValidatorVersion < VersionTuple(1, 8))
CombinedSFMask.UAVsAtEveryStage =
MMDI.ShaderProfile != Triple::EnvironmentType::Compute &&
MMDI.ShaderProfile != Triple::EnvironmentType::Pixel;
else // MMDI.ValidatorVersion >= VersionTuple(1, 8)
switch (MMDI.ShaderProfile) {
default:
break;
case Triple::EnvironmentType::Vertex:
case Triple::EnvironmentType::Hull:
case Triple::EnvironmentType::Domain:
case Triple::EnvironmentType::Geometry:
CombinedSFMask.UAVsAtEveryStage = true;
break;
}
}
}

void ComputedShaderFlags::print(raw_ostream &OS) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ define void @test() "hlsl.export" {
ret void
}

; Set dx.valver and dx.resmayalias to prevent flags ResMayNotAlias and
; UAVsAtEveryStage from being set, as to not distract from the flag that is
; actually being tested
!llvm.module.flags = !{!0}
!dx.valver = !{!1}
!0 = !{i32 1, !"dx.resmayalias", i32 1}
!1 = !{i32 1, i32 8}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ define void @test() "hlsl.export" {
ret void
}

; Set dx.valver and dx.resmayalias to prevent flags ResMayNotAlias and
; UAVsAtEveryStage from being set, as to not distract from the flag that is
; actually being tested
!llvm.module.flags = !{!0}
!dx.valver = !{!1}
!0 = !{i32 1, !"dx.resmayalias", i32 1}
!1 = !{i32 1, i32 8}
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ define void @test() "hlsl.export" {
ret void
}

; Set dx.valver and dx.resmayalias to prevent flags ResMayNotAlias and
; UAVsAtEveryStage from being set, as to not distract from the flag that is
; actually being tested
!llvm.module.flags = !{!0}
!dx.valver = !{!1}
!0 = !{i32 1, !"dx.resmayalias", i32 1}
!1 = !{i32 1, i32 8}
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-0.ll
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ define float @loadSRV() #0 {
; But if it does, ensure that it has no effect.
!0 = !{i32 1, !"dx.resmayalias", i32 0}

; Set dx.valver to prevent the flag UAVsAtEveryStage from being set, as to not
; distract from the flag that is actually being tested
!dx.valver = !{!1}
!1 = !{i32 1, i32 8}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
6 changes: 5 additions & 1 deletion llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ define float @loadSRV() #0 {
}

!llvm.module.flags = !{!0}

!0 = !{i32 1, !"dx.resmayalias", i32 1}

; Set dx.valver to prevent the flag UAVsAtEveryStage from being set, as to not
; distract from the flag that is actually being tested
!dx.valver = !{!1}
!1 = !{i32 1, i32 8}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ define float @loadSRV() #0 {
ret float %val
}

; Set dx.valver to prevent the flag UAVsAtEveryStage from being set, as to not
; distract from the flag that is actually being tested
!dx.valver = !{!0}
!0 = !{i32 1, i32 8}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ define float @loadSRV() #0 {
ret float %val
}

; Set dx.valver to prevent the flag UAVsAtEveryStage from being set, as to not
; distract from the flag that is actually being tested
!dx.valver = !{!0}
!0 = !{i32 1, i32 8}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ define float @loadSRV() #0 {
ret float %val
}

; Set dx.valver to prevent the flag UAVsAtEveryStage from being set, as to not
; distract from the flag that is actually being tested
!dx.valver = !{!0}
!0 = !{i32 1, i32 8}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ define void @noload(<4 x float> %val) #0 {
ret void
}

; Set dx.valver and dx.resmayalias to prevent flags ResMayNotAlias and
; UAVsAtEveryStage from being set, as to not distract from the flag that is
; actually being tested
!llvm.module.flags = !{!0}
!dx.valver = !{!1}
!0 = !{i32 1, !"dx.resmayalias", i32 1}
!1 = !{i32 1, i32 8}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC

; This test ensures that a library shader with a UAV gets the module and
; shader feature flag UAVsAtEveryStage when the DXIL validator version is < 1.8

target triple = "dxil-pc-shadermodel6.5-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x00010000

; CHECK: Note: shader requires additional functionality:
; CHECK: UAVs at every shader stage

; CHECK: Function test : 0x00000000
define void @test() "hlsl.export" {
; RWBuffer<float> Buf : register(u0, space0)
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0t(
i32 0, i32 0, i32 1, i32 0, i1 false)
ret void
}

!dx.valver = !{!1}
!1 = !{i32 1, i32 7}

; Set dx.resmayalias to prevent the shader flag ResMayNotAlias from being set,
; as to not distract from the shader flag that is actually being tested
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"dx.resmayalias", i32 1}

; DXC: - Name: SFI0
; DXC-NEXT: Size: 8
; DXC-NEXT: Flags:
; DXC-NOT: {{[A-Za-z]+: +true}}
; DXC: UAVsAtEveryStage: true
; DXC-NOT: {{[A-Za-z]+: +true}}
; DXC: NextUnusedBit: false
; DXC: ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test ensures that a library shader with a UAV does not get the module and
; shader feature flag UAVsAtEveryStage when the DXIL validator version is >= 1.8

target triple = "dxil-pc-shadermodel6.5-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x00000000

; CHECK-NOT: Note: shader requires additional functionality:
; CHECK-NOT: UAVs at every shader stage

; CHECK: Function test : 0x00000000
define void @test() "hlsl.export" {
; RWBuffer<float> Buf : register(u0, space0)
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0t(
i32 0, i32 0, i32 1, i32 0, i1 false)
ret void
}

!dx.valver = !{!1}
!1 = !{i32 1, i32 8}

; Set dx.resmayalias to prevent the shader flag ResMayNotAlias from being set,
; as to not distract from the shader flag that is actually being tested
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"dx.resmayalias", i32 1}
40 changes: 40 additions & 0 deletions llvm/test/CodeGen/DirectX/ShaderFlags/uavs-at-every-stage-vs.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
; TODO: Remove this comment and add 'RUN' to the line below once vertex shaders are supported by llc
; llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC

; This test ensures that a Vertex shader with a UAV gets the module and
; shader feature flag UAVsAtEveryStage

target triple = "dxil-pc-shadermodel6.5-vertex"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x00010000

; CHECK: Note: shader requires additional functionality:
; CHECK: UAVs at every shader stage

; CHECK: Function VSMain : 0x00000000
define void @VSMain() {
; RWBuffer<float> Buf : register(u0, space0)
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0t(
i32 0, i32 0, i32 1, i32 0, i1 false)
ret void
}

!dx.valver = !{!1}
!1 = !{i32 1, i32 8}

; Set dx.resmayalias to prevent the shader flag ResMayNotAlias from being set,
; as to not distract from the shader flag that is actually being tested
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"dx.resmayalias", i32 1}

; DXC: - Name: SFI0
; DXC-NEXT: Size: 8
; DXC-NEXT: Flags:
; DXC-NOT: {{[A-Za-z]+: +true}}
; DXC-NEXT: UAVsAtEveryStage: true
; DXC-NOT: {{[A-Za-z]+: +true}}
; DXC: NextUnusedBit: false
; DXC: ...
Loading