[all-commits] [llvm/llvm-project] 4f5877: [IR] Reduce memory usage in metadata use tracking ...

Kazu Hirata via All-commits all-commits at lists.llvm.org
Tue Sep 29 22:19:29 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 4f5877ff5b6e947123142f7e17ffd04850fd416a
      https://github.com/llvm/llvm-project/commit/4f5877ff5b6e947123142f7e17ffd04850fd416a
  Author: Kazu Hirata <kazu at google.com>
  Date:   2026-09-29 (Tue, 29 Sep 2026)

  Changed paths:
    M llvm/include/llvm/IR/Metadata.h
    M llvm/lib/IR/Metadata.cpp
    M llvm/unittests/IR/MetadataTest.cpp

  Log Message:
  -----------
  [IR] Reduce memory usage in metadata use tracking (NFC) (#221866)

This patch reduces the memory footprint of metadata use tracking.

Currently, ReplaceableUses uses:

    SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4>

where the uint64_t records insertion order.  The problem is that
once it exceeds 2 elements, it allocates 64 buckets (~1.5 KB).  In large
compilations with debug info, thousands of metadata nodes have 3 or 4
uses, leading to significant overhead.

With this patch, uses are stored directly in a SmallVector:

    struct UseEntry {
      void *Ref;
      OwnerTy Owner;
    };
    SmallVector<UseEntry, 4> UseMap;

    // In addRef:
    UseMap.push_back({Ref, Owner});

SmallVector stores up to 4 elements inline (~87% of metadata nodes)
without heap allocation, up from 2 elements (~64% of metadata nodes)
with SmallDenseMap.  It also provides a deterministic array order,
eliminating the need to sort uses by an explicit ID in several places.
For the rare nodes with more than 32 uses, an on-demand
DenseMap<void *, unsigned> (IndexMap) is lazily allocated to map each
Ref to its index in UseMap.

To support this representation efficiently:

- For nodes with up to 32 uses, IndexMap remains null and findRef scans
  UseMap backwards from the end, taking advantage of temporal locality
  since recently added references are typically dropped or moved first.

- Once UseMap exceeds 32 entries, IndexMap is allocated to provide O(1)
  lookups in findRef, and is reset once UseMap shrinks back to 16 or
  fewer entries so it cannot thrash.

- In dropRef, the removed entry is overwritten with UseMap.back() and
  the back is popped, keeping removals O(1) from any position while
  maintaining a deterministic order.

Benchmark results:

                           Base     Test
    File                Max RSS  Max RSS    Delta           Wall Time
    -----------------------------------------------------------------
    SLPVectorizer.ii    1343 MB  1207 MB  -10.14%   43.48s ->  42.80s
    SemaOpenMP.ii       1358 MB  1196 MB  -11.95%   34.42s ->  33.71s
    X86ISelLowering.ii   881 MB   838 MB   -4.85%   26.23s ->  26.03s
    -----------------------------------------------------------------
    Total               3582 MB  3241 MB   -9.52%  104.13s -> 102.53s
    Geometric Mean                         -9.03%              -1.47%

- Compilation flags: -O3 -g -fdebug-info-for-profiling -flto=thin
- Max RSS & Wall Time: average of 3 runs after 1 warmup run.

Thanks to Fangrui Song for exempting ConstantData from metadata use
tracking (#223303), suggesting the swap-and-pop removal in dropRef, and
providing the LargeUseList unit test (#227186).

Assisted-by: Antigravity



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list