Skip to content

Commit

Permalink
[Flang][OpenMP] Support teams reductions lowering (llvm#122683)
Browse files Browse the repository at this point in the history
This patch adds PFT to MLIR lowering of teams reductions. Since there is
still no MLIR to LLVM IR translation implemented, compilation of
programs including these constructs will still trigger
not-yet-implemented errors.
  • Loading branch information
skatrak authored Jan 13, 2025
1 parent 73b0e8a commit 82b9eb1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
33 changes: 24 additions & 9 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1336,19 +1336,18 @@ static void genWorkshareClauses(lower::AbstractConverter &converter,
cp.processNowait(clauseOps);
}

static void genTeamsClauses(lower::AbstractConverter &converter,
semantics::SemanticsContext &semaCtx,
lower::StatementContext &stmtCtx,
const List<Clause> &clauses, mlir::Location loc,
mlir::omp::TeamsOperands &clauseOps) {
static void genTeamsClauses(
lower::AbstractConverter &converter, semantics::SemanticsContext &semaCtx,
lower::StatementContext &stmtCtx, const List<Clause> &clauses,
mlir::Location loc, mlir::omp::TeamsOperands &clauseOps,
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processAllocate(clauseOps);
cp.processIf(llvm::omp::Directive::OMPD_teams, clauseOps);
cp.processNumTeams(stmtCtx, clauseOps);
cp.processThreadLimit(stmtCtx, clauseOps);
cp.processReduction(loc, clauseOps, reductionSyms);
// TODO Support delayed privatization.

cp.processTODO<clause::Reduction>(loc, llvm::omp::Directive::OMPD_teams);
}

static void genWsloopClauses(
Expand Down Expand Up @@ -2015,13 +2014,29 @@ genTeamsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::const_iterator item) {
lower::StatementContext stmtCtx;

mlir::omp::TeamsOperands clauseOps;
genTeamsClauses(converter, semaCtx, stmtCtx, item->clauses, loc, clauseOps);
llvm::SmallVector<const semantics::Symbol *> reductionSyms;
genTeamsClauses(converter, semaCtx, stmtCtx, item->clauses, loc, clauseOps,
reductionSyms);

EntryBlockArgs args;
// TODO: Add private syms and vars.
args.reduction.syms = reductionSyms;
args.reduction.vars = clauseOps.reductionVars;

auto genRegionEntryCB = [&](mlir::Operation *op) {
genEntryBlock(converter.getFirOpBuilder(), args, op->getRegion(0));
bindEntryBlockArgs(
converter, llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(op), args);
return llvm::to_vector(args.getSyms());
};

return genOpWithBody<mlir::omp::TeamsOp>(
OpWithBodyGenInfo(converter, symTable, semaCtx, loc, eval,
llvm::omp::Directive::OMPD_teams)
.setClauses(&item->clauses),
.setClauses(&item->clauses)
.setGenRegionEntryCb(genRegionEntryCB),
queue, item, clauseOps);
}

Expand Down
12 changes: 0 additions & 12 deletions flang/test/Lower/OpenMP/Todo/reduction-teams.f90

This file was deleted.

18 changes: 18 additions & 0 deletions flang/test/Lower/OpenMP/reduction-teams.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
! RUN: bbc -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s

! CHECK: omp.declare_reduction @[[RED:.*]] : i32 init {

! CHECK: func.func @_QPreduction_teams() {
subroutine reduction_teams()
integer :: i
i = 0

! CHECK: omp.teams reduction(@[[RED]] %{{.*}}#0 -> %[[PRIV_I:.*]] : !fir.ref<i32>) {
!$omp teams reduction(+:i)
! CHECK: %[[DECL_I:.*]]:2 = hlfir.declare %[[PRIV_I]]
! CHECK: %{{.*}} = fir.load %[[DECL_I]]#0 : !fir.ref<i32>
! CHECK: hlfir.assign %{{.*}} to %[[DECL_I]]#0 : i32, !fir.ref<i32>
i = i + 1
!$omp end teams
end subroutine reduction_teams

0 comments on commit 82b9eb1

Please sign in to comment.