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( + 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}