Skip to content

Conversation

abidh
Copy link
Contributor

@abidh abidh commented Sep 15, 2025

The dwarf version to use can be set in the LLVM IR using the Dwarf Version flag. This PR allows setting this flag by using a new attribute on the module.

The dwarf version to use can be set in the LLVM IR using the
"Dwarf Version" flag. This PR allows setting this flag by using a
new attribute on the module.
@llvmbot
Copy link
Member

llvmbot commented Sep 15, 2025

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-mlir

Author: Abid Qadeer (abidh)

Changes

The dwarf version to use can be set in the LLVM IR using the Dwarf Version flag. This PR allows setting this flag by using a new attribute on the module.


Full diff: https://github.com/llvm/llvm-project/pull/158692.diff

5 Files Affected:

  • (modified) mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td (+2)
  • (modified) mlir/lib/Target/LLVMIR/DebugTranslation.cpp (+9-1)
  • (modified) mlir/lib/Target/LLVMIR/DebugTranslation.h (+1-1)
  • (modified) mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (+1-1)
  • (modified) mlir/test/Target/LLVMIR/llvmir.mlir (+20)
diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
index d2d71318a6118..542302131b31a 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
@@ -92,6 +92,8 @@ def LLVM_Dialect : Dialect {
     static StringRef getDependentLibrariesAttrName() {
       return "llvm.dependent_libraries";
     }
+    /// Name of the dwarf version attribute.
+    static StringRef getDwarfVersionAttrName() { return "llvm.dwarf_version"; }
 
     /// Names of known llvm module flag keys.
     static StringRef getModuleFlagKeyCGProfileName() {
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
index a55445deddc2d..1e27d6192c1b9 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -36,8 +36,9 @@ DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
 
 static constexpr StringRef kDebugVersionKey = "Debug Info Version";
 static constexpr StringRef kCodeViewKey = "CodeView";
+static constexpr StringRef kDwarfVersionKey = "Dwarf Version";
 
-void DebugTranslation::addModuleFlagsIfNotPresent() {
+void DebugTranslation::addModuleFlagsIfNotPresent(Operation *module) {
   // TODO: The version information should be encoded on the LLVM module itself,
   // not implicitly set here.
 
@@ -53,6 +54,13 @@ void DebugTranslation::addModuleFlagsIfNotPresent() {
     if (!llvmModule.getModuleFlag(kCodeViewKey))
       llvmModule.addModuleFlag(llvm::Module::Warning, kCodeViewKey, 1);
   }
+  if (auto attr = module->getAttrOfType<IntegerAttr>(
+          LLVM::LLVMDialect::getDwarfVersionAttrName())) {
+    int32_t attrValue = attr.getInt();
+    assert((attrValue >= 2 && attrValue <= 5) && "Unsupported DWARF version");
+    if (!llvmModule.getModuleFlag(kDwarfVersionKey))
+      llvmModule.addModuleFlag(llvm::Module::Max, kDwarfVersionKey, attrValue);
+  }
 }
 
 /// Translate the debug information for the given function.
diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.h b/mlir/lib/Target/LLVMIR/DebugTranslation.h
index b690d4820d7b0..9e89346ee9291 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.h
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.h
@@ -32,7 +32,7 @@ class DebugTranslation {
   DebugTranslation(Operation *module, llvm::Module &llvmModule);
 
   /// Adds the necessary module flags to the module, if not yet present.
-  void addModuleFlagsIfNotPresent();
+  void addModuleFlagsIfNotPresent(Operation *module);
 
   /// Translate the given location to an llvm debug location.
   llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 9725359160a1a..4ff610379e53d 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -2418,7 +2418,7 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
 
   // Add the necessary debug info module flags, if they were not encoded in MLIR
   // beforehand.
-  translator.debugTranslation->addModuleFlagsIfNotPresent();
+  translator.debugTranslation->addModuleFlagsIfNotPresent(module);
 
   // Call the OpenMP IR Builder callbacks prior to verifying the module
   if (auto *ompBuilder = translator.getOpenMPBuilder())
diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index 69814f2748e1d..860f1eb5b3502 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -3040,3 +3040,23 @@ llvm.mlir.global external @target_specific_attrs_only() {target_specific_attrs =
 // CHECK: @target_specific_attrs_combined = global i32 2, section "mysection", align 4 #[[ATTRS:[0-9]+]]
 // CHECK: attributes #[[ATTRS]] = { norecurse "bss-section"="my_bss.1" }
 llvm.mlir.global external @target_specific_attrs_combined(2 : i32) {alignment = 4 : i64, section = "mysection", target_specific_attrs = ["norecurse", ["bss-section", "my_bss.1"]]} : i32
+
+// -----
+
+module attributes {llvm.dwarf_version = 5 : i32} {}
+// CHECK: !{i32 7, !"Dwarf Version", i32 5}
+
+// -----
+
+module attributes {llvm.dwarf_version = 4 : i32} {}
+// CHECK: !{i32 7, !"Dwarf Version", i32 4}
+
+// -----
+
+module attributes {llvm.dwarf_version = 3 : i32} {}
+// CHECK: !{i32 7, !"Dwarf Version", i32 3}
+
+// -----
+
+module attributes {llvm.dwarf_version = 2 : i32} {}
+// CHECK: !{i32 7, !"Dwarf Version", i32 2}

Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this.

Can you update the import from LLVM IR as well?

static StringRef getDependentLibrariesAttrName() {
return "llvm.dependent_libraries";
}
/// Name of the dwarf version attribute.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Name of the dwarf version attribute.
/// Name of the dwarf version attribute.

nit: let's add a newline as for the other cases

if (auto attr = module->getAttrOfType<IntegerAttr>(
LLVM::LLVMDialect::getDwarfVersionAttrName())) {
int32_t attrValue = attr.getInt();
assert((attrValue >= 2 && attrValue <= 5) && "Unsupported DWARF version");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance can emit an error diagnostics and return failure here (ideally including a small test)? A warning may also be appropriate if the information can be dropped safely.

An assert seems a bit hard since an input IR change may trigger the assert.

@abidh
Copy link
Contributor Author

abidh commented Sep 16, 2025

While looking to implement import functionality for this feature, I found that there is already a mechanism to set the module flag using ModuleFlagAttr and ModuleFlagsOp which clients can use. So this PR is not required. Closing it for now.

@abidh abidh closed this Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants