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

[Flang] Incorrect FIR generated for select case statement

Open jonathonpenix opened this issue 4 years ago • 2 comments

Flang seems to be generating incorrect FIR for select case constructs under certain circumstances. Please see the example below:

subroutine foo(num)
  implicit none

  integer, intent(in) :: num
  real, dimension(1) :: array
  array = 0.0

  select case (num)
  case (6)
    write(*,*) "Case 6!"
    where (array >= 0.0)
      array = 42
    end where
  case default
    write(*,*) "Default case!"
    array = 43
  end select

  write(*,*) array(1)
end subroutine foo

program test
  implicit none
  call foo(6)
end

Using gfortran to build, I see the following (expected) output:

 Case 6!
   42.0000000

Using flang to build, I am seeing the the following:

 Case 6!
 Default case!
 43.

In the FIR, it looks like the branch generated at the end of the case (6) body targets the block corresponding to the subsequent case statement rather than the block after the end of the select case. This seems associated with the case body ending with the where clause, but I'm not entirely sure. (For example, adding another write after the end where or making the assignment to array unconditional in the above example both result in the correct IR being generated.)

This issue relates to 459.GemsFDTD.

jonathonpenix avatar Jul 12 '22 23:07 jonathonpenix

@llvm/issue-subscribers-flang-ir

llvmbot avatar Jul 13 '22 05:07 llvmbot

Please see here for an attempted fix! https://reviews.llvm.org/D129879

jonathonpenix avatar Jul 15 '22 17:07 jonathonpenix