Skip to content
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

Merged
merged 1 commit into from
Dec 28, 2023

Conversation

Friedrich20
Copy link
Contributor

@Friedrich20 Friedrich20 commented Dec 7, 2023

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.

@llvmbot
Copy link
Collaborator

llvmbot commented Dec 7, 2023

@llvm/pr-subscribers-llvm-transforms

Author: Wei Tao (Friedrich20)

Changes

This 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:

  • (modified) llvm/lib/Transforms/Utils/CanonicalizeFreezeInLoops.cpp (+10-3)
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);

@Friedrich20 Friedrich20 marked this pull request as draft December 8, 2023 09:06
@Friedrich20 Friedrich20 marked this pull request as ready for review December 8, 2023 09:07
@nikic nikic changed the title [LLVM][CanonicalizeFreezeInLoops] fix duplicate removal [CanonicalizeFreezeInLoops] fix duplicate removal Dec 8, 2023
@nikic
Copy link
Contributor

nikic commented Dec 8, 2023

Can you please add a (reduced) test case in llvm/test/Transforms/CanonicalizeFreezeInLoops/?

LLVM_DEBUG(dbgs() << "canonfr: found: " << *FI << "\n");
Info.FI = FI;
Candidates.push_back(Info);
for (const auto &Candidate : Candidates) {
Copy link
Contributor

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

Copy link
Contributor Author

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!

@Friedrich20 Friedrich20 force-pushed the Friedrich20/fix-duplicate-removal branch 2 times, most recently from 5ef1180 to 66cca98 Compare December 14, 2023 18:18
Copy link

github-actions bot commented Dec 14, 2023

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

@Friedrich20 Friedrich20 force-pushed the Friedrich20/fix-duplicate-removal branch 2 times, most recently from 557f831 to 6ed0a98 Compare December 15, 2023 09:54
@Friedrich20 Friedrich20 force-pushed the Friedrich20/fix-duplicate-removal branch 3 times, most recently from a870759 to a8658a0 Compare December 21, 2023 14:47
FrozenIndPHIInfo(PHINode *PHI, BinaryOperator *StepInst)
: PHI(PHI), StepInst(StepInst) {}

bool operator==(const FrozenIndPHIInfo &Other) { return FI == Other.FI; }
Copy link
Contributor

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?

Copy link
Contributor Author

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; }
           ~~~~~~^~~~~~~~~~~

@Friedrich20 Friedrich20 force-pushed the Friedrich20/fix-duplicate-removal branch from a8658a0 to c8fec5f Compare December 25, 2023 04:17
@Friedrich20 Friedrich20 force-pushed the Friedrich20/fix-duplicate-removal branch from c8fec5f to 37c5aa5 Compare December 25, 2023 04:29
@nikic nikic merged commit a700298 into llvm:main Dec 28, 2023
4 checks passed
@Friedrich20
Copy link
Contributor Author

@nikic,
many thanks!

@Friedrich20 Friedrich20 deleted the Friedrich20/fix-duplicate-removal branch December 28, 2023 16:08
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.

None yet

4 participants