[flang-commits] [flang] [llvm] [mlir] [openmp] [Flang][OpenMP] Lower scan directive and inscan reduction modifier (PR #206747)

CHANDRA GHALE via flang-commits flang-commits at lists.llvm.org
Wed Jul 22 07:35:49 PDT 2026


chandraghale wrote:




> Thanks for working on this. I tested the lowering with the PR compiler, with substantial assistance from Codex in identifying edge cases and constructing reproducers. In particular:
> 
> * An inclusive scan with a nonidentity incoming value omits that value. With x = 10 and four +1 iterations, Flang produces 1, 2, 3, 4 instead of 11, 12, 13, 14, as required by OpenMP’s orig-val semantics.
> * Legal loop shapes outside 1..N with unit stride corrupt the scan buffer. A 10..20 loop and a stride-two loop both produce incorrect results; the generated IR indexes a tripCount + 1 allocation using the source induction value.
> * Zero-trip loops crash at runtime. Runtime bounds, by-reference reductions, and multidimensional MLIR loop nests can also crash during compilation.
> 
> The general legal cases—nonidentity initial values, arbitrary bounds and strides, zero-trip loops, and runtime bounds—need to work before scan lowering is enabled. Cases intentionally outside this patch’s scope, such as by-reference reductions and multidimensional/collapsed loops, should instead produce clear “not yet implemented” diagnostics.
> 
> Given the number and severity of the confirmed failures, I don’t think the patch is ready for detailed review yet. Please test the supported cases more broadly and ping me once the implementation either handles or explicitly rejects them; I’ll be happy to review the revised patch.

Thanks for the review!  

Fixed
- The reduction variable's incoming value is now seeded on the first iteration
  instead of the identity, so non-identity initial values are handled correctly.
- The scan buffer is now indexed by the 1-based logical iteration number rather
  than the induction-variable value, fixing arbitrary bounds and non-unit strides.
- Zero-trip loops are now guarded so they no longer crash and correctly leave the
  reduction variable unchanged.
- Inner scan loops now use an empty ComputeIP, fixing the compile-time crash for
  runtime (non-constant) trip counts.

Diagnosed (not yet implemented)
- By-ref inscan reductions now emit a clean "not yet implemented" error instead of crashing.
- Collapsed/nested scan loops remain guarded in flang lowering.

Tests added
- Runtime: scan-nonidentity, scan-bounds (bounds + stride), scan-edge (zero-trip + runtime bounds).
- MLIR: by-ref inscan TODO test in openmp-todo.mlir; updated openmp-reduction-scan.mlir CHECKs.

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


More information about the flang-commits mailing list