Skip to content

Commit 0b4a900

Browse files
committed
[MLIR][LLVM] Avoid duplicated module flags in the export
This commit resolves an issue in the LLVMIR export that caused the duplication of the "Debug Info Version" module flag, when it was already in MLIR.
1 parent 9b1ce47 commit 0b4a900

File tree

6 files changed

+40
-27
lines changed

6 files changed

+40
-27
lines changed

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,29 @@ DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
3232
if (!module->walk(interruptIfValidLocation).wasInterrupted())
3333
return;
3434
debugEmissionIsEnabled = true;
35+
}
36+
37+
static constexpr StringRef kDebugVersionKey = "Debug Info Version";
38+
static constexpr StringRef kCodeViewKey = "CodeView";
3539

40+
void DebugTranslation::addModuleFlagsIfNotPresent() {
3641
// TODO: The version information should be encoded on the LLVM module itself,
3742
// not implicitly set here.
3843

3944
// Mark this module as having debug information.
40-
StringRef debugVersionKey = "Debug Info Version";
41-
if (!llvmModule.getModuleFlag(debugVersionKey))
42-
llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
45+
if (!llvmModule.getModuleFlag(kDebugVersionKey))
46+
llvmModule.addModuleFlag(llvm::Module::Warning, kDebugVersionKey,
4347
llvm::DEBUG_METADATA_VERSION);
4448

45-
if (auto targetTripleAttr = module->getDiscardableAttr(
46-
LLVM::LLVMDialect::getTargetTripleAttrName())) {
47-
auto targetTriple =
48-
llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue());
49-
if (targetTriple.isKnownWindowsMSVCEnvironment()) {
50-
// Dwarf debugging files will be generated by default, unless "CodeView"
51-
// is set explicitly. Windows/MSVC should use CodeView instead.
52-
llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1);
53-
}
49+
const llvm::Triple &targetTriple = llvmModule.getTargetTriple();
50+
if (targetTriple.isKnownWindowsMSVCEnvironment()) {
51+
// Dwarf debugging files will be generated by default, unless "CodeView"
52+
// is set explicitly. Windows/MSVC should use CodeView instead.
53+
if (!llvmModule.getModuleFlag(kCodeViewKey))
54+
llvmModule.addModuleFlag(llvm::Module::Warning, kCodeViewKey, 1);
5455
}
5556
}
5657

57-
/// Finalize the translation of debug information.
58-
void DebugTranslation::finalize() {}
59-
6058
/// Translate the debug information for the given function.
6159
void DebugTranslation::translate(LLVMFuncOp func, llvm::Function &llvmFunc) {
6260
if (!debugEmissionIsEnabled)

mlir/lib/Target/LLVMIR/DebugTranslation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class DebugTranslation {
3131
public:
3232
DebugTranslation(Operation *module, llvm::Module &llvmModule);
3333

34-
/// Finalize the translation of debug information.
35-
void finalize();
34+
/// Adds the necessary module flags to the module, if not yet present.
35+
void addModuleFlagsIfNotPresent();
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,10 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
22232223
// See https://llvm.org/docs/RemoveDIsDebugInfo.html
22242224
translator.llvmModule->setIsNewDbgInfoFormat(UseNewDbgInfoFormat);
22252225

2226+
// Add the necessary debug info module flags, if they were not encoded in MLIR
2227+
// beforehand.
2228+
translator.debugTranslation->addModuleFlagsIfNotPresent();
2229+
22262230
if (!disableVerification &&
22272231
llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
22282232
return nullptr;

mlir/test/Target/LLVMIR/llvmir-debug.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ llvm.func @dbg_intrinsics_with_no_location(%arg0: i32) -> (i32) {
316316

317317
// CHECK: @global_with_expr_1 = external global i64, !dbg {{.*}}
318318
// CHECK: @global_with_expr_2 = external global i64, !dbg {{.*}}
319-
// CHECK: !llvm.module.flags = !{{{.*}}}
320-
// CHECK: !llvm.dbg.cu = !{{{.*}}}
319+
// CHECK-DAG: !llvm.module.flags = !{{{.*}}}
320+
// CHECK-DAG: !llvm.dbg.cu = !{{{.*}}}
321321
// CHECK-DAG: ![[FILE:.*]] = !DIFile(filename: "not", directory: "existence")
322322
// CHECK-DAG: ![[TYPE:.*]] = !DIBasicType(name: "uint64_t", size: 64, encoding: DW_ATE_unsigned)
323323
// CHECK-DAG: ![[SCOPE:.*]] = distinct !DICompileUnit(language: DW_LANG_C, file: ![[FILE]], producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: ![[GVALS:.*]])
@@ -336,8 +336,8 @@ llvm.mlir.global external @global_with_expr_2() {addr_space = 0 : i32, dbg_exprs
336336
// -----
337337

338338
// CHECK: @module_global = external global i64, !dbg {{.*}}
339-
// CHECK: !llvm.module.flags = !{{{.*}}}
340-
// CHECK: !llvm.dbg.cu = !{{{.*}}}
339+
// CHECK-DAG: !llvm.module.flags = !{{{.*}}}
340+
// CHECK-DAG: !llvm.dbg.cu = !{{{.*}}}
341341
// CHECK-DAG: ![[FILE:.*]] = !DIFile(filename: "test.f90", directory: "existence")
342342
// CHECK-DAG: ![[TYPE:.*]] = !DIBasicType(name: "integer", size: 64, encoding: DW_ATE_signed)
343343
// CHECK-DAG: ![[SCOPE:.*]] = distinct !DICompileUnit(language: DW_LANG_Fortran95, file: ![[FILE]], producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: ![[GVALS:.*]])

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,11 +2774,22 @@ module {
27742774
#llvm.mlir.module_flag<max, "frame-pointer", 1>]
27752775
}
27762776

2777-
// CHECK: !llvm.module.flags = !{![[#DBG:]], ![[#WCHAR:]], ![[#PIC:]], ![[#PIE:]], ![[#UWTABLE:]], ![[#FrameP:]]}
2777+
// CHECK: !llvm.module.flags = !{![[#WCHAR:]], ![[#PIC:]], ![[#PIE:]], ![[#UWTABLE:]], ![[#FrameP:]], ![[#DBG:]]}
27782778

2779-
// CHECK: ![[#DBG]] = !{i32 2, !"Debug Info Version", i32 3}
27802779
// CHECK: ![[#WCHAR]] = !{i32 1, !"wchar_size", i32 4}
27812780
// CHECK: ![[#PIC]] = !{i32 8, !"PIC Level", i32 2}
27822781
// CHECK: ![[#PIE]] = !{i32 7, !"PIE Level", i32 2}
27832782
// CHECK: ![[#UWTABLE]] = !{i32 7, !"uwtable", i32 2}
27842783
// CHECK: ![[#FrameP]] = !{i32 7, !"frame-pointer", i32 1}
2784+
// CHECK: ![[#DBG]] = !{i32 2, !"Debug Info Version", i32 3}
2785+
2786+
// -----
2787+
2788+
// Verifies that the debug info version is not added twice, if it's already present initially.
2789+
2790+
module {
2791+
llvm.module_flags [#llvm.mlir.module_flag<warning, "Debug Info Version", 3>]
2792+
}
2793+
2794+
// CHECK: !llvm.module.flags = !{![[#DBG:]]}
2795+
// CHECK: ![[#DBG]] = !{i32 2, !"Debug Info Version", i32 3}

mlir/test/Target/LLVMIR/nvvmir.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,17 +646,17 @@ llvm.func @kernel_func() attributes {nvvm.kernel, nvvm.maxntid = array<i32: 1, 2
646646
// -----
647647
// CHECK: define ptx_kernel void @kernel_func
648648
// CHECK: !nvvm.annotations =
649-
// CHECK: !1 = !{ptr @kernel_func, !"grid_constant", !2}
650-
// CHECK: !2 = !{i32 1}
649+
// CHECK: !{{.*}} = !{ptr @kernel_func, !"grid_constant", ![[ID:[[:alnum:]]+]]}
650+
// CHECK: ![[ID]] = !{i32 1}
651651
llvm.func @kernel_func(%arg0: !llvm.ptr {llvm.byval = i32, nvvm.grid_constant}) attributes {nvvm.kernel} {
652652
llvm.return
653653
}
654654

655655
// -----
656656
// CHECK: define ptx_kernel void @kernel_func
657657
// CHECK: !nvvm.annotations =
658-
// CHECK: !1 = !{ptr @kernel_func, !"grid_constant", !2}
659-
// CHECK: !2 = !{i32 1, i32 3}
658+
// CHECK: !{{.*}} = !{ptr @kernel_func, !"grid_constant", ![[ID:[[:alnum:]]+]]}
659+
// CHECK: ![[ID]] = !{i32 1, i32 3}
660660
llvm.func @kernel_func(%arg0: !llvm.ptr {llvm.byval = i32, nvvm.grid_constant}, %arg1: f32, %arg2: !llvm.ptr {llvm.byval = f32, nvvm.grid_constant}) attributes {nvvm.kernel} {
661661
llvm.return
662662
}

0 commit comments

Comments
 (0)