1 // Test maxval inlining for both elemental and designate
2 // RUN: fir-opt %s -opt-bufferization | FileCheck %s
4 // subroutine test(array)
5 // integer :: array(:), x
6 // x = minval(abs(array))
9 func.func @_QPtest(%arg0: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "array"}) {
10 %c31_i32 = arith.constant 31 : i32
11 %c0 = arith.constant 0 : index
12 %0 = fir.dummy_scope : !fir.dscope
13 %1:2 = hlfir.declare %arg0 dummy_scope %0 {uniq_name = "_QFtestEarray"} : (!fir.box<!fir.array<?xi32>>, !fir.dscope) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
14 %2 = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtestEx"}
15 %3:2 = hlfir.declare %2 {uniq_name = "_QFtestEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
16 %4:3 = fir.box_dims %1#0, %c0 : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
17 %5 = fir.shape %4#1 : (index) -> !fir.shape<1>
18 %6 = hlfir.elemental %5 unordered : (!fir.shape<1>) -> !hlfir.expr<?xi32> {
20 %8 = hlfir.designate %1#0 (%arg1) : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
21 %9 = fir.load %8 : !fir.ref<i32>
22 %10 = arith.shrsi %9, %c31_i32 : i32
23 %11 = arith.xori %9, %10 : i32
24 %12 = arith.subi %11, %10 : i32
25 hlfir.yield_element %12 : i32
27 %7 = hlfir.minval %6 {fastmath = #arith.fastmath<contract>} : (!hlfir.expr<?xi32>) -> i32
28 hlfir.assign %7 to %3#0 : i32, !fir.ref<i32>
29 hlfir.destroy %6 : !hlfir.expr<?xi32>
33 // CHECK-LABEL: func.func @_QPtest(%arg0: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "array"}) {
34 // CHECK-NEXT: %c1 = arith.constant 1 : index
35 // CHECK-NEXT: %c2147483647_i32 = arith.constant 2147483647 : i32
36 // CHECK-NEXT: %c31_i32 = arith.constant 31 : i32
37 // CHECK-NEXT: %c0 = arith.constant 0 : index
38 // CHECK-NEXT: %[[V0:.*]] = fir.dummy_scope : !fir.dscope
39 // CHECK-NEXT: %[[V1:.*]]:2 = hlfir.declare %arg0 dummy_scope %[[V0]] {uniq_name = "_QFtestEarray"} : (!fir.box<!fir.array<?xi32>>, !fir.dscope) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
40 // CHECK-NEXT: %[[V2:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFtestEx"}
41 // CHECK-NEXT: %[[V3:.*]]:2 = hlfir.declare %[[V2]] {uniq_name = "_QFtestEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
42 // CHECK-NEXT: %[[V4:.*]]:3 = fir.box_dims %[[V1]]#0, %c0 : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
43 // CHECK-NEXT: %[[V5:.*]] = fir.do_loop %arg1 = %c1 to %[[V4]]#1 step %c1 iter_args(%arg2 = %c2147483647_i32) -> (i32) {
44 // CHECK-NEXT: %[[V6:.*]] = hlfir.designate %[[V1]]#0 (%arg1) : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
45 // CHECK-NEXT: %[[V7:.*]] = fir.load %[[V6]] : !fir.ref<i32>
46 // CHECK-NEXT: %[[V8:.*]] = arith.shrsi %[[V7]], %c31_i32 : i32
47 // CHECK-NEXT: %[[V9:.*]] = arith.xori %[[V7]], %[[V8]] : i32
48 // CHECK-NEXT: %[[V10:.*]] = arith.subi %[[V9]], %[[V8]] : i32
49 // CHECK-NEXT: %[[V11:.*]] = arith.cmpi slt, %[[V10]], %arg2 : i32
50 // CHECK-NEXT: %[[V12:.*]] = arith.select %[[V11]], %[[V10]], %arg2 : i32
51 // CHECK-NEXT: fir.result %[[V12]] : i32
53 // CHECK-NEXT: hlfir.assign %[[V5]] to %[[V3]]#0 : i32, !fir.ref<i32>
57 // subroutine test(array)
58 // real :: array(:), x
59 // x = minval(array(3:6))
60 // end subroutine test
62 func.func @_QPtest_float(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "array"}) {
63 %c4 = arith.constant 4 : index
64 %c1 = arith.constant 1 : index
65 %c6 = arith.constant 6 : index
66 %c3 = arith.constant 3 : index
67 %0 = fir.dummy_scope : !fir.dscope
68 %1:2 = hlfir.declare %arg0 dummy_scope %0 {uniq_name = "_QFtestEarray"} : (!fir.box<!fir.array<?xf32>>, !fir.dscope) -> (!fir.box<!fir.array<?xf32>>, !fir.box<!fir.array<?xf32>>)
69 %2 = fir.alloca f32 {bindc_name = "x", uniq_name = "_QFtestEx"}
70 %3:2 = hlfir.declare %2 {uniq_name = "_QFtestEx"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
71 %4 = fir.shape %c4 : (index) -> !fir.shape<1>
72 %5 = hlfir.designate %1#0 (%c3:%c6:%c1) shape %4 : (!fir.box<!fir.array<?xf32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<4xf32>>
73 %6 = hlfir.minval %5 {fastmath = #arith.fastmath<contract>} : (!fir.box<!fir.array<4xf32>>) -> f32
74 hlfir.assign %6 to %3#0 : f32, !fir.ref<f32>
78 // CHECK-LABEL: _QPtest_float
79 // CHECK: %cst = arith.constant 0x7F800000 : f32
80 // CHECK: %[[V4:.*]] = fir.shape %c4 : (index) -> !fir.shape<1>
81 // CHECK-NEXT: %[[V5:.*]] = hlfir.designate %{{.*}} (%c3:%c6:%c1) shape %[[V4]] : (!fir.box<!fir.array<?xf32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<4xf32>>
82 // CHECK-NEXT: %[[V6:.*]] = fir.do_loop %arg1 = %c1 to %c4 step %c1 iter_args(%arg2 = %cst) -> (f32) {
83 // CHECK-NEXT: %[[V7:.*]] = hlfir.designate %[[V5]] (%arg1) : (!fir.box<!fir.array<4xf32>>, index) -> !fir.ref<f32>
84 // CHECK-NEXT: %[[V8:.*]] = fir.load %[[V7]] : !fir.ref<f32>
85 // CHECK-NEXT: %[[V9:.*]] = arith.cmpf olt, %[[V8]], %arg2 : f32
86 // CHECK-NEXT: %[[V10:.*]] = arith.cmpf une, %arg2, %arg2 : f32
87 // CHECK-NEXT: %[[V11:.*]] = arith.cmpf oeq, %[[V8]], %[[V8]] : f32
88 // CHECK-NEXT: %[[V12:.*]] = arith.andi %[[V10]], %[[V11]] : i1
89 // CHECK-NEXT: %[[V13:.*]] = arith.ori %[[V9]], %[[V12]] : i1
90 // CHECK-NEXT: %[[V14:.*]] = arith.select %[[V13]], %[[V8]], %arg2 : f32
91 // CHECK-NEXT: fir.result %[[V14]] : f32
93 // CHECK-NEXT: hlfir.assign %[[V6]] to %3#0 : f32, !fir.ref<f32>