llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] Compile error when derived-type array is defined using derived type and reshape intrinsic function in where construct

Open ohno-fj opened this issue 1 year ago • 10 comments

Version of flang : 21.0.0(8c7c791284877e36f73c41ffa56b52c13e613993)/AArch64

As in the attached program, when derived-type array (str) is defined using derived type (ty1) and reshape intrinsic function in where construct, a compilation error occurs.
When the compilation option (-flang-deprecated-no-hlfir) is specified, the following message is outputted:
not yet implemented: allocatable components in derived type assignment
The above program is snfmm509_21.f90.

When reshape intrinsic function is not used, the compilation succeeds.
The above program is snfmm509_22.f90.

The following are the test program, Flang, Gfortran and ifx compilation/execution result.

snfmm509_21.f90:

program main
  call test06()
  print *,'pass'
end program main

subroutine test06()
  type ty1
     integer ,allocatable :: a(:,:,:)
  end type ty1
  type(ty1) :: str(1)
  integer ,allocatable :: b(:,:,:)
  allocate(str(1)%a(1,1,1),b(1,1,1))
  b=1
  write(6,*) "b                                 = ", b
  write(6,*) "reshape((/(b,jj=1,1)/),(/1,1,1/)) = ", reshape((/(b,jj=1,1)/),(/1,1,1/))
! where ((/.true./)) str=(/(ty1(b                                ),ii=1,1)/)  ! compilation normal termination
  where ((/.true./)) str=(/(ty1(reshape((/(b,jj=1,1)/),(/1,1,1/))),ii=1,1)/)  ! compilation error
end subroutine test06
$ flang snfmm509_21.f90 -flang-deprecated-no-hlfir
error: loc("/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/fe2_05/snfmm509_21.f90":17:3): /work/groups/ssoft/compiler/llvm/src/llvm-main/flang/lib/Lower/ConvertExpr.cpp:1012: not yet implemented: allocatable components in derived type assignment
LLVM ERROR: aborting
$ flang snfmm509_21.f90
error: loc("/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/fe2_05/snfmm509_21.f90":17:3): failed to legalize operation 'hlfir.exactly_once'
error: failure in HLFIR bufferization pass
error: Lowering to LLVM IR failed
error: loc("/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/fe2_05/snfmm509_21.f90":1:1): cannot be converted to LLVM IR: missing `LLVMTranslationDialectInterface` registration for dialect for op: func.func
error: failed to create the LLVM module
$
$ gfortran snfmm509_21.f90; ./a.out
 b                                 =            1
 reshape((/(b,jj=1,1)/),(/1,1,1/)) =            1
 pass
$
$ ifx snfmm509_21.f90; ./a.out
 b                                 =            1
 reshape((/(b,jj=1,1)/),(/1,1,1/)) =            1
 pass
$

snfmm509_22.f90:

program main
  call test06()
  print *,'pass'
end program main

subroutine test06()
  type ty1
     integer ,allocatable :: a(:,:,:)
  end type ty1
  type(ty1) :: str(1)
  integer ,allocatable :: b(:,:,:)
  allocate(str(1)%a(1,1,1),b(1,1,1))
  b=1
  write(6,*) "b                                 = ", b
  write(6,*) "reshape((/(b,jj=1,1)/),(/1,1,1/)) = ", reshape((/(b,jj=1,1)/),(/1,1,1/))
  where ((/.true./)) str=(/(ty1(b                                ),ii=1,1)/)  ! compilation normal termination
! where ((/.true./)) str=(/(ty1(reshape((/(b,jj=1,1)/),(/1,1,1/))),ii=1,1)/)  ! compilation error
end subroutine test06
$ flang snfmm509_22.f90; ./a.out
 b                                 =  1
 reshape((/(b,jj=1,1)/),(/1,1,1/)) =  1
 pass
$

ohno-fj avatar Mar 10 '25 01:03 ohno-fj

Here is a bit simplified test case:

subroutine test06()
  interface
     function func1()
       integer :: func1
     end function func1
     function func2()
       integer :: func2(1)
     end function func2
  end interface
  integer :: str2(1)
  where ((/.true./)) str2=(/(func1(),ii=1,1)/)  ! error
!  where ((/.true./)) str2=func2()  ! no error
end subroutine test06

You may see that in the problem case the ordered-assignment pass does not clone the body of hlfir.exactly_once as if it does not "see it". In the good case, the hlfir.exactly_once's body is cloned before the where region and the operation is erased.

My guess is that the ordered-assignment only looks for hlfir.exactly_once immediately nested inside OrderedAssignmentTreeOpInterface ops, but I am not 100% sure. I suggest figuring out the difference between these two cases and find why hlfir.exactly_once is not processed in the problem case.

vzakhari avatar Aug 06 '25 19:08 vzakhari

Here is a bit simplified test case:

subroutine test06()
  interface
     function func1()
       integer :: func1
     end function func1
     function func2()
       integer :: func2(1)
     end function func2
  end interface
  integer :: str2(1)
  where ((/.true./)) str2=(/(func1(),ii=1,1)/)  ! error
!  where ((/.true./)) str2=func2()  ! no error
end subroutine test06

You may see that in the problem case the ordered-assignment pass does not clone the body of hlfir.exactly_once as if it does not "see it". In the good case, the hlfir.exactly_once's body is cloned before the where region and the operation is erased.

My guess is that the ordered-assignment only looks for hlfir.exactly_once immediately nested inside OrderedAssignmentTreeOpInterface ops, but I am not 100% sure. I suggest figuring out the difference between these two cases and find why hlfir.exactly_once is not processed in the problem case.

Thanks, I'll investigate this

ceseo avatar Aug 06 '25 20:08 ceseo

My guess is that the ordered-assignment only looks for hlfir.exactly_once immediately nested inside OrderedAssignmentTreeOpInterface ops, but I am not 100% sure. I suggest figuring out the difference between these two cases and find why hlfir.exactly_once is not processed in the problem case.

Your assessment is correct, and I may need some advice on how to properly fix this.

If I understand correctly, hlfir.exactly_once lives inside an array constructor/reshape that feeds a struct constructor on the rhs of a where assignment. That produces an hlfir.exactly_once nested under other HLFIR ops that wrap the constructor/reshape. Since the LowerHLFIROrderedAssignments pass is the one that clones/inlines the exactly_once bodies before where and erases the op, leaving one behind causes the error we see in the HLFIR bufferization pass.

I see that reshape has its own HLFIR op and array constructors have a dedicated lowering path (hlfir.array_ctor, etc), so hlfir.exactly_once never gets processed in the problematic case.

I tried modifying LowerHLFIROrderedAssignments to recursively process hlfir.exactly_once ops anywhere inside the where/assignment rhs/mask, not only when immediately nested, but that didn't work.

Any thoughts?

ceseo avatar Aug 21 '25 15:08 ceseo

@vzakhari did you have time to look at my comments above? Thanks!

ceseo avatar Sep 03 '25 17:09 ceseo

Hi @ceseo, sorry I missed it. @jeanPerier is the best expert in this area. Jean, can you please comment?

vzakhari avatar Sep 03 '25 17:09 vzakhari

Hi @ceseo, looking at the issue and the standard, I think the issue is that lowering should generate an hlfir.exactly_once around the array constructor generation to fulfill the standard requirements in F2023 section 10.2.3.2 that:

If an array constructor appears in a where-assignment-stmt or in a mask-expr, the array constructor is evaluated
without any masked control and then the where-assignment-stmt is executed or the mask-expr is evaluated.

The goal of hlfir.exactly_once is to fullfill the requirements that something inside a where-assignment-stmt must be evaluated without masked control. As such, it is not meant to be nested (since we need to be able to hoist it when generating the actual where assignment loops). The issue is that hlfir.exactly_once is currently only generated for the non-elemental function case and their arguments and I missed the array constructor case.

For instance, in the following program, if one was to just hoist the currently generated hlfir.exactly_once around foo() the program output would become incorrect because foo() has side effects and should be evaluated for every iteration of the array constructor implied do. Blindly hoisting it from the where loops would hoist it from the array constructor, which is incorrect. Instead, we need to update lowering to generate an hlfir.exactly_once around array constructor so that the whole array constructor is evaluated unmasked.

module m
  integer :: i = 1
contains
  real function foo()
    print *, i
    foo = i
    i = i + 1
  end function

  subroutine test(l, x)
    logical :: l(4)
    real :: x(4)
    where (l) x = [(x(i)+foo(),i=1,4)]
  end subroutine
end module

  use m
  real :: x(4) = [1.,2.,3.,4.]
  logical :: l(4) = .false.
  call test(l,x)
  print *, x
end

Should print:

           1
           2
           3
           4
   1.00000000       2.00000000       3.00000000       4.00000000 

Do you want to work on the fix in lowering?

jeanPerier avatar Sep 05 '25 15:09 jeanPerier

Side note that adding hlfir.exactly_once will have no negative impact on optimizations for code like below where teh array constructor has no side effects and can be (and currently is) evaluated under the mask to avoid the creation of an array temporary:

  subroutine test(l, x, n)
    logical :: l(n)
    real :: x(n)
    integer :: n
    where (l) x = [(real(i),i=1,n)]
  end subroutine

This could be dealt with either in lowering or in LowerHLFIROrderedAssignments by checking if the array constructor actually has side effects or and not emitting/removing the hlfir.exactly_once.

jeanPerier avatar Sep 05 '25 15:09 jeanPerier

Do you want to work on the fix in lowering?

I could try. But if you already know where to modify the code and can do it quicker, please go ahead and reassign the bug to yourself.

ceseo avatar Sep 05 '25 21:09 ceseo

@jeanPerier do you have a fix for this, or should I give it a try?

ceseo avatar Sep 16 '25 02:09 ceseo

@jeanPerier do you have a fix for this, or should I give it a try?

No, feel free to go ahead, thanks!

jeanPerier avatar Sep 16 '25 12:09 jeanPerier