Skip to content

[DirectX] Set shader feature flags MinimumPrecision and NativeLowPrecision, and refactor the logic for setting low-precision-related flags #139623

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
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions llvm/include/llvm/BinaryFormat/DXContainerConstants.def
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SHADER_FEATURE_FLAG(14, 19, WaveOps, "Wave level operations")
SHADER_FEATURE_FLAG(15, 20, Int64Ops, "64-Bit integer")
SHADER_FEATURE_FLAG(16, 21, ViewID, "View Instancing")
SHADER_FEATURE_FLAG(17, 22, Barycentrics, "Barycentrics")
SHADER_FEATURE_FLAG(18, -1, NativeLowPrecision, "Use native low precision")
SHADER_FEATURE_FLAG(18, 23, NativeLowPrecision, "Use native low precision")
SHADER_FEATURE_FLAG(19, 24, ShadingRate, "Shading Rate")
SHADER_FEATURE_FLAG(20, 25, Raytracing_Tier_1_1, "Raytracing tier 1.1 features")
SHADER_FEATURE_FLAG(21, 26, SamplerFeedback, "Sampler feedback")
Expand Down Expand Up @@ -117,7 +117,6 @@ DXIL_MODULE_FLAG( 3, ForceEarlyDepthStencil, "Force early depth-stencil test")
DXIL_MODULE_FLAG( 4, EnableRawAndStructuredBuffers, "Raw and structured buffers")
DXIL_MODULE_FLAG( 5, LowPrecisionPresent, "Low-precision data types")
DXIL_MODULE_FLAG( 8, AllResourcesBound, "All resources bound for the duration of shader execution")
DXIL_MODULE_FLAG(23, UseNativeLowPrecision, "Use native low precision")
DXIL_MODULE_FLAG(33, ResMayNotAlias, "Any UAV may not alias any other UAV")

#undef DXIL_MODULE_FLAG
Expand Down
29 changes: 19 additions & 10 deletions llvm/lib/Target/DirectX/DXILShaderFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
}
}

if (CSF.LowPrecisionPresent) {
if (NativeLowPrecisionAvailable)
CSF.NativeLowPrecision = true;
else
CSF.MinimumPrecision = true;
}

if (!CSF.Int64Ops)
CSF.Int64Ops = I.getType()->isIntegerTy(64);

Expand Down Expand Up @@ -206,14 +213,23 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
const ModuleMetadataInfo &MMDI) {

CanSetResMayNotAlias = MMDI.DXILVersion >= VersionTuple(1, 7);

// Check if -res-may-alias was provided on the command line.
// The command line option will set the dx.resmayalias module flag to 1.
// The command line option -res-may-alias will set the dx.resmayalias module
// flag to 1 and disable the ability to set the ResMayNotAlias flag
if (auto *RMA = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("dx.resmayalias")))
if (RMA->getValue() != 0)
CanSetResMayNotAlias = false;

// NativeLowPrecision can only be set when the command line option
// -enable-16bit-types is provided. This is indicated by the dx.nativelowprec
// module flag being set
NativeLowPrecisionAvailable = false;
if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("dx.nativelowprec")))
if (MMDI.ShaderModelVersion >= VersionTuple(6, 2) &&
NativeLowPrec->getValue() != 0)
NativeLowPrecisionAvailable = true;

CallGraph CG(M);

// Compute Shader Flags Mask for all functions using post-order visit of SCC
Expand Down Expand Up @@ -243,13 +259,6 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
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.ShaderModelVersion >= VersionTuple(6, 2) &&
NativeLowPrec->getValue() != 0)
SCCSF.UseNativeLowPrecision = true;

ComputedShaderFlags CSF;
for (const auto &BB : *F)
for (const auto &I : BB)
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/DirectX/DXILShaderFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct ModuleShaderFlags {

private:
bool CanSetResMayNotAlias;
bool NativeLowPrecisionAvailable;
/// Map of Function-Shader Flag Mask pairs representing properties of each of
/// the functions in the module. Shader Flags of each function represent both
/// module-level and function-level flags
Expand Down
20 changes: 17 additions & 3 deletions llvm/test/CodeGen/DirectX/ShaderFlags/low-precision.ll
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
; 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

; Check that when the dx.nativelowprec module flag is not specified, the
; module-level shader flag UseNativeLowPrecision is not set, and the
; MinimumPrecision feature flag is set due to the presence of half and i16
; without native low precision.

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: ; Minimum-precision data types
;CHECK-NEXT: ; Note: extra DXIL module flags:
;CHECK-NEXT: ; Low-precision data types
;CHECK-NEXT: ;
;CHECK-NEXT: ; Shader Flags for Module Functions

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

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

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

; DXC: - Name: SFI0
; DXC-NEXT: Size: 8
; DXC-NEXT: Flags:
; DXC: MinimumPrecision: true
; DXC: NativeLowPrecision: false
; DXC: ...
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
; 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

; Check that when the dx.nativelowprec module flag is set to 0, the module-level
; shader flag UseNativeLowPrecision is not set
; shader flag UseNativeLowPrecision is not set, and the MinimumPrecision feature
; flag is set due to the presence of half and i16 without native low precision.

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: ; Minimum-precision data types
;CHECK-NEXT: ; Note: extra DXIL module flags:
;CHECK-NEXT: ; Low-precision data types
;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) {
define i16 @add_i16(i16 %a, i16 %b) "hlsl.export" {
%sum = add i16 %a, %b
ret i16 %sum
}

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

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

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

; DXC: - Name: SFI0
; DXC-NEXT: Size: 8
; DXC-NEXT: Flags:
; DXC: MinimumPrecision: true
; DXC: NativeLowPrecision: false
; DXC: ...
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
; 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

; Check that when the dx.nativelowprec module flag is set to 1, the module-level
; shader flag UseNativeLowPrecision is set, and the NativeLowPrecision feature
; flag is set

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

;CHECK: ; Combined Shader Flags for Module
;CHECK-NEXT: ; Shader Flags Value: 0x00800020
;CHECK-NEXT: ;
;CHECK-NEXT: ; Note: shader requires additional functionality:
;CHECK-NEXT: ; Use native low precision
;CHECK-NEXT: ; Note: extra DXIL module flags:
;CHECK-NEXT: ; Low-precision data types
;CHECK-NEXT: ; Use native low precision
;CHECK-NEXT: ;
;CHECK-NEXT: ; Shader Flags for Module Functions

;CHECK-LABEL: ; Function add_i16 : 0x00800020
define i16 @add_i16(i16 %a, i16 %b) {
define i16 @add_i16(i16 %a, i16 %b) "hlsl.export" {
%sum = add i16 %a, %b
ret i16 %sum
}

; NOTE: The flag for native low precision (0x80000) is set for every function
; in the module regardless of whether or not the function uses low precision
; data types (flag 0x20). This matches the behavior in DXC
;CHECK-LABEL: ; Function add_i32 : 0x00800000
define i32 @add_i32(i32 %a, i32 %b) {
; NOTE: In DXC, the flag for native low precision (0x80000) is set for every
; function in the module regardless of whether or not the function uses low
; precision data types (flag 0x20). However, this will not be the case for Clang
;CHECK-LABEL: ; Function add_i32 : 0x00000000
define i32 @add_i32(i32 %a, i32 %b) "hlsl.export" {
%sum = add i32 %a, %b
ret i32 %sum
}

;CHECK-LABEL: ; Function add_half : 0x00800020
define half @add_half(half %a, half %b) {
define half @add_half(half %a, half %b) "hlsl.export" {
%sum = fadd half %a, %b
ret half %sum
}

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

; DXC: - Name: SFI0
; DXC-NEXT: Size: 8
; DXC-NEXT: Flags:
; DXC: MinimumPrecision: false
; DXC: NativeLowPrecision: true
; DXC: ...