[llvm] [Transforms] Let amdgcn take advantage of sin(-x) --> -sin(x) (PR #79700)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 29 10:23:23 PST 2024
https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/79700
>From 57a66744b14a06795faac496bb743bc6d8e331c4 Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Sat, 27 Jan 2024 12:54:09 -0500
Subject: [PATCH 1/2] [Transforms] Let amdgcn take advantage of sin(-x) -->
-sin(x)
We do it for amdgcn_cos, and we should do it for amdgcn_sin as well.
---
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index a647be2d26c7613..ce54d49323dc6eb 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2497,11 +2497,12 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
break;
}
- case Intrinsic::sin: {
+ case Intrinsic::sin:
+ case Intrinsic::amdgcn_sin: {
Value *X;
if (match(II->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) {
// sin(-x) --> -sin(x)
- Value *NewSin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, II);
+ Value *NewSin = Builder.CreateUnaryIntrinsic(IID, X, II);
Instruction *FNeg = UnaryOperator::CreateFNeg(NewSin);
FNeg->copyFastMathFlags(II);
return FNeg;
>From a367853bab00159a2d801d7c6c8a3bb98d53665a Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Mon, 29 Jan 2024 13:22:55 -0500
Subject: [PATCH 2/2] ok
---
.../InstCombine/AMDGPU/amdgcn-intrinsics.ll | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
index 804283cc20cd6a3..645e0d9a0505bc7 100644
--- a/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/AMDGPU/amdgcn-intrinsics.ll
@@ -1023,6 +1023,40 @@ define float @cos_fabs_unary_fneg_f32(float %x) {
ret float %cos
}
+
+; --------------------------------------------------------------------
+; llvm.amdgcn.sin
+; --------------------------------------------------------------------
+declare float @llvm.amdgcn.sin.f32(float) nounwind readnone
+
+; CHECK-LABEL: @sin_fneg_f32(
+; CHECK: %cos = call float @llvm.amdgcn.cos.f32(float %x)
+; CHECK-NEXT: ret float %cos
+define float @sin_fneg_f32(float %x) {
+ %x.fneg = fsub float -0.0, %x
+ %sin = call float @llvm.amdgcn.sin.f32(float %x.fneg)
+ ret float %sin
+}
+
+; CHECK-LABEL: @sin_fabs_f32(
+; CHECK-NEXT: %sin = call float @llvm.amdgcn.sin.f32(float %x)
+; CHECK-NEXT: ret float %sin
+define float @sin_fabs_f32(float %x) {
+ %x.fabs = call float @llvm.fabs.f32(float %x)
+ %sin = call float @llvm.amdgcn.sin.f32(float %x.fabs)
+ ret float %sin
+}
+
+; CHECK-LABEL: @sin_fabs_fneg_f32(
+; CHECK-NEXT: %sin = call float @llvm.amdgcn.sin.f32(float %x)
+; CHECK-NEXT: ret float %sin
+define float @sin_fabs_fneg_f32(float %x) {
+ %x.fabs = call float @llvm.fabs.f32(float %x)
+ %x.fabs.fneg = fsub float -0.0, %x.fabs
+ %sin = call float @llvm.amdgcn.sin.f32(float %x.fabs.fneg)
+ ret float %sin
+}
+
; --------------------------------------------------------------------
; llvm.amdgcn.cvt.pkrtz
; --------------------------------------------------------------------
More information about the llvm-commits
mailing list