Skip to content

Commit 2caaad0

Browse files
committed
ColdBlockInfo: post-order walk converges faster
I thought `reverse(silFn)` would do a post-order walk, but I was wrong. This patch cuts the number of iterations to propagate coldness from 3-4 down to 2 in a few of the simple regression test cases. At least on macOS (as the stdlib can vary per platform).
1 parent ec62258 commit 2caaad0

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

lib/SILOptimizer/Analysis/ColdBlockInfo.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/SIL/SILModule.h"
1818
#include "swift/SILOptimizer/Analysis/ColdBlockInfo.h"
1919
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
20+
#include "llvm/ADT/PostOrderIterator.h"
2021

2122
#define DEBUG_TYPE "cold-block-info"
2223

@@ -385,13 +386,16 @@ void ColdBlockInfo::analyze(SILFunction *fn) {
385386
bool changed;
386387
do {
387388
changed = false;
388-
// TODO: We could use a worklist so we progressively visit fewer blocks
389-
// on each iteration, as we only need to visit non-cold/non-leaf blocks.
390-
for (auto &BB : llvm::reverse(*fn)) {
389+
390+
391+
// We're bubbling up coldness from the leaves of the function up towards the
392+
// entry block, so walk the blocks in post-order to converge faster.
393+
for (auto *BB : llvm::post_order(fn)) {
394+
391395
// Only on the first pass, search recursively for an expected value,
392-
// now that more temperature data has been seeded.
393-
if (!completedIters && !foundExpectedCond.contains(&BB)) {
394-
if (auto *CBI = dyn_cast<CondBranchInst>(BB.getTerminator())) {
396+
// if needed, now that more temperature data has been determined.
397+
if (!completedIters && !foundExpectedCond.contains(BB)) {
398+
if (auto *CBI = dyn_cast<CondBranchInst>(BB->getTerminator())) {
395399
auto cond = getCondition(CBI->getCondition());
396400
if (auto val = searchForExpectedValue(cond)) {
397401
setExpectedCondition(CBI, val);
@@ -401,15 +405,15 @@ void ColdBlockInfo::analyze(SILFunction *fn) {
401405
}
402406

403407
// Nothing to propagate from.
404-
if (BB.getNumSuccessors() == 0)
408+
if (BB->getNumSuccessors() == 0)
405409
continue;
406410

407411
// Coldness already exists here.
408-
if (isCold(&BB))
412+
if (isCold(BB))
409413
continue;
410414

411-
if (llvm::all_of(BB.getSuccessorBlocks(), isColdBlock)) {
412-
resetToCold(&BB);
415+
if (llvm::all_of(BB->getSuccessorBlocks(), isColdBlock)) {
416+
resetToCold(BB);
413417
changed = true;
414418
}
415419
}

test/SILOptimizer/cold_block_info.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// RUN: -enable-throws-prediction \
55
// RUN: -Xllvm --debug-only=cold-block-info 2> %t/debug.txt
66

7-
// RUN: %FileCheck %s --input-file=%t/debug.txt
7+
// RUN: %FileCheck %s --input-file=%t/debug.txt \
8+
// RUN: --implicit-check-not 'converged after {{[3-9]}} iters'
89

910
public enum MyError: Error { case err; case number(Int) }
1011

@@ -131,7 +132,7 @@ public func pleasePleasePlease(_ i: Int) throws {
131132
}
132133

133134

134-
// CHECK-LABEL: --> Final for $s4main21nestedTreesOfThrowingyySi_S3itAA7MyErrorOYKF | converged after 1 iters
135+
// CHECK-LABEL: --> Final for $s4main21nestedTreesOfThrowingyySi_S3itAA7MyErrorOYKF
135136
// CHECK-NOT: bb0
136137
// CHECK: }
137138
public func nestedTreesOfThrowing(_ i: Int, _ j: Int, _ k: Int, _ l: Int) throws(MyError) {

0 commit comments

Comments
 (0)