[flang-commits] [flang] 460408f - Reapply "[MLIR][Flang][DebugInfo] Set debug info format in MLIR->IR translation (#95098)"

Stephen Tozer via flang-commits flang-commits at lists.llvm.org
Tue Jun 11 05:48:19 PDT 2024


Author: Stephen Tozer
Date: 2024-06-11T13:46:09+01:00
New Revision: 460408f78b30720950040e336f7b566aa7203269

URL: https://github.com/llvm/llvm-project/commit/460408f78b30720950040e336f7b566aa7203269
DIFF: https://github.com/llvm/llvm-project/commit/460408f78b30720950040e336f7b566aa7203269.diff

LOG: Reapply "[MLIR][Flang][DebugInfo] Set debug info format in MLIR->IR translation (#95098)"

Reapplies the original patch with some additional conversion layers added
to the MLIR translator, to ensure that we don't write the new debug info
format unless WriteNewDbgInfoFormat is set.

This reverts commit 8c5d9c79b96ed8297b381e00d3a706a432cd6c9d.

Added: 
    

Modified: 
    flang/lib/Frontend/FrontendActions.cpp
    mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index b1b6391f1439c..a4db944e8c0ab 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -50,6 +50,7 @@
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Bitcode/BitcodeWriterPass.h"
 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
+#include "llvm/IR/DebugProgramInstruction.h"
 #include "llvm/IR/LLVMRemarkStreamer.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Verifier.h"
@@ -81,6 +82,8 @@ using namespace Fortran::frontend;
   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
 #include "llvm/Support/Extension.def"
 
+extern llvm::cl::opt<bool> WriteNewDbgInfoFormat;
+
 /// Save the given \c mlirModule to a temporary .mlir file, in a location
 /// decided by the -save-temps flag. No files are produced if the flag is not
 /// specified.
@@ -1271,6 +1274,12 @@ void CodeGenAction::executeAction() {
   runOptimizationPipeline(ci.isOutputStreamNull() ? *os : ci.getOutputStream());
 
   if (action == BackendActionTy::Backend_EmitLL) {
+    // When printing LLVM IR, we should convert the module to the debug info
+    // format that LLVM expects us to print.
+    llvm::ScopedDbgInfoFormatSetter FormatSetter(*llvmModule,
+                                                 WriteNewDbgInfoFormat);
+    if (WriteNewDbgInfoFormat)
+      llvmModule->removeDebugIntrinsicDeclarations();
     llvmModule->print(ci.isOutputStreamNull() ? *os : ci.getOutputStream(),
                       /*AssemblyAnnotationWriter=*/nullptr);
     return;

diff  --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
index 7b86b250c294b..e1a60f195fe89 100644
--- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -64,6 +64,8 @@ using namespace mlir;
 using namespace mlir::LLVM;
 using namespace mlir::LLVM::detail;
 
+extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
+
 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
 
 namespace {
@@ -1789,6 +1791,9 @@ prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
                   StringRef name) {
   m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
   auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
+  // ModuleTranslation can currently only construct modules in the old debug
+  // info format, so set the flag accordingly.
+  llvmModule->setNewDbgInfoFormatFlag(false);
   if (auto dataLayoutAttr =
           m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
     llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue());
@@ -1867,6 +1872,11 @@ mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
   if (failed(translator.convertFunctions()))
     return nullptr;
 
+  // Once we've finished constructing elements in the module, we should convert
+  // it to use the debug info format desired by LLVM.
+  // See https://llvm.org/docs/RemoveDIsDebugInfo.html
+  translator.llvmModule->setIsNewDbgInfoFormat(UseNewDbgInfoFormat);
+
   if (!disableVerification &&
       llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
     return nullptr;


        


More information about the flang-commits mailing list