Skip to content

Commit 675d8d6

Browse files
authored
(New) Add option to generate additional debug info for expression dereferencing pointer to pointers (#95298)
This is a different implementation to #94100, which has been reverted. When -fdebug-info-for-profiling is specified, for any Load expression if the pointer operand is not a declared variable, clang will emit debug info describing the type of the pointer operand (which can be an intermediate expr)
1 parent cddb9ce commit 675d8d6

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5746,6 +5746,48 @@ void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
57465746
Var->addDebugInfo(GVE);
57475747
}
57485748

5749+
void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
5750+
llvm::Instruction *Value, QualType Ty) {
5751+
// Only when -g2 or above is specified, debug info for variables will be
5752+
// generated.
5753+
if (CGM.getCodeGenOpts().getDebugInfo() <=
5754+
llvm::codegenoptions::DebugLineTablesOnly)
5755+
return;
5756+
5757+
llvm::DILocation *DIL = Value->getDebugLoc().get();
5758+
if (!DIL)
5759+
return;
5760+
5761+
llvm::DIFile *Unit = DIL->getFile();
5762+
llvm::DIType *Type = getOrCreateType(Ty, Unit);
5763+
5764+
// Check if Value is already a declared variable and has debug info, in this
5765+
// case we have nothing to do. Clang emits a declared variable as alloca, and
5766+
// it is loaded upon use, so we identify such pattern here.
5767+
if (llvm::LoadInst *Load = dyn_cast<llvm::LoadInst>(Value)) {
5768+
llvm::Value *Var = Load->getPointerOperand();
5769+
// There can be implicit type cast applied on a variable if it is an opaque
5770+
// ptr, in this case its debug info may not match the actual type of object
5771+
// being used as in the next instruction, so we will need to emit a pseudo
5772+
// variable for type-casted value.
5773+
auto DeclareTypeMatches = [&](auto *DbgDeclare) {
5774+
return DbgDeclare->getVariable()->getType() == Type;
5775+
};
5776+
if (any_of(llvm::findDbgDeclares(Var), DeclareTypeMatches) ||
5777+
any_of(llvm::findDVRDeclares(Var), DeclareTypeMatches))
5778+
return;
5779+
}
5780+
5781+
llvm::DILocalVariable *D =
5782+
DBuilder.createAutoVariable(LexicalBlockStack.back(), "", nullptr, 0,
5783+
Type, false, llvm::DINode::FlagArtificial);
5784+
5785+
if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
5786+
DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
5787+
&**InsertPoint);
5788+
}
5789+
}
5790+
57495791
void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
57505792
const GlobalDecl GD) {
57515793

clang/lib/CodeGen/CGDebugInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ class CGDebugInfo {
530530
/// Emit information about an external variable.
531531
void EmitExternalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl);
532532

533+
/// Emit a pseudo variable and debug info for an intermediate value if it does
534+
/// not correspond to a variable in the source code, so that a profiler can
535+
/// track more accurate usage of certain instructions of interest.
536+
void EmitPseudoVariable(CGBuilderTy &Builder, llvm::Instruction *Value,
537+
QualType Ty);
538+
533539
/// Emit information about global variable alias.
534540
void EmitGlobalAlias(const llvm::GlobalValue *GV, const GlobalDecl Decl);
535541

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,26 @@ Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
19371937
}
19381938
}
19391939

1940-
return EmitLoadOfLValue(E);
1940+
llvm::Value *Result = EmitLoadOfLValue(E);
1941+
1942+
// If -fdebug-info-for-profiling is specified, emit a pseudo variable and its
1943+
// debug info for the pointer, even if there is no variable associated with
1944+
// the pointer's expression.
1945+
if (CGF.CGM.getCodeGenOpts().DebugInfoForProfiling && CGF.getDebugInfo()) {
1946+
if (llvm::LoadInst *Load = dyn_cast<llvm::LoadInst>(Result)) {
1947+
if (llvm::GetElementPtrInst *GEP =
1948+
dyn_cast<llvm::GetElementPtrInst>(Load->getPointerOperand())) {
1949+
if (llvm::Instruction *Pointer =
1950+
dyn_cast<llvm::Instruction>(GEP->getPointerOperand())) {
1951+
QualType Ty = E->getBase()->getType();
1952+
if (!E->isArrow())
1953+
Ty = CGF.getContext().getPointerType(Ty);
1954+
CGF.getDebugInfo()->EmitPseudoVariable(Builder, Pointer, Ty);
1955+
}
1956+
}
1957+
}
1958+
}
1959+
return Result;
19411960
}
19421961

19431962
Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Test debug info for intermediate value of a chained pointer deferencing
2+
// expression when the flag -fdebug-info-for-pointer-type is enabled.
3+
// RUN: %clang_cc1 -emit-llvm -triple x86_64-linux-gnu %s -fdebug-info-for-profiling -debug-info-kind=constructor -o - | FileCheck %s
4+
5+
class A {
6+
public:
7+
int i;
8+
char c;
9+
void *p;
10+
int arr[3];
11+
};
12+
13+
class B {
14+
public:
15+
A* a;
16+
};
17+
18+
class C {
19+
public:
20+
B* b;
21+
A* a;
22+
A arr[10];
23+
};
24+
25+
// CHECK-LABEL: define dso_local noundef i32 @{{.*}}func1{{.*}}(
26+
// CHECK: [[A_ADDR:%.*]] = getelementptr inbounds %class.B, ptr {{%.*}}, i32 0, i32 0, !dbg [[DBG1:![0-9]+]]
27+
// CHECK-NEXT: [[A:%.*]] = load ptr, ptr [[A_ADDR]], align {{.*}}, !dbg [[DBG1]]
28+
// CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[A]], metadata [[META1:![0-9]+]], metadata !DIExpression()), !dbg [[DBG1]]
29+
// CHECK-NEXT: {{%.*}} = getelementptr inbounds %class.A, ptr [[A]], i32 0, i32 0,
30+
int func1(B *b) {
31+
return b->a->i;
32+
}
33+
34+
// Should generate a pseudo variable when pointer is type-casted.
35+
// CHECK-LABEL: define dso_local noundef ptr @{{.*}}func2{{.*}}(
36+
// CHECK: call void @llvm.dbg.declare(metadata ptr [[B_ADDR:%.*]], metadata [[META2:![0-9]+]], metadata !DIExpression())
37+
// CHECK-NEXT: [[B:%.*]] = load ptr, ptr [[B_ADDR]],
38+
// CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[B]], metadata [[META3:![0-9]+]], metadata !DIExpression())
39+
// CHECK-NEXT: {{%.*}} = getelementptr inbounds %class.B, ptr [[B]], i32 0,
40+
A* func2(void *b) {
41+
return ((B*)b)->a;
42+
}
43+
44+
// Should not generate pseudo variable in this case.
45+
// CHECK-LABEL: define dso_local noundef i32 @{{.*}}func3{{.*}}(
46+
// CHECK: call void @llvm.dbg.declare(metadata ptr [[B_ADDR:%.*]], metadata [[META4:![0-9]+]], metadata !DIExpression())
47+
// CHECK: call void @llvm.dbg.declare(metadata ptr [[LOCAL1:%.*]], metadata [[META5:![0-9]+]], metadata !DIExpression())
48+
// CHECK-NOT: call void @llvm.dbg.value(metadata ptr
49+
int func3(B *b) {
50+
A *local1 = b->a;
51+
return local1->i;
52+
}
53+
54+
// CHECK-LABEL: define dso_local noundef signext i8 @{{.*}}func4{{.*}}(
55+
// CHECK: [[A_ADDR:%.*]] = getelementptr inbounds %class.C, ptr {{%.*}}, i32 0, i32 1
56+
// CHECK-NEXT: [[A:%.*]] = load ptr, ptr [[A_ADDR]],
57+
// CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[A]], metadata [[META6:![0-9]+]], metadata !DIExpression())
58+
// CHECK-NEXT: {{%.*}} = getelementptr inbounds %class.A, ptr [[A]], i32 0, i32 0,
59+
// CHECK: [[CALL:%.*]] = call noundef ptr @{{.*}}foo{{.*}}(
60+
// CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[CALL]], metadata [[META6]], metadata !DIExpression())
61+
// CHECK-NEXT: [[I1:%.*]] = getelementptr inbounds %class.A, ptr [[CALL]], i32 0, i32 1
62+
char func4(C *c) {
63+
extern A* foo(int x);
64+
return foo(c->a->i)->c;
65+
}
66+
67+
// CHECK-LABEL: define dso_local noundef signext i8 @{{.*}}func5{{.*}}(
68+
// CHECK: call void @llvm.dbg.declare(metadata ptr {{%.*}}, metadata [[META7:![0-9]+]], metadata !DIExpression())
69+
// CHECK: call void @llvm.dbg.declare(metadata ptr {{%.*}}, metadata [[META8:![0-9]+]], metadata !DIExpression())
70+
// CHECK: [[A_ADDR:%.*]] = getelementptr inbounds %class.A, ptr {{%.*}}, i64 {{%.*}},
71+
// CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[A_ADDR]], metadata [[META9:![0-9]+]], metadata !DIExpression())
72+
// CHECK-NEXT: {{%.*}} = getelementptr inbounds %class.A, ptr [[A_ADDR]], i32 0, i32 1,
73+
char func5(void *arr, int n) {
74+
return ((A*)arr)[n].c;
75+
}
76+
77+
// CHECK-LABEL: define dso_local noundef i32 @{{.*}}func6{{.*}}(
78+
// CHECK: call void @llvm.dbg.declare(metadata ptr {{%.*}}, metadata [[META10:![0-9]+]], metadata !DIExpression())
79+
// CHECK: call void @llvm.dbg.value(metadata ptr {{%.*}}, metadata [[META11:![0-9]+]], metadata !DIExpression())
80+
int func6(B &b) {
81+
return reinterpret_cast<A&>(b).i;
82+
}
83+
84+
// CHECK-LABEL: define dso_local noundef i32 @{{.*}}global{{.*}}(
85+
// CHECK: [[GA:%.*]] = load ptr, ptr @ga
86+
// CHECK-NEXT: call void @llvm.dbg.value(metadata ptr [[GA]], metadata [[META12:![0-9]+]], metadata !DIExpression())
87+
A *ga;
88+
int global() {
89+
return ga->i;
90+
}
91+
92+
93+
// CHECK-DAG: [[META_A:![0-9]+]] = distinct !DICompositeType(tag: DW_TAG_class_type, name: "A",
94+
// CHECK-DAG: [[META_AP:![0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[META_A]],
95+
// CHECK-DAG: [[META_B:![0-9]+]] = distinct !DICompositeType(tag: DW_TAG_class_type, name: "B",
96+
// CHECK-DAG: [[META_BP:![0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[META_B]],
97+
// CHECK-DAG: [[META_C:![0-9]+]] = distinct !DICompositeType(tag: DW_TAG_class_type, name: "C",
98+
// CHECK-DAG: [[META_CP:![0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[META_C]],
99+
// CHECK-DAG: [[META_VP:![0-9]+]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null,
100+
// CHECK-DAG: [[META_I32:![0-9]+]] = !DIBasicType(name: "int", size: 32,
101+
// CHECK-DAG: [[META_BR:![0-9]+]] = !DIDerivedType(tag: DW_TAG_reference_type, baseType: [[META_B]],
102+
103+
// CHECK-DAG: [[DBG1]] = !DILocation(line: 31, column: 13,
104+
// CHECK-DAG: [[META1]] = !DILocalVariable(scope: {{.*}}, type: [[META_AP]], flags: DIFlagArtificial)
105+
// CHECK-DAG: [[META2]] = !DILocalVariable(name: "b", arg: 1, scope: {{.*}}, file: {{.*}}, line: 40, type: [[META_VP]])
106+
// CHECK-DAG: [[META3]] = !DILocalVariable(scope: {{.*}}, type: [[META_BP]], flags: DIFlagArtificial)
107+
// CHECK-DAG: [[META4]] = !DILocalVariable(name: "b", arg: 1, scope: {{.*}}, file: {{.*}}, line: 49, type: [[META_BP]])
108+
// CHECK-DAG: [[META5]] = !DILocalVariable(name: "local1", scope: {{.*}}, file: {{.*}}, line: 50, type: [[META_AP]])
109+
// CHECK-DAG: [[META6]] = !DILocalVariable(scope: {{.*}}, type: [[META_AP]], flags: DIFlagArtificial)
110+
// CHECK-DAG: [[META7]] = !DILocalVariable(name: "arr", arg: 1, scope: {{.*}}, file: {{.*}}, line: 73, type: [[META_VP]])
111+
// CHECK-DAG: [[META8]] = !DILocalVariable(name: "n", arg: 2, scope: {{.*}}, file: {{.*}}, line: 73, type: [[META_I32]])
112+
// CHECK-DAG: [[META9]] = !DILocalVariable(scope: {{.*}}, type: [[META_AP]], flags: DIFlagArtificial)
113+
// CHECK-DAG: [[META10]] = !DILocalVariable(name: "b", arg: 1, scope: {{.*}}, file: {{.*}}, line: 80, type: [[META_BR]])
114+
// CHECK-DAG: [[META11]] = !DILocalVariable(scope: {{.*}}, type: [[META_AP]], flags: DIFlagArtificial)
115+
// CHECK-DAG: [[META12]] = !DILocalVariable(scope: {{.*}}, type: [[META_AP]], flags: DIFlagArtificial)

0 commit comments

Comments
 (0)