Skip to content

[flang][lowering] delay stack save/restor emission in elemental calls #109142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion flang/lib/Lower/ConvertCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,11 @@ std::pair<fir::ExtendedValue, bool> Fortran::lower::genCallOpAndResult(
resultLengths = lengths;
}

if (!extents.empty() || !lengths.empty()) {
if ((!extents.empty() || !lengths.empty()) && !isElemental) {
// Note: in the elemental context, the alloca ownership inside the
// elemental region is implicit, and later pass in lowering (stack
// reclaim) fir.do_loop will be in charge of emitting any stack
// save/restore if needed.
auto *bldr = &converter.getFirOpBuilder();
mlir::Value sp = bldr->genStackSave(loc);
stmtCtx.attachCleanup(
Expand Down
2 changes: 0 additions & 2 deletions flang/test/Lower/HLFIR/elemental-array-ops.f90
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,10 @@ end subroutine char_return
! CHECK: %[[VAL_23:.*]] = arith.constant 0 : index
! CHECK: %[[VAL_24:.*]] = arith.cmpi sgt, %[[VAL_22]], %[[VAL_23]] : index
! CHECK: %[[VAL_25:.*]] = arith.select %[[VAL_24]], %[[VAL_22]], %[[VAL_23]] : index
! CHECK: %[[VAL_26:.*]] = llvm.intr.stacksave : !llvm.ptr
! CHECK: %[[VAL_27:.*]] = fir.call @_QPcallee(%[[VAL_2]], %[[VAL_25]], %[[VAL_20]]) fastmath<contract> : (!fir.ref<!fir.char<1,3>>, index, !fir.boxchar<1>) -> !fir.boxchar<1>
! CHECK: %[[VAL_28:.*]]:2 = hlfir.declare %[[VAL_2]] typeparams %[[VAL_25]] {uniq_name = ".tmp.func_result"} : (!fir.ref<!fir.char<1,3>>, index) -> (!fir.ref<!fir.char<1,3>>, !fir.ref<!fir.char<1,3>>)
! CHECK: %[[MustFree:.*]] = arith.constant false
! CHECK: %[[ResultTemp:.*]] = hlfir.as_expr %[[VAL_28]]#0 move %[[MustFree]] : (!fir.ref<!fir.char<1,3>>, i1) -> !hlfir.expr<!fir.char<1,3>>
! CHECK: llvm.intr.stackrestore %[[VAL_26]] : !llvm.ptr
! CHECK: hlfir.yield_element %[[ResultTemp]] : !hlfir.expr<!fir.char<1,3>>
! CHECK: }
! CHECK: %[[VAL_29:.*]] = arith.constant 0 : index
Expand Down
22 changes: 22 additions & 0 deletions flang/test/Lower/HLFIR/elemental-user-procedure-stacksave.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
! Check that stack save and restore needed for elemental function result
! allocation inside loops are not emitted directly in lowering, but inserted if
! needed in the stack-reclaim pass.

! RUN: %flang_fc1 -emit-hlfir %s -o - | FileCheck %s --check-prefix=CHECK-HLFIR
! RUN: %flang_fc1 -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-LLVM
subroutine foo(c1, c2)
character(*), dimension(100) :: c1, c2
interface
elemental pure function func(c)
character(*), intent(in) :: c
character(len(c)) :: func
end function
end interface
c1 = func(c2)
end subroutine

! CHECK-HLFIR-NOT: stacksave
! CHECK: return

! CHECK-LLVM: stacksave
! CHECK-LLVM: stackrestore
Loading