Skip to content

Commit c941d92

Browse files
author
Chen Zheng
committed
[MachineCycle][NFC] add a cache for block and its top level cycle
This solves llvm#57664 Reviewed By: sameerds Differential Revision: https://reviews.llvm.org/D134019
1 parent 3db2917 commit c941d92

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

llvm/include/llvm/ADT/GenericCycleImpl.h

+21-8
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,28 @@ template <typename ContextT> class GenericCycleInfoCompute {
144144
};
145145

146146
template <typename ContextT>
147-
auto GenericCycleInfo<ContextT>::getTopLevelParentCycle(
148-
const BlockT *Block) const -> CycleT * {
147+
auto GenericCycleInfo<ContextT>::getTopLevelParentCycle(BlockT *Block)
148+
-> CycleT * {
149+
auto Cycle = BlockMapTopLevel.find(Block);
150+
if (Cycle != BlockMapTopLevel.end())
151+
return Cycle->second;
152+
149153
auto MapIt = BlockMap.find(Block);
150154
if (MapIt == BlockMap.end())
151155
return nullptr;
152156

153157
auto *C = MapIt->second;
154158
while (C->ParentCycle)
155159
C = C->ParentCycle;
160+
BlockMapTopLevel.try_emplace(Block, C);
156161
return C;
157162
}
158163

159164
template <typename ContextT>
160-
void GenericCycleInfo<ContextT>::moveToNewParent(CycleT *NewParent,
161-
CycleT *Child) {
165+
void GenericCycleInfo<ContextT>::moveTopLevelCycleToNewParent(CycleT *NewParent,
166+
CycleT *Child) {
167+
assert((!Child->ParentCycle && !NewParent->ParentCycle) &&
168+
"NewParent and Child must be both top level cycle!\n");
162169
auto &CurrentContainer =
163170
Child->ParentCycle ? Child->ParentCycle->Children : TopLevelCycles;
164171
auto Pos = llvm::find_if(CurrentContainer, [=](const auto &Ptr) -> bool {
@@ -169,6 +176,13 @@ void GenericCycleInfo<ContextT>::moveToNewParent(CycleT *NewParent,
169176
*Pos = std::move(CurrentContainer.back());
170177
CurrentContainer.pop_back();
171178
Child->ParentCycle = NewParent;
179+
180+
NewParent->Blocks.insert(NewParent->Blocks.end(), Child->block_begin(),
181+
Child->block_end());
182+
183+
for (auto &It : BlockMapTopLevel)
184+
if (It.second == Child)
185+
It.second = NewParent;
172186
}
173187

174188
/// \brief Main function of the cycle info computations.
@@ -240,10 +254,7 @@ void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {
240254
<< "discovered child cycle "
241255
<< Info.Context.print(BlockParent->getHeader()) << "\n");
242256
// Make BlockParent the child of NewCycle.
243-
Info.moveToNewParent(NewCycle.get(), BlockParent);
244-
NewCycle->Blocks.insert(NewCycle->Blocks.end(),
245-
BlockParent->block_begin(),
246-
BlockParent->block_end());
257+
Info.moveTopLevelCycleToNewParent(NewCycle.get(), BlockParent);
247258

248259
for (auto *ChildEntry : BlockParent->entries())
249260
ProcessPredecessors(ChildEntry);
@@ -257,6 +268,7 @@ void GenericCycleInfoCompute<ContextT>::run(BlockT *EntryBlock) {
257268
assert(!is_contained(NewCycle->Blocks, Block));
258269
NewCycle->Blocks.push_back(Block);
259270
ProcessPredecessors(Block);
271+
Info.BlockMapTopLevel.try_emplace(Block, NewCycle.get());
260272
}
261273
} while (!Worklist.empty());
262274

@@ -336,6 +348,7 @@ void GenericCycleInfoCompute<ContextT>::dfs(BlockT *EntryBlock) {
336348
template <typename ContextT> void GenericCycleInfo<ContextT>::clear() {
337349
TopLevelCycles.clear();
338350
BlockMap.clear();
351+
BlockMapTopLevel.clear();
339352
}
340353

341354
/// \brief Compute the cycle info for a function.

llvm/include/llvm/ADT/GenericCycleInfo.h

+11-8
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,24 @@ template <typename ContextT> class GenericCycleInfo {
232232
private:
233233
ContextT Context;
234234

235-
/// Map basic blocks to their inner-most containing loop.
235+
/// Map basic blocks to their inner-most containing cycle.
236236
DenseMap<BlockT *, CycleT *> BlockMap;
237237

238+
/// Map basic blocks to their top level containing cycle.
239+
DenseMap<BlockT *, CycleT *> BlockMapTopLevel;
240+
238241
/// Top-level cycles discovered by any DFS.
239242
///
240243
/// Note: The implementation treats the nullptr as the parent of
241244
/// every top-level cycle. See \ref contains for an example.
242245
std::vector<std::unique_ptr<CycleT>> TopLevelCycles;
243246

247+
/// Move \p Child to \p NewParent by manipulating Children vectors.
248+
///
249+
/// Note: This is an incomplete operation that does not update the depth of
250+
/// the subtree.
251+
void moveTopLevelCycleToNewParent(CycleT *NewParent, CycleT *Child);
252+
244253
public:
245254
GenericCycleInfo() = default;
246255
GenericCycleInfo(GenericCycleInfo &&) = default;
@@ -254,13 +263,7 @@ template <typename ContextT> class GenericCycleInfo {
254263

255264
CycleT *getCycle(const BlockT *Block) const;
256265
unsigned getCycleDepth(const BlockT *Block) const;
257-
CycleT *getTopLevelParentCycle(const BlockT *Block) const;
258-
259-
/// Move \p Child to \p NewParent by manipulating Children vectors.
260-
///
261-
/// Note: This is an incomplete operation that does not update the
262-
/// list of blocks in the new parent or the depth of the subtree.
263-
void moveToNewParent(CycleT *NewParent, CycleT *Child);
266+
CycleT *getTopLevelParentCycle(BlockT *Block);
264267

265268
/// Methods for debug and self-test.
266269
//@{

0 commit comments

Comments
 (0)