xref: /llvm-project/flang/test/Fir/array-modify.fir (revision 59b18b5c91ec655189c73a123cb5204062601671)
1// Test array-copy-value pass (copy elision) with fir.array_modify
2// RUN: fir-opt %s --array-value-copy | FileCheck %s
3// RUN: fir-opt %s --array-value-copy="optimize-conflicts=true" | FileCheck %s
4
5// Test user_defined_assignment(arg0(:), arg1(:))
6func.func @no_overlap(%arg0: !fir.ref<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>>) {
7  %c100 = arith.constant 100 : index
8  %c99 = arith.constant 99 : index
9  %c1 = arith.constant 1 : index
10  %c0 = arith.constant 0 : index
11  %0 = fir.alloca f32
12  %1 = fir.shape %c100 : (index) -> !fir.shape<1>
13  %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
14  %3 = fir.array_load %arg1(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
15  %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %2) -> (!fir.array<100xf32>) {
16    %5 = fir.array_fetch %3, %arg2 : (!fir.array<100xf32>, index) -> f32
17    %6:2 = fir.array_modify %arg3, %arg2 : (!fir.array<100xf32>, index) -> (!fir.ref<f32>, !fir.array<100xf32>)
18    fir.store %5 to %0 : !fir.ref<f32>
19    fir.call @user_defined_assignment(%6#0, %0) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
20    fir.result %6#1 : !fir.array<100xf32>
21  }
22  fir.array_merge_store %2, %4 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
23  return
24}
25// CHECK-LABEL:   func @no_overlap(
26// CHECK-SAME:                     %[[VAL_0:.*]]: !fir.ref<!fir.array<100xf32>>,
27// CHECK-SAME:                     %[[VAL_1:.*]]: !fir.ref<!fir.array<100xf32>>) {
28// CHECK-DAG:           %[[VAL_2:.*]] = arith.constant 100 : index
29// CHECK-DAG:           %[[VAL_3:.*]] = arith.constant 99 : index
30// CHECK-DAG:           %[[VAL_4:.*]] = arith.constant 1 : index
31// CHECK-DAG:           %[[VAL_5:.*]] = arith.constant 0 : index
32// CHECK:           %[[VAL_6:.*]] = fir.alloca f32
33// CHECK:           %[[VAL_7:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
34// CHECK:           %[[VAL_8:.*]] = fir.undefined !fir.array<100xf32>
35// CHECK:           %[[VAL_9:.*]] = fir.undefined !fir.array<100xf32>
36// CHECK:           %[[VAL_10:.*]] = fir.do_loop %[[VAL_11:.*]] = %[[VAL_5]] to %[[VAL_3]] step %[[VAL_4]] unordered iter_args(%[[VAL_12:.*]] = %[[VAL_8]]) -> (!fir.array<100xf32>) {
37// CHECK:             %[[VAL_13:.*]] = arith.constant 1 : index
38// CHECK:             %[[VAL_14:.*]] = arith.addi %[[VAL_11]], %[[VAL_13]] : index
39// CHECK:             %[[VAL_15:.*]] = fir.array_coor %[[VAL_1]](%[[VAL_7]]) %[[VAL_14]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
40// CHECK:             %[[VAL_16:.*]] = fir.load %[[VAL_15]] : !fir.ref<f32>
41// CHECK:             %[[VAL_17:.*]] = arith.constant 1 : index
42// CHECK:             %[[VAL_18:.*]] = arith.addi %[[VAL_11]], %[[VAL_17]] : index
43// CHECK:             %[[VAL_19:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_18]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
44// CHECK:             fir.store %[[VAL_16]] to %[[VAL_6]] : !fir.ref<f32>
45// CHECK:             fir.call @user_defined_assignment(%[[VAL_19]], %[[VAL_6]]) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
46// CHECK:             fir.result %[[VAL_8]] : !fir.array<100xf32>
47// CHECK:           }
48// CHECK:           return
49// CHECK:         }
50
51
52// Test user_defined_assignment(arg0(:), arg0(100:1:-1))
53func.func @overlap(%arg0: !fir.ref<!fir.array<100xf32>>) {
54  %c100 = arith.constant 100 : index
55  %c99 = arith.constant 99 : index
56  %c1 = arith.constant 1 : index
57  %c-1 = arith.constant -1 : index
58  %c0 = arith.constant 0 : index
59  %0 = fir.alloca f32
60  %1 = fir.shape %c100 : (index) -> !fir.shape<1>
61  %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
62  %3 = fir.slice %c100, %c1, %c-1 : (index, index, index) -> !fir.slice<1>
63  %4 = fir.array_load %arg0(%1) [%3] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<100xf32>
64  %5 = fir.do_loop %arg1 = %c0 to %c99 step %c1 unordered iter_args(%arg2 = %2) -> (!fir.array<100xf32>) {
65    %6 = fir.array_fetch %4, %arg1 : (!fir.array<100xf32>, index) -> f32
66    %7:2 = fir.array_modify %arg2, %arg1 : (!fir.array<100xf32>, index) -> (!fir.ref<f32>, !fir.array<100xf32>)
67    fir.store %6 to %0 : !fir.ref<f32>
68    fir.call @user_defined_assignment(%7#0, %0) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
69    fir.result %7#1 : !fir.array<100xf32>
70  }
71  fir.array_merge_store %2, %5 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
72  return
73}
74// CHECK-LABEL:   func @overlap(
75// CHECK-SAME:                  %[[VAL_0:.*]]: !fir.ref<!fir.array<100xf32>>) {
76// CHECK-DAG:           %[[VAL_1:.*]] = arith.constant 100 : index
77// CHECK-DAG:           %[[VAL_2:.*]] = arith.constant 99 : index
78// CHECK-DAG:           %[[VAL_3:.*]] = arith.constant 1 : index
79// CHECK-DAG:           %[[VAL_4:.*]] = arith.constant -1 : index
80// CHECK-DAG:           %[[VAL_5:.*]] = arith.constant 0 : index
81// CHECK:           %[[VAL_6:.*]] = fir.alloca f32
82// CHECK:           %[[VAL_7:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1>
83// CHECK:           %[[VAL_8:.*]] = fir.allocmem !fir.array<100xf32>{{$}}
84// CHECK:           %[[VAL_9:.*]] = fir.convert %[[VAL_1]] : (index) -> index
85// CHECK:           %[[VAL_10:.*]] = arith.constant 0 : index
86// CHECK:           %[[VAL_11:.*]] = arith.constant 1 : index
87// CHECK:           %[[VAL_12:.*]] = arith.subi %[[VAL_9]], %[[VAL_11]] : index
88// CHECK:           fir.do_loop %[[VAL_13:.*]] = %[[VAL_10]] to %[[VAL_12]] step %[[VAL_11]] {
89// CHECK:             %[[VAL_14:.*]] = arith.constant 1 : index
90// CHECK:             %[[VAL_15:.*]] = arith.addi %[[VAL_13]], %[[VAL_14]] : index
91// CHECK:             %[[VAL_16:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_15]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
92// CHECK:             %[[VAL_18:.*]] = arith.constant 1 : index
93// CHECK:             %[[VAL_19:.*]] = arith.addi %[[VAL_13]], %[[VAL_18]] : index
94// CHECK:             %[[VAL_20:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_7]]) %[[VAL_19]] : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
95// CHECK:             %[[VAL_17:.*]] = fir.load %[[VAL_16]] : !fir.ref<f32>
96// CHECK:             fir.store %[[VAL_17]] to %[[VAL_20]] : !fir.ref<f32>
97// CHECK:           }
98// CHECK:           %[[VAL_21:.*]] = fir.undefined !fir.array<100xf32>
99// CHECK:           %[[VAL_22:.*]] = fir.slice %[[VAL_1]], %[[VAL_3]], %[[VAL_4]] : (index, index, index) -> !fir.slice<1>
100// CHECK:           %[[VAL_23:.*]] = fir.undefined !fir.array<100xf32>
101// CHECK:           %[[VAL_24:.*]] = fir.do_loop %[[VAL_25:.*]] = %[[VAL_5]] to %[[VAL_2]] step %[[VAL_3]] unordered iter_args(%[[VAL_26:.*]] = %[[VAL_21]]) -> (!fir.array<100xf32>) {
102// CHECK:             %[[VAL_27:.*]] = arith.constant 1 : index
103// CHECK:             %[[VAL_28:.*]] = arith.addi %[[VAL_25]], %[[VAL_27]] : index
104// CHECK:             %[[VAL_29:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) {{\[}}%[[VAL_22]]] %[[VAL_28]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
105// CHECK:             %[[VAL_30:.*]] = fir.load %[[VAL_29]] : !fir.ref<f32>
106// CHECK:             %[[VAL_31:.*]] = arith.constant 1 : index
107// CHECK:             %[[VAL_32:.*]] = arith.addi %[[VAL_25]], %[[VAL_31]] : index
108// CHECK:             %[[VAL_33:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_7]]) %[[VAL_32]] : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
109// CHECK:             fir.store %[[VAL_30]] to %[[VAL_6]] : !fir.ref<f32>
110// CHECK:             fir.call @user_defined_assignment(%[[VAL_33]], %[[VAL_6]]) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
111// CHECK:             fir.result %[[VAL_21]] : !fir.array<100xf32>
112// CHECK:           }
113// CHECK:           %[[VAL_34:.*]] = fir.convert %[[VAL_1]] : (index) -> index
114// CHECK:           %[[VAL_35:.*]] = arith.constant 0 : index
115// CHECK:           %[[VAL_36:.*]] = arith.constant 1 : index
116// CHECK:           %[[VAL_37:.*]] = arith.subi %[[VAL_34]], %[[VAL_36]] : index
117// CHECK:           fir.do_loop %[[VAL_38:.*]] = %[[VAL_35]] to %[[VAL_37]] step %[[VAL_36]] {
118// CHECK:             %[[VAL_39:.*]] = arith.constant 1 : index
119// CHECK:             %[[VAL_40:.*]] = arith.addi %[[VAL_38]], %[[VAL_39]] : index
120// CHECK:             %[[VAL_41:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_7]]) %[[VAL_40]] : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
121// CHECK:             %[[VAL_43:.*]] = arith.constant 1 : index
122// CHECK:             %[[VAL_44:.*]] = arith.addi %[[VAL_38]], %[[VAL_43]] : index
123// CHECK:             %[[VAL_45:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_44]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
124// CHECK:             %[[VAL_42:.*]] = fir.load %[[VAL_41]] : !fir.ref<f32>
125// CHECK:             fir.store %[[VAL_42]] to %[[VAL_45]] : !fir.ref<f32>
126// CHECK:           }
127// CHECK:           fir.freemem %[[VAL_8]] : !fir.heap<!fir.array<100xf32>>
128// CHECK:           return
129// CHECK:         }
130
131func.func private @user_defined_assignment(!fir.ref<f32>, !fir.ref<f32>)
132