Add `frexp` to `isTriviallyVectorizable`
Making this vectorizable was out of scope for this work so did not add frexp to isTriviallyVectorizable. We should do a follow up task for this.
Originally posted by @farzonl in https://github.com/llvm/llvm-project/pull/111569#discussion_r1792278053
If we can make frexp scalarizable then some of the same techniques could be used for vectorization.
Now that https://github.com/llvm/llvm-project/commit/e0e67a62076ad56f48c64a7cd2ebf5754b8326b7 has landed you should be able to add frexp to isTriviallyVectorizable
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Farzon Lotfi (farzonl)
Originally posted by @farzonl in https://github.com/llvm/llvm-project/pull/111569#discussion_r1792278053
If we can make frexp scalarizable then some of the same techniques could be used for vectorization.
Marking as good-first-issue.
Can I give it a try?
Go for it.
I have implemented the change to add frexp to isTriviallyVectorizable as requested. However, I encountered a regression/crash in InstCombine during testing.
When instcombine runs on a vector frexp with a select operand, it hits this assertion: Assertion !areInvalidOperands(C, S1, S2) && "Invalid operands for select" failed.
Backtrace highlight:
#12 FoldOpIntoSelect(llvm::Instruction& Op, ...)
#13 visitCallInst(llvm::CallInst& CI)
It seems FoldOpIntoSelect is trying to transform: frexp(select(cond, a, b)) -> select(cond, frexp(a), frexp(b)). @farzonl @fhahn I'm eager to fix this properly rather than just bypassing the crash. Could you please provide some guidance or point me to the right direction?
I believe you're hitting this as the condition is a vector and the result of frexp is a struct type. You should resolve this by checking that if the condition is a vector, the result is a vector in FoldOpIntoSelect. If that does not hold the fold should not apply.
(That change should be a separate PR + test)
Following @MacDue's feedback, I have submitted a prerequisite PR #173208 to fix the latent crash in InstCombine.
This PR adds a safety check in FoldOpIntoSelect to prevent invalid folding of struct-returning operations into vector selects. While this crash isn't currently triggered by existing intrinsics on main, it is a necessary infrastructure fix to unblock the vectorization of frexp.
Once #173208 is landed, I will update my original PR (#172957) to finalize the frexp support.
Hi, here's a similar issue: https://godbolt.org/z/a76dY3z9M. Although it crashes when using modf.
https://github.com/llvm/llvm-project/blob/251724c25dc39e77d709dfa3ffc71a274a932951/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp#L1489-L1491