[Mlir-commits] [mlir] 36c34ec - [mlir][debug] Support DICommonBlock. (#111706)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Oct 10 10:07:10 PDT 2024
Author: Abid Qadeer
Date: 2024-10-10T18:07:06+01:00
New Revision: 36c34ec967c28c77406fe85ef3237a167a243763
URL: https://github.com/llvm/llvm-project/commit/36c34ec967c28c77406fe85ef3237a167a243763
DIFF: https://github.com/llvm/llvm-project/commit/36c34ec967c28c77406fe85ef3237a167a243763.diff
LOG: [mlir][debug] Support DICommonBlock. (#111706)
A COMMON block is a named area of memory that holds a collection of
variables. Fortran subprograms may map the COMMON block memory area to a
list of variables. A common block is represented in LLVM debug by
DICommonBlock.
This PR adds support for this in MLIR. The changes are mostly mechanical
apart from small change to access the DICompileUnit when the scope of
the variable is DICommonBlock.
---------
Co-authored-by: Tobias Gysi <tobias.gysi at nextsilicon.com>
Added:
Modified:
mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
mlir/lib/Target/LLVMIR/DebugImporter.cpp
mlir/lib/Target/LLVMIR/DebugImporter.h
mlir/lib/Target/LLVMIR/DebugTranslation.cpp
mlir/lib/Target/LLVMIR/DebugTranslation.h
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
mlir/test/Dialect/LLVMIR/debuginfo.mlir
mlir/test/Target/LLVMIR/Import/debug-info.ll
mlir/test/Target/LLVMIR/llvmir-debug.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index c298c8277eb0c3..0d904f13037c61 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -701,6 +701,22 @@ def LLVM_DISubrangeAttr : LLVM_Attr<"DISubrange", "di_subrange", /*traits=*/[],
let assemblyFormat = "`<` struct(params) `>`";
}
+//===----------------------------------------------------------------------===//
+// DICommonBlockAttr
+//===----------------------------------------------------------------------===//
+
+def LLVM_DICommonBlockAttr : LLVM_Attr<"DICommonBlock", "di_common_block",
+ /*traits=*/[], "DIScopeAttr"> {
+ let parameters = (ins
+ "DIScopeAttr":$scope,
+ OptionalParameter<"DIGlobalVariableAttr">:$decl,
+ "StringAttr":$name,
+ OptionalParameter<"DIFileAttr">:$file,
+ OptionalParameter<"unsigned">:$line
+ );
+ let assemblyFormat = "`<` struct(params) `>`";
+}
+
//===----------------------------------------------------------------------===//
// DISubroutineTypeAttr
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
index 99871dac81d326..9640bbdf28df45 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
@@ -56,13 +56,14 @@ void LLVMDialect::registerAttributes() {
//===----------------------------------------------------------------------===//
bool DINodeAttr::classof(Attribute attr) {
- return llvm::isa<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
- DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
- DIImportedEntityAttr, DILabelAttr, DILexicalBlockAttr,
- DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
- DINamespaceAttr, DINullTypeAttr, DIAnnotationAttr,
- DIStringTypeAttr, DISubprogramAttr, DISubrangeAttr,
- DISubroutineTypeAttr>(attr);
+ return llvm::isa<DIBasicTypeAttr, DICommonBlockAttr, DICompileUnitAttr,
+ DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
+ DIGlobalVariableAttr, DIImportedEntityAttr, DILabelAttr,
+ DILexicalBlockAttr, DILexicalBlockFileAttr,
+ DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
+ DINullTypeAttr, DIAnnotationAttr, DIStringTypeAttr,
+ DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
+ attr);
}
//===----------------------------------------------------------------------===//
@@ -70,8 +71,9 @@ bool DINodeAttr::classof(Attribute attr) {
//===----------------------------------------------------------------------===//
bool DIScopeAttr::classof(Attribute attr) {
- return llvm::isa<DICompileUnitAttr, DICompositeTypeAttr, DIFileAttr,
- DILocalScopeAttr, DIModuleAttr, DINamespaceAttr>(attr);
+ return llvm::isa<DICommonBlockAttr, DICompileUnitAttr, DICompositeTypeAttr,
+ DIFileAttr, DILocalScopeAttr, DIModuleAttr, DINamespaceAttr>(
+ attr);
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 2c7af8712d420c..006d412936a337 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -3369,11 +3369,12 @@ struct LLVMOpAsmDialectInterface : public OpAsmDialectInterface {
AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
return TypeSwitch<Attribute, AliasResult>(attr)
.Case<AccessGroupAttr, AliasScopeAttr, AliasScopeDomainAttr,
- DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
- DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
- DIGlobalVariableExpressionAttr, DIImportedEntityAttr, DILabelAttr,
- DILexicalBlockAttr, DILexicalBlockFileAttr, DILocalVariableAttr,
- DIModuleAttr, DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
+ DIBasicTypeAttr, DICommonBlockAttr, DICompileUnitAttr,
+ DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
+ DIGlobalVariableAttr, DIGlobalVariableExpressionAttr,
+ DIImportedEntityAttr, DILabelAttr, DILexicalBlockAttr,
+ DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
+ DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
DISubprogramAttr, DISubroutineTypeAttr, LoopAnnotationAttr,
LoopVectorizeAttr, LoopInterleaveAttr, LoopUnrollAttr,
LoopUnrollAndJamAttr, LoopLICMAttr, LoopDistributeAttr,
diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.cpp b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
index cd992be62b4719..412125b6ea65f6 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
@@ -302,6 +302,13 @@ DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
getAttrOrNull(node->getStride()));
}
+DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
+ return DICommonBlockAttr::get(context, translate(node->getScope()),
+ translate(node->getDecl()),
+ getStringAttrOrNull(node->getRawName()),
+ translate(node->getFile()), node->getLineNo());
+}
+
DISubroutineTypeAttr
DebugImporter::translateImpl(llvm::DISubroutineType *node) {
SmallVector<DITypeAttr> types;
@@ -339,6 +346,8 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
auto translateNode = [this](llvm::DINode *node) -> DINodeAttr {
if (auto *casted = dyn_cast<llvm::DIBasicType>(node))
return translateImpl(casted);
+ if (auto *casted = dyn_cast<llvm::DICommonBlock>(node))
+ return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DICompileUnit>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DICompositeType>(node))
diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.h b/mlir/lib/Target/LLVMIR/DebugImporter.h
index cb796676759c39..a452e01a9f6041 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.h
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.h
@@ -79,6 +79,7 @@ class DebugImporter {
DIScopeAttr translateImpl(llvm::DIScope *node);
DISubprogramAttr translateImpl(llvm::DISubprogram *node);
DISubrangeAttr translateImpl(llvm::DISubrange *node);
+ DICommonBlockAttr translateImpl(llvm::DICommonBlock *node);
DISubroutineTypeAttr translateImpl(llvm::DISubroutineType *node);
DITypeAttr translateImpl(llvm::DIType *node);
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
index 92ff079a10c8aa..2491db299af312 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -397,6 +397,13 @@ llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
getMetadataOrNull(attr.getStride()));
}
+llvm::DICommonBlock *DebugTranslation::translateImpl(DICommonBlockAttr attr) {
+ return llvm::DICommonBlock::get(llvmCtx, translate(attr.getScope()),
+ translate(attr.getDecl()),
+ getMDStringOrNull(attr.getName()),
+ translate(attr.getFile()), attr.getLine());
+}
+
llvm::DISubroutineType *
DebugTranslation::translateImpl(DISubroutineTypeAttr attr) {
// Concatenate the result and argument types into a single array.
@@ -428,12 +435,13 @@ llvm::DINode *DebugTranslation::translate(DINodeAttr attr) {
if (!node)
node = TypeSwitch<DINodeAttr, llvm::DINode *>(attr)
- .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
- DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
- DIImportedEntityAttr, DILabelAttr, DILexicalBlockAttr,
- DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
- DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
- DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
+ .Case<DIBasicTypeAttr, DICommonBlockAttr, DICompileUnitAttr,
+ DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
+ DIGlobalVariableAttr, DIImportedEntityAttr, DILabelAttr,
+ DILexicalBlockAttr, DILexicalBlockFileAttr,
+ DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
+ DINullTypeAttr, DIStringTypeAttr, DISubprogramAttr,
+ DISubrangeAttr, DISubroutineTypeAttr>(
[&](auto attr) { return translateImpl(attr); });
if (node && !node->isTemporary())
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.h b/mlir/lib/Target/LLVMIR/DebugTranslation.h
index 422aa34e28f3c9..ff4eaa46c564e2 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.h
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.h
@@ -88,6 +88,7 @@ class DebugTranslation {
llvm::DIScope *translateImpl(DIScopeAttr attr);
llvm::DISubprogram *translateImpl(DISubprogramAttr attr);
llvm::DISubrange *translateImpl(DISubrangeAttr attr);
+ llvm::DICommonBlock *translateImpl(DICommonBlockAttr attr);
llvm::DISubroutineType *translateImpl(DISubroutineTypeAttr attr);
llvm::DIType *translateImpl(DITypeAttr attr);
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index cc0de5bc838c99..a5de90160c4145 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1064,19 +1064,27 @@ LogicalResult ModuleTranslation::convertGlobals() {
// There is no `globals` field in DICompileUnitAttr which can be directly
// assigned to DICompileUnit. We have to build the list by looking at the
// dbgExpr of all the GlobalOps. The scope of the variable is used to get
- // the DICompileUnit in which to add it. But for the languages that
- // support modules, the scope hierarchy can be
- // variable -> module -> compile unit
- // If a variable scope points to the module then we use the scope of the
- // module to get the compile unit.
- // Global variables are also used for things like static local variables
- // in C and local variables with the save attribute in Fortran. The scope
- // of the variable is the parent function. We use the compile unit of the
- // parent function in this case.
+ // the DICompileUnit in which to add it.
+ // But there are cases where the scope of a global does not
+ // directly point to the DICompileUnit and we have to do a bit more work
+ // to get to it. Some of those cases are:
+ //
+ // 1. For the languages that support modules, the scope hierarchy can be
+ // variable -> DIModule -> DICompileUnit
+ //
+ // 2. For the Fortran common block variable, the scope hierarchy can be
+ // variable -> DICommonBlock -> DISubprogram -> DICompileUnit
+ //
+ // 3. For entities like static local variables in C or variable with
+ // SAVE attribute in Fortran, the scope hierarchy can be
+ // variable -> DISubprogram -> DICompileUnit
llvm::DIScope *scope = diGlobalVar->getScope();
if (auto *mod = dyn_cast_if_present<llvm::DIModule>(scope))
scope = mod->getScope();
- else if (auto *sp = dyn_cast_if_present<llvm::DISubprogram>(scope))
+ else if (auto *cb = dyn_cast_if_present<llvm::DICommonBlock>(scope)) {
+ if (auto *sp = dyn_cast_if_present<llvm::DISubprogram>(cb->getScope()))
+ scope = sp->getUnit();
+ } else if (auto *sp = dyn_cast_if_present<llvm::DISubprogram>(scope))
scope = sp->getUnit();
// Get the compile unit (scope) of the the global variable.
diff --git a/mlir/test/Dialect/LLVMIR/debuginfo.mlir b/mlir/test/Dialect/LLVMIR/debuginfo.mlir
index af95ec97833a13..8475ec6c3510db 100644
--- a/mlir/test/Dialect/LLVMIR/debuginfo.mlir
+++ b/mlir/test/Dialect/LLVMIR/debuginfo.mlir
@@ -156,6 +156,14 @@
// CHECK-DAG: #[[LABEL2:.*]] = #llvm.di_label<scope = #[[BLOCK2]]>
#label2 = #llvm.di_label<scope = #block2>
+// CHECK-DAG: #llvm.di_common_block<scope = #[[SP1]], name = "block", file = #[[FILE]], line = 3>
+#di_common_block = #llvm.di_common_block<scope = #sp1, name = "block", file = #file, line = 3>
+#global_var = #llvm.di_global_variable<scope = #di_common_block, name = "a",
+ file = #file, line = 2, type = #int0>
+#var_expression = #llvm.di_global_variable_expression<var = #global_var,
+ expr = <>>
+llvm.mlir.global common @block_() {dbg_expr = #var_expression} : i64
+
// CHECK: llvm.func @addr(%[[ARG:.*]]: i64)
llvm.func @addr(%arg: i64) {
// CHECK: %[[ALLOC:.*]] = llvm.alloca
diff --git a/mlir/test/Target/LLVMIR/Import/debug-info.ll b/mlir/test/Target/LLVMIR/Import/debug-info.ll
index 6267990b0bf803..09909d7d63b2ab 100644
--- a/mlir/test/Target/LLVMIR/Import/debug-info.ll
+++ b/mlir/test/Target/LLVMIR/Import/debug-info.ll
@@ -843,3 +843,27 @@ define void @fn_with_annotations() !dbg !12 {
; CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "fn_with_annotations"{{.*}}annotations = #llvm.di_annotation<name = "foo", value = "bar">>
+
+; // -----
+
+ at block = common global [4 x i8] zeroinitializer, !dbg !0
+
+define void @test() !dbg !3 {
+ ret void
+}
+
+!llvm.module.flags = !{!10}
+!llvm.dbg.cu = !{!7}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "alpha", scope: !2, file: !4, type: !9)
+!2 = !DICommonBlock(scope: !3, declaration: null, name: "block", file: !4, line: 3)
+!3 = distinct !DISubprogram(name: "test", scope: !4, file: !4, spFlags: DISPFlagDefinition, unit: !7)
+!4 = !DIFile(filename: "test.f90", directory: "")
+!7 = distinct !DICompileUnit(language: DW_LANG_Fortran95, file: !4)
+!9 = !DIBasicType(name: "integer", size: 32, encoding: DW_ATE_signed)
+!10 = !{i32 2, !"Debug Info Version", i32 3}
+
+; CHECK: #[[FILE:.+]] = #llvm.di_file<"test.f90" in "">
+; CHECK: #[[SP:.+]] = #llvm.di_subprogram<{{.*}}name = "test"{{.*}}>
+; CHECK: #llvm.di_common_block<scope = #[[SP]], name = "block", file = #[[FILE]], line = 3>
diff --git a/mlir/test/Target/LLVMIR/llvmir-debug.mlir b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
index b09a60b8dcac90..826fda60c5efef 100644
--- a/mlir/test/Target/LLVMIR/llvmir-debug.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
@@ -660,3 +660,33 @@ llvm.func @string_ty(%arg0: !llvm.ptr) {
// CHECK-DAG: !DIStringType(name: "character(*)", stringLength: ![[VAR:[0-9]+]], stringLengthExpression: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 8), stringLocationExpression: !DIExpression(DW_OP_push_object_address, DW_OP_deref), size: 32, align: 8)
// CHECK-DAG: ![[VAR]] = !DILocalVariable(name: "string_size"{{.*}} flags: DIFlagArtificial)
+
+// -----
+
+// Test translation of DICommonBlockAttr.
+#bt = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "int", sizeInBits = 32>
+#file = #llvm.di_file<"test.f90" in "">
+#cu = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_C,
+ file = #file, isOptimized = false, emissionKind = Full>
+#sp = #llvm.di_subprogram<compileUnit = #cu, scope = #file, name = "test",
+ file = #file, subprogramFlags = Definition>
+#di_common_block = #llvm.di_common_block<scope = #sp, name = "block",
+ file = #file, line = 3>
+#global_var = #llvm.di_global_variable<scope = #di_common_block, name = "a",
+ file = #file, line = 2, type = #bt>
+#var_expression = #llvm.di_global_variable_expression<var = #global_var,
+ expr = <>>
+
+llvm.mlir.global common @block_(dense<0> : tensor<8xi8>)
+ {dbg_expr = #var_expression} : !llvm.array<8 x i8>
+
+llvm.func @test() {
+ llvm.return
+} loc(#loc2)
+
+#loc1 = loc("test.f90":1:0)
+#loc2 = loc(fused<#sp>[#loc1])
+
+// CHECK: !DICommonBlock(scope: ![[SCOPE:[0-9]+]], declaration: null, name: "block", file: ![[FILE:[0-9]+]], line: 3)
+// CHECK: ![[SCOPE]] = {{.*}}!DISubprogram(name: "test"{{.*}})
+// CHECK: ![[FILE]] = !DIFile(filename: "test.f90"{{.*}})
More information about the Mlir-commits
mailing list