Skip to content

Commit 5c35b63

Browse files
authored
[MLIR][LLVM] add dwarfAddressSpace to DIDerivedType (#92043)
This field is present in LLVM, but was missing from the MLIR wrapper type. This addition allows MLIR languages to add proper DWARF info for GPU programs.
1 parent 3c2638d commit 5c35b63

File tree

9 files changed

+46
-17
lines changed

9 files changed

+46
-17
lines changed

mlir/include/mlir-c/Dialect/LLVM.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,16 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompositeTypeAttrGet(
241241
MlirAttribute baseType, int64_t flags, uint64_t sizeInBits,
242242
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements);
243243

244-
/// Creates a LLVM DIDerivedType attribute.
244+
/// Creates a LLVM DIDerivedType attribute. Note that `dwarfAddressSpace` is an
245+
/// optional field, where `MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL` indicates null
246+
/// and non-negative values indicate a value present.
245247
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
246248
MlirContext ctx, unsigned int tag, MlirAttribute name,
247249
MlirAttribute baseType, uint64_t sizeInBits, uint32_t alignInBits,
248-
uint64_t offsetInBits, MlirAttribute extraData);
250+
uint64_t offsetInBits, int64_t dwarfAddressSpace, MlirAttribute extraData);
251+
252+
/// Constant to represent std::nullopt for dwarfAddressSpace to omit the field.
253+
#define MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL -1
249254

250255
/// Gets the base type from a LLVM DIDerivedType attribute.
251256
MLIR_CAPI_EXPORTED MlirAttribute

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def LLVM_DIDerivedTypeAttr : LLVM_Attr<"DIDerivedType", "di_derived_type",
418418
OptionalParameter<"uint64_t">:$sizeInBits,
419419
OptionalParameter<"uint32_t">:$alignInBits,
420420
OptionalParameter<"uint64_t">:$offsetInBits,
421+
OptionalParameter<"std::optional<unsigned>">:$dwarfAddressSpace,
421422
OptionalParameter<"DINodeAttr">:$extraData
422423
);
423424
let assemblyFormat = "`<` struct(params) `>`";

mlir/lib/CAPI/Dialect/LLVM.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,17 @@ MlirAttribute mlirLLVMDICompositeTypeAttrGet(
176176
[](Attribute a) { return cast<DINodeAttr>(a); })));
177177
}
178178

179-
MlirAttribute
180-
mlirLLVMDIDerivedTypeAttrGet(MlirContext ctx, unsigned int tag,
181-
MlirAttribute name, MlirAttribute baseType,
182-
uint64_t sizeInBits, uint32_t alignInBits,
183-
uint64_t offsetInBits, MlirAttribute extraData) {
179+
MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
180+
MlirContext ctx, unsigned int tag, MlirAttribute name,
181+
MlirAttribute baseType, uint64_t sizeInBits, uint32_t alignInBits,
182+
uint64_t offsetInBits, int64_t dwarfAddressSpace, MlirAttribute extraData) {
183+
std::optional<unsigned> addressSpace = std::nullopt;
184+
if (dwarfAddressSpace >= 0)
185+
addressSpace = (unsigned)dwarfAddressSpace;
184186
return wrap(DIDerivedTypeAttr::get(
185187
unwrap(ctx), tag, cast<StringAttr>(unwrap(name)),
186188
cast<DITypeAttr>(unwrap(baseType)), sizeInBits, alignInBits, offsetInBits,
187-
cast<DINodeAttr>(unwrap(extraData))));
189+
addressSpace, cast<DINodeAttr>(unwrap(extraData))));
188190
}
189191

190192
MlirAttribute

mlir/lib/Target/LLVMIR/DebugImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
106106
return DIDerivedTypeAttr::get(
107107
context, node->getTag(), getStringAttrOrNull(node->getRawName()),
108108
baseType, node->getSizeInBits(), node->getAlignInBits(),
109-
node->getOffsetInBits(), extraData);
109+
node->getOffsetInBits(), node->getDWARFAddressSpace(), extraData);
110110
}
111111

112112
DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
165165
/*File=*/nullptr, /*Line=*/0,
166166
/*Scope=*/nullptr, translate(attr.getBaseType()), attr.getSizeInBits(),
167167
attr.getAlignInBits(), attr.getOffsetInBits(),
168-
/*DWARFAddressSpace=*/std::nullopt, /*PtrAuthData=*/std::nullopt,
168+
attr.getDwarfAddressSpace(), /*PtrAuthData=*/std::nullopt,
169169
/*Flags=*/llvm::DINode::FlagZero, translate(attr.getExtraData()));
170170
}
171171

mlir/test/CAPI/llvm.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,14 @@ static void testDebugInfoAttributes(MlirContext ctx) {
296296
mlirAttributeDump(mlirLLVMDILocalVariableAttrGet(ctx, compile_unit, foo, file,
297297
1, 0, 8, di_type));
298298
// CHECK: #llvm.di_derived_type<{{.*}}>
299+
// CHECK-NOT: dwarfAddressSpace
300+
mlirAttributeDump(mlirLLVMDIDerivedTypeAttrGet(
301+
ctx, 0, bar, di_type, 64, 8, 0, MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL,
302+
di_type));
303+
304+
// CHECK: #llvm.di_derived_type<{{.*}} dwarfAddressSpace = 3{{.*}}>
299305
mlirAttributeDump(
300-
mlirLLVMDIDerivedTypeAttrGet(ctx, 0, bar, di_type, 64, 8, 0, di_type));
306+
mlirLLVMDIDerivedTypeAttrGet(ctx, 0, bar, di_type, 64, 8, 0, 3, di_type));
301307

302308
// CHECK: #llvm.di_composite_type<{{.*}}>
303309
mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(

mlir/test/Dialect/LLVMIR/debuginfo.mlir

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
tag = DW_TAG_pointer_type, name = "ptr1"
3838
>
3939

40+
// CHECK-DAG: #[[PTR2:.*]] = #llvm.di_derived_type<tag = DW_TAG_pointer_type, baseType = #[[INT0]], sizeInBits = 64, alignInBits = 32, offsetInBits = 4, dwarfAddressSpace = 3, extraData = #[[INT1]]>
41+
#ptr2 = #llvm.di_derived_type<
42+
tag = DW_TAG_pointer_type, baseType = #int0,
43+
sizeInBits = 64, alignInBits = 32, offsetInBits = 4,
44+
dwarfAddressSpace = 3, extraData = #int1
45+
>
46+
4047
// CHECK-DAG: #[[COMP0:.*]] = #llvm.di_composite_type<tag = DW_TAG_array_type, name = "array0", line = 10, sizeInBits = 128, alignInBits = 32>
4148
#comp0 = #llvm.di_composite_type<
4249
tag = DW_TAG_array_type, name = "array0",
@@ -73,9 +80,9 @@
7380
flags = "TypePassByReference|NonTrivial"
7481
>
7582

76-
// CHECK-DAG: #[[SPTYPE0:.*]] = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #[[NULL]], #[[INT0]], #[[PTR0]], #[[PTR1]], #[[COMP0:.*]], #[[COMP1:.*]], #[[COMP2:.*]]>
83+
// CHECK-DAG: #[[SPTYPE0:.*]] = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #[[NULL]], #[[INT0]], #[[PTR0]], #[[PTR1]], #[[PTR2]], #[[COMP0:.*]], #[[COMP1:.*]], #[[COMP2:.*]]>
7784
#spType0 = #llvm.di_subroutine_type<
78-
callingConvention = DW_CC_normal, types = #null, #int0, #ptr0, #ptr1, #comp0, #comp1, #comp2
85+
callingConvention = DW_CC_normal, types = #null, #int0, #ptr0, #ptr1, #ptr2, #comp0, #comp1, #comp2
7986
>
8087

8188
// CHECK-DAG: #[[SPTYPE1:.*]] = #llvm.di_subroutine_type<types = #[[INT1]], #[[INT1]]>

mlir/test/Target/LLVMIR/Import/debug-info.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ define void @basic_type() !dbg !3 {
137137
; CHECK: #[[INT:.+]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "int">
138138
; CHECK: #[[PTR1:.+]] = #llvm.di_derived_type<tag = DW_TAG_pointer_type, baseType = #[[INT]]>
139139
; CHECK: #[[PTR2:.+]] = #llvm.di_derived_type<tag = DW_TAG_pointer_type, name = "mypointer", baseType = #[[INT]], sizeInBits = 64, alignInBits = 32, offsetInBits = 4, extraData = #[[INT]]>
140-
; CHECK: #llvm.di_subroutine_type<types = #[[PTR1]], #[[PTR2]]>
140+
; CHECK: #[[PTR3:.+]] = #llvm.di_derived_type<tag = DW_TAG_pointer_type, baseType = #[[INT]], dwarfAddressSpace = 3>
141+
; CHECK: #llvm.di_subroutine_type<types = #[[PTR1]], #[[PTR2]], #[[PTR3]]>
141142

142143
define void @derived_type() !dbg !3 {
143144
ret void
@@ -150,10 +151,11 @@ define void @derived_type() !dbg !3 {
150151
!2 = !DIFile(filename: "debug-info.ll", directory: "/")
151152
!3 = distinct !DISubprogram(name: "derived_type", scope: !2, file: !2, spFlags: DISPFlagDefinition, unit: !1, type: !4)
152153
!4 = !DISubroutineType(types: !5)
153-
!5 = !{!7, !8}
154+
!5 = !{!7, !8, !9}
154155
!6 = !DIBasicType(name: "int")
155156
!7 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6)
156157
!8 = !DIDerivedType(name: "mypointer", tag: DW_TAG_pointer_type, baseType: !6, size: 64, align: 32, offset: 4, extraData: !6)
158+
!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !6, dwarfAddressSpace: 3)
157159

158160
; // -----
159161

mlir/test/Target/LLVMIR/llvmir-debug.mlir

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ llvm.func @func_no_debug() {
3535
// Specify the name parameter.
3636
tag = DW_TAG_pointer_type, name = "named", baseType = #si32
3737
>
38+
#ptrWithAddressSpace = #llvm.di_derived_type<
39+
tag = DW_TAG_pointer_type, baseType = #si32,
40+
sizeInBits = 64, alignInBits = 32, offsetInBits = 8,
41+
dwarfAddressSpace = 3
42+
>
3843
#cu = #llvm.di_compile_unit<
3944
id = distinct[0]<>, sourceLanguage = DW_LANG_C, file = #file,
4045
producer = "MLIR", isOptimized = true, emissionKind = Full,
@@ -51,7 +56,7 @@ llvm.func @func_no_debug() {
5156
elements = #llvm.di_subrange<lowerBound = 0, upperBound = 4, stride = 1>
5257
>
5358
#null = #llvm.di_null_type
54-
#spType0 = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #null, #si64, #ptr, #named, #composite, #vector>
59+
#spType0 = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #null, #si64, #ptr, #named, #ptrWithAddressSpace, #composite, #vector>
5560
#toplevel_namespace = #llvm.di_namespace<
5661
name = "toplevel", exportSymbols = true
5762
>
@@ -138,11 +143,12 @@ llvm.func @empty_types() {
138143
// CHECK: ![[NESTED_NAMESPACE]] = !DINamespace(name: "nested", scope: ![[TOPLEVEL_NAMESPACE:.*]])
139144
// CHECK: ![[TOPLEVEL_NAMESPACE]] = !DINamespace(name: "toplevel", scope: null, exportSymbols: true)
140145
// CHECK: ![[FUNC_TYPE]] = !DISubroutineType(cc: DW_CC_normal, types: ![[FUNC_ARGS:.*]])
141-
// CHECK: ![[FUNC_ARGS]] = !{null, ![[ARG_TYPE:.*]], ![[PTR_TYPE:.*]], ![[NAMED_TYPE:.*]], ![[COMPOSITE_TYPE:.*]], ![[VECTOR_TYPE:.*]]}
146+
// CHECK: ![[FUNC_ARGS]] = !{null, ![[ARG_TYPE:.*]], ![[PTR_TYPE:.*]], ![[NAMED_TYPE:.*]], ![[PTR_WITH_ADDR_SPACE:.*]], ![[COMPOSITE_TYPE:.*]], ![[VECTOR_TYPE:.*]]}
142147
// CHECK: ![[ARG_TYPE]] = !DIBasicType(name: "si64")
143148
// CHECK: ![[PTR_TYPE]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[BASE_TYPE:.*]], size: 64, align: 32, offset: 8, extraData: ![[BASE_TYPE]])
144149
// CHECK: ![[BASE_TYPE]] = !DIBasicType(name: "si32", size: 32, encoding: DW_ATE_signed)
145150
// CHECK: ![[NAMED_TYPE]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "named", baseType: ![[BASE_TYPE:.*]])
151+
// CHECK: ![[PTR_WITH_ADDR_SPACE]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[BASE_TYPE:.*]], size: 64, align: 32, offset: 8, dwarfAddressSpace: 3)
146152
// CHECK: ![[COMPOSITE_TYPE]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "composite", file: ![[CU_FILE_LOC]], line: 42, size: 64, align: 32, elements: ![[COMPOSITE_ELEMENTS:.*]])
147153
// CHECK: ![[COMPOSITE_ELEMENTS]] = !{![[COMPOSITE_ELEMENT:.*]]}
148154
// CHECK: ![[COMPOSITE_ELEMENT]] = !DISubrange(count: 4)

0 commit comments

Comments
 (0)