-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-flang-fir-hlfir @llvm/pr-subscribers-mlir-llvm Author: Abid Qadeer (abidh) ChangesThe fortran arrays use 'dataLocation', 'rank', 'allocated' and 'associated' fields of the DICompositeType. These were not available in 'DICompositeTypeAttr'. This PR adds the missing fields. Full diff: https://github.com/llvm/llvm-project/pull/93226.diff 10 Files Affected:
diff --git a/mlir/include/mlir-c/Dialect/LLVM.h b/mlir/include/mlir-c/Dialect/LLVM.h
index 446d364342ac8..c9e48fe9ce0b8 100644
--- a/mlir/include/mlir-c/Dialect/LLVM.h
+++ b/mlir/include/mlir-c/Dialect/LLVM.h
@@ -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 location, 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
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index 8965f4f652a20..535cf8dfd2ced 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -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 = [{
diff --git a/mlir/lib/CAPI/Dialect/LLVM.cpp b/mlir/lib/CAPI/Dialect/LLVM.cpp
index 9a28749631f57..42e945a536bca 100644
--- a/mlir/lib/CAPI/Dialect/LLVM.cpp
+++ b/mlir/lib/CAPI/Dialect/LLVM.cpp
@@ -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 location, MlirAttribute rank, MlirAttribute allocated,
+ MlirAttribute associated) {
SmallVector<Attribute> elementsStorage;
elementsStorage.reserve(nElements);
@@ -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(location)),
+ cast<DIExpressionAttr>(unwrap(rank)),
+ cast<DIExpressionAttr>(unwrap(allocated)),
+ cast<DIExpressionAttr>(unwrap(associated))));
}
MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
index a44e83313c9c1..9bc71e1ebc489 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
@@ -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, {}, {}, {}, {}, {});
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.cpp b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
index b40bfa4f4dbb0..6c011b3c756ff 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
@@ -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) {
@@ -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.
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
index 815943c40bdef..dfb7d4952157d 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -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;
@@ -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) {
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.h b/mlir/lib/Target/LLVMIR/DebugTranslation.h
index c7a5228cbf5e9..04b7ea41add9a 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.h
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.h
@@ -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 *>,
diff --git a/mlir/test/CAPI/llvm.c b/mlir/test/CAPI/llvm.c
index 0c589a45f9df4..f1d02b43d5232 100644
--- a/mlir/test/CAPI/llvm.c
+++ b/mlir/test/CAPI/llvm.c
@@ -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);
@@ -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) {
diff --git a/mlir/test/Target/LLVMIR/Import/composite-type-fortran.ll b/mlir/test/Target/LLVMIR/Import/composite-type-fortran.ll
new file mode 100644
index 0000000000000..6c01c793dc85c
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/composite-type-fortran.ll
@@ -0,0 +1,27 @@
+; 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]>
+
+
diff --git a/mlir/test/Target/LLVMIR/llvmir-debug.mlir b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
index 1cb94bca169db..a33024818169c 100644
--- a/mlir/test/Target/LLVMIR/llvmir-debug.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
@@ -484,3 +484,39 @@ 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: !DISubprogram(name: "fn_with_composite"{{.*}}type: ![[TY1:[0-9]+]]{{.*}})
+// CHECK: [[TY1]] = !DISubroutineType({{.*}}types: ![[TYPES:[0-9]+]])
+// CHECK: ![[TYPES]] = !{null, ![[COMPTY:[0-9]+]]}
+// CHECK: ![[COMPTY]] = !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)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Looks good. I added some minor formatting / naming comments.
llvm.func @fn_with_composite() { | ||
llvm.return | ||
}loc(fused<#sp0>["foo.mlir":1:1]) | ||
// CHECK: !DISubprogram(name: "fn_with_composite"{{.*}}type: ![[TY1:[0-9]+]]{{.*}}) | ||
// CHECK: [[TY1]] = !DISubroutineType({{.*}}types: ![[TYPES:[0-9]+]]) | ||
// CHECK: ![[TYPES]] = !{null, ![[COMPTY:[0-9]+]]} | ||
// CHECK: ![[COMPTY]] = !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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
llvm.func @fn_with_composite() { | |
llvm.return | |
}loc(fused<#sp0>["foo.mlir":1:1]) | |
// CHECK: !DISubprogram(name: "fn_with_composite"{{.*}}type: ![[TY1:[0-9]+]]{{.*}}) | |
// CHECK: [[TY1]] = !DISubroutineType({{.*}}types: ![[TYPES:[0-9]+]]) | |
// CHECK: ![[TYPES]] = !{null, ![[COMPTY:[0-9]+]]} | |
// CHECK: ![[COMPTY]] = !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) | |
llvm.func @fn_with_composite() { | |
llvm.return | |
} loc(fused<#sp0>["foo.mlir":1:1]) | |
// 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would check for the composite type itself here but not necessarily for the enclosing subroutine type. Also added some whitespaces to format the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used this approach initially but observed that test will fail because FileCheck
will match an earlier !DICompositeType
in the file. In the updated patch, I used your suggestion but added a CHECK-LABEL
to avoid the wrong match.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks LGTM modulo last nit!
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]>
Co-authored-by: Tobias Gysi <[email protected]>
Co-authored-by: Tobias Gysi <[email protected]>
Co-authored-by: Tobias Gysi <[email protected]>
The llvm#93226 adds 4 more fields in DICompositeTypeAttr. This PR provides default value of those 4 fields. This will be required to avoid build error once llvm#93226 is merged.
The #92568 introduced call of DICompositeTypeAttr::get in flang. That will need to be updated once this PR is merged. I have opened #93516 for that. |
There seem to be issues with the build bots still. Once they are fixed this commit can be landed! |
The llvm#92568 introduced call of DICompositeTypeAttr::get in flang. This commit adjusts the call to fix the build. I initially opened the llvm#93516 for it but was advised to add this change in this PR.
After opening of this PR, #92568 went in which added a call to DICompositeTypeAttr::get in flang that was causing the build failure. I have pushed a commit to adjust the call. I will wait for bots to turn green before landing. |
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]>
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.