diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td @@ -35,6 +35,9 @@ /// Uses `reportError` to report errors. static LogicalResult verifyDataLayoutString( StringRef descr, llvm::function_ref reportError); + + /// Name of the target triple attribute. + static StringRef getTargetTripleAttrName() { return "llvm.target_triple"; } }]; } diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp --- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp @@ -48,6 +48,17 @@ if (!llvmModule.getModuleFlag(debugVersionKey)) llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey, llvm::DEBUG_METADATA_VERSION); + + if (auto targetTripleAttr = + module->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) { + auto targetTriple = + llvm::Triple(targetTripleAttr.cast().getValue()); + if (targetTriple.isKnownWindowsMSVCEnvironment()) { + // Dwarf debugging files will be generated by default, unless "CodeView" + // is set explicitly. Windows/MSVC should use CodeView instead. + llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1); + } + } } /// Finalize the translation of debug information. diff --git a/mlir/test/Target/llvmir.mlir b/mlir/test/Target/llvmir.mlir --- a/mlir/test/Target/llvmir.mlir +++ b/mlir/test/Target/llvmir.mlir @@ -1311,3 +1311,17 @@ llvm.func @module_big_endian() } +// ----- + +// CHECK: "CodeView", i32 1 +module attributes {llvm.target_triple = "x86_64-pc-windows-msvc"} {} + +// ----- + +// CHECK-NOT: "CodeView", i32 1 +module attributes {llvm.target_triple = "aarch64-linux-android"} {} + +// ----- + +// CHECK-NOT: "CodeView", i32 1 +module attributes {} {}