1 // Test code generation of hlfir.where/hflir.elsewhere when an
2 // "impure" mask is used and several runs are needed. The mask
3 // must be saved so that the impure function is only evaluated once.
4 // RUN: fir-opt %s --lower-hlfir-ordered-assignments | FileCheck %s
6 func.func private @impure() -> !fir.heap<!fir.array<10x!fir.logical<4>>>
7 func.func @test_elsewhere_impure_mask(%x: !fir.ref<!fir.array<10xi32>>, %y: !fir.ref<!fir.array<10xi32>>, %z: !fir.ref<!fir.array<10xi32>>, %mask: !fir.ref<!fir.array<10x!fir.logical<4>>>) {
8 %c-1 = arith.constant -1 : index
9 %c1 = arith.constant 1 : index
10 %c10 = arith.constant 10 : index
11 %1 = fir.shape %c10 : (index) -> !fir.shape<1>
13 hlfir.yield %mask : !fir.ref<!fir.array<10x!fir.logical<4>>>
15 hlfir.elsewhere mask {
16 %mask2 = hlfir.exactly_once : !fir.heap<!fir.array<10x!fir.logical<4>>> {
17 %imp = fir.call @impure() : () -> !fir.heap<!fir.array<10x!fir.logical<4>>>
18 hlfir.yield %imp : !fir.heap<!fir.array<10x!fir.logical<4>>> cleanup {
19 fir.freemem %imp : !fir.heap<!fir.array<10x!fir.logical<4>>>
22 hlfir.yield %mask2 : !fir.heap<!fir.array<10x!fir.logical<4>>>
25 hlfir.yield %y : !fir.ref<!fir.array<10xi32>>
27 hlfir.yield %x : !fir.ref<!fir.array<10xi32>>
30 hlfir.yield %x : !fir.ref<!fir.array<10xi32>>
32 hlfir.yield %z : !fir.ref<!fir.array<10xi32>>
38 // CHECK-LABEL: func.func @test_elsewhere_impure_mask(
39 // CHECK: %[[VAL_12:.*]] = fir.call @impure() : () -> !fir.heap<!fir.array<10x!fir.logical<4>>>
40 // CHECK: %[[VAL_21:.*]] = fir.allocmem !fir.array<?x!fir.logical<4>>, %[[extent:[^ ]*]]
41 // CHECK: %[[VAL_22:.*]] = fir.shape %[[extent]] : (index) -> !fir.shape<1>
42 // CHECK: %[[VAL_23:.*]]:2 = hlfir.declare %[[VAL_21]](%{{.*}}) {uniq_name = ".tmp.where"}
44 // CHECK: fir.if {{.*}} {
46 // CHECK: %[[VAL_28:.*]] = hlfir.designate %[[VAL_12]] (%{{.*}})
47 // CHECK: %[[VAL_29:.*]] = fir.load %[[VAL_28]] : !fir.ref<!fir.logical<4>>
48 // CHECK: %[[VAL_32:.*]] = hlfir.designate %[[VAL_23]]#0 (%{{.*}}) : (!fir.box<!fir.array<?x!fir.logical<4>>>, index) -> !fir.ref<!fir.logical<4>>
49 // CHECK: hlfir.assign %[[VAL_29]] to %[[VAL_32]] : !fir.logical<4>, !fir.ref<!fir.logical<4>>
52 // CHECK-NOT: fir.call @impure
54 // CHECK: fir.if {{.*}} {
56 // CHECK: %[[VAL_42:.*]] = hlfir.designate %[[VAL_23]]#0 (%{{.*}}) : (!fir.box<!fir.array<?x!fir.logical<4>>>, index) -> !fir.ref<!fir.logical<4>>
57 // CHECK: %[[VAL_43:.*]] = fir.load %[[VAL_42]] : !fir.ref<!fir.logical<4>>
58 // CHECK: %[[VAL_44:.*]] = fir.convert %[[VAL_43]] : (!fir.logical<4>) -> i1
59 // CHECK: fir.if %[[VAL_44]] {
63 // CHECK-NOT: fir.call @impure
65 // CHECK: fir.if {{.*}} {
67 // CHECK: %[[VAL_52:.*]] = hlfir.designate %[[VAL_23]]#0 (%{{.*}}) : (!fir.box<!fir.array<?x!fir.logical<4>>>, index) -> !fir.ref<!fir.logical<4>>
68 // CHECK: %[[VAL_53:.*]] = fir.load %[[VAL_52]] : !fir.ref<!fir.logical<4>>
69 // CHECK: %[[VAL_54:.*]] = fir.convert %[[VAL_53]] : (!fir.logical<4>) -> i1
70 // CHECK: fir.if %[[VAL_54]] {
74 // CHECK: fir.freemem %[[VAL_21]] : !fir.heap<!fir.array<?x!fir.logical<4>>>
75 // CHECK: fir.freemem %[[VAL_12]] : !fir.heap<!fir.array<10x!fir.logical<4>>>