[flang-commits] [clang] [flang] [llvm] [clang][flang][OpenMP] Fix context selector matching and scoring (PR #224431)

via flang-commits flang-commits at lists.llvm.org
Fri Oct 9 01:00:05 PDT 2026


================
@@ -5242,6 +5281,14 @@ static mlir::omp::DistributeOp genCompositeDistributeParallelDo(
   ConstructQueue::const_iterator parallelItem = std::next(distributeItem);
   ConstructQueue::const_iterator doItem = std::next(parallelItem);
 
+  // Clause expressions follow source nesting even though the composite
+  // operations place PARALLEL outside DISTRIBUTE.
+  mlir::omp::DistributeOperands distributeClauseOps;
+  genDistributeClauses(converter, semaCtx, stmtCtx, distributeItem->clauses,
----------------
MattPD wrote:

Moving `genDistributeClauses` before `genParallelOp` creates the `dist_schedule` operand's temporary outside PARALLEL, but `genOMPDispatch` still finalizes `stmtCtx` after the inner DISTRIBUTE operation. Each thread then destroys the same temporary. The DISTRIBUTE PARALLEL DO SIMD composite has the same problem.

You can reproduce this by saving the following to `repro.f90`, building with `flang -fopenmp -fopenmp-version=52 -module-dir /tmp repro.f90 -o repro`, and running `OMP_NUM_TEAMS=1 OMP_NUM_THREADS=4 ./repro`:

```fortran
module m
contains
  function get(n) result(r)
    integer, intent(in) :: n
    integer, allocatable :: r(:)
    allocate(r(n))
    r = 1
  end function
  subroutine s(n, a)
    integer :: n, i, a(n)
    !$omp teams distribute parallel do dist_schedule(static, size(get(n)))
    do i = 1, n
      a(i) = i
    end do
  end subroutine
end module
program p
  use m
  integer :: a(64)
  a = 0
  call s(64, a)
  print *, sum(a)
end program
```

At 9c59c04 and 1850727, this prints `2080` and then aborts with `double free or corruption`. The [merge base](https://github.com/llvm/llvm-project/commit/ae7f7ef515657eeb037e1cad045ade1a45cc7951) exits normally with `2080`. At 9c59c04, HLFIR places the call to `get` in `omp.teams` and its `hlfir.destroy` in `omp.parallel`. At the base, both execute inside PARALLEL. The program also exits normally at 9c59c04 when it runs with one thread or when the construct uses DISTRIBUTE without PARALLEL.

Could both composite helpers preserve the operand's source context and the required cleanup order while destroying each temporary exactly once?

https://github.com/llvm/llvm-project/pull/224431


More information about the flang-commits mailing list