Skip to content

Commit e3d8ee3

Browse files
committed
reland "[DebugInfo] Support to emit debugInfo for extern variables"
Commit d77ae15 ("[DebugInfo] Support to emit debugInfo for extern variables") added deebugInfo for extern variables for BPF target. The commit is reverted by 891e25b as the committed tests using %clang instead of %clang_cc1 causing test failed in certain scenarios as reported by Reid Kleckner. This patch fixed the tests by using %clang_cc1. Differential Revision: https://reviews.llvm.org/D71818
1 parent fb53396 commit e3d8ee3

File tree

20 files changed

+166
-7
lines changed

20 files changed

+166
-7
lines changed

clang/include/clang/AST/ASTConsumer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class ASTConsumer {
102102
/// modified by the introduction of an implicit zero initializer.
103103
virtual void CompleteTentativeDefinition(VarDecl *D) {}
104104

105+
/// CompleteExternalDeclaration - Callback invoked at the end of a translation
106+
/// unit to notify the consumer that the given external declaration should be
107+
/// completed.
108+
virtual void CompleteExternalDeclaration(VarDecl *D) {}
109+
105110
/// Callback invoked when an MSInheritanceAttr has been attached to a
106111
/// CXXRecordDecl.
107112
virtual void AssignInheritanceModel(CXXRecordDecl *RD) {}

clang/include/clang/Basic/TargetInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,9 @@ class TargetInfo : public virtual TransferrableTargetInfo,
13891389

13901390
virtual void setAuxTarget(const TargetInfo *Aux) {}
13911391

1392+
/// Whether target allows debuginfo types for decl only variables.
1393+
virtual bool allowDebugInfoForExternalVar() const { return false; }
1394+
13921395
protected:
13931396
/// Copy type and layout related info.
13941397
void copyAuxTarget(const TargetInfo *Aux);

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,9 @@ class Sema final {
668668
/// All the tentative definitions encountered in the TU.
669669
TentativeDefinitionsType TentativeDefinitions;
670670

671+
/// All the external declarations encoutered and used in the TU.
672+
SmallVector<VarDecl *, 4> ExternalDeclarations;
673+
671674
typedef LazyVector<const DeclaratorDecl *, ExternalSemaSource,
672675
&ExternalSemaSource::ReadUnusedFileScopedDecls, 2, 2>
673676
UnusedFileScopedDeclsType;

clang/lib/Basic/Targets/BPF.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
7676
return None;
7777
}
7878

79+
bool allowDebugInfoForExternalVar() const override { return true; }
80+
7981
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
8082
switch (CC) {
8183
default:

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,7 +4485,7 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
44854485

44864486
GVE = DBuilder.createGlobalVariableExpression(
44874487
DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
4488-
Var->hasLocalLinkage(),
4488+
Var->hasLocalLinkage(), true,
44894489
Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
44904490
getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
44914491
Align);
@@ -4588,10 +4588,29 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
45884588

45894589
GV.reset(DBuilder.createGlobalVariableExpression(
45904590
DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
4591-
true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
4591+
true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
45924592
TemplateParameters, Align));
45934593
}
45944594

4595+
void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
4596+
const VarDecl *D) {
4597+
assert(DebugKind >= codegenoptions::LimitedDebugInfo);
4598+
if (D->hasAttr<NoDebugAttr>())
4599+
return;
4600+
4601+
auto Align = getDeclAlignIfRequired(D, CGM.getContext());
4602+
llvm::DIFile *Unit = getOrCreateFile(D->getLocation());
4603+
StringRef Name = D->getName();
4604+
llvm::DIType *Ty = getOrCreateType(D->getType(), Unit);
4605+
4606+
llvm::DIScope *DContext = getDeclContextDescriptor(D);
4607+
llvm::DIGlobalVariableExpression *GVE =
4608+
DBuilder.createGlobalVariableExpression(
4609+
DContext, Name, StringRef(), Unit, getLineNumber(D->getLocation()),
4610+
Ty, false, false, nullptr, nullptr, nullptr, Align);
4611+
Var->addDebugInfo(GVE);
4612+
}
4613+
45954614
llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
45964615
if (!LexicalBlockStack.empty())
45974616
return LexicalBlockStack.back();

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ class CGDebugInfo {
478478
/// Emit a constant global variable's debug info.
479479
void EmitGlobalVariable(const ValueDecl *VD, const APValue &Init);
480480

481+
/// Emit information about an external variable.
482+
void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
483+
481484
/// Emit C++ using directive.
482485
void EmitUsingDirective(const UsingDirectiveDecl &UD);
483486

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ namespace clang {
336336
Gen->CompleteTentativeDefinition(D);
337337
}
338338

339+
void CompleteExternalDeclaration(VarDecl *D) override {
340+
Gen->CompleteExternalDeclaration(D);
341+
}
342+
339343
void AssignInheritanceModel(CXXRecordDecl *RD) override {
340344
Gen->AssignInheritanceModel(RD);
341345
}

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3730,6 +3730,10 @@ void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
37303730
EmitGlobalVarDefinition(D);
37313731
}
37323732

3733+
void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) {
3734+
EmitExternalVarDeclaration(D);
3735+
}
3736+
37333737
CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
37343738
return Context.toCharUnitsFromBits(
37353739
getDataLayout().getTypeStoreSizeInBits(Ty));
@@ -4113,6 +4117,19 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
41134117
DI->EmitGlobalVariable(GV, D);
41144118
}
41154119

4120+
void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
4121+
if (CGDebugInfo *DI = getModuleDebugInfo())
4122+
if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo) {
4123+
QualType ASTTy = D->getType();
4124+
llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
4125+
llvm::PointerType *PTy =
4126+
llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
4127+
llvm::Constant *GV = GetOrCreateLLVMGlobal(D->getName(), PTy, D);
4128+
DI->EmitExternalVariable(
4129+
cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
4130+
}
4131+
}
4132+
41164133
static bool isVarDeclStrongDefinition(const ASTContext &Context,
41174134
CodeGenModule &CGM, const VarDecl *D,
41184135
bool NoCommon) {

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,8 @@ class CodeGenModule : public CodeGenTypeCache {
11701170

11711171
void EmitTentativeDefinition(const VarDecl *D);
11721172

1173+
void EmitExternalDeclaration(const VarDecl *D);
1174+
11731175
void EmitVTable(CXXRecordDecl *Class);
11741176

11751177
void RefreshTypeCacheForClass(const CXXRecordDecl *Class);
@@ -1405,6 +1407,7 @@ class CodeGenModule : public CodeGenTypeCache {
14051407
void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
14061408

14071409
void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
1410+
void EmitExternalVarDeclaration(const VarDecl *D);
14081411
void EmitAliasDefinition(GlobalDecl GD);
14091412
void emitIFuncDefinition(GlobalDecl GD);
14101413
void emitCPUDispatchDefinition(GlobalDecl GD);

clang/lib/CodeGen/ModuleBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ namespace {
290290
Builder->EmitTentativeDefinition(D);
291291
}
292292

293+
void CompleteExternalDeclaration(VarDecl *D) override {
294+
Builder->EmitExternalDeclaration(D);
295+
}
296+
293297
void HandleVTable(CXXRecordDecl *RD) override {
294298
if (Diags.hasErrorOccurred())
295299
return;

0 commit comments

Comments
 (0)