Skip to content

[flang] invalid code for where #168561

@MarDiehl

Description

@MarDiehl

I've encountered an issue with Flang 21.1.5 which generates invalid code for where. I could reproduce something similar with the following snippet. It should print the sequence of numbers from 1 to 33 but it prints 33 33 33 33 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33. Note that I've disabled use of the random number generator because it always gives 2.2470951E-05.

The same behavior is seen on compiler explorer which uses Flang trunk (commit cce852f)

program test

  implicit none
  integer, dimension(:), allocatable :: v
  real :: r
  integer :: i

  call random_number(r) ! does not work

  print*, 'r',r
  r = 0.32
  allocate(v(1+int(r*100.0)),source=0)

  print*, 'size(v)',size(v)

  do i = 1, size(v)
    where(x(size(v),i)) v=i
  end do

  print*, v
  contains

  function x(n,i)
    integer, intent(in) :: n,i
    logical, dimension(:), allocatable :: x

    allocate(x(n),source=.false.)
    x(i) = .true.

  end function x

end program test

Note: Also reported on LLVM Discourse

Metadata

Metadata

Assignees

No one assigned

    Labels

    flangFlang issues not falling into any other category

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions