[llvm] ValueTracking: Treat poison more aggressively in computeKnownFPClass (PR #87990)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 15 02:08:06 PDT 2024
https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/87990
>From 8ee0eb22793b90a4a3afaf239699edcb4fb110df Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 4 Apr 2024 15:01:56 -0400
Subject: [PATCH 1/2] ValueTracking: Treat poison more aggressively in
computeKnownFPClass
Assume no valid values, and the sign bit is 0.
---
llvm/lib/Analysis/ValueTracking.cpp | 6 ++
llvm/test/CodeGen/AMDGPU/fold-fabs.ll | 6 --
llvm/unittests/Analysis/ValueTrackingTest.cpp | 55 +++++++++++++++++++
3 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index d50ccd8af287d8..ed44e9bc6ecc5f 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4513,6 +4513,12 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
return;
}
+ if (isa<PoisonValue>(V)) {
+ Known.KnownFPClasses = fcNone;
+ Known.SignBit = false;
+ return;
+ }
+
// Try to handle fixed width vector constants
auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
const Constant *CV = dyn_cast<Constant>(V);
diff --git a/llvm/test/CodeGen/AMDGPU/fold-fabs.ll b/llvm/test/CodeGen/AMDGPU/fold-fabs.ll
index bb2bad9d3521ad..a04bf445493253 100644
--- a/llvm/test/CodeGen/AMDGPU/fold-fabs.ll
+++ b/llvm/test/CodeGen/AMDGPU/fold-fabs.ll
@@ -99,12 +99,6 @@ define float @fold_abs_in_branch_poison(float %arg1, float %arg2) {
; GFX10-LABEL: fold_abs_in_branch_poison:
; GFX10: ; %bb.0: ; %entry
; GFX10-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; GFX10-NEXT: v_add_f32_e64 v0, |s4|, |s4|
-; GFX10-NEXT: v_cmp_lt_f32_e32 vcc_lo, 1.0, v0
-; GFX10-NEXT: s_cbranch_vccnz .LBB3_2
-; GFX10-NEXT: ; %bb.1: ; %if
-; GFX10-NEXT: v_mul_f32_e64 v0, 0x3e4ccccd, |s4|
-; GFX10-NEXT: .LBB3_2: ; %exit
; GFX10-NEXT: s_setpc_b64 s[30:31]
entry:
%0 = fadd reassoc nnan nsz arcp contract afn float %arg1, %arg2
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index b4d2270d70703f..65bde9fdcf3d06 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -2035,6 +2035,61 @@ TEST_F(ComputeKnownFPClassTest, Constants) {
ASSERT_TRUE(ConstAggZero.SignBit);
EXPECT_FALSE(*ConstAggZero.SignBit);
}
+
+ {
+ KnownFPClass Undef =
+ computeKnownFPClass(UndefValue::get(F32), M->getDataLayout(),
+ fcAllFlags, 0, nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(fcAllFlags, Undef.KnownFPClasses);
+ EXPECT_FALSE(Undef.SignBit);
+ }
+
+ {
+ KnownFPClass Poison =
+ computeKnownFPClass(PoisonValue::get(F32), M->getDataLayout(),
+ fcAllFlags, 0, nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(fcNone, Poison.KnownFPClasses);
+ ASSERT_TRUE(Poison.SignBit);
+ EXPECT_FALSE(*Poison.SignBit);
+ }
+
+ {
+ // Assume the poison element should be 0.
+ Constant *ZeroF32 = ConstantFP::getZero(F32);
+ Constant *PoisonF32 = PoisonValue::get(F32);
+
+ KnownFPClass PartiallyPoison = computeKnownFPClass(
+ ConstantVector::get({ZeroF32, PoisonF32}), M->getDataLayout(),
+ fcAllFlags, 0, nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(fcPosZero, PartiallyPoison.KnownFPClasses);
+ ASSERT_TRUE(PartiallyPoison.SignBit);
+ EXPECT_FALSE(*PartiallyPoison.SignBit);
+ }
+
+ {
+ // Assume the poison element should be 1.
+ Constant *NegZeroF32 = ConstantFP::getZero(F32, true);
+ Constant *PoisonF32 = PoisonValue::get(F32);
+
+ KnownFPClass PartiallyPoison = computeKnownFPClass(
+ ConstantVector::get({NegZeroF32, PoisonF32}), M->getDataLayout(),
+ fcAllFlags, 0, nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(fcNegZero, PartiallyPoison.KnownFPClasses);
+ ASSERT_TRUE(PartiallyPoison.SignBit);
+ EXPECT_TRUE(*PartiallyPoison.SignBit);
+ }
+
+ {
+ // Assume the poison element should be 1.
+ Constant *NegZeroF32 = ConstantFP::getZero(F32, true);
+ Constant *PoisonF32 = PoisonValue::get(F32);
+
+ KnownFPClass PartiallyPoison = computeKnownFPClass(
+ ConstantVector::get({PoisonF32, NegZeroF32}), M->getDataLayout(),
+ fcAllFlags, 0, nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(fcNegZero, PartiallyPoison.KnownFPClasses);
+ EXPECT_TRUE(PartiallyPoison.SignBit);
+ }
}
TEST_F(ValueTrackingTest, isNonZeroRecurrence) {
>From 4b04530d03b943635bf2f4fab45c5e2c88b3520f Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Fri, 12 Apr 2024 23:11:13 +0200
Subject: [PATCH 2/2] Add freeze poison test
---
llvm/test/Transforms/Attributor/nofpclass.ll | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/llvm/test/Transforms/Attributor/nofpclass.ll b/llvm/test/Transforms/Attributor/nofpclass.ll
index 7828629f9fc010..d2d11e0276c4dc 100644
--- a/llvm/test/Transforms/Attributor/nofpclass.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass.ll
@@ -54,6 +54,18 @@ define float @returned_poison() {
ret float poison
}
+; Know nothing
+define float @returned_freeze_poison() {
+; CHECK-LABEL: define noundef float @returned_freeze_poison() {
+; CHECK-NEXT: call void @unknown()
+; CHECK-NEXT: [[FREEZE_POISON:%.*]] = freeze float poison
+; CHECK-NEXT: ret float [[FREEZE_POISON]]
+;
+ call void @unknown()
+ %freeze.poison = freeze float poison
+ ret float %freeze.poison
+}
+
define double @returned_snan() {
; CHECK-LABEL: define noundef nofpclass(qnan inf zero sub norm) double @returned_snan() {
; CHECK-NEXT: call void @unknown()
More information about the llvm-commits
mailing list