This is an archive of the discontinued LLVM Phabricator instance.

[flang] Fix explicit space finalization
AbandonedPublic

Authored by peixin on Apr 22 2022, 7:23 AM.

Details

Summary

When assigning one scalar result to array section in forall construct
and the scalar result is generated with some temporary memory, the free
operation cannot be done after lowering the rhs expression since the
free operation will be outside the fir.do_loop region and the argument
of the free operation is illegal. Move the explicit space finalization
to lowerArrayExpression(rhs) when the rhs has scalar result.

Diff Detail

Event Timeline

peixin created this revision.Apr 22 2022, 7:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 22 2022, 7:23 AM
peixin requested review of this revision.Apr 22 2022, 7:23 AM
schweitz added inline comments.Apr 22 2022, 10:26 AM
flang/lib/Lower/ConvertExpr.cpp
3371

Testing the rank all over the place doesn't seem correct. Expressions can contain subexpressions with or without rank and build temporaries accordingly.

Finalization should really happen unconditionally. (In the right place, of course.)

peixin added inline comments.Apr 22 2022, 11:20 AM
flang/lib/Lower/ConvertExpr.cpp
3371

Expressions can contain subexpressions with or without rank and build temporaries accordingly.

  integer :: arr(6), i
  forall (i=1:6:2)
    arr(i:i+1) = (/(func(j),j=i,i+1)/)
  end forall
 print *, arr
contains
  pure function func(a) result(res)
    integer, intent(in) :: a
    integer :: res
    res = a
  end function
end

Are you pointing to this?

  1. I don't understand the case of subexpressions without rank.
  2. To my understanding, the subexpressions do not matter. The key point is the expression of rhs. If its final result is scalar, one do_loop assignment will be performed from rhs to lhs and the allocmem is always inside the do_loop. Please correct me if I am wrong.

Finalization should really happen unconditionally. (In the right place, of course.)

If so, the solution would be better than this patch. If you already how to do it, can you take up this? This kind of solution is beyond my current knowledge of FIR lowering. Sorry about this.

schweitz added inline comments.Apr 22 2022, 4:32 PM
flang/lib/Lower/ConvertExpr.cpp
3371

Sure. Let me take a closer look here.