Index: flang/include/flang/Lower/PFTBuilder.h =================================================================== --- flang/include/flang/Lower/PFTBuilder.h +++ flang/include/flang/Lower/PFTBuilder.h @@ -141,6 +141,9 @@ using DeclConstructs = std::tuple; +using OMPACCDirectives = + std::tuple; + template static constexpr bool isActionStmt{common::HasMember}; @@ -162,6 +165,9 @@ template static constexpr bool isDeclConstruct{common::HasMember}; +template +static constexpr bool isOMPACCDirective{common::HasMember}; + template static constexpr bool isIntermediateConstructStmt{common::HasMember< A, std::tuple>; }}); } + constexpr bool isOMPACCDirective() const { + return visit(common::visitors{[](auto &r) { + return pft::isOMPACCDirective>; + }}); + } constexpr bool isNopConstructStmt() const { return visit(common::visitors{[](auto &r) { return pft::isNopConstructStmt>; Index: flang/include/flang/Lower/Runtime.h =================================================================== --- flang/include/flang/Lower/Runtime.h +++ flang/include/flang/Lower/Runtime.h @@ -53,7 +53,7 @@ void genEventWaitStatement(AbstractConverter &, const parser::EventWaitStmt &); void genLockStatement(AbstractConverter &, const parser::LockStmt &); void genFailImageStatement(AbstractConverter &); -void genStopStatement(AbstractConverter &, const parser::StopStmt &); +void genStopStatement(AbstractConverter &, const parser::StopStmt &, bool); void genSyncAllStatement(AbstractConverter &, const parser::SyncAllStmt &); void genSyncImagesStatement(AbstractConverter &, const parser::SyncImagesStmt &); Index: flang/lib/Lower/Bridge.cpp =================================================================== --- flang/lib/Lower/Bridge.cpp +++ flang/lib/Lower/Bridge.cpp @@ -3313,7 +3313,10 @@ // call STOP, ERROR STOP in runtime void genFIR(const Fortran::parser::StopStmt &stmt) { - genStopStatement(*this, stmt); + bool hasAttachedUnreachable = + !getEval().parentConstruct || + !getEval().parentConstruct->isOMPACCDirective(); + genStopStatement(*this, stmt, hasAttachedUnreachable); } void genFIR(const Fortran::parser::ReturnStmt &stmt) { Index: flang/lib/Lower/Runtime.cpp =================================================================== --- flang/lib/Lower/Runtime.cpp +++ flang/lib/Lower/Runtime.cpp @@ -42,7 +42,7 @@ void Fortran::lower::genStopStatement( Fortran::lower::AbstractConverter &converter, - const Fortran::parser::StopStmt &stmt) { + const Fortran::parser::StopStmt &stmt, bool hasAttachedUnreachable = true) { fir::FirOpBuilder &builder = converter.getFirOpBuilder(); mlir::Location loc = converter.getCurrentLocation(); Fortran::lower::StatementContext stmtCtx; @@ -106,7 +106,8 @@ } builder.create(loc, callee, operands); - genUnreachable(builder, loc); + if (hasAttachedUnreachable) + genUnreachable(builder, loc); } void Fortran::lower::genFailImageStatement( Index: flang/test/Lower/OpenACC/stop-stmt-in-region.f90 =================================================================== --- /dev/null +++ flang/test/Lower/OpenACC/stop-stmt-in-region.f90 @@ -0,0 +1,41 @@ +! This test checks lowering of stop statement in OpenACC region. + +! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s +! RUN: %flang_fc1 -emit-fir -fopenacc %s -o - | FileCheck %s + +! CHECK-LABEL: func.func @_QPtest_stop_in_region1() { +! CHECK: acc.parallel { +! CHECK: %[[VAL_0:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_1:.*]] = arith.constant false +! CHECK: %[[VAL_2:.*]] = arith.constant false +! CHECK: %[[VAL_3:.*]] = fir.call @_FortranAStopStatement(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) {{.*}} : (i32, i1, i1) -> none +! CHECK: acc.yield +! CHECK: } +! CHECK: return +! CHECK: } + +subroutine test_stop_in_region1() + !$acc parallel + stop 1 + !$acc end parallel +end + +! CHECK-LABEL: func.func @_QPtest_stop_in_region2() { +! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region2Ex"} +! CHECK: acc.parallel { +! CHECK: %[[VAL_1:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_2:.*]] = arith.constant false +! CHECK: %[[VAL_3:.*]] = arith.constant false +! CHECK: %[[VAL_4:.*]] = fir.call @_FortranAStopStatement(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) {{.*}} : (i32, i1, i1) -> none +! CHECK: acc.yield +! CHECK: } +! CHECK: return +! CHECK: } + +subroutine test_stop_in_region2() + integer :: x + !$acc parallel + stop 1 + x = 2 + !$acc end parallel +end Index: flang/test/Lower/OpenMP/stop-stmt-in-region.f90 =================================================================== --- /dev/null +++ flang/test/Lower/OpenMP/stop-stmt-in-region.f90 @@ -0,0 +1,117 @@ +! This test checks lowering of stop statement in OpenMP region. + +! RUN: bbc -fopenmp -emit-fir %s -o - | FileCheck %s +! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s + +! CHECK-LABEL: func.func @_QPtest_stop_in_region1() { +! CHECK: omp.parallel { +! CHECK: %[[VAL_0:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_1:.*]] = arith.constant false +! CHECK: %[[VAL_2:.*]] = arith.constant false +! CHECK: %[[VAL_3:.*]] = fir.call @_FortranAStopStatement(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) {{.*}} : (i32, i1, i1) -> none +! CHECK-NOT: fir.unreachable +! CHECK: omp.terminator +! CHECK: } +! CHECK: return +! CHECK: } + +subroutine test_stop_in_region1() + !$omp parallel + stop 1 + !$omp end parallel +end + +! CHECK-LABEL: func.func @_QPtest_stop_in_region2() { +! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region2Ex"} +! CHECK: omp.parallel { +! CHECK: %[[VAL_1:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_2:.*]] = arith.constant false +! CHECK: %[[VAL_3:.*]] = arith.constant false +! CHECK: %[[VAL_4:.*]] = fir.call @_FortranAStopStatement(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) {{.*}} : (i32, i1, i1) -> none +! CHECK-NOT: fir.unreachable +! CHECK: cf.br ^bb1 +! CHECK: ^bb1: +! CHECK: %[[VAL_5:.*]] = arith.constant 2 : i32 +! CHECK: fir.store %[[VAL_5]] to %[[VAL_0]] : !fir.ref +! CHECK: omp.terminator +! CHECK: } +! CHECK: return +! CHECK: } + +subroutine test_stop_in_region2() + integer :: x + !$omp parallel + stop 1 + x = 2 + !$omp end parallel +end + +! CHECK-LABEL: func.func @_QPtest_stop_in_region3() { +! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region3Ex"} +! CHECK: omp.parallel { +! CHECK: %[[VAL_1:.*]] = arith.constant 3 : i32 +! CHECK: fir.store %[[VAL_1]] to %[[VAL_0]] : !fir.ref +! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref +! CHECK: %[[VAL_3:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_4:.*]] = arith.cmpi sgt, %[[VAL_2]], %[[VAL_3]] : i32 +! CHECK: cf.cond_br %[[VAL_4]], ^bb1, ^bb2 +! CHECK: ^bb1: +! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_0]] : !fir.ref +! CHECK: %[[VAL_6:.*]] = arith.constant false +! CHECK: %[[VAL_7:.*]] = arith.constant false +! CHECK: %[[VAL_8:.*]] = fir.call @_FortranAStopStatement(%[[VAL_5]], %[[VAL_6]], %[[VAL_7]]) {{.*}} : (i32, i1, i1) -> none +! CHECK: fir.unreachable +! CHECK: ^bb2: +! CHECK: omp.terminator +! CHECK: } +! CHECK: return +! CHECK: } + +subroutine test_stop_in_region3() + integer :: x + !$omp parallel + x = 3 + if (x > 1) stop x + !$omp end parallel +end + +! CHECK-LABEL: func.func @_QPtest_stop_in_region4() { +! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {adapt.valuebyref} +! CHECK: %[[VAL_1:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_stop_in_region4Ei"} +! CHECK: %[[VAL_2:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region4Ex"} +! CHECK: %[[VAL_3:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_4:.*]] = arith.constant 10 : i32 +! CHECK: %[[VAL_5:.*]] = arith.constant 1 : i32 +! CHECK: omp.wsloop for (%[[VAL_6:.*]]) : i32 = (%[[VAL_3]]) to (%[[VAL_4]]) inclusive step (%[[VAL_5]]) { +! CHECK: fir.store %[[VAL_6]] to %[[VAL_0]] : !fir.ref +! CHECK: cf.br ^bb1 +! CHECK: ^bb1: +! CHECK: %[[VAL_7:.*]] = arith.constant 3 : i32 +! CHECK: fir.store %[[VAL_7]] to %[[VAL_2]] : !fir.ref +! CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_2]] : !fir.ref +! CHECK: %[[VAL_9:.*]] = arith.constant 1 : i32 +! CHECK: %[[VAL_10:.*]] = arith.cmpi sgt, %[[VAL_8]], %[[VAL_9]] : i32 +! CHECK: cf.cond_br %[[VAL_10]], ^bb2, ^bb3 +! CHECK: ^bb2: +! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_2]] : !fir.ref +! CHECK: %[[VAL_12:.*]] = arith.constant false +! CHECK: %[[VAL_13:.*]] = arith.constant false +! CHECK: %[[VAL_14:.*]] = fir.call @_FortranAStopStatement(%[[VAL_11]], %[[VAL_12]], %[[VAL_13]]) {{.*}} : (i32, i1, i1) -> none +! CHECK: fir.unreachable +! CHECK: ^bb3: +! CHECK: omp.yield +! CHECK: } +! CHECK: cf.br ^bb1 +! CHECK: ^bb1: +! CHECK: return +! CHECK: } + +subroutine test_stop_in_region4() + integer :: x + !$omp do + do i = 1, 10 + x = 3 + if (x > 1) stop x + enddo + !$omp end do +end