Skip to content

Commit ca920bb

Browse files
authored
[MLIR][Flang][DebugInfo] Set debug info format in MLIR->IR translation (#95098)
MLIR's LLVM dialect does not internally support debug records, only converting to/from debug intrinsics. To smooth the transition from intrinsics to records, there is a step prior to IR->MLIR translation that switches the IR module to intrinsic-form; this patch adds the equivalent conversion to record-form at MLIR->IR translation, and also modifies the flang front end to use the WriteNewDbgInfoFormat flag when it is emitting LLVM IR.
1 parent a141a28 commit ca920bb

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/Analysis/TargetTransformInfo.h"
5151
#include "llvm/Bitcode/BitcodeWriterPass.h"
5252
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
53+
#include "llvm/IR/DebugProgramInstruction.h"
5354
#include "llvm/IR/LLVMRemarkStreamer.h"
5455
#include "llvm/IR/LegacyPassManager.h"
5556
#include "llvm/IR/Verifier.h"
@@ -81,6 +82,8 @@ using namespace Fortran::frontend;
8182
llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
8283
#include "llvm/Support/Extension.def"
8384

85+
extern llvm::cl::opt<bool> WriteNewDbgInfoFormat;
86+
8487
/// Save the given \c mlirModule to a temporary .mlir file, in a location
8588
/// decided by the -save-temps flag. No files are produced if the flag is not
8689
/// specified.
@@ -1271,6 +1274,12 @@ void CodeGenAction::executeAction() {
12711274
runOptimizationPipeline(ci.isOutputStreamNull() ? *os : ci.getOutputStream());
12721275

12731276
if (action == BackendActionTy::Backend_EmitLL) {
1277+
// When printing LLVM IR, we should convert the module to the debug info
1278+
// format that LLVM expects us to print.
1279+
llvm::ScopedDbgInfoFormatSetter FormatSetter(*llvmModule,
1280+
WriteNewDbgInfoFormat);
1281+
if (WriteNewDbgInfoFormat)
1282+
llvmModule->removeDebugIntrinsicDeclarations();
12741283
llvmModule->print(ci.isOutputStreamNull() ? *os : ci.getOutputStream(),
12751284
/*AssemblyAnnotationWriter=*/nullptr);
12761285
return;

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ using namespace mlir;
6464
using namespace mlir::LLVM;
6565
using namespace mlir::LLVM::detail;
6666

67+
extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
68+
6769
#include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
6870

6971
namespace {
@@ -1789,6 +1791,9 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
17891791
StringRef name) {
17901792
m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
17911793
auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
1794+
// ModuleTranslation can currently only construct modules in the old debug
1795+
// info format, so set the flag accordingly.
1796+
llvmModule->setNewDbgInfoFormatFlag(false);
17921797
if (auto dataLayoutAttr =
17931798
m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
17941799
llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue());
@@ -1867,6 +1872,11 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
18671872
if (failed(translator.convertFunctions()))
18681873
return nullptr;
18691874

1875+
// Once we've finished constructing elements in the module, we should convert
1876+
// it to use the debug info format desired by LLVM.
1877+
// See https://llvm.org/docs/RemoveDIsDebugInfo.html
1878+
translator.llvmModule->setIsNewDbgInfoFormat(UseNewDbgInfoFormat);
1879+
18701880
if (!disableVerification &&
18711881
llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
18721882
return nullptr;

0 commit comments

Comments
 (0)