Skip to content

Commit 48e4ddb

Browse files
authored
Merge pull request #27598 from gottesmm/pr-86500d6ad50210ce9319f02bd3347793bc8b52af
[mandatory-combine] Improve basic block traversal when initializing worklist
2 parents 813dfc4 + 9f3bb93 commit 48e4ddb

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,18 @@ class MandatoryCombiner final
127127
//===----------------------------------------------------------------------===//
128128

129129
void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) {
130-
SmallBlotSetVector<SILBasicBlock *, 32> blockWorklist;
131-
SmallBlotSetVector<SILBasicBlock *, 32> blocksVisited;
132-
SmallVector<SILInstruction *, 128> instructions;
130+
SmallVector<SILBasicBlock *, 32> blockWorklist;
131+
SmallPtrSet<SILBasicBlock *, 32> blockAlreadyAddedToWorklist;
132+
SmallVector<SILInstruction *, 128> initialInstructionWorklist;
133+
134+
{
135+
auto *firstBlock = &*function.begin();
136+
blockWorklist.push_back(firstBlock);
137+
blockAlreadyAddedToWorklist.insert(firstBlock);
138+
}
133139

134-
blockWorklist.insert(&*function.begin());
135140
while (!blockWorklist.empty()) {
136-
auto *block = blockWorklist.pop_back_val().getValueOr(nullptr);
137-
if (block == nullptr) {
138-
continue;
139-
}
140-
141-
if (!blocksVisited.insert(block).second) {
142-
continue;
143-
}
141+
auto *block = blockWorklist.pop_back_val();
144142

145143
for (auto iterator = block->begin(), end = block->end(); iterator != end;) {
146144
auto *instruction = &*iterator;
@@ -150,14 +148,17 @@ void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) {
150148
continue;
151149
}
152150

153-
instructions.push_back(instruction);
151+
initialInstructionWorklist.push_back(instruction);
154152
}
155153

156-
for_each(block->getSuccessorBlocks(),
157-
[&](SILBasicBlock *block) { blockWorklist.insert(block); });
154+
llvm::copy_if(block->getSuccessorBlocks(),
155+
std::back_inserter(blockWorklist),
156+
[&](SILBasicBlock *block) -> bool {
157+
return blockAlreadyAddedToWorklist.insert(block).second;
158+
});
158159
}
159160

160-
worklist.addInitialGroup(instructions);
161+
worklist.addInitialGroup(initialInstructionWorklist);
161162
}
162163

163164
bool MandatoryCombiner::doOneIteration(SILFunction &function,
@@ -282,6 +283,12 @@ class MandatoryCombine final : public SILFunctionTransform {
282283
void run() override {
283284
auto *function = getFunction();
284285

286+
// If this function is an external declaration, bail. We only want to visit
287+
// functions with bodies.
288+
if (function->isExternalDeclaration()) {
289+
return;
290+
}
291+
285292
MandatoryCombiner combiner(createdInstructions);
286293
bool madeChange = combiner.runOnFunction(*function);
287294

0 commit comments

Comments
 (0)