Skip to content

[flang][debug] set DW_AT_main_subprogram for fortran main function #111350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

tblah
Copy link
Contributor

@tblah tblah commented Oct 7, 2024

Requested here #111022 (comment)

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Oct 7, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Tom Eccles (tblah)

Changes

Requested here #111022 (comment)


Full diff: https://github.com/llvm/llvm-project/pull/111350.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Transforms/AddDebugInfo.cpp (+5)
  • (modified) flang/test/Transforms/debug-fn-info.fir (+1-1)
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]]>

@DavidSpickett
Copy link
Collaborator

Didn't expect something that quickly :)

What does gdb say for info main before and after the change? (note that this is a new feature, I know at least 115 has it)

@tblah
Copy link
Contributor Author

tblah commented Oct 7, 2024

For

program example
end program

Both before and after this patch using gdb 15.0.50.

(gdb) info main
example

I'm not sure how gdb found the right name before this patch. I can't see the DW_AT_main_subprogram attribute in objdump -g output before this patch and do after.

@DavidSpickett
Copy link
Collaborator

Well I didn't look into GDB's code I'm just going on what experts told me. So there could be a heuristic in there too.

Either way according to https://dwarfstd.org/doc/DWARF4.pdf this is a legitimate use of DW_AT_main_subprogram and it's what LLDB will end up looking for in the future.

Copy link
Collaborator

@DavidSpickett DavidSpickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@abidh
Copy link
Contributor

abidh commented Oct 7, 2024

I'm not sure how gdb found the right name before this patch. I can't see the DW_AT_main_subprogram attribute in objdump -g output before this patch and do after.

We set DW_CC_program as calling convention for main subprogram which could be used by the GDB in absence of DW_AT_main_subprogram.

Copy link
Contributor

@abidh abidh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@tblah tblah merged commit 91d6e77 into llvm:main Oct 7, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants