Skip to content

Commit d08eaa2

Browse files
abidhgysit
authored andcommitted
[mlir] Add missing fields in DICompositeTypeAttr. (llvm#93226)
The fortran arrays use 'dataLocation', 'rank', 'allocated' and 'associated' fields of the DICompositeType. These were not available in 'DICompositeTypeAttr'. This PR adds the missing fields. --------- Co-authored-by: Tobias Gysi <[email protected]>
1 parent 3843d61 commit d08eaa2

File tree

11 files changed

+119
-17
lines changed

11 files changed

+119
-17
lines changed

flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertSequenceType(
7373
context, llvm::dwarf::DW_TAG_array_type, /*recursive id*/ {},
7474
/* name */ nullptr, /* file */ nullptr, /* line */ 0, /* scope */ nullptr,
7575
elemTy, mlir::LLVM::DIFlags::Zero, /* sizeInBits */ 0,
76-
/*alignInBits*/ 0, elements);
76+
/*alignInBits*/ 0, elements, /* dataLocation */ nullptr,
77+
/* rank */ nullptr, /* allocated */ nullptr,
78+
/* associated */ nullptr);
7779
}
7880

7981
mlir::LLVM::DITypeAttr

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompositeTypeAttrGet(
239239
MlirContext ctx, unsigned int tag, MlirAttribute recId, MlirAttribute name,
240240
MlirAttribute file, uint32_t line, MlirAttribute scope,
241241
MlirAttribute baseType, int64_t flags, uint64_t sizeInBits,
242-
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements);
242+
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements,
243+
MlirAttribute dataLocation, MlirAttribute rank, MlirAttribute allocated,
244+
MlirAttribute associated);
243245

244246
/// Creates a LLVM DIDerivedType attribute. Note that `dwarfAddressSpace` is an
245247
/// optional field, where `MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL` indicates null

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,11 @@ def LLVM_DICompositeTypeAttr : LLVM_Attr<"DICompositeType", "di_composite_type",
385385
OptionalParameter<"DIFlags", "DIFlags::Zero">:$flags,
386386
OptionalParameter<"uint64_t">:$sizeInBits,
387387
OptionalParameter<"uint64_t">:$alignInBits,
388-
OptionalArrayRefParameter<"DINodeAttr">:$elements
388+
OptionalArrayRefParameter<"DINodeAttr">:$elements,
389+
OptionalParameter<"DIExpressionAttr">:$dataLocation,
390+
OptionalParameter<"DIExpressionAttr">:$rank,
391+
OptionalParameter<"DIExpressionAttr">:$allocated,
392+
OptionalParameter<"DIExpressionAttr">:$associated
389393
);
390394
let assemblyFormat = "`<` struct(params) `>`";
391395
let extraClassDeclaration = [{

mlir/lib/CAPI/Dialect/LLVM.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ MlirAttribute mlirLLVMDICompositeTypeAttrGet(
163163
MlirContext ctx, unsigned int tag, MlirAttribute recId, MlirAttribute name,
164164
MlirAttribute file, uint32_t line, MlirAttribute scope,
165165
MlirAttribute baseType, int64_t flags, uint64_t sizeInBits,
166-
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements) {
166+
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements,
167+
MlirAttribute dataLocation, MlirAttribute rank, MlirAttribute allocated,
168+
MlirAttribute associated) {
167169
SmallVector<Attribute> elementsStorage;
168170
elementsStorage.reserve(nElements);
169171

@@ -173,7 +175,11 @@ MlirAttribute mlirLLVMDICompositeTypeAttrGet(
173175
cast<DIScopeAttr>(unwrap(scope)), cast<DITypeAttr>(unwrap(baseType)),
174176
DIFlags(flags), sizeInBits, alignInBits,
175177
llvm::map_to_vector(unwrapList(nElements, elements, elementsStorage),
176-
[](Attribute a) { return cast<DINodeAttr>(a); })));
178+
[](Attribute a) { return cast<DINodeAttr>(a); }),
179+
cast<DIExpressionAttr>(unwrap(dataLocation)),
180+
cast<DIExpressionAttr>(unwrap(rank)),
181+
cast<DIExpressionAttr>(unwrap(allocated)),
182+
cast<DIExpressionAttr>(unwrap(associated))));
177183
}
178184

179185
MlirAttribute mlirLLVMDIDerivedTypeAttrGet(

mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,17 @@ void printExpressionArg(AsmPrinter &printer, uint64_t opcode,
192192

193193
DIRecursiveTypeAttrInterface
194194
DICompositeTypeAttr::withRecId(DistinctAttr recId) {
195-
return DICompositeTypeAttr::get(getContext(), getTag(), recId, getName(),
196-
getFile(), getLine(), getScope(),
197-
getBaseType(), getFlags(), getSizeInBits(),
198-
getAlignInBits(), getElements());
195+
return DICompositeTypeAttr::get(
196+
getContext(), getTag(), recId, getName(), getFile(), getLine(),
197+
getScope(), getBaseType(), getFlags(), getSizeInBits(), getAlignInBits(),
198+
getElements(), getDataLocation(), getRank(), getAllocated(),
199+
getAssociated());
199200
}
200201

201202
DIRecursiveTypeAttrInterface
202203
DICompositeTypeAttr::getRecSelf(DistinctAttr recId) {
203204
return DICompositeTypeAttr::get(recId.getContext(), 0, recId, {}, {}, 0, {},
204-
{}, DIFlags(), 0, 0, {});
205+
{}, DIFlags(), 0, 0, {}, {}, {}, {}, {});
205206
}
206207

207208
//===----------------------------------------------------------------------===//

mlir/lib/Target/LLVMIR/DebugImporter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
9393
getStringAttrOrNull(node->getRawName()), translate(node->getFile()),
9494
node->getLine(), translate(node->getScope()), baseType,
9595
flags.value_or(DIFlags::Zero), node->getSizeInBits(),
96-
node->getAlignInBits(), elements);
96+
node->getAlignInBits(), elements,
97+
translateExpression(node->getDataLocationExp()),
98+
translateExpression(node->getRankExp()),
99+
translateExpression(node->getAllocatedExp()),
100+
translateExpression(node->getAssociatedExp()));
97101
}
98102

99103
DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
@@ -456,6 +460,9 @@ Location DebugImporter::translateLoc(llvm::DILocation *loc) {
456460
}
457461

458462
DIExpressionAttr DebugImporter::translateExpression(llvm::DIExpression *node) {
463+
if (!node)
464+
return nullptr;
465+
459466
SmallVector<DIExpressionElemAttr> ops;
460467

461468
// Begin processing the operations.

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
8383
return nullptr;
8484
}
8585

86+
llvm::DIExpression *
87+
DebugTranslation::getExpressionAttrOrNull(DIExpressionAttr attr) {
88+
if (!attr)
89+
return nullptr;
90+
return translateExpression(attr);
91+
}
92+
8693
llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
8794
if (!stringAttr || stringAttr.empty())
8895
return nullptr;
@@ -156,7 +163,13 @@ DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
156163
/*OffsetInBits=*/0,
157164
/*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
158165
llvm::MDNode::get(llvmCtx, elements),
159-
/*RuntimeLang=*/0, /*VTableHolder=*/nullptr);
166+
/*RuntimeLang=*/0, /*VTableHolder=*/nullptr,
167+
/*TemplateParams=*/nullptr, /*Identifier=*/nullptr,
168+
/*Discriminator=*/nullptr,
169+
getExpressionAttrOrNull(attr.getDataLocation()),
170+
getExpressionAttrOrNull(attr.getAssociated()),
171+
getExpressionAttrOrNull(attr.getAllocated()),
172+
getExpressionAttrOrNull(attr.getRank()));
160173
}
161174

162175
llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {

mlir/lib/Target/LLVMIR/DebugTranslation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class DebugTranslation {
103103
/// nullptr if `stringAttr` is null or contains and empty string.
104104
llvm::MDString *getMDStringOrNull(StringAttr stringAttr);
105105

106+
/// Constructs a DIExpression metadata node from the DIExpressionAttr. Returns
107+
/// nullptr if `DIExpressionAttr` is null.
108+
llvm::DIExpression *getExpressionAttrOrNull(DIExpressionAttr attr);
109+
106110
/// A mapping between mlir location+scope and the corresponding llvm debug
107111
/// metadata.
108112
DenseMap<std::tuple<Location, llvm::DILocalScope *, const llvm::DILocation *>,

mlir/test/CAPI/llvm.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,6 @@ static void testDebugInfoAttributes(MlirContext ctx) {
305305
mlirAttributeDump(
306306
mlirLLVMDIDerivedTypeAttrGet(ctx, 0, bar, di_type, 64, 8, 0, 3, di_type));
307307

308-
// CHECK: #llvm.di_composite_type<{{.*}}>
309-
mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(
310-
ctx, 0, id, foo, file, 1, compile_unit, di_type, 0, 64, 8, 1, &di_type));
311-
312308
MlirAttribute subroutine_type =
313309
mlirLLVMDISubroutineTypeAttrGet(ctx, 0x0, 1, &di_type);
314310

@@ -336,8 +332,15 @@ static void testDebugInfoAttributes(MlirContext ctx) {
336332
// CHECK: #llvm<di_expression_elem(1)>
337333
mlirAttributeDump(expression_elem);
338334

335+
MlirAttribute expression =
336+
mlirLLVMDIExpressionAttrGet(ctx, 1, &expression_elem);
339337
// CHECK: #llvm.di_expression<[(1)]>
340-
mlirAttributeDump(mlirLLVMDIExpressionAttrGet(ctx, 1, &expression_elem));
338+
mlirAttributeDump(expression);
339+
340+
// CHECK: #llvm.di_composite_type<{{.*}}>
341+
mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(
342+
ctx, 0, id, foo, file, 1, compile_unit, di_type, 0, 64, 8, 1, &di_type,
343+
expression, expression, expression, expression));
341344
}
342345

343346
int main(void) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: mlir-translate -import-llvm -mlir-print-debuginfo %s | FileCheck %s
2+
3+
define void @fn_with_composite() !dbg !3 {
4+
ret void
5+
}
6+
7+
!llvm.dbg.cu = !{!1}
8+
!llvm.module.flags = !{!0}
9+
!0 = !{i32 2, !"Debug Info Version", i32 3}
10+
!1 = distinct !DICompileUnit(language: DW_LANG_Fortran95, file: !2, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
11+
!2 = !DIFile(filename: "test.f90", directory: "")
12+
!3 = distinct !DISubprogram(name: "fn_with_composite", scope: !1, file: !2, type: !4, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !1)
13+
!4 = !DISubroutineType(cc: DW_CC_normal, types: !5)
14+
!5 = !{null, !6}
15+
!6 = !DICompositeType(tag: DW_TAG_array_type, baseType: !7, elements: !8, dataLocation: !DIExpression(DW_OP_push_object_address, DW_OP_deref), associated: !DIExpression(DW_OP_lit0, DW_OP_eq), allocated: !DIExpression(DW_OP_lit0, DW_OP_ne), rank: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 16, DW_OP_deref))
16+
!7 = !DIBasicType(name: "integer", size: 32, encoding: DW_ATE_signed)
17+
!8 = !{!9}
18+
!9 = !DISubrange(count: 5, lowerBound: 1)
19+
!10 = !DILocation(line: 26, column: 3, scope: !3)
20+
21+
; CHECK: #llvm.di_composite_type<tag = DW_TAG_array_type,
22+
; CHECK-SAME: dataLocation = <[DW_OP_push_object_address, DW_OP_deref]>
23+
; CHECK-SAME: rank = <[DW_OP_push_object_address, DW_OP_plus_uconst(16), DW_OP_deref]>
24+
; CHECK-SAME: allocated = <[DW_OP_lit0, DW_OP_ne]>
25+
; CHECK-SAME: associated = <[DW_OP_lit0, DW_OP_eq]>

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,38 @@ llvm.mlir.global @global_variable() {dbg_expr = #di_global_variable_expression}
484484
// CHECK: ![[SCOPE]] = !DISubprogram({{.*}}type: ![[SUBROUTINE:[0-9]+]],
485485
// CHECK: ![[SUBROUTINE]] = !DISubroutineType(types: ![[SR_TYPES:[0-9]+]])
486486
// CHECK: ![[SR_TYPES]] = !{![[COMP]]}
487+
488+
// -----
489+
490+
#file = #llvm.di_file<"test.f90" in "">
491+
#cu = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_Fortran95,
492+
file = #file, producer = "", isOptimized = false, emissionKind = Full>
493+
#i32 = #llvm.di_basic_type<
494+
tag = DW_TAG_base_type, name = "integer",
495+
sizeInBits = 32, encoding = DW_ATE_signed
496+
>
497+
#null = #llvm.di_null_type
498+
#alloc = #llvm.di_expression<[DW_OP_lit0, DW_OP_ne]>
499+
#assoc = #llvm.di_expression<[DW_OP_lit0, DW_OP_eq]>
500+
#rank = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(16), DW_OP_deref]>
501+
#datal = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_deref]>
502+
#array = #llvm.di_composite_type<tag = DW_TAG_array_type,
503+
baseType = #i32,
504+
dataLocation = #datal, rank = #rank,
505+
allocated = #alloc, associated = #assoc,
506+
elements = #llvm.di_subrange<lowerBound = 1, count = 5>
507+
>
508+
#spType0 = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #null, #array>
509+
#sp0 = #llvm.di_subprogram<
510+
compileUnit = #cu, scope = #cu, name = "fn_with_composite", file = #file,
511+
subprogramFlags = "Definition|Optimized", type = #spType0
512+
>
513+
llvm.func @fn_with_composite() {
514+
llvm.return
515+
}loc(fused<#sp0>["foo.mlir":1:1])
516+
// CHECK-LABEL: define void @fn_with_composite()
517+
// CHECK: !DICompositeType(
518+
// CHECK-SAME: dataLocation: !DIExpression(DW_OP_push_object_address, DW_OP_deref)
519+
// CHECK-SAME: associated: !DIExpression(DW_OP_lit0, DW_OP_eq)
520+
// CHECK-SAME: allocated: !DIExpression(DW_OP_lit0, DW_OP_ne)
521+
// CHECK-SAME: rank: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 16, DW_OP_deref)

0 commit comments

Comments
 (0)