-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir] Allow setting of Dwarf version information. #158692
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
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.
@llvm/pr-subscribers-mlir-llvm @llvm/pr-subscribers-mlir Author: Abid Qadeer (abidh) ChangesThe dwarf version to use can be set in the LLVM IR using the Full diff: https://github.com/llvm/llvm-project/pull/158692.diff 5 Files Affected:
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}
|
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 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. |
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.
/// 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"); |
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.
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.
While looking to implement import functionality for this feature, I found that there is already a mechanism to set the module flag using |
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.