Skip to content

Commit 165acd3

Browse files
authored
[LLVM-C] Support debug info for enumerators of arbitrary sizes (#76735)
Since `LLVMDIBuilderCreateEnumerator` only supports up to 64 bits, this PR adds a new `LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision` function that takes an arbitrary number of words, based on `LLVMConstIntOfArbitraryPrecision`. This allows even larger enumeration types to represent their values exactly. (It seems LLVM should already support i128 enums since 13.0.0.)
1 parent 419a2cb commit 165acd3

File tree

5 files changed

+101
-49
lines changed

5 files changed

+101
-49
lines changed

llvm/docs/ReleaseNotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ Changes to the C API
210210
reading `ConstantDataArray` values without needing extra `LLVMValueRef`s for
211211
individual elements.
212212

213+
* Added ``LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision`` for creating
214+
debugging metadata of enumerators larger than 64 bits.
215+
213216
Changes to the CodeGen infrastructure
214217
-------------------------------------
215218

llvm/include/llvm-c/DebugInfo.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,19 @@ LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder,
629629
int64_t Value,
630630
LLVMBool IsUnsigned);
631631

632+
/**
633+
* Create debugging information entry for an enumerator of arbitrary precision.
634+
* @param Builder The DIBuilder.
635+
* @param Name Enumerator name.
636+
* @param NameLen Length of enumerator name.
637+
* @param SizeInBits Number of bits of the value.
638+
* @param Words The words that make up the value.
639+
* @param IsUnsigned True if the value is unsigned.
640+
*/
641+
LLVMMetadataRef LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision(
642+
LLVMDIBuilderRef Builder, const char *Name, size_t NameLen,
643+
uint64_t SizeInBits, const uint64_t Words[], LLVMBool IsUnsigned);
644+
632645
/**
633646
* Create debugging information entry for an enumeration.
634647
* \param Builder The DIBuilder.

llvm/lib/IR/DebugInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "llvm-c/DebugInfo.h"
1515
#include "LLVMContextImpl.h"
16+
#include "llvm/ADT/APSInt.h"
1617
#include "llvm/ADT/DenseMap.h"
1718
#include "llvm/ADT/DenseSet.h"
1819
#include "llvm/ADT/STLExtras.h"
@@ -1297,6 +1298,15 @@ LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder,
12971298
IsUnsigned != 0));
12981299
}
12991300

1301+
LLVMMetadataRef LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision(
1302+
LLVMDIBuilderRef Builder, const char *Name, size_t NameLen,
1303+
uint64_t SizeInBits, const uint64_t Words[], LLVMBool IsUnsigned) {
1304+
uint64_t NumWords = (SizeInBits + 63) / 64;
1305+
return wrap(unwrap(Builder)->createEnumerator(
1306+
{Name, NameLen},
1307+
APSInt(APInt(SizeInBits, ArrayRef(Words, NumWords)), IsUnsigned != 0)));
1308+
}
1309+
13001310
LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
13011311
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
13021312
size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,

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

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@
44
; CHECK: ; ModuleID = 'debuginfo.c'
55
; CHECK-NEXT: source_filename = "debuginfo.c"
66

7-
; CHECK: define i64 @foo(i64 %0, i64 %1, <10 x i64> %2) !dbg !31 {
7+
; CHECK: define i64 @foo(i64 %0, i64 %1, <10 x i64> %2) !dbg !36 {
88
; CHECK-NEXT: entry:
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)
9+
; CHECK-NEXT: #dbg_declare(i64 0, !43, !DIExpression(), !50)
10+
; CHECK-NEXT: #dbg_declare(i64 0, !44, !DIExpression(), !50)
11+
; CHECK-NEXT: #dbg_declare(i64 0, !45, !DIExpression(), !50)
12+
; CHECK-NEXT: #dbg_label(!51, !50)
1313
; CHECK-NEXT: br label %vars
14-
; CHECK-NEXT: #dbg_label(!47, !45)
14+
; CHECK-NEXT: #dbg_label(!52, !50)
1515
; CHECK-NEXT: br label %vars
1616
; CHECK: vars:
1717
; CHECK-NEXT: %p1 = phi i64 [ 0, %entry ]
1818
; CHECK-NEXT: %p2 = phi i64 [ 0, %entry ]
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)
19+
; CHECK-NEXT: #dbg_value(i64 0, !46, !DIExpression(DW_OP_constu, 0, DW_OP_stack_value), !53)
20+
; CHECK-NEXT: #dbg_value(i64 1, !48, !DIExpression(DW_OP_constu, 1, DW_OP_stack_value), !53)
2121
; CHECK-NEXT: %a = add i64 %p1, %p2
2222
; CHECK-NEXT: ret i64 0
2323
; CHECK-NEXT: }
2424

2525
; CHECK: !llvm.dbg.cu = !{!0}
26-
; CHECK-NEXT: !FooType = !{!28}
26+
; CHECK-NEXT: !FooType = !{!33}
2727
; CHECK-NEXT: !EnumTest = !{!3}
28+
; CHECK-NEXT: !LargeEnumTest = !{!11}
2829

29-
; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "llvm-c-test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !11, imports: !19, macros: !23, splitDebugInlining: false, sysroot: "/")
30+
; CHECK: !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "llvm-c-test", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !16, imports: !24, macros: !28, splitDebugInlining: false, sysroot: "/")
3031
; CHECK-NEXT: !1 = !DIFile(filename: "debuginfo.c", directory: ".")
31-
; CHECK-NEXT: !2 = !{!3}
32+
; CHECK-NEXT: !2 = !{!3, !11}
3233
; CHECK-NEXT: !3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "EnumTest", scope: !4, file: !1, baseType: !6, size: 64, elements: !7)
3334
; CHECK-NEXT: !4 = !DINamespace(name: "NameSpace", scope: !5)
3435
; CHECK-NEXT: !5 = !DIModule(scope: null, name: "llvm-c-test", includePath: "/test/include/llvm-c-test.h")
@@ -37,41 +38,46 @@
3738
; CHECK-NEXT: !8 = !DIEnumerator(name: "Test_A", value: 0, isUnsigned: true)
3839
; CHECK-NEXT: !9 = !DIEnumerator(name: "Test_B", value: 1, isUnsigned: true)
3940
; CHECK-NEXT: !10 = !DIEnumerator(name: "Test_B", value: 2, isUnsigned: true)
40-
; CHECK-NEXT: !11 = !{!12, !16}
41-
; CHECK-NEXT: !12 = !DIGlobalVariableExpression(var: !13, expr: !DIExpression(DW_OP_constu, 0, DW_OP_stack_value))
42-
; CHECK-NEXT: !13 = distinct !DIGlobalVariable(name: "globalClass", scope: !5, file: !1, line: 1, type: !14, isLocal: true, isDefinition: true)
43-
; CHECK-NEXT: !14 = !DICompositeType(tag: DW_TAG_structure_type, name: "TestClass", scope: !1, file: !1, line: 42, size: 64, flags: DIFlagObjcClassComplete, elements: !15)
44-
; CHECK-NEXT: !15 = !{}
45-
; CHECK-NEXT: !16 = !DIGlobalVariableExpression(var: !17, expr: !DIExpression(DW_OP_constu, 0, DW_OP_stack_value))
46-
; CHECK-NEXT: !17 = distinct !DIGlobalVariable(name: "global", scope: !5, file: !1, line: 1, type: !18, isLocal: true, isDefinition: true)
47-
; CHECK-NEXT: !18 = !DIDerivedType(tag: DW_TAG_typedef, name: "int64_t", scope: !1, file: !1, line: 42, baseType: !6)
48-
; CHECK-NEXT: !19 = !{!20, !22}
49-
; CHECK-NEXT: !20 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !5, entity: !21, file: !1, line: 42)
50-
; CHECK-NEXT: !21 = !DIModule(scope: null, name: "llvm-c-test-import", includePath: "/test/include/llvm-c-test-import.h")
51-
; CHECK-NEXT: !22 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !5, entity: !20, file: !1, line: 42)
52-
; CHECK-NEXT: !23 = !{!24}
53-
; CHECK-NEXT: !24 = !DIMacroFile(file: !1, nodes: !25)
54-
; CHECK-NEXT: !25 = !{!26, !27}
55-
; CHECK-NEXT: !26 = !DIMacro(type: DW_MACINFO_define, name: "SIMPLE_DEFINE")
56-
; CHECK-NEXT: !27 = !DIMacro(type: DW_MACINFO_define, name: "VALUE_DEFINE", value: "1")
57-
; CHECK-NEXT: !28 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !29, size: 192, dwarfAddressSpace: 0)
58-
; CHECK-NEXT: !29 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyStruct", scope: !4, file: !1, size: 192, elements: !30, runtimeLang: DW_LANG_C89, identifier: "MyStruct")
59-
; CHECK-NEXT: !30 = !{!6, !6, !6}
60-
; CHECK-NEXT: !31 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !1, file: !1, line: 42, type: !32, scopeLine: 42, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !0, retainedNodes: !37)
61-
; CHECK-NEXT: !32 = !DISubroutineType(types: !33)
62-
; CHECK-NEXT: !33 = !{!6, !6, !34}
63-
; CHECK-NEXT: !34 = !DICompositeType(tag: DW_TAG_array_type, baseType: !6, size: 640, flags: DIFlagVector, elements: !35)
64-
; CHECK-NEXT: !35 = !{!36}
65-
; CHECK-NEXT: !36 = !DISubrange(count: 10, lowerBound: 0)
66-
; CHECK-NEXT: !37 = !{!38, !39, !40, !41, !43, !44}
67-
; CHECK-NEXT: !38 = !DILocalVariable(name: "a", arg: 1, scope: !31, file: !1, line: 42, type: !6)
68-
; CHECK-NEXT: !39 = !DILocalVariable(name: "b", arg: 2, scope: !31, file: !1, line: 42, type: !6)
69-
; CHECK-NEXT: !40 = !DILocalVariable(name: "c", arg: 3, scope: !31, file: !1, line: 42, type: !34)
70-
; CHECK-NEXT: !41 = !DILocalVariable(name: "d", scope: !42, file: !1, line: 43, type: !6)
71-
; CHECK-NEXT: !42 = distinct !DILexicalBlock(scope: !31, file: !1, line: 42)
72-
; CHECK-NEXT: !43 = !DILocalVariable(name: "e", scope: !42, file: !1, line: 44, type: !6)
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)
41+
; CHECK-NEXT: !11 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "LargeEnumTest", scope: !4, file: !1, baseType: !12, size: 128, elements: !13)
42+
; CHECK-NEXT: !12 = !DIBasicType(name: "UInt128", size: 128)
43+
; CHECK-NEXT: !13 = !{!14, !15}
44+
; CHECK-NEXT: !14 = !DIEnumerator(name: "Test_D", value: 100000000000000000000000000000000000000)
45+
; CHECK-NEXT: !15 = !DIEnumerator(name: "Test_E", value: -1)
46+
; CHECK-NEXT: !16 = !{!17, !21}
47+
; CHECK-NEXT: !17 = !DIGlobalVariableExpression(var: !18, expr: !DIExpression(DW_OP_constu, 0, DW_OP_stack_value))
48+
; CHECK-NEXT: !18 = distinct !DIGlobalVariable(name: "globalClass", scope: !5, file: !1, line: 1, type: !19, isLocal: true, isDefinition: true)
49+
; CHECK-NEXT: !19 = !DICompositeType(tag: DW_TAG_structure_type, name: "TestClass", scope: !1, file: !1, line: 42, size: 64, flags: DIFlagObjcClassComplete, elements: !20)
50+
; CHECK-NEXT: !20 = !{}
51+
; CHECK-NEXT: !21 = !DIGlobalVariableExpression(var: !22, expr: !DIExpression(DW_OP_constu, 0, DW_OP_stack_value))
52+
; CHECK-NEXT: !22 = distinct !DIGlobalVariable(name: "global", scope: !5, file: !1, line: 1, type: !23, isLocal: true, isDefinition: true)
53+
; CHECK-NEXT: !23 = !DIDerivedType(tag: DW_TAG_typedef, name: "int64_t", scope: !1, file: !1, line: 42, baseType: !6)
54+
; CHECK-NEXT: !24 = !{!25, !27}
55+
; CHECK-NEXT: !25 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !5, entity: !26, file: !1, line: 42)
56+
; CHECK-NEXT: !26 = !DIModule(scope: null, name: "llvm-c-test-import", includePath: "/test/include/llvm-c-test-import.h")
57+
; CHECK-NEXT: !27 = !DIImportedEntity(tag: DW_TAG_imported_module, scope: !5, entity: !25, file: !1, line: 42)
58+
; CHECK-NEXT: !28 = !{!29}
59+
; CHECK-NEXT: !29 = !DIMacroFile(file: !1, nodes: !30)
60+
; CHECK-NEXT: !30 = !{!31, !32}
61+
; CHECK-NEXT: !31 = !DIMacro(type: DW_MACINFO_define, name: "SIMPLE_DEFINE")
62+
; CHECK-NEXT: !32 = !DIMacro(type: DW_MACINFO_define, name: "VALUE_DEFINE", value: "1")
63+
; CHECK-NEXT: !33 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !34, size: 192, dwarfAddressSpace: 0)
64+
; CHECK-NEXT: !34 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyStruct", scope: !4, file: !1, size: 192, elements: !35, runtimeLang: DW_LANG_C89, identifier: "MyStruct")
65+
; CHECK-NEXT: !35 = !{!6, !6, !6}
66+
; CHECK-NEXT: !36 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: !1, file: !1, line: 42, type: !37, scopeLine: 42, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition, unit: !0, retainedNodes: !42)
67+
; CHECK-NEXT: !37 = !DISubroutineType(types: !38)
68+
; CHECK-NEXT: !38 = !{!6, !6, !39}
69+
; CHECK-NEXT: !39 = !DICompositeType(tag: DW_TAG_array_type, baseType: !6, size: 640, flags: DIFlagVector, elements: !40)
70+
; CHECK-NEXT: !40 = !{!41}
71+
; CHECK-NEXT: !41 = !DISubrange(count: 10, lowerBound: 0)
72+
; CHECK-NEXT: !42 = !{!43, !44, !45, !46, !48, !49}
73+
; CHECK-NEXT: !43 = !DILocalVariable(name: "a", arg: 1, scope: !36, file: !1, line: 42, type: !6)
74+
; CHECK-NEXT: !44 = !DILocalVariable(name: "b", arg: 2, scope: !36, file: !1, line: 42, type: !6)
75+
; CHECK-NEXT: !45 = !DILocalVariable(name: "c", arg: 3, scope: !36, file: !1, line: 42, type: !39)
76+
; CHECK-NEXT: !46 = !DILocalVariable(name: "d", scope: !47, file: !1, line: 43, type: !6)
77+
; CHECK-NEXT: !47 = distinct !DILexicalBlock(scope: !36, file: !1, line: 42)
78+
; CHECK-NEXT: !48 = !DILocalVariable(name: "e", scope: !47, file: !1, line: 44, type: !6)
79+
; CHECK-NEXT: !49 = !DILabel(scope: !36, name: "label3", file: !1, line: 42)
80+
; CHECK-NEXT: !50 = !DILocation(line: 42, scope: !36)
81+
; CHECK-NEXT: !51 = !DILabel(scope: !36, name: "label1", file: !1, line: 42)
82+
; CHECK-NEXT: !52 = !DILabel(scope: !36, name: "label2", file: !1, line: 42)
83+
; CHECK-NEXT: !53 = !DILocation(line: 43, scope: !36)

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,26 @@ int llvm_test_dibuilder(void) {
215215
LLVMAddNamedMetadataOperand(
216216
M, "EnumTest", LLVMMetadataAsValue(LLVMGetModuleContext(M), EnumTest));
217217

218+
LLVMMetadataRef UInt128Ty = LLVMDIBuilderCreateBasicType(
219+
DIB, "UInt128", strlen("UInt128"), 128, 0, LLVMDIFlagZero);
220+
const uint64_t WordsTestD[] = {0x098a224000000000ull, 0x4b3b4ca85a86c47aull};
221+
const uint64_t WordsTestE[] = {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull};
222+
223+
LLVMMetadataRef LargeEnumeratorTestD =
224+
LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision(
225+
DIB, "Test_D", strlen("Test_D"), 128, WordsTestD, false);
226+
LLVMMetadataRef LargeEnumeratorTestE =
227+
LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision(
228+
DIB, "Test_E", strlen("Test_E"), 128, WordsTestE, false);
229+
LLVMMetadataRef LargeEnumeratorsTest[] = {LargeEnumeratorTestD,
230+
LargeEnumeratorTestE};
231+
LLVMMetadataRef LargeEnumTest = LLVMDIBuilderCreateEnumerationType(
232+
DIB, NameSpace, "LargeEnumTest", strlen("LargeEnumTest"), File, 0, 128, 0,
233+
LargeEnumeratorsTest, 2, UInt128Ty);
234+
LLVMAddNamedMetadataOperand(
235+
M, "LargeEnumTest",
236+
LLVMMetadataAsValue(LLVMGetModuleContext(M), LargeEnumTest));
237+
218238
// Using the new debug format, debug records get attached to instructions.
219239
// Insert a `br` and `ret` now to absorb the debug records which are
220240
// currently "trailing", meaning that they're associated with a block

0 commit comments

Comments
 (0)