Skip to content

Conversation

@Rajveer100
Copy link
Member

Resolves #110591

@llvmbot
Copy link
Member

llvmbot commented Oct 20, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Rajveer Singh Bharadwaj (Rajveer100)

Changes

Resolves #110591


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+8)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index c8b9f166b16020..911e5baba8a4de 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1699,6 +1699,14 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
   if (SI->getType()->isIntOrIntVectorTy(1))
     return nullptr;
 
+  if (auto *II = dyn_cast<IntrinsicInst>(&Op)) {
+    if (II->getIntrinsicID() == Intrinsic::sqrt) {
+      Value *NewTV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, TV, nullptr, "sqrt_fold");
+      Value *NewFV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, FV, nullptr, "sqrt_fold");
+      return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, SI);
+    }
+  }
+
   // Test if a FCmpInst instruction is used exclusively by a select as
   // part of a minimum or maximum operation. If so, refrain from doing
   // any other folding. This helps out other analyses which understand

@Rajveer100
Copy link
Member Author

@RKSimon
I am not quite sure if this is the intended way as I do see lot of helper functions (like unary, binary, etc) for individual broken down expressions.

@github-actions
Copy link

github-actions bot commented Oct 20, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@Rajveer100 Rajveer100 force-pushed the instcombine-opt-sqrt branch from 711b46b to 8c0278c Compare October 20, 2024 13:21
@dtcxzyw dtcxzyw requested a review from arsenm October 20, 2024 13:28
@RKSimon RKSimon requested a review from goldsteinn October 20, 2024 13:30
@goldsteinn
Copy link
Contributor

return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr,
SI);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As this currently exists, it will also fold if one of the arms is not constant.

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

Needs tests

Comment on lines +1704 to +1707
Value *NewTV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, TV, nullptr,
"sqrt_fold");
Value *NewFV = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, FV, nullptr,
"sqrt_fold");
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't bother naming these values.

You should also be able to preserve at least some of the flags

@RKSimon
Copy link
Collaborator

RKSimon commented Oct 20, 2024

The fold should only occur if at least one of the new sqrt calls will constant fold away.

@Rajveer100
Copy link
Member Author

Rajveer100 commented Oct 21, 2024

Quick question:

https://alive2.llvm.org/ce/z/YJkX7m

Isn't this fold already applied in the existing source code?

@arsenm
Copy link
Contributor

arsenm commented Oct 21, 2024

Quick question:

https://alive2.llvm.org/ce/z/YJkX7m

Isn't this fold already applied in the existing source code?

Yes, that looks like this is already happening

@Rajveer100
Copy link
Member Author

Rajveer100 commented Oct 22, 2024

In that case the original issue and this PR can be closed?

Edit: Issue was closed, hence closing this.

@Rajveer100 Rajveer100 closed this Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[InstCombine] Missing fold for fsqrt(select(b, c1, c2)) => select(b, fsqrt(c1), fsqrt(c2))

6 participants