[llvm] [IR] Reduce memory usage in metadata use tracking (NFC) (PR #221866)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 10 02:45:42 PDT 2026


================
@@ -1636,6 +1636,11 @@ class LLVMContextImpl {
   DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
   DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
   DenseSet<DIArgList *, DIArgListInfo> DIArgLists;
+  // Maps a tracked metadata reference pointer (void *Ref) to its current index
+  // in ReplaceableMetadataImpl::UseMap. Only populated for replaceable metadata
+  // instances in Large Mode (IsLarge == true). Instantiated eagerly in the
+  // constructor and reset to std::nullopt at teardown.
+  std::optional<DenseMap<void *, unsigned>> MetadataUseMap;
----------------
kazutakahirata wrote:

Thank you for taking a look, and noted on the weekly ping policy!

### std::optional

Used for teardown:

* **Deallocation**: `.reset()` frees bucket/bit-vector memory immediately, avoiding `clear()`'s `memset` on occupancy bits.
* **Sentinel**: `!MetadataUseMap.has_value()` lets `dropRef`/`clear` skip individual `erase` calls during `LLVMContextImpl` destruction.

(Happy to switch to a plain `DenseMap` with `bool IsTearingDown` if preferred.)

### Map shrinking

I sampled every 1000 metadata operations on `SLPVectorizer.ii` and `SemaOpenMP.ii` (`-O3 -g -fdebug-info-for-profiling -flto=thin`):
* **Size**: The size dips mid-compilation but rebounds to meet or exceed the initial peak by the end of compilation.
* **Footprint**: The central hash table peaks at 16 MB (~1.3% of memory), while the patch saves 140 MB–170 MB net.

<img width="1500" height="750" alt="image" src="https://github.com/user-attachments/assets/de29c61c-42a0-4c53-b081-fb6b2b42b152" />
<img width="1500" height="750" alt="image" src="https://github.com/user-attachments/assets/c830efbd-5219-413c-9332-4551e53a2a53" />


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


More information about the llvm-commits mailing list