Skip to content

Commit b103037

Browse files
authored
[Coroutines] [NFCI] Don't search the DILocalVariable for __promise when constructing the debug varaible for __coro_frame (#105626)
As the title mentioned, do not search for the DILocalVariable for __promise when constructing the debug variable for __coro_frame. This should make sense because the debug variable of `__coro_frame` shouldn't dependent on the debug variable of `__promise`. And actually, it is not. Currently, we search the debug variable for `__promise` only because we want to get the debug location and the debug scope for the `__promise`. However, we can construct the debug location directly from the debug scope of the being compiled function. Then it is not necessary any more to search the `__promise` variable. And this patch makes the codes to construct the debug variable for `__coro_frame` to be more stable. Now we will always be able to construct the debug variable for the coroutine frame no matter if we found the debug variable for the __promise or not. This patch is not strictly NFC but it is intended to not affect any end users.
1 parent 68a1593 commit b103037

File tree

4 files changed

+31
-43
lines changed

4 files changed

+31
-43
lines changed

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,38 +1113,20 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
11131113
// neither.
11141114
if (!DIS || !DIS->getUnit() ||
11151115
!dwarf::isCPlusPlus(
1116-
(dwarf::SourceLanguage)DIS->getUnit()->getSourceLanguage()))
1116+
(dwarf::SourceLanguage)DIS->getUnit()->getSourceLanguage()) ||
1117+
DIS->getUnit()->getEmissionKind() != DICompileUnit::DebugEmissionKind::FullDebug)
11171118
return;
11181119

11191120
assert(Shape.ABI == coro::ABI::Switch &&
11201121
"We could only build debug infomation for C++ coroutine now.\n");
11211122

11221123
DIBuilder DBuilder(*F.getParent(), /*AllowUnresolved*/ false);
11231124

1124-
AllocaInst *PromiseAlloca = Shape.getPromiseAlloca();
1125-
assert(PromiseAlloca &&
1125+
assert(Shape.getPromiseAlloca() &&
11261126
"Coroutine with switch ABI should own Promise alloca");
11271127

1128-
TinyPtrVector<DbgDeclareInst *> DIs = findDbgDeclares(PromiseAlloca);
1129-
TinyPtrVector<DbgVariableRecord *> DVRs = findDVRDeclares(PromiseAlloca);
1130-
1131-
DILocalVariable *PromiseDIVariable = nullptr;
1132-
DILocation *DILoc = nullptr;
1133-
if (!DIs.empty()) {
1134-
DbgDeclareInst *PromiseDDI = DIs.front();
1135-
PromiseDIVariable = PromiseDDI->getVariable();
1136-
DILoc = PromiseDDI->getDebugLoc().get();
1137-
} else if (!DVRs.empty()) {
1138-
DbgVariableRecord *PromiseDVR = DVRs.front();
1139-
PromiseDIVariable = PromiseDVR->getVariable();
1140-
DILoc = PromiseDVR->getDebugLoc().get();
1141-
} else {
1142-
return;
1143-
}
1144-
1145-
DILocalScope *PromiseDIScope = PromiseDIVariable->getScope();
1146-
DIFile *DFile = PromiseDIScope->getFile();
1147-
unsigned LineNum = PromiseDIVariable->getLine();
1128+
DIFile *DFile = DIS->getFile();
1129+
unsigned LineNum = DIS->getLine();
11481130

11491131
DICompositeType *FrameDITy = DBuilder.createStructType(
11501132
DIS->getUnit(), Twine(F.getName() + ".coro_frame_ty").str(),
@@ -1254,10 +1236,9 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
12541236

12551237
DBuilder.replaceArrays(FrameDITy, DBuilder.getOrCreateArray(Elements));
12561238

1257-
auto *FrameDIVar = DBuilder.createAutoVariable(PromiseDIScope, "__coro_frame",
1258-
DFile, LineNum, FrameDITy,
1259-
true, DINode::FlagArtificial);
1260-
assert(FrameDIVar->isValidLocationForIntrinsic(DILoc));
1239+
auto *FrameDIVar =
1240+
DBuilder.createAutoVariable(DIS, "__coro_frame", DFile, LineNum,
1241+
FrameDITy, true, DINode::FlagArtificial);
12611242

12621243
// Subprogram would have ContainedNodes field which records the debug
12631244
// variables it contained. So we need to add __coro_frame to the
@@ -1266,14 +1247,17 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
12661247
// If we don't add __coro_frame to the RetainedNodes, user may get
12671248
// `no symbol __coro_frame in context` rather than `__coro_frame`
12681249
// is optimized out, which is more precise.
1269-
if (auto *SubProgram = dyn_cast<DISubprogram>(PromiseDIScope)) {
1270-
auto RetainedNodes = SubProgram->getRetainedNodes();
1271-
SmallVector<Metadata *, 32> RetainedNodesVec(RetainedNodes.begin(),
1272-
RetainedNodes.end());
1273-
RetainedNodesVec.push_back(FrameDIVar);
1274-
SubProgram->replaceOperandWith(
1275-
7, (MDTuple::get(F.getContext(), RetainedNodesVec)));
1276-
}
1250+
auto RetainedNodes = DIS->getRetainedNodes();
1251+
SmallVector<Metadata *, 32> RetainedNodesVec(RetainedNodes.begin(),
1252+
RetainedNodes.end());
1253+
RetainedNodesVec.push_back(FrameDIVar);
1254+
DIS->replaceOperandWith(7, (MDTuple::get(F.getContext(), RetainedNodesVec)));
1255+
1256+
// Construct the location for the frame debug variable. The column number
1257+
// is fake but it should be fine.
1258+
DILocation *DILoc =
1259+
DILocation::get(DIS->getContext(), LineNum, /*Column=*/1, DIS);
1260+
assert(FrameDIVar->isValidLocationForIntrinsic(DILoc));
12771261

12781262
if (UseNewDbgInfoFormat) {
12791263
DbgVariableRecord *NewDVR =

llvm/test/Transforms/Coroutines/coro-debug-coro-frame.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
;
1616
; CHECK-DAG: ![[FILE:[0-9]+]] = !DIFile(filename: "coro-debug.cpp"
1717
; CHECK-DAG: ![[RAMP:[0-9]+]] = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov",
18-
; CHECK-DAG: ![[RAMP_SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: ![[RAMP]], file: ![[FILE]], line: 23
19-
; CHECK-DAG: ![[CORO_FRAME]] = !DILocalVariable(name: "__coro_frame", scope: ![[RAMP_SCOPE]], file: ![[FILE]], line: [[PROMISE_VAR_LINE:[0-9]+]], type: ![[FRAME_TYPE:[0-9]+]], flags: DIFlagArtificial)
18+
; CHECK-DAG: ![[CORO_FRAME]] = !DILocalVariable(name: "__coro_frame", scope: ![[RAMP]], file: ![[FILE]], line: [[CORO_FRAME_LINE:[0-9]+]], type: ![[FRAME_TYPE:[0-9]+]], flags: DIFlagArtificial)
2019
; CHECK-DAG: ![[FRAME_TYPE]] = !DICompositeType(tag: DW_TAG_structure_type, name: "f.coro_frame_ty", {{.*}}elements: ![[ELEMENTS:[0-9]+]]
2120
; CHECK-DAG: ![[ELEMENTS]] = !{![[RESUME_FN:[0-9]+]], ![[DESTROY_FN:[0-9]+]], ![[PROMISE:[0-9]+]], ![[VECTOR_TYPE:[0-9]+]], ![[INT64_0:[0-9]+]], ![[DOUBLE_1:[0-9]+]], ![[INT64_PTR:[0-9]+]], ![[INT32_2:[0-9]+]], ![[INT32_3:[0-9]+]], ![[UNALIGNED_UNKNOWN:[0-9]+]], ![[STRUCT:[0-9]+]], ![[CORO_INDEX:[0-9]+]], ![[SMALL_UNKNOWN:[0-9]+]]
2221
; CHECK-DAG: ![[RESUME_FN]] = !DIDerivedType(tag: DW_TAG_member, name: "__resume_fn"{{.*}}, baseType: ![[RESUME_FN_TYPE:[0-9]+]]{{.*}}, flags: DIFlagArtificial
@@ -29,25 +28,26 @@
2928
; CHECK-DAG: ![[UNKNOWN_TYPE_BASE]] = !DIBasicType(name: "UnknownType", size: 8, encoding: DW_ATE_unsigned_char, flags: DIFlagArtificial)
3029
; CHECK-DAG: ![[VECTOR_TYPE_BASE_ELEMENTS]] = !{![[VECTOR_TYPE_BASE_SUBRANGE:[0-9]+]]}
3130
; CHECK-DAG: ![[VECTOR_TYPE_BASE_SUBRANGE]] = !DISubrange(count: 16, lowerBound: 0)
32-
; CHECK-DAG: ![[INT64_0]] = !DIDerivedType(tag: DW_TAG_member, name: "__int_64_1", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[PROMISE_VAR_LINE]], baseType: ![[I64_BASE:[0-9]+]],{{.*}}, flags: DIFlagArtificial
31+
; CHECK-DAG: ![[INT64_0]] = !DIDerivedType(tag: DW_TAG_member, name: "__int_64_1", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[CORO_FRAME_LINE]], baseType: ![[I64_BASE:[0-9]+]],{{.*}}, flags: DIFlagArtificial
3332
; CHECK-DAG: ![[I64_BASE]] = !DIBasicType(name: "__int_64", size: 64, encoding: DW_ATE_signed, flags: DIFlagArtificial)
34-
; CHECK-DAG: ![[DOUBLE_1]] = !DIDerivedType(tag: DW_TAG_member, name: "__double__2", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[PROMISE_VAR_LINE]], baseType: ![[DOUBLE_BASE:[0-9]+]]{{.*}}, flags: DIFlagArtificial
33+
; CHECK-DAG: ![[DOUBLE_1]] = !DIDerivedType(tag: DW_TAG_member, name: "__double__2", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[CORO_FRAME_LINE]], baseType: ![[DOUBLE_BASE:[0-9]+]]{{.*}}, flags: DIFlagArtificial
3534
; CHECK-DAG: ![[DOUBLE_BASE]] = !DIBasicType(name: "__double_", size: 64, encoding: DW_ATE_float, flags: DIFlagArtificial)
36-
; CHECK-DAG: ![[INT32_2]] = !DIDerivedType(tag: DW_TAG_member, name: "__int_32_4", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[PROMISE_VAR_LINE]], baseType: ![[I32_BASE:[0-9]+]]{{.*}}, flags: DIFlagArtificial
35+
; CHECK-DAG: ![[INT32_2]] = !DIDerivedType(tag: DW_TAG_member, name: "__int_32_4", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[CORO_FRAME_LINE]], baseType: ![[I32_BASE:[0-9]+]]{{.*}}, flags: DIFlagArtificial
3736
; CHECK-DAG: ![[I32_BASE]] = !DIBasicType(name: "__int_32", size: 32, encoding: DW_ATE_signed, flags: DIFlagArtificial)
38-
; CHECK-DAG: ![[INT32_3]] = !DIDerivedType(tag: DW_TAG_member, name: "__int_32_5", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[PROMISE_VAR_LINE]], baseType: ![[I32_BASE]]
37+
; CHECK-DAG: ![[INT32_3]] = !DIDerivedType(tag: DW_TAG_member, name: "__int_32_5", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[CORO_FRAME_LINE]], baseType: ![[I32_BASE]]
3938
; CHECK-DAG: ![[UNALIGNED_UNKNOWN]] = !DIDerivedType(tag: DW_TAG_member, name: "_6",{{.*}}baseType: ![[UNALIGNED_UNKNOWN_BASE:[0-9]+]], size: 9
4039
; CHECK-DAG: ![[UNALIGNED_UNKNOWN_BASE]] = !DICompositeType(tag: DW_TAG_array_type, baseType: ![[UNKNOWN_TYPE_BASE]], size: 16,{{.*}} elements: ![[UNALIGNED_UNKNOWN_ELEMENTS:[0-9]+]])
4140
; CHECK-DAG: ![[UNALIGNED_UNKNOWN_ELEMENTS]] = !{![[UNALIGNED_UNKNOWN_SUBRANGE:[0-9]+]]}
4241
; CHECk-DAG: ![[UNALIGNED_UNKNOWN_SUBRANGE]] = !DISubrange(count: 2, lowerBound: 0)
43-
; CHECK-DAG: ![[STRUCT]] = !DIDerivedType(tag: DW_TAG_member, name: "struct_big_structure_7", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[PROMISE_VAR_LINE]], baseType: ![[STRUCT_BASE:[0-9]+]]
42+
; CHECK-DAG: ![[STRUCT]] = !DIDerivedType(tag: DW_TAG_member, name: "struct_big_structure_7", scope: ![[FRAME_TYPE]], file: ![[FILE]], line: [[CORO_FRAME_LINE]], baseType: ![[STRUCT_BASE:[0-9]+]]
4443
; CHECK-DAG: ![[STRUCT_BASE]] = !DICompositeType(tag: DW_TAG_structure_type, name: "struct_big_structure"{{.*}}, align: 64, flags: DIFlagArtificial, elements: ![[STRUCT_ELEMENTS:[0-9]+]]
4544
; CHECK-DAG: ![[STRUCT_ELEMENTS]] = !{![[MEM_TYPE:[0-9]+]]}
4645
; CHECK-DAG: ![[MEM_TYPE]] = !DIDerivedType(tag: DW_TAG_member,{{.*}} baseType: ![[MEM_TYPE_BASE:[0-9]+]], size: 4000
4746
; CHECK-DAG: ![[MEM_TYPE_BASE]] = !DICompositeType(tag: DW_TAG_array_type, baseType: ![[UNKNOWN_TYPE_BASE]], size: 4000,
4847
; CHECK-DAG: ![[CORO_INDEX]] = !DIDerivedType(tag: DW_TAG_member, name: "__coro_index"
4948
; CHECK-DAG: ![[SMALL_UNKNOWN]] = !DIDerivedType(tag: DW_TAG_member, name: "UnknownType_8",{{.*}} baseType: ![[UNKNOWN_TYPE_BASE]], size: 5
50-
; CHECK-DAG: ![[PROMISE_VAR:[0-9]+]] = !DILocalVariable(name: "__promise", scope: ![[RAMP_SCOPE]], file: ![[FILE]], line: [[PROMISE_VAR_LINE]]
49+
; CHECK-DAG: ![[PROMISE_VAR:[0-9]+]] = !DILocalVariable(name: "__promise", scope: ![[RAMP_SCOPE:[0-9]+]], file: ![[FILE]]
50+
; CHECK-DAG: ![[RAMP_SCOPE]] = distinct !DILexicalBlock(scope: ![[RAMP]], file: ![[FILE]], line: 23
5151
; CHECK-DAG: ![[BAR_FUNC:[0-9]+]] = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv",
5252
; CHECK-DAG: ![[BAR_SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: ![[BAR_FUNC]], file: !1
5353
; CHECK-DAG: ![[FRAME_TYPE_IN_BAR:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "bar.coro_frame_ty", file: ![[FILE]], line: [[BAR_LINE:[0-9]+]]{{.*}}elements: ![[ELEMENTS_IN_BAR:[0-9]+]]

llvm/test/Transforms/Coroutines/coro-debug-dbg.values.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
; CHECK-SAME: ptr {{.*}} %[[frame:.*]])
2626
; CHECK-SAME: !dbg ![[RESUME_FN_DBG_NUM:[0-9]+]]
2727
; CHECK: %[[frame_alloca:.*]] = alloca ptr
28+
; CHECK-NEXT: #dbg_declare(ptr %begin.debug, ![[FRAME_DI_NUM:[0-9]+]],
2829
; CHECK-NEXT: store ptr %[[frame]], ptr %[[frame_alloca]]
2930
; CHECK: init.ready:
3031
; CHECK: #dbg_value(ptr %[[frame_alloca]], ![[XVAR_RESUME:[0-9]+]],
@@ -38,6 +39,7 @@
3839
; CHECK-SAME: !DIExpression(DW_OP_deref, DW_OP_plus_uconst, [[OffsetJ]], DW_OP_deref)
3940
;
4041
; CHECK: ![[RESUME_FN_DBG_NUM]] = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov"
42+
; CHECK: ![[FRAME_DI_NUM]] = !DILocalVariable(name: "__coro_frame"
4143
; CHECK: ![[IVAR_RESUME]] = !DILocalVariable(name: "i"
4244
; CHECK: ![[XVAR_RESUME]] = !DILocalVariable(name: "x"
4345
; CHECK: ![[JVAR_RESUME]] = !DILocalVariable(name: "j"

llvm/test/Transforms/Coroutines/coro-debug-frame-variable.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@
4242
; CHECK-NEXT: %[[DBG_PTR:.*]] = alloca ptr
4343
; CHECK-NEXT: #dbg_declare(ptr %[[DBG_PTR]], ![[XVAR_RESUME:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 32),
4444
; CHECK-NEXT: #dbg_declare(ptr %[[DBG_PTR]], ![[IVAR_RESUME:[0-9]+]], !DIExpression(DW_OP_deref, DW_OP_plus_uconst, 20), ![[IDBGLOC_RESUME:[0-9]+]]
45+
; CHECK-NEXT: #dbg_declare(ptr %[[DBG_PTR]], ![[FRAME_RESUME:[0-9]+]], !DIExpression(DW_OP_deref),
4546
; CHECK-NEXT: store ptr {{.*}}, ptr %[[DBG_PTR]]
4647
; CHECK: %[[J:.*]] = alloca i32, align 4
4748
; CHECK-NEXT: #dbg_declare(ptr %[[J]], ![[JVAR_RESUME:[0-9]+]], !DIExpression(), ![[JDBGLOC_RESUME:[0-9]+]]
4849
; CHECK: init.ready:
4950
; CHECK: await.ready:
5051
;
52+
; CHECK-DAG: ![[FRAME_RESUME]] = !DILocalVariable(name: "__coro_frame"
5153
; CHECK-DAG: ![[IVAR]] = !DILocalVariable(name: "i"
5254
; CHECK-DAG: ![[PROG_SCOPE:[0-9]+]] = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov"
5355
; CHECK-DAG: ![[BLK_SCOPE:[0-9]+]] = distinct !DILexicalBlock(scope: ![[PROG_SCOPE]], file: !1, line: 23, column: 12)

0 commit comments

Comments
 (0)