[llvm] [ThinLTO] Change GlobalValueSummaryMapTy from std::map to DenseMap+deque (PR #157839)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 28 10:45:20 PDT 2026


================
@@ -181,13 +183,93 @@ struct alignas(8) GlobalValueSummaryInfo {
 };
 
 /// Map from global value GUID to corresponding summary structures. Use a
-/// std::map rather than a DenseMap so that pointers to the map's value_type
-/// (which are used by ValueInfo) are not invalidated by insertion. Also it will
-/// likely incur less overhead, as the value type is not very small and the size
-/// of the map is unknown, resulting in inefficiencies due to repeated
-/// insertions and resizing.
-using GlobalValueSummaryMapTy =
-    std::map<GlobalValue::GUID, GlobalValueSummaryInfo>;
+/// DenseMap for O(1) lookup and a std::deque for storage. std::deque
+/// guarantees that pointers to elements are not invalidated by push_back,
+/// which is required because ValueInfo stores a raw pointer to elements of
+/// this container.
+class GlobalValueSummaryMap {
+public:
+  using key_type = GlobalValue::GUID;
+  using mapped_type = GlobalValueSummaryInfo;
+  using value_type = std::pair<key_type, mapped_type>;
+  using iterator = std::deque<value_type>::iterator;
+  using const_iterator = std::deque<value_type>::const_iterator;
+  using size_type = std::deque<value_type>::size_type;
+
+private:
+  /// Vector of pointers into Storage, used for key-sorted iteration.
+  using SortedEntriesVec = SmallVector<const value_type *, 0>;
+
+  DenseMap<key_type, unsigned> Map;
+  std::deque<value_type> Storage;
----------------
MaskRay wrote:

I was going to suggest ` DenseMap<key, value_type*> + SpecificBumpPtrAllocator<value_type>`. However, the deque here is also used for iteration, while SpecificBumpPtrAllocator isn't enumerable. A dense `deque` scan is far more efficient than scanning `DenseMap` values (non-continuous).

https://github.com/llvm/llvm-project/pull/157839


More information about the llvm-commits mailing list