Skip to content

[DirectX] Fix shader flag version-checking logic to match DXC #136787

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 2 commits into from
Apr 25, 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
17 changes: 9 additions & 8 deletions llvm/lib/Target/DirectX/DXILShaderFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
case Intrinsic::dx_resource_handlefrombinding: {
dxil::ResourceTypeInfo &RTI = DRTM[cast<TargetExtType>(II->getType())];

// Set ResMayNotAlias if DXIL version >= 1.8 and function uses UAVs
// Set ResMayNotAlias if DXIL validator version >= 1.8 and the function
// uses UAVs
if (!CSF.ResMayNotAlias && CanSetResMayNotAlias &&
MMDI.DXILVersion >= VersionTuple(1, 8) && RTI.isUAV())
MMDI.ValidatorVersion >= VersionTuple(1, 8) && RTI.isUAV())
CSF.ResMayNotAlias = true;

switch (RTI.getResourceKind()) {
Expand Down Expand Up @@ -208,15 +209,15 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
continue;
}

// Set ResMayNotAlias to true if DXIL version < 1.8 and there are UAVs
// present globally.
if (CanSetResMayNotAlias && MMDI.DXILVersion < VersionTuple(1, 8))
// Set ResMayNotAlias to true if DXIL validator version < 1.8 and there
// are UAVs present globally.
if (CanSetResMayNotAlias && MMDI.ValidatorVersion < VersionTuple(1, 8))
SCCSF.ResMayNotAlias = !DRM.uavs().empty();

// Set UseNativeLowPrecision using dx.nativelowprec module metadata
if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("dx.nativelowprec")))
if (MMDI.DXILVersion >= VersionTuple(1, 2) &&
if (MMDI.ShaderModelVersion >= VersionTuple(6, 2) &&
NativeLowPrec->getValue() != 0)
SCCSF.UseNativeLowPrecision = true;

Expand Down Expand Up @@ -259,9 +260,9 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
// Set the Max64UAVs flag if the number of UAVs is > 8
uint32_t NumUAVs = 0;
for (auto &UAV : DRM.uavs())
if (MMDI.DXILVersion < VersionTuple(1, 6))
if (MMDI.ValidatorVersion < VersionTuple(1, 6))
NumUAVs++;
else // MMDI.DXILVersion >= VersionTuple(1, 6)
else // MMDI.ValidatorVersion >= VersionTuple(1, 6)
NumUAVs += UAV.getBinding().Size;
if (NumUAVs > 8)
CombinedSFMask.Max64UAVs = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test makes sure that resource arrays only add 1 to the count of the
; number of UAVs for setting the shader flag '64 UAV slots' when the shader
; model version is < 6.6
; number of UAVs for setting the shader flag '64 UAV slots' when the validator
; version is < 1.6

; Note: there is no feature flag here (only a module flag), so we don't have an
; object test.

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

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x00000000
Expand All @@ -29,5 +26,10 @@ define void @test() "hlsl.export" {
ret void
}

; Set validator version to 1.5
!dx.valver = !{!1}
!1 = !{i32 1, i32 5}

; Set this flag to 1 to prevent the ResMayNotAlias flag from being set
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"dx.resmayalias", i32 1}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
; 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 makes sure that resource arrays sizes are accounted for when
; counting the number of UAVs for setting the shader flag '64 UAV slots' when
; the shader model version is >= 6.6
; the validator version is >= 1.6

; Note: there is no feature flag here (only a module flag), so we don't have an
; object test.

target triple = "dxil-pc-shadermodel6.6-library"
target triple = "dxil-pc-shadermodel6.7-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x00008000
Expand All @@ -29,5 +27,19 @@ define void @test() "hlsl.export" {
ret void
}

; Set validator version to 1.6
!dx.valver = !{!1}
!1 = !{i32 1, i32 6}

; Set this flag to 1 to prevent the ResMayNotAlias flag from being set
!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: Max64UAVs: true
; DXC-NOT: {{[A-Za-z]+: +true}}
; DXC: NextUnusedBit: false
; DXC: ...
13 changes: 10 additions & 3 deletions llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs.ll
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
; 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 makes sure that the shader flag '64 UAV slots' is set when there are
; more than 8 UAVs in the module.

; Note: there is no feature flag here (only a module flag), so we don't have an
; object test.

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

; CHECK: Combined Shader Flags for Module
Expand Down Expand Up @@ -58,3 +56,12 @@ define void @test() "hlsl.export" {

!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: Max64UAVs: true
; DXC-NOT: {{[A-Za-z]+: +true}}
; DXC: NextUnusedBit: false
; DXC: ...
2 changes: 2 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 @@ -35,9 +35,11 @@ define float @loadSRV() #0 {
}

!llvm.module.flags = !{!0}
!dx.valver = !{!1}

; dx.resmayalias should never appear with a value of 0.
; But if it does, ensure that it has no effect.
!0 = !{i32 1, !"dx.resmayalias", i32 0}
!1 = !{i32 1, i32 8}

attributes #0 = { convergent norecurse nounwind "hlsl.export"}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure the behavior of the DXIL shader flag analysis
; for the flag ResMayNotAlias is correct when the DXIL Version is 1.6. The
; for the flag ResMayNotAlias is correct when the DXIL Version is < 1.7. The
; ResMayNotAlias flag (0x20000000) should not be set at all.

target triple = "dxil-pc-shadermodel6.6-library"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure the behavior of the DXIL shader flag analysis
; for the flag ResMayNotAlias is correct when the DXIL Version is 1.7. The
; for the flag ResMayNotAlias is correct when the DXIL Version is >= 1.7. The
; ResMayNotAlias flag (0x20000000) should be set on all functions if there are
; one or more UAVs present globally in the module.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; This test checks to ensure the behavior of the DXIL shader flag analysis
; for the flag ResMayNotAlias is correct when the DXIL Version is 1.8. The
; ResMayNotAlias flag (0x20000000) should only be set when a function uses a
; This test checks to ensure the behavior of the DXIL shader flag analysis for
; the flag ResMayNotAlias is correct when the DXIL validator version is >= 1.8.
; The ResMayNotAlias flag (0x20000000) should only be set when a function uses a
; UAV.

target triple = "dxil-pc-shadermodel6.8-library"
target triple = "dxil-pc-shadermodel6.7-library"

; CHECK: Combined Shader Flags for Module
; CHECK-NEXT: Shader Flags Value: 0x200000010
Expand Down Expand Up @@ -35,4 +35,7 @@ define float @loadSRV() #0 {
ret float %val
}

!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
@@ -0,0 +1,37 @@
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s

; Check that when the dx.nativelowprec module flag is set to 0, the module-level
; shader flag UseNativeLowPrecision is not set

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

;CHECK: ; Combined Shader Flags for Module
;CHECK-NEXT: ; Shader Flags Value: 0x00000020
;CHECK-NEXT: ;
;CHECK-NEXT: ; Note: shader requires additional functionality:
;CHECK-NEXT: ; Note: extra DXIL module flags:
;CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_ENABLE_MINIMUM_PRECISION
;CHECK-NOT: ; Native 16bit types enabled
;CHECK-NEXT: ;
;CHECK-NEXT: ; Shader Flags for Module Functions

;CHECK-LABEL: ; Function add_i16 : 0x00000020
define i16 @add_i16(i16 %a, i16 %b) {
%sum = add i16 %a, %b
ret i16 %sum
}

;CHECK-LABEL: ; Function add_i32 : 0x00000000
define i32 @add_i32(i32 %a, i32 %b) {
%sum = add i32 %a, %b
ret i32 %sum
}

;CHECK-LABEL: ; Function add_half : 0x00000020
define half @add_half(half %a, half %b) {
%sum = fadd half %a, %b
ret half %sum
}

!llvm.module.flags = !{!0}
!0 = !{i32 1, !"dx.nativelowprec", i32 0}
Loading