Skip to content

[KnownFPClass] Do not propagate non-NaN through asin and acos - #213812

Merged
dtcxzyw merged 1 commit into
llvm:mainfrom
ZERICO2005:fpclass_asin_acos_fix
Aug 5, 2026
Merged

dtcxzyw merged 1 commit into
llvm:mainfrom
ZERICO2005:fpclass_asin_acos_fix

Conversation

@ZERICO2005

Copy link
Copy Markdown
Contributor

KnownFPClass incorrectly assumed that a non-NaN input to asin or acos implies a non-NaN result. However, both asin and acos can return NaN from a finite input when |x| > 1.0, such as asin(2.0) or acos(2.0).

I have fixed this by removing the calls to Known.propagateNaN(KnownSrc), which incorrectly propagated that asin(non-NaN) == non-NaN.

I discovered this while working on #211686. The same issue was previously noted in a post-merge review comment on #190609 (comment)

AI disclosure:
I used OpenAI Codex (GPT-5.6-sol) to help generate the test updates, which I reviewed and tested locally.

@llvmorg-github-actions

llvmorg-github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-support

@llvm/pr-subscribers-llvm-globalisel

Author: ZERICO2005

Changes

KnownFPClass incorrectly assumed that a non-NaN input to asin or acos implies a non-NaN result. However, both asin and acos can return NaN from a finite input when |x| > 1.0, such as asin(2.0) or acos(2.0).

I have fixed this by removing the calls to Known.propagateNaN(KnownSrc), which incorrectly propagated that asin(non-NaN) == non-NaN.

I discovered this while working on #211686. The same issue was previously noted in a post-merge review comment on #190609 (comment)

AI disclosure:
I used OpenAI Codex (GPT-5.6-sol) to help generate the test updates, which I reviewed and tested locally.


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

3 Files Affected:

  • (modified) llvm/lib/Support/KnownFPClass.cpp (-4)
  • (modified) llvm/test/Transforms/Attributor/nofpclass-trig.ll (+4-4)
  • (modified) llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp (+26-3)
diff --git a/llvm/lib/Support/KnownFPClass.cpp b/llvm/lib/Support/KnownFPClass.cpp
index 4fb1d4cd4723e..8e97781e45389 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -649,8 +649,6 @@ KnownFPClass KnownFPClass::asin(const KnownFPClass &KnownSrc) {
 
   // NaN propagates. asin(x) is also NaN for |x| > 1, so we cannot rule
   // out NaN without knowing the source is in [-1, 1].
-  Known.propagateNaN(KnownSrc);
-
   return Known;
 }
 
@@ -663,8 +661,6 @@ KnownFPClass KnownFPClass::acos(const KnownFPClass &KnownSrc) {
 
   // NaN propagates. acos(x) is also NaN for |x| > 1, so we cannot rule
   // out NaN without knowing the source is in [-1, 1].
-  Known.propagateNaN(KnownSrc);
-
   return Known;
 }
 
diff --git a/llvm/test/Transforms/Attributor/nofpclass-trig.ll b/llvm/test/Transforms/Attributor/nofpclass-trig.ll
index ba96ecfa776b8..5f50b580d9eea 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-trig.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-trig.ll
@@ -137,9 +137,9 @@ define float @ret_asin_nonneg(float nofpclass(ninf nzero nsub nnorm) %arg) {
 }
 
 define float @ret_asin_nonan(float nofpclass(nan) %arg) {
-; CHECK-LABEL: define nofpclass(nan inf) float @ret_asin_nonan
+; CHECK-LABEL: define nofpclass(inf) float @ret_asin_nonan
 ; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(nan inf) float @llvm.asin.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.asin.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.asin.f32(float %arg)
@@ -158,9 +158,9 @@ define float @ret_acos(float %arg) {
 }
 
 define float @ret_acos_nonan(float nofpclass(nan) %arg) {
-; CHECK-LABEL: define nofpclass(nan inf nzero nsub nnorm) float @ret_acos_nonan
+; CHECK-LABEL: define nofpclass(inf nzero nsub nnorm) float @ret_acos_nonan
 ; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(nan inf nzero nsub nnorm) float @llvm.acos.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf nzero nsub nnorm) float @llvm.acos.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.acos.f32(float %arg)
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
index 8d888d8c50f4a..6b17d708d058a 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
@@ -1500,7 +1500,8 @@ TEST_F(AArch64GISelMITest, TestFPClassFAsin) {
 }
 
 TEST_F(AArch64GISelMITest, TestFPClassFAsinPos) {
-  // asin is sign-preserving and bounded: non-negative finite output.
+  // For 0 <= x <= 1, asin is sign-preserving and returns a non-negative
+  // finite value. For x > 1, it returns NaN.
   StringRef MIRString = R"(
     %ptr:_(p0) = G_IMPLICIT_DEF
     %val:_(s32) = G_LOAD %ptr(p0) :: (load (s32))
@@ -1516,8 +1517,8 @@ TEST_F(AArch64GISelMITest, TestFPClassFAsinPos) {
   Register SrcReg = FinalCopy->getOperand(1).getReg();
   GISelValueTracking Info(*MF);
   KnownFPClass Known = Info.computeKnownFPClass(SrcReg);
-  EXPECT_EQ(fcPosFinite, Known.KnownFPClasses);
-  EXPECT_EQ(false, Known.SignBit);
+  EXPECT_EQ(fcPosFinite | fcNan, Known.KnownFPClasses);
+  EXPECT_EQ(std::nullopt, Known.SignBit);
 }
 
 TEST_F(AArch64GISelMITest, TestFPClassFAcos) {
@@ -1540,6 +1541,28 @@ TEST_F(AArch64GISelMITest, TestFPClassFAcos) {
   EXPECT_EQ(std::nullopt, Known.SignBit);
 }
 
+TEST_F(AArch64GISelMITest, TestFPClassFAcosPos) {
+  // For 0 <= x <= 1, acos returns a non-negative finite value.
+  // For x > 1, acos returns NaN.
+  StringRef MIRString = R"(
+    %ptr:_(p0) = G_IMPLICIT_DEF
+    %val:_(s32) = G_LOAD %ptr(p0) :: (load (s32))
+    %fabs:_(s32) = nnan ninf G_FABS %val
+    %facos:_(s32) = G_FACOS %fabs
+    %copy:_(s32) = COPY %facos
+)";
+  setUp(MIRString);
+  if (!TM)
+    GTEST_SKIP();
+  Register CopyReg = Copies[Copies.size() - 1];
+  MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+  Register SrcReg = FinalCopy->getOperand(1).getReg();
+  GISelValueTracking Info(*MF);
+  KnownFPClass Known = Info.computeKnownFPClass(SrcReg);
+  EXPECT_EQ(fcPosFinite | fcNan, Known.KnownFPClasses);
+  EXPECT_EQ(std::nullopt, Known.SignBit);
+}
+
 TEST_F(AArch64GISelMITest, TestFPClassFAtan) {
   // atan is bounded to (-π/2, π/2): never Inf (atan(±Inf) = ±π/2, finite).
   StringRef MIRString = R"(

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-transforms

Author: ZERICO2005

Changes

KnownFPClass incorrectly assumed that a non-NaN input to asin or acos implies a non-NaN result. However, both asin and acos can return NaN from a finite input when |x| &gt; 1.0, such as asin(2.0) or acos(2.0).

I have fixed this by removing the calls to Known.propagateNaN(KnownSrc), which incorrectly propagated that asin(non-NaN) == non-NaN.

I discovered this while working on #211686. The same issue was previously noted in a post-merge review comment on #190609 (comment)

AI disclosure:
I used OpenAI Codex (GPT-5.6-sol) to help generate the test updates, which I reviewed and tested locally.


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

3 Files Affected:

  • (modified) llvm/lib/Support/KnownFPClass.cpp (-4)
  • (modified) llvm/test/Transforms/Attributor/nofpclass-trig.ll (+4-4)
  • (modified) llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp (+26-3)
diff --git a/llvm/lib/Support/KnownFPClass.cpp b/llvm/lib/Support/KnownFPClass.cpp
index 4fb1d4cd4723e..8e97781e45389 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -649,8 +649,6 @@ KnownFPClass KnownFPClass::asin(const KnownFPClass &KnownSrc) {
 
   // NaN propagates. asin(x) is also NaN for |x| > 1, so we cannot rule
   // out NaN without knowing the source is in [-1, 1].
-  Known.propagateNaN(KnownSrc);
-
   return Known;
 }
 
@@ -663,8 +661,6 @@ KnownFPClass KnownFPClass::acos(const KnownFPClass &KnownSrc) {
 
   // NaN propagates. acos(x) is also NaN for |x| > 1, so we cannot rule
   // out NaN without knowing the source is in [-1, 1].
-  Known.propagateNaN(KnownSrc);
-
   return Known;
 }
 
diff --git a/llvm/test/Transforms/Attributor/nofpclass-trig.ll b/llvm/test/Transforms/Attributor/nofpclass-trig.ll
index ba96ecfa776b8..5f50b580d9eea 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-trig.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-trig.ll
@@ -137,9 +137,9 @@ define float @ret_asin_nonneg(float nofpclass(ninf nzero nsub nnorm) %arg) {
 }
 
 define float @ret_asin_nonan(float nofpclass(nan) %arg) {
-; CHECK-LABEL: define nofpclass(nan inf) float @ret_asin_nonan
+; CHECK-LABEL: define nofpclass(inf) float @ret_asin_nonan
 ; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(nan inf) float @llvm.asin.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf) float @llvm.asin.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.asin.f32(float %arg)
@@ -158,9 +158,9 @@ define float @ret_acos(float %arg) {
 }
 
 define float @ret_acos_nonan(float nofpclass(nan) %arg) {
-; CHECK-LABEL: define nofpclass(nan inf nzero nsub nnorm) float @ret_acos_nonan
+; CHECK-LABEL: define nofpclass(inf nzero nsub nnorm) float @ret_acos_nonan
 ; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(nan inf nzero nsub nnorm) float @llvm.acos.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT:    [[CALL:%.*]] = call nofpclass(inf nzero nsub nnorm) float @llvm.acos.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
 ; CHECK-NEXT:    ret float [[CALL]]
 ;
   %call = call float @llvm.acos.f32(float %arg)
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
index 8d888d8c50f4a..6b17d708d058a 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
@@ -1500,7 +1500,8 @@ TEST_F(AArch64GISelMITest, TestFPClassFAsin) {
 }
 
 TEST_F(AArch64GISelMITest, TestFPClassFAsinPos) {
-  // asin is sign-preserving and bounded: non-negative finite output.
+  // For 0 <= x <= 1, asin is sign-preserving and returns a non-negative
+  // finite value. For x > 1, it returns NaN.
   StringRef MIRString = R"(
     %ptr:_(p0) = G_IMPLICIT_DEF
     %val:_(s32) = G_LOAD %ptr(p0) :: (load (s32))
@@ -1516,8 +1517,8 @@ TEST_F(AArch64GISelMITest, TestFPClassFAsinPos) {
   Register SrcReg = FinalCopy->getOperand(1).getReg();
   GISelValueTracking Info(*MF);
   KnownFPClass Known = Info.computeKnownFPClass(SrcReg);
-  EXPECT_EQ(fcPosFinite, Known.KnownFPClasses);
-  EXPECT_EQ(false, Known.SignBit);
+  EXPECT_EQ(fcPosFinite | fcNan, Known.KnownFPClasses);
+  EXPECT_EQ(std::nullopt, Known.SignBit);
 }
 
 TEST_F(AArch64GISelMITest, TestFPClassFAcos) {
@@ -1540,6 +1541,28 @@ TEST_F(AArch64GISelMITest, TestFPClassFAcos) {
   EXPECT_EQ(std::nullopt, Known.SignBit);
 }
 
+TEST_F(AArch64GISelMITest, TestFPClassFAcosPos) {
+  // For 0 <= x <= 1, acos returns a non-negative finite value.
+  // For x > 1, acos returns NaN.
+  StringRef MIRString = R"(
+    %ptr:_(p0) = G_IMPLICIT_DEF
+    %val:_(s32) = G_LOAD %ptr(p0) :: (load (s32))
+    %fabs:_(s32) = nnan ninf G_FABS %val
+    %facos:_(s32) = G_FACOS %fabs
+    %copy:_(s32) = COPY %facos
+)";
+  setUp(MIRString);
+  if (!TM)
+    GTEST_SKIP();
+  Register CopyReg = Copies[Copies.size() - 1];
+  MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+  Register SrcReg = FinalCopy->getOperand(1).getReg();
+  GISelValueTracking Info(*MF);
+  KnownFPClass Known = Info.computeKnownFPClass(SrcReg);
+  EXPECT_EQ(fcPosFinite | fcNan, Known.KnownFPClasses);
+  EXPECT_EQ(std::nullopt, Known.SignBit);
+}
+
 TEST_F(AArch64GISelMITest, TestFPClassFAtan) {
   // atan is bounded to (-π/2, π/2): never Inf (atan(±Inf) = ±π/2, finite).
   StringRef MIRString = R"(

@tgymnich tgymnich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@dtcxzyw
dtcxzyw merged commit 0130acf into llvm:main Aug 5, 2026
16 checks passed
jinge90 pushed a commit to jinge90/llvm-project that referenced this pull request Aug 6, 2026
…vm#213812)

`KnownFPClass` incorrectly assumed that a non-NaN input to `asin` or
`acos` implies a non-NaN result. However, both `asin` and `acos` can
return NaN from a finite input when `|x| > 1.0`, such as `asin(2.0)` or
`acos(2.0)`.

I have fixed this by removing the calls to
`Known.propagateNaN(KnownSrc)`, which incorrectly propagated that
`asin(non-NaN) == non-NaN`.

I discovered this while working on
llvm#211686. The same issue was
previously noted in a post-merge review comment on
llvm#190609 (comment)

AI disclosure:
I used OpenAI Codex (GPT-5.6-sol) to help generate the test updates,
which I reviewed and tested locally.

@fpallaresintel fpallaresintel left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the fix @dtcxzyw!

Removing the propagateNaN is fine, but I think we could also have something like this:

if (KnownSrc.isKnownNever(fcSNan))
  Known.knownNot(fcSNan);

A signaling NaN can still be ruled out, since acos never produces one when the source is not signaling.

@ZERICO2005
ZERICO2005 deleted the fpclass_asin_acos_fix branch August 7, 2026 00:50
@ZERICO2005

Copy link
Copy Markdown
Contributor Author

Thanks for the fix @dtcxzyw!

Removing the propagateNaN is fine, but I think we could also have something like this:

if (KnownSrc.isKnownNever(fcSNan))
  Known.knownNot(fcSNan);

A signaling NaN can still be ruled out, since acos never produces one when the source is not signaling.

I can work on this for asin and acos

arsenm pushed a commit that referenced this pull request Aug 12, 2026
Addresses
#213812 (review)

`asin` and `acos` can only produce `sNaN` if the source is `sNaN`.
Otherwise finite arguments outside the range `-1.0 <= x <= +1.0` will
still produce `qNaN`.
llvm-sync Bot pushed a commit to arm/arm-toolchain that referenced this pull request Aug 12, 2026
Addresses
llvm/llvm-project#213812 (review)

`asin` and `acos` can only produce `sNaN` if the source is `sNaN`.
Otherwise finite arguments outside the range `-1.0 <= x <= +1.0` will
still produce `qNaN`.
llvm-upstreamsync Bot pushed a commit to qualcomm/cpullvm-toolchain that referenced this pull request Aug 12, 2026
Addresses
llvm/llvm-project#213812 (review)

`asin` and `acos` can only produce `sNaN` if the source is `sNaN`.
Otherwise finite arguments outside the range `-1.0 <= x <= +1.0` will
still produce `qNaN`.
zhangweize9-cyber pushed a commit to zhangweize9-cyber/llvm-project that referenced this pull request Aug 16, 2026
Addresses
llvm#213812 (review)

`asin` and `acos` can only produce `sNaN` if the source is `sNaN`.
Otherwise finite arguments outside the range `-1.0 <= x <= +1.0` will
still produce `qNaN`.
carlobertolli pushed a commit to carlobertolli/llvm-project that referenced this pull request Aug 18, 2026
Addresses
llvm#213812 (review)

`asin` and `acos` can only produce `sNaN` if the source is `sNaN`.
Otherwise finite arguments outside the range `-1.0 <= x <= +1.0` will
still produce `qNaN`.
tru pushed a commit that referenced this pull request Sep 30, 2026
…13812)

`KnownFPClass` incorrectly assumed that a non-NaN input to `asin` or
`acos` implies a non-NaN result. However, both `asin` and `acos` can
return NaN from a finite input when `|x| > 1.0`, such as `asin(2.0)` or
`acos(2.0)`.

I have fixed this by removing the calls to
`Known.propagateNaN(KnownSrc)`, which incorrectly propagated that
`asin(non-NaN) == non-NaN`.

I discovered this while working on
#211686. The same issue was
previously noted in a post-merge review comment on
#190609 (comment)

AI disclosure:
I used OpenAI Codex (GPT-5.6-sol) to help generate the test updates,
which I reviewed and tested locally.

(cherry picked from commit 0130acf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants