Skip to content

Commit 4be3083

Browse files
authored
[BOLT] Remove mutable from BB::LayoutIndex (#93224)
Removed mutability from BB::LayoutIndex, subsequently removed const from BB::SetLayout, and changed BF::dfs to track visited blocks with a set as opposed to tracking and altering LayoutIndexes for more consistent code.
1 parent ed155f3 commit 4be3083

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

bolt/include/bolt/Core/BinaryBasicBlock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class BinaryBasicBlock {
115115
unsigned Index{InvalidIndex};
116116

117117
/// Index in the current layout.
118-
mutable unsigned LayoutIndex{InvalidIndex};
118+
unsigned LayoutIndex{InvalidIndex};
119119

120120
/// Number of pseudo instructions in this block.
121121
uint32_t NumPseudos{0};
@@ -891,7 +891,7 @@ class BinaryBasicBlock {
891891
}
892892

893893
/// Set layout index. To be used by BinaryFunction.
894-
void setLayoutIndex(unsigned Index) const { LayoutIndex = Index; }
894+
void setLayoutIndex(unsigned Index) { LayoutIndex = Index; }
895895

896896
/// Needed by graph traits.
897897
BinaryFunction *getParent() const { return getFunction(); }

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,8 +3641,8 @@ bool BinaryFunction::forEachEntryPoint(EntryPointCallbackTy Callback) const {
36413641

36423642
BinaryFunction::BasicBlockListType BinaryFunction::dfs() const {
36433643
BasicBlockListType DFS;
3644-
unsigned Index = 0;
36453644
std::stack<BinaryBasicBlock *> Stack;
3645+
std::set<BinaryBasicBlock *> Visited;
36463646

36473647
// Push entry points to the stack in reverse order.
36483648
//
@@ -3659,17 +3659,13 @@ BinaryFunction::BasicBlockListType BinaryFunction::dfs() const {
36593659
for (BinaryBasicBlock *const BB : reverse(EntryPoints))
36603660
Stack.push(BB);
36613661

3662-
for (BinaryBasicBlock &BB : blocks())
3663-
BB.setLayoutIndex(BinaryBasicBlock::InvalidIndex);
3664-
36653662
while (!Stack.empty()) {
36663663
BinaryBasicBlock *BB = Stack.top();
36673664
Stack.pop();
36683665

3669-
if (BB->getLayoutIndex() != BinaryBasicBlock::InvalidIndex)
3666+
if (Visited.find(BB) != Visited.end())
36703667
continue;
3671-
3672-
BB->setLayoutIndex(Index++);
3668+
Visited.insert(BB);
36733669
DFS.push_back(BB);
36743670

36753671
for (BinaryBasicBlock *SuccBB : BB->landing_pads()) {

bolt/lib/Passes/IdenticalCodeFolding.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@ Error IdenticalCodeFolding::runOnFunctions(BinaryContext &BC) {
356356
"ICF breakdown", opts::TimeICF);
357357
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
358358
// Make sure indices are in-order.
359-
BF.getLayout().updateLayoutIndices();
359+
if (opts::ICFUseDFS)
360+
BF.getLayout().updateLayoutIndices(BF.dfs());
361+
else
362+
BF.getLayout().updateLayoutIndices();
360363

361364
// Pre-compute hash before pushing into hashtable.
362365
// Hash instruction operands to minimize hash collisions.

bolt/lib/Profile/YAMLProfileWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
7474
llvm::copy(UseDFS ? BF.dfs() : BF.getLayout().blocks(),
7575
std::back_inserter(Order));
7676

77+
const FunctionLayout Layout = BF.getLayout();
78+
Layout.updateLayoutIndices(Order);
79+
7780
for (const BinaryBasicBlock *BB : Order) {
7881
yaml::bolt::BinaryBasicBlockProfile YamlBB;
7982
YamlBB.Index = BB->getLayoutIndex();

0 commit comments

Comments
 (0)