Skip to content

Commit b690a3c

Browse files
committed
[mlir] Allow setting of Dwarf version information.
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.
1 parent e299d9a commit b690a3c

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def LLVM_Dialect : Dialect {
9292
static StringRef getDependentLibrariesAttrName() {
9393
return "llvm.dependent_libraries";
9494
}
95+
/// Name of the dwarf version attribute.
96+
static StringRef getDwarfVersionAttrName() { return "llvm.dwarf_version"; }
9597

9698
/// Names of known llvm module flag keys.
9799
static StringRef getModuleFlagKeyCGProfileName() {

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
3636

3737
static constexpr StringRef kDebugVersionKey = "Debug Info Version";
3838
static constexpr StringRef kCodeViewKey = "CodeView";
39+
static constexpr StringRef kDwarfVersionKey = "Dwarf Version";
3940

40-
void DebugTranslation::addModuleFlagsIfNotPresent() {
41+
void DebugTranslation::addModuleFlagsIfNotPresent(Operation *module) {
4142
// TODO: The version information should be encoded on the LLVM module itself,
4243
// not implicitly set here.
4344

@@ -53,6 +54,13 @@ void DebugTranslation::addModuleFlagsIfNotPresent() {
5354
if (!llvmModule.getModuleFlag(kCodeViewKey))
5455
llvmModule.addModuleFlag(llvm::Module::Warning, kCodeViewKey, 1);
5556
}
57+
if (auto attr = module->getAttrOfType<IntegerAttr>(
58+
LLVM::LLVMDialect::getDwarfVersionAttrName())) {
59+
int32_t attrValue = attr.getInt();
60+
assert((attrValue >= 2 && attrValue <= 5) && "Unsupported DWARF version");
61+
if (!llvmModule.getModuleFlag(kDwarfVersionKey))
62+
llvmModule.addModuleFlag(llvm::Module::Max, kDwarfVersionKey, attrValue);
63+
}
5664
}
5765

5866
/// Translate the debug information for the given function.

mlir/lib/Target/LLVMIR/DebugTranslation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DebugTranslation {
3232
DebugTranslation(Operation *module, llvm::Module &llvmModule);
3333

3434
/// Adds the necessary module flags to the module, if not yet present.
35-
void addModuleFlagsIfNotPresent();
35+
void addModuleFlagsIfNotPresent(Operation *module);
3636

3737
/// Translate the given location to an llvm debug location.
3838
llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,7 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
24182418

24192419
// Add the necessary debug info module flags, if they were not encoded in MLIR
24202420
// beforehand.
2421-
translator.debugTranslation->addModuleFlagsIfNotPresent();
2421+
translator.debugTranslation->addModuleFlagsIfNotPresent(module);
24222422

24232423
// Call the OpenMP IR Builder callbacks prior to verifying the module
24242424
if (auto *ompBuilder = translator.getOpenMPBuilder())

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3040,3 +3040,23 @@ llvm.mlir.global external @target_specific_attrs_only() {target_specific_attrs =
30403040
// CHECK: @target_specific_attrs_combined = global i32 2, section "mysection", align 4 #[[ATTRS:[0-9]+]]
30413041
// CHECK: attributes #[[ATTRS]] = { norecurse "bss-section"="my_bss.1" }
30423042
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
3043+
3044+
// -----
3045+
3046+
module attributes {llvm.dwarf_version = 5 : i32} {}
3047+
// CHECK: !{i32 7, !"Dwarf Version", i32 5}
3048+
3049+
// -----
3050+
3051+
module attributes {llvm.dwarf_version = 4 : i32} {}
3052+
// CHECK: !{i32 7, !"Dwarf Version", i32 4}
3053+
3054+
// -----
3055+
3056+
module attributes {llvm.dwarf_version = 3 : i32} {}
3057+
// CHECK: !{i32 7, !"Dwarf Version", i32 3}
3058+
3059+
// -----
3060+
3061+
module attributes {llvm.dwarf_version = 2 : i32} {}
3062+
// CHECK: !{i32 7, !"Dwarf Version", i32 2}

0 commit comments

Comments
 (0)