Skip to content

Commit fe42e63

Browse files
authored
[mlir][NFC] Refactor eraseState to take constant time (llvm#121670)
Refactors `analysisStates` to use two nested maps . This prevents `eraseState` from having to scan through every analysis state which can be costly when there are many analysis states and/or `eraseState` is called frequently. Signed-off-by: Ian Wood <[email protected]>
1 parent d40235a commit fe42e63

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

mlir/include/mlir/Analysis/DataFlowFramework.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,11 @@ class DataFlowSolver {
332332
/// does not exist.
333333
template <typename StateT, typename AnchorT>
334334
const StateT *lookupState(AnchorT anchor) const {
335-
auto it =
336-
analysisStates.find({LatticeAnchor(anchor), TypeID::get<StateT>()});
337-
if (it == analysisStates.end())
335+
const auto &mapIt = analysisStates.find(LatticeAnchor(anchor));
336+
if (mapIt == analysisStates.end())
337+
return nullptr;
338+
auto it = mapIt->second.find(TypeID::get<StateT>());
339+
if (it == mapIt->second.end())
338340
return nullptr;
339341
return static_cast<const StateT *>(it->second.get());
340342
}
@@ -343,11 +345,7 @@ class DataFlowSolver {
343345
template <typename AnchorT>
344346
void eraseState(AnchorT anchor) {
345347
LatticeAnchor la(anchor);
346-
347-
for (auto it = analysisStates.begin(); it != analysisStates.end(); ++it) {
348-
if (it->first.first == la)
349-
analysisStates.erase(it);
350-
}
348+
analysisStates.erase(LatticeAnchor(anchor));
351349
}
352350

353351
// Erase all analysis states
@@ -426,7 +424,8 @@ class DataFlowSolver {
426424

427425
/// A type-erased map of lattice anchors to associated analysis states for
428426
/// first-class lattice anchors.
429-
DenseMap<std::pair<LatticeAnchor, TypeID>, std::unique_ptr<AnalysisState>>
427+
DenseMap<LatticeAnchor, DenseMap<TypeID, std::unique_ptr<AnalysisState>>,
428+
DenseMapInfo<LatticeAnchor::ParentTy>>
430429
analysisStates;
431430

432431
/// Allow the base child analysis class to access the internals of the solver.
@@ -643,7 +642,7 @@ AnalysisT *DataFlowSolver::load(Args &&...args) {
643642
template <typename StateT, typename AnchorT>
644643
StateT *DataFlowSolver::getOrCreateState(AnchorT anchor) {
645644
std::unique_ptr<AnalysisState> &state =
646-
analysisStates[{LatticeAnchor(anchor), TypeID::get<StateT>()}];
645+
analysisStates[LatticeAnchor(anchor)][TypeID::get<StateT>()];
647646
if (!state) {
648647
state = std::unique_ptr<StateT>(new StateT(anchor));
649648
#if LLVM_ENABLE_ABI_BREAKING_CHECKS
@@ -689,10 +688,6 @@ struct DenseMapInfo<mlir::ProgramPoint> {
689688
}
690689
};
691690

692-
template <>
693-
struct DenseMapInfo<mlir::LatticeAnchor>
694-
: public DenseMapInfo<mlir::LatticeAnchor::ParentTy> {};
695-
696691
// Allow llvm::cast style functions.
697692
template <typename To>
698693
struct CastInfo<To, mlir::LatticeAnchor>

0 commit comments

Comments
 (0)