1 ! RUN: bbc -emit-fir %s -o - | FileCheck %s
2 ! RUN: flang-new -fc1 -fdefault-integer-8 -emit-fir %s -o - | FileCheck %s
4 ! Test OPTIONAL lowering on caller/callee and PRESENT intrinsic.
8 real, allocatable
:: p(:)
12 ! Test simple scalar optional
13 ! CHECK-LABEL: func @_QMoptPintrinsic_scalar(
14 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<f32> {fir.bindc_name = "x", fir.optional}) {
15 subroutine intrinsic_scalar(x
)
17 ! CHECK: fir.is_present %[[arg0]] : (!fir.ref<f32>) -> i1
20 ! CHECK-LABEL: @_QMoptPcall_intrinsic_scalar()
21 subroutine call_intrinsic_scalar()
22 ! CHECK: %[[x:.*]] = fir.alloca f32
24 ! CHECK: fir.call @_QMoptPintrinsic_scalar(%[[x]]) {{.*}}: (!fir.ref<f32>) -> ()
25 call intrinsic_scalar(x
)
26 ! CHECK: %[[absent:.*]] = fir.absent !fir.ref<f32>
27 ! CHECK: fir.call @_QMoptPintrinsic_scalar(%[[absent]]) {{.*}}: (!fir.ref<f32>) -> ()
28 call intrinsic_scalar()
31 ! Test explicit shape array optional
32 ! CHECK-LABEL: func @_QMoptPintrinsic_f77_array(
33 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.array<100xf32>> {fir.bindc_name = "x", fir.optional}) {
34 subroutine intrinsic_f77_array(x
)
35 real, optional
:: x(100)
36 ! CHECK: fir.is_present %[[arg0]] : (!fir.ref<!fir.array<100xf32>>) -> i1
39 ! CHECK-LABEL: func @_QMoptPcall_intrinsic_f77_array()
40 subroutine call_intrinsic_f77_array()
41 ! CHECK: %[[x:.*]] = fir.alloca !fir.array<100xf32>
43 ! CHECK: fir.call @_QMoptPintrinsic_f77_array(%[[x]]) {{.*}}: (!fir.ref<!fir.array<100xf32>>) -> ()
44 call intrinsic_f77_array(x
)
45 ! CHECK: %[[absent:.*]] = fir.absent !fir.ref<!fir.array<100xf32>>
46 ! CHECK: fir.call @_QMoptPintrinsic_f77_array(%[[absent]]) {{.*}}: (!fir.ref<!fir.array<100xf32>>) -> ()
47 call intrinsic_f77_array()
50 ! Test optional character scalar
51 ! CHECK-LABEL: func @_QMoptPcharacter_scalar(
52 ! CHECK-SAME: %[[arg0:.*]]: !fir.boxchar<1> {fir.bindc_name = "x", fir.optional}) {
53 subroutine character_scalar(x
)
54 ! CHECK: %[[unboxed:.*]]:2 = fir.unboxchar %[[arg0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
55 ! CHECK: %[[ref:.*]] = fir.convert %[[unboxed]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.char<1,10>>
56 character(10), optional
:: x
57 ! CHECK: fir.is_present %[[ref]] : (!fir.ref<!fir.char<1,10>>) -> i1
60 ! CHECK-LABEL: func @_QMoptPcall_character_scalar()
61 subroutine call_character_scalar()
62 ! CHECK: %[[addr:.*]] = fir.alloca !fir.char<1,10>
64 ! CHECK: %[[addrCast:.*]] = fir.convert %[[addr]]
65 ! CHECK: %[[x:.*]] = fir.emboxchar %[[addrCast]], {{.*}}
66 ! CHECK: fir.call @_QMoptPcharacter_scalar(%[[x]]) {{.*}}: (!fir.boxchar<1>) -> ()
67 call character_scalar(x
)
68 ! CHECK: %[[absent:.*]] = fir.absent !fir.boxchar<1>
69 ! CHECK: fir.call @_QMoptPcharacter_scalar(%[[absent]]) {{.*}}: (!fir.boxchar<1>) -> ()
70 call character_scalar()
73 ! Test optional character function
74 ! CHECK-LABEL: func @_QMoptPchar_proc(
75 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.char<1,3>>,
76 character(len
=3) function char_proc(i
)
80 ! CHECK-LABEL: func @_QMoptPuse_char_proc(
81 ! CHECK-SAME: %[[arg0:.*]]: tuple<!fir.boxproc<() -> ()>, i64> {fir.char_proc},
82 subroutine use_char_proc(f
, c
)
85 character(len
=3) function f(i
)
90 ! CHECK: %[[boxProc:.*]] = fir.extract_value %[[arg0]], [0 : index] : (tuple<!fir.boxproc<() -> ()>, i64>) -> !fir.boxproc<() -> ()>
91 ! CHECK: %[[procAddr:.*]] = fir.box_addr %[[boxProc]] : (!fir.boxproc<() -> ()>) -> (() -> ())
92 ! CHECK: %{{.*}} = fir.is_present %[[procAddr]] : (() -> ()) -> i1
99 ! CHECK-LABEL: func @_QMoptPcall_use_char_proc(
100 subroutine call_use_char_proc()
101 character(len
=3) :: c
102 ! CHECK: %[[boxProc:.*]] = fir.absent !fir.boxproc<() -> ()>
103 ! CHECK: %[[undef:.*]] = fir.undefined index
104 ! CHECK: %[[charLen:.*]] = fir.convert %[[undef]] : (index) -> i64
105 ! CHECK: %[[tuple:.*]] = fir.undefined tuple<!fir.boxproc<() -> ()>, i64>
106 ! CHECK: %[[tuple2:.*]] = fir.insert_value %[[tuple]], %[[boxProc]], [0 : index] : (tuple<!fir.boxproc<() -> ()>, i64>, !fir.boxproc<() -> ()>) -> tuple<!fir.boxproc<() -> ()>, i64>
107 ! CHECK: %[[tuple3:.*]] = fir.insert_value %[[tuple2]], %[[charLen]], [1 : index] : (tuple<!fir.boxproc<() -> ()>, i64>, i64) -> tuple<!fir.boxproc<() -> ()>, i64>
108 ! CHECK: fir.call @_QMoptPuse_char_proc(%[[tuple3]], %{{.*}}){{.*}} : (tuple<!fir.boxproc<() -> ()>, i64>, !fir.boxchar<1>) -> ()
109 call use_char_proc(c
=c
)
110 ! CHECK: %[[funcAddr:.*]] = fir.address_of(@_QMoptPchar_proc) : (!fir.ref<!fir.char<1,3>>, index, {{.*}}) -> !fir.boxchar<1>
111 ! CHECK: %[[c3:.*]] = arith.constant 3 : i64
112 ! CHECK: %[[boxProc2:.*]] = fir.emboxproc %[[funcAddr]] : ((!fir.ref<!fir.char<1,3>>, index, {{.*}}) -> !fir.boxchar<1>) -> !fir.boxproc<() -> ()>
113 ! CHECK: %[[tuple4:.*]] = fir.undefined tuple<!fir.boxproc<() -> ()>, i64>
114 ! CHECK: %[[tuple5:.*]] = fir.insert_value %[[tuple4]], %[[boxProc2]], [0 : index] : (tuple<!fir.boxproc<() -> ()>, i64>, !fir.boxproc<() -> ()>) -> tuple<!fir.boxproc<() -> ()>, i64>
115 ! CHECK: %[[tuple6:.*]] = fir.insert_value %[[tuple5]], %[[c3]], [1 : index] : (tuple<!fir.boxproc<() -> ()>, i64>, i64) -> tuple<!fir.boxproc<() -> ()>, i64>
116 ! CHECK: fir.call @_QMoptPuse_char_proc(%[[tuple6]], {{.*}}){{.*}} : (tuple<!fir.boxproc<() -> ()>, i64>, !fir.boxchar<1>) -> ()
117 call use_char_proc(char_proc
, c
)
120 ! Test optional assumed shape
121 ! CHECK-LABEL: func @_QMoptPassumed_shape(
122 ! CHECK-SAME: %[[arg0:.*]]: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "x", fir.optional}) {
123 subroutine assumed_shape(x
)
124 real, optional
:: x(:)
125 ! CHECK: fir.is_present %[[arg0]] : (!fir.box<!fir.array<?xf32>>) -> i1
128 ! CHECK: func @_QMoptPcall_assumed_shape()
129 subroutine call_assumed_shape()
130 ! CHECK: %[[addr:.*]] = fir.alloca !fir.array<100xf32>
132 ! CHECK: %[[embox:.*]] = fir.embox %[[addr]]
133 ! CHECK: %[[x:.*]] = fir.convert %[[embox]] : (!fir.box<!fir.array<100xf32>>) -> !fir.box<!fir.array<?xf32>>
134 ! CHECK: fir.call @_QMoptPassumed_shape(%[[x]]) {{.*}}: (!fir.box<!fir.array<?xf32>>) -> ()
135 call assumed_shape(x
)
136 ! CHECK: %[[absent:.*]] = fir.absent !fir.box<!fir.array<?xf32>>
137 ! CHECK: fir.call @_QMoptPassumed_shape(%[[absent]]) {{.*}}: (!fir.box<!fir.array<?xf32>>) -> ()
141 ! Test optional allocatable
142 ! CHECK: func @_QMoptPallocatable_array(
143 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>> {fir.bindc_name = "x", fir.optional}) {
144 subroutine allocatable_array(x
)
145 real, allocatable
, optional
:: x(:)
146 ! CHECK: fir.is_present %[[arg0]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> i1
149 ! CHECK: func @_QMoptPcall_allocatable_array()
150 subroutine call_allocatable_array()
151 ! CHECK: %[[x:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?xf32>>>
152 real, allocatable
:: x(:)
153 ! CHECK: fir.call @_QMoptPallocatable_array(%[[x]]) {{.*}}: (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> ()
154 call allocatable_array(x
)
155 ! CHECK: %[[absent:.*]] = fir.absent !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
156 ! CHECK: fir.call @_QMoptPallocatable_array(%[[absent]]) {{.*}}: (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>) -> ()
157 call allocatable_array()
160 ! CHECK: func @_QMoptPallocatable_to_assumed_optional_array(
161 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>{{.*}}) {
162 subroutine allocatable_to_assumed_optional_array(x
)
163 real, allocatable
:: x(:)
165 ! CHECK: %[[xboxload:.*]] = fir.load %[[arg0]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf32>>>>
166 ! CHECK: %[[xptr:.*]] = fir.box_addr %[[xboxload]] : (!fir.box<!fir.heap<!fir.array<?xf32>>>) -> !fir.heap<!fir.array<?xf32>>
167 ! CHECK: %[[xaddr:.*]] = fir.convert %[[xptr]] : (!fir.heap<!fir.array<?xf32>>) -> i64
168 ! CHECK: %[[isAlloc:.*]] = arith.cmpi ne, %[[xaddr]], %c0{{.*}} : i64
169 ! CHECK: %[[absent:.*]] = fir.absent !fir.box<!fir.array<?xf32>>
170 ! CHECK: %[[embox:.*]] = fir.embox %{{.*}}
171 ! CHECK: %[[actual:.*]] = arith.select %[[isAlloc]], %[[embox]], %[[absent]] : !fir.box<!fir.array<?xf32>>
172 ! CHECK: fir.call @_QMoptPassumed_shape(%[[actual]]) {{.*}}: (!fir.box<!fir.array<?xf32>>) -> ()
173 call assumed_shape(x
)
176 ! CHECK-LABEL: func @_QMoptPalloc_component_to_optional_assumed_shape(
177 subroutine alloc_component_to_optional_assumed_shape(x
)
179 ! CHECK-DAG: %[[isAlloc:.*]] = arith.cmpi ne
180 ! CHECK-DAG: %[[absent:.*]] = fir.absent !fir.box<!fir.array<?xf32>>
181 ! CHECK: %[[select:.*]] = arith.select %[[isAlloc]], %{{.*}}, %[[absent]] : !fir.box<!fir.array<?xf32>>
182 ! CHECK: fir.call @_QMoptPassumed_shape(%[[select]])
183 call assumed_shape(x(55)%p
)
186 ! CHECK-LABEL: func @_QMoptPalloc_component_eval_only_once(
187 subroutine alloc_component_eval_only_once(x
)
188 integer, external :: ifoo
190 ! Verify that the index in the component reference are not evaluated twice
191 ! because if the optional handling logic.
192 ! CHECK: fir.call @_QPifoo()
193 ! CHECK-NOT: fir.call @_QPifoo()
194 call assumed_shape(x(ifoo())%p
)
197 ! CHECK-LABEL: func @_QMoptPnull_as_optional() {
198 subroutine null_as_optional
199 ! CHECK: %[[null_ptr:.*]] = fir.alloca !fir.box<!fir.ptr<none>>
200 ! CHECK: %[[null:.*]] = fir.zero_bits !fir.ptr<none>
201 ! CHECK: %[[null_box:.*]] = fir.embox %[[null]] : (!fir.ptr<none>) -> !fir.box<!fir.ptr<none>>
202 ! CHECK: fir.store %[[null_box]] to %[[null_ptr]] : !fir.ref<!fir.box<!fir.ptr<none>>>
203 ! CHECK: fir.call @_QMoptPassumed_shape(%{{.*}}) {{.*}}: (!fir.box<!fir.array<?xf32>>) -> ()
204 call assumed_shape(null())
205 end subroutine null_as_optional