Skip to content

[DirectX] Set module-level flag LowPrecisionPresent in DXIL Shader Flags Analysis #129109

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 3 commits into from
Mar 4, 2025

Conversation

Icohedron
Copy link
Contributor

Fixes #114561

@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2025

@llvm/pr-subscribers-backend-directx

Author: Deric Cheung (Icohedron)

Changes

Fixes #114561


Full diff: https://github.com/llvm/llvm-project/pull/129109.diff

2 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DXILShaderFlags.cpp (+16)
  • (added) llvm/test/CodeGen/DirectX/ShaderFlags/low-precision.ll (+38)
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index e1cf516758663..98b8c0a916456 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -99,6 +99,22 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
     }
   }
 
+  auto IsLowPrecisionType = [](const Type *Ty) {
+    return (Ty->isIntegerTy() && Ty->getScalarSizeInBits() == 16) || Ty->isHalfTy();
+  };
+
+  if (!CSF.LowPrecisionPresent)
+    CSF.LowPrecisionPresent = IsLowPrecisionType(I.getType());
+
+  if (!CSF.LowPrecisionPresent) {
+    for (const Value *Op : I.operands()) {
+      if (IsLowPrecisionType(Op->getType())) {
+        CSF.LowPrecisionPresent = true;
+        break;
+      }
+    }
+  }
+
   if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
     switch (II->getIntrinsicID()) {
     default:
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/low-precision.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/low-precision.ll
new file mode 100644
index 0000000000000..fd25a165bfe8d
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/low-precision.ll
@@ -0,0 +1,38 @@
+; 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
+
+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-NEXT: ;
+;CHECK-NEXT: ; Shader Flags for Module Functions
+
+;CHECK-LABEL: ; Function add_i16 : 0x00000020
+define i16 @add_i16(i16 %a, i16 %b) #0 {
+  %sum = add i16 %a, %b
+  ret i16 %sum
+}
+
+;CHECK-LABEL: ; Function add_i32 : 0x00000000
+define i32 @add_i32(i32 %a, i32 %b) #0 {
+  %sum = add i32 %a, %b
+  ret i32 %sum
+}
+
+;CHECK-LABEL: ; Function add_half : 0x00000020
+define half @add_half(half %a, half %b) #0 {
+  %sum = fadd half %a, %b
+  ret half %sum
+}
+
+attributes #0 = { convergent norecurse nounwind "hlsl.export"}
+
+; DXC: - Name:            SFI0
+; DXC-NEXT:     Size:            8
+; DXC-NOT:     Flags:
+; DXC: ...

Copy link
Contributor

@spall spall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM but I don't understand the test enough to comment on if that is sufficient.

@Icohedron Icohedron requested a review from bharadwajy March 3, 2025 18:40
Copy link
Member

@hekota hekota left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Icohedron Icohedron merged commit bbbdb23 into llvm:main Mar 4, 2025
12 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
@damyanp damyanp moved this to Closed in HLSL Support Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

[DirectX] Implement Shader Flags Analysis for LowPrecisionPresent
4 participants