Skip to content

[mlir] Add missing fields in DICompositeTypeAttr. #93226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertSequenceType(
context, llvm::dwarf::DW_TAG_array_type, /*recursive id*/ {},
/* name */ nullptr, /* file */ nullptr, /* line */ 0, /* scope */ nullptr,
elemTy, mlir::LLVM::DIFlags::Zero, /* sizeInBits */ 0,
/*alignInBits*/ 0, elements);
/*alignInBits*/ 0, elements, /* dataLocation */ nullptr,
/* rank */ nullptr, /* allocated */ nullptr,
/* associated */ nullptr);
}

mlir::LLVM::DITypeAttr
Expand Down
4 changes: 3 additions & 1 deletion mlir/include/mlir-c/Dialect/LLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompositeTypeAttrGet(
MlirContext ctx, unsigned int tag, MlirAttribute recId, MlirAttribute name,
MlirAttribute file, uint32_t line, MlirAttribute scope,
MlirAttribute baseType, int64_t flags, uint64_t sizeInBits,
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements);
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements,
MlirAttribute dataLocation, MlirAttribute rank, MlirAttribute allocated,
MlirAttribute associated);

/// Creates a LLVM DIDerivedType attribute. Note that `dwarfAddressSpace` is an
/// optional field, where `MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL` indicates null
Expand Down
6 changes: 5 additions & 1 deletion mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ def LLVM_DICompositeTypeAttr : LLVM_Attr<"DICompositeType", "di_composite_type",
OptionalParameter<"DIFlags", "DIFlags::Zero">:$flags,
OptionalParameter<"uint64_t">:$sizeInBits,
OptionalParameter<"uint64_t">:$alignInBits,
OptionalArrayRefParameter<"DINodeAttr">:$elements
OptionalArrayRefParameter<"DINodeAttr">:$elements,
OptionalParameter<"DIExpressionAttr">:$dataLocation,
OptionalParameter<"DIExpressionAttr">:$rank,
OptionalParameter<"DIExpressionAttr">:$allocated,
OptionalParameter<"DIExpressionAttr">:$associated
);
let assemblyFormat = "`<` struct(params) `>`";
let extraClassDeclaration = [{
Expand Down
10 changes: 8 additions & 2 deletions mlir/lib/CAPI/Dialect/LLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ MlirAttribute mlirLLVMDICompositeTypeAttrGet(
MlirContext ctx, unsigned int tag, MlirAttribute recId, MlirAttribute name,
MlirAttribute file, uint32_t line, MlirAttribute scope,
MlirAttribute baseType, int64_t flags, uint64_t sizeInBits,
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements) {
uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements,
MlirAttribute dataLocation, MlirAttribute rank, MlirAttribute allocated,
MlirAttribute associated) {
SmallVector<Attribute> elementsStorage;
elementsStorage.reserve(nElements);

Expand All @@ -173,7 +175,11 @@ MlirAttribute mlirLLVMDICompositeTypeAttrGet(
cast<DIScopeAttr>(unwrap(scope)), cast<DITypeAttr>(unwrap(baseType)),
DIFlags(flags), sizeInBits, alignInBits,
llvm::map_to_vector(unwrapList(nElements, elements, elementsStorage),
[](Attribute a) { return cast<DINodeAttr>(a); })));
[](Attribute a) { return cast<DINodeAttr>(a); }),
cast<DIExpressionAttr>(unwrap(dataLocation)),
cast<DIExpressionAttr>(unwrap(rank)),
cast<DIExpressionAttr>(unwrap(allocated)),
cast<DIExpressionAttr>(unwrap(associated))));
}

MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
Expand Down
11 changes: 6 additions & 5 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,17 @@ void printExpressionArg(AsmPrinter &printer, uint64_t opcode,

DIRecursiveTypeAttrInterface
DICompositeTypeAttr::withRecId(DistinctAttr recId) {
return DICompositeTypeAttr::get(getContext(), getTag(), recId, getName(),
getFile(), getLine(), getScope(),
getBaseType(), getFlags(), getSizeInBits(),
getAlignInBits(), getElements());
return DICompositeTypeAttr::get(
getContext(), getTag(), recId, getName(), getFile(), getLine(),
getScope(), getBaseType(), getFlags(), getSizeInBits(), getAlignInBits(),
getElements(), getDataLocation(), getRank(), getAllocated(),
getAssociated());
}

DIRecursiveTypeAttrInterface
DICompositeTypeAttr::getRecSelf(DistinctAttr recId) {
return DICompositeTypeAttr::get(recId.getContext(), 0, recId, {}, {}, 0, {},
{}, DIFlags(), 0, 0, {});
{}, DIFlags(), 0, 0, {}, {}, {}, {}, {});
}

//===----------------------------------------------------------------------===//
Expand Down
9 changes: 8 additions & 1 deletion mlir/lib/Target/LLVMIR/DebugImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
getStringAttrOrNull(node->getRawName()), translate(node->getFile()),
node->getLine(), translate(node->getScope()), baseType,
flags.value_or(DIFlags::Zero), node->getSizeInBits(),
node->getAlignInBits(), elements);
node->getAlignInBits(), elements,
translateExpression(node->getDataLocationExp()),
translateExpression(node->getRankExp()),
translateExpression(node->getAllocatedExp()),
translateExpression(node->getAssociatedExp()));
}

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

DIExpressionAttr DebugImporter::translateExpression(llvm::DIExpression *node) {
if (!node)
return nullptr;

SmallVector<DIExpressionElemAttr> ops;

// Begin processing the operations.
Expand Down
15 changes: 14 additions & 1 deletion mlir/lib/Target/LLVMIR/DebugTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
return nullptr;
}

llvm::DIExpression *
DebugTranslation::getExpressionAttrOrNull(DIExpressionAttr attr) {
if (!attr)
return nullptr;
return translateExpression(attr);
}

llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
if (!stringAttr || stringAttr.empty())
return nullptr;
Expand Down Expand Up @@ -156,7 +163,13 @@ DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
/*OffsetInBits=*/0,
/*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
llvm::MDNode::get(llvmCtx, elements),
/*RuntimeLang=*/0, /*VTableHolder=*/nullptr);
/*RuntimeLang=*/0, /*VTableHolder=*/nullptr,
/*TemplateParams=*/nullptr, /*Identifier=*/nullptr,
/*Discriminator=*/nullptr,
getExpressionAttrOrNull(attr.getDataLocation()),
getExpressionAttrOrNull(attr.getAssociated()),
getExpressionAttrOrNull(attr.getAllocated()),
getExpressionAttrOrNull(attr.getRank()));
}

llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
Expand Down
4 changes: 4 additions & 0 deletions mlir/lib/Target/LLVMIR/DebugTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class DebugTranslation {
/// nullptr if `stringAttr` is null or contains and empty string.
llvm::MDString *getMDStringOrNull(StringAttr stringAttr);

/// Constructs a DIExpression metadata node from the DIExpressionAttr. Returns
/// nullptr if `DIExpressionAttr` is null.
llvm::DIExpression *getExpressionAttrOrNull(DIExpressionAttr attr);

/// A mapping between mlir location+scope and the corresponding llvm debug
/// metadata.
DenseMap<std::tuple<Location, llvm::DILocalScope *, const llvm::DILocation *>,
Expand Down
13 changes: 8 additions & 5 deletions mlir/test/CAPI/llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,6 @@ static void testDebugInfoAttributes(MlirContext ctx) {
mlirAttributeDump(
mlirLLVMDIDerivedTypeAttrGet(ctx, 0, bar, di_type, 64, 8, 0, 3, di_type));

// CHECK: #llvm.di_composite_type<{{.*}}>
mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(
ctx, 0, id, foo, file, 1, compile_unit, di_type, 0, 64, 8, 1, &di_type));

MlirAttribute subroutine_type =
mlirLLVMDISubroutineTypeAttrGet(ctx, 0x0, 1, &di_type);

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

MlirAttribute expression =
mlirLLVMDIExpressionAttrGet(ctx, 1, &expression_elem);
// CHECK: #llvm.di_expression<[(1)]>
mlirAttributeDump(mlirLLVMDIExpressionAttrGet(ctx, 1, &expression_elem));
mlirAttributeDump(expression);

// CHECK: #llvm.di_composite_type<{{.*}}>
mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(
ctx, 0, id, foo, file, 1, compile_unit, di_type, 0, 64, 8, 1, &di_type,
expression, expression, expression, expression));
}

int main(void) {
Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Target/LLVMIR/Import/composite-type-fortran.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; RUN: mlir-translate -import-llvm -mlir-print-debuginfo %s | FileCheck %s

define void @fn_with_composite() !dbg !3 {
ret void
}

!llvm.dbg.cu = !{!1}
!llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_Fortran95, file: !2, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
!2 = !DIFile(filename: "test.f90", directory: "")
!3 = distinct !DISubprogram(name: "fn_with_composite", scope: !1, file: !2, type: !4, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !1)
!4 = !DISubroutineType(cc: DW_CC_normal, types: !5)
!5 = !{null, !6}
!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))
!7 = !DIBasicType(name: "integer", size: 32, encoding: DW_ATE_signed)
!8 = !{!9}
!9 = !DISubrange(count: 5, lowerBound: 1)
!10 = !DILocation(line: 26, column: 3, scope: !3)

; CHECK: #llvm.di_composite_type<tag = DW_TAG_array_type,
; CHECK-SAME: dataLocation = <[DW_OP_push_object_address, DW_OP_deref]>
; CHECK-SAME: rank = <[DW_OP_push_object_address, DW_OP_plus_uconst(16), DW_OP_deref]>
; CHECK-SAME: allocated = <[DW_OP_lit0, DW_OP_ne]>
; CHECK-SAME: associated = <[DW_OP_lit0, DW_OP_eq]>
35 changes: 35 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir-debug.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,38 @@ llvm.mlir.global @global_variable() {dbg_expr = #di_global_variable_expression}
// CHECK: ![[SCOPE]] = !DISubprogram({{.*}}type: ![[SUBROUTINE:[0-9]+]],
// CHECK: ![[SUBROUTINE]] = !DISubroutineType(types: ![[SR_TYPES:[0-9]+]])
// CHECK: ![[SR_TYPES]] = !{![[COMP]]}

// -----

#file = #llvm.di_file<"test.f90" in "">
#cu = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_Fortran95,
file = #file, producer = "", isOptimized = false, emissionKind = Full>
#i32 = #llvm.di_basic_type<
tag = DW_TAG_base_type, name = "integer",
sizeInBits = 32, encoding = DW_ATE_signed
>
#null = #llvm.di_null_type
#alloc = #llvm.di_expression<[DW_OP_lit0, DW_OP_ne]>
#assoc = #llvm.di_expression<[DW_OP_lit0, DW_OP_eq]>
#rank = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_plus_uconst(16), DW_OP_deref]>
#datal = #llvm.di_expression<[DW_OP_push_object_address, DW_OP_deref]>
#array = #llvm.di_composite_type<tag = DW_TAG_array_type,
baseType = #i32,
dataLocation = #datal, rank = #rank,
allocated = #alloc, associated = #assoc,
elements = #llvm.di_subrange<lowerBound = 1, count = 5>
>
#spType0 = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #null, #array>
#sp0 = #llvm.di_subprogram<
compileUnit = #cu, scope = #cu, name = "fn_with_composite", file = #file,
subprogramFlags = "Definition|Optimized", type = #spType0
>
llvm.func @fn_with_composite() {
llvm.return
}loc(fused<#sp0>["foo.mlir":1:1])
// CHECK-LABEL: define void @fn_with_composite()
// CHECK: !DICompositeType(
// CHECK-SAME: dataLocation: !DIExpression(DW_OP_push_object_address, DW_OP_deref)
// CHECK-SAME: associated: !DIExpression(DW_OP_lit0, DW_OP_eq)
// CHECK-SAME: allocated: !DIExpression(DW_OP_lit0, DW_OP_ne)
// CHECK-SAME: rank: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 16, DW_OP_deref)
Loading