[flang-commits] [flang] [flang][debug] set DW_AT_main_subprogram for fortran main function (PR #111350)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Mon Oct 7 02:21:59 PDT 2024


https://github.com/tblah created https://github.com/llvm/llvm-project/pull/111350

Requested here https://github.com/llvm/llvm-project/pull/111022#issuecomment-2396287781

>From 75d5a758e9db45167e7baf4daa1bbb6c7a684fa4 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Mon, 7 Oct 2024 09:17:24 +0000
Subject: [PATCH] [flang][debug] set DW_AT_main_subprogram for fortran main
 function

Requested here https://github.com/llvm/llvm-project/pull/111022#issuecomment-2396287781
---
 flang/lib/Optimizer/Transforms/AddDebugInfo.cpp | 5 +++++
 flang/test/Transforms/debug-fn-info.fir         | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
index 927e275b360898..bf4de78dbdb213 100644
--- a/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
+++ b/flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
@@ -267,7 +267,9 @@ void AddDebugInfoPass::handleFuncOp(mlir::func::FuncOp funcOp,
   funcName = mlir::StringAttr::get(context, result.second.name);
 
   // try to use a better function name than _QQmain for the program statement
+  bool isMain = false;
   if (funcName == fir::NameUniquer::doProgramEntry()) {
+    isMain = true;
     mlir::StringAttr bindcName =
         funcOp->getAttrOfType<mlir::StringAttr>(fir::getSymbolAttrName());
     if (bindcName)
@@ -302,6 +304,9 @@ void AddDebugInfoPass::handleFuncOp(mlir::func::FuncOp funcOp,
       mlir::LLVM::DISubprogramFlags{};
   if (isOptimized)
     subprogramFlags = mlir::LLVM::DISubprogramFlags::Optimized;
+  if (isMain)
+    subprogramFlags =
+        subprogramFlags | mlir::LLVM::DISubprogramFlags::MainSubprogram;
   if (!funcOp.isExternal()) {
     // Place holder and final function have to have different IDs, otherwise
     // translation code will reject one of them.
diff --git a/flang/test/Transforms/debug-fn-info.fir b/flang/test/Transforms/debug-fn-info.fir
index eaf88152a21bc6..85cfd13643ec3f 100644
--- a/flang/test/Transforms/debug-fn-info.fir
+++ b/flang/test/Transforms/debug-fn-info.fir
@@ -76,7 +76,7 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
 // CHECK: #[[TY3:.*]] = #llvm.di_subroutine_type<callingConvention = DW_CC_normal, types = #di_null_type, #[[INT4]]>
 
 // Line numbers should match the number in corresponding loc entry.
-// CHECK: #llvm.di_subprogram<{{.*}}name = "mn", linkageName = "_QQmain", file = {{.*}}, line = 15, scopeLine = 15, subprogramFlags = Definition, type = #[[TY0]]>
+// CHECK: #llvm.di_subprogram<{{.*}}name = "mn", linkageName = "_QQmain", file = {{.*}}, line = 15, scopeLine = 15, subprogramFlags = "Definition|MainSubprogram", type = #[[TY0]]>
 // CHECK: #llvm.di_subprogram<{{.*}}name = "fn1", linkageName = "_QFPfn1", file = {{.*}}, line = 26, scopeLine = 26, subprogramFlags = Definition, type = #[[TY1]]>
 // CHECK: #llvm.di_subprogram<{{.*}}name = "fn2", linkageName = "_QFPfn2", file = {{.*}}, line = 43, scopeLine = 43, subprogramFlags = Definition, type = #[[TY2]]>
 // CHECK: #llvm.di_subprogram<{{.*}}name = "fn3", linkageName = "_QFPfn3", file = {{.*}}, line = 53, scopeLine = 53, subprogramFlags = Definition, type = #[[TY3]]>



More information about the flang-commits mailing list