xref: /llvm-project/flang/test/Lower/OpenMP/stop-stmt-in-region.f90 (revision 937cbce14c9aa956342a9c818c26a8a557802843)
1! This test checks lowering of stop statement in OpenMP region.
2
3! RUN: %flang_fc1 -flang-experimental-hlfir -emit-hlfir -fopenmp %s -o - | FileCheck %s
4! RUN: bbc -hlfir -emit-hlfir -fopenmp %s -o - | FileCheck %s
5
6
7! CHECK-LABEL: func.func @_QPtest_stop_in_region1() {
8! CHECK:         omp.parallel   {
9! CHECK:           %[[VAL_0:.*]] = arith.constant 1 : i32
10! CHECK:           %[[VAL_1:.*]] = arith.constant false
11! CHECK:           %[[VAL_2:.*]] = arith.constant false
12! CHECK:           fir.call @_FortranAStopStatement(%[[VAL_0]], %[[VAL_1]], %[[VAL_2]]) {{.*}} : (i32, i1, i1) -> ()
13! CHECK-NOT:       fir.unreachable
14! CHECK:           omp.terminator
15! CHECK:         }
16! CHECK:         return
17! CHECK:       }
18
19subroutine test_stop_in_region1()
20  !$omp parallel
21    stop 1
22  !$omp end parallel
23end
24
25! CHECK-LABEL: func.func @_QPtest_stop_in_region2() {
26! CHECK:         %{{.*}} = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region2Ex"}
27! CHECK:         omp.parallel   {
28! CHECK:           %[[VAL_1:.*]] = arith.constant 1 : i32
29! CHECK:           %[[VAL_2:.*]] = arith.constant false
30! CHECK:           %[[VAL_3:.*]] = arith.constant false
31! CHECK:           fir.call @_FortranAStopStatement(%[[VAL_1]], %[[VAL_2]], %[[VAL_3]]) {{.*}} : (i32, i1, i1) -> ()
32! CHECK:           omp.terminator
33! CHECK:         }
34! CHECK:         return
35! CHECK:       }
36
37subroutine test_stop_in_region2()
38  integer :: x
39  !$omp parallel
40    stop 1
41    x = 2
42  !$omp end parallel
43end
44
45! CHECK-LABEL: func.func @_QPtest_stop_in_region3() {
46! CHECK:         %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region3Ex"}
47! CHECK:         %[[VAL_0_DECL:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFtest_stop_in_region3Ex"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
48! CHECK:         omp.parallel   {
49! CHECK:           %[[VAL_1:.*]] = arith.constant 3 : i32
50! CHECK:           hlfir.assign %[[VAL_1]] to %[[VAL_0_DECL]]#0 : i32, !fir.ref<i32>
51! CHECK:           %[[VAL_2:.*]] = fir.load %[[VAL_0_DECL]]#0 : !fir.ref<i32>
52! CHECK:           %[[VAL_3:.*]] = arith.constant 1 : i32
53! CHECK:           %[[VAL_4:.*]] = arith.cmpi sgt, %[[VAL_2]], %[[VAL_3]] : i32
54! CHECK:           cf.cond_br %[[VAL_4]], ^bb1, ^bb2
55! CHECK:         ^bb1:
56! CHECK:           %[[VAL_5:.*]] = fir.load %[[VAL_0_DECL]]#0 : !fir.ref<i32>
57! CHECK:           %[[VAL_6:.*]] = arith.constant false
58! CHECK:           %[[VAL_7:.*]] = arith.constant false
59! CHECK:           fir.call @_FortranAStopStatement(%[[VAL_5]], %[[VAL_6]], %[[VAL_7]]) {{.*}} : (i32, i1, i1) -> ()
60! CHECK:           omp.terminator
61! CHECK:         ^bb2:
62! CHECK:           omp.terminator
63! CHECK:         }
64! CHECK:         return
65! CHECK:       }
66
67subroutine test_stop_in_region3()
68  integer :: x
69  !$omp parallel
70    x = 3
71    if (x > 1) stop x
72  !$omp end parallel
73end
74
75! CHECK-LABEL: func.func @_QPtest_stop_in_region4() {
76
77! CHECK:         %[[VAL_1:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_stop_in_region4Ei"}
78! CHECK:         %[[VAL_1_DECL:.*]]:2 = hlfir.declare %[[VAL_1]] {uniq_name = "_QFtest_stop_in_region4Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
79
80! CHECK:         %[[VAL_2:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtest_stop_in_region4Ex"}
81! CHECK:         %[[VAL_2_DECL:.*]]:2 = hlfir.declare %[[VAL_2]] {uniq_name = "_QFtest_stop_in_region4Ex"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
82
83! CHECK:         %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "i", pinned, {{.*}}}
84! CHECK:         %[[VAL_0_DECL:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QFtest_stop_in_region4Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
85
86! CHECK:         %[[VAL_3:.*]] = arith.constant 1 : i32
87! CHECK:         %[[VAL_4:.*]] = arith.constant 10 : i32
88! CHECK:         %[[VAL_5:.*]] = arith.constant 1 : i32
89! CHECK:         omp.wsloop {
90! CHECK-NEXT:      omp.loop_nest (%[[VAL_6:.*]]) : i32 = (%[[VAL_3]]) to (%[[VAL_4]]) inclusive step (%[[VAL_5]]) {
91! CHECK:             fir.store %[[VAL_6]] to %[[VAL_0_DECL]]#1 : !fir.ref<i32>
92! CHECK:             cf.br ^bb1
93! CHECK:           ^bb1:
94! CHECK:             %[[VAL_7:.*]] = arith.constant 3 : i32
95! CHECK:             hlfir.assign %[[VAL_7]] to %[[VAL_2_DECL]]#0 : i32, !fir.ref<i32>
96! CHECK:             %[[VAL_8:.*]] = fir.load %[[VAL_2_DECL]]#0 : !fir.ref<i32>
97! CHECK:             %[[VAL_9:.*]] = arith.constant 1 : i32
98! CHECK:             %[[VAL_10:.*]] = arith.cmpi sgt, %[[VAL_8]], %[[VAL_9]] : i32
99! CHECK:             cf.cond_br %[[VAL_10]], ^bb2, ^bb3
100! CHECK:           ^bb2:
101! CHECK:             %[[VAL_11:.*]] = fir.load %[[VAL_2_DECL]]#0 : !fir.ref<i32>
102! CHECK:             %[[VAL_12:.*]] = arith.constant false
103! CHECK:             %[[VAL_13:.*]] = arith.constant false
104! CHECK:             fir.call @_FortranAStopStatement(%[[VAL_11]], %[[VAL_12]], %[[VAL_13]]) {{.*}} : (i32, i1, i1) -> ()
105! CHECK:             omp.yield
106! CHECK:           ^bb3:
107! CHECK:             omp.yield
108! CHECK:           }
109! CHECK:         }
110! CHECK:         cf.br ^bb1
111! CHECK:       ^bb1:
112! CHECK:         return
113! CHECK:       }
114
115subroutine test_stop_in_region4()
116  integer :: x
117  !$omp do
118  do i = 1, 10
119    x = 3
120    if (x > 1) stop x
121  enddo
122  !$omp end do
123end
124
125
126!CHECK-LABEL: func.func @_QPtest_stop_in_region5
127!CHECK:   omp.parallel   {
128!CHECK:     {{.*}} fir.call @_FortranAStopStatement({{.*}}, {{.*}}, {{.*}}) fastmath<contract> : (i32, i1, i1) -> ()
129!CHECK:     omp.terminator
130!CHECK:   }
131!CHECK:   return
132
133subroutine test_stop_in_region5()
134  !$omp parallel
135  block
136    stop 1
137  end block
138  !$omp end parallel
139end
140
141!CHECK-LABEL: func.func @_QPtest_stop_in_region6
142!CHECK:  omp.parallel   {
143!CHECK:    cf.cond_br %{{.*}}, ^[[BB1:.*]], ^[[BB2:.*]]
144!CHECK:  ^[[BB1]]:
145!CHECK:    {{.*}}fir.call @_FortranAStopStatement({{.*}}, {{.*}}, {{.*}}) fastmath<contract> : (i32, i1, i1) -> ()
146!CHECK:    omp.terminator
147!CHECK:  ^[[BB2]]:
148!CHECK:    {{.*}}fir.call @_FortranAStopStatement({{.*}}, {{.*}}, {{.*}}) fastmath<contract> : (i32, i1, i1) -> ()
149!CHECK:    omp.terminator
150!CHECK:  }
151!CHECK:  return
152
153subroutine test_stop_in_region6(x)
154  integer :: x
155  !$omp parallel
156  if (x .gt. 1) then
157    stop 1
158  else
159    stop 2
160  end if
161  !$omp end parallel
162end
163