[Mlir-commits] [mlir] [mlir] Add support for DIGlobalVariable, DIGlobalVariableExpression and DIExpression (PR #73367)

Justin Wilson llvmlistbot at llvm.org
Fri Nov 24 12:25:42 PST 2023


https://github.com/waj334 created https://github.com/llvm/llvm-project/pull/73367

This PR introduces DIGlobalVariableAttr, DIGlobalVariableExpressionAttr and DIExpressionAttr so that ModuleTranslation can emit the required metadata needed for debug information about global variable. The translator implementation for debug metadata needed to be refactored in order to allow translation of nodes based on MDNode (DIGlobalVariableExpressionAttr and DIExpression) in addition to DINode-based nodes.

A DIGlobalVariableExpressionAttr can now be passed to the GlobalOp operation directly and ModuleTranslation will create the respective DIGlobalVariable and DIGlobalVariableExpression nodes. The compile unit that DIGlobalVariable is expected to be configured with will be updated with the created DIGlobalVariableExpression.

>From 2801a423b1de6237b1a132f6829a8e634a825e4a Mon Sep 17 00:00:00 2001
From: "Justin A. Wilson" <justin.wilson at omibyte.io>
Date: Fri, 24 Nov 2023 14:22:34 -0600
Subject: [PATCH] [mlir] Add support for DIGlobalVariable,
 DIGlobalVariableExpression and DIExpression

Introduces DIGlobalVariableAttr, DIGlobalVariableExpressionAttr and DIExpressionAttr so that ModuleTranslation can emit the required metadata needed for debug information about global variable. The translator implementation for debug metadata needed to be refactored in order to allow translation of nodes based on MDNode (DIGlobalVariableExpressionAttr and DIExpression) in addition to DINode-based nodes.

A DIGlobalVariableExpressionAttr can now be passed to the GlobalOp operation directly and ModuleTranslation will create the respective DIGlobalVariable and DIGlobalVariableExpression nodes. The compile unit that DIGlobalVariable is expected to be configured with will be updated with the created DIGlobalVariableExpression.
---
 .../mlir/Dialect/LLVMIR/LLVMAttrDefs.td       | 45 +++++++++++++++++++
 mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h  | 13 +++++-
 mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td   |  4 +-
 .../mlir/Target/LLVMIR/ModuleTranslation.h    |  1 +
 .../Conversion/LLVMCommon/TypeConverter.cpp   | 10 ++++-
 mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp      | 18 +++++++-
 mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp    |  6 ++-
 mlir/lib/Target/LLVMIR/DebugImporter.cpp      | 37 ++++++++++++---
 mlir/lib/Target/LLVMIR/DebugImporter.h        | 18 +++++---
 mlir/lib/Target/LLVMIR/DebugTranslation.cpp   | 37 +++++++++++----
 mlir/lib/Target/LLVMIR/DebugTranslation.h     | 13 ++++--
 mlir/lib/Target/LLVMIR/ModuleTranslation.cpp  | 15 +++++++
 12 files changed, 188 insertions(+), 29 deletions(-)

diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
index 9e35bf1ba977725..09f2f7c8048349f 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
@@ -11,6 +11,7 @@
 
 include "mlir/Dialect/LLVMIR/LLVMDialect.td"
 include "mlir/IR/AttrTypeBase.td"
+include "mlir/IR/CommonAttrConstraints.td"
 
 // All of the attributes will extend this class.
 class LLVM_Attr<string name, string attrMnemonic,
@@ -447,6 +448,50 @@ def LLVM_DILocalVariableAttr : LLVM_Attr<"DILocalVariable", "di_local_variable",
   let assemblyFormat = "`<` struct(params) `>`";
 }
 
+//===----------------------------------------------------------------------===//
+// DIExpressionAttr
+//===----------------------------------------------------------------------===//
+
+def LLVM_DIExpressionAttr : LLVM_Attr<"DIExpression", "di_expression",
+                                          /*traits=*/[], "MDNodeAttr"> {
+  let parameters = (ins
+    ArrayRefParameter<"uint64_t">:$operations
+  );
+  let assemblyFormat = "`<` struct(params) `>`";
+}
+
+//===----------------------------------------------------------------------===//
+// DIGlobalVariableExpressionAttr
+//===----------------------------------------------------------------------===//
+
+def LLVM_DIGlobalVariableExpressionAttr : LLVM_Attr<"DIGlobalVariableExpression", "di_global_variable_expression",
+                                          /*traits=*/[], "MDNodeAttr"> {
+  let parameters = (ins
+    "DIGlobalVariableAttr":$var,
+    OptionalParameter<"DIExpressionAttr">:$expr
+  );
+  let assemblyFormat = "`<` struct(params) `>`";
+}
+
+//===----------------------------------------------------------------------===//
+// DIGlobalVariableAttr
+//===----------------------------------------------------------------------===//
+
+def LLVM_DIGlobalVariable : LLVM_Attr<"DIGlobalVariable", "di_global_variable",
+                                      /*traits=*/[], "DINodeAttr"> {
+  let parameters = (ins
+    "DIScopeAttr":$scope,
+    OptionalParameter<"StringAttr">:$name,
+    OptionalParameter<"StringAttr">:$linkageName,
+    OptionalParameter<"DIFileAttr">:$file,
+    OptionalParameter<"unsigned">:$line,
+    OptionalParameter<"DITypeAttr">:$type,
+    OptionalParameter<"bool">:$isLocalToUnit,
+    OptionalParameter<"bool">:$isDefined,
+    OptionalParameter<"unsigned">:$alignInBits);
+  let assemblyFormat = "`<` struct(params) `>`";
+}
+
 //===----------------------------------------------------------------------===//
 // DISubprogramAttr
 //===----------------------------------------------------------------------===//
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h
index c370bfa2b733d65..e49b40e2d487dc8 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h
@@ -23,8 +23,8 @@
 namespace mlir {
 namespace LLVM {
 
-/// This class represents the base attribute for all debug info attributes.
-class DINodeAttr : public Attribute {
+/// This class represents the base attribute for all metadata attributes.
+class MDNodeAttr : public Attribute {
 public:
   using Attribute::Attribute;
 
@@ -32,6 +32,15 @@ class DINodeAttr : public Attribute {
   static bool classof(Attribute attr);
 };
 
+/// This class represents the base attribute for all debug info attributes.
+class DINodeAttr : public MDNodeAttr {
+public:
+  using MDNodeAttr::MDNodeAttr;
+
+  // Support LLVM type casting.
+  static bool classof(Attribute attr);
+};
+
 /// This class represents a LLVM attribute that describes a debug info scope.
 class DIScopeAttr : public DINodeAttr {
 public:
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
index 88f4f81735372b9..ecfdced641299fe 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
@@ -1087,6 +1087,7 @@ def LLVM_GlobalOp : LLVM_Op<"mlir.global",
     OptionalAttr<UnnamedAddr>:$unnamed_addr,
     OptionalAttr<StrAttr>:$section,
     OptionalAttr<SymbolRefAttr>:$comdat,
+    OptionalAttr<LLVM_DIGlobalVariableExpressionAttr>:$dbg_expr,
     DefaultValuedAttr<Visibility, "mlir::LLVM::Visibility::Default">:$visibility_
   );
   let summary = "LLVM dialect global.";
@@ -1196,7 +1197,8 @@ def LLVM_GlobalOp : LLVM_Op<"mlir.global",
       CArg<"bool", "false">:$dsoLocal,
       CArg<"bool", "false">:$thread_local_,
       CArg<"SymbolRefAttr", "{}">:$comdat,
-      CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)>
+      CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs,
+      CArg<"DIGlobalVariableExpressionAttr", "{}">:$dbgExpr)>
   ];
 
   let extraClassDeclaration = [{
diff --git a/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h b/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
index 4820e826d0ca357..a94596cc9583b1f 100644
--- a/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
+++ b/mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
@@ -46,6 +46,7 @@ class LoopAnnotationTranslation;
 
 class AliasScopeAttr;
 class AliasScopeDomainAttr;
+class MDNodeAttr;
 class DINodeAttr;
 class LLVMFuncOp;
 class ComdatSelectorOp;
diff --git a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
index 3a01795ce3f53e5..a306007953d5b7b 100644
--- a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
+++ b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
@@ -86,7 +86,15 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx,
 
     if (type.isIdentified()) {
       auto convertedType = LLVM::LLVMStructType::getIdentified(
-          type.getContext(), ("_Converted." + type.getName()).str());
+          type.getContext(), ("_Converted_" + type.getName()).str());
+      unsigned counter = 1;
+      while (convertedType.isInitialized()) {
+        assert(counter != UINT_MAX &&
+               "about to overflow struct renaming counter in conversion");
+        convertedType = LLVM::LLVMStructType::getIdentified(
+            type.getContext(),
+            ("_Converted_" + std::to_string(counter) + type.getName()).str());
+      }
 
       SmallVectorImpl<Type> &recursiveStack = getCurrentThreadRecursiveStack();
       if (llvm::count(recursiveStack, type)) {
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
index 3d45ab8fac4d705..1f5ed86f8d875e9 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
@@ -37,14 +37,28 @@ void LLVMDialect::registerAttributes() {
       >();
 }
 
+//===----------------------------------------------------------------------===//
+// MDNodeAttr
+//===----------------------------------------------------------------------===//
+
+bool MDNodeAttr::classof(Attribute attr) {
+  return llvm::isa<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
+                   DIDerivedTypeAttr, DIExpressionAttr, DIFileAttr,
+                   DIGlobalVariableAttr, DIGlobalVariableExpressionAttr,
+                   DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
+                   DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
+                   DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
+                   DISubroutineTypeAttr>(attr);
+}
+
 //===----------------------------------------------------------------------===//
 // DINodeAttr
 //===----------------------------------------------------------------------===//
 
 bool DINodeAttr::classof(Attribute attr) {
   return llvm::isa<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
-                   DIDerivedTypeAttr, DIFileAttr, DILabelAttr,
-                   DILexicalBlockAttr, DILexicalBlockFileAttr,
+                   DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
+                   DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
                    DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
                    DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
                    DISubroutineTypeAttr>(attr);
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index f6c8f388732c3da..53c0c9b70a8824a 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -1781,7 +1781,7 @@ void GlobalOp::build(OpBuilder &builder, OperationState &result, Type type,
                      bool isConstant, Linkage linkage, StringRef name,
                      Attribute value, uint64_t alignment, unsigned addrSpace,
                      bool dsoLocal, bool threadLocal, SymbolRefAttr comdat,
-                     ArrayRef<NamedAttribute> attrs) {
+                     ArrayRef<NamedAttribute> attrs, DIGlobalVariableExpressionAttr dbgExpr) {
   result.addAttribute(getSymNameAttrName(result.name),
                       builder.getStringAttr(name));
   result.addAttribute(getGlobalTypeAttrName(result.name), TypeAttr::get(type));
@@ -1812,6 +1812,10 @@ void GlobalOp::build(OpBuilder &builder, OperationState &result, Type type,
     result.addAttribute(getAddrSpaceAttrName(result.name),
                         builder.getI32IntegerAttr(addrSpace));
   result.attributes.append(attrs.begin(), attrs.end());
+
+  if (dbgExpr)
+    result.addAttribute(getDbgExprAttrName(result.name), dbgExpr);
+
   result.addRegion();
 }
 
diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.cpp b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
index 36a03ec71c0c6c6..ae221bd1779eeeb 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.cpp
@@ -61,7 +61,7 @@ DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
   SmallVector<DINodeAttr> elements;
   for (llvm::DINode *element : node->getElements()) {
     assert(element && "expected a non-null element type");
-    elements.push_back(translate(element));
+    elements.push_back(cast<DINodeAttr>(translate(element)));
   }
   // Drop the elements parameter if a cyclic dependency is detected. We
   // currently cannot model these cycles and thus drop the parameter if
@@ -116,6 +116,27 @@ DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
                                      node->getDiscriminator());
 }
 
+DIExpressionAttr DebugImporter::translateImpl(llvm::DIExpression *node) {
+  return DIExpressionAttr::get(context, node->getElements());
+}
+
+DIGlobalVariableExpressionAttr
+DebugImporter::translateImpl(llvm::DIGlobalVariableExpression *node) {
+  return DIGlobalVariableExpressionAttr::get(context,
+                                             translate(node->getVariable()),
+                                             translate(node->getExpression()));
+}
+
+DIGlobalVariableAttr
+DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
+  return DIGlobalVariableAttr::get(
+      context, translate(node->getScope()),
+      StringAttr::get(context, node->getName()),
+      StringAttr::get(context, node->getLinkageName()),
+      translate(node->getFile()), node->getLine(), translate(node->getType()),
+      node->isLocalToUnit(), node->isDefinition(), node->getAlignInBits());
+}
+
 DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
   return DILocalVariableAttr::get(context, translate(node->getScope()),
                                   getStringAttrOrNull(node->getRawName()),
@@ -204,12 +225,12 @@ DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
   return cast<DITypeAttr>(translate(static_cast<llvm::DINode *>(node)));
 }
 
-DINodeAttr DebugImporter::translate(llvm::DINode *node) {
+MDNodeAttr DebugImporter::translate(llvm::MDNode *node) {
   if (!node)
     return nullptr;
 
   // Check for a cached instance.
-  if (DINodeAttr attr = nodeToAttr.lookup(node))
+  if (MDNodeAttr attr = nodeToAttr.lookup(node))
     return attr;
 
   // Return nullptr if a cyclic dependency is detected since the same node is
@@ -220,7 +241,7 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
   auto guard = llvm::make_scope_exit([&]() { translationStack.pop_back(); });
 
   // Convert the debug metadata if possible.
-  auto translateNode = [this](llvm::DINode *node) -> DINodeAttr {
+  auto translateNode = [this](llvm::MDNode *node) -> MDNodeAttr {
     if (auto *casted = dyn_cast<llvm::DIBasicType>(node))
       return translateImpl(casted);
     if (auto *casted = dyn_cast<llvm::DICompileUnit>(node))
@@ -231,6 +252,12 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
       return translateImpl(casted);
     if (auto *casted = dyn_cast<llvm::DIFile>(node))
       return translateImpl(casted);
+    if (auto *casted = dyn_cast<llvm::DIExpression>(node))
+      return translateImpl(casted);
+    if (auto *casted = dyn_cast<llvm::DIGlobalVariableExpression>(node))
+      return translateImpl(casted);
+    if (auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
+      return translateImpl(casted);
     if (auto *casted = dyn_cast<llvm::DILabel>(node))
       return translateImpl(casted);
     if (auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
@@ -251,7 +278,7 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
       return translateImpl(casted);
     return nullptr;
   };
-  if (DINodeAttr attr = translateNode(node)) {
+  if (MDNodeAttr attr = translateNode(node)) {
     nodeToAttr.insert({node, attr});
     return attr;
   }
diff --git a/mlir/lib/Target/LLVMIR/DebugImporter.h b/mlir/lib/Target/LLVMIR/DebugImporter.h
index 5c07ec82d0191bc..6e92a8823889387 100644
--- a/mlir/lib/Target/LLVMIR/DebugImporter.h
+++ b/mlir/lib/Target/LLVMIR/DebugImporter.h
@@ -40,15 +40,18 @@ class DebugImporter {
   Location translateFuncLocation(llvm::Function *func);
 
   /// Translates the given LLVM debug metadata to MLIR.
-  DINodeAttr translate(llvm::DINode *node);
+  MDNodeAttr translate(llvm::MDNode *node);
+  MDNodeAttr translate(llvm::DINode *node) {
+    return translate(cast<llvm::MDNode>(node));
+  }
 
   /// Infers the metadata type and translates it to MLIR.
-  template <typename DINodeT>
-  auto translate(DINodeT *node) {
+  template <typename MDNodeT>
+  auto translate(MDNodeT *node) {
     // Infer the MLIR type from the LLVM metadata type.
     using MLIRTypeT = decltype(translateImpl(node));
     return cast_or_null<MLIRTypeT>(
-        translate(static_cast<llvm::DINode *>(node)));
+        translate(static_cast<llvm::MDNode *>(node)));
   }
 
 private:
@@ -61,6 +64,9 @@ class DebugImporter {
   DILabelAttr translateImpl(llvm::DILabel *node);
   DILexicalBlockAttr translateImpl(llvm::DILexicalBlock *node);
   DILexicalBlockFileAttr translateImpl(llvm::DILexicalBlockFile *node);
+  DIExpressionAttr translateImpl(llvm::DIExpression *node);
+  DIGlobalVariableExpressionAttr translateImpl(llvm::DIGlobalVariableExpression *node);
+  DIGlobalVariableAttr translateImpl(llvm::DIGlobalVariable *node);
   DILocalVariableAttr translateImpl(llvm::DILocalVariable *node);
   DIModuleAttr translateImpl(llvm::DIModule *node);
   DINamespaceAttr translateImpl(llvm::DINamespace *node);
@@ -75,11 +81,11 @@ class DebugImporter {
   StringAttr getStringAttrOrNull(llvm::MDString *stringNode);
 
   /// A mapping between LLVM debug metadata and the corresponding attribute.
-  DenseMap<llvm::DINode *, DINodeAttr> nodeToAttr;
+  DenseMap<llvm::MDNode *, MDNodeAttr> nodeToAttr;
 
   /// A stack that stores the metadata nodes that are being traversed. The stack
   /// is used to detect cyclic dependencies during the metadata translation.
-  SetVector<llvm::DINode *> translationStack;
+  SetVector<llvm::MDNode *> translationStack;
 
   MLIRContext *context;
   ModuleOp mlirModule;
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
index a15ffb40065a08b..49f69c2376220a9 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -189,6 +189,25 @@ DebugTranslation::translateImpl(DILocalVariableAttr attr) {
       /*Annotations=*/nullptr);
 }
 
+llvm::DIExpression *DebugTranslation::translateImpl(DIExpressionAttr attr) {
+  return llvm::DIExpression::get(llvmCtx, attr.getOperations());
+}
+
+llvm::DIGlobalVariableExpression *
+DebugTranslation::translateImpl(DIGlobalVariableExpressionAttr attr) {
+  return llvm::DIGlobalVariableExpression::get(
+      llvmCtx, translate(attr.getVar()), translate(attr.getExpr()));
+}
+
+llvm::DIGlobalVariable *
+DebugTranslation::translateImpl(DIGlobalVariableAttr attr) {
+  return llvm::DIGlobalVariable::getDistinct(
+      llvmCtx, translate(attr.getScope()), getMDStringOrNull(attr.getName()),
+      getMDStringOrNull(attr.getLinkageName()), translate(attr.getFile()),
+      attr.getLine(), translate(attr.getType()), attr.getIsLocalToUnit(),
+      attr.getIsDefined(), nullptr, nullptr, attr.getAlignInBits(), nullptr);
+}
+
 llvm::DIScope *DebugTranslation::translateImpl(DIScopeAttr attr) {
   return cast<llvm::DIScope>(translate(DINodeAttr(attr)));
 }
@@ -250,20 +269,22 @@ llvm::DIType *DebugTranslation::translateImpl(DITypeAttr attr) {
   return cast<llvm::DIType>(translate(DINodeAttr(attr)));
 }
 
-llvm::DINode *DebugTranslation::translate(DINodeAttr attr) {
+llvm::MDNode *DebugTranslation::translate(MDNodeAttr attr) {
   if (!attr)
     return nullptr;
   // Check for a cached instance.
-  if (llvm::DINode *node = attrToNode.lookup(attr))
+  if (llvm::MDNode *node = attrToNode.lookup(attr))
     return node;
 
-  llvm::DINode *node =
-      TypeSwitch<DINodeAttr, llvm::DINode *>(attr)
+  llvm::MDNode *node =
+      TypeSwitch<MDNodeAttr, llvm::MDNode *>(attr)
           .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
-                DIDerivedTypeAttr, DIFileAttr, DILabelAttr, DILexicalBlockAttr,
-                DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
-                DINamespaceAttr, DINullTypeAttr, DISubprogramAttr,
-                DISubrangeAttr, DISubroutineTypeAttr>(
+                DIDerivedTypeAttr, DIExpressionAttr, DIFileAttr,
+                DIGlobalVariableAttr, DIGlobalVariableExpressionAttr,
+                DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
+                DILocalVariableAttr, DIModuleAttr, 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 d22c3d80dafd5f9..a9b5adf9bdf2c59 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.h
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.h
@@ -41,14 +41,17 @@ class DebugTranslation {
   void translate(LLVMFuncOp func, llvm::Function &llvmFunc);
 
   /// Translate the given LLVM debug metadata to LLVM.
-  llvm::DINode *translate(DINodeAttr attr);
+  llvm::MDNode *translate(MDNodeAttr attr);
+  llvm::MDNode *translate(DINodeAttr attr) {
+    return translate(cast<MDNodeAttr>(attr));
+  }
 
   /// Translate the given derived LLVM debug metadata to LLVM.
   template <typename DIAttrT>
   auto translate(DIAttrT attr) {
     // Infer the LLVM type from the attribute type.
     using LLVMTypeT = std::remove_pointer_t<decltype(translateImpl(attr))>;
-    return cast_or_null<LLVMTypeT>(translate(DINodeAttr(attr)));
+    return cast_or_null<LLVMTypeT>(translate(MDNodeAttr(attr)));
   }
 
 private:
@@ -72,6 +75,10 @@ class DebugTranslation {
   llvm::DILexicalBlockFile *translateImpl(DILexicalBlockFileAttr attr);
   llvm::DILocalScope *translateImpl(DILocalScopeAttr attr);
   llvm::DILocalVariable *translateImpl(DILocalVariableAttr attr);
+  llvm::DIExpression *translateImpl(DIExpressionAttr attr);
+  llvm::DIGlobalVariableExpression *
+  translateImpl(DIGlobalVariableExpressionAttr attr);
+  llvm::DIGlobalVariable *translateImpl(DIGlobalVariableAttr attr);
   llvm::DIModule *translateImpl(DIModuleAttr attr);
   llvm::DINamespace *translateImpl(DINamespaceAttr attr);
   llvm::DIScope *translateImpl(DIScopeAttr attr);
@@ -92,7 +99,7 @@ class DebugTranslation {
 
   /// A mapping between debug attribute and the corresponding llvm debug
   /// metadata.
-  DenseMap<Attribute, llvm::DINode *> attrToNode;
+  DenseMap<Attribute, llvm::MDNode *> attrToNode;
 
   /// A mapping between filename and llvm debug file.
   /// TODO: Change this to DenseMap<Identifier, ...> when we can
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 322843e65627603..b2faac162e87919 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -782,6 +782,21 @@ LogicalResult ModuleTranslation::convertGlobals() {
     var->setVisibility(convertVisibilityToLLVM(op.getVisibility_()));
 
     globalsMapping.try_emplace(op, var);
+
+    // Add debug information if present
+    if (op.getDbgExpr().has_value()) {
+      const auto globalExpr = debugTranslation->translate(*op.getDbgExpr());
+      auto globalVar = globalExpr->getVariable();
+      var->addDebugInfo(globalExpr);
+
+      // Get the compile unit (scope) of the the global variable
+      if (const auto compileUnit = cast<llvm::DICompileUnit>(globalVar->getScope());
+          compileUnit) {
+        // Update the compile unit with this incoming global variable
+        compileUnit->replaceGlobalVariables(
+            llvm::MDTuple::get(getLLVMContext(), {globalExpr}));
+      }
+    }
   }
 
   // Convert global variable bodies. This is done after all global variables



More information about the Mlir-commits mailing list