[llvm] [llvm][ADT] Add invalidate method to ScopedHashTableScope (PR #193516)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 22 08:17:00 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: lonely eagle (linuxlonelyeagle)
<details>
<summary>Changes</summary>
This PR introduces an `invalidate` method to `ScopedHashTable`, designed to remove the most recent value associated with a given key within the scope stack. To support efficient deletion, the internal `ScopedHashTableVal` structure has been refactored into a doubly linked list, allowing the predecessor of a node to be identified in O(1) time during removal. Fix the MLIR CSE issue https://github.com/llvm/llvm-project/pull/191135#discussion_r3100850607
---
Full diff: https://github.com/llvm/llvm-project/pull/193516.diff
1 Files Affected:
- (modified) llvm/include/llvm/ADT/ScopedHashTable.h (+40)
``````````diff
diff --git a/llvm/include/llvm/ADT/ScopedHashTable.h b/llvm/include/llvm/ADT/ScopedHashTable.h
index 97d15463fdfd7..6d54272a2b3ba 100644
--- a/llvm/include/llvm/ADT/ScopedHashTable.h
+++ b/llvm/include/llvm/ADT/ScopedHashTable.h
@@ -46,6 +46,7 @@ template <typename K, typename V>
class ScopedHashTableVal {
ScopedHashTableVal *NextInScope;
ScopedHashTableVal *NextForKey;
+ ScopedHashTableVal *PreInScope;
K Key;
V Val;
@@ -59,6 +60,7 @@ class ScopedHashTableVal {
ScopedHashTableVal *getNextForKey() { return NextForKey; }
const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
ScopedHashTableVal *getNextInScope() { return NextInScope; }
+ ScopedHashTableVal *getPreInScope() { return PreInScope; }
template <typename AllocatorTy>
static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
@@ -70,6 +72,9 @@ class ScopedHashTableVal {
new (New) ScopedHashTableVal(key, val);
New->NextInScope = nextInScope;
New->NextForKey = nextForKey;
+ New->PreInScope = nullptr;
+ if (nextInScope)
+ nextInScope->PreInScope = New;
return New;
}
@@ -78,6 +83,20 @@ class ScopedHashTableVal {
this->~ScopedHashTableVal();
Allocator.Deallocate(this);
}
+
+ template <typename AllocatorTy>
+ static void invalidate(ScopedHashTableVal<K, V> *&ThisEntry,
+ AllocatorTy &Allocator) {
+ ScopedHashTableVal<K, V> *ToDestroy = ThisEntry;
+ ScopedHashTableVal<K, V> *NextInScope = ThisEntry->NextInScope;
+ ScopedHashTableVal<K, V> *PrevInScope = ThisEntry->PreInScope;
+ if (PrevInScope)
+ PrevInScope->NextInScope = NextInScope;
+ if (NextInScope)
+ NextInScope->PreInScope = PrevInScope;
+ ThisEntry = ThisEntry->NextForKey;
+ ToDestroy->Destroy(Allocator);
+ }
};
template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
@@ -101,6 +120,7 @@ class ScopedHashTableScope {
ScopedHashTableScope *getParentScope() { return PrevScope; }
const ScopedHashTableScope *getParentScope() const { return PrevScope; }
+ void invalidate(const K &key);
private:
friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
@@ -219,6 +239,8 @@ class ScopedHashTable : detail::AllocatorHolder<AllocatorTy> {
getAllocator());
S->setLastValInScope(KeyEntry);
}
+
+ void invalidate(const K &key) { CurScope->invalidate(key); }
};
/// ScopedHashTableScope ctor - Install this as the current scope for the hash
@@ -257,6 +279,24 @@ ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
}
}
+template <typename K, typename V, typename KInfo, typename Allocator>
+void ScopedHashTableScope<K, V, KInfo, Allocator>::invalidate(const K &key) {
+ if (!HT.TopLevelMap.contains(key))
+ return;
+ ScopedHashTableVal<K, V> *&ThisEntry = HT.TopLevelMap[key];
+
+ auto S = this;
+ while (S) {
+ if (ThisEntry == S->LastValInScope) {
+ S->LastValInScope = ThisEntry->getNextInScope();
+ break;
+ }
+ S = S->PrevScope;
+ }
+ if (ThisEntry->getNextForKey() == nullptr)
+ HT.TopLevelMap.erase(key);
+ ScopedHashTableVal<K, V>::invalidate(ThisEntry, HT.getAllocator());
+}
} // end namespace llvm
#endif // LLVM_ADT_SCOPEDHASHTABLE_H
``````````
</details>
https://github.com/llvm/llvm-project/pull/193516
More information about the llvm-commits
mailing list