Skip to content

Commit 4663d25

Browse files
authored
[NewPM] Don't preserve BlockFrequencyInfo in FunctionToLoopPassAdaptor (#157888)
Function analyses in LoopStandardAnalysisResults are marked as preserved by the loop pass adaptor, because LoopAnalysisManagerFunctionProxy manually invalidates most of them. However the proxy doesn't invalidate BFI, since it is only preserved on a "lossy" basis: see https://reviews.llvm.org/D86156 and https://reviews.llvm.org/D110438. So any changes to the CFG will result in BFI giving incorrect results, which is fine for loop passes which deal with the lossiness. But the loop pass adapator still marks it as preserved, which causes the lossy result to leak out into function passes. This causes incorrect results when viewed from e.g. LoopVectorizer, where an innermost loop header may be reported to have a smaller frequency than its successors. This fixes this by dropping the call to preserve, and adds a test with the -O1 pipeline which shows the effects whenever the CFG is changed and UseBlockFrequencyInfo is set. I've also dropped it for BranchProbabilityAnalysis too, but I couldn't test for it since UseBranchProbabilityInfo always seems to be false? This may be dead code.
1 parent b6e440b commit 4663d25

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

llvm/lib/Transforms/Scalar/LoopPassManager.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,6 @@ PreservedAnalyses FunctionToLoopPassAdaptor::run(Function &F,
349349
PA.preserve<DominatorTreeAnalysis>();
350350
PA.preserve<LoopAnalysis>();
351351
PA.preserve<ScalarEvolutionAnalysis>();
352-
if (UseBlockFrequencyInfo && F.hasProfileData())
353-
PA.preserve<BlockFrequencyAnalysis>();
354-
if (UseBranchProbabilityInfo && F.hasProfileData())
355-
PA.preserve<BranchProbabilityAnalysis>();
356352
if (UseMemorySSA)
357353
PA.preserve<MemorySSAAnalysis>();
358354
return PA;

llvm/test/Other/loop-pm-invalidation.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
; RUN: opt -disable-output -disable-verify -verify-analysis-invalidation=0 -debug-pass-manager %s -aa-pipeline= 2>&1 \
1717
; RUN: -passes='loop(no-op-loop,loop-deletion),invalidate<scalar-evolution>,loop(no-op-loop)' \
1818
; RUN: | FileCheck %s --check-prefix=CHECK-SCEV-INV-AFTER-DELETE
19+
;
20+
; Test that BFI is invalidated after the loop adapter if any of the loop passes
21+
; invalidated it.
22+
; RUN: opt -disable-output -disable-verify -verify-analysis-invalidation=0 -debug-pass-manager %s -aa-pipeline= 2>&1 \
23+
; RUN: -O1 | FileCheck %s --check-prefix=CHECK-BFI-INV
1924

2025
define void @no_loops() {
2126
; CHECK-LOOP-INV: Running pass: LoopSimplifyPass
@@ -242,3 +247,28 @@ l0.header:
242247
exit:
243248
ret void
244249
}
250+
251+
; CHECK-BFI-INV-LABEL: Running analysis: OuterAnalysisManagerProxy<{{.*}}> on loop %l0.header in function simplifiable_loop
252+
; CHECK-BFI-INV-NEXT: Running pass: LoopInstSimplifyPass on loop %l0.header in function simplifiable_loop
253+
; CHECK-BFI-INV-NEXT: Running pass: LoopSimplifyCFGPass on loop %l0.header in function simplifiable_loop
254+
; CHECK-BFI-INV-NEXT: Running pass: LICMPass on loop %l0.header in function simplifiable_loop
255+
; CHECK-BFI-INV-NEXT: Running pass: LoopRotatePass on loop %l0.header in function simplifiable_loop
256+
; CHECK-BFI-INV-NEXT: Running pass: LICMPass on loop %l0.header in function simplifiable_loop
257+
; CHECK-BFI-INV-NEXT: Running pass: SimpleLoopUnswitchPass on loop %l0.header in function simplifiable_loop
258+
; CHECK-BFI-INV-NEXT: Invalidating analysis: PostDominatorTreeAnalysis on simplifiable_loop
259+
; CHECK-BFI-INV-NEXT: Invalidating analysis: BranchProbabilityAnalysis on simplifiable_loop
260+
; CHECK-BFI-INV-NEXT: Invalidating analysis: BlockFrequencyAnalysis on simplifiable_loop
261+
; CHECK-BFI-INV-NEXT: Running pass: SimplifyCFGPass on simplifiable_loop (5 instructions)
262+
263+
define void @simplifiable_loop(i1 %c) !prof !0 {
264+
entry:
265+
br label %l0.header
266+
267+
l0.header:
268+
br label %l0.latch
269+
270+
l0.latch:
271+
br i1 %c, label %l0.header, label %l0.latch
272+
}
273+
274+
!0 = !{!"function_entry_count", i64 1}

0 commit comments

Comments
 (0)