1 ! Test for PassBy::BaseAddressValueAttribute
2 ! RUN: bbc -emit-fir %s -o - | FileCheck %s
3 program call_by_value_attr
9 integer, value
, dimension(10) :: val
13 !CHECK-LABEL: func @_QQmain()
15 integer, dimension(10) :: a
16 integer, dimension(15) :: b
19 !CHECK: %[[ARRAY_A:.*]] = fir.address_of(@_QFEa)
20 !CHECK: %[[CONST_10_1:.*]] = arith.constant 10 : index
21 !CHECK: %[[ARRAY_B:.*]] = fir.address_of(@_QFEb)
22 !CHECK: %[[CONST_15_1:.*]] = arith.constant 15 : index
23 !CHECK: %[[VALUE:.*]] = fir.alloca i32 {bindc_name = "v", {{.*}}}
24 !CHECK: %[[CONST:.*]] = arith.constant 17
25 !CHECK: fir.store %[[CONST]] to %[[VALUE]]
26 !CHECK: %[[LOAD:.*]] = fir.load %[[VALUE]]
27 !CHECK: fir.call @_QPsubri(%[[LOAD]]) {{.*}}: {{.*}}
28 a
= (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /)
29 !CHECK: %[[SHAPE_1:.*]] = fir.shape %[[CONST_10_1]]
30 !CHECK: %[[ARRAY_LOAD_1:.*]] = fir.array_load %[[ARRAY_A]](%[[SHAPE_1]]) : {{.*}}
31 !CHECK: %[[ARRAY_INIT_A:.*]] = fir.address_of({{.*}})
32 !CHECK: %[[CONST_10_2:.*]] = arith.constant 10 : index
33 !CHECK: %[[SHAPE_2:.*]] = fir.shape %[[CONST_10_2]]
34 !CHECK: %[[ARRAY_LOAD_2:.*]] = fir.array_load %[[ARRAY_INIT_A]](%[[SHAPE_2]]) : {{.*}}
35 !CHECK: %[[DO_1:.*]] = fir.do_loop {{.*}} {
37 !CHECK: fir.array_merge_store %[[ARRAY_LOAD_1]], %[[DO_1]] to %[[ARRAY_A]]
38 !CHECK: %[[ARRAY_COPY:.*]] = fir.allocmem !fir.array<10xi32>, %[[CONST_10_1]] {uniq_name = ".copy"}
39 !CHECK: %[[SHAPE_3:.*]] = fir.shape %[[CONST_10_1]]
40 !CHECK: %[[ARRAY_LOAD_3:.*]] = fir.array_load %[[ARRAY_COPY]](%[[SHAPE_3]]) : {{.*}}
41 !CHECK: %[[SHAPE_4:.*]] = fir.shape %[[CONST_10_1]]
42 !CHECK: %[[ARRAY_LOAD_4:.*]] = fir.array_load %[[ARRAY_A]](%[[SHAPE_4]]) : {{.*}}
43 !CHECK: %[[DO_2:.*]] = fir.do_loop {{.*}} {
45 !CHECK: fir.array_merge_store %[[ARRAY_LOAD_3]], %[[DO_2]] to %[[ARRAY_COPY]]
46 !CHECK: %[[CONVERT:.*]] = fir.convert %[[ARRAY_COPY]] : (!fir.heap<!fir.array<10xi32>>) -> !fir.ref<!fir.array<10xi32>>
47 !CHECK: fir.call @_QPsubra(%[[CONVERT]])
49 b
= (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 /)
50 !CHECK: %[[SHAPE_5:.*]] = fir.shape %[[CONST_15_1]]
51 !CHECK: %[[ARRAY_LOAD_5:.*]] = fir.array_load %[[ARRAY_B]](%[[SHAPE_5]]) : {{.*}}
52 !CHECK: %[[ARRAY_INIT_B:.*]] = fir.address_of({{.*}})
53 !CHECK: %[[CONST_15_2:.*]] = arith.constant 15 : index
54 !CHECK: %[[SHAPE_6:.*]] = fir.shape %[[CONST_15_2]] : (index) -> !fir.shape<1>
55 !CHECK: %[[ARRAY_LOAD_6:.*]] = fir.array_load %[[ARRAY_INIT_B]](%[[SHAPE_6]]) : (!fir.ref<!fir.array<15xi32>>, !fir.shape<1>) -> !fir.array<15xi32>
56 !CHECK: %[[DO_3:.*]] = fir.do_loop {{.*}} {
58 !CHECK: fir.array_merge_store %[[ARRAY_LOAD_5]], %[[DO_3]] to %[[ARRAY_B]]
59 !CHECK: %[[CONST_5:.*]] = arith.constant 5 : i64
60 !CHECK: %[[CONV_5:.*]] = fir.convert %[[CONST_5]] : (i64) -> index
61 !CHECK: %[[CONST_1:.*]] = arith.constant 1 : i64
62 !CHECK: %[[CONV_1:.*]] = fir.convert %[[CONST_1]] : (i64) -> index
63 !CHECK: %[[CONST_15_3:.*]] = arith.constant 15 : i64
64 !CHECK: %[[CONV_15:.*]] = fir.convert %[[CONST_15_3]] : (i64) -> index
65 !CHECK: %[[SHAPE_7:.*]] = fir.shape %[[CONST_15_1]] : (index) -> !fir.shape<1>
66 !CHECK: %[[SLICE:.*]] = fir.slice %[[CONV_5]], %[[CONV_15]], %[[CONV_1]] : (index, index, index) -> !fir.slice<1>
67 !CHECK: %[[BOX:.*]] = fir.embox %[[ARRAY_B]](%[[SHAPE_7]]) [%[[SLICE]]] : (!fir.ref<!fir.array<15xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<11xi32>>
68 !CHECK: %[[BOX_NONE:.*]] = fir.convert %[[BOX]] : (!fir.box<!fir.array<11xi32>>) -> !fir.box<none>
69 !CHECK: %[[IS_CONTIGUOUS:.*]] = fir.call @_FortranAIsContiguous(%[[BOX_NONE]]) {{.*}}: (!fir.box<none>) -> i1
70 !CHECK: %[[ADDR:.*]] = fir.if %[[IS_CONTIGUOUS]] -> (!fir.heap<!fir.array<11xi32>>) {
71 !CHECK: %[[BOX_ADDR:.*]] = fir.box_addr %[[BOX]] : (!fir.box<!fir.array<11xi32>>) -> !fir.heap<!fir.array<11xi32>>
72 !CHECKL fir.result %[[BOXADDR]] : !fir.heap<!fir.array<11xi32>>
73 !CHECK: %[[CONST_0:.*]] = arith.constant 0 : index
74 !CHECK: %[[DIMS:.*]]:3 = fir.box_dims %[[BOX]], %[[CONST_0]] : (!fir.box<!fir.array<11xi32>>, index) -> (index, index, index)
75 !CHECK: %[[ARRAY_COPY_2:.*]] = fir.allocmem !fir.array<11xi32>, %[[DIMS]]#1 {uniq_name = ".copy"}
76 !CHECK: %[[SHAPE_8:.*]] = fir.shape %[[DIMS]]#1 : (index) -> !fir.shape<1>
77 !CHECK: %[[TEMP_BOX:.*]] = fir.embox %[[ARRAY_COPY_2]](%[[SHAPE_8]]) : (!fir.heap<!fir.array<11xi32>>, !fir.shape<1>) -> !fir.box<!fir.array<11xi32>>
78 !CHECK: fir.store %[[TEMP_BOX]] to %[[TEMP_BOX_LOC:.*]] : !fir.ref<!fir.box<!fir.array<11xi32>>>
79 !CHECK: %[[TEMP_BOX_ADDR:.*]] = fir.convert %[[TEMP_BOX_LOC]] : (!fir.ref<!fir.box<!fir.array<11xi32>>>) -> !fir.ref<!fir.box<none>>
80 !CHECK: %[[BOX_ADDR:.*]] = fir.convert %[[BOX]] : (!fir.box<!fir.array<11xi32>>) -> !fir.box<none>
81 !CHECK: fir.call @_FortranAAssign(%[[TEMP_BOX_ADDR]], %[[BOX_ADDR]], %{{.*}}, %{{.*}}){{.*}}: (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> none
82 !CHECK: fir.result %[[ARRAY_COPY_2]] : !fir.heap<!fir.array<11xi32>>
83 !CHECK: %[[CONVERT_B:.*]] = fir.convert %[[ADDR]] : (!fir.heap<!fir.array<11xi32>>) -> !fir.ref<!fir.array<10xi32>>
84 !CHECK: fir.call @_QPsubra(%[[CONVERT_B]])
86 end program call_by_value_attr
88 ! CHECK-LABEL: func.func @_QPsubri(
89 ! CHECK-SAME: %[[VAL_0:.*]]: i32 {fir.bindc_name = "val"}) {
90 ! CHECK: %[[VAL_1:.*]] = fir.alloca i32
91 ! CHECK: fir.store %[[VAL_0]] to %[[VAL_1]] : !fir.ref<i32>
92 ! CHECK: fir.call @_QPtest_numeric_scalar_value(%[[VAL_1]]) {{.*}}: (!fir.ref<i32>) -> ()
98 call test_numeric_scalar_value(val
)
101 ! CHECK-LABEL: func @_QPtest_litteral_copies_1
102 subroutine test_litteral_copies_1
103 ! VALUE arguments can be modified by the callee, so the static storage of
104 ! literal constants and named parameters must not be passed directly to them.
106 subroutine takes_array_value(v
)
107 integer, value
:: v(4)
110 integer, parameter :: p(100) = 42
111 ! CHECK: %[[VAL_0:.*]] = arith.constant 100 : index
112 ! CHECK: %[[VAL_1:.*]] = fir.address_of(@_QQ{{.*}}) : !fir.ref<!fir.array<100xi32>>
113 ! CHECK: %[[VAL_5:.*]] = fir.allocmem !fir.array<100xi32>
114 ! CHECK: fir.do_loop %
116 ! CHECK: fir.array_merge_store %{{.*}}, %{{.*}} to %[[VAL_5]] : !fir.array<100xi32>, !fir.array<100xi32>, !fir.heap<!fir.array<100xi32>>
117 ! CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_5]] : (!fir.heap<!fir.array<100xi32>>) -> !fir.ref<!fir.array<4xi32>>
118 ! CHECK: fir.call @_QPtakes_array_value(%[[VAL_17]]) {{.*}}: (!fir.ref<!fir.array<4xi32>>) -> ()
119 call takes_array_value(p
)
120 ! CHECK: fir.freemem %[[VAL_5]] : !fir.heap<!fir.array<100xi32>>
123 ! CHECK-LABEL: func @_QPtest_litteral_copies_2
124 subroutine test_litteral_copies_2
126 subroutine takes_char_value(v
)
127 character(*), value
:: v
130 ! CHECK: %[[VAL_0:.*]] = fir.address_of(@_QQ{{.*}}) : !fir.ref<!fir.char<1,71>>
131 ! CHECK: %[[VAL_1:.*]] = arith.constant 71 : index
132 ! CHECK: %[[VAL_2:.*]] = fir.alloca !fir.char<1,71> {bindc_name = ".chrtmp"}
133 ! CHECK: %[[VAL_3:.*]] = arith.constant 1 : i64
134 ! CHECK: %[[VAL_4:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
135 ! CHECK: %[[VAL_5:.*]] = arith.muli %[[VAL_3]], %[[VAL_4]] : i64
136 ! CHECK: %[[VAL_6:.*]] = arith.constant false
137 ! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_2]] : (!fir.ref<!fir.char<1,71>>) -> !fir.ref<i8>
138 ! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<!fir.char<1,71>>) -> !fir.ref<i8>
139 ! CHECK: fir.call @llvm.memmove.p0.p0.i64(%[[VAL_7]], %[[VAL_8]], %[[VAL_5]], %[[VAL_6]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64, i1) -> ()
140 ! CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_2]] : (!fir.ref<!fir.char<1,71>>) -> !fir.ref<!fir.char<1,?>>
141 ! CHECK: %[[VAL_10:.*]] = fir.emboxchar %[[VAL_9]], %[[VAL_1]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
142 ! CHECK: fir.call @_QPtakes_char_value(%[[VAL_10]]) {{.*}}: (!fir.boxchar<1>) -> ()
143 call takes_char_value("a character string litteral that could be locally modfied by the callee")