Skip to content

Commit 2ee4dda

Browse files
author
spupyrev
committed
profilie inference changes for stale profile matching
This diff facilitates a new stale profile matching in BOLT: D144500 This is a no-op for existing usages of profi (CSSPGO). Reviewed By: hoy Differential Revision: https://reviews.llvm.org/D150466
1 parent 3971e80 commit 2ee4dda

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

llvm/lib/Transforms/Utils/SampleProfileInference.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <queue>
2121
#include <set>
2222
#include <stack>
23+
#include <unordered_set>
2324

2425
using namespace llvm;
2526
#define DEBUG_TYPE "sample-profile-inference"
@@ -1218,10 +1219,23 @@ void extractWeights(const ProfiParams &Params, MinCostMaxFlow &Network,
12181219
#ifndef NDEBUG
12191220
/// Verify that the provided block/jump weights are as expected.
12201221
void verifyInput(const FlowFunction &Func) {
1221-
// Verify the entry block
1222+
// Verify entry and exit blocks
12221223
assert(Func.Entry == 0 && Func.Blocks[0].isEntry());
1224+
size_t NumExitBlocks = 0;
12231225
for (size_t I = 1; I < Func.Blocks.size(); I++) {
12241226
assert(!Func.Blocks[I].isEntry() && "multiple entry blocks");
1227+
if (Func.Blocks[I].isExit())
1228+
NumExitBlocks++;
1229+
}
1230+
assert(NumExitBlocks > 0 && "cannot find exit blocks");
1231+
1232+
// Verify that there are no parallel edges
1233+
for (auto &Block : Func.Blocks) {
1234+
std::unordered_set<uint64_t> UniqueSuccs;
1235+
for (auto &Jump : Block.SuccJumps) {
1236+
auto It = UniqueSuccs.insert(Jump->Target);
1237+
assert(It.second && "input CFG contains parallel edges");
1238+
}
12251239
}
12261240
// Verify CFG jumps
12271241
for (auto &Block : Func.Blocks) {
@@ -1304,8 +1318,26 @@ void verifyOutput(const FlowFunction &Func) {
13041318

13051319
} // end of anonymous namespace
13061320

1307-
/// Apply the profile inference algorithm for a given function
1321+
/// Apply the profile inference algorithm for a given function and provided
1322+
/// profi options
13081323
void llvm::applyFlowInference(const ProfiParams &Params, FlowFunction &Func) {
1324+
// Check if the function has samples and assign initial flow values
1325+
bool HasSamples = false;
1326+
for (FlowBlock &Block : Func.Blocks) {
1327+
if (Block.Weight > 0)
1328+
HasSamples = true;
1329+
Block.Flow = Block.Weight;
1330+
}
1331+
for (FlowJump &Jump : Func.Jumps) {
1332+
if (Jump.Weight > 0)
1333+
HasSamples = true;
1334+
Jump.Flow = Jump.Weight;
1335+
}
1336+
1337+
// Quit early for functions with a single block or ones w/o samples
1338+
if (Func.Blocks.size() <= 1 || !HasSamples)
1339+
return;
1340+
13091341
#ifndef NDEBUG
13101342
// Verify the input data
13111343
verifyInput(Func);

0 commit comments

Comments
 (0)