Skip to content

[rocprofiler-systems] Add Fortran main detection to rocprof-sys-instrument to avoid instrumenting around C "main" wrapper#1322

Merged
dgaliffiAMD merged 15 commits intoROCm:developfrom
kcossett-amd:users/kcossett-amd/instrument-fortran-main
Oct 21, 2025
Merged

[rocprofiler-systems] Add Fortran main detection to rocprof-sys-instrument to avoid instrumenting around C "main" wrapper#1322
dgaliffiAMD merged 15 commits intoROCm:developfrom
kcossett-amd:users/kcossett-amd/instrument-fortran-main

Conversation

@kcossett-amd
Copy link
Contributor

@kcossett-amd kcossett-amd commented Oct 9, 2025

Motivation

Currently, rocprof-sys-instrument will force the main_func variable to be either main or _main. This variable essentially dictates the primary function to instrument around.

In Fortran programs, there is usually a C-style main which will call the Fortran main. This is usually not a problem as it will consider the Fortran main as a subroutine and instrument it. However, in the case where the Fortran main is non-returning (i.e. Dyninst determine that its FuncReturnStatus == NORETURN), it will not be instrumented. (See SWDEV-558265 for more details).

The solution is to check if any of the Fortran main symbols exist and use them if possible.

Technical Details

Added check for Fortran main function. Uses regex.

  • I will note that Cray compilers are very annoying here. The symbol name given to the Fortran main is the same as what the user uses in their PROGRAM statement. HOWEVER: The cray compiler will generate a "main" symbol with the same start address as the fortran main, fixing any issues. Essentially, in cray fortran, it instruments the fortran main by default.

Remove unused variable _init_arg0.

Test Plan

Tested using:

  • openmp-vv. With amdflang, it will now center around _QQmain (Fortran main symbol for LLVM compilers).
  • intervals_mpi.f90 (this is part of the MPI CTest PR and is the original program where this issue was discovered). With gfortran, binary-rewrite will now center around MAIN__ (Fortran main symbol for gfortran).
  • Custom hello.f90 program that forces the Fortran main to have FuncReturnStatus == NORETURN. With gfortran, it now centers itself around MAIN__.

Test Result

  • For openmp-vv, the omp traces are still present and the CTests pass.
  • For intervals_mpi.f90, the CTest passes and works on every workflow.
    • NOTE: To see this, please take a look at PR $ , it basically contains the same code here.
  • For the custom program, I now see the MPI traces.

Submission Checklist

@kcossett-amd kcossett-amd marked this pull request as ready for review October 9, 2025 14:59
@kcossett-amd kcossett-amd requested review from a team and jrmadsen as code owners October 9, 2025 14:59
Copy link
Contributor

@dgaliffiAMD dgaliffiAMD left a comment

Choose a reason for hiding this comment

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

Thanks, @kcossett-amd. Your change looks good.

A couple additions that I would like to suggest:

@kcossett-amd kcossett-amd requested a review from a team as a code owner October 14, 2025 13:01
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Oct 14, 2025
@kcossett-amd
Copy link
Contributor Author

@dgaliffiAMD The cray compiler issue was resolved (it generates "main" symbol that has same address as the Fortran main. So for PROGRAM hello, "hello_" and "main" have same address). Still, I added a general note to troubleshooting reminding the user that --main-function option exists.

@kcossett-amd kcossett-amd changed the title [rocprofiler-systems] Add check to force rocprof-sys-instrument to profile around the Fortran main [rocprofiler-systems] Add Fortran main detection to rocprof-sys-instrument to avoid instrumenting around C "main" wrapper Oct 14, 2025
Copy link
Contributor

@dgaliffiAMD dgaliffiAMD left a comment

Choose a reason for hiding this comment

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

Looks good to me.
@ROCm/rocm-documentation, please review documentation changes.

kcossett-amd and others added 4 commits October 16, 2025 10:05
Co-authored-by: Pratik Basyal <pratik.basyal@amd.com>
Co-authored-by: Pratik Basyal <pratik.basyal@amd.com>
Co-authored-by: Pratik Basyal <pratik.basyal@amd.com>
Copy link
Contributor

@prbasyal-amd prbasyal-amd left a comment

Choose a reason for hiding this comment

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

LGTM

@dgaliffiAMD dgaliffiAMD merged commit f0a41b6 into ROCm:develop Oct 21, 2025
44 of 48 checks passed
systems-assistant bot pushed a commit to ROCm/rocprofiler-systems that referenced this pull request Oct 21, 2025
 rocprof-sys-instrument to avoid instrumenting around C "main" wrapper (#1322)

* Add check for Fortran main

* Comment change

* MAIN__ -> Fortran main

* Cray Compiler comment change

* Add changelog and troubleshooting comments

* Improve CHANGELOG.md message

* Change CHANGELOG msg to be in 7.2.0

* Apply review change #1

Co-authored-by: Pratik Basyal <pratik.basyal@amd.com>

* Apply review change #2

Co-authored-by: Pratik Basyal <pratik.basyal@amd.com>

* Apply review change #3

Co-authored-by: Pratik Basyal <pratik.basyal@amd.com>
[rocm-systems] ROCm/rocm-systems#1322 (commit f0a41b6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants