[Mlir-commits] [mlir] e133cb9 - [mlir][llvm] Add DINamespace attribute
Christian Ulmann
llvmlistbot at llvm.org
Fri Feb 17 04:11:11 PST 2023
Author: Christian Ulmann
Date: 2023-02-17T13:09:29+01:00
New Revision: e133cb9c6daf5f6c9fb2c1bdb138e0a02b00fadb
URL: https://github.com/llvm/llvm-project/commit/e133cb9c6daf5f6c9fb2c1bdb138e0a02b00fadb
DIFF: https://github.com/llvm/llvm-project/commit/e133cb9c6daf5f6c9fb2c1bdb138e0a02b00fadb.diff
LOG: [mlir][llvm] Add DINamespace attribute
This commit introduces the DINamespaceAttr to model LLVM's DINamespace metadata.
Reviewed By: gysit
Differential Revision: https://reviews.llvm.org/D144243
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/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 6c4a8af1b22c..06755f3f3204 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -469,6 +469,21 @@ def LLVM_DISubprogramAttr : LLVM_Attr<"DISubprogram", "di_subprogram",
let assemblyFormat = "`<` struct(params) `>`";
}
+//===----------------------------------------------------------------------===//
+// DINamespaceAttr
+//===----------------------------------------------------------------------===//
+
+def LLVM_DINamespaceAttr : LLVM_Attr<"DINamespace", "di_namespace",
+ /*traits=*/[], "DIScopeAttr"> {
+ let parameters = (ins
+ "StringAttr":$name,
+ OptionalParameter<"DIScopeAttr">:$scope,
+ "bool":$exportSymbols
+ );
+
+ let assemblyFormat = "`<` struct(params) `>`";
+}
+
//===----------------------------------------------------------------------===//
// DISubrangeAttr
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
index 1bb0f6a9438d..808428937988 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
@@ -42,10 +42,10 @@ void LLVMDialect::registerAttributes() {
//===----------------------------------------------------------------------===//
bool DINodeAttr::classof(Attribute attr) {
- return llvm::isa<DINullTypeAttr, DIBasicTypeAttr, DICompileUnitAttr,
- DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
- DILexicalBlockAttr, DILexicalBlockFileAttr,
- DILocalVariableAttr, DISubprogramAttr, DISubrangeAttr,
+ return llvm::isa<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
+ DIDerivedTypeAttr, DIFileAttr, DILexicalBlockAttr,
+ DILexicalBlockFileAttr, DILocalVariableAttr, DINamespaceAttr,
+ DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
DISubroutineTypeAttr>(attr);
}
@@ -63,7 +63,7 @@ bool DIScopeAttr::classof(Attribute attr) {
//===----------------------------------------------------------------------===//
bool DILocalScopeAttr::classof(Attribute attr) {
- return llvm::isa<DILexicalBlockAttr, DISubprogramAttr>(attr);
+ return llvm::isa<DILexicalBlockAttr, DINamespaceAttr, DISubprogramAttr>(attr);
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 72f6c697a2f2..5e37409a8e20 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -2800,17 +2800,17 @@ struct LLVMOpAsmDialectInterface : public OpAsmDialectInterface {
AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
return TypeSwitch<Attribute, AliasResult>(attr)
- .Case<DINullTypeAttr, DIBasicTypeAttr, DICompileUnitAttr,
- DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
- DILexicalBlockAttr, DILexicalBlockFileAttr, DILocalVariableAttr,
- DISubprogramAttr, DISubroutineTypeAttr, LoopAnnotationAttr,
- LoopVectorizeAttr, LoopInterleaveAttr, LoopUnrollAttr,
- LoopUnrollAndJamAttr, LoopLICMAttr, LoopDistributeAttr,
- LoopPipelineAttr, LoopPeeledAttr, LoopUnswitchAttr>(
- [&](auto attr) {
- os << decltype(attr)::getMnemonic();
- return AliasResult::OverridableAlias;
- })
+ .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
+ DIDerivedTypeAttr, DIFileAttr, DILexicalBlockAttr,
+ DILexicalBlockFileAttr, DILocalVariableAttr, DINamespaceAttr,
+ DINullTypeAttr, DISubprogramAttr, DISubroutineTypeAttr,
+ LoopAnnotationAttr, LoopVectorizeAttr, LoopInterleaveAttr,
+ LoopUnrollAttr, LoopUnrollAndJamAttr, LoopLICMAttr,
+ LoopDistributeAttr, LoopPipelineAttr, LoopPeeledAttr,
+ LoopUnswitchAttr>([&](auto attr) {
+ os << decltype(attr)::getMnemonic();
+ return AliasResult::OverridableAlias;
+ })
.Default([](Attribute) { return AliasResult::NoAlias; });
}
};
diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.cpp b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
index d6ddd6efd149..20cd5da30ae5 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
@@ -113,6 +113,12 @@ DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
return cast<DIScopeAttr>(translate(static_cast<llvm::DINode *>(node)));
}
+DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
+ return DINamespaceAttr::get(
+ context, StringAttr::get(context, node->getName()),
+ translate(node->getScope()), node->getExportSymbols());
+}
+
DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
std::optional<DISubprogramFlags> subprogramFlags =
symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
@@ -205,6 +211,8 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubprogram>(node))
return translateImpl(casted);
+ if (auto *casted = dyn_cast<llvm::DINamespace>(node))
+ return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubrange>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubroutineType>(node))
diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.h b/mlir/lib/Target/LLVMIR/DebugImporter.h
index 7ae633df6e7b..2ce19bfbe0be 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.h
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.h
@@ -62,6 +62,7 @@ class DebugImporter {
DILocalVariableAttr translateImpl(llvm::DILocalVariable *node);
DIScopeAttr translateImpl(llvm::DIScope *node);
DISubprogramAttr translateImpl(llvm::DISubprogram *node);
+ DINamespaceAttr translateImpl(llvm::DINamespace *node);
DISubrangeAttr translateImpl(llvm::DISubrange *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 9b13e81ace67..478216df312a 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -198,6 +198,11 @@ llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
translate(attr.getCompileUnit()));
}
+llvm::DINamespace *DebugTranslation::translateImpl(DINamespaceAttr attr) {
+ return llvm::DINamespace::get(llvmCtx, translate(attr.getScope()),
+ attr.getName(), attr.getExportSymbols());
+}
+
llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
auto getMetadataOrNull = [&](IntegerAttr attr) -> llvm::Metadata * {
if (!attr)
@@ -235,10 +240,11 @@ llvm::DINode *DebugTranslation::translate(DINodeAttr attr) {
llvm::DINode *node =
TypeSwitch<DINodeAttr, llvm::DINode *>(attr)
- .Case<DINullTypeAttr, DIBasicTypeAttr, DICompileUnitAttr,
- DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
- DILexicalBlockAttr, DILexicalBlockFileAttr, DILocalVariableAttr,
- DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
+ .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
+ DIDerivedTypeAttr, DIFileAttr, DILexicalBlockAttr,
+ DILexicalBlockFileAttr, DILocalVariableAttr, DINamespaceAttr,
+ DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
+ DISubroutineTypeAttr>(
[&](auto attr) { return translateImpl(attr); });
attrToNode.insert({attr, node});
return node;
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.h b/mlir/lib/Target/LLVMIR/DebugTranslation.h
index 17bc35c8ab44..4160ca09a1c9 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.h
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.h
@@ -73,6 +73,7 @@ class DebugTranslation {
llvm::DILocalVariable *translateImpl(DILocalVariableAttr attr);
llvm::DIScope *translateImpl(DIScopeAttr attr);
llvm::DISubprogram *translateImpl(DISubprogramAttr attr);
+ llvm::DINamespace *translateImpl(DINamespaceAttr attr);
llvm::DISubrange *translateImpl(DISubrangeAttr attr);
llvm::DISubroutineType *translateImpl(DISubroutineTypeAttr attr);
llvm::DIType *translateImpl(DITypeAttr attr);
diff --git a/mlir/test/Dialect/LLVMIR/debuginfo.mlir b/mlir/test/Dialect/LLVMIR/debuginfo.mlir
index 7004d57762b9..1c3c553f1340 100644
--- a/mlir/test/Dialect/LLVMIR/debuginfo.mlir
+++ b/mlir/test/Dialect/LLVMIR/debuginfo.mlir
@@ -50,9 +50,19 @@
elements = #llvm.di_subrange<count = 4>
>
-// CHECK-DAG: #[[COMP2:.*]] = #llvm.di_composite_type<tag = DW_TAG_class_type, name = "class_name", file = #[[FILE]], scope = #[[FILE]], flags = "TypePassByReference|NonTrivial">
+// CHECK-DAG: #[[TOPLEVEL:.*]] = #llvm.di_namespace<name = "toplevel", exportSymbols = true>
+#toplevel_namespace = #llvm.di_namespace<
+ name = "toplevel", exportSymbols = true
+>
+
+// CHECK-DAG: #[[NESTED:.*]] = #llvm.di_namespace<name = "nested", scope = #[[TOPLEVEL]], exportSymbols = false>
+#nested_namespace = #llvm.di_namespace<
+ name = "nested", scope = #toplevel_namespace, exportSymbols = false
+>
+
+// CHECK-DAG: #[[COMP2:.*]] = #llvm.di_composite_type<tag = DW_TAG_class_type, name = "class_name", file = #[[FILE]], scope = #[[NESTED]], flags = "TypePassByReference|NonTrivial">
#comp2 = #llvm.di_composite_type<
- tag = DW_TAG_class_type, name = "class_name", file = #file, scope = #file,
+ tag = DW_TAG_class_type, name = "class_name", file = #file, scope = #nested_namespace,
flags = "TypePassByReference|NonTrivial"
>
diff --git a/mlir/test/Target/LLVMIR/Import/debug-info.ll b/mlir/test/Target/LLVMIR/Import/debug-info.ll
index 896d42202c23..fa1dccb47179 100644
--- a/mlir/test/Target/LLVMIR/Import/debug-info.ll
+++ b/mlir/test/Target/LLVMIR/Import/debug-info.ll
@@ -367,3 +367,25 @@ declare void @llvm.dbg.value(metadata, metadata, metadata)
!7 = !DILocalVariable(scope: !8, name: "var", file: !2, type: !5);
!8 = distinct !DISubprogram(name: "dbg_use_before_def", scope: !2, file: !2, spFlags: DISPFlagDefinition, unit: !1)
!9 = !DILocation(line: 1, column: 2, scope: !8)
+
+; // -----
+
+; CHECK-DAG: #[[NAMESPACE:.+]] = #llvm.di_namespace<name = "std", exportSymbols = false>
+; CHECK-DAG: #[[SUBPROGRAM:.+]] = #llvm.di_subprogram<compileUnit = #{{.*}}, scope = #[[NAMESPACE]], name = "namespace"
+
+define void @namespace(ptr %arg) {
+ call void @llvm.dbg.value(metadata ptr %arg, metadata !7, metadata !DIExpression()), !dbg !9
+ ret void
+}
+
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+!llvm.dbg.cu = !{!1}
+!llvm.module.flags = !{!0}
+!0 = !{i32 2, !"Debug Info Version", i32 3}
+!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2)
+!2 = !DIFile(filename: "debug-info.ll", directory: "/")
+!7 = !DILocalVariable(scope: !8, name: "var")
+!8 = distinct !DISubprogram(name: "namespace", scope: !10, file: !2, unit: !1);
+!9 = !DILocation(line: 1, column: 2, scope: !8)
+!10 = !DINamespace(name: "std", scope: null)
diff --git a/mlir/test/Target/LLVMIR/llvmir-debug.mlir b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
index 8b5a9f29864d..6e07b685abd9 100644
--- a/mlir/test/Target/LLVMIR/llvmir-debug.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
@@ -50,8 +50,14 @@ llvm.func @func_no_debug() {
>
#null = #llvm.di_null_type
#spType0 = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #null, #si64, #ptr, #named, #composite, #vector>
+#toplevel_namespace = #llvm.di_namespace<
+ name = "toplevel", exportSymbols = true
+>
+#nested_namespace = #llvm.di_namespace<
+ name = "nested", scope = #toplevel_namespace, exportSymbols = false
+>
#sp0 = #llvm.di_subprogram<
- compileUnit = #cu, scope = #file, name = "func_with_debug", linkageName = "func_with_debug",
+ compileUnit = #cu, scope = #nested_namespace, name = "func_with_debug", linkageName = "func_with_debug",
file = #file, line = 3, scopeLine = 3, subprogramFlags = "Definition|Optimized", type = #spType0
>
#calleeType = #llvm.di_subroutine_type<
@@ -113,7 +119,9 @@ llvm.func @empty_types() {
// CHECK: ![[CU_LOC:.*]] = distinct !DICompileUnit(language: DW_LANG_C, file: ![[CU_FILE_LOC:.*]], producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
// CHECK: ![[CU_FILE_LOC]] = !DIFile(filename: "foo.mlir", directory: "/test/")
-// CHECK: ![[FUNC_LOC]] = distinct !DISubprogram(name: "func_with_debug", linkageName: "func_with_debug", scope: ![[CU_FILE_LOC]], file: ![[CU_FILE_LOC]], line: 3, type: ![[FUNC_TYPE:.*]], scopeLine: 3, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: ![[CU_LOC]])
+// CHECK: ![[FUNC_LOC]] = distinct !DISubprogram(name: "func_with_debug", linkageName: "func_with_debug", scope: ![[NESTED_NAMESPACE:.*]], file: ![[CU_FILE_LOC]], line: 3, type: ![[FUNC_TYPE:.*]], scopeLine: 3, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: ![[CU_LOC]])
+// CHECK: ![[NESTED_NAMESPACE]] = !DINamespace(name: "nested", scope: ![[TOPLEVEL_NAMESPACE:.*]])
+// CHECK: ![[TOPLEVEL_NAMESPACE]] = !DINamespace(name: "toplevel", scope: null, exportSymbols: true)
// CHECK: ![[FUNC_TYPE]] = !DISubroutineType(cc: DW_CC_normal, types: ![[FUNC_ARGS:.*]])
// CHECK: ![[FUNC_ARGS]] = !{null, ![[ARG_TYPE:.*]], ![[PTR_TYPE:.*]], ![[NAMED_TYPE:.*]], ![[COMPOSITE_TYPE:.*]], ![[VECTOR_TYPE:.*]]}
// CHECK: ![[ARG_TYPE]] = !DIBasicType(name: "si64")
More information about the Mlir-commits
mailing list