-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CanonicalizeFreezeInLoops] fix duplicate removal #74716
[CanonicalizeFreezeInLoops] fix duplicate removal #74716
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Wei Tao (Friedrich20) ChangesThis PR fixes #74572 where the the freeze instruction could be found twice by the pass CanonicalizeFreezeInLoops, and then the compiling may crash in second removal since the instruction has already gone. Full diff: https://github.com/llvm/llvm-project/pull/74716.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/CanonicalizeFreezeInLoops.cpp b/llvm/lib/Transforms/Utils/CanonicalizeFreezeInLoops.cpp
index fb4d828853772..ff1eb17e0c248 100644
--- a/llvm/lib/Transforms/Utils/CanonicalizeFreezeInLoops.cpp
+++ b/llvm/lib/Transforms/Utils/CanonicalizeFreezeInLoops.cpp
@@ -151,11 +151,18 @@ bool CanonicalizeFreezeInLoopsImpl::run() {
}
}
+ bool Exist = false;
auto Visit = [&](User *U) {
if (auto *FI = dyn_cast<FreezeInst>(U)) {
- LLVM_DEBUG(dbgs() << "canonfr: found: " << *FI << "\n");
- Info.FI = FI;
- Candidates.push_back(Info);
+ for (const auto &Candidate : Candidates) {
+ auto *FI_cand = Candidate.FI;
+ Exist = (FI_cand == FI) ? true : Exist;
+ }
+ if (!Exist) {
+ LLVM_DEBUG(dbgs() << "canonfr: found: " << *FI << "\n");
+ Info.FI = FI;
+ Candidates.push_back(Info);
+ }
}
};
for_each(PHI.users(), Visit);
|
Can you please add a (reduced) test case in |
LLVM_DEBUG(dbgs() << "canonfr: found: " << *FI << "\n"); | ||
Info.FI = FI; | ||
Candidates.push_back(Info); | ||
for (const auto &Candidate : Candidates) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about instead changing Candidates to a SetVector as suggested by @nikic at the issue? I suppose it would require specializing DenseMapInfo
for FrozenIndPHIInfo
. See https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Analysis/MemoryLocation.h#L330 for an example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fhahn ,
The fix has been refactored by using SmallSetVector.
Thanks for the example that really helps a lot!
5ef1180
to
66cca98
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
557f831
to
6ed0a98
Compare
a870759
to
a8658a0
Compare
FrozenIndPHIInfo(PHINode *PHI, BinaryOperator *StepInst) | ||
: PHI(PHI), StepInst(StepInst) {} | ||
|
||
bool operator==(const FrozenIndPHIInfo &Other) { return FI == Other.FI; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function looks unused / unnecessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nikic ,
I have tested this fix on both llvmorg-15.0.7 and the latest main branch. Just as you said, this function is totally unused on llvm15. However, it is required by the main branch to overload this function to support operator ==
between type
and const type
.
/home/taowei/repo/llvm-project/llvm/lib/Transforms/Utils/CanonicalizeFreezeInLoops.cpp:184:31: required from here
/usr/include/c++/7/bits/predefined_ops.h:241:17: error: no match for ‘operator==’ (operand types are ‘llvm::FrozenIndPHIInfo’ and ‘const llvm::FrozenIndPHIInfo’)
{ return *__it == _M_value; }
~~~~~~^~~~~~~~~~~
a8658a0
to
c8fec5f
Compare
c8fec5f
to
37c5aa5
Compare
@nikic, |
This PR fixes #74572 where the freeze instruction could be found twice by the pass CanonicalizeFreezeInLoops, and then the compiling may crash in second removal since the instruction has already gone.