1 // Test code generation to FIR of hlfir.designate operations
2 // without component reference.
3 // RUN: fir-opt %s -convert-hlfir-to-fir | FileCheck %s
5 func.func @array_ref(%arg0: !fir.box<!fir.array<?xf32>>, %arg1: !fir.ref<i64>) {
6 %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)
7 %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box<!fir.array<?xf32>>) -> (!fir.box<!fir.array<?xf32>>, !fir.box<!fir.array<?xf32>>)
8 %2 = fir.load %0#0 : !fir.ref<i64>
9 %3 = hlfir.designate %1#0 (%2) : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
12 // CHECK-LABEL: func.func @array_ref(
13 // CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xf32>>,
14 // CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i64>) {
15 // CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref<i64>) -> !fir.ref<i64>
16 // CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box<!fir.array<?xf32>>) -> !fir.box<!fir.array<?xf32>>
17 // CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_2]] : !fir.ref<i64>
18 // CHECK: %[[VAL_6:.*]] = fir.array_coor %[[VAL_3]] %[[VAL_5]] : (!fir.box<!fir.array<?xf32>>, i64) -> !fir.ref<f32>
21 func.func @char_array_ref(%arg0: !fir.box<!fir.array<?x!fir.char<1,?>>>, %arg1: !fir.ref<i32>) {
22 %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
23 %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> (!fir.box<!fir.array<?x!fir.char<1,?>>>, !fir.box<!fir.array<?x!fir.char<1,?>>>)
24 %2 = fir.box_elesize %1#1 : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> index
25 %c10 = arith.constant 10 : index
26 %3 = hlfir.designate %1#0 (%c10) typeparams %2 : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index, index) -> !fir.boxchar<1>
29 // CHECK-LABEL: func.func @char_array_ref(
30 // CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.char<1,?>>>,
31 // CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32>) {
32 // CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref<i32>) -> !fir.ref<i32>
33 // CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
34 // CHECK: %[[VAL_5:.*]] = fir.box_elesize %[[VAL_3]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> index
35 // CHECK: %[[VAL_6:.*]] = arith.constant 10 : index
36 // CHECK: %[[VAL_7:.*]] = fir.array_coor %[[VAL_3]] %[[VAL_6]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index) -> !fir.ref<!fir.char<1,?>>
37 // CHECK: %[[VAL_8:.*]] = fir.emboxchar %[[VAL_7]], %[[VAL_5]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
40 func.func @char_array_ref_cst_len(%arg0: !fir.box<!fir.array<?x!fir.char<1,5>>>, %arg1: !fir.ref<i32>) {
41 %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
42 %c5 = arith.constant 5 : index
43 %1:2 = hlfir.declare %arg0 typeparams %c5 {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index) -> (!fir.box<!fir.array<?x!fir.char<1,5>>>, !fir.box<!fir.array<?x!fir.char<1,5>>>)
44 %c10 = arith.constant 10 : index
45 %2 = hlfir.designate %1#0 (%c10) typeparams %c5 : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index, index) -> !fir.ref<!fir.char<1,5>>
48 // CHECK-LABEL: func.func @char_array_ref_cst_len(
49 // CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.char<1,5>>>,
50 // CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32>) {
51 // CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref<i32>) -> !fir.ref<i32>
52 // CHECK: %[[VAL_3:.*]] = arith.constant 5 : index
53 // CHECK: %[[VAL_4:.*]] = fir.declare %[[VAL_0]] typeparams %[[VAL_3]] {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index) -> !fir.box<!fir.array<?x!fir.char<1,5>>>
54 // CHECK: %[[VAL_6:.*]] = arith.constant 10 : index
55 // CHECK: %[[VAL_7:.*]] = fir.array_coor %[[VAL_4]] %[[VAL_6]] : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index) -> !fir.ref<!fir.char<1,5>>
58 func.func @char_array_ref_3(%arg0: !fir.ref<!fir.array<100x!fir.char<1,?>>>, %arg1: !fir.ref<i32>) {
59 %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
60 %c5 = arith.constant 5 : index
61 %c100 = arith.constant 100 : index
62 %sh = fir.shape %c100 : (index) -> !fir.shape<1>
63 %1:2 = hlfir.declare %arg0(%sh) typeparams %c5 {uniq_name = "x"} : (!fir.ref<!fir.array<100x!fir.char<1,?>>>, !fir.shape<1>, index) -> (!fir.box<!fir.array<100x!fir.char<1,?>>>, !fir.ref<!fir.array<100x!fir.char<1,?>>>)
64 %c10 = arith.constant 10 : index
65 %3 = hlfir.designate %1#0 (%c10) typeparams %c5 : (!fir.box<!fir.array<100x!fir.char<1,?>>>, index, index) -> !fir.boxchar<1>
68 // CHECK-LABEL: func.func @char_array_ref_3(
69 // CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<100x!fir.char<1,?>>>,
70 // CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32>) {
71 // CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref<i32>) -> !fir.ref<i32>
72 // CHECK: %[[VAL_3:.*]] = arith.constant 5 : index
73 // CHECK: %[[VAL_4:.*]] = arith.constant 100 : index
74 // CHECK: %[[VAL_5:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1>
75 // CHECK: %[[VAL_6:.*]] = fir.declare %[[VAL_0]](%[[VAL_5]]) typeparams %[[VAL_3]] {uniq_name = "x"} : (!fir.ref<!fir.array<100x!fir.char<1,?>>>, !fir.shape<1>, index) -> !fir.ref<!fir.array<100x!fir.char<1,?>>>
76 // CHECK: %[[VAL_8:.*]] = arith.constant 10 : index
77 // CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_6]](%[[VAL_5]]) %[[VAL_8]] typeparams %[[VAL_3]] : (!fir.ref<!fir.array<100x!fir.char<1,?>>>, !fir.shape<1>, index, index) -> !fir.ref<!fir.char<1,?>>
78 // CHECK: %[[VAL_10:.*]] = fir.emboxchar %[[VAL_9]], %[[VAL_3]] : (!fir.ref<!fir.char<1,?>>, index) -> !fir.boxchar<1>
81 func.func @array_section(%arg0: !fir.ref<!fir.array<10xf32>>) {
82 %c10 = arith.constant 10 : index
83 %0 = fir.shape %c10 : (index) -> !fir.shape<1>
84 %1:2 = hlfir.declare %arg0(%0) {uniq_name = "x"} : (!fir.ref<!fir.array<10xf32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xf32>>, !fir.ref<!fir.array<10xf32>>)
85 %c2 = arith.constant 2 : index
86 %c8 = arith.constant 8 : index
87 %c3 = arith.constant 3 : index
88 %2 = fir.shape %c3 : (index) -> !fir.shape<1>
89 %3 = hlfir.designate %1#0 (%c2:%c8:%c3) shape %2 : (!fir.ref<!fir.array<10xf32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<3xf32>>
92 // CHECK-LABEL: func.func @array_section(
93 // CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<10xf32>>) {
94 // CHECK: %[[VAL_1:.*]] = arith.constant 10 : index
95 // CHECK: %[[VAL_2:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1>
96 // CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]](%[[VAL_2]]) {uniq_name = "x"} : (!fir.ref<!fir.array<10xf32>>, !fir.shape<1>) -> !fir.ref<!fir.array<10xf32>>
97 // CHECK: %[[VAL_4:.*]] = arith.constant 2 : index
98 // CHECK: %[[VAL_5:.*]] = arith.constant 8 : index
99 // CHECK: %[[VAL_6:.*]] = arith.constant 3 : index
100 // CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_6]] : (index) -> !fir.shape<1>
101 // CHECK: %[[VAL_8:.*]] = fir.undefined index
102 // CHECK: %[[VAL_9:.*]] = fir.slice %[[VAL_4]], %[[VAL_5]], %[[VAL_6]] : (index, index, index) -> !fir.slice<1>
103 // CHECK: %[[VAL_10:.*]] = fir.embox %[[VAL_3]](%[[VAL_2]]) {{\[}}%[[VAL_9]]] : (!fir.ref<!fir.array<10xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<3xf32>>
106 func.func @array_section_2(%arg0: !fir.box<!fir.array<?xf32>>, %arg1: !fir.ref<i64>) {
107 %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref<i64>) -> (!fir.ref<i64>, !fir.ref<i64>)
108 %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box<!fir.array<?xf32>>) -> (!fir.box<!fir.array<?xf32>>, !fir.box<!fir.array<?xf32>>)
109 %2 = fir.load %0#0 : !fir.ref<i64>
110 %c0 = arith.constant 0 : index
111 %3:3 = fir.box_dims %1#1, %c0 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
112 %4 = fir.convert %2 : (i64) -> index
113 %c3 = arith.constant 3 : index
114 %c42 = arith.constant 42 : index
115 %5 = fir.shape %c42 : (index) -> !fir.shape<1>
116 %6 = hlfir.designate %1#0 (%4:%3#1:%c3) shape %5 : (!fir.box<!fir.array<?xf32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xf32>>
119 // CHECK-LABEL: func.func @array_section_2(
120 // CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xf32>>,
121 // CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i64>) {
122 // CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref<i64>) -> !fir.ref<i64>
123 // CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box<!fir.array<?xf32>>) -> !fir.box<!fir.array<?xf32>>
124 // CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_2]] : !fir.ref<i64>
125 // CHECK: %[[VAL_6:.*]] = arith.constant 0 : index
126 // CHECK: %[[VAL_7:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_6]] : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
127 // CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_5]] : (i64) -> index
128 // CHECK: %[[VAL_9:.*]] = arith.constant 3 : index
129 // CHECK: %[[VAL_10:.*]] = arith.constant 42 : index
130 // CHECK: %[[VAL_11:.*]] = fir.shape %[[VAL_10]] : (index) -> !fir.shape<1>
131 // CHECK: %[[VAL_12:.*]] = fir.undefined index
132 // CHECK: %[[VAL_13:.*]] = fir.slice %[[VAL_8]], %[[VAL_7]]#1, %[[VAL_9]] : (index, index, index) -> !fir.slice<1>
133 // CHECK: %[[VAL_14:.*]] = fir.rebox %[[VAL_3]] {{\[}}%[[VAL_13]]] : (!fir.box<!fir.array<?xf32>>, !fir.slice<1>) -> !fir.box<!fir.array<?xf32>>
136 func.func @char_array_section(%arg0: !fir.box<!fir.array<?x!fir.char<1,?>>>, %arg1: !fir.ref<i32>) {
137 %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
138 %1:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> (!fir.box<!fir.array<?x!fir.char<1,?>>>, !fir.box<!fir.array<?x!fir.char<1,?>>>)
139 %2 = fir.box_elesize %1#1 : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> index
140 %c1 = arith.constant 1 : index
141 %c0 = arith.constant 0 : index
142 %3:3 = fir.box_dims %1#1, %c0 : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index) -> (index, index, index)
143 %c3 = arith.constant 3 : index
144 %c42 = arith.constant 42 : index
145 %4 = fir.shape %c42 : (index) -> !fir.shape<1>
146 %5 = hlfir.designate %1#0 (%c1:%3#1:%c3) shape %4 typeparams %2 : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index, index, index, !fir.shape<1>, index) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
149 // CHECK-LABEL: func.func @char_array_section(
150 // CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.char<1,?>>>,
151 // CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32>) {
152 // CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref<i32>) -> !fir.ref<i32>
153 // CHECK: %[[VAL_3:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
154 // CHECK: %[[VAL_5:.*]] = fir.box_elesize %[[VAL_3]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>) -> index
155 // CHECK: %[[VAL_6:.*]] = arith.constant 1 : index
156 // CHECK: %[[VAL_7:.*]] = arith.constant 0 : index
157 // CHECK: %[[VAL_8:.*]]:3 = fir.box_dims %[[VAL_3]], %[[VAL_7]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, index) -> (index, index, index)
158 // CHECK: %[[VAL_9:.*]] = arith.constant 3 : index
159 // CHECK: %[[VAL_10:.*]] = arith.constant 42 : index
160 // CHECK: %[[VAL_11:.*]] = fir.shape %[[VAL_10]] : (index) -> !fir.shape<1>
161 // CHECK: %[[VAL_12:.*]] = fir.undefined index
162 // CHECK: %[[VAL_13:.*]] = fir.slice %[[VAL_6]], %[[VAL_8]]#1, %[[VAL_9]] : (index, index, index) -> !fir.slice<1>
163 // CHECK: %[[VAL_14:.*]] = fir.rebox %[[VAL_3]] {{\[}}%[[VAL_13]]] : (!fir.box<!fir.array<?x!fir.char<1,?>>>, !fir.slice<1>) -> !fir.box<!fir.array<?x!fir.char<1,?>>>
166 func.func @char_array_section_cst_len(%arg0: !fir.box<!fir.array<?x!fir.char<1,5>>>, %arg1: !fir.ref<i32>) {
167 %0:2 = hlfir.declare %arg1 {uniq_name = "n"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
168 %c5 = arith.constant 5 : index
169 %1:2 = hlfir.declare %arg0 typeparams %c5 {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index) -> (!fir.box<!fir.array<?x!fir.char<1,5>>>, !fir.box<!fir.array<?x!fir.char<1,5>>>)
170 %c1 = arith.constant 1 : index
171 %c0 = arith.constant 0 : index
172 %2:3 = fir.box_dims %1#1, %c0 : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index) -> (index, index, index)
173 %c3 = arith.constant 3 : index
174 %c42 = arith.constant 42 : index
175 %3 = fir.shape %c42 : (index) -> !fir.shape<1>
176 %4 = hlfir.designate %1#0 (%c1:%2#1:%c3) shape %3 typeparams %c5 : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index, index, index, !fir.shape<1>, index) -> !fir.box<!fir.array<?x!fir.char<1,5>>>
179 // CHECK-LABEL: func.func @char_array_section_cst_len(
180 // CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?x!fir.char<1,5>>>,
181 // CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32>) {
182 // CHECK: %[[VAL_2:.*]] = fir.declare %[[VAL_1]] {uniq_name = "n"} : (!fir.ref<i32>) -> !fir.ref<i32>
183 // CHECK: %[[VAL_3:.*]] = arith.constant 5 : index
184 // CHECK: %[[VAL_4:.*]] = fir.declare %[[VAL_0]] typeparams %[[VAL_3]] {uniq_name = "x"} : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index) -> !fir.box<!fir.array<?x!fir.char<1,5>>>
185 // CHECK: %[[VAL_6:.*]] = arith.constant 1 : index
186 // CHECK: %[[VAL_7:.*]] = arith.constant 0 : index
187 // CHECK: %[[VAL_8:.*]]:3 = fir.box_dims %[[VAL_4]], %[[VAL_7]] : (!fir.box<!fir.array<?x!fir.char<1,5>>>, index) -> (index, index, index)
188 // CHECK: %[[VAL_9:.*]] = arith.constant 3 : index
189 // CHECK: %[[VAL_10:.*]] = arith.constant 42 : index
190 // CHECK: %[[VAL_11:.*]] = fir.shape %[[VAL_10]] : (index) -> !fir.shape<1>
191 // CHECK: %[[VAL_12:.*]] = fir.undefined index
192 // CHECK: %[[VAL_13:.*]] = fir.slice %[[VAL_6]], %[[VAL_8]]#1, %[[VAL_9]] : (index, index, index) -> !fir.slice<1>
193 // CHECK: %[[VAL_14:.*]] = fir.rebox %[[VAL_4]] {{\[}}%[[VAL_13]]] : (!fir.box<!fir.array<?x!fir.char<1,5>>>, !fir.slice<1>) -> !fir.box<!fir.array<?x!fir.char<1,5>>>