[llvm-branch-commits] [llvm] [DebugInfo][FLMD] Add classes for function-local DebugLocs (PR #244622)
Stephen Tozer via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Nov 23 06:54:45 PST 2026
https://github.com/SLTozer updated https://github.com/llvm/llvm-project/pull/244622
>From caa8ba17238bbb5720afa9a11a77a63dc08d2342 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Thu, 19 Nov 2026 16:41:35 +0000
Subject: [PATCH] [FLMD][DebugInfo] Add classes for function-local DebugLocs
This patch adds two new classes. The first, FLDebugLoc, represents a
DebugLoc in a function-local context; combined with the correct
`!MDFunctionLocalContext`, this class is equivalent in information to a
`!DILocation`, with some exceptions (IsImplicitCode is not included,
IRLayers are to-be-implemented). The second, DIDbgLoc, is a new
MDNode type which wraps an `MDFunctionLocalContext*` and an `FLDebugLoc`.
This makes it an effective substitute for `DILocation` in metadata,
able to replicate the interface of `DILocation` entirely. Implementation
of this interface is added in a later patch.
---
llvm/include/llvm-c/DebugInfo.h | 1 +
llvm/include/llvm/IR/DebugInfoMetadata.h | 79 +++++++
llvm/include/llvm/IR/DebugLoc.h | 194 ++++++++++++++++++
llvm/include/llvm/IR/FunctionLocalMetadata.h | 9 +
llvm/include/llvm/IR/Metadata.def | 1 +
llvm/lib/AsmParser/LLParser.cpp | 4 +
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 8 +
llvm/lib/IR/AsmWriter.cpp | 5 +
llvm/lib/IR/DebugInfo.cpp | 6 +-
llvm/lib/IR/DebugInfoMetadata.cpp | 36 ++++
llvm/lib/IR/LLVMContextImpl.h | 17 ++
llvm/lib/IR/Verifier.cpp | 10 +
.../DirectX/DXILWriter/DXILBitcodeWriter.cpp | 4 +
llvm/unittests/IR/DebugInfoTest.cpp | 125 +++++++++++
14 files changed, 496 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm-c/DebugInfo.h b/llvm/include/llvm-c/DebugInfo.h
index 14ce02ea315f25..6aee40c8d34e03 100644
--- a/llvm/include/llvm-c/DebugInfo.h
+++ b/llvm/include/llvm-c/DebugInfo.h
@@ -216,6 +216,7 @@ enum {
LLVMDILayerLocMetadataKind,
LLVMDILayerLocListMetadataKind,
LLVMMDFunctionLocalContextMetadataKind,
+ LLVMDIDbgLocMetadataKind,
};
typedef unsigned LLVMMetadataKind;
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index 1503ee89a7ca05..3bc97c51eae122 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -23,6 +23,7 @@
#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DbgVariableFragmentInfo.h"
+#include "llvm/IR/FunctionLocalMetadata.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/PseudoProbe.h"
#include "llvm/Support/Casting.h"
@@ -3104,6 +3105,84 @@ class DILocation : public MDNode {
}
};
+class DIDbgLoc : public MDNode {
+ friend class LLVMContextImpl;
+ friend class MDNode;
+
+ DIDbgLoc(LLVMContext &C, StorageType Storage, Metadata *FLContext,
+ FLIndex<FLSrcLoc> SrcLocIdx, FLIndex<FLInlinedCall> InlinedAt,
+ uint16_t AtomGroup, uint8_t AtomRank);
+ DIDbgLoc(LLVMContext &C, StorageType Storage, Metadata *FLContext,
+ FLDebugLoc DL)
+ : DIDbgLoc(C, Storage, FLContext, DL.SrcLocIdx, DL.InlinedAtIdx,
+ DL.AtomGroup, DL.AtomRank) {}
+ ~DIDbgLoc() { dropAllReferences(); }
+
+ uint16_t AtomGroup : 13;
+ uint16_t AtomRank : 3;
+
+ LLVM_ABI static DIDbgLoc *getImpl(LLVMContext &Context,
+ MDFunctionLocalContext *FLContext,
+ FLDebugLoc DL, StorageType Storage,
+ bool ShouldCreate = true) {
+ return getImpl(Context, static_cast<Metadata *>(FLContext), DL.SrcLocIdx,
+ DL.InlinedAtIdx, DL.AtomGroup, DL.AtomRank, Storage,
+ ShouldCreate);
+ }
+
+ LLVM_ABI static DIDbgLoc *getImpl(LLVMContext &Context, Metadata *FLContext,
+ FLDebugLoc DL, StorageType Storage,
+ bool ShouldCreate = true) {
+ return getImpl(Context, FLContext, DL.SrcLocIdx, DL.InlinedAtIdx,
+ DL.AtomGroup, DL.AtomRank, Storage, ShouldCreate);
+ }
+
+ LLVM_ABI static DIDbgLoc *getImpl(LLVMContext &Context, Metadata *FLContext,
+ FLIndex<FLSrcLoc> SrcLocIdx,
+ FLIndex<FLInlinedCall> InlinedAt,
+ uint16_t AtomGroup, uint8_t AtomRank,
+ StorageType Storage,
+ bool ShouldCreate = true);
+
+ TempDIDbgLoc cloneImpl() const {
+ return getTemporary(getContext(), getFLContext(), getFLDebugLoc());
+ }
+
+public:
+ DEFINE_MDNODE_GET(DIDbgLoc,
+ (MDFunctionLocalContext * FLContext, FLDebugLoc DL),
+ (FLContext, DL))
+ DEFINE_MDNODE_GET(DIDbgLoc, (Metadata * FLContext, FLDebugLoc DL),
+ (FLContext, DL))
+ DEFINE_MDNODE_GET(DIDbgLoc,
+ (Metadata * FLContext, FLIndex<FLSrcLoc> SrcLoc,
+ FLIndex<FLInlinedCall> InlinedAt, uint16_t AtomGroup,
+ uint8_t AtomRank),
+ (FLContext, SrcLoc, InlinedAt, AtomGroup, AtomRank))
+
+ TempDIDbgLoc clone() const { return cloneImpl(); }
+
+ FLIndex<FLSrcLoc> getSrcLoc() const {
+ return FLIndex<FLSrcLoc>::fromRaw(SubclassData32);
+ }
+ FLIndex<FLInlinedCall> getInlinedAt() const {
+ return FLIndex<FLInlinedCall>::fromRaw(SubclassData16);
+ }
+ uint16_t getAtomGroup() const { return AtomGroup; }
+ uint8_t getAtomRank() const { return AtomRank; }
+ FLDebugLoc getFLDebugLoc() const {
+ return FLDebugLoc(getSrcLoc(), getInlinedAt(), AtomGroup, AtomRank);
+ }
+ Metadata *getRawFLContext() const { return getOperand(0); }
+ MDFunctionLocalContext *getFLContext() const {
+ return cast<MDFunctionLocalContext>(getOperand(0));
+ }
+
+ static bool classof(const Metadata *MD) {
+ return MD->getMetadataID() == DIDbgLocKind;
+ }
+};
+
class DILexicalBlockBase : public DILocalScope {
protected:
LLVM_ABI DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
diff --git a/llvm/include/llvm/IR/DebugLoc.h b/llvm/include/llvm/IR/DebugLoc.h
index b0caec891f4205..b78bdefade61ee 100644
--- a/llvm/include/llvm/IR/DebugLoc.h
+++ b/llvm/include/llvm/IR/DebugLoc.h
@@ -117,6 +117,200 @@ using DebugLocRef = DILocAndCoverageTracking;
using DebugLocRef = DILocation *;
#endif // LLVM_ENABLE_DEBUGLOC_TRACKING_COVERAGE
+/// Represents a DebugLoc in Function-Local form, where it is defined primarily
+/// by Function-Local Metadata references, and thus requires an
+/// MDFunctionLocalContext* to resolve to its contents.
+/// FLDebugLoc has 3 possible kinds:
+/// - `Empty`, no SrcLocIdx, no InlinedAtIdx: represents the absence of a
+/// location. Must have an atom group/rank of 0.
+/// - `InstrLoc`, has SrcLocIdx, optional InlinedAtIdx: represents a "normal"
+/// DebugLoc that may be attached to e.g. an instruction, record, or loopMD.
+/// May have a valid atom group/rank.
+/// - `InlinedCallLoc`, no SrcLocIdx, has InlinedAtIdx: represents an inlined
+/// call, which it references via InlinedAtIdx. This is the kind of an
+/// FLDebugLoc returned by `getInlinedAt()`, and it may not be attached to an
+/// instruction or other end-user. Must have an atom group/rank of 0.
+struct FLDebugLoc {
+ FLIndex<FLSrcLoc> SrcLocIdx;
+ FLIndex<FLInlinedCall> InlinedAtIdx;
+ uint16_t AtomGroup : 13;
+ uint16_t AtomRank : 3;
+
+ /// Default Empty-kind constructor.
+ FLDebugLoc() : SrcLocIdx(), InlinedAtIdx(), AtomGroup(0), AtomRank(0) {}
+ /// Constructor with arguments for an InstrLoc-kind FLDebugLoc.
+ FLDebugLoc(FLIndex<FLSrcLoc> SrcLocIdx, FLIndex<FLInlinedCall> InlinedAtIdx,
+ uint16_t AtomGroup = 0, uint8_t AtomRank = 0)
+ : SrcLocIdx(SrcLocIdx), InlinedAtIdx(InlinedAtIdx), AtomGroup(AtomGroup),
+ AtomRank(AtomRank) {
+ assert((SrcLocIdx || !AtomGroup && !AtomRank) &&
+ "Only FLDebugLoc with a valid SrcLoc may have an atom.");
+ }
+ /// Constructor with arguments for an InlinedCall-kind FLDebugLoc.
+ FLDebugLoc(FLIndex<FLInlinedCall> InlinedCallIdx)
+ : SrcLocIdx(), InlinedAtIdx(InlinedCallIdx), AtomGroup(0), AtomRank(0) {}
+
+ bool operator==(const FLDebugLoc &Other) const {
+ return asRawInt() == Other.asRawInt();
+ }
+ bool operator!=(const FLDebugLoc &Other) const {
+ return asRawInt() != Other.asRawInt();
+ }
+ bool operator<(const FLDebugLoc &Other) const {
+ return asRawInt() < Other.asRawInt();
+ }
+
+ // Used primarily for hashing/comparison; more efficient than unpacking
+ // fields/bitfields individually.
+ uint64_t asRawInt() const {
+ static_assert(sizeof(*this) == sizeof(uint64_t));
+ uint64_t Result;
+ std::memcpy(&Result, this, sizeof(Result));
+ return Result;
+ }
+ static FLDebugLoc fromRawInt(uint64_t Raw) {
+ static_assert(sizeof(FLDebugLoc) == sizeof(uint64_t));
+ FLDebugLoc Result;
+ std::memcpy(&Result, &Raw, sizeof(Result));
+ return Result;
+ }
+
+ //////////////////////////////////////////////////////////////////////////////
+ // Standalone methods (no context required)
+
+ /// An FLDebugLoc is empty iff !SrcLocIdx and !InlinedAtIdx; but since
+ /// !SrcLocIdx implies !AtomGroup and !AtomRank, we can perform a more
+ /// efficient check against `asRawInt()`.
+ explicit operator bool() const { return asRawInt(); }
+ bool isEmpty() const { return !*this; }
+ bool isInlinedInstrLoc() const { return InlinedAtIdx && SrcLocIdx; }
+ bool isNonInlinedInstrLoc() const { return !InlinedAtIdx && SrcLocIdx; }
+ bool isInstrLoc() const { return (bool)SrcLocIdx; }
+ bool isInlinedCall() const { return InlinedAtIdx && !SrcLocIdx; }
+
+ /// Returns the index of the InlinedCall that this FLDebugLoc represents; only
+ /// valid for InlinedCallLocs.
+ FLIndex<FLInlinedCall> getIdxForInlinedCall() const {
+ assert(isInlinedCall() &&
+ "can only getIdxForInlinedCall for an inlined call");
+ return InlinedAtIdx;
+ }
+
+ /// Returns the InlinedCall index that this loc was inlined at, without the
+ /// need for a context; invalid for an InlinedCallLoc.
+ FLIndex<FLInlinedCall> getInstrInlinedAt() const {
+ assert(!isInlinedCall() &&
+ "getInstrInlinedAt is invalid for InlinedCallLocs");
+ return InlinedAtIdx;
+ }
+
+ //////////////////////////////////////////////////////////////////////////////
+ // Context methods
+
+ /// Methods for InlinedCallLocs
+
+ /// Where this loc is an InlinedCall, return the FLDebugLoc for the call's
+ /// original source location (minus any atom). Note that this removes the
+ /// distinction between distinct inlined calls with identical source
+ /// locations, meaning it is possible to have:
+ /// D1.getInlinedAt() != D2->getInlinedAt() &&
+ /// D1.getInlinedAt().getLocForInlinedCall() ==
+ /// D2.getInlinedAt().getLocForInlinedCall();
+ FLDebugLoc getLocForInlinedCall(MDFunctionLocalContext *Context) const {
+ assert(isInlinedCall() &&
+ "getLocForInlinedCallLoc requires an inlined call");
+ FLInlinedCall InlinedCall = Context->getInlinedCall(InlinedAtIdx);
+ assert((!InlinedCall.InlinedAtIdx ||
+ InlinedCall.InlinedAtIdx.get() < InlinedAtIdx.get()) &&
+ "cyclic inlined call can't be handled");
+ return FLDebugLoc(InlinedCall.SrcLocIdx, InlinedCall.InlinedAtIdx);
+ }
+ /// Where this loc is an InlinedCall, return the FLInlinedCall that it
+ /// corresponds to.
+ FLInlinedCall asInlinedCall(MDFunctionLocalContext *Context) const {
+ assert(isInlinedCall() && "asInlinedCall called for non-InlinedCallLoc.");
+ return Context->getInlinedCall(InlinedAtIdx);
+ }
+ /// This is an awkward special case, where when we inlined DebugLocs into a
+ /// nodebug function, we create a special inlined call at the end of the
+ /// inline chain; this call has no SrcLocIdx, meaning some methods (such as
+ /// `getSrcLoc()`) will fail, similar to being called on an empty loc.
+ /// Fortunately, we more-or-less never try to handle such locs, as we ignore
+ /// DebugLocs inside of a nodebug function until it's time to inline them,
+ /// where this case is handled.
+ bool isNoDebugInlinedCall(MDFunctionLocalContext *Context) const {
+ return isInlinedCall() && !asInlinedCall(Context).SrcLocIdx;
+ }
+
+ /// Context data access methods; in general, these methods are used to fulfill
+ /// DebugLoc methods, e.g. getLine() => getSrcLoc(Ctx).Line.
+
+ FLDebugLoc getInlinedAt(MDFunctionLocalContext *Context) const {
+ if (isInlinedCall())
+ return FLDebugLoc(getLocForInlinedCall(Context).InlinedAtIdx);
+ return FLDebugLoc(InlinedAtIdx);
+ }
+ FLIndex<FLInlinedCall>
+ getInlinedAtIdx(MDFunctionLocalContext *Context) const {
+ if (isInlinedCall())
+ return getLocForInlinedCall(Context).InlinedAtIdx;
+ return InlinedAtIdx;
+ }
+ FLSrcLoc getSrcLoc(MDFunctionLocalContext *Context) const {
+ if (isInlinedCall())
+ return getLocForInlinedCall(Context).getSrcLoc(Context);
+ return Context->getSrcLoc(SrcLocIdx, InlinedAtIdx);
+ }
+ DILocalScope *getScope(MDFunctionLocalContext *Context) const {
+ if (isInlinedCall())
+ return getLocForInlinedCall(Context).getScope(Context);
+ FLSrcLoc SrcLoc = getSrcLoc(Context);
+ return Context->getScope(SrcLoc.ScopeIdx, InlinedAtIdx);
+ }
+
+ /// Returns the FunctionLocalContext which this FLDebugLoc represents a
+ /// location in, which is either the InlineeContext of the InlinedCall this
+ /// location immediately belongs to, or just Context if this is not inlined.
+ MDFunctionLocalContext *getInstrSrcContext(MDFunctionLocalContext *Context) {
+ FLDebugLoc InlinedAt = getInlinedAt(Context);
+ if (!InlinedAt)
+ return Context;
+ return InlinedAt.asInlinedCall(Context).InlineeContext;
+ }
+ /// Returns false if this loc contains indices that, when resolved against the
+ /// given \p Context, are invalid; returns true otherwise.
+ /// If false, then methods that read into \p Context may fail.
+ bool isValidInContext(MDFunctionLocalContext *Context) const {
+ if (InlinedAtIdx && InlinedAtIdx.get() >= Context->getInlinedCalls().size())
+ return false;
+ if (isInlinedCall()) {
+ FLInlinedCall IC = asInlinedCall(Context);
+ if (IC.InlinedAtIdx &&
+ IC.InlinedAtIdx.get() >= Context->getInlinedCalls().size())
+ return false;
+ if (IC.SrcLocIdx &&
+ !Context->isValidSrcLoc(IC.SrcLocIdx, IC.InlinedAtIdx))
+ return false;
+ return true;
+ }
+ // Empty locations have no indices that can be invalid.
+ if (!SrcLocIdx)
+ return true;
+ return Context->isValidSrcLoc(SrcLocIdx, InlinedAtIdx);
+ }
+};
+
+inline hash_code hash_value(const FLDebugLoc &Val) {
+ return hash_value(Val.asRawInt());
+}
+template <> struct DenseMapInfo<FLDebugLoc> {
+ static unsigned getHashValue(FLDebugLoc DL) { return hash_value(DL); }
+
+ static bool isEqual(FLDebugLoc LHS, FLDebugLoc RHS) {
+ return LHS.asRawInt() == RHS.asRawInt();
+ }
+};
+
/// A debug info location.
///
/// This class is a wrapper around an \a DILocation
diff --git a/llvm/include/llvm/IR/FunctionLocalMetadata.h b/llvm/include/llvm/IR/FunctionLocalMetadata.h
index 51ea88e2038d7c..324738601310c6 100644
--- a/llvm/include/llvm/IR/FunctionLocalMetadata.h
+++ b/llvm/include/llvm/IR/FunctionLocalMetadata.h
@@ -315,6 +315,15 @@ class MDFunctionLocalContext : public MDNode {
return SrcLocs[Idx.get()];
return InlinedCalls[InlinedAt.get()].getInlinee()->getSrcLoc(Idx);
}
+ bool isValidSrcLoc(FLIndex<FLSrcLoc> Idx,
+ FLIndex<FLInlinedCall> InlinedAt) const {
+ if (!Idx)
+ return false;
+ if (!InlinedAt)
+ return Idx.get() < SrcLocs.size();
+ return Idx.get() <
+ InlinedCalls[InlinedAt.get()].InlineeContext->SrcLocs.size();
+ }
FLInlinedCall getInlinedCall(FLIndex<FLInlinedCall> Idx) const {
assert(Idx && "Attempted to fetch an InlinedCall with an empty index!");
return InlinedCalls[Idx.get()];
diff --git a/llvm/include/llvm/IR/Metadata.def b/llvm/include/llvm/IR/Metadata.def
index 7362784f11f4f4..2e842c82730460 100644
--- a/llvm/include/llvm/IR/Metadata.def
+++ b/llvm/include/llvm/IR/Metadata.def
@@ -124,6 +124,7 @@ HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIGenericSubrange)
HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DISubrangeType)
HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIFixedPointType)
HANDLE_SPECIALIZED_MDNODE_LEAF(MDFunctionLocalContext)
+HANDLE_SPECIALIZED_MDNODE_LEAF_UNIQUABLE(DIDbgLoc)
#undef HANDLE_METADATA
#undef HANDLE_METADATA_LEAF
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 32f0d1303a8e7e..5b1bd1a5d0d50d 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -6109,6 +6109,10 @@ bool LLParser::parseDILayerLocList(MDNode *&Result, bool IsDistinct) {
return false;
}
+bool LLParser::parseDIDbgLoc(MDNode *&Result, bool IsDistinct) {
+ llvm_unreachable("not implemented");
+}
+
/// parseDIAssignID:
/// ::= distinct !DIAssignID()
bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) {
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 0b94c1bcc46867..bff9f795d9208a 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -337,6 +337,8 @@ class ModuleBitcodeWriter : public ModuleBitcodeWriterBase {
unsigned Abbrev);
void writeDILayerLocList(const DILayerLocList *N,
SmallVectorImpl<uint64_t> &Record, unsigned Abbrev);
+ void writeDIDbgLoc(const DIDbgLoc *N, SmallVectorImpl<uint64_t> &Record,
+ unsigned &Abbrev);
void writeMDFunctionLocalContext(const MDFunctionLocalContext *N,
SmallVectorImpl<uint64_t> &Record,
unsigned &Abbrev);
@@ -2016,6 +2018,12 @@ void ModuleBitcodeWriter::writeDILayerLocList(const DILayerLocList *N,
Record.clear();
}
+void ModuleBitcodeWriter::writeDIDbgLoc(const DIDbgLoc *N,
+ SmallVectorImpl<uint64_t> &Record,
+ unsigned &Abbrev) {
+ llvm_unreachable("not implemented");
+}
+
unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() {
// Assume the column is usually under 128, and always output the inlined-at
// location (it's never more expensive than building an array size 1).
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 2bec2215834d04..309195052b6d52 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -2237,6 +2237,11 @@ static void writeMDFunctionLocalContext(raw_ostream &Out,
Out << ")";
}
+static void writeDIDbgLoc(raw_ostream &Out, const DIDbgLoc *DL,
+ AsmWriterContext &WriterCtx) {
+ llvm_unreachable("not implemented");
+}
+
static void writeDILocation(raw_ostream &Out, const DILocation *DL,
AsmWriterContext &WriterCtx) {
Out << "!DILocation(";
diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
index 8702755e00a1db..13c7fbbba334aa 100644
--- a/llvm/lib/IR/DebugInfo.cpp
+++ b/llvm/lib/IR/DebugInfo.cpp
@@ -566,7 +566,7 @@ static bool isDILocationReachable(SmallPtrSetImpl<Metadata *> &Visited,
MDNode *N = dyn_cast_or_null<MDNode>(MD);
if (!N)
return false;
- if (isa<DILocation>(N) || Reachable.count(N))
+ if (isa<DILocation>(N) || isa<DIDbgLoc>(N) || Reachable.count(N))
return true;
if (!Visited.insert(N).second)
return false;
@@ -588,7 +588,7 @@ static bool isAllDILocation(SmallPtrSetImpl<Metadata *> &Visited,
MDNode *N = dyn_cast_or_null<MDNode>(MD);
if (!N)
return false;
- if (isa<DILocation>(N) || AllDILocation.count(N))
+ if (isa<DILocation>(N) || isa<DIDbgLoc>(N) || AllDILocation.count(N))
return true;
if (!DIReachable.count(N))
return false;
@@ -609,7 +609,7 @@ static bool isAllDILocation(SmallPtrSetImpl<Metadata *> &Visited,
static Metadata *
stripLoopMDLoc(const SmallPtrSetImpl<Metadata *> &AllDILocation,
const SmallPtrSetImpl<Metadata *> &DIReachable, Metadata *MD) {
- if (isa<DILocation>(MD) || AllDILocation.count(MD))
+ if (isa<DILocation>(MD) || isa<DIDbgLoc>(MD) || AllDILocation.count(MD))
return nullptr;
if (!DIReachable.count(MD))
diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp
index 057b425542e77e..07161e69b49ca8 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -177,6 +177,42 @@ DILocation *DILocation::getMergedLocations(ArrayRef<DILocation *> Locs) {
return Merged;
}
+DIDbgLoc::DIDbgLoc(LLVMContext &C, StorageType Storage, Metadata *FLContext,
+ FLIndex<FLSrcLoc> SrcLocIdx,
+ FLIndex<FLInlinedCall> InlinedAt, uint16_t AtomGroup,
+ uint8_t AtomRank)
+ : MDNode(C, DIDbgLocKind, Storage, FLContext), AtomGroup(AtomGroup),
+ AtomRank(AtomRank) {
+ assert(AtomRank <= 7 && "AtomRank number should fit in 3 bits");
+ assert(AtomGroup < (1 << 13) && "AtomGroup number should fit in 13 bits");
+
+ SubclassData32 = SrcLocIdx.asRaw();
+ SubclassData16 = InlinedAt.asRaw();
+}
+
+DIDbgLoc *DIDbgLoc::getImpl(LLVMContext &Context, Metadata *FLContext,
+ FLIndex<FLSrcLoc> SrcLoc,
+ FLIndex<FLInlinedCall> InlinedAt,
+ uint16_t AtomGroup, uint8_t AtomRank,
+ StorageType Storage, bool ShouldCreate) {
+ if (Storage == Uniqued) {
+ if (auto *N = getUniqued(
+ Context.pImpl->DIDbgLocs,
+ DIDbgLocInfo::KeyTy(
+ FLContext, FLDebugLoc(SrcLoc, InlinedAt, AtomGroup, AtomRank))))
+ return N;
+ if (!ShouldCreate)
+ return nullptr;
+ } else {
+ assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
+ }
+
+ return storeImpl(new (1, Storage)
+ DIDbgLoc(Context, Storage, FLContext, SrcLoc, InlinedAt,
+ AtomGroup, AtomRank),
+ Storage, Context.pImpl->DIDbgLocs);
+}
+
static DILexicalBlockBase *cloneAndReplaceParentScope(DILexicalBlockBase *LBB,
DIScope *NewParent) {
TempMDNode ClonedScope = LBB->clone();
diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h
index 3d8e1ed4e087a0..2520b71675a230 100644
--- a/llvm/lib/IR/LLVMContextImpl.h
+++ b/llvm/lib/IR/LLVMContextImpl.h
@@ -31,6 +31,7 @@
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
+#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
@@ -366,6 +367,22 @@ template <> struct MDNodeKeyImpl<DILayerLocList> : MDNodeOpsKey {
}
};
+/// DenseMapInfo for DIDbgLoc.
+template <> struct MDNodeKeyImpl<DIDbgLoc> {
+ FLDebugLoc DL;
+ Metadata *Ctx;
+
+ MDNodeKeyImpl(Metadata *Ctx, FLDebugLoc DL) : DL(DL), Ctx(Ctx) {}
+ MDNodeKeyImpl(const DIDbgLoc *N)
+ : DL(N->getFLDebugLoc()), Ctx(N->getRawFLContext()) {}
+
+ bool isKeyOf(const DIDbgLoc *RHS) const {
+ return DL == RHS->getFLDebugLoc() && Ctx == RHS->getRawFLContext();
+ }
+
+ unsigned getHashValue() const { return hash_combine(DL, Ctx); }
+};
+
/// DenseMapInfo for GenericDINode.
template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
unsigned Tag;
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index ac89f3a4b36004..6b3bcfe91e58eb 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -1277,6 +1277,16 @@ void Verifier::visitMDFunctionLocalContext(const MDFunctionLocalContext &N) {
visitMDNode(*Node, AreDebugLocsAllowed::No);
}
+void Verifier::visitDIDbgLoc(const DIDbgLoc &N) {
+ CheckDI(N.getRawFLContext() &&
+ isa<MDFunctionLocalContext>(N.getRawFLContext()),
+ "context must be an MDFunctionLocalContext", &N, N.getRawFLContext());
+ CheckDI(N.getFLDebugLoc().isInstrLoc(),
+ "expected an instruction-location (with a valid SrcLoc)", &N);
+ CheckDI(N.getFLDebugLoc().isValidInContext(N.getFLContext()),
+ "invalid !!dbgLoc indexes into context", &N, N.getFLContext());
+}
+
void Verifier::visitDILocation(const DILocation &N) {
CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
"location requires a valid scope", &N, N.getRawScope());
diff --git a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
index 4a2667ce0ba8c1..f85f28b5ff49dd 100644
--- a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
+++ b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
@@ -326,6 +326,10 @@ class DXILBitcodeWriter {
unsigned &Abbrev) {
llvm_unreachable("DXIL cannot contain MDFunctionLocalContext Nodes");
}
+ void writeDIDbgLoc(const DIDbgLoc *N, SmallVectorImpl<uint64_t> &Record,
+ unsigned &Abbrev) {
+ llvm_unreachable("DXIL cannot contain DIDbgLoc Nodes");
+ }
unsigned createMetadataStringsAbbrev();
void writeMetadataStrings(ArrayRef<const Metadata *> Strings,
SmallVectorImpl<uint64_t> &Record);
diff --git a/llvm/unittests/IR/DebugInfoTest.cpp b/llvm/unittests/IR/DebugInfoTest.cpp
index 8df07e55002f84..df01d4c440d5e3 100644
--- a/llvm/unittests/IR/DebugInfoTest.cpp
+++ b/llvm/unittests/IR/DebugInfoTest.cpp
@@ -2026,4 +2026,129 @@ TEST(FLMDTest, VerifyFLContext) {
EXPECT_FALSE(verifyFunction(*F));
}
+TEST(FLMDTest, FLDebugLocInterface) {
+ LLVMContext Ctx;
+ auto M = std::make_unique<Module>("TestModule", Ctx);
+ DIBuilder DIB(*M);
+ DIFile *File = DIB.createFile("test.cpp", "/");
+ DICompileUnit *CU = DIB.createCompileUnit(dwarf::DW_LANG_C_plus_plus, File,
+ "test", false, "", 0);
+ DISubprogram *CallerSP = DIB.createFunction(
+ CU, "caller", "", File, 1, DIB.createSubroutineType({}), 2,
+ DINode::FlagZero, DISubprogram::SPFlagDefinition);
+ DISubprogram *CalleeSP = DIB.createFunction(
+ CU, "callee", "", File, 11, DIB.createSubroutineType({}), 11,
+ DINode::FlagZero, DISubprogram::SPFlagDefinition);
+
+ auto *CallerFLCtx = MDFunctionLocalContext::getDistinct(Ctx);
+ auto *CalleeFLCtx = MDFunctionLocalContext::getDistinct(Ctx);
+
+ // Assemble a simple valid pair of FLContexts...
+ auto *CalleeBlock = DILexicalBlock::getDistinct(Ctx, CalleeSP, File, 13, 4);
+ CalleeFLCtx->insertScope(CalleeSP);
+ CalleeFLCtx->insertScope(CalleeBlock);
+ CalleeFLCtx->insertSrcLoc(FLSrcLoc(12, 4, 0));
+ CalleeFLCtx->insertSrcLoc(FLSrcLoc(13, 13, 0));
+ CalleeFLCtx->insertSrcLoc(FLSrcLoc(14, 0, 1));
+ CalleeFLCtx->insertSrcLoc(FLSrcLoc(15, 0, 0));
+
+ auto *CallerBlock = DILexicalBlock::getDistinct(Ctx, CallerSP, File, 5, 0);
+ CallerFLCtx->insertScope(CallerSP);
+ CallerFLCtx->insertScope(CallerBlock);
+ CallerFLCtx->insertSrcLoc(FLSrcLoc(0, 0, 0));
+ CallerFLCtx->insertSrcLoc(FLSrcLoc(4, 10, 0));
+ CallerFLCtx->insertSrcLoc(FLSrcLoc(5, 3, 0));
+ CallerFLCtx->insertSrcLoc(FLSrcLoc(6, 1, 1));
+ CallerFLCtx->insertSrcLoc(FLSrcLoc(7, 7, 1));
+ CallerFLCtx->insertSrcLoc(FLSrcLoc(8, 3, 0));
+ CallerFLCtx->insertInlinedCall(FLInlinedCall(4, {}, CalleeFLCtx, false));
+ CallerFLCtx->insertInlinedCall(FLInlinedCall(3, 0, CalleeFLCtx, false));
+ CallerFLCtx->insertInlinedCall(FLInlinedCall(3, 0, CalleeFLCtx, false));
+
+ FunctionType *FTy =
+ FunctionType::get(Type::getVoidTy(Ctx), std::vector<Type *>(), false);
+ auto *F = Function::Create(FTy, Function::ExternalLinkage, "caller", *M);
+ BasicBlock::Create(Ctx, "", F);
+ ReturnInst::Create(Ctx, nullptr, &*F->begin());
+ F->setSubprogram(CallerSP);
+ F->setFLContext(CallerFLCtx);
+
+ FLDebugLoc I0(2, {}, 4, 1);
+ FLDebugLoc I1(3, {}, 5, 1);
+ FLDebugLoc I2(1, 1, 6, 2);
+ FLDebugLoc I3(2, {}, 4, 1);
+ FLDebugLoc I4(1, 2, 6, 2);
+ FLDebugLoc I5;
+
+ // Check equality between FLDebugLocs.
+ EXPECT_NE(I0, I1);
+ EXPECT_EQ(I0, I3);
+
+ // Quick check for the "kinds".
+ EXPECT_TRUE(I0.isInstrLoc());
+ EXPECT_TRUE(I2.isInstrLoc());
+ EXPECT_TRUE(I0.isNonInlinedInstrLoc());
+ EXPECT_TRUE(I2.isInlinedInstrLoc());
+
+ // getInlinedAt works for inlined and non-inlined instructions, returning an
+ // InlinedCall DebugLoc and an Empty DebugLoc respectively.
+ FLDebugLoc IC0 = I0.getInlinedAt(CallerFLCtx);
+ FLDebugLoc IC1 = I2.getInlinedAt(CallerFLCtx);
+ FLDebugLoc IC2 = I4.getInlinedAt(CallerFLCtx);
+ EXPECT_TRUE(IC0.isEmpty());
+ EXPECT_TRUE(IC1.isInlinedCall());
+
+ // An InlinedCall that has the same InlinedAtIdx field as an InstrLoc will
+ // have a different getInlinedAt result.
+ EXPECT_EQ(I2.InlinedAtIdx, IC1.InlinedAtIdx);
+ EXPECT_EQ(I2.getInstrInlinedAt(), IC1.getIdxForInlinedCall());
+ EXPECT_NE(I2.getInlinedAt(CallerFLCtx), IC1.getInlinedAt(CallerFLCtx));
+
+ // For an InstrLoc, getInlinedAtIdx is equal to getInstrInlinedAt; for an
+ // InlinedCallLoc, getInlinedAtIdx is not equal to getIdxForInlinedCall.
+ EXPECT_EQ(I2.getInstrInlinedAt(), I2.getInlinedAtIdx(CallerFLCtx));
+ EXPECT_NE(IC1.getIdxForInlinedCall(), IC1.getInlinedAtIdx(CallerFLCtx));
+
+ // Identical but distinct inlined calls are different, until we erase their
+ // identity via getLocForInlinedCall().
+ EXPECT_NE(IC1, IC2);
+ EXPECT_EQ(IC1.getLocForInlinedCall(CallerFLCtx),
+ IC2.getLocForInlinedCall(CallerFLCtx));
+ EXPECT_TRUE(IC1.asInlinedCall(CallerFLCtx)
+ .isEquivalent(IC2.asInlinedCall(CallerFLCtx)));
+
+ // Test fetching context data.
+ EXPECT_EQ(I0.getSrcLoc(CallerFLCtx).Line, (unsigned)5);
+ EXPECT_EQ(I0.getSrcLoc(CalleeFLCtx).Line, (unsigned)14);
+ // SrcLocIdx==1, InlineeCtx==CalleeFLCtx
+ EXPECT_EQ(I2.getSrcLoc(CallerFLCtx).Line, (unsigned)13);
+ // SrcLocIdx==3, InlineeCtx==CalleeFLCtx
+ EXPECT_EQ(IC1.getSrcLoc(CallerFLCtx).Line, (unsigned)15);
+ // SrcLocIdx==4, No InlineeCtx
+ EXPECT_EQ(IC1.getInlinedAt(CallerFLCtx).getSrcLoc(CallerFLCtx).Line,
+ (unsigned)7);
+ // Result of getInlinedAt has no atom group/rank.
+ EXPECT_EQ(IC1.AtomGroup, 0);
+ EXPECT_EQ(IC1.AtomRank, 0);
+
+ // Check `isValidInContext`
+ EXPECT_TRUE(I0.isValidInContext(CallerFLCtx));
+ EXPECT_TRUE(I1.isValidInContext(CallerFLCtx));
+ EXPECT_TRUE(I2.isValidInContext(CallerFLCtx));
+ EXPECT_TRUE(I3.isValidInContext(CallerFLCtx));
+ EXPECT_TRUE(I4.isValidInContext(CallerFLCtx));
+ EXPECT_TRUE(I5.isValidInContext(CallerFLCtx));
+
+ EXPECT_TRUE(IC1.isValidInContext(CallerFLCtx));
+ EXPECT_TRUE(IC2.isValidInContext(CallerFLCtx));
+ EXPECT_FALSE(IC1.isValidInContext(CalleeFLCtx));
+
+ FLDebugLoc Invalid0(6, {}); // SrcLocIdx too high
+ EXPECT_FALSE(Invalid0.isValidInContext(CallerFLCtx));
+ FLDebugLoc Invalid1(2, 3); // InlinedAtIdx too high
+ EXPECT_FALSE(Invalid1.isValidInContext(CallerFLCtx));
+ FLDebugLoc Invalid2(4, 1); // SrcLocIdx too high in inlined context
+ EXPECT_FALSE(Invalid2.isValidInContext(CallerFLCtx));
+}
+
} // end namespace
More information about the llvm-branch-commits
mailing list