Skip to content
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

[DirectX] Scalarize the dx.saturate intrinsic #134381

Merged
merged 1 commit into from
Apr 7, 2025

Conversation

bogner
Copy link
Contributor

@bogner bogner commented Apr 4, 2025

The DXIL Saturate op only takes scalars.

Fixes #134378.

The DXIL Saturate op only takes scalars.

Fixes llvm#134378.
@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2025

@llvm/pr-subscribers-backend-directx

Author: Justin Bogner (bogner)

Changes

The DXIL Saturate op only takes scalars.

Fixes #134378.


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

2 Files Affected:

  • (modified) llvm/lib/Target/DirectX/DirectXTargetTransformInfo.cpp (+1)
  • (modified) llvm/test/CodeGen/DirectX/saturate.ll (+31-5)
diff --git a/llvm/lib/Target/DirectX/DirectXTargetTransformInfo.cpp b/llvm/lib/Target/DirectX/DirectXTargetTransformInfo.cpp
index 765e1977041b9..4cf3282b108f1 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetTransformInfo.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetTransformInfo.cpp
@@ -44,6 +44,7 @@ bool DirectXTTIImpl::isTargetIntrinsicTriviallyScalarizable(
   case Intrinsic::dx_firstbituhigh:
   case Intrinsic::dx_frac:
   case Intrinsic::dx_rsqrt:
+  case Intrinsic::dx_saturate:
   case Intrinsic::dx_splitdouble:
   case Intrinsic::dx_wave_readlane:
   case Intrinsic::dx_wave_reduce_max:
diff --git a/llvm/test/CodeGen/DirectX/saturate.ll b/llvm/test/CodeGen/DirectX/saturate.ll
index 0bb1e55421046..1e4a235448a48 100644
--- a/llvm/test/CodeGen/DirectX/saturate.ll
+++ b/llvm/test/CodeGen/DirectX/saturate.ll
@@ -1,4 +1,4 @@
-; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+; RUN: opt -S -scalarizer -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
 ; Make sure the intrinsic dx.saturate is to appropriate DXIL op for half/float/double data types.
 
 ; CHECK-LABEL: test_saturate_half
@@ -28,9 +28,35 @@ entry:
   ret double %hlsl.saturate
 }
 
-; CHECK: attributes #[[#ATTR]] = {{{.*}} memory(none) {{.*}}}
+; CHECK-LABEL: test_saturate_half4
+define noundef <4 x half> @test_saturate_half4(<4 x half> noundef %p0) {
+entry:
+  ; CHECK: call half @dx.op.unary.f16(i32 7, half
+  ; CHECK: call half @dx.op.unary.f16(i32 7, half
+  ; CHECK: call half @dx.op.unary.f16(i32 7, half
+  ; CHECK: call half @dx.op.unary.f16(i32 7, half
+  %hlsl.saturate = call <4 x half> @llvm.dx.saturate.v4f16(<4 x half> %p0)
+  ret <4 x half> %hlsl.saturate
+}
+
+; CHECK-LABEL: test_saturate_float3
+define noundef <3 x float> @test_saturate_float3(<3 x float> noundef %p0) {
+entry:
+  ; CHECK: call float @dx.op.unary.f32(i32 7, float
+  ; CHECK: call float @dx.op.unary.f32(i32 7, float
+  ; CHECK: call float @dx.op.unary.f32(i32 7, float
+  %hlsl.saturate = call <3 x float> @llvm.dx.saturate.v3f32(<3 x float> %p0)
+  ret <3 x float> %hlsl.saturate
+}
 
-declare half @llvm.dx.saturate.f16(half)
-declare float @llvm.dx.saturate.f32(float)
-declare double @llvm.dx.saturate.f64(double)
+; CHECK-LABEL: test_saturate_double2
+define noundef <2 x double> @test_saturate_double2(<2 x double> noundef %p0) {
+entry:
+  ; CHECK: call double @dx.op.unary.f64(i32 7, double
+  ; CHECK: call double @dx.op.unary.f64(i32 7, double
+  %hlsl.saturate = call <2 x double> @llvm.dx.saturate.v4f64(<2 x double> %p0)
+  ret <2 x double> %hlsl.saturate
+}
 
+
+; CHECK: attributes #[[#ATTR]] = {{{.*}} memory(none) {{.*}}}


declare half @llvm.dx.saturate.f16(half)
Copy link
Member

Choose a reason for hiding this comment

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

Not sure why we removed these. Were they causing an error? If you want to make sure they are gone we could do a CHECK-NOT: for these intrinsics?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're just unnecessary noise. The IR parser will add declarations of intrinsics when it sees uses, so rather than list every overload we happen to use in the file we can just let the IR parser create them lazily.

CHECK-NOT wouldn't be appropriate here, since they will correctly be present in the output.

@bogner bogner merged commit 0afa872 into llvm:main Apr 7, 2025
10 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

[DirectX] The dx.saturate intrinsic is not being scalarized
5 participants