Skip to content

Commit f23bdbb

Browse files
authored
Add DILabel functions for LLVM-C (llvm#112840)
Addresses llvm#112799
1 parent 4cf1285 commit f23bdbb

File tree

4 files changed

+120
-10
lines changed

4 files changed

+120
-10
lines changed

llvm/include/llvm-c/DebugInfo.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,52 @@ LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst);
14151415
*/
14161416
void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc);
14171417

1418+
/**
1419+
* Create a new descriptor for a label
1420+
*
1421+
* \param Builder The DIBuilder.
1422+
* \param Scope The scope to create the label in.
1423+
* \param Name Variable name.
1424+
* \param NameLen Length of variable name.
1425+
* \param File The file to create the label in.
1426+
* \param LineNo Line Number.
1427+
* \param AlwaysPreserve Preserve the label regardless of optimization.
1428+
*
1429+
* @see llvm::DIBuilder::createLabel()
1430+
*/
1431+
LLVMMetadataRef LLVMDIBuilderCreateLabel(
1432+
LLVMDIBuilderRef Builder,
1433+
LLVMMetadataRef Context, const char *Name, size_t NameLen,
1434+
LLVMMetadataRef File, unsigned LineNo, LLVMBool AlwaysPreserve);
1435+
1436+
/**
1437+
* Insert a new llvm.dbg.label intrinsic call
1438+
*
1439+
* \param Builder The DIBuilder.
1440+
* \param LabelInfo The Label's debug info descriptor
1441+
* \param Location The debug info location
1442+
* \param InsertBefore Location for the new intrinsic.
1443+
*
1444+
* @see llvm::DIBuilder::insertLabel()
1445+
*/
1446+
LLVMDbgRecordRef LLVMDIBuilderInsertLabelBefore(
1447+
LLVMDIBuilderRef Builder, LLVMMetadataRef LabelInfo,
1448+
LLVMMetadataRef Location, LLVMValueRef InsertBefore);
1449+
1450+
/**
1451+
* Insert a new llvm.dbg.label intrinsic call
1452+
*
1453+
* \param Builder The DIBuilder.
1454+
* \param LabelInfo The Label's debug info descriptor
1455+
* \param Location The debug info location
1456+
* \param InsertAtEnd Location for the new intrinsic.
1457+
*
1458+
* @see llvm::DIBuilder::insertLabel()
1459+
*/
1460+
LLVMDbgRecordRef LLVMDIBuilderInsertLabelAtEnd(
1461+
LLVMDIBuilderRef Builder, LLVMMetadataRef LabelInfo,
1462+
LLVMMetadataRef Location, LLVMBasicBlockRef InsertAtEnd);
1463+
14181464
/**
14191465
* Obtain the enumerated type of a Metadata instance.
14201466
*

llvm/lib/IR/DebugInfo.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,47 @@ void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) {
17991799
unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc());
18001800
}
18011801

1802+
LLVMMetadataRef LLVMDIBuilderCreateLabel(
1803+
LLVMDIBuilderRef Builder,
1804+
LLVMMetadataRef Context, const char *Name, size_t NameLen,
1805+
LLVMMetadataRef File, unsigned LineNo, LLVMBool AlwaysPreserve) {
1806+
return wrap(unwrap(Builder)->createLabel(
1807+
unwrapDI<DIScope>(Context), StringRef(Name, NameLen),
1808+
unwrapDI<DIFile>(File), LineNo, AlwaysPreserve));
1809+
}
1810+
1811+
LLVMDbgRecordRef LLVMDIBuilderInsertLabelBefore(
1812+
LLVMDIBuilderRef Builder, LLVMMetadataRef LabelInfo,
1813+
LLVMMetadataRef Location, LLVMValueRef InsertBefore) {
1814+
DbgInstPtr DbgInst = unwrap(Builder)->insertLabel(
1815+
unwrapDI<DILabel>(LabelInfo), unwrapDI<DILocation>(Location),
1816+
unwrap<Instruction>(InsertBefore));
1817+
// This assert will fail if the module is in the old debug info format.
1818+
// This function should only be called if the module is in the new
1819+
// debug info format.
1820+
// See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
1821+
// LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
1822+
assert(isa<DbgRecord *>(DbgInst) &&
1823+
"Function unexpectedly in old debug info format");
1824+
return wrap(cast<DbgRecord *>(DbgInst));
1825+
}
1826+
1827+
LLVMDbgRecordRef LLVMDIBuilderInsertLabelAtEnd(
1828+
LLVMDIBuilderRef Builder, LLVMMetadataRef LabelInfo,
1829+
LLVMMetadataRef Location, LLVMBasicBlockRef InsertAtEnd) {
1830+
DbgInstPtr DbgInst = unwrap(Builder)->insertLabel(
1831+
unwrapDI<DILabel>(LabelInfo), unwrapDI<DILocation>(Location),
1832+
unwrap(InsertAtEnd));
1833+
// This assert will fail if the module is in the old debug info format.
1834+
// This function should only be called if the module is in the new
1835+
// debug info format.
1836+
// See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
1837+
// LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
1838+
assert(isa<DbgRecord *>(DbgInst) &&
1839+
"Function unexpectedly in old debug info format");
1840+
return wrap(cast<DbgRecord *>(DbgInst));
1841+
}
1842+
18021843
LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
18031844
switch(unwrap(Metadata)->getMetadataID()) {
18041845
#define HANDLE_METADATA_LEAF(CLASS) \

llvm/test/Bindings/llvm-c/debug_info_new_format.ll

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66

77
; CHECK: define i64 @foo(i64 %0, i64 %1, <10 x i64> %2) !dbg !31 {
88
; CHECK-NEXT: entry:
9-
; CHECK-NEXT: #dbg_declare(i64 0, !38, !DIExpression(), !44)
10-
; CHECK-NEXT: #dbg_declare(i64 0, !39, !DIExpression(), !44)
11-
; CHECK-NEXT: #dbg_declare(i64 0, !40, !DIExpression(), !44)
9+
; CHECK-NEXT: #dbg_declare(i64 0, !38, !DIExpression(), !45)
10+
; CHECK-NEXT: #dbg_declare(i64 0, !39, !DIExpression(), !45)
11+
; CHECK-NEXT: #dbg_declare(i64 0, !40, !DIExpression(), !45)
12+
; CHECK-NEXT: #dbg_label(!46, !45)
13+
; CHECK-NEXT: br label %vars
14+
; CHECK-NEXT: #dbg_label(!47, !45)
1215
; CHECK-NEXT: br label %vars
1316
; CHECK: vars:
1417
; CHECK-NEXT: %p1 = phi i64 [ 0, %entry ]
1518
; CHECK-NEXT: %p2 = phi i64 [ 0, %entry ]
16-
; CHECK-NEXT: #dbg_value(i64 0, !41, !DIExpression(DW_OP_constu, 0, DW_OP_stack_value), !45)
17-
; CHECK-NEXT: #dbg_value(i64 1, !43, !DIExpression(DW_OP_constu, 1, DW_OP_stack_value), !45)
19+
; CHECK-NEXT: #dbg_value(i64 0, !41, !DIExpression(DW_OP_constu, 0, DW_OP_stack_value), !48)
20+
; CHECK-NEXT: #dbg_value(i64 1, !43, !DIExpression(DW_OP_constu, 1, DW_OP_stack_value), !48)
1821
; CHECK-NEXT: %a = add i64 %p1, %p2
1922
; CHECK-NEXT: ret i64 0
2023
; CHECK-NEXT: }
@@ -60,12 +63,15 @@
6063
; CHECK-NEXT: !34 = !DICompositeType(tag: DW_TAG_array_type, baseType: !6, size: 640, flags: DIFlagVector, elements: !35)
6164
; CHECK-NEXT: !35 = !{!36}
6265
; CHECK-NEXT: !36 = !DISubrange(count: 10, lowerBound: 0)
63-
; CHECK-NEXT: !37 = !{!38, !39, !40, !41, !43}
66+
; CHECK-NEXT: !37 = !{!38, !39, !40, !41, !43, !44}
6467
; CHECK-NEXT: !38 = !DILocalVariable(name: "a", arg: 1, scope: !31, file: !1, line: 42, type: !6)
6568
; CHECK-NEXT: !39 = !DILocalVariable(name: "b", arg: 2, scope: !31, file: !1, line: 42, type: !6)
6669
; CHECK-NEXT: !40 = !DILocalVariable(name: "c", arg: 3, scope: !31, file: !1, line: 42, type: !34)
6770
; CHECK-NEXT: !41 = !DILocalVariable(name: "d", scope: !42, file: !1, line: 43, type: !6)
6871
; CHECK-NEXT: !42 = distinct !DILexicalBlock(scope: !31, file: !1, line: 42)
6972
; CHECK-NEXT: !43 = !DILocalVariable(name: "e", scope: !42, file: !1, line: 44, type: !6)
70-
; CHECK-NEXT: !44 = !DILocation(line: 42, scope: !31)
71-
; CHECK-NEXT: !45 = !DILocation(line: 43, scope: !31)
73+
; CHECK-NEXT: !44 = !DILabel(scope: !31, name: "label3", file: !1, line: 42)
74+
; CHECK-NEXT: !45 = !DILocation(line: 42, scope: !31)
75+
; CHECK-NEXT: !46 = !DILabel(scope: !31, name: "label1", file: !1, line: 42)
76+
; CHECK-NEXT: !47 = !DILabel(scope: !31, name: "label2", file: !1, line: 42)
77+
; CHECK-NEXT: !48 = !DILocation(line: 43, scope: !31)

llvm/tools/llvm-c-test/debuginfo.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ int llvm_test_dibuilder(void) {
163163

164164
LLVMSetSubprogram(FooFunction, FunctionMetadata);
165165

166+
LLVMMetadataRef FooLabel1 = LLVMDIBuilderCreateLabel(DIB, FunctionMetadata,
167+
"label1", 6, File, 42, false);
168+
LLVMDIBuilderInsertLabelAtEnd(DIB, FooLabel1, FooParamLocation,
169+
FooEntryBlock);
170+
166171
LLVMMetadataRef FooLexicalBlock =
167172
LLVMDIBuilderCreateLexicalBlock(DIB, FunctionMetadata, File, 42, 0);
168173

@@ -210,8 +215,6 @@ int llvm_test_dibuilder(void) {
210215
LLVMAddNamedMetadataOperand(
211216
M, "EnumTest", LLVMMetadataAsValue(LLVMGetModuleContext(M), EnumTest));
212217

213-
LLVMDIBuilderFinalize(DIB);
214-
215218
// Using the new debug format, debug records get attached to instructions.
216219
// Insert a `br` and `ret` now to absorb the debug records which are
217220
// currently "trailing", meaning that they're associated with a block
@@ -221,6 +224,20 @@ int llvm_test_dibuilder(void) {
221224
LLVMPositionBuilderAtEnd(Builder, FooEntryBlock);
222225
// Build `br label %vars` in entry.
223226
LLVMBuildBr(Builder, FooVarBlock);
227+
228+
// Build another br for the sake of testing labels.
229+
LLVMMetadataRef FooLabel2 = LLVMDIBuilderCreateLabel(DIB, FunctionMetadata,
230+
"label2", 6, File, 42, false);
231+
LLVMDIBuilderInsertLabelBefore(DIB, FooLabel2, FooParamLocation,
232+
LLVMBuildBr(Builder, FooVarBlock));
233+
// label3 will be emitted, but label4 won't be emitted
234+
// because label3 is AlwaysPreserve and label4 is not.
235+
LLVMDIBuilderCreateLabel(DIB, FunctionMetadata,
236+
"label3", 6, File, 42, true);
237+
LLVMDIBuilderCreateLabel(DIB, FunctionMetadata,
238+
"label4", 6, File, 42, false);
239+
LLVMDIBuilderFinalize(DIB);
240+
224241
// Build `ret i64 0` in vars.
225242
LLVMPositionBuilderAtEnd(Builder, FooVarBlock);
226243
LLVMTypeRef I64 = LLVMInt64TypeInContext(Ctx);

0 commit comments

Comments
 (0)