[flang-commits] [flang] [llvm] [mlir] [openmp] [Flang][OpenMP] Lower scan directive and inscan reduction modifier (PR #206747)
via flang-commits
flang-commits at lists.llvm.org
Thu Jul 23 12:53:12 PDT 2026
================
@@ -5096,6 +5424,46 @@ convertOmpLoopNest(Operation &opInst, llvm::IRBuilderBase &builder,
computeIP = loopInfos.front()->getPreheaderIP();
}
+ // If this loop is the worksharing loop of a scan reduction, generate a
+ // pair of canonical loops (an input loop and a scan loop) instead of a
+ // single loop. The `scan` directive in the body is translated against the
+ // `ScanInfo` recorded here.
+ bool isInScanRegion = false;
+ if (auto wsloopOp = loopOp->getParentOfType<omp::WsloopOp>())
+ isInScanRegion =
+ wsloopOp.getReductionMod() && (wsloopOp.getReductionMod().value() ==
+ mlir::omp::ReductionModifier::inscan);
+ if (isInScanRegion) {
+ llvm::Expected<llvm::ScanInfo *> res = ompBuilder->scanInfoInitialize();
+ if (failed(handleError(res, *loopOp)))
+ return failure();
+ llvm::ScanInfo *scanInfo = res.get();
+ moduleTranslation.stackWalk<OpenMPLoopInfoStackFrame>(
+ [&](OpenMPLoopInfoStackFrame &frame) {
+ frame.scanInfo = scanInfo;
+ return WalkResult::interrupt();
+ });
+ llvm::Expected<llvm::SmallVector<llvm::CanonicalLoopInfo *>> loopResults =
+ ompBuilder->createCanonicalScanLoops(
----------------
chichunchen wrote:
Could we either reject non-i32 loop types here or make the scan implementation use a consistent index type?
Flang emits i32 loop bounds for default INTEGER and i64 bounds for INTEGER(KIND=8). LoopNestOp::verify() permits both, so an i64 Span can reach this call.
The existing i32 scan test succeeds, while the same valid scan using an i64 induction variable aborts in emitScanBasedDirectiveDeclsIR.
Regression generated by codex and I can verify it triggered assert:
```
Assertion `C1->getType() == C2->getType() &&
"Operand types in binary constant expression should match"' failed.
```
```
program scan_i64_abort
implicit none
integer(kind=8) :: i
integer :: x, result(4)
x = 0
!$omp parallel do reduction(inscan, +: x)
do i = 1_8, 4_8
x = x + 1
!$omp scan inclusive(x)
result(i) = x
end do
end program
```
https://github.com/llvm/llvm-project/pull/206747
More information about the flang-commits
mailing list