Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Lower / forall / forall-2.f90
blobd69c4edcb5b2ed4e2ff5462bb58ebf91f0181378
1 ! RUN: bbc -emit-fir %s -o - | FileCheck %s
2 ! RUN: bbc %s -o - | FileCheck --check-prefix=POSTOPT %s
4 ! CHECK-LABEL: func @_QPimplied_iters_allocatable(
5 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.type<_QFimplied_iters_allocatableTt{oui:!fir.logical<4>,arr:!fir.box<!fir.heap<!fir.array<?xf32>>>}>>>{{.*}}, %[[VAL_1:.*]]: !fir.box<!fir.array<?xf32>>{{.*}}) {
6 ! CHECK: return
7 ! CHECK: }
9 subroutine implied_iters_allocatable(thing, a1)
10 ! No dependence between lhs and rhs.
11 ! Lhs may need to be reallocated to conform.
12 real :: a1(:)
13 type t
14 logical :: oui
15 real, allocatable :: arr(:)
16 end type t
17 type(t) :: thing(:)
18 integer :: i
20 forall (i=5:13)
21 ! commenting out this test for the moment (hits assert)
22 ! thing(i)%arr = a1
23 end forall
24 end subroutine implied_iters_allocatable
26 subroutine conflicting_allocatable(thing, lo, hi)
27 ! Introduce a crossing dependence to produce copy-in/copy-out code.
28 integer :: lo,hi
29 type t
30 logical :: oui
31 real, allocatable :: arr(:)
32 end type t
33 type(t) :: thing(:)
34 integer :: i
36 forall (i = lo:hi)
37 ! commenting out this test for the moment (hits assert)
38 ! thing(i)%arr = thing(hi-i)%arr
39 end forall
40 end subroutine conflicting_allocatable
42 ! CHECK-LABEL: func @_QPforall_pointer_assign(
43 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>> {fir.bindc_name = "ap"}, %[[VAL_1:.*]]: !fir.ref<f32> {fir.bindc_name = "at"}, %[[VAL_2:.*]]: !fir.ref<i32> {fir.bindc_name = "ii"}, %[[VAL_3:.*]]: !fir.ref<i32> {fir.bindc_name = "ij"}) {
44 ! CHECK: %[[VAL_4:.*]] = fir.alloca i32 {adapt.valuebyref, bindc_name = "i"}
45 ! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_2]] : !fir.ref<i32>
46 ! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_5]] : (i32) -> index
47 ! CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_3]] : !fir.ref<i32>
48 ! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (i32) -> index
49 ! CHECK: %[[VAL_9:.*]] = arith.constant 8 : i32
50 ! CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_9]] : (i32) -> index
51 ! CHECK: %[[VAL_11:.*]] = fir.array_load %[[VAL_0]] : (!fir.box<!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>) -> !fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>
52 ! CHECK: %[[VAL_12:.*]] = fir.array_load %[[VAL_0]] : (!fir.box<!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>) -> !fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>
53 ! CHECK: %[[VAL_13:.*]] = fir.do_loop %[[VAL_14:.*]] = %[[VAL_6]] to %[[VAL_8]] step %[[VAL_10]] unordered iter_args(%[[VAL_15:.*]] = %[[VAL_11]]) -> (!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>) {
54 ! CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_14]] : (index) -> i32
55 ! CHECK: fir.store %[[VAL_16]] to %[[VAL_4]] : !fir.ref<i32>
56 ! CHECK-DAG: %[[VAL_17:.*]] = arith.constant 1 : index
57 ! CHECK-DAG: %[[VAL_18:.*]] = arith.constant 1 : i32
58 ! CHECK-DAG: %[[VAL_19:.*]] = fir.load %[[VAL_4]] : !fir.ref<i32>
59 ! CHECK: %[[VAL_20:.*]] = arith.subi %[[VAL_19]], %[[VAL_18]] : i32
60 ! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (i32) -> i64
61 ! CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_21]] : (i64) -> index
62 ! CHECK: %[[VAL_23:.*]] = arith.subi %[[VAL_22]], %[[VAL_17]] : index
63 ! CHECK: %[[VAL_24:.*]] = fir.field_index ptr, !fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
64 ! CHECK: %[[VAL_25:.*]] = fir.array_fetch %[[VAL_12]], %[[VAL_23]], %[[VAL_24]] : (!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, index, !fir.field) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
65 ! CHECK: %[[VAL_26:.*]] = arith.constant 1 : index
66 ! CHECK: %[[VAL_27:.*]] = fir.load %[[VAL_4]] : !fir.ref<i32>
67 ! CHECK: %[[VAL_28:.*]] = fir.convert %[[VAL_27]] : (i32) -> i64
68 ! CHECK: %[[VAL_29:.*]] = fir.convert %[[VAL_28]] : (i64) -> index
69 ! CHECK: %[[VAL_30:.*]] = arith.subi %[[VAL_29]], %[[VAL_26]] : index
70 ! CHECK: %[[VAL_31:.*]] = fir.field_index ptr, !fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
71 ! CHECK: %[[VAL_32:.*]] = fir.array_update %[[VAL_15]], %[[VAL_25]], %[[VAL_30]], %[[VAL_31]] : (!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.box<!fir.ptr<!fir.array<?xf32>>>, index, !fir.field) -> !fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>
72 ! CHECK: fir.result %[[VAL_32]] : !fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>
73 ! CHECK: }
74 ! CHECK: fir.array_merge_store %[[VAL_11]], %[[VAL_33:.*]] to %[[VAL_0]] : !fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, !fir.box<!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>
75 ! CHECK: return
76 ! CHECK: }
78 ! POSTOPT-LABEL: func @_QPforall_pointer_assign(
79 ! POSTOPT: %[[VAL_15:.*]] = fir.allocmem !fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>, %{{.*}}#1
80 ! POSTOPT: ^bb{{[0-9]+}}(%[[VAL_16:.*]]: index, %[[VAL_17:.*]]: index):
81 ! POSTOPT: ^bb{{[0-9]+}}(%[[VAL_30:.*]]: index, %[[VAL_31:.*]]: index):
82 ! POSTOPT: ^bb{{[0-9]+}}(%[[VAL_46:.*]]: index, %[[VAL_47:.*]]: index):
83 ! POSTOPT-NOT: ^bb{{[0-9]+}}(%{{.*}}: index, %{{.*}}: index):
84 ! POSTOPT: fir.freemem %[[VAL_15]] : !fir.heap<!fir.array<?x!fir.type<_QFforall_pointer_assignTt{ptr:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>>>
85 ! POSTOPT: }
87 subroutine forall_pointer_assign(ap, at, ii, ij)
88 ! Set pointer members in an array of derived type of pointers to arrays.
89 ! Introduce a loop carried dependence to produce copy-in/copy-out code.
90 type t
91 real, pointer :: ptr(:)
92 end type t
93 type(t) :: ap(:)
94 integer :: ii, ij
96 forall (i = ii:ij:8)
97 ap(i)%ptr => ap(i-1)%ptr
98 end forall
99 end subroutine forall_pointer_assign
101 ! CHECK-LABEL: func @_QPslice_with_explicit_iters() {
102 ! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {adapt.valuebyref, bindc_name = "i"}
103 ! CHECK: %[[VAL_1:.*]] = arith.constant 10 : index
104 ! CHECK: %[[VAL_2:.*]] = arith.constant 10 : index
105 ! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array<10x10xi32> {bindc_name = "a", uniq_name = "_QFslice_with_explicit_itersEa"}
106 ! CHECK: %[[VAL_4:.*]] = arith.constant 1 : i32
107 ! CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_4]] : (i32) -> index
108 ! CHECK: %[[VAL_6:.*]] = arith.constant 5 : i32
109 ! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_6]] : (i32) -> index
110 ! CHECK: %[[VAL_8:.*]] = arith.constant 1 : index
111 ! CHECK: %[[VAL_9:.*]] = fir.shape %[[VAL_1]], %[[VAL_2]] : (index, index) -> !fir.shape<2>
112 ! CHECK: %[[VAL_10:.*]] = fir.array_load %[[VAL_3]](%[[VAL_9]]) : (!fir.ref<!fir.array<10x10xi32>>, !fir.shape<2>) -> !fir.array<10x10xi32>
113 ! CHECK: %[[VAL_11:.*]] = fir.do_loop %[[VAL_12:.*]] = %[[VAL_5]] to %[[VAL_7]] step %[[VAL_8]] unordered iter_args(%[[VAL_13:.*]] = %[[VAL_10]]) -> (!fir.array<10x10xi32>) {
114 ! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_12]] : (index) -> i32
115 ! CHECK: fir.store %[[VAL_14]] to %[[VAL_0]] : !fir.ref<i32>
116 ! CHECK: %[[VAL_15:.*]] = arith.constant 1 : index
117 ! CHECK: %[[VAL_16:.*]] = arith.constant 1 : i64
118 ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (i64) -> index
119 ! CHECK: %[[VAL_18:.*]] = arith.constant 1 : i64
120 ! CHECK: %[[VAL_19:.*]] = fir.convert %[[VAL_18]] : (i64) -> index
121 ! CHECK: %[[VAL_20:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
122 ! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (i32) -> i64
123 ! CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_21]] : (i64) -> index
124 ! CHECK: %[[VAL_23:.*]] = arith.constant 0 : index
125 ! CHECK: %[[VAL_24:.*]] = arith.subi %[[VAL_22]], %[[VAL_17]] : index
126 ! CHECK: %[[VAL_25:.*]] = arith.addi %[[VAL_24]], %[[VAL_19]] : index
127 ! CHECK: %[[VAL_26:.*]] = arith.divsi %[[VAL_25]], %[[VAL_19]] : index
128 ! CHECK: %[[VAL_27:.*]] = arith.cmpi sgt, %[[VAL_26]], %[[VAL_23]] : index
129 ! CHECK: %[[VAL_28:.*]] = arith.select %[[VAL_27]], %[[VAL_26]], %[[VAL_23]] : index
130 ! CHECK: %[[VAL_29:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
131 ! CHECK: %[[VAL_30:.*]] = fir.convert %[[VAL_29]] : (i32) -> i64
132 ! CHECK: %[[VAL_31:.*]] = fir.convert %[[VAL_30]] : (i64) -> index
133 ! CHECK: %[[VAL_32:.*]] = arith.subi %[[VAL_31]], %[[VAL_15]] : index
134 ! CHECK: %[[VAL_39:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
135 ! CHECK: %[[VAL_40:.*]] = arith.constant 0 : i32
136 ! CHECK: %[[VAL_41:.*]] = arith.subi %[[VAL_40]], %[[VAL_39]] : i32
137 ! CHECK: %[[VAL_33:.*]] = arith.constant 1 : index
138 ! CHECK: %[[VAL_34:.*]] = arith.constant 0 : index
139 ! CHECK: %[[VAL_35:.*]] = arith.subi %[[VAL_28]], %[[VAL_33]] : index
140 ! CHECK: %[[VAL_36:.*]] = fir.do_loop %[[VAL_37:.*]] = %[[VAL_34]] to %[[VAL_35]] step %[[VAL_33]] unordered iter_args(%[[VAL_38:.*]] = %[[VAL_13]]) -> (!fir.array<10x10xi32>) {
141 ! CHECK: %[[VAL_42:.*]] = arith.subi %[[VAL_17]], %[[VAL_15]] : index
142 ! CHECK: %[[VAL_43:.*]] = arith.muli %[[VAL_37]], %[[VAL_19]] : index
143 ! CHECK: %[[VAL_44:.*]] = arith.addi %[[VAL_42]], %[[VAL_43]] : index
144 ! CHECK: %[[VAL_45:.*]] = fir.array_update %[[VAL_38]], %[[VAL_41]], %[[VAL_44]], %[[VAL_32]] : (!fir.array<10x10xi32>, i32, index, index) -> !fir.array<10x10xi32>
145 ! CHECK: fir.result %[[VAL_45]] : !fir.array<10x10xi32>
146 ! CHECK: }
147 ! CHECK: fir.result %[[VAL_46:.*]] : !fir.array<10x10xi32>
148 ! CHECK: }
149 ! CHECK: fir.array_merge_store %[[VAL_10]], %[[VAL_47:.*]] to %[[VAL_3]] : !fir.array<10x10xi32>, !fir.array<10x10xi32>, !fir.ref<!fir.array<10x10xi32>>
150 ! CHECK: return
151 ! CHECK: }
153 subroutine slice_with_explicit_iters
155 integer :: a(10,10)
156 forall (i=1:5)
157 a(1:i, i) = -i
158 end forall
159 end subroutine slice_with_explicit_iters
161 ! CHECK-LABEL: func @_QPembox_argument_with_slice(
162 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<1xi32>>{{.*}}, %[[VAL_1:.*]]: !fir.ref<!fir.array<2x2xi32>>{{.*}}) {
163 ! CHECK: %[[VAL_2:.*]] = fir.alloca i32 {adapt.valuebyref, bindc_name = "i"}
164 ! CHECK: %[[VAL_3:.*]] = arith.constant 1 : index
165 ! CHECK: %[[VAL_4:.*]] = arith.constant 2 : index
166 ! CHECK: %[[VAL_5:.*]] = arith.constant 2 : index
167 ! CHECK: %[[VAL_6:.*]] = arith.constant 1 : i32
168 ! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_6]] : (i32) -> index
169 ! CHECK: %[[VAL_8:.*]] = arith.constant 1 : i32
170 ! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_8]] : (i32) -> index
171 ! CHECK: %[[VAL_10:.*]] = arith.constant 1 : index
172 ! CHECK: %[[VAL_11:.*]] = fir.shape %[[VAL_3]] : (index) -> !fir.shape<1>
173 ! CHECK: %[[VAL_12:.*]] = fir.array_load %[[VAL_0]](%[[VAL_11]]) : (!fir.ref<!fir.array<1xi32>>, !fir.shape<1>) -> !fir.array<1xi32>
174 ! CHECK: %[[VAL_13:.*]] = fir.do_loop %[[VAL_14:.*]] = %[[VAL_7]] to %[[VAL_9]] step %[[VAL_10]] unordered iter_args(%[[VAL_15:.*]] = %[[VAL_12]]) -> (!fir.array<1xi32>) {
175 ! CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_14]] : (index) -> i32
176 ! CHECK: fir.store %[[VAL_16]] to %[[VAL_2]] : !fir.ref<i32>
177 ! CHECK-DAG: %[[VAL_18:.*]] = arith.constant 1 : index
178 ! CHECK-DAG: %[[VAL_19:.*]] = arith.constant 1 : i64
179 ! CHECK: %[[VAL_20:.*]] = fir.convert %[[VAL_19]] : (i64) -> index
180 ! CHECK: %[[VAL_21:.*]] = arith.addi %[[VAL_18]], %[[VAL_4]] : index
181 ! CHECK: %[[VAL_22:.*]] = arith.subi %[[VAL_21]], %[[VAL_18]] : index
182 ! CHECK: %[[VAL_23:.*]] = fir.load %[[VAL_2]] : !fir.ref<i32>
183 ! CHECK: %[[VAL_24:.*]] = fir.convert %[[VAL_23]] : (i32) -> i64
184 ! CHECK: %[[VAL_25:.*]] = fir.undefined index
185 ! CHECK: %[[VAL_26:.*]] = fir.shape %[[VAL_4]], %[[VAL_5]] : (index, index) -> !fir.shape<2>
186 ! CHECK: %[[VAL_27:.*]] = fir.slice %[[VAL_18]], %[[VAL_22]], %[[VAL_20]], %[[VAL_24]], %[[VAL_25]], %[[VAL_25]] : (index, index, index, i64, index, index) -> !fir.slice<2>
187 ! CHECK: %[[VAL_28:.*]] = fir.embox %[[VAL_1]](%[[VAL_26]]) {{\[}}%[[VAL_27]]] : (!fir.ref<!fir.array<2x2xi32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?xi32>>
188 ! CHECK: %[[VAL_29:.*]] = fir.call @_QPe(%[[VAL_28]]) {{.*}}: (!fir.box<!fir.array<?xi32>>) -> i32
189 ! CHECK: %[[VAL_17:.*]] = arith.constant 1 : i32
190 ! CHECK: %[[VAL_30:.*]] = arith.addi %[[VAL_29]], %[[VAL_17]] : i32
191 ! CHECK: %[[VAL_31:.*]] = arith.constant 1 : index
192 ! CHECK: %[[VAL_32:.*]] = fir.load %[[VAL_2]] : !fir.ref<i32>
193 ! CHECK: %[[VAL_33:.*]] = fir.convert %[[VAL_32]] : (i32) -> i64
194 ! CHECK: %[[VAL_34:.*]] = fir.convert %[[VAL_33]] : (i64) -> index
195 ! CHECK: %[[VAL_35:.*]] = arith.subi %[[VAL_34]], %[[VAL_31]] : index
196 ! CHECK: %[[VAL_36:.*]] = fir.array_update %[[VAL_15]], %[[VAL_30]], %[[VAL_35]] : (!fir.array<1xi32>, i32, index) -> !fir.array<1xi32>
197 ! CHECK: fir.result %[[VAL_36]] : !fir.array<1xi32>
198 ! CHECK: }
199 ! CHECK: fir.array_merge_store %[[VAL_12]], %[[VAL_37:.*]] to %[[VAL_0]] : !fir.array<1xi32>, !fir.array<1xi32>, !fir.ref<!fir.array<1xi32>>
200 ! CHECK: return
201 ! CHECK: }
203 subroutine embox_argument_with_slice(a,b)
204 interface
205 pure integer function e(a)
206 integer, intent(in) :: a(:)
207 end function e
208 end interface
209 integer a(1), b(2,2)
211 forall (i=1:1)
212 a(i) = e(b(:,i)) + 1
213 end forall
214 end subroutine embox_argument_with_slice