Skip to content

[CycleInfo] skip unreachable predecessors #101316

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

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/include/llvm/ADT/GenericCycleImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ template <typename ContextT> class GenericCycleInfoCompute {
DFSInfo() = default;
explicit DFSInfo(unsigned Start) : Start(Start) {}

explicit operator bool() const { return Start; }

/// Whether this node is an ancestor (or equal to) the node \p Other
/// in the DFS tree.
bool isAncestorOf(const DFSInfo &Other) const {
Expand Down Expand Up @@ -231,6 +233,8 @@ void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {

for (BlockT *Pred : predecessors(HeaderCandidate)) {
const DFSInfo PredDFSInfo = BlockDFSInfo.lookup(Pred);
// This automatically ignores unreachable predecessors since they have
// zeros in their DFSInfo.
if (CandidateInfo.isAncestorOf(PredDFSInfo))
Worklist.push_back(Pred);
}
Expand All @@ -257,6 +261,10 @@ void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {
const DFSInfo PredDFSInfo = BlockDFSInfo.lookup(Pred);
if (CandidateInfo.isAncestorOf(PredDFSInfo)) {
Worklist.push_back(Pred);
} else if (!PredDFSInfo) {
// Ignore an unreachable predecessor. It will will incorrectly cause
// Block to be treated as a cycle entry.
LLVM_DEBUG(errs() << " skipped unreachable predecessor.\n");
} else {
IsEntry = true;
}
Expand Down
23 changes: 23 additions & 0 deletions llvm/test/Analysis/CycleInfo/unreachable-predecessor.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
; RUN: opt < %s -disable-output -passes='print<cycles>' 2>&1 | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need an implicit-check-not to make sure this doesn't appear in the output?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The CHECK is an exact one. The incorrect entry will show up in the entries() parentheses below and the CHECK will fail.

; CHECK-LABEL: CycleInfo for function: unreachable
; CHECK: depth=1: entries(loop.body) loop.latch inner.block
define void @unreachable(i32 %n) {
entry:
br label %loop.body

loop.body:
br label %inner.block

; This branch should not cause %inner.block to appear as an entry.
unreachable.block:
br label %inner.block

inner.block:
br i1 undef, label %loop.exit, label %loop.latch

loop.latch:
br label %loop.body

loop.exit:
ret void
}
Loading