// Test inlining of hlfir.assign for allocatable LHS with hlfir.expr RHS. // This tests the -inline-hlfir-allocatable-expr-assign flag. // RUN: fir-opt --inline-hlfir-assign %s | FileCheck %s --check-prefix=DEFAULT // RUN: fir-opt -inline-hlfir-allocatable-expr-assign --inline-hlfir-assign %s | FileCheck %s --check-prefix=ENABLED // Test case: c = cos(a) where c is allocatable // This is derived from the flang-529628 test case. // The hlfir.elemental produces an hlfir.expr which doesn't alias with // memory, so the assignment can be safely inlined. func.func @test_allocatable_elemental_assign(%arg0: !fir.ref>>>, %arg1: !fir.ref>>>) { %c0 = arith.constant 0 : index %c1 = arith.constant 1 : index // Declare the allocatables %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>>>) // Load a to get its shape %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> // Create elemental: cos(a) %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 } // Assign elemental result to allocatable c hlfir.assign %elemental to %c#0 realloc : !hlfir.expr, !fir.ref>>> hlfir.destroy %elemental : !hlfir.expr return } // DEFAULT-LABEL: func.func @test_allocatable_elemental_assign // By default (without the option), the allocatable assign should NOT be inlined // DEFAULT: hlfir.assign %{{.*}} to %{{.*}} realloc : !hlfir.expr, !fir.ref>>> // ENABLED-LABEL: func.func @test_allocatable_elemental_assign // With the option enabled, the assign should be inlined. // The generated code uses genReallocIfNeeded which creates fir.if for // allocation checking and a loop for assignment. // ENABLED-NOT: hlfir.assign %{{.*}} realloc // ENABLED: hlfir.elemental // ENABLED: %[[ORIG_MEM:.*]] = fir.box_addr %{{.*}} : (!fir.box>>) -> !fir.heap> // ENABLED: %[[IF_RET:.*]]:2 = fir.if %[[COND:.*]] -> (i1, !fir.heap>) { // ENABLED: fir.do_loop // ENABLED: hlfir.apply // ENABLED: hlfir.designate // ENABLED: hlfir.assign %{{.*}} : f64, !fir.ref // ENABLED: fir.if %[[IF_RET]]#0 { // ENABLED: fir.if %[[COND]] { // ENABLED: fir.freemem %[[ORIG_MEM]] : !fir.heap> // Test case: Non-trivial element type should NOT be inlined func.func @test_allocatable_nontrivial_type(%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>>>>) // Character elemental - should NOT be inlined even with the option %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 } // Character types are not trivial, so this should never be inlined // ENABLED-LABEL: func.func @test_allocatable_nontrivial_type // ENABLED: hlfir.assign %{{.*}} to %{{.*}} realloc : !hlfir.expr> // Test case: Variable RHS (not hlfir.expr) should NOT be inlined by this pattern func.func @test_allocatable_variable_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>) // Variable RHS - NOT an hlfir.expr, so the allocatable pattern should NOT match hlfir.assign %b#0 to %a#0 realloc : !fir.ref>, !fir.ref>>> return } // Variable RHS should keep the original assign (allocatable pattern doesn't match) // ENABLED-LABEL: func.func @test_allocatable_variable_rhs // ENABLED: hlfir.assign %{{.*}} to %{{.*}} realloc : !fir.ref>