Skip to content

Commit 2bee163

Browse files
committed
WIP: some IRGen changes
1 parent 8fb4e5a commit 2bee163

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

include/swift/SIL/SILSuccessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class SILSuccessor {
8080
SILSuccessor *getNext() const { return Next; }
8181

8282
ProfileCounter getCount() const { return Count; }
83+
void setCount(ProfileCounter newCount) { Count = newCount; }
8384

8485
// Do not copy or move these.
8586
SILSuccessor(const SILSuccessor &) = delete;

lib/SILOptimizer/Mandatory/IRGenPrepare.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "swift/SILOptimizer/PassManager/Passes.h"
3030
#include "swift/SILOptimizer/PassManager/Transforms.h"
3131
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
32+
#include "swift/SILOptimizer/Analysis/ColdBlockInfo.h"
33+
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
3234
#include "swift/Strings.h"
3335

3436
using namespace swift;
@@ -77,6 +79,58 @@ static bool cleanFunction(SILFunction &fn) {
7779
return madeChange;
7880
}
7981

82+
/// Embed information about cold edges into the SIL via ProfileCounters
83+
/// So that it's available in LLVM.
84+
static bool lowerColdBlockInfo(DominanceAnalysis *DA,
85+
PostDominanceAnalysis *PDA,
86+
SILFunction &fn) {
87+
bool invalidate = false;
88+
89+
ColdBlockInfo CBI(DA, PDA);
90+
CBI.analyze(&fn);
91+
92+
SmallVector<SILSuccessor*, 8> coldSuccs;
93+
SmallVector<SILSuccessor*, 8> warmSuccs;
94+
for (auto &block : fn) {
95+
if (CBI.isCold(&block) || block.getNumSuccessors() < 2)
96+
continue;
97+
98+
coldSuccs.clear(); warmSuccs.clear();
99+
100+
// Partition the successors.
101+
bool hasExistingProfileData = false;
102+
for (SILSuccessor &succ : block.getSuccessors()) {
103+
if (succ.getCount().hasValue()) {
104+
hasExistingProfileData = true;
105+
break;
106+
}
107+
108+
if (CBI.isCold(succ))
109+
coldSuccs.push_back(&succ);
110+
else
111+
warmSuccs.push_back(&succ);
112+
}
113+
114+
if (hasExistingProfileData)
115+
continue;
116+
117+
// Nothing to annotate if everything's warm.
118+
if (coldSuccs.empty())
119+
continue;
120+
121+
ASSERT(!warmSuccs.empty() && "all succs are cold, yet the block isn't?");
122+
invalidate = true;
123+
124+
for (auto *coldSucc : coldSuccs)
125+
coldSucc->setCount(ProfileCounter(1));
126+
127+
for (auto *warmSucc : warmSuccs)
128+
warmSucc->setCount(ProfileCounter(2000));
129+
}
130+
131+
return invalidate;
132+
}
133+
80134
//===----------------------------------------------------------------------===//
81135
// Top Level Entrypoint
82136
//===----------------------------------------------------------------------===//
@@ -86,6 +140,8 @@ namespace {
86140
class IRGenPrepare : public SILFunctionTransform {
87141
void run() override {
88142
SILFunction *F = getFunction();
143+
auto *DA = PM->getAnalysis<DominanceAnalysis>();
144+
auto *PDA = PM->getAnalysis<PostDominanceAnalysis>();
89145

90146
if (getOptions().EmbeddedSwift) {
91147
// In embedded swift all the code is generated in the top-level module.
@@ -98,6 +154,9 @@ class IRGenPrepare : public SILFunctionTransform {
98154

99155
bool shouldInvalidate = cleanFunction(*F);
100156

157+
// FIXME: just for testing...
158+
// shouldInvalidate |= lowerColdBlockInfo(DA, PDA, *F);
159+
101160
if (shouldInvalidate)
102161
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
103162
}

0 commit comments

Comments
 (0)