Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Lower / HLFIR / array-ctor-as-runtime-temp.f90
blob81b29e37a08d8b8566714437d8464e480900e2dc
1 ! Test lowering of array constructors requiring runtime library help to HLFIR.
2 ! RUN: bbc -emit-fir -hlfir -o - %s | FileCheck %s
3 module arrayctor
4 contains
6 subroutine test_loops()
7 call takes_int([((i, i=1,ifoo()), j=1,ibar())])
8 end subroutine
9 ! CHECK-LABEL: func.func @_QMarrayctorPtest_loops() {
10 ! CHECK: %[[VAL_0:.*]] = fir.alloca i32
11 ! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.array<10xi64> {bindc_name = ".rt.arrayctor.vector"}
12 ! CHECK: %[[VAL_2:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>> {bindc_name = ".tmp.arrayctor"}
13 ! CHECK: %[[VAL_3:.*]] = arith.constant 0 : index
14 ! CHECK: %[[VAL_4:.*]] = fir.zero_bits !fir.heap<!fir.array<?xi32>>
15 ! CHECK: %[[VAL_5:.*]] = fir.shape %[[VAL_3]] : (index) -> !fir.shape<1>
16 ! CHECK: %[[VAL_6:.*]] = fir.embox %[[VAL_4]](%[[VAL_5]]) : (!fir.heap<!fir.array<?xi32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xi32>>>
17 ! CHECK: fir.store %[[VAL_6]] to %[[VAL_2]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
18 ! CHECK: %[[VAL_7:.*]] = arith.constant false
19 ! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<10xi64>>) -> !fir.llvm_ptr<i8>
20 ! CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_2]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.ref<!fir.box<none>>
21 ! CHECK: %[[VAL_14:.*]] = fir.call @_FortranAInitArrayConstructorVector(%[[VAL_8]], %[[VAL_12]], %[[VAL_7]], %{{.*}}, %{{.*}}, %{{.*}}) {{.*}}: (!fir.llvm_ptr<i8>, !fir.ref<!fir.box<none>>, i1, i32, !fir.ref<i8>, i32) -> none
22 ! CHECK: %[[VAL_15:.*]] = arith.constant 1 : i64
23 ! CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_15]] : (i64) -> index
24 ! CHECK: %[[VAL_17:.*]] = fir.call @_QMarrayctorPibar() {{.*}}: () -> i32
25 ! CHECK: %[[VAL_18:.*]] = fir.convert %[[VAL_17]] : (i32) -> i64
26 ! CHECK: %[[VAL_19:.*]] = fir.convert %[[VAL_18]] : (i64) -> index
27 ! CHECK: %[[VAL_20:.*]] = arith.constant 1 : i64
28 ! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (i64) -> index
29 ! CHECK: fir.do_loop %[[VAL_22:.*]] = %[[VAL_16]] to %[[VAL_19]] step %[[VAL_21]] {
30 ! CHECK: %[[VAL_23:.*]] = arith.constant 1 : i64
31 ! CHECK: %[[VAL_24:.*]] = fir.convert %[[VAL_23]] : (i64) -> index
32 ! CHECK: %[[VAL_25:.*]] = fir.call @_QMarrayctorPifoo() {{.*}}: () -> i32
33 ! CHECK: %[[VAL_26:.*]] = fir.convert %[[VAL_25]] : (i32) -> i64
34 ! CHECK: %[[VAL_27:.*]] = fir.convert %[[VAL_26]] : (i64) -> index
35 ! CHECK: %[[VAL_28:.*]] = arith.constant 1 : i64
36 ! CHECK: %[[VAL_29:.*]] = fir.convert %[[VAL_28]] : (i64) -> index
37 ! CHECK: fir.do_loop %[[VAL_30:.*]] = %[[VAL_24]] to %[[VAL_27]] step %[[VAL_29]] {
38 ! CHECK: %[[VAL_31:.*]] = fir.convert %[[VAL_30]] : (index) -> i32
39 ! CHECK: fir.store %[[VAL_31]] to %[[VAL_0]] : !fir.ref<i32>
40 ! CHECK: %[[VAL_32:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<i32>) -> !fir.llvm_ptr<i8>
41 ! CHECK: %[[VAL_33:.*]] = fir.call @_FortranAPushArrayConstructorSimpleScalar(%[[VAL_8]], %[[VAL_32]]) {{.*}}: (!fir.llvm_ptr<i8>, !fir.llvm_ptr<i8>) -> none
42 ! CHECK: }
43 ! CHECK: }
44 ! CHECK: %[[VAL_34:.*]] = arith.constant true
45 ! CHECK: %[[VAL_35:.*]] = fir.load %[[VAL_2]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
46 ! CHECK: hlfir.as_expr %[[VAL_35]] move %[[VAL_34]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, i1) -> !hlfir.expr<?xi32>
48 subroutine test_arrays(a)
49 integer :: a(:, :)
50 call takes_int([a, a])
51 end subroutine
52 ! CHECK-LABEL: func.func @_QMarrayctorPtest_arrays(
53 ! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.array<10xi64> {bindc_name = ".rt.arrayctor.vector"}
54 ! CHECK: %[[VAL_2:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>> {bindc_name = ".tmp.arrayctor"}
55 ! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare {{.*}}Ea"
56 ! CHECK: %[[VAL_4:.*]] = arith.constant 0 : i64
57 ! CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
58 ! CHECK: %[[VAL_6:.*]]:3 = fir.box_dims %[[VAL_3]]#0, %[[VAL_5]] : (!fir.box<!fir.array<?x?xi32>>, index) -> (index, index, index)
59 ! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_6]]#1 : (index) -> i64
60 ! CHECK: %[[VAL_8:.*]] = arith.constant 1 : index
61 ! CHECK: %[[VAL_9:.*]]:3 = fir.box_dims %[[VAL_3]]#0, %[[VAL_8]] : (!fir.box<!fir.array<?x?xi32>>, index) -> (index, index, index)
62 ! CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_9]]#1 : (index) -> i64
63 ! CHECK: %[[VAL_11:.*]] = arith.muli %[[VAL_7]], %[[VAL_10]] : i64
64 ! CHECK: %[[VAL_12:.*]] = arith.addi %[[VAL_4]], %[[VAL_11]] : i64
65 ! CHECK: %[[VAL_20:.*]] = arith.addi %[[VAL_12]], %{{.*}} : i64
66 ! CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (i64) -> index
67 ! CHECK: %[[VAL_22:.*]] = fir.allocmem !fir.array<?xi32>, %[[VAL_21]] {bindc_name = ".tmp.arrayctor", uniq_name = ""}
68 ! CHECK: %[[VAL_23:.*]] = fir.shape %[[VAL_21]] : (index) -> !fir.shape<1>
69 ! CHECK: %[[VAL_24:.*]]:2 = hlfir.declare %[[VAL_22]](%[[VAL_23]]) {uniq_name = ".tmp.arrayctor"} : (!fir.heap<!fir.array<?xi32>>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.heap<!fir.array<?xi32>>)
70 ! CHECK: %[[VAL_25:.*]] = fir.embox %[[VAL_24]]#1(%[[VAL_23]]) : (!fir.heap<!fir.array<?xi32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xi32>>>
71 ! CHECK: fir.store %[[VAL_25]] to %[[VAL_2]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
72 ! CHECK: %[[VAL_26:.*]] = arith.constant false
73 ! CHECK: %[[VAL_27:.*]] = fir.convert %[[VAL_1]] : (!fir.ref<!fir.array<10xi64>>) -> !fir.llvm_ptr<i8>
74 ! CHECK: %[[VAL_31:.*]] = fir.convert %[[VAL_2]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.ref<!fir.box<none>>
75 ! CHECK: %[[VAL_33:.*]] = fir.call @_FortranAInitArrayConstructorVector(%[[VAL_27]], %[[VAL_31]], %[[VAL_26]], %{{.*}}, %{{.*}}, %{{.*}}) {{.*}}: (!fir.llvm_ptr<i8>, !fir.ref<!fir.box<none>>, i1, i32, !fir.ref<i8>, i32) -> none
76 ! CHECK: %[[VAL_34:.*]] = fir.convert %[[VAL_3]]#1 : (!fir.box<!fir.array<?x?xi32>>) -> !fir.box<none>
77 ! CHECK: %[[VAL_35:.*]] = fir.call @_FortranAPushArrayConstructorValue(%[[VAL_27]], %[[VAL_34]]) {{.*}}: (!fir.llvm_ptr<i8>, !fir.box<none>) -> none
78 ! CHECK: %[[VAL_36:.*]] = fir.convert %[[VAL_3]]#1 : (!fir.box<!fir.array<?x?xi32>>) -> !fir.box<none>
79 ! CHECK: %[[VAL_37:.*]] = fir.call @_FortranAPushArrayConstructorValue(%[[VAL_27]], %[[VAL_36]]) {{.*}}: (!fir.llvm_ptr<i8>, !fir.box<none>) -> none
80 ! CHECK: %[[VAL_38:.*]] = arith.constant true
81 ! CHECK: hlfir.as_expr %[[VAL_24]]#0 move %[[VAL_38]] : (!fir.box<!fir.array<?xi32>>, i1) -> !hlfir.expr<?xi32>
83 subroutine test_arrays_unpredictable_size()
84 call takes_int([rank1(), rank3(), rank1()])
85 ! CHECK-LABEL: func.func @_QMarrayctorPtest_arrays_unpredictable_size() {
86 ! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array<10xi64> {bindc_name = ".rt.arrayctor.vector"}
87 ! CHECK: %[[VAL_4:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xi32>>> {bindc_name = ".tmp.arrayctor"}
88 ! CHECK: %[[VAL_5:.*]] = arith.constant 0 : index
89 ! CHECK: %[[VAL_6:.*]] = fir.zero_bits !fir.heap<!fir.array<?xi32>>
90 ! CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_5]] : (index) -> !fir.shape<1>
91 ! CHECK: %[[VAL_8:.*]] = fir.embox %[[VAL_6]](%[[VAL_7]]) : (!fir.heap<!fir.array<?xi32>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?xi32>>>
92 ! CHECK: fir.store %[[VAL_8]] to %[[VAL_4]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
93 ! CHECK: %[[VAL_9:.*]] = arith.constant false
94 ! CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_3]] : (!fir.ref<!fir.array<10xi64>>) -> !fir.llvm_ptr<i8>
95 ! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_4]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>) -> !fir.ref<!fir.box<none>>
96 ! CHECK: %[[VAL_16:.*]] = fir.call @_FortranAInitArrayConstructorVector(%[[VAL_10]], %[[VAL_14]], %[[VAL_9]], %{{.*}}, %{{.*}}, %{{.*}}) {{.*}}: (!fir.llvm_ptr<i8>, !fir.ref<!fir.box<none>>, i1, i32, !fir.ref<i8>, i32) -> none
97 ! CHECK: fir.call @_QMarrayctorPrank1() {{.*}}: () -> !fir.box<!fir.heap<!fir.array<?xi32>>>
98 ! CHECK: %[[VAL_21:.*]] = fir.call @_FortranAPushArrayConstructorValue(%[[VAL_10]], %{{.*}}) {{.*}}: (!fir.llvm_ptr<i8>, !fir.box<none>) -> none
99 ! CHECK: fir.call @_QMarrayctorPrank3() {{.*}}: () -> !fir.box<!fir.heap<!fir.array<?x?x?xi32>>>
100 ! CHECK: %[[VAL_26:.*]] = fir.call @_FortranAPushArrayConstructorValue(%[[VAL_10]], %{{.*}}) {{.*}}: (!fir.llvm_ptr<i8>, !fir.box<none>) -> none
101 ! CHECK: fir.call @_QMarrayctorPrank1() {{.*}}: () -> !fir.box<!fir.heap<!fir.array<?xi32>>>
102 ! CHECK: %[[VAL_31:.*]] = fir.call @_FortranAPushArrayConstructorValue(%[[VAL_10]], %{{.*}}) {{.*}}: (!fir.llvm_ptr<i8>, !fir.box<none>) -> none
103 ! CHECK: %[[VAL_32:.*]] = arith.constant true
104 ! CHECK: %[[VAL_33:.*]] = fir.load %[[VAL_4]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
105 ! CHECK: hlfir.as_expr %[[VAL_33]] move %[[VAL_32]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, i1) -> !hlfir.expr<?xi32>
106 end subroutine
109 ! End to to end test implementation
110 function rank1()
111 integer, save :: counter = 2
112 integer, allocatable :: rank1(:)
113 allocate(rank1(counter))
114 do i=1,counter
115 rank1(i)=i
116 end do
117 counter = counter +1
118 end function
119 function rank3()
120 integer, save :: counter = 1
121 integer, allocatable :: rank3(:, :, :)
122 allocate(rank3(counter, counter+1, counter+2))
123 do k=1,counter+2
124 do j=1,counter+1
125 do i=1,counter
126 rank3(i, j, k)=i+(j-1)*counter+(k-1)*counter*(counter+1)
127 end do
128 end do
129 end do
130 counter = counter+1
131 end function
133 function ifoo()
134 integer, save :: counter = 0
135 ifoo = counter
136 counter = counter +1
137 end function
139 function ibar()
140 ibar = 6
141 end function
144 subroutine takes_int(a)
145 integer :: a(:)
146 print *, "got :", a
147 end subroutine
148 end module
150 use arrayctor
151 integer :: a(2,3) = reshape([1,2,3,4,5,6], shape=[2,3])
152 print *, "expect: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5"
153 call test_loops()
154 print *, "expect: 1 2 3 4 5 6 1 2 3 4 5 6"
155 call test_arrays(a)
156 print *, "expect: 1 2 1 2 3 4 5 6 1 2 3"
157 call test_arrays_unpredictable_size()