Skip to content
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

[flang]Add new intrinsic function backtrace and complete the TODO of abort #117603

Merged
merged 13 commits into from
Nov 28, 2024

Conversation

dty2
Copy link
Contributor

@dty2 dty2 commented Nov 25, 2024

Hey guys, I found that Flang's built-in ABORT function is incomplete when I was using it. Compared with gfortran's ABORT (which can both abort and print out a backtrace), flang's ABORT implementation lacks the function of printing out a backtrace. This feature is essential for debugging and understanding the call stack at the failure point.

To solve this problem, I completed the "// TODO:" of the abort function, and then implemented an additional built-in function BACKTRACE for flang. After a brief reading of the relevant source code, I used backtrace and backtrace_symbols in "execinfo.h" to quickly implement this. But since I used the above two functions directly, my implementation is slightly different from gfortran's implementation (in the output, the function call stack before main is additionally output, and the function line number is missing). In addition, since I used the above two functions, I did not need to add -g to embed debug information into the ELF file, but needed -rdynamic to ensure that the symbols are added to the dynamic symbol table (so that the function name will be printed out).

Here is a comparison of the output between gfortran 's backtrace and my implementation:
gfortran's implemention output:

#0  0x557eb71f4184 in testfun2_
        at /home/hunter/plct/fortran/test.f90:5
#1  0x557eb71f4165 in testfun1_
        at /home/hunter/plct/fortran/test.f90:13
#2  0x557eb71f4192 in test_backtrace
        at /home/hunter/plct/fortran/test.f90:17
#3  0x557eb71f41ce in main
        at /home/hunter/plct/fortran/test.f90:18

my impelmention output:

Backtrace:
#0 ./test(_FortranABacktrace+0x32) [0x574f07efcf92]
#1 ./test(testfun2_+0x14) [0x574f07efc7b4]
#2 ./test(testfun1_+0xd) [0x574f07efc7cd]
#3 ./test(_QQmain+0x9) [0x574f07efc7e9]
#4 ./test(main+0x12) [0x574f07efc802]
#5 /usr/lib/libc.so.6(+0x25e08) [0x76954694fe08]
#6 /usr/lib/libc.so.6(__libc_start_main+0x8c) [0x76954694fecc]
#7 ./test(_start+0x25) [0x574f07efc6c5]

test program is:

function testfun2() result(err)
  implicit none
  integer :: err
  err = 1
  call backtrace
end function testfun2

subroutine testfun1()
  implicit none
  integer :: err
  integer :: testfun2

  err = testfun2()
end subroutine testfun1

program test_backtrace
  call testfun1()
end program test_backtrace

I am well aware of the importance of line numbers, so I am now working on implementing line numbers (by parsing DWARF information) and supporting cross-platform (Windows) support.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels Nov 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2024

@llvm/pr-subscribers-flang-semantics
@llvm/pr-subscribers-flang-runtime

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

Author: 执着 (dty2)

Changes

Hey guys, I found that Flang's built-in ABORT function is incomplete when I was using it. Compared with gfortran's ABORT (which can both abort and print out a backtrace), flang's ABORT implementation lacks the function of printing out a backtrace. This feature is essential for debugging and understanding the call stack at the failure point.

To solve this problem, I completed the "// TODO:" of the abort function, and then implemented an additional built-in function BACKTRACE for flang. After a brief reading of the relevant source code, I used backtrace and backtrace_symbols in "execinfo.h" to quickly implement this. But since I used the above two functions directly, my implementation is slightly different from gfortran's implementation (in the output, the function call stack before main is additionally output, and the function line number is missing). In addition, since I used the above two functions, I did not need to add -g to embed debug information into the ELF file, but needed -rdynamic to ensure that the symbols are added to the dynamic symbol table (so that the function name will be printed out).

Here is a comparison of the output between gfortran 's backtrace and my implementation:
gfortran's implemention output:

#<!-- -->0  0x557eb71f4184 in testfun2_
        at /home/hunter/plct/fortran/test.f90:5
#<!-- -->1  0x557eb71f4165 in testfun1_
        at /home/hunter/plct/fortran/test.f90:13
#<!-- -->2  0x557eb71f4192 in test_backtrace
        at /home/hunter/plct/fortran/test.f90:17
#<!-- -->3  0x557eb71f41ce in main
        at /home/hunter/plct/fortran/test.f90:18

my impelmention output:

Backtrace:
#<!-- -->0 ./test(_FortranABacktrace+0x32) [0x574f07efcf92]
#<!-- -->1 ./test(testfun2_+0x14) [0x574f07efc7b4]
#<!-- -->2 ./test(testfun1_+0xd) [0x574f07efc7cd]
#<!-- -->3 ./test(_QQmain+0x9) [0x574f07efc7e9]
#<!-- -->4 ./test(main+0x12) [0x574f07efc802]
#<!-- -->5 /usr/lib/libc.so.6(+0x25e08) [0x76954694fe08]
#<!-- -->6 /usr/lib/libc.so.6(__libc_start_main+0x8c) [0x76954694fecc]
#<!-- -->7 ./test(_start+0x25) [0x574f07efc6c5]

test program is:

function testfun2() result(err)
  implicit none
  integer :: err
  err = 1
  call backtrace
end function testfun2

subroutine testfun1()
  implicit none
  integer :: err
  integer :: testfun2

  err = testfun2()
end subroutine testfun1

program test_backtrace
  call testfun1()
end program test_backtrace

I am well aware of the importance of line numbers, so I am now working on implementing line numbers (by parsing DWARF information) and supporting cross-platform (Windows) support.


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

8 Files Affected:

  • (modified) flang/include/flang/Optimizer/Builder/IntrinsicCall.h (+1)
  • (modified) flang/include/flang/Optimizer/Builder/Runtime/Stop.h (+3)
  • (modified) flang/include/flang/Runtime/stop.h (+1)
  • (modified) flang/lib/Evaluate/intrinsics.cpp (+1)
  • (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+7)
  • (modified) flang/lib/Optimizer/Builder/Runtime/Stop.cpp (+7)
  • (modified) flang/runtime/stop.cpp (+20-1)
  • (added) flang/test/Lower/Intrinsics/backtrace.f90 (+10)
diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
index e83d1a42e34133..7f4b9ebf1d1c21 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -196,6 +196,7 @@ struct IntrinsicLibrary {
   fir::ExtendedValue genAssociated(mlir::Type,
                                    llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genAtand(mlir::Type, llvm::ArrayRef<mlir::Value>);
+  void genBacktrace(llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genBesselJn(mlir::Type,
                                  llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genBesselYn(mlir::Type,
diff --git a/flang/include/flang/Optimizer/Builder/Runtime/Stop.h b/flang/include/flang/Optimizer/Builder/Runtime/Stop.h
index 6f764badf6f3a8..be73cffff021e3 100644
--- a/flang/include/flang/Optimizer/Builder/Runtime/Stop.h
+++ b/flang/include/flang/Optimizer/Builder/Runtime/Stop.h
@@ -30,6 +30,9 @@ void genExit(fir::FirOpBuilder &, mlir::Location, mlir::Value status);
 /// Generate call to ABORT intrinsic runtime routine.
 void genAbort(fir::FirOpBuilder &, mlir::Location);
 
+/// Generate call to BACKTRACE intrinsic runtime routine.
+void genBacktrace(fir::FirOpBuilder &builder, mlir::Location loc);
+
 /// Generate call to crash the program with an error message when detecting
 /// an invalid situation at runtime.
 void genReportFatalUserError(fir::FirOpBuilder &, mlir::Location,
diff --git a/flang/include/flang/Runtime/stop.h b/flang/include/flang/Runtime/stop.h
index f7c4ffe7403e8e..d442f72bfe1fa4 100644
--- a/flang/include/flang/Runtime/stop.h
+++ b/flang/include/flang/Runtime/stop.h
@@ -29,6 +29,7 @@ NORETURN void RTNAME(ProgramEndStatement)(NO_ARGUMENTS);
 // Extensions
 NORETURN void RTNAME(Exit)(int status DEFAULT_VALUE(EXIT_SUCCESS));
 NORETURN void RTNAME(Abort)(NO_ARGUMENTS);
+void RTNAME(Backtrace)(NO_ARGUMENTS);
 
 // Crash with an error message when the program dynamically violates a Fortran
 // constraint.
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index 1e27c0ae4216c5..599a7d0124b800 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -1333,6 +1333,7 @@ static const IntrinsicInterface intrinsicSubroutine[]{
             {"stat", AnyInt, Rank::scalar, Optionality::optional,
                 common::Intent::Out}},
         {}, Rank::elemental, IntrinsicClass::atomicSubroutine},
+    {"backtrace", {}, {}, Rank::elemental, IntrinsicClass::pureSubroutine},
     {"co_broadcast",
         {{"a", AnyData, Rank::anyOrAssumedRank, Optionality::required,
              common::Intent::InOut},
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index a2b327f45c6939..c748c6583a5ce9 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -150,6 +150,7 @@ static constexpr IntrinsicHandler handlers[]{
     {"atan2pi", &I::genAtanpi},
     {"atand", &I::genAtand},
     {"atanpi", &I::genAtanpi},
+    {"backtrace", &I::genBacktrace},
     {"bessel_jn",
      &I::genBesselJn,
      {{{"n1", asValue}, {"n2", asValue}, {"x", asValue}}},
@@ -2681,6 +2682,12 @@ IntrinsicLibrary::genBesselJn(mlir::Type resultType,
   }
 }
 
+// Backtrace
+void IntrinsicLibrary::genBacktrace(llvm::ArrayRef<fir::ExtendedValue> args) {
+  assert(args.size() == 0);
+  fir::runtime::genBacktrace(builder, loc);
+}
+
 // BESSEL_YN
 fir::ExtendedValue
 IntrinsicLibrary::genBesselYn(mlir::Type resultType,
diff --git a/flang/lib/Optimizer/Builder/Runtime/Stop.cpp b/flang/lib/Optimizer/Builder/Runtime/Stop.cpp
index 411181cc6dd1ca..541e5f3b5d11a8 100644
--- a/flang/lib/Optimizer/Builder/Runtime/Stop.cpp
+++ b/flang/lib/Optimizer/Builder/Runtime/Stop.cpp
@@ -28,6 +28,13 @@ void fir::runtime::genAbort(fir::FirOpBuilder &builder, mlir::Location loc) {
   builder.create<fir::CallOp>(loc, abortFunc, std::nullopt);
 }
 
+void fir::runtime::genBacktrace(fir::FirOpBuilder &builder,
+                                mlir::Location loc) {
+  mlir::func::FuncOp backtraceFunc =
+      fir::runtime::getRuntimeFunc<mkRTKey(Backtrace)>(loc, builder);
+  builder.create<fir::CallOp>(loc, backtraceFunc, std::nullopt);
+}
+
 void fir::runtime::genReportFatalUserError(fir::FirOpBuilder &builder,
                                            mlir::Location loc,
                                            llvm::StringRef message) {
diff --git a/flang/runtime/stop.cpp b/flang/runtime/stop.cpp
index cfb36b40840200..57209dc37befa7 100644
--- a/flang/runtime/stop.cpp
+++ b/flang/runtime/stop.cpp
@@ -15,6 +15,7 @@
 #include <cfenv>
 #include <cstdio>
 #include <cstdlib>
+#include <execinfo.h>
 
 extern "C" {
 
@@ -152,11 +153,29 @@ void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
   std::exit(status);
 }
 
+static void PrintBacktrace() {
+  // TODO: Need to parse DWARF information to print function line numbers
+  const int MAX_CALL_STACK = 999;
+  void *buffer[MAX_CALL_STACK];
+  int nptrs = backtrace(buffer, MAX_CALL_STACK);
+  char **symbols = backtrace_symbols(buffer, nptrs);
+  if (symbols == nullptr) {
+    Fortran::runtime::Terminator{}.Crash("no symbols");
+    std::exit(EXIT_FAILURE);
+  }
+  for (int i = 0; i < nptrs; i++) {
+    Fortran::runtime::Terminator{}.PrintCrashArgs("#%d %s\n", i, symbols[i]);
+  }
+  free(symbols);
+}
+
 [[noreturn]] void RTNAME(Abort)() {
-  // TODO: Add backtrace call, unless with `-fno-backtrace`.
+  PrintBacktrace();
   std::abort();
 }
 
+void RTNAME(Backtrace)() { PrintBacktrace(); }
+
 [[noreturn]] void RTNAME(ReportFatalUserError)(
     const char *message, const char *source, int line) {
   Fortran::runtime::Terminator{source, line}.Crash(message);
diff --git a/flang/test/Lower/Intrinsics/backtrace.f90 b/flang/test/Lower/Intrinsics/backtrace.f90
new file mode 100644
index 00000000000000..9d5e7b4965baff
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/backtrace.f90
@@ -0,0 +1,10 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+! CHECK-LABEL: func.func @_QPbacktrace_test() {
+! CHECK:         %[[VAL_0:.*]] = fir.call @_FortranABacktrace() {{.*}}: () -> none
+! CHECK:         return
+! CHECK:       }
+
+subroutine backtrace_test()
+  call backtrace
+end subroutine

@dty2 dty2 closed this Nov 25, 2024
@dty2 dty2 deleted the dty2 branch November 25, 2024 18:40
Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thank you for implementing this.

flang/runtime/stop.cpp Outdated Show resolved Hide resolved
flang/runtime/stop.cpp Outdated Show resolved Hide resolved
flang/runtime/stop.cpp Outdated Show resolved Hide resolved
flang/runtime/stop.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@kparzysz kparzysz left a comment

Choose a reason for hiding this comment

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

This is all good, could you restore the branch and reopen the PR?

flang/runtime/stop.cpp Show resolved Hide resolved
@dty2 dty2 restored the dty2 branch November 25, 2024 19:09
@dty2 dty2 reopened this Nov 25, 2024
@dty2
Copy link
Contributor Author

dty2 commented Nov 26, 2024

@kparzysz Could you please review the latest changes? I've addressed all the feedback from the previous review. Thank you! And I don't have merge permission. I hope you can merge the code if there is no problem after checking. Thank you very much.

flang/runtime/stop.cpp Outdated Show resolved Hide resolved
flang/runtime/stop.cpp Outdated Show resolved Hide resolved
flang/runtime/stop.cpp Outdated Show resolved Hide resolved
flang/runtime/stop.cpp Outdated Show resolved Hide resolved
flang/runtime/stop.cpp Show resolved Hide resolved
@dty2
Copy link
Contributor Author

dty2 commented Nov 26, 2024

@kparzysz Thank you for your patience in guiding my code. Please review the latest changes again to see if they meet the merge criteria. I will try my best to implement the -fno-backtrace option. If the implementation is successful, I will submit another PR. And I will continue to do my part for the llvm and flang communities.

@kparzysz
Copy link
Contributor

Looks good to me.

@dty2
Copy link
Contributor Author

dty2 commented Nov 26, 2024

Oh, thanks, Can you merge this code? I don't have merge permissions

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

LGTM. If nobody objects I can merge this tomorrow.

Thanks for all of the quick responses to feedback.

flang/runtime/stop.cpp Show resolved Hide resolved
flang/runtime/stop.cpp Outdated Show resolved Hide resolved
@dty2 dty2 requested a review from kparzysz November 27, 2024 06:59
Copy link

github-actions bot commented Nov 27, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@kparzysz kparzysz left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

It looks like you need to run clang-format. Please make sure that the pre-merge checks pass before merging.

Other than that, LGTM. Thanks for seeing this through.

@tblah tblah merged commit 159e601 into llvm:main Nov 28, 2024
8 checks passed
Copy link

@dty2 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 28, 2024

LLVM Buildbot has detected a new failure on builder flang-runtime-cuda-gcc running on as-builder-7 while building flang at step 5 "build-FortranRuntime".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/152/builds/710

Here is the relevant piece of the build log for the reference
Step 5 (build-FortranRuntime) failure: build (failure)
...
0.090 [1/10/55] Building CUDA object CMakeFiles/FortranRuntime.dir/namelist.cpp.o
0.092 [1/9/56] Building CUDA object CMakeFiles/FortranRuntime.dir/unit.cpp.o
0.094 [1/8/57] Building CUDA object CMakeFiles/FortranRuntime.dir/io-stmt.cpp.o
0.102 [1/7/58] Building CUDA object CMakeFiles/FortranRuntime.dir/io-api.cpp.o
0.128 [1/6/59] Building CUDA object CMakeFiles/FortranRuntime.dir/dot-product.cpp.o
0.151 [1/5/60] Building CUDA object CMakeFiles/FortranRuntime.dir/extrema.cpp.o
0.218 [1/4/61] Building CUDA object CMakeFiles/FortranRuntime.dir/matmul-transpose.cpp.o
0.264 [1/3/62] Building CUDA object CMakeFiles/FortranRuntime.dir/matmul.cpp.o
0.341 [1/2/63] Building CUDA object CMakeFiles/FortranRuntime.dir/findloc.cpp.o
0.919 [1/1/64] Building CXX object CMakeFiles/FortranRuntime.dir/stop.cpp.o
FAILED: CMakeFiles/FortranRuntime.dir/stop.cpp.o 
ccache /usr/bin/g++ -DFLANG_LITTLE_ENDIAN=1 -DGTEST_HAS_RTTI=0 -DRT_USE_LIBCUDACXX=1 -D_DEBUG -D_GLIBCXX_ASSERTIONS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/llvm-project/flang/runtime/../include -I/home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/build -I/home/buildbot/worker/third-party/nv/cccl/libcudacxx/include -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -Wimplicit-fallthrough -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -fno-lto -O3 -DNDEBUG   -U_GLIBCXX_ASSERTIONS -U_LIBCPP_ENABLE_ASSERTIONS -UNDEBUG -std=c++17  -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -MD -MT CMakeFiles/FortranRuntime.dir/stop.cpp.o -MF CMakeFiles/FortranRuntime.dir/stop.cpp.o.d -o CMakeFiles/FortranRuntime.dir/stop.cpp.o -c /home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/llvm-project/flang/runtime/stop.cpp
/home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/llvm-project/flang/runtime/stop.cpp:19:10: fatal error: llvm/Config/config.h: No such file or directory
   19 | #include "llvm/Config/config.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.

tblah added a commit that referenced this pull request Nov 28, 2024
…TODO of abort" (#117990)

Reverts #117603 due to failed buildbot
https://lab.llvm.org/buildbot/#/builders/152/builds/710

The important bit of the log was
```
FAILED: CMakeFiles/FortranRuntime.dir/stop.cpp.o 
ccache /usr/bin/g++ -DFLANG_LITTLE_ENDIAN=1 -DGTEST_HAS_RTTI=0 -DRT_USE_LIBCUDACXX=1 -D_DEBUG -D_GLIBCXX_ASSERTIONS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/llvm-project/flang/runtime/../include -I/home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/build -I/home/buildbot/worker/third-party/nv/cccl/libcudacxx/include -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -Wimplicit-fallthrough -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -fno-lto -O3 -DNDEBUG   -U_GLIBCXX_ASSERTIONS -U_LIBCPP_ENABLE_ASSERTIONS -UNDEBUG -std=c++17  -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -MD -MT CMakeFiles/FortranRuntime.dir/stop.cpp.o -MF CMakeFiles/FortranRuntime.dir/stop.cpp.o.d -o CMakeFiles/FortranRuntime.dir/stop.cpp.o -c /home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/llvm-project/flang/runtime/stop.cpp
/home/buildbot/worker/as-builder-7/ramdisk/flang-runtime-cuda-gcc/llvm-project/flang/runtime/stop.cpp:19:10: fatal error: llvm/Config/config.h: No such file or directory
   19 | #include "llvm/Config/config.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
```

CC @dty2
@tblah
Copy link
Contributor

tblah commented Nov 28, 2024

I reverted due to the failed buildbot. The buildbot failed because the LLVM configuration header was missing.

The buildbot had a separate step for building the runtime so I wonder if it built only that and nothing in llvm/. So the problem could be a missing cmake dependency from the runtime to whatever in llvm/ generates that header.

It looks like there is already a $BUILD_DIR/flang/runtime/config.h generated from $SOURCE_DIR/flang/runtime/config.h.cmake. I think you will need to add the right line to that cmake file so that this variable is set, then include the flang runtime version of config.h. You can find the line you need in llvm/include/llvm/Config/config.h.cmake.

Thanks for all of your work so far :) don't hesitate to reach out if you have any questions.

@@ -1336,6 +1336,7 @@ static const IntrinsicInterface intrinsicSubroutine[]{
{"stat", AnyInt, Rank::scalar, Optionality::optional,
common::Intent::Out}},
{}, Rank::elemental, IntrinsicClass::atomicSubroutine},
{"backtrace", {}, {}, Rank::elemental, IntrinsicClass::pureSubroutine},
Copy link
Contributor

Choose a reason for hiding this comment

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

I am late in the review, but since this has been reverted, I do not think "elemental" is correct here, and I would also debate the pure aspect since this is doing IO on the standard output which is forbidden for pure subroutine.

In fact, I am not quite sure that the front-end needs to know about backtrace since there are type resolution or argument requiring explicit interface.

It may just be simpler to deal with it like a user procedure for which the runtime provides an implementation (if the user has not). See FDATE implementation for instance.

Please also document the extension support in docs/Intrinsics.md.

Thanks for adding this, it is a great feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! I understand what you said. I think you are right. But in this case, it seems to me that the built-in functions such as Abort and Exit need to be rewritten, because they can be regarded as user processes like backtrace. (If I understand correctly) I plan to complete backtrace in the original way first, and rewrite the built-in functions such as Abort and exit in the next new PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I temporarily changed pure to impure to match element (I saw the original Abort implementation was the same). Since the previous PR was problematic and had been rejected, I submitted a new PR after making modifications. You can follow this PR to follow the latest changes.#118179

Copy link
Contributor

@jeanPerier jeanPerier Dec 2, 2024

Choose a reason for hiding this comment

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

I do not think that Exit can be rewritten without front-end support because it has an OPTIONAL argument, so it requires an explicit interface. Maybe Abort could be handled without front-end support, however, I think its NORETURN aspect is something that may be valuable to lowering (not leveraged yet), so it may be worth keeping it like this. @klausler, do you agree Abort and Exit need or benefit from front-end support, while Backtrace does not?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:runtime flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants