// Test the separate-allocatable-assign pass. // It should transform hlfir.assign ... realloc into conditional reallocation // followed by a non-realloc hlfir.assign. // RUN: fir-opt --separate-allocatable-assign %s | FileCheck %s // Test: allocatable array assignment with elemental RHS func.func @test_expr_rhs(%arg0: !fir.ref>>>, %arg1: !fir.ref>>>) { %c0 = arith.constant 0 : index %c1 = arith.constant 1 : index %a:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFEa"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) %c:2 = hlfir.declare %arg1 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFEc"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) %a_box = fir.load %a#0 : !fir.ref>>> %dims:3 = fir.box_dims %a_box, %c0 : (!fir.box>>, index) -> (index, index, index) %shape = fir.shape %dims#1 : (index) -> !fir.shape<1> %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr { ^bb0(%i: index): %lb_offset = arith.subi %dims#0, %c1 : index %idx = arith.addi %i, %lb_offset : index %a_elem = hlfir.designate %a_box (%idx) : (!fir.box>>, index) -> !fir.ref %a_val = fir.load %a_elem : !fir.ref %cos_val = math.cos %a_val fastmath : f64 hlfir.yield_element %cos_val : f64 } hlfir.assign %elemental to %c#0 realloc : !hlfir.expr, !fir.ref>>> hlfir.destroy %elemental : !hlfir.expr return } // CHECK-LABEL: func.func @test_expr_rhs // The realloc assign should be separated into realloc + non-realloc assign. // CHECK-NOT: hlfir.assign{{.*}}realloc // CHECK: fir.if // CHECK: fir.allocmem // CHECK: %[[BOX:.*]] = fir.load %{{.*}} : !fir.ref>>> // CHECK: hlfir.assign %{{.*}} to %[[BOX]] : !hlfir.expr, !fir.box>> // Test: allocatable array assignment with variable RHS func.func @test_var_rhs(%arg0: !fir.ref>>>, %arg1: !fir.ref>) { %c10 = arith.constant 10 : index %shape = fir.shape %c10 : (index) -> !fir.shape<1> %a:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFEa"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) %b:2 = hlfir.declare %arg1(%shape) {uniq_name = "_QFEb"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) hlfir.assign %b#0 to %a#0 realloc : !fir.ref>, !fir.ref>>> return } // CHECK-LABEL: func.func @test_var_rhs // CHECK-NOT: hlfir.assign{{.*}}realloc // CHECK: fir.if // CHECK: fir.allocmem // CHECK: %[[BOX:.*]] = fir.load %{{.*}} : !fir.ref>>> // CHECK: hlfir.assign %{{.*}} to %[[BOX]] : !fir.ref>, !fir.box>> // Test: non-trivial element type should NOT be separated func.func @test_nontrivial(%arg0: !fir.ref>>>>) { %c10 = arith.constant 10 : index %shape = fir.shape %c10 : (index) -> !fir.shape<1> %a:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFEa"} : (!fir.ref>>>>) -> (!fir.ref>>>>, !fir.ref>>>>) %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr> { ^bb0(%i: index): %str = fir.undefined !fir.char<1,10> hlfir.yield_element %str : !fir.char<1,10> } hlfir.assign %elemental to %a#0 realloc : !hlfir.expr>, !fir.ref>>>> hlfir.destroy %elemental : !hlfir.expr> return } // CHECK-LABEL: func.func @test_nontrivial // Character types are not trivial, so the assign should remain // CHECK: hlfir.assign %{{.*}} to %{{.*}} realloc // Test: non-allocatable assign should NOT be modified func.func @test_non_allocatable(%arg0: !fir.ref>, %arg1: !fir.ref>) { %c10 = arith.constant 10 : index %shape = fir.shape %c10 : (index) -> !fir.shape<1> %a:2 = hlfir.declare %arg0(%shape) {uniq_name = "_QFEa"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) %b:2 = hlfir.declare %arg1(%shape) {uniq_name = "_QFEb"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) hlfir.assign %b#0 to %a#0 : !fir.ref>, !fir.ref> return } // CHECK-LABEL: func.func @test_non_allocatable // Non-allocatable assign should pass through unchanged // CHECK: hlfir.assign %{{.*}} to %{{.*}} : !fir.ref>, !fir.ref> // CHECK-NOT: fir.if // Test: self-aliasing (a = a(:n)) should NOT be separated because realloc // would free the old LHS storage that the RHS still references. func.func @test_self_alias(%arg0: !fir.ref>>>) { %c0 = arith.constant 0 : index %c1 = arith.constant 1 : index %c5 = arith.constant 5 : index %a:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFEa"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) %a_box = fir.load %a#0 : !fir.ref>>> %dims:3 = fir.box_dims %a_box, %c0 : (!fir.box>>, index) -> (index, index, index) %shape = fir.shape %c5 : (index) -> !fir.shape<1> %section = hlfir.designate %a_box (%c1:%c5:%c1) shape %shape : (!fir.box>>, index, index, index, !fir.shape<1>) -> !fir.box> hlfir.assign %section to %a#0 realloc : !fir.box>, !fir.ref>>> return } // CHECK-LABEL: func.func @test_self_alias // Self-aliasing must keep the original realloc assign // CHECK: hlfir.assign %{{.*}} to %{{.*}} realloc // Test: expr RHS whose producing elemental reads from the LHS // (e.g. a = a(:n) + 1). The pass separates the reallocation regardless of // any self-reference in the expression: keeping the (lazy) expression // evaluation from being moved across the reallocation's deallocation is the // responsibility of the hlfir.assign lowering / expression bufferization, not // of this pass. See the deallocation-conflict check in OptimizedBufferization. func.func @test_self_alias_expr(%arg0: !fir.ref>>>) { %c1 = arith.constant 1 : index %c5 = arith.constant 5 : index %one_i32 = arith.constant 1 : i32 %a:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFEa"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) %a_box = fir.load %a#0 : !fir.ref>>> %shape = fir.shape %c5 : (index) -> !fir.shape<1> %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr { ^bb0(%i: index): %elem = hlfir.designate %a_box (%i) : (!fir.box>>, index) -> !fir.ref %val = fir.load %elem : !fir.ref %sum = arith.addi %val, %one_i32 : i32 hlfir.yield_element %sum : i32 } hlfir.assign %elemental to %a#0 realloc : !hlfir.expr, !fir.ref>>> hlfir.destroy %elemental : !hlfir.expr return } // CHECK-LABEL: func.func @test_self_alias_expr // Expression RHS is always separated; alias safety is left to bufferization. // CHECK-NOT: hlfir.assign{{.*}}realloc // CHECK: fir.if // CHECK: fir.allocmem // CHECK: %[[BOX:.*]] = fir.load %{{.*}} : !fir.ref>>> // CHECK: hlfir.assign %{{.*}} to %[[BOX]] : !hlfir.expr, !fir.box>> // Test: lower bounds from RHS should be preserved during reallocation. // source(10:12) has lower bound 10; dest should get lower bound 10 after // dest = source. func.func @test_lower_bounds(%arg0: !fir.ref>>>, %arg1: !fir.ref>) { %c10 = arith.constant 10 : index %c3 = arith.constant 3 : index %shapeshift = fir.shape_shift %c10, %c3 : (index, index) -> !fir.shapeshift<1> %dest:2 = hlfir.declare %arg0 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFEdest"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) %source:2 = hlfir.declare %arg1(%shapeshift) {uniq_name = "_QFEsource"} : (!fir.ref>, !fir.shapeshift<1>) -> (!fir.box>, !fir.ref>) hlfir.assign %source#0 to %dest#0 realloc : !fir.box>, !fir.ref>>> return } // CHECK-LABEL: func.func @test_lower_bounds // The realloc should be separated with lower bound 10 propagated. // CHECK-NOT: hlfir.assign{{.*}}realloc // CHECK: %[[C10:.*]] = arith.constant 10 : index // CHECK: fir.if // CHECK: fir.allocmem // Lower bound 10 should appear in the embox/store of the new allocation. // CHECK: %[[BOX:.*]] = fir.load %{{.*}} : !fir.ref>>> // CHECK: hlfir.assign %{{.*}} to %[[BOX]] : !fir.box>, !fir.box>>