Skip to content

Commit 892270c

Browse files
rnkIanWood1
authored andcommitted
[BPF] Fix issues with external declarations of C++ structor decls (llvm#137079)
Use GetAddrOfGlobal, which is a more general API that takes a GlobalDecl, and handles declaring C++ destructors and other types in a general way. We can use this to generalize over functions and variable declarations. This fixes issues reported on llvm#130674 by @lexi-nadia .
1 parent 04f9d12 commit 892270c

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5378,11 +5378,33 @@ void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
53785378
EmitGlobalVarDefinition(D);
53795379
}
53805380

5381+
// Return a GlobalDecl. Use the base variants for destructors and constructors.
5382+
static GlobalDecl getBaseVariantGlobalDecl(const NamedDecl *D) {
5383+
if (auto const *CD = dyn_cast<const CXXConstructorDecl>(D))
5384+
return GlobalDecl(CD, CXXCtorType::Ctor_Base);
5385+
else if (auto const *DD = dyn_cast<const CXXDestructorDecl>(D))
5386+
return GlobalDecl(DD, CXXDtorType::Dtor_Base);
5387+
return GlobalDecl(D);
5388+
}
5389+
53815390
void CodeGenModule::EmitExternalDeclaration(const DeclaratorDecl *D) {
5382-
if (auto const *V = dyn_cast<const VarDecl>(D))
5383-
EmitExternalVarDeclaration(V);
5384-
if (auto const *FD = dyn_cast<const FunctionDecl>(D))
5385-
EmitExternalFunctionDeclaration(FD);
5391+
CGDebugInfo *DI = getModuleDebugInfo();
5392+
if (!DI || !getCodeGenOpts().hasReducedDebugInfo())
5393+
return;
5394+
5395+
GlobalDecl GD = getBaseVariantGlobalDecl(D);
5396+
if (!GD)
5397+
return;
5398+
5399+
llvm::Constant *Addr = GetAddrOfGlobal(GD)->stripPointerCasts();
5400+
if (const auto *VD = dyn_cast<VarDecl>(D)) {
5401+
DI->EmitExternalVariable(
5402+
cast<llvm::GlobalVariable>(Addr->stripPointerCasts()), VD);
5403+
} else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
5404+
llvm::Function *Fn = cast<llvm::Function>(Addr);
5405+
if (!Fn->getSubprogram())
5406+
DI->EmitFunctionDecl(GD, FD->getLocation(), FD->getType(), Fn);
5407+
}
53865408
}
53875409

53885410
CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
@@ -5825,30 +5847,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
58255847
DI->EmitGlobalVariable(GV, D);
58265848
}
58275849

5828-
void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
5829-
if (CGDebugInfo *DI = getModuleDebugInfo())
5830-
if (getCodeGenOpts().hasReducedDebugInfo()) {
5831-
QualType ASTTy = D->getType();
5832-
llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
5833-
llvm::Constant *GV =
5834-
GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D);
5835-
DI->EmitExternalVariable(
5836-
cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
5837-
}
5838-
}
5839-
5840-
void CodeGenModule::EmitExternalFunctionDeclaration(const FunctionDecl *FD) {
5841-
if (CGDebugInfo *DI = getModuleDebugInfo())
5842-
if (getCodeGenOpts().hasReducedDebugInfo()) {
5843-
auto *Ty = getTypes().ConvertType(FD->getType());
5844-
StringRef MangledName = getMangledName(FD);
5845-
auto *Fn = cast<llvm::Function>(
5846-
GetOrCreateLLVMFunction(MangledName, Ty, FD, /* ForVTable */ false));
5847-
if (!Fn->getSubprogram())
5848-
DI->EmitFunctionDecl(FD, FD->getLocation(), FD->getType(), Fn);
5849-
}
5850-
}
5851-
58525850
static bool isVarDeclStrongDefinition(const ASTContext &Context,
58535851
CodeGenModule &CGM, const VarDecl *D,
58545852
bool NoCommon) {

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,8 +1852,6 @@ class CodeGenModule : public CodeGenTypeCache {
18521852
void EmitMultiVersionFunctionDefinition(GlobalDecl GD, llvm::GlobalValue *GV);
18531853

18541854
void EmitGlobalVarDefinition(const VarDecl *D, bool IsTentative = false);
1855-
void EmitExternalVarDeclaration(const VarDecl *D);
1856-
void EmitExternalFunctionDeclaration(const FunctionDecl *D);
18571855
void EmitAliasDefinition(GlobalDecl GD);
18581856
void emitIFuncDefinition(GlobalDecl GD);
18591857
void emitCPUDispatchDefinition(GlobalDecl GD);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -debug-info-kind=constructor -triple bpf -emit-llvm %s -o - | FileCheck %s
2+
3+
class Foo {
4+
public:
5+
virtual ~Foo() noexcept;
6+
};
7+
8+
class Bar : public Foo {
9+
public:
10+
Bar() noexcept {}
11+
~Bar() noexcept override;
12+
};
13+
14+
// CHECK: declare !dbg !{{[0-9]+}} void @_ZN3FooD2Ev(ptr noundef nonnull align 8 dereferenceable(8))

0 commit comments

Comments
 (0)