[clang] 71fef6d - [clang][DebugInfo] Store current location metadata in CGDebugInfo (#193918)
via cfe-commits
cfe-commits at lists.llvm.org
Fri May 8 00:04:39 PDT 2026
Author: Cullen Rhodes
Date: 2026-05-08T08:04:34+01:00
New Revision: 71fef6d5a306d1adf8bf7d30d2fe9e286380fecf
URL: https://github.com/llvm/llvm-project/commit/71fef6d5a306d1adf8bf7d30d2fe9e286380fecf
DIFF: https://github.com/llvm/llvm-project/commit/71fef6d5a306d1adf8bf7d30d2fe9e286380fecf.diff
LOG: [clang][DebugInfo] Store current location metadata in CGDebugInfo (#193918)
EmitLocation and related functions are particularly hot and rederive
current source location metadata (line, column, file). Storing this
metadata when updating the current location in CGDebugInfo::setLocation
and reusing is a nice compile-time improvement on debug builds:
CTMark geomean:
- stage1-ReleaseLTO-g: -0.65%
- stage1-O0-g: -3.48%
- stage1-aarch64-O0-g: -2.82%
- stage2-O0-g: -3.53%
http://llvm-compile-time-tracker.com/compare.php?from=99c9a1f566df3ab4f37e156b62afd1d743882de0&to=78d281116ba7ee7e6c13625906be325b6495205a&stat=instructions%3Au
Assisted-by: codex
Added:
Modified:
clang/lib/CodeGen/CGDebugInfo.cpp
clang/lib/CodeGen/CGDebugInfo.h
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 641ba32dfc224..bfff3d9953471 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -352,8 +352,23 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
if (Loc.isInvalid())
return;
- CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(
- getMacroDebugLoc(CGM, Loc));
+ SourceManager &SM = CGM.getContext().getSourceManager();
+ SourceLocation NewLoc = SM.getExpansionLoc(getMacroDebugLoc(CGM, Loc));
+ if (CurLoc != NewLoc) {
+ CurLoc = NewLoc;
+ CurLocFile = nullptr;
+ CurLocLine = 0;
+ CurLocColumn = 0;
+
+ PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
+ if (PCLoc.isInvalid())
+ return;
+
+ CurLocLine = PCLoc.getLine();
+ if (CGM.getCodeGenOpts().DebugColumnInfo)
+ CurLocColumn = PCLoc.getColumn();
+ CurLocFile = getOrCreateFile(CurLoc);
+ }
// If we've changed files in the middle of a lexical scope go ahead
// and create a new lexical scope with file node if it's
diff erent
@@ -361,21 +376,19 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
if (LexicalBlockStack.empty())
return;
- SourceManager &SM = CGM.getContext().getSourceManager();
auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
- PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
- if (PCLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurLoc))
+ if (!CurLocFile || Scope->getFile() == CurLocFile)
return;
if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
LexicalBlockStack.pop_back();
- LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
- LBF->getScope(), getOrCreateFile(CurLoc)));
+ LexicalBlockStack.emplace_back(
+ DBuilder.createLexicalBlockFile(LBF->getScope(), CurLocFile));
} else if (isa<llvm::DILexicalBlock>(Scope) ||
isa<llvm::DISubprogram>(Scope)) {
LexicalBlockStack.pop_back();
LexicalBlockStack.emplace_back(
- DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
+ DBuilder.createLexicalBlockFile(Scope, CurLocFile));
}
}
@@ -582,7 +595,11 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
FileName = TheCU->getFile()->getFilename();
CSInfo = TheCU->getFile()->getChecksum();
} else {
- PresumedLoc PLoc = SM.getPresumedLoc(getMacroDebugLoc(CGM, Loc));
+ Loc = getMacroDebugLoc(CGM, Loc);
+ if (Loc == CurLoc && CurLocFile)
+ return CurLocFile;
+
+ PresumedLoc PLoc = SM.getPresumedLoc(Loc);
FileName = PLoc.getFilename();
if (FileName.empty()) {
@@ -665,20 +682,25 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
if (Loc.isInvalid())
return 0;
SourceManager &SM = CGM.getContext().getSourceManager();
- return SM.getPresumedLoc(getMacroDebugLoc(CGM, Loc)).getLine();
+ SourceLocation DebugLoc = getMacroDebugLoc(CGM, Loc);
+ if (DebugLoc == CurLoc)
+ return CurLocLine;
+ return SM.getPresumedLoc(DebugLoc).getLine();
}
-unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
+unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
// We may not want column information at all.
- if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo)
+ if (!CGM.getCodeGenOpts().DebugColumnInfo)
return 0;
// If the location is invalid then use the current column.
if (Loc.isInvalid() && CurLoc.isInvalid())
return 0;
SourceManager &SM = CGM.getContext().getSourceManager();
- PresumedLoc PLoc =
- SM.getPresumedLoc(Loc.isValid() ? getMacroDebugLoc(CGM, Loc) : CurLoc);
+ SourceLocation DebugLoc = Loc.isValid() ? getMacroDebugLoc(CGM, Loc) : CurLoc;
+ if (DebugLoc == CurLoc)
+ return CurLocColumn;
+ PresumedLoc PLoc = SM.getPresumedLoc(DebugLoc);
return PLoc.isValid() ? PLoc.getColumn() : 0;
}
@@ -4946,7 +4968,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
isa<VarDecl>(D) || isa<CapturedDecl>(D)) {
Flags |= llvm::DINode::FlagArtificial;
// Artificial functions should not silently reuse CurLoc.
- CurLoc = SourceLocation();
+ clearCurLoc();
}
if (CurFuncIsThunk)
@@ -5032,7 +5054,7 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc,
Flags |= llvm::DINode::FlagArtificial;
// Artificial functions without a location should not silently reuse CurLoc.
if (Loc.isInvalid())
- CurLoc = SourceLocation();
+ clearCurLoc();
}
unsigned LineNo = getLineNumber(Loc);
unsigned ScopeLine = 0;
@@ -5146,9 +5168,8 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
return;
llvm::MDNode *Scope = LexicalBlockStack.back();
- Builder.SetCurrentDebugLocation(
- llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(CurLoc),
- getColumnNumber(CurLoc), Scope, CurInlinedAt));
+ Builder.SetCurrentDebugLocation(llvm::DILocation::get(
+ CGM.getLLVMContext(), CurLocLine, CurLocColumn, Scope, CurInlinedAt));
}
void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
diff --git a/clang/lib/CodeGen/CGDebugInfo.h b/clang/lib/CodeGen/CGDebugInfo.h
index f4b5471648399..62b1afa224ffc 100644
--- a/clang/lib/CodeGen/CGDebugInfo.h
+++ b/clang/lib/CodeGen/CGDebugInfo.h
@@ -69,6 +69,9 @@ class CGDebugInfo {
ModuleMap *ClangModuleMap = nullptr;
ASTSourceDescriptor PCHDescriptor;
SourceLocation CurLoc;
+ llvm::DIFile *CurLocFile = nullptr;
+ unsigned CurLocLine = 0;
+ unsigned CurLocColumn = 0;
llvm::MDNode *CurInlinedAt = nullptr;
llvm::DIType *VTablePtrType = nullptr;
llvm::DIType *ClassTy = nullptr;
@@ -876,8 +879,15 @@ class CGDebugInfo {
/// Get column number for the location. If location is
/// invalid then use current location.
- /// \param Force Assume DebugColumnInfo option is true.
- unsigned getColumnNumber(SourceLocation Loc, bool Force = false);
+ unsigned getColumnNumber(SourceLocation Loc);
+
+ /// Clear the current location and its derived metadata.
+ void clearCurLoc() {
+ CurLoc = SourceLocation();
+ CurLocFile = nullptr;
+ CurLocLine = 0;
+ CurLocColumn = 0;
+ }
/// Collect various properties of a FunctionDecl.
/// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl.
More information about the cfe-commits
mailing list