Skip to content

Conversation

@ojhunt
Copy link
Contributor

@ojhunt ojhunt commented Aug 9, 2025

The existing code hits the std::optional is of comparing to a bool being "correct" for the std::optional and the intended comparison.

This corrects the comparison while reinforcing the idea that we need some way to diagnose this.

Fixes #152824

@ojhunt ojhunt self-assigned this Aug 9, 2025
@ojhunt ojhunt requested a review from nikic as a code owner August 9, 2025 04:01
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:analysis Includes value tracking, cost tables and constant folding labels Aug 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Oliver Hunt (ojhunt)

Changes

The existing code hits the std::optional<bool> is of comparing to a bool being "correct" for the std::optional and the intended comparison.

This corrects the comparison while reinforcing the idea that we need some way to diagnose this.

Fixes #152824


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+1-1)
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 5907e21065331..afe6275d0426e 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -6304,7 +6304,7 @@ static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0,
   Value *X;
   switch (IID) {
   case Intrinsic::fabs:
-    if (computeKnownFPSignBit(Op0, Q) == false)
+    if (*computeKnownFPSignBit(Op0, Q) == false)
       return Op0;
     break;
   case Intrinsic::bswap:

@ojhunt
Copy link
Contributor Author

ojhunt commented Aug 9, 2025

I can make a test for this at the clang level but this seems like it should be an llvm/ir test and I have no idea how to write it :D

@ojhunt ojhunt force-pushed the users/ojhunt/incorrect-fabs-simplification branch 2 times, most recently from 751516f to 4bebc84 Compare August 9, 2025 04:23
@ojhunt
Copy link
Contributor Author

ojhunt commented Aug 9, 2025

I realized it's possible I've just completely broken fabs-optimizations so I need to run more than clang tests :D

The existing code hits the std::optional<bool> is of comparing
to a bool being "correct" for the std::optional and the intended
comparison.

This corrects the comparison while reinforcing the idea that we
need some way to diagnose this.

Fixes #152824
@ojhunt ojhunt force-pushed the users/ojhunt/incorrect-fabs-simplification branch from 4bebc84 to 47c862f Compare August 9, 2025 05:28
@ojhunt
Copy link
Contributor Author

ojhunt commented Aug 9, 2025

Ok, this is weird, I believe (based on other code) that this is the "correct" fix for this specific piece of code, but making that fix does not actually fix the original bug?

@efriedma-quic
Copy link
Collaborator

Given std::optional<bool>Res;, Res == false, Res == std::optional<bool>(false), and Res && *Res == false all mean the same thing.

@ojhunt
Copy link
Contributor Author

ojhunt commented Aug 9, 2025

Given std::optional<bool>Res;, Res == false, Res == std::optional<bool>(false), and Res && *Res == false all mean the same thing.

Is that the conversion path that is actually happening? (and yes with more testing I found the actual issue is that in my first pass I was just disabling the optimization) I'm closing this PR for now, although independent of fixing anything I think this code would be clearer with the * + == version.

@ojhunt ojhunt closed this Aug 9, 2025
@ojhunt ojhunt deleted the users/ojhunt/incorrect-fabs-simplification branch August 9, 2025 19:51
@efriedma-quic
Copy link
Collaborator

operator== is overloaded, so it's not literally doing the conversion, but yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[llvm] Incorrect simplification of fabs intrinsic

4 participants