Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def LLVM_Dialect : Dialect {
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

static StringRef getDwarfVersionAttrName() { return "llvm.dwarf_version"; }

/// Names of known llvm module flag keys.
static StringRef getModuleFlagKeyCGProfileName() {
Expand Down
10 changes: 9 additions & 1 deletion mlir/lib/Target/LLVMIR/DebugTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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");
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.

if (!llvmModule.getModuleFlag(kDwarfVersionKey))
llvmModule.addModuleFlag(llvm::Module::Max, kDwarfVersionKey, attrValue);
}
}

/// Translate the debug information for the given function.
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Target/LLVMIR/DebugTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
20 changes: 20 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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}