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