Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/Frontend/Driver/CodeGenOptions.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSummaryIndex.h"
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3472,7 +3472,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
EmitTrapCall(Intrinsic::trap);
return RValue::get(nullptr);
case Builtin::BI__builtin_verbose_trap: {
llvm::DILocation *TrapLocation = Builder.getCurrentDebugLocation();
DebugLoc TrapLocation = Builder.getCurrentDebugLocation();
if (getDebugInfo()) {
TrapLocation = getDebugInfo()->CreateTrapFailureMessageFor(
TrapLocation, *E->getArg(0)->tryEvaluateString(getContext()),
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ namespace {

class DeclAsInlineDebugLocation {
CGDebugInfo *DI;
llvm::MDNode *InlinedAt;
llvm::DebugLoc InlinedAt;
std::optional<ApplyDebugLocation> Location;

public:
Expand Down
65 changes: 35 additions & 30 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
Expand Down Expand Up @@ -157,9 +158,12 @@ void ApplyDebugLocation::init(SourceLocation TemporaryLocation,

// Construct a location that has a valid scope, but no line info.
assert(!DI->LexicalBlockStack.empty());
llvm::DIScope *Scope = DI->LexicalBlockStack.back();
while (!isa<llvm::DISubprogram>(Scope))
Scope = Scope->getScope();
CGF->Builder.SetCurrentDebugLocation(
llvm::DILocation::get(DI->LexicalBlockStack.back()->getContext(), 0, 0,
DI->LexicalBlockStack.back(), DI->getInlinedAt()));
llvm::DebugLoc(0, cast<llvm::DISubprogram>(Scope)->getLocScopeIndex(
llvm::DILocScopeData(cast<llvm::DILocalScope>(DI->LexicalBlockStack.back()), DI->getInlinedAt()), true)));
}

ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
Expand Down Expand Up @@ -194,8 +198,9 @@ ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF,
}
auto &DI = *CGF.getDebugInfo();
SavedLocation = DI.getLocation();
llvm::DISubprogram *SP = CGF.Builder.GetInsertBlock()->getParent()->getSubprogram();
assert((DI.getInlinedAt() ==
CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&
CGF.Builder.getCurrentDebugLocation().getInlinedAt(SP)) &&
"CGDebugInfo and IRBuilder are out of sync");

DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn);
Expand Down Expand Up @@ -537,6 +542,12 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
return PLoc.isValid() ? PLoc.getColumn() : 0;
}

llvm::DISrcLocData CGDebugInfo::getDISrcLoc(SourceLocation Loc, bool ForceColumn) {
unsigned Line = getLineNumber(Loc);
unsigned Column = getColumnNumber(Loc, ForceColumn);
return llvm::DISrcLocData(Line, Column);
}

StringRef CGDebugInfo::getCurrentDirname() {
if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
return CGM.getCodeGenOpts().DebugCompilationDir;
Expand Down Expand Up @@ -3614,7 +3625,7 @@ llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
return DBuilder.createTempMacroFile(Parent, Line, FName);
}

llvm::DILocation *CGDebugInfo::CreateTrapFailureMessageFor(
llvm::DebugLoc CGDebugInfo::CreateTrapFailureMessageFor(
llvm::DebugLoc TrapLocation, StringRef Category, StringRef FailureMsg) {
// Create a debug location from `TrapLocation` that adds an artificial inline
// frame.
Expand All @@ -3626,9 +3637,9 @@ llvm::DILocation *CGDebugInfo::CreateTrapFailureMessageFor(
FuncName += FailureMsg;

llvm::DISubprogram *TrapSP =
createInlinedTrapSubprogram(FuncName, TrapLocation->getFile());
return llvm::DILocation::get(CGM.getLLVMContext(), /*Line=*/0, /*Column=*/0,
/*Scope=*/TrapSP, /*InlinedAt=*/TrapLocation);
createInlinedTrapSubprogram(FuncName, llvm::DILocRef(CurSP, TrapLocation)->getFile());
CurSP->FnLocScopes.push_back(llvm::DILocScopeData(TrapSP, TrapLocation));
return llvm::DebugLoc(0, CurSP->FnLocScopes.size() - 1);
}

static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) {
Expand Down Expand Up @@ -4455,6 +4466,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
if (SP && SP->isDefinition()) {
LexicalBlockStack.emplace_back(SP);
RegionMap[D].reset(SP);
CurSP = SP;
return;
}
}
Expand Down Expand Up @@ -4531,6 +4543,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc,
DeclCache[D->getCanonicalDecl()].reset(SP);

// Push the function onto the lexical block stack.
CurSP = SP;
LexicalBlockStack.emplace_back(SP);

if (HasDecl)
Expand Down Expand Up @@ -4652,7 +4665,7 @@ void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
assert(CurInlinedAt && "unbalanced inline scope stack");
EmitFunctionEnd(Builder, nullptr);
setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
setInlinedAt(CurInlinedAt.getInlinedAt(CurSP));
}

void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
Expand All @@ -4664,8 +4677,7 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {

llvm::MDNode *Scope = LexicalBlockStack.back();
Builder.SetCurrentDebugLocation(
llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(CurLoc),
getColumnNumber(CurLoc), Scope, CurInlinedAt));
CurSP->getDebugLoc(getDISrcLoc(CurLoc), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true));
}

void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Expand Down Expand Up @@ -4696,9 +4708,8 @@ void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder,
setLocation(Loc);

// Emit a line table change for the current location inside the new scope.
Builder.SetCurrentDebugLocation(llvm::DILocation::get(
CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc),
LexicalBlockStack.back(), CurInlinedAt));
Builder.SetCurrentDebugLocation(
CurSP->getDebugLoc(getDISrcLoc(CurLoc), llvm::DILocScopeData(cast<llvm::DILocalScope>(LexicalBlockStack.back()), CurInlinedAt), true));

if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
return;
Expand Down Expand Up @@ -4733,8 +4744,10 @@ void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) {
}
FnBeginRegionCount.pop_back();

if (Fn && Fn->getSubprogram())
if (Fn && Fn->getSubprogram()) {
DBuilder.finalizeSubprogram(Fn->getSubprogram());
Fn->getSubprogram()->normalizeDebugLocs(Fn);
}
}

CGDebugInfo::BlockByRefType
Expand Down Expand Up @@ -4914,9 +4927,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,

// Insert an llvm.dbg.declare into the current block.
DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
llvm::DILocation::get(CGM.getLLVMContext(), Line,
Column, Scope,
CurInlinedAt),
CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
Builder.GetInsertBlock());
}
}
Expand Down Expand Up @@ -4983,8 +4994,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
}
// Insert an llvm.dbg.declare into the current block.
DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
llvm::DILocation::get(CGM.getLLVMContext(), Line,
Column, Scope, CurInlinedAt),
CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
Builder.GetInsertBlock());

return D;
Expand Down Expand Up @@ -5089,8 +5099,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,

// Insert an llvm.dbg.declare into the current block.
DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
llvm::DILocation::get(CGM.getLLVMContext(), Line,
Column, Scope, CurInlinedAt),
CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
Builder.GetInsertBlock());

return D;
Expand Down Expand Up @@ -5136,8 +5145,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {

// Insert an llvm.dbg.label into the current block.
DBuilder.insertLabel(L,
llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
Scope, CurInlinedAt),
CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true),
Builder.GetInsertBlock()->end());
}

Expand Down Expand Up @@ -5212,8 +5220,7 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
Line, Ty, false, llvm::DINode::FlagZero, Align);

// Insert an llvm.dbg.declare into the current block.
auto DL = llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
LexicalBlockStack.back(), CurInlinedAt);
auto DL = CurSP->getDebugLoc(llvm::DISrcLocData(Line, Column), llvm::DILocScopeData(cast<llvm::DILocalScope>(LexicalBlockStack.back()), CurInlinedAt), true);
auto *Expr = DBuilder.createExpression(addr);
if (InsertPoint)
DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator());
Expand Down Expand Up @@ -5399,8 +5406,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,

// Insert an llvm.dbg.declare into the current block.
DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
llvm::DILocation::get(CGM.getLLVMContext(), line,
column, scope, CurInlinedAt),
CurSP->getDebugLoc(llvm::DISrcLocData(line, column), llvm::DILocScopeData(scope, CurInlinedAt), true),
Builder.GetInsertBlock());
}

Expand Down Expand Up @@ -5851,7 +5857,7 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
llvm::codegenoptions::DebugLineTablesOnly)
return;

llvm::DILocation *DIL = Value->getDebugLoc().get();
llvm::DILocRef DIL(*Value);
if (!DIL)
return;

Expand Down Expand Up @@ -6163,8 +6169,7 @@ llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) {
return llvm::DebugLoc();

llvm::MDNode *Scope = LexicalBlockStack.back();
return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(Loc),
getColumnNumber(Loc), Scope);
return CurSP->getDebugLoc(getDISrcLoc(Loc), llvm::DILocScopeData(cast<llvm::DILocalScope>(Scope), CurInlinedAt), true);
}

llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const {
Expand Down
21 changes: 15 additions & 6 deletions clang/lib/CodeGen/CGDebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/ValueHandle.h"
Expand Down Expand Up @@ -66,7 +67,10 @@ class CGDebugInfo {
ModuleMap *ClangModuleMap = nullptr;
ASTSourceDescriptor PCHDescriptor;
SourceLocation CurLoc;
llvm::MDNode *CurInlinedAt = nullptr;
/// The DISubprogram of the current function, if any, ignoring any inlined
/// functions.
llvm::DISubprogram *CurSP = nullptr;
llvm::DebugLoc CurInlinedAt;
llvm::DIType *VTablePtrType = nullptr;
llvm::DIType *ClassTy = nullptr;
llvm::DICompositeType *ObjTy = nullptr;
Expand Down Expand Up @@ -454,10 +458,10 @@ class CGDebugInfo {

/// Update the current inline scope. All subsequent calls to \p EmitLocation
/// will create a location with this inlinedAt field.
void setInlinedAt(llvm::MDNode *InlinedAt) { CurInlinedAt = InlinedAt; }
void setInlinedAt(llvm::DebugLoc InlinedAt) { CurInlinedAt = InlinedAt; }

/// \return the current inline scope.
llvm::MDNode *getInlinedAt() const { return CurInlinedAt; }
llvm::DebugLoc getInlinedAt() const { return CurInlinedAt; }

// Converts a SourceLocation to a DebugLoc
llvm::DebugLoc SourceLocToDebugLoc(SourceLocation Loc);
Expand Down Expand Up @@ -632,9 +636,9 @@ class CGDebugInfo {
/// `<Prefix>` is "__clang_trap_msg".
///
/// This is used to store failure reasons for traps.
llvm::DILocation *CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation,
StringRef Category,
StringRef FailureMsg);
llvm::DebugLoc CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation,
StringRef Category,
StringRef FailureMsg);

private:
/// Emit call to llvm.dbg.declare for a variable declaration.
Expand Down Expand Up @@ -819,6 +823,11 @@ class CGDebugInfo {
/// invalid then use current location.
/// \param Force Assume DebugColumnInfo option is true.
unsigned getColumnNumber(SourceLocation Loc, bool Force = false);

/// Get the LLVM SrcLoc for the location. If location is invalid then use
/// current location.
/// \param ForceColumn Assume DebugColumnInfo option is true.
llvm::DISrcLocData getDISrcLoc(SourceLocation Loc, bool ForceColumn = false);

/// Collect various properties of a FunctionDecl.
/// \param GD A GlobalDecl whose getDecl() must return a FunctionDecl.
Expand Down
9 changes: 5 additions & 4 deletions clang/lib/CodeGen/CGLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
Expand Down Expand Up @@ -395,16 +396,16 @@ SmallVector<Metadata *, 4> LoopInfo::createMetadata(
bool &HasUserTransforms) {
SmallVector<Metadata *, 3> LoopProperties;

LLVMContext &Ctx = Header->getContext();
// If we have a valid start debug location for the loop, add it.
if (StartLoc) {
LoopProperties.push_back(StartLoc.getAsMDNode());

LoopProperties.push_back(StartLoc.toMetadata(Ctx));
// If we also have a valid end debug location for the loop, add it.
if (EndLoc)
LoopProperties.push_back(EndLoc.getAsMDNode());
LoopProperties.push_back(EndLoc.toMetadata(Ctx));
}

LLVMContext &Ctx = Header->getContext();
if (Attrs.MustProgress)
LoopProperties.push_back(
MDNode::get(Ctx, MDString::get(Ctx, "llvm.loop.mustprogress")));
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CGLoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Compiler.h"

Expand Down
11 changes: 6 additions & 5 deletions llvm/include/llvm/Analysis/InlineAdvisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/Analysis/InlineCost.h"
#include "llvm/Analysis/LazyCallGraph.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/PassManager.h"
#include <memory>

Expand Down Expand Up @@ -108,7 +109,7 @@ class InlineAdvice {

/// Get the inlining recommendation.
bool isInliningRecommended() const { return IsInliningRecommended; }
const DebugLoc &getOriginalCallSiteDebugLoc() const { return DLoc; }
DILocRef getOriginalCallSiteDebugLoc() const { return DLoc; }
const BasicBlock *getOriginalCallSiteBasicBlock() const { return Block; }

protected:
Expand All @@ -125,7 +126,7 @@ class InlineAdvice {
// Capture the context of CB before inlining, as a successful inlining may
// change that context, and we want to report success or failure in the
// original context.
const DebugLoc DLoc;
DILocRef DLoc;
const BasicBlock *const Block;
OptimizationRemarkEmitter &ORE;
const bool IsInliningRecommended;
Expand Down Expand Up @@ -367,21 +368,21 @@ shouldInline(CallBase &CB, TargetTransformInfo &CalleeTTI,
OptimizationRemarkEmitter &ORE, bool EnableDeferral = true);

/// Emit ORE message.
void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
void emitInlinedInto(OptimizationRemarkEmitter &ORE, DILocRef DLoc,
const BasicBlock *Block, const Function &Callee,
const Function &Caller, bool IsMandatory,
function_ref<void(OptimizationRemark &)> ExtraContext = {},
const char *PassName = nullptr);

/// Emit ORE message based in cost (default heuristic).
void emitInlinedIntoBasedOnCost(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
void emitInlinedIntoBasedOnCost(OptimizationRemarkEmitter &ORE, DILocRef DLoc,
const BasicBlock *Block, const Function &Callee,
const Function &Caller, const InlineCost &IC,
bool ForProfileContext = false,
const char *PassName = nullptr);

/// Add location info to ORE message.
void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc);
void addLocationToRemarks(OptimizationRemark &Remark, DILocRef DLoc);

/// Set the inline-remark attribute.
void setInlineRemark(CallBase &CB, StringRef Message);
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/Analysis/LoopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class LLVM_ABI Loop : public LoopBase<BasicBlock, Loop> {
/// cannot find a terminating instruction with location information,
/// it returns an unknown location.
DebugLoc getStartLoc() const;
DILocRefWrapper getStartLocRef() const;

/// Return the source code span of the loop.
LocRange getLocRange() const;
Expand Down
Loading