1 ! Test lowering of elemental calls in array expressions.
2 ! RUN: bbc -o - -emit-fir %s | FileCheck %s
7 elemental
integer function elem_by_ref(a
,b
) result(r
)
8 integer, intent(in
) :: a
12 elemental
integer function elem_by_valueref(a
,b
) result(r
)
18 ! CHECK-LABEL: func @_QMscalar_in_elemPtest_elem_by_ref(
19 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.array<100xi32>>{{.*}}, %[[arg1:.*]]: !fir.ref<!fir.array<100xi32>>{{.*}}) {
20 subroutine test_elem_by_ref(i
, j
)
21 integer :: i(100), j(100)
22 ! CHECK: %[[tmp:.*]] = fir.alloca f32
23 ! CHECK: %[[cst:.*]] = arith.constant 4.200000e+01 : f32
24 ! CHECK: fir.store %[[cst]] to %[[tmp]] : !fir.ref<f32>
27 ! CHECK: %[[j:.*]] = fir.array_coor %[[arg1]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<100xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
28 ! CHECK: fir.call @_QMscalar_in_elemPelem_by_ref(%[[j]], %[[tmp]]) {{.*}}: (!fir.ref<i32>, !fir.ref<f32>) -> i32
30 i
= elem_by_ref(j
, 42.)
33 ! CHECK-LABEL: func @_QMscalar_in_elemPtest_elem_by_valueref(
34 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.array<100xi32>>{{.*}}, %[[arg1:.*]]: !fir.ref<!fir.array<100xi32>>{{.*}}) {
35 subroutine test_elem_by_valueref(i
, j
)
36 integer :: i(100), j(100)
37 ! CHECK: %[[jload:.*]] = fir.array_load %[[arg1]]
38 ! CHECK: %[[cst:.*]] = arith.constant 4.200000e+01 : f32
41 ! CHECK: %[[j:.*]] = fir.array_fetch %[[jload]], %{{.*}} : (!fir.array<100xi32>, index) -> i32
42 ! CHECK: fir.call @_QMscalar_in_elemPelem_by_valueref(%[[j]], %[[cst]]) {{.*}}: (i32, f32) -> i32
44 i
= elem_by_valueref(j
, 42.)
49 ! Test that impure elemental functions cause ordered loops to be emitted
50 subroutine test_loop_order(i
, j
)
53 elemental
integer function pure_func(j
)
54 integer, intent(in
) :: j
56 elemental impure
integer function impure_func(j
)
57 integer, intent(in
) :: j
62 i
= 42 + impure_func(j
)
65 ! CHECK-LABEL: func @_QPtest_loop_order(
66 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xi32>>{{.*}}, %[[VAL_1:.*]]: !fir.box<!fir.array<?xi32>>{{.*}}) {
67 ! CHECK: %[[VAL_2:.*]] = arith.constant 0 : index
68 ! CHECK: %[[VAL_3:.*]]:3 = fir.box_dims %[[VAL_0]], %[[VAL_2]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
69 ! CHECK: %[[VAL_4:.*]] = fir.array_load %[[VAL_0]] : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
70 ! CHECK: %[[VAL_5:.*]] = arith.constant 42 : i32
71 ! CHECK: %[[VAL_6:.*]] = arith.constant 1 : index
72 ! CHECK: %[[VAL_7:.*]] = arith.constant 0 : index
73 ! CHECK: %[[VAL_8:.*]] = arith.subi %[[VAL_3]]#1, %[[VAL_6]] : index
74 ! CHECK: %[[VAL_9:.*]] = fir.do_loop %[[VAL_10:.*]] = %[[VAL_7]] to %[[VAL_8]] step %[[VAL_6]] unordered iter_args(%[[VAL_11:.*]] = %[[VAL_4]]) -> (!fir.array<?xi32>) {
75 ! CHECK: %[[VAL_12:.*]] = arith.constant 1 : index
76 ! CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_10]], %[[VAL_12]] : index
77 ! CHECK: %[[VAL_14:.*]] = fir.array_coor %[[VAL_1]] %[[VAL_13]] : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
78 ! CHECK: %[[VAL_15:.*]] = fir.call @_QPpure_func(%[[VAL_14]]) {{.*}}: (!fir.ref<i32>) -> i32
79 ! CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_5]], %[[VAL_15]] : i32
80 ! CHECK: %[[VAL_17:.*]] = fir.array_update %[[VAL_11]], %[[VAL_16]], %[[VAL_10]] : (!fir.array<?xi32>, i32, index) -> !fir.array<?xi32>
81 ! CHECK: fir.result %[[VAL_17]] : !fir.array<?xi32>
83 ! CHECK: fir.array_merge_store %[[VAL_4]], %[[VAL_18:.*]] to %[[VAL_0]] : !fir.array<?xi32>, !fir.array<?xi32>, !fir.box<!fir.array<?xi32>>
84 ! CHECK: %[[VAL_19:.*]] = arith.constant 0 : index
85 ! CHECK: %[[VAL_20:.*]]:3 = fir.box_dims %[[VAL_0]], %[[VAL_19]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
86 ! CHECK: %[[VAL_21:.*]] = fir.array_load %[[VAL_0]] : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
87 ! CHECK: %[[VAL_22:.*]] = arith.constant 42 : i32
88 ! CHECK: %[[VAL_23:.*]] = arith.constant 1 : index
89 ! CHECK: %[[VAL_24:.*]] = arith.constant 0 : index
90 ! CHECK: %[[VAL_25:.*]] = arith.subi %[[VAL_20]]#1, %[[VAL_23]] : index
91 ! CHECK: %[[VAL_26:.*]] = fir.do_loop %[[VAL_27:.*]] = %[[VAL_24]] to %[[VAL_25]] step %[[VAL_23]] iter_args(%[[VAL_28:.*]] = %[[VAL_21]]) -> (!fir.array<?xi32>) {
92 ! CHECK: %[[VAL_29:.*]] = arith.constant 1 : index
93 ! CHECK: %[[VAL_30:.*]] = arith.addi %[[VAL_27]], %[[VAL_29]] : index
94 ! CHECK: %[[VAL_31:.*]] = fir.array_coor %[[VAL_1]] %[[VAL_30]] : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
95 ! CHECK: %[[VAL_32:.*]] = fir.call @_QPimpure_func(%[[VAL_31]]) {{.*}}: (!fir.ref<i32>) -> i32
96 ! CHECK: %[[VAL_33:.*]] = arith.addi %[[VAL_22]], %[[VAL_32]] : i32
97 ! CHECK: %[[VAL_34:.*]] = fir.array_update %[[VAL_28]], %[[VAL_33]], %[[VAL_27]] : (!fir.array<?xi32>, i32, index) -> !fir.array<?xi32>
98 ! CHECK: fir.result %[[VAL_34]] : !fir.array<?xi32>
100 ! CHECK: fir.array_merge_store %[[VAL_21]], %[[VAL_35:.*]] to %[[VAL_0]] : !fir.array<?xi32>, !fir.array<?xi32>, !fir.box<!fir.array<?xi32>>