Skip to content

Commit 1f9212d

Browse files
committed
[flang] Support extention intrinsic ABORT
The semantic checks and runtime have been supported. This supports the lowering of intrinsic ABORT. `gfortran` prints a backtrace before abort, unless `-fno-backtrace` is given. This is good to use. The intrinsic BACKTRACE is not supported yet, so add TODO in the runtime. This extention is needed in SPEC2017 521.wrf_r in llvm#55955. Reviewed By: klausler Differential Revision: https://reviews.llvm.org/D130439
1 parent 9b86792 commit 1f9212d

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

flang/include/flang/Optimizer/Builder/Runtime/Stop.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ namespace fir::runtime {
2727
/// Generate call to EXIT intrinsic runtime routine.
2828
void genExit(fir::FirOpBuilder &, mlir::Location, mlir::Value status);
2929

30+
/// Generate call to ABORT intrinsic runtime routine.
31+
void genAbort(fir::FirOpBuilder &, mlir::Location);
32+
3033
/// Generate call to crash the program with an error message when detecting
3134
/// an invalid situation at runtime.
3235
void genReportFatalUserError(fir::FirOpBuilder &, mlir::Location,

flang/lib/Lower/IntrinsicCall.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ struct IntrinsicLibrary {
441441
getRuntimeCallGenerator(llvm::StringRef name,
442442
mlir::FunctionType soughtFuncType);
443443

444+
void genAbort(llvm::ArrayRef<fir::ExtendedValue>);
445+
444446
/// Lowering for the ABS intrinsic. The ABS intrinsic expects one argument in
445447
/// the llvm::ArrayRef. The ABS intrinsic is lowered into MLIR/FIR operation
446448
/// if the argument is an integer, into llvm intrinsics if the argument is
@@ -685,6 +687,7 @@ static constexpr bool handleDynamicOptional = true;
685687
/// argument must not be lowered by value. In which case, the lowering rules
686688
/// should be provided for all the intrinsic arguments for completeness.
687689
static constexpr IntrinsicHandler handlers[]{
690+
{"abort", &I::genAbort},
688691
{"abs", &I::genAbs},
689692
{"achar", &I::genChar},
690693
{"adjustl",
@@ -2105,6 +2108,12 @@ mlir::Value IntrinsicLibrary::genConversion(mlir::Type resultType,
21052108
return builder.convertWithSemantics(loc, resultType, args[0]);
21062109
}
21072110

2111+
// ABORT
2112+
void IntrinsicLibrary::genAbort(llvm::ArrayRef<fir::ExtendedValue> args) {
2113+
assert(args.size() == 0);
2114+
fir::runtime::genAbort(builder, loc);
2115+
}
2116+
21082117
// ABS
21092118
mlir::Value IntrinsicLibrary::genAbs(mlir::Type resultType,
21102119
llvm::ArrayRef<mlir::Value> args) {

flang/lib/Optimizer/Builder/Runtime/Stop.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ void fir::runtime::genExit(fir::FirOpBuilder &builder, mlir::Location loc,
2222
builder.create<fir::CallOp>(loc, exitFunc, args);
2323
}
2424

25+
void fir::runtime::genAbort(fir::FirOpBuilder &builder, mlir::Location loc) {
26+
mlir::func::FuncOp abortFunc =
27+
fir::runtime::getRuntimeFunc<mkRTKey(Abort)>(loc, builder);
28+
builder.create<fir::CallOp>(loc, abortFunc, llvm::None);
29+
}
30+
2531
void fir::runtime::genReportFatalUserError(fir::FirOpBuilder &builder,
2632
mlir::Location loc,
2733
llvm::StringRef message) {

flang/runtime/stop.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ void RTNAME(PauseStatementText)(const char *code, std::size_t length) {
142142
std::exit(status);
143143
}
144144

145-
[[noreturn]] void RTNAME(Abort)() { std::abort(); }
145+
[[noreturn]] void RTNAME(Abort)() {
146+
// TODO: Add backtrace call, unless with `-fno-backtrace`.
147+
std::abort();
148+
}
146149

147150
[[noreturn]] void RTNAME(ReportFatalUserError)(
148151
const char *message, const char *source, int line) {

flang/test/Lower/Intrinsics/abort.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func.func @_QPabort_test() {
4+
! CHECK: %[[VAL_0:.*]] = fir.call @_FortranAAbort() : () -> none
5+
! CHECK: return
6+
! CHECK: }
7+
8+
subroutine abort_test()
9+
call abort
10+
end subroutine

0 commit comments

Comments
 (0)