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

    <tr>
        <th>Summary</th>
        <td>
            [flang][openmp]Incorrect order in openmp line causes assert
        </td>
    </tr>

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

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

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

<pre>
    The following code is "wrong", but I happened to be confused and surely it should lead to an error or correct code, not an assert in the middle of lowering...  Gfortran gives an error message when compiling the same code.

```
program omp_examples
  implicit none
  integer, parameter :: n = 100
  real :: values(n)
  integer :: i
  real :: sum
  
  call random_number(values)

  sum = 0
  !$omp do parallel reduction(+:sum)
 do i = 1, n
     sum = sum + values(i)
  end do

  
  print *, values
 print *, "sum=", sum
end program omp_examples
```

To reproduce: `flang -fc1 -femit-obj -fopenmp sample.f90` 

(Change the `!$omp do parallel` into `!$omp parallel do` to be correct).

The assert that fails is the `!decompose.output.empty()` in this section of flang/lib/Lower/OpenMP/Decomposer.cpp:
```
ConstructQueue buildConstructQueue(
    mlir::ModuleOp modOp, Fortran::semantics::SemanticsContext &semaCtx,
 Fortran::lower::pft::Evaluation &eval, const parser::CharBlock &source,
 llvm::omp::Directive compound, const List<Clause> &clauses) {

 ConstructDecomposition decompose(modOp, semaCtx, eval, compound, clauses);
 assert(!decompose.output.empty() && "Construct decomposition failed");

 for (UnitConstruct &uc : decompose.output) {
 assert(getLeafConstructs(uc.id).empty() && "unexpected compound directive");
    //  If some clauses are left without source information, use the directive's
    //  source.
    for (auto &clause : uc.clauses)
 if (clause.source.empty())
        clause.source = source;
  }

 return decompose.output;
}
```

Again, raising bug just in case I get sidetracked and don't fix this in the next couple of days... The fix is probably not really in this section, and may be a bit more complicated to fix.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx8VkuP4jAS_jXmU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