Skip to content

Commit 19291b7

Browse files
committed
[Flang][OpenMP] Add Lowering support for taskloop reductions
Support for lowering the Reduction clause support already exists, so we can extend the support for taskloop to include reduction. As support for Reduction in taskloop was only added in OpenMP 5.0, the use of the Clause has been restricted to that version of OpenMP or greater.
1 parent ce5dac6 commit 19291b7

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,21 +1763,22 @@ static void genTaskgroupClauses(
17631763
cp.processTaskReduction(loc, clauseOps, taskReductionSyms);
17641764
}
17651765

1766-
static void genTaskloopClauses(lower::AbstractConverter &converter,
1767-
semantics::SemanticsContext &semaCtx,
1768-
lower::StatementContext &stmtCtx,
1769-
const List<Clause> &clauses, mlir::Location loc,
1770-
mlir::omp::TaskloopOperands &clauseOps) {
1766+
static void genTaskloopClauses(
1767+
lower::AbstractConverter &converter, semantics::SemanticsContext &semaCtx,
1768+
lower::StatementContext &stmtCtx, const List<Clause> &clauses,
1769+
mlir::Location loc, mlir::omp::TaskloopOperands &clauseOps,
1770+
llvm::SmallVectorImpl<const semantics::Symbol *> &taskReductionSyms) {
17711771

17721772
ClauseProcessor cp(converter, semaCtx, clauses);
17731773
cp.processGrainsize(stmtCtx, clauseOps);
17741774
cp.processNumTasks(stmtCtx, clauseOps);
1775+
cp.processReduction(loc, clauseOps, taskReductionSyms);
17751776

17761777
cp.processTODO<clause::Allocate, clause::Collapse, clause::Default,
17771778
clause::Final, clause::If, clause::InReduction,
17781779
clause::Lastprivate, clause::Mergeable, clause::Nogroup,
1779-
clause::Priority, clause::Reduction, clause::Shared,
1780-
clause::Untied>(loc, llvm::omp::Directive::OMPD_taskloop);
1780+
clause::Priority, clause::Shared, clause::Untied>(
1781+
loc, llvm::omp::Directive::OMPD_taskloop);
17811782
}
17821783

17831784
static void genTaskwaitClauses(lower::AbstractConverter &converter,
@@ -2979,8 +2980,9 @@ static mlir::omp::TaskloopOp genStandaloneTaskloop(
29792980
lower::pft::Evaluation &eval, mlir::Location loc,
29802981
const ConstructQueue &queue, ConstructQueue::const_iterator item) {
29812982
mlir::omp::TaskloopOperands taskloopClauseOps;
2983+
llvm::SmallVector<const semantics::Symbol *> taskReductionSyms;
29822984
genTaskloopClauses(converter, semaCtx, stmtCtx, item->clauses, loc,
2983-
taskloopClauseOps);
2985+
taskloopClauseOps, taskReductionSyms);
29842986
DataSharingProcessor dsp(converter, semaCtx, item->clauses, eval,
29852987
/*shouldCollectPreDeterminedSymbols=*/true,
29862988
enableDelayedPrivatization, symTable);
@@ -2994,6 +2996,8 @@ static mlir::omp::TaskloopOp genStandaloneTaskloop(
29942996
EntryBlockArgs taskloopArgs;
29952997
taskloopArgs.priv.syms = dsp.getDelayedPrivSymbols();
29962998
taskloopArgs.priv.vars = taskloopClauseOps.privateVars;
2999+
taskloopArgs.reduction.syms = taskReductionSyms;
3000+
taskloopArgs.reduction.vars = taskloopClauseOps.reductionVars;
29973001

29983002
auto taskLoopOp = genWrapperOp<mlir::omp::TaskloopOp>(
29993003
converter, loc, taskloopClauseOps, taskloopArgs);

flang/test/Lower/OpenMP/Todo/taskloop-reduction.f90

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
! This test checks the lowering of the reduction clause in the taskloop construct
2+
! RUN: bbc -emit-hlfir -fopenmp -fopenmp-version=50 -o - %s 2>&1 | FileCheck %s
3+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 -o - %s 2>&1 | FileCheck %s
4+
! RUN %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=45 -o - %s 2>&1 | FileCheck %s --check-prefix=CHECK-VERSION
5+
6+
! CHECK-VERSION: error: REDUCTION clause is not allowed on directive TASKLOOP in OpenMP v4.5, try -fopenmp-version=50
7+
8+
! CHECK-LABEL: omp.private
9+
! CHECK-SAME: {type = private} @[[I_PRIVATE:.*]] : i32
10+
11+
! CHECK-LABEL: func.func @_QPtest_reduction()
12+
! CHECK: %[[ALLOCA_A:.*]] = fir.alloca !fir.array<10xi32> {bindc_name = "a", uniq_name = "_QFtest_reductionEa"}
13+
! CHECK: %[[DECLARE_A:.*]]:2 = hlfir.declare %[[ALLOCA_A]](%2) {uniq_name = "_QFtest_reductionEa"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
14+
! CHECK: %[[ALLOCA_I:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_reductionEi"}
15+
! CHECK: %[[DECLARE_I:.*]]:2 = hlfir.declare %[[ALLOCA_I]] {uniq_name = "_QFtest_reductionEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
16+
! CHECK: %[[ALLOCA_SUM_I:.*]] = fir.alloca i32 {bindc_name = "sum_i", uniq_name = "_QFtest_reductionEsum_i"}
17+
! CHECK: %[[DECLARE_SUM_I:.*]]:2 = hlfir.declare %[[ALLOCA_SUM_I]] {uniq_name = "_QFtest_reductionEsum_i"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
18+
19+
subroutine test_reduction
20+
integer :: i, a(10), sum_i
21+
22+
! CHECK: omp.taskloop
23+
! CHECK-SAME: private(@[[I_PRIVATE]] %[[DECLARE_I]]#0 -> %arg0 : !fir.ref<i32>) reduction(@add_reduction_i32 %[[DECLARE_SUM_I]]#0 -> %arg1 : !fir.ref<i32>) {
24+
!$omp taskloop reduction (+:sum_i)
25+
do i = 1,10
26+
sum_i = sum_i + i
27+
end do
28+
!$omp end taskloop
29+
30+
end subroutine

llvm/include/llvm/Frontend/OpenMP/OMP.td

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,17 +1284,16 @@ def OMP_TaskGroup : Directive<[Spelling<"taskgroup">]> {
12841284
let category = CA_Executable;
12851285
}
12861286
def OMP_TaskLoop : Directive<[Spelling<"taskloop">]> {
1287-
let allowedClauses = [
1288-
VersionedClause<OMPC_Allocate>,
1289-
VersionedClause<OMPC_FirstPrivate>,
1290-
VersionedClause<OMPC_InReduction>,
1291-
VersionedClause<OMPC_LastPrivate>,
1292-
VersionedClause<OMPC_Mergeable>,
1293-
VersionedClause<OMPC_NoGroup>,
1294-
VersionedClause<OMPC_Private>,
1295-
VersionedClause<OMPC_Reduction>,
1296-
VersionedClause<OMPC_Shared>,
1297-
VersionedClause<OMPC_Untied>,
1287+
let allowedClauses = [VersionedClause<OMPC_Allocate>,
1288+
VersionedClause<OMPC_FirstPrivate>,
1289+
VersionedClause<OMPC_InReduction>,
1290+
VersionedClause<OMPC_LastPrivate>,
1291+
VersionedClause<OMPC_Mergeable>,
1292+
VersionedClause<OMPC_NoGroup>,
1293+
VersionedClause<OMPC_Private>,
1294+
VersionedClause<OMPC_Reduction, 50>,
1295+
VersionedClause<OMPC_Shared>,
1296+
VersionedClause<OMPC_Untied>,
12981297
];
12991298
let allowedOnceClauses = [
13001299
VersionedClause<OMPC_Collapse>,

0 commit comments

Comments
 (0)