<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/107622>107622</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] MLIR Assertion `mayBeGraphRegion(*op->getParentRegion()) && "expected that op has no uses"' failed.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir,
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mjklemm
      </td>
    </tr>
</table>

<pre>
    When compiling the below snippet of Fortran code, Flang stops with an assertion in MLIR:

```shell
> flang-new -O1 -c assertion.f90
flang-new: /work1/omp-nightly/build/git/trunk20.0-atd/llvm-project/mlir/lib/IR/PatternMatch.cpp:181: mlir::RewriterBase::eraseOp(mlir::Operation*)::<lambda(mlir::Operation*)>: Assertion `mayBeGraphRegion(*op->getParentRegion()) && "expected that op has no uses"' failed.
```

This happens with all optimization levels > `-O0`.

```Fortran
! assertion.f90
MODULE MODE
  INTERFACE MYFUNC
    MODULE PROCEDURE MYFUNC_1D
  END INTERFACE

 CONTAINS

    FUNCTION MYFUNC_1D(INPUT) RESULT(OUTPUT)
 implicit none
      REAL, DIMENSION(:), INTENT(IN) :: INPUT
      REAL, DIMENSION(SIZE(INPUT))   :: OUTPUT
      OUTPUT(:) = INPUT(:)
    END FUNCTION MYFUNC_1D

END MODULE MODE

SUBROUTINE INIT(DATA)
 use MODE
  implicit none

  TYPE MYTYPE
    REAL, DIMENSION(:,:), POINTER :: D1
    REAL, DIMENSION(:)  , POINTER :: RES
  END TYPE MYTYPE

  TYPE(MYTYPE),      INTENT(INOUT) :: DATA
  REAL, DIMENSION(1000) :: RES

  WHERE(DATA%D1(:,1)==0)
    RES(:)= 0
 ELSEWHERE
    RES(:) = DATA%D1(:,1) / MYFUNC(DATA%D1(:,1))
  END WHERE
END SUBROUTINE INIT
```
</pre>
<img width="1px" height="1px" alt="" src="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