Skip to content

Commit 9ee5dfb

Browse files
Icohedronbogner
authored andcommitted
[DirectX] Implement Shader Flag Analysis for UAVsAtEveryStage (llvm#137085)
Fixes llvm#112272 In addition to the implementation of the UAVsAtEveryStage shader flag analysis, several unrelated tests have had the `dx.valver` module metadata defined to avoid setting the UAVsAtEveryStage shader flag in them. Example: ``` !dx.valver = !{!0} !0 = !{i32 1, i32 8} ``` --------- Co-authored-by: Justin Bogner <[email protected]>
1 parent 77e5d18 commit 9ee5dfb

12 files changed

+159
-20
lines changed

llvm/lib/Target/DirectX/DXILShaderFlags.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@
3232
using namespace llvm;
3333
using namespace llvm::dxil;
3434

35+
static bool hasUAVsAtEveryStage(DXILResourceMap &DRM,
36+
const ModuleMetadataInfo &MMDI) {
37+
if (DRM.uavs().empty())
38+
return false;
39+
40+
switch (MMDI.ShaderProfile) {
41+
default:
42+
return false;
43+
case Triple::EnvironmentType::Compute:
44+
case Triple::EnvironmentType::Pixel:
45+
return false;
46+
case Triple::EnvironmentType::Vertex:
47+
case Triple::EnvironmentType::Geometry:
48+
case Triple::EnvironmentType::Hull:
49+
case Triple::EnvironmentType::Domain:
50+
return true;
51+
case Triple::EnvironmentType::Library:
52+
case Triple::EnvironmentType::RayGeneration:
53+
case Triple::EnvironmentType::Intersection:
54+
case Triple::EnvironmentType::AnyHit:
55+
case Triple::EnvironmentType::ClosestHit:
56+
case Triple::EnvironmentType::Miss:
57+
case Triple::EnvironmentType::Callable:
58+
case Triple::EnvironmentType::Mesh:
59+
case Triple::EnvironmentType::Amplification:
60+
return MMDI.ValidatorVersion < VersionTuple(1, 8);
61+
}
62+
}
63+
3564
static bool checkWaveOps(Intrinsic::ID IID) {
3665
// Currently unsupported intrinsics
3766
// case Intrinsic::dx_wave_getlanecount:
@@ -266,6 +295,8 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
266295
NumUAVs += UAV.getBinding().Size;
267296
if (NumUAVs > 8)
268297
CombinedSFMask.Max64UAVs = true;
298+
299+
CombinedSFMask.UAVsAtEveryStage = hasUAVsAtEveryStage(DRM, MMDI);
269300
}
270301

271302
void ComputedShaderFlags::print(raw_ostream &OS) const {

llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs-array-valver1.5.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
target triple = "dxil-pc-shadermodel6.7-library"
88

99
; CHECK: Combined Shader Flags for Module
10-
; CHECK-NEXT: Shader Flags Value: 0x00000000
10+
; CHECK-NEXT: Shader Flags Value: 0x00010000
1111

12-
; CHECK-NOT: Note: shader requires additional functionality:
12+
; CHECK: Note: shader requires additional functionality:
13+
; CHECK: UAVs at every shader stage
1314
; CHECK-NOT: 64 UAV slots
1415

1516
; CHECK: Function test : 0x00000000
@@ -26,10 +27,8 @@ define void @test() "hlsl.export" {
2627
ret void
2728
}
2829

29-
; Set validator version to 1.5
3030
!dx.valver = !{!1}
3131
!1 = !{i32 1, i32 5}
3232

33-
; Set this flag to 1 to prevent the ResMayNotAlias flag from being set
3433
!llvm.module.flags = !{!0}
3534
!0 = !{i32 1, !"dx.resmayalias", i32 1}

llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs-array-valver1.6.ll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
target triple = "dxil-pc-shadermodel6.7-library"
99

1010
; CHECK: Combined Shader Flags for Module
11-
; CHECK-NEXT: Shader Flags Value: 0x00008000
11+
; CHECK-NEXT: Shader Flags Value: 0x00018000
1212

1313
; CHECK: Note: shader requires additional functionality:
14+
; CHECK: UAVs at every shader stage
1415
; CHECK: 64 UAV slots
1516

1617
; CHECK: Function test : 0x00000000
@@ -27,19 +28,15 @@ define void @test() "hlsl.export" {
2728
ret void
2829
}
2930

30-
; Set validator version to 1.6
3131
!dx.valver = !{!1}
3232
!1 = !{i32 1, i32 6}
3333

34-
; Set this flag to 1 to prevent the ResMayNotAlias flag from being set
3534
!llvm.module.flags = !{!0}
3635
!0 = !{i32 1, !"dx.resmayalias", i32 1}
3736

3837
; DXC: - Name: SFI0
3938
; DXC-NEXT: Size: 8
4039
; DXC-NEXT: Flags:
41-
; DXC-NOT: {{[A-Za-z]+: +true}}
42-
; DXC: Max64UAVs: true
43-
; DXC-NOT: {{[A-Za-z]+: +true}}
40+
; DXC: Max64UAVs: true
4441
; DXC: NextUnusedBit: false
4542
; DXC: ...

llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ define void @test() "hlsl.export" {
5555
}
5656

5757
!llvm.module.flags = !{!0}
58+
!dx.valver = !{!1}
5859
!0 = !{i32 1, !"dx.resmayalias", i32 1}
60+
!1 = !{i32 1, i32 8}
5961

6062
; DXC: - Name: SFI0
6163
; DXC-NEXT: Size: 8
6264
; DXC-NEXT: Flags:
63-
; DXC-NOT: {{[A-Za-z]+: +true}}
64-
; DXC: Max64UAVs: true
65-
; DXC-NOT: {{[A-Za-z]+: +true}}
65+
; DXC: Max64UAVs: true
6666
; DXC: NextUnusedBit: false
6767
; DXC: ...

llvm/test/CodeGen/DirectX/ShaderFlags/res-may-alias-1.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ define float @loadSRV() #0 {
3535
}
3636

3737
!llvm.module.flags = !{!0}
38-
3938
!0 = !{i32 1, !"dx.resmayalias", i32 1}
4039

40+
!dx.valver = !{!1}
41+
!1 = !{i32 1, i32 8}
42+
4143
attributes #0 = { convergent norecurse nounwind "hlsl.export"}

llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-sm6.6.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ define float @loadSRV() #0 {
3434
ret float %val
3535
}
3636

37+
!dx.valver = !{!0}
38+
!0 = !{i32 1, i32 8}
39+
3740
attributes #0 = { convergent norecurse nounwind "hlsl.export"}

llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-sm6.7.ll

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
22

33
; This test checks to ensure the behavior of the DXIL shader flag analysis
4-
; for the flag ResMayNotAlias is correct when the DXIL Version is >= 1.7. The
5-
; ResMayNotAlias flag (0x20000000) should be set on all functions if there are
6-
; one or more UAVs present globally in the module.
4+
; for the flag ResMayNotAlias is correct when the DXIL Version is >= 1.7 and the
5+
; DXIL Validator Version < 1.8. The ResMayNotAlias flag (0x20000000) should be
6+
; set on all functions if there are one or more UAVs present globally in the
7+
; module.
78

89
target triple = "dxil-pc-shadermodel6.7-library"
910

1011
; CHECK: Combined Shader Flags for Module
11-
; CHECK-NEXT: Shader Flags Value: 0x200000010
12+
; CHECK-NEXT: Shader Flags Value: 0x200010010
13+
14+
; CHECK: Note: shader requires additional functionality:
15+
; CHECK: UAVs at every shader stage
1216

1317
; CHECK: Note: extra DXIL module flags:
1418
; CHECK: Raw and structured buffers
@@ -35,4 +39,7 @@ define float @loadSRV() #0 {
3539
ret float %val
3640
}
3741

42+
!dx.valver = !{!0}
43+
!0 = !{i32 1, i32 7}
44+
3845
attributes #0 = { convergent norecurse nounwind "hlsl.export"}

llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-valver1.8.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ define float @loadSRV() #0 {
3535
ret float %val
3636
}
3737

38-
!dx.valver = !{!1}
39-
!1 = !{i32 1, i32 8}
38+
!dx.valver = !{!0}
39+
!0 = !{i32 1, i32 8}
4040

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

llvm/test/CodeGen/DirectX/ShaderFlags/typed-uav-load-additional-formats.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ define void @noload(<4 x float> %val) #0 {
4444
}
4545

4646
!llvm.module.flags = !{!0}
47+
!dx.valver = !{!1}
4748
!0 = !{i32 1, !"dx.resmayalias", i32 1}
49+
!1 = !{i32 1, i32 8}
4850

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
2+
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
3+
4+
; This test ensures that a library shader with a UAV gets the module and
5+
; shader feature flag UAVsAtEveryStage when the DXIL validator version is < 1.8
6+
7+
target triple = "dxil-pc-shadermodel6.5-library"
8+
9+
; CHECK: Combined Shader Flags for Module
10+
; CHECK-NEXT: Shader Flags Value: 0x00010000
11+
12+
; CHECK: Note: shader requires additional functionality:
13+
; CHECK: UAVs at every shader stage
14+
15+
; CHECK: Function test : 0x00000000
16+
define void @test() "hlsl.export" {
17+
; RWBuffer<float> Buf : register(u0, space0)
18+
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)
19+
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0t(
20+
i32 0, i32 0, i32 1, i32 0, i1 false)
21+
ret void
22+
}
23+
24+
!dx.valver = !{!1}
25+
!1 = !{i32 1, i32 7}
26+
27+
!llvm.module.flags = !{!0}
28+
!0 = !{i32 1, !"dx.resmayalias", i32 1}
29+
30+
; DXC: - Name: SFI0
31+
; DXC-NEXT: Size: 8
32+
; DXC-NEXT: Flags:
33+
; DXC: UAVsAtEveryStage: true
34+
; DXC: NextUnusedBit: false
35+
; DXC: ...
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
2+
3+
; This test ensures that a library shader with a UAV does not get the module and
4+
; shader feature flag UAVsAtEveryStage when the DXIL validator version is >= 1.8
5+
6+
target triple = "dxil-pc-shadermodel6.5-library"
7+
8+
; CHECK: Combined Shader Flags for Module
9+
; CHECK-NEXT: Shader Flags Value: 0x00000000
10+
11+
; CHECK-NOT: Note: shader requires additional functionality:
12+
; CHECK-NOT: UAVs at every shader stage
13+
14+
; CHECK: Function test : 0x00000000
15+
define void @test() "hlsl.export" {
16+
; RWBuffer<float> Buf : register(u0, space0)
17+
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)
18+
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0t(
19+
i32 0, i32 0, i32 1, i32 0, i1 false)
20+
ret void
21+
}
22+
23+
!dx.valver = !{!1}
24+
!1 = !{i32 1, i32 8}
25+
26+
!llvm.module.flags = !{!0}
27+
!0 = !{i32 1, !"dx.resmayalias", i32 1}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
2+
; TODO: Remove this comment and add 'RUN' to the line below once vertex shaders are supported by llc
3+
; llc %s --filetype=obj -o - | obj2yaml | FileCheck %s --check-prefix=DXC
4+
5+
; This test ensures that a Vertex shader with a UAV gets the module and
6+
; shader feature flag UAVsAtEveryStage
7+
8+
target triple = "dxil-pc-shadermodel6.5-vertex"
9+
10+
; CHECK: Combined Shader Flags for Module
11+
; CHECK-NEXT: Shader Flags Value: 0x00010000
12+
13+
; CHECK: Note: shader requires additional functionality:
14+
; CHECK: UAVs at every shader stage
15+
16+
; CHECK: Function VSMain : 0x00000000
17+
define void @VSMain() {
18+
; RWBuffer<float> Buf : register(u0, space0)
19+
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)
20+
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_1_0t(
21+
i32 0, i32 0, i32 1, i32 0, i1 false)
22+
ret void
23+
}
24+
25+
!dx.valver = !{!1}
26+
!1 = !{i32 1, i32 8}
27+
28+
!llvm.module.flags = !{!0}
29+
!0 = !{i32 1, !"dx.resmayalias", i32 1}
30+
31+
; DXC: - Name: SFI0
32+
; DXC-NEXT: Size: 8
33+
; DXC-NEXT: Flags:
34+
; DXC: UAVsAtEveryStage: true
35+
; DXC: NextUnusedBit: false
36+
; DXC: ...

0 commit comments

Comments
 (0)