[llvm-branch-commits] [llvm] [DebugInfo][FLMD] Add bitcode printing/parsing for function context (PR #236801)

Stephen Tozer via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Oct 29 08:06:49 PDT 2026


https://github.com/SLTozer updated https://github.com/llvm/llvm-project/pull/236801

>From b961858819347ea1f70faa99fab3c9848a0a0cd3 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Fri, 23 Oct 2026 17:50:21 +0100
Subject: [PATCH 1/2] [DebugInfo][FLMD] Add bitcode printing/parsing for
 function context

This patch adds support for bitcode printing/parsing for
MDFunctionLocalContext. This is an unusual entry in that it contains
multiple variable-length arrays, and collectively comprises a potentially
very large (technically up to ~2^32, realistically >10000 is possible)
number of records. There is no abbreviation, since the existing
abbreviation mechanism does not easily translate to this case.

Also unique is that, as with the LLParser, we use a custom forward
reference resolution. In this case, we track the metadata IDs rather than
maintaining a TrackingMDRef, and resolve these at the general metadata
resolution step, where the MetadataList has been finalized.
---
 llvm/include/llvm/Bitcode/LLVMBitCodes.h     |   1 +
 llvm/include/llvm/IR/FunctionLocalMetadata.h |   1 +
 llvm/lib/Bitcode/Reader/MetadataLoader.cpp   | 133 +++++++++++++++++++
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp    |  30 ++++-
 llvm/lib/Bitcode/Writer/ValueEnumerator.cpp  |  86 +++++++++---
 llvm/test/Bitcode/flmd-context.ll            | 118 ++++++++++++++++
 6 files changed, 350 insertions(+), 19 deletions(-)
 create mode 100644 llvm/test/Bitcode/flmd-context.ll

diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 89e52bf02bb6e9..6b1d712c0556bc 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -403,6 +403,7 @@ enum MetadataCodes {
   METADATA_PROPERTY = 50, // [distinct, name, file, line, type, backing_storage]
   METADATA_LAYERLOC = 51, // [distinct, line, column, file, kind]
   METADATA_LAYERLOCLIST = 52, // [distinct, n x layerloc]
+  METADATA_FLMD = 53, // [nonDebug, n x scopes, m x srcLocs, l x inlinedCalls]
 };
 
 // The constants block (CONSTANTS_BLOCK_ID) describes emission for each
diff --git a/llvm/include/llvm/IR/FunctionLocalMetadata.h b/llvm/include/llvm/IR/FunctionLocalMetadata.h
index e947fe7218c496..51ea88e2038d7c 100644
--- a/llvm/include/llvm/IR/FunctionLocalMetadata.h
+++ b/llvm/include/llvm/IR/FunctionLocalMetadata.h
@@ -253,6 +253,7 @@ class MDFunctionLocalContext : public MDNode {
   friend class LLVMContextImpl;
   friend class MDNode;
   friend class LLParser;
+  friend class MetadataLoader;
   constexpr static uint16_t MaxAllowedAtomGroup =
       FLInlinedCall::MaxAllowedAtomGroup;
 
diff --git a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
index fbe3db5fbcc3bc..6299f7c0792d0e 100644
--- a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
+++ b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
@@ -462,6 +462,15 @@ class MetadataLoader::MetadataLoaderImpl {
   Module &TheModule;
   MetadataLoaderCallbacks Callbacks;
 
+  /// Forward-reference tracking for MDFunctionLocalContext.
+  /// Each call to parseOneMetadata for a context adds an entry to these arrays
+  /// iff it contains at least one forward reference to a scope/context; flushed
+  /// when we resolve metadata references.
+  SmallVector<std::pair<MDFunctionLocalContext *, SmallVector<uint64_t, 0>>, 0>
+      FLMDScopeMetadataIDs;
+  SmallVector<std::pair<MDFunctionLocalContext *, SmallVector<uint64_t, 0>>, 0>
+      FLMDInlineeMetadataIDs;
+
   /// Cursor associated with the lazy-loading of Metadata. This is the easy way
   /// to keep around the right "context" (Abbrev list) to be able to jump in
   /// the middle of the metadata block and load any record.
@@ -1353,6 +1362,37 @@ void MetadataLoader::MetadataLoaderImpl::resolveForwardRefsAndPlaceholders(
   // Finally, everything is in place, we can replace the placeholders operands
   // with the final node they refer to.
   Placeholders.flush(MetadataList);
+
+  // Now that we've flushed, we can resolve the FLMD fwd refs too.
+  for (auto &[FLMD, ScopeMDs] : FLMDScopeMetadataIDs) {
+    for (size_t ScopeIdx = 0; ScopeIdx < FLMD->Scopes.size(); ++ScopeIdx) {
+      // Forward references are the expected case, but not guaranteed, so skip
+      // anything that isn't a forward reference now.
+      if (FLMD->Scopes[ScopeIdx].Scope)
+        continue;
+      Metadata *ScopeMD = MetadataList.lookup(ScopeMDs[ScopeIdx]);
+      // If the cast here fails, the scope will be left with a nullptr Scope,
+      // which should fail in the verifier.
+      FLMD->Scopes[ScopeIdx].Scope = dyn_cast_or_null<DILocalScope>(ScopeMD);
+    }
+  }
+  for (auto &[FLMD, InlineeMDs] : FLMDInlineeMetadataIDs) {
+    for (size_t InlinedCallIdx = 0; InlinedCallIdx < FLMD->InlinedCalls.size();
+         ++InlinedCallIdx) {
+      // Forward references are the expected case, but not guaranteed, so skip
+      // anything that isn't a forward reference now.
+      if (FLMD->InlinedCalls[InlinedCallIdx].InlineeContext)
+        continue;
+      Metadata *InlineeContextMD =
+          MetadataList.lookup(InlineeMDs[InlinedCallIdx]);
+      // If the cast here fails, the InlinedCall will be left with a nullptr
+      // InlineeContext, which should fail in the verifier.
+      FLMD->InlinedCalls[InlinedCallIdx].InlineeContext =
+          dyn_cast_or_null<MDFunctionLocalContext>(InlineeContextMD);
+    }
+  }
+  FLMDScopeMetadataIDs.clear();
+  FLMDInlineeMetadataIDs.clear();
 }
 
 static Value *getValueFwdRef(BitcodeReaderValueList &ValueList, unsigned Idx,
@@ -2636,6 +2676,99 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
     NextMetadataNo++;
     break;
   }
+  case bitc::METADATA_FLMD: {
+    // MDFunctionLocalContext is never uniqued.
+    IsDistinct = true;
+    // Since we have several variable-length arrays in this metadata, we'll be
+    // checking the number of records multiple times.
+    size_t NumRecords = Record.size();
+    // Minimal possible case, 1 Version/NonDebug record + 4 "size" records.
+    if (NumRecords < 5)
+      return error("Invalid record");
+    bool IsNonDebug = Record[0] & 1;
+    uint64_t Version = Record[0] >> 1;
+    (void)Version;
+    // FLMD comprises 3 arrays with a leading length field.
+    uint64_t CurrentRecord = 1;
+    MDFunctionLocalContext *NewFLMD = MDFunctionLocalContext::getDistinct(Context, IsNonDebug);
+    // Assign the metadata number now, so that we don't have to use forward
+    // references for ourself.
+    MetadataList.assignValue(NewFLMD, NextMetadataNo);
+    // Scopes
+    uint64_t NumScopes = Record[CurrentRecord++];
+    // Check we have enough remaining records for NumScopes and 2 more sizes.
+    if (NumRecords - CurrentRecord < NumScopes + 2)
+      return error("Invalid record");
+    // NB: Keeping the FwdRefs array as a raw pointer won't work if this
+    // function recurses from here or if the root array otherwise ends up being
+    // written to.
+    SmallVectorImpl<uint64_t> *ScopeFwdRefs = nullptr;
+    for (uint64_t ScopeIdx = 0; ScopeIdx < NumScopes; ++ScopeIdx) {
+      uint64_t Scope = Record[CurrentRecord++];
+      auto *ScopeMD = getMD(Scope);
+      // If the Scope metadata has already been loaded then use it, otherwise
+      // track the forward reference.
+      if (auto *ScopeNode = dyn_cast<DILocalScope>(ScopeMD)) {
+        NewFLMD->Scopes.push_back(FLScope(ScopeNode));
+      } else {
+        if (!ScopeFwdRefs) {
+          FLMDScopeMetadataIDs.push_back({NewFLMD, {}});
+          ScopeFwdRefs = &FLMDScopeMetadataIDs.back().second;
+          ScopeFwdRefs->resize_for_overwrite(NumScopes);
+        }
+        (*ScopeFwdRefs)[ScopeIdx] = Scope;
+        NewFLMD->Scopes.push_back(FLScope(nullptr));
+      }
+    }
+    // SrcLocs
+    uint64_t NumSrcLocs = Record[CurrentRecord++];
+    // Check we have enough remaining records for NumSrcLocs and 1 more size.
+    if (NumRecords - CurrentRecord < (NumSrcLocs * 3) + 1)
+      return error("Invalid record");
+    for (uint64_t SrcLocIdx = 0; SrcLocIdx < NumSrcLocs; ++SrcLocIdx) {
+      uint32_t Line = Record[CurrentRecord++];
+      uint16_t Column = Record[CurrentRecord++];
+      uint16_t RawScopeIdx = Record[CurrentRecord++];
+      NewFLMD->SrcLocs.push_back(FLSrcLoc(
+        Line, Column, FLIndex<FLScope>::fromRaw(RawScopeIdx)));
+    }
+    // InlinedCalls
+    uint64_t NumInlinedCalls = Record[CurrentRecord++];
+    // Check we have exactly enough remaining records for NumInlinedCalls.
+    if (NumRecords - CurrentRecord != (NumInlinedCalls * 4))
+      return error("Invalid record");
+    // NB: Keeping the FwdRefs array as a raw pointer won't work if this
+    // function recurses from here or if the root array otherwise ends up being
+    // written to.
+    SmallVectorImpl<uint64_t> *InlinedCallFwdRefs = nullptr;
+    for (uint64_t InlinedCallIdx = 0; InlinedCallIdx < NumInlinedCalls; ++InlinedCallIdx) {
+      uint32_t RawSrcLocIdx = Record[CurrentRecord++];
+      uint16_t RawInlinedAtIdx = Record[CurrentRecord++];
+      bool Uniquable = Record[CurrentRecord++];
+      uint64_t InlineeContext = Record[CurrentRecord++];
+      auto *InlineeMD = getMD(InlineeContext);
+      if (MDFunctionLocalContext *InlineeFLContext =
+              dyn_cast<MDFunctionLocalContext>(InlineeMD)) {
+        NewFLMD->InlinedCalls.push_back(
+            FLInlinedCall(FLIndex<FLSrcLoc>::fromRaw(RawSrcLocIdx),
+                          FLIndex<FLInlinedCall>::fromRaw(RawInlinedAtIdx),
+                          InlineeFLContext, Uniquable));
+      } else {
+        if (!InlinedCallFwdRefs) {
+          FLMDInlineeMetadataIDs.push_back({NewFLMD, {}});
+          InlinedCallFwdRefs = &FLMDInlineeMetadataIDs.back().second;
+          InlinedCallFwdRefs->resize_for_overwrite(NumInlinedCalls);
+        }
+        (*InlinedCallFwdRefs)[InlinedCallIdx] = InlineeContext;
+        NewFLMD->InlinedCalls.push_back(
+            FLInlinedCall(FLIndex<FLSrcLoc>::fromRaw(RawSrcLocIdx),
+                          FLIndex<FLInlinedCall>::fromRaw(RawInlinedAtIdx),
+                          nullptr, Uniquable));
+      }
+    }
+    NextMetadataNo++;
+    break;
+  }
   }
   return Error::success();
 #undef GET_OR_DISTINCT
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index fb95fed3da77c0..13b302bef04c43 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -1920,7 +1920,35 @@ void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
 void ModuleBitcodeWriter::writeMDFunctionLocalContext(
     const MDFunctionLocalContext *N, SmallVectorImpl<uint64_t> &Record,
     unsigned &Abbrev) {
-  report_fatal_error("!MDFunctionLocalContext bitcode not yet implemented");
+  // Don't record distinctness, since MDFunctionLocalContext is always distinct,
+  // but use a spare bit in the version record to record isNonDebug.
+  const uint64_t Version = 1 << 1;
+  Record.push_back((uint64_t)N->isNonDebug() | Version);
+  // Also do not record MaxAtomGroup, as it is a transient field.
+  // Print each array separately, leading with a length-field.
+  // Scopes
+  Record.push_back(N->getScopes().size());
+  for (const FLScope &Scope : N->getScopes())
+    Record.push_back(VE.getMetadataID(Scope.get()));
+  // SrcLocs
+  Record.push_back(N->getSrcLocs().size());
+  for (const FLSrcLoc &SrcLoc : N->getSrcLocs()) {
+    Record.push_back(SrcLoc.Line);
+    Record.push_back(SrcLoc.Column);
+    // Raw FLIndex is more efficiently encoded than the underlying value.
+    Record.push_back(SrcLoc.ScopeIdx.asRaw());
+  }
+  // InlinedCalls
+  Record.push_back(N->getInlinedCalls().size());
+  for (const FLInlinedCall &InlinedCall : N->getInlinedCalls()) {
+    Record.push_back(InlinedCall.SrcLocIdx.asRaw());
+    Record.push_back(InlinedCall.InlinedAtIdx.asRaw());
+    // Don't record MaxAtomGroup, as it is a transient field.
+    Record.push_back(InlinedCall.Uniquable);
+    Record.push_back(VE.getMetadataID(InlinedCall.InlineeContext));
+  }
+  Stream.EmitRecord(bitc::METADATA_FLMD, Record, Abbrev);
+  Record.clear();
 }
 
 unsigned ModuleBitcodeWriter::createDILocationAbbrev(bool WithIRLayers) {
diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
index 2a1ad62c2804c6..ee63320d74ba1d 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -19,6 +19,7 @@
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Function.h"
+#include "llvm/IR/FunctionLocalMetadata.h"
 #include "llvm/IR/GlobalAlias.h"
 #include "llvm/IR/GlobalIFunc.h"
 #include "llvm/IR/GlobalObject.h"
@@ -674,14 +675,23 @@ void ValueEnumerator::dropFunctionFromMetadata(
         Worklist.push_back(N);
   };
   push(FirstMD);
-  while (!Worklist.empty())
-    for (const Metadata *Op : Worklist.pop_back_val()->operands()) {
+  while (!Worklist.empty()) {
+    const MDNode *Next = Worklist.pop_back_val();
+    for (const Metadata *Op : Next->operands()) {
       if (!Op)
         continue;
       auto MD = MetadataMap.find(Op);
       if (MD != MetadataMap.end())
         push(*MD);
     }
+    if (auto *FLContext = dyn_cast<MDFunctionLocalContext>(Next)) {
+      SmallVector<const MDNode *> UsedNodes;
+      FLContext->getUsedMDNodes(UsedNodes);
+      for (const Metadata *Op : UsedNodes)
+        if (auto MD = MetadataMap.find(Op); MD != MetadataMap.end())
+          push(*MD);
+    }
+  }
 }
 
 void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
@@ -693,28 +703,68 @@ void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
 
   // Start by enumerating MD, and then work through its transitive operands in
   // post-order.  This requires a depth-first search.
-  SmallVector<std::pair<const MDNode *, MDNode::op_iterator>, 32> Worklist;
+  SmallVector<std::pair<const MDNode *, size_t>, 32> Worklist;
+
   if (const MDNode *N = enumerateMetadataImpl(F, MD))
-    Worklist.push_back(std::make_pair(N, N->op_begin()));
+    Worklist.push_back(std::make_pair(N, 0));
 
+  // Fetching the MDNodes used by an MDFunctionLocalContext is expensive; if we
+  // need to get them at all, do so once in this map and reuse as needed.
+  DenseMap<const MDFunctionLocalContext *, SmallVector<const MDNode *, 0>>
+    FLContextUsedNodes;
   while (!Worklist.empty()) {
     const MDNode *N = Worklist.back().first;
+    size_t &OpNum = Worklist.back().second;
 
     // Enumerate operands until we hit a new node.  We need to traverse these
     // nodes' operands before visiting the rest of N's operands.
-    MDNode::op_iterator I = std::find_if(
-        Worklist.back().second, N->op_end(),
-        [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
-    if (I != N->op_end()) {
-      auto *Op = cast<MDNode>(*I);
-      Worklist.back().second = ++I;
-
-      // Delay traversing Op if it's a distinct node and N is uniqued.
-      if (Op->isDistinct() && !N->isDistinct())
-        DelayedDistinctNodes.push_back(Op);
-      else
-        Worklist.push_back(std::make_pair(Op, Op->op_begin()));
-      continue;
+    if (OpNum < N->getNumOperands()) {
+      MDNode::op_iterator I = std::find_if(
+          N->op_begin() + OpNum, N->op_end(),
+          [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
+      if (I != N->op_end()) {
+        auto *Op = cast<MDNode>(*I);
+        OpNum = I - N->op_begin() + 1;
+
+        // Delay traversing Op if it's a distinct node and N is uniqued.
+        if (Op->isDistinct() && !N->isDistinct())
+          DelayedDistinctNodes.push_back(Op);
+        else
+          Worklist.push_back(std::make_pair(Op, 0));
+        continue;
+      }
+    }
+    // MDFunctionLocalContext contains MDNode references outside of its
+    // operands, which can be fetched via `getUsedMDNodes()`; OpNum indexes into
+    // this vector if N is an MDFunctionLocalContext.
+    if (auto *FLContext = dyn_cast<MDFunctionLocalContext>(N)) {
+      // Currently, MDFunctionLocalContext has no operands; if it ever does in
+      // future, then we need to account for them by offsetting OpNum by the
+      // operand count. For now, we have an assert to prevent a silent fail from
+      // being introduced.
+      assert(N->getNumOperands() == 0 &&
+        "MDFunctionLocalContext expected to have no operands");
+      auto Result = FLContextUsedNodes.try_emplace(FLContext);
+      auto &UsedMDNodes = Result.first->second;
+      if (Result.second)
+        FLContext->getUsedMDNodes(UsedMDNodes);
+      auto *NextUsed = std::find_if(
+          UsedMDNodes.begin() + OpNum, UsedMDNodes.end(),
+          [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
+      if (NextUsed != UsedMDNodes.end()) {
+        auto *Op = *NextUsed;
+        OpNum = NextUsed - UsedMDNodes.begin() + 1;
+
+        // Delay traversing Op if it's a distinct node and N is uniqued.
+        if (Op->isDistinct() && !N->isDistinct())
+          DelayedDistinctNodes.push_back(Op);
+        else
+          Worklist.push_back(std::make_pair(Op, 0));
+        continue;
+      }
+    } else {
+      assert(OpNum <= N->getNumOperands() &&
+        "Visited past the end of N's operands");
     }
 
     // All the operands have been visited.  Now assign an ID.
@@ -726,7 +776,7 @@ void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
     // that are leaves in last uniqued subgraph.
     if (Worklist.empty() || Worklist.back().first->isDistinct()) {
       for (const MDNode *N : DelayedDistinctNodes)
-        Worklist.push_back(std::make_pair(N, N->op_begin()));
+        Worklist.push_back(std::make_pair(N, 0));
       DelayedDistinctNodes.clear();
     }
   }
diff --git a/llvm/test/Bitcode/flmd-context.ll b/llvm/test/Bitcode/flmd-context.ll
new file mode 100644
index 00000000000000..44d251fd24eda6
--- /dev/null
+++ b/llvm/test/Bitcode/flmd-context.ll
@@ -0,0 +1,118 @@
+;; Basic test that we can roundtrip an MDFunctionLocalContext to bitcode and
+;; back.
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
+
+; CHECK: define i32 @foo
+; CHECK-SAME: !dbg ![[FOO_SP:[0-9]+]] !flmd ![[FOO_CTX:[0-9]+]]
+; CHECK: define i32 @baz
+; CHECK-SAME: !flmd ![[BAZ_CTX:[0-9]+]]
+
+; CHECK: ![[FOO_SP]] = distinct !DISubprogram(name: "foo",
+; CHECK: ![[FOO_CTX]] = distinct !MDFunctionLocalContext(scopes: [
+; CHECK-NEXT:   !!scope(scope: ![[FOO_SP]])
+; CHECK-NEXT: ], srcLocs: [
+; CHECK-NEXT:   !!srcLoc(line: 0, scope: 0),
+; CHECK-NEXT:   !!srcLoc(line: 2, scope: 0),
+; CHECK-NEXT:   !!srcLoc(line: 3, scope: 0)
+; CHECK-NEXT: ], inlinedCalls: [
+; CHECK-NEXT:   !!inlinedCall(srcLoc: 2, inlineeContext: ![[BAR_CTX:[0-9]+]]),
+; CHECK-NEXT:   !!inlinedCall(srcLoc: 4, inlinedAt: 0, uniquable: true, inlineeContext: ![[QUX_CTX:[0-9]+]]),
+; CHECK-NEXT:   !!inlinedCall(srcLoc: 2, inlineeContext: ![[FOO_CTX]])
+; CHECK-NEXT: ])
+; CHECK: ![[BAR_CTX]] = distinct !MDFunctionLocalContext(scopes: [
+; CHECK-NEXT:   !!scope(scope: ![[BAR_SP:[0-9]+]]),
+; CHECK-NEXT:   !!scope(scope: ![[BAR_LB:[0-9]+]])
+; CHECK-NEXT: ], srcLocs: [
+; CHECK-NEXT:   !!srcLoc(line: 0, scope: 0),
+; CHECK-NEXT:   !!srcLoc(line: 12, scope: 0),
+; CHECK-NEXT:   !!srcLoc(line: 13, scope: 0),
+; CHECK-NEXT:   !!srcLoc(line: 14, column: 7, scope: 1),
+; CHECK-NEXT:   !!srcLoc(line: 15, scope: 1),
+; CHECK-NEXT:   !!srcLoc(line: 16, column: 12, scope: 0)
+; CHECK-NEXT: ])
+; CHECK: ![[BAR_SP]] = distinct !DISubprogram(name: "bar",
+; CHECK: ![[BAR_LB]] = distinct !DILexicalBlock(scope: ![[BAR_SP]]
+; CHECK: ![[QUX_CTX]] = distinct !MDFunctionLocalContext(scopes: [
+; CHECK-NEXT:   !!scope(scope: ![[QUX_SP:[0-9]+]])
+; CHECK-NEXT:   !!scope(scope: ![[QUX_LB:[0-9]+]])
+; CHECK-NEXT: ], srcLocs: [
+; CHECK-NEXT:   !!srcLoc(line: 0, scope: 0),
+; CHECK-NEXT:   !!srcLoc(line: 22, scope: 0),
+; CHECK-NEXT:   !!srcLoc(line: 23, scope: 1)
+; CHECK-NEXT: ], inlinedCalls: [
+; CHECK-NEXT:   !!inlinedCall(srcLoc: 1, inlineeContext: ![[FOO_CTX]])
+; CHECK-NEXT: ])
+; CHECK: ![[QUX_SP]] = distinct !DISubprogram(name: "qux",
+; CHECK: ![[QUX_LB]] = !DILexicalBlock(scope: ![[QUX_SP]]
+; CHECK: ![[BAZ_CTX]] = distinct !MDFunctionLocalContext(nonDebug: true, inlinedCalls: [
+; CHECK-NEXT:   !!inlinedCall(inlineeContext: ![[FOO_CTX]]),
+; CHECK-NEXT:   !!inlinedCall(srcLoc: 2, inlinedAt: 0, inlineeContext: ![[BAR_CTX]])
+; CHECK-NEXT: ])
+
+define i32 @foo(i32 %i) !dbg !9 !flmd !12 {
+entry:
+  ret i32 %i
+}
+
+define i32 @baz(i32 %i) !flmd !19 {
+entry:
+  ret i32 %i
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3, !4, !5, !6, !7}
+!llvm.ident = !{!8}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 24.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.c", directory: "/")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 8, !"PIC Level", i32 2}
+!5 = !{i32 7, !"PIE Level", i32 2}
+!6 = !{i32 7, !"uwtable", i32 2}
+!7 = !{i32 7, !"frame-pointer", i32 2}
+!8 = !{!"clang version 24.0.0git"}
+!9 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 2, type: !10, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
+!10 = !DISubroutineType(types: !11)
+!11 = !{}
+!12 = distinct !MDFunctionLocalContext(scopes: [
+  !!scope(scope: !9),
+], srcLocs: [
+  !!srcLoc(line: 0, scope: 0),
+  !!srcLoc(line: 2, scope: 0),
+  !!srcLoc(line: 3, scope: 0),
+], inlinedCalls: [
+  !!inlinedCall(srcLoc: 2, inlineeContext: !13),
+  !!inlinedCall(srcLoc: 4, inlinedAt: 0, uniquable: true, inlineeContext: !16),
+  !!inlinedCall(srcLoc: 2, inlineeContext: !12),
+])
+!13 = distinct !MDFunctionLocalContext(scopes: [
+  !!scope(scope: !14),
+  !!scope(scope: !15),
+], srcLocs: [
+  !!srcLoc(line: 0, scope: 0),
+  !!srcLoc(line: 12, scope: 0),
+  !!srcLoc(line: 13, scope: 0),
+  !!srcLoc(line: 14, column: 7, scope: 1),
+  !!srcLoc(line: 15, scope: 1),
+  !!srcLoc(line: 16, column: 12, scope: 0),
+])
+!14 = distinct !DISubprogram(name: "bar", scope: !1, file: !1, line: 12, type: !10, scopeLine: 12, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
+!15 = distinct !DILexicalBlock(scope: !14, file: !1, line: 13)
+!16 = distinct !MDFunctionLocalContext(scopes: [
+  !!scope(scope: !17),
+  !!scope(scope: !18),
+], srcLocs: [
+  !!srcLoc(line: 0, scope: 0),
+  !!srcLoc(line: 22, scope: 0),
+  !!srcLoc(line: 23, scope: 1),
+], inlinedCalls: [
+  !!inlinedCall(srcLoc: 1, inlineeContext: !12),
+])
+!17 = distinct !DISubprogram(name: "qux", scope: !1, file: !1, line: 22, type: !10, scopeLine: 22, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
+!18 = !DILexicalBlock(scope: !17, file: !1, line: 23)
+!19 = distinct !MDFunctionLocalContext(nonDebug: true, inlinedCalls: [
+  !!inlinedCall(inlineeContext: !12),
+  !!inlinedCall(srcLoc: 2, inlinedAt: 0, inlineeContext: !13),
+])

>From f6a8dc63fee89377021fa2d68ec5fc01a241bdc9 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Thu, 29 Oct 2026 15:05:54 +0000
Subject: [PATCH 2/2] clang-format

---
 llvm/lib/Bitcode/Reader/MetadataLoader.cpp  | 10 ++++++----
 llvm/lib/Bitcode/Writer/ValueEnumerator.cpp |  6 +++---
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
index 6299f7c0792d0e..944d2bdee53f91 100644
--- a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
+++ b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
@@ -2690,7 +2690,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
     (void)Version;
     // FLMD comprises 3 arrays with a leading length field.
     uint64_t CurrentRecord = 1;
-    MDFunctionLocalContext *NewFLMD = MDFunctionLocalContext::getDistinct(Context, IsNonDebug);
+    MDFunctionLocalContext *NewFLMD =
+        MDFunctionLocalContext::getDistinct(Context, IsNonDebug);
     // Assign the metadata number now, so that we don't have to use forward
     // references for ourself.
     MetadataList.assignValue(NewFLMD, NextMetadataNo);
@@ -2729,8 +2730,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
       uint32_t Line = Record[CurrentRecord++];
       uint16_t Column = Record[CurrentRecord++];
       uint16_t RawScopeIdx = Record[CurrentRecord++];
-      NewFLMD->SrcLocs.push_back(FLSrcLoc(
-        Line, Column, FLIndex<FLScope>::fromRaw(RawScopeIdx)));
+      NewFLMD->SrcLocs.push_back(
+          FLSrcLoc(Line, Column, FLIndex<FLScope>::fromRaw(RawScopeIdx)));
     }
     // InlinedCalls
     uint64_t NumInlinedCalls = Record[CurrentRecord++];
@@ -2741,7 +2742,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
     // function recurses from here or if the root array otherwise ends up being
     // written to.
     SmallVectorImpl<uint64_t> *InlinedCallFwdRefs = nullptr;
-    for (uint64_t InlinedCallIdx = 0; InlinedCallIdx < NumInlinedCalls; ++InlinedCallIdx) {
+    for (uint64_t InlinedCallIdx = 0; InlinedCallIdx < NumInlinedCalls;
+         ++InlinedCallIdx) {
       uint32_t RawSrcLocIdx = Record[CurrentRecord++];
       uint16_t RawInlinedAtIdx = Record[CurrentRecord++];
       bool Uniquable = Record[CurrentRecord++];
diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
index ee63320d74ba1d..0b5fe64ca7fb46 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -711,7 +711,7 @@ void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
   // Fetching the MDNodes used by an MDFunctionLocalContext is expensive; if we
   // need to get them at all, do so once in this map and reuse as needed.
   DenseMap<const MDFunctionLocalContext *, SmallVector<const MDNode *, 0>>
-    FLContextUsedNodes;
+      FLContextUsedNodes;
   while (!Worklist.empty()) {
     const MDNode *N = Worklist.back().first;
     size_t &OpNum = Worklist.back().second;
@@ -743,7 +743,7 @@ void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
       // operand count. For now, we have an assert to prevent a silent fail from
       // being introduced.
       assert(N->getNumOperands() == 0 &&
-        "MDFunctionLocalContext expected to have no operands");
+             "MDFunctionLocalContext expected to have no operands");
       auto Result = FLContextUsedNodes.try_emplace(FLContext);
       auto &UsedMDNodes = Result.first->second;
       if (Result.second)
@@ -764,7 +764,7 @@ void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
       }
     } else {
       assert(OpNum <= N->getNumOperands() &&
-        "Visited past the end of N's operands");
+             "Visited past the end of N's operands");
     }
 
     // All the operands have been visited.  Now assign an ID.



More information about the llvm-branch-commits mailing list