Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2326,12 +2326,40 @@ genParallelOp(lower::AbstractConverter &converter, lower::SymMap &symTable,

static mlir::omp::ScanOp
genScanOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, mlir::Location loc,
const ConstructQueue &queue, ConstructQueue::const_iterator item) {
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::const_iterator item) {
mlir::omp::ScanOperands clauseOps;
genScanClauses(converter, semaCtx, item->clauses, loc, clauseOps);
return mlir::omp::ScanOp::create(converter.getFirOpBuilder(),
converter.getCurrentLocation(), clauseOps);
mlir::omp::ScanOp scanOp = mlir::omp::ScanOp::create(
converter.getFirOpBuilder(), converter.getCurrentLocation(), clauseOps);
// All loop indices should be loaded after the scan construct as otherwise,
// it would result in using the index variable across scan directive.
// (`Intra-iteration dependences from a statement in the structured
// block sequence that precede a scan directive to a statement in the
// structured block sequence that follows a scan directive must not exist,
// except for dependences for the list items specified in an inclusive or
// exclusive clause.`).
// TODO: Nested loops are not handled.
mlir::omp::LoopNestOp loopNestOp =
scanOp->getParentOfType<mlir::omp::LoopNestOp>();
assert(loopNestOp.getNumLoops() == 1 &&
"Scan directive inside nested do loops is not handled yet.");
mlir::Region &region = loopNestOp->getRegion(0);
mlir::Value indexVal = fir::getBase(region.getArgument(0));
lower::pft::Evaluation *doConstructEval = eval.parentConstruct;
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
lower::pft::Evaluation *doLoop = &doConstructEval->getFirstNestedEvaluation();
auto *doStmt = doLoop->getIf<parser::NonLabelDoStmt>();
assert(doStmt && "Expected do loop to be in the nested evaluation");
const auto &loopControl =
std::get<std::optional<parser::LoopControl>>(doStmt->t);
const parser::LoopControl::Bounds *bounds =
std::get_if<parser::LoopControl::Bounds>(&loopControl->u);
mlir::Operation *storeOp =
setLoopVar(converter, loc, indexVal, bounds->name.thing.symbol);
firOpBuilder.setInsertionPointAfter(storeOp);
return scanOp;
}

static mlir::omp::SectionsOp
Expand Down Expand Up @@ -3416,7 +3444,7 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
loc, queue, item);
break;
case llvm::omp::Directive::OMPD_scan:
newOp = genScanOp(converter, symTable, semaCtx, loc, queue, item);
newOp = genScanOp(converter, symTable, semaCtx, eval, loc, queue, item);
break;
case llvm::omp::Directive::OMPD_section:
llvm_unreachable("genOMPDispatch: OMPD_section");
Expand Down
Loading