Skip to content

Commit 6e8afd5

Browse files
committed
[Flang][OpenMP] Lowering linear clause to MLIR
1 parent ced15cd commit 6e8afd5

File tree

6 files changed

+93
-19
lines changed

6 files changed

+93
-19
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,36 @@ bool ClauseProcessor::processIsDevicePtr(
867867
});
868868
}
869869

870+
bool ClauseProcessor::processLinear(mlir::omp::LinearClauseOps &result) const {
871+
lower::StatementContext stmtCtx;
872+
return findRepeatableClause<omp::clause::Linear>(
873+
[&](const omp::clause::Linear &clause, const parser::CharBlock &) {
874+
auto &objects = std::get<omp::ObjectList>(clause.t);
875+
for (const omp::Object &object : objects) {
876+
semantics::Symbol *sym = object.sym();
877+
const mlir::Value variable = converter.getSymbolAddress(*sym);
878+
result.linearVars.push_back(variable);
879+
}
880+
if (objects.size()) {
881+
if (auto &mod = std::get<0>(clause.t)) {
882+
mlir::Value operand =
883+
fir::getBase(converter.genExprValue(*mod, stmtCtx));
884+
result.linearStepVars.append(objects.size(), operand);
885+
} else if (auto &mod = std::get<1>(clause.t)) {
886+
mlir::Value operand =
887+
fir::getBase(converter.genExprValue(*mod, stmtCtx));
888+
result.linearStepVars.append(objects.size(), operand);
889+
} else {
890+
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
891+
mlir::Location currentLocation = converter.getCurrentLocation();
892+
mlir::Value operand = firOpBuilder.createIntegerConstant(
893+
currentLocation, firOpBuilder.getI32Type(), 1);
894+
result.linearStepVars.append(objects.size(), operand);
895+
}
896+
}
897+
});
898+
}
899+
870900
bool ClauseProcessor::processLink(
871901
llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const {
872902
return findRepeatableClause<omp::clause::Link>(

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class ClauseProcessor {
103103
bool processIsDevicePtr(
104104
mlir::omp::IsDevicePtrClauseOps &result,
105105
llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSyms) const;
106+
bool processLinear(mlir::omp::LinearClauseOps &result) const;
106107
bool
107108
processLink(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;
108109

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,13 +1201,12 @@ static void genSimdClauses(
12011201
ClauseProcessor cp(converter, semaCtx, clauses);
12021202
cp.processAligned(clauseOps);
12031203
cp.processIf(llvm::omp::Directive::OMPD_simd, clauseOps);
1204+
cp.processLinear(clauseOps);
12041205
cp.processNontemporal(clauseOps);
12051206
cp.processOrder(clauseOps);
12061207
cp.processReduction(loc, clauseOps, reductionSyms);
12071208
cp.processSafelen(clauseOps);
12081209
cp.processSimdlen(clauseOps);
1209-
1210-
cp.processTODO<clause::Linear>(loc, llvm::omp::Directive::OMPD_simd);
12111210
}
12121211

12131212
static void genSingleClauses(lower::AbstractConverter &converter,
@@ -1354,14 +1353,14 @@ static void genWsloopClauses(
13541353
mlir::Location loc, mlir::omp::WsloopOperands &clauseOps,
13551354
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
13561355
ClauseProcessor cp(converter, semaCtx, clauses);
1356+
cp.processLinear(clauseOps);
13571357
cp.processNowait(clauseOps);
13581358
cp.processOrder(clauseOps);
13591359
cp.processOrdered(clauseOps);
13601360
cp.processReduction(loc, clauseOps, reductionSyms);
13611361
cp.processSchedule(stmtCtx, clauseOps);
13621362

1363-
cp.processTODO<clause::Allocate, clause::Linear>(
1364-
loc, llvm::omp::Directive::OMPD_do);
1363+
cp.processTODO<clause::Allocate>(loc, llvm::omp::Directive::OMPD_do);
13651364
}
13661365

13671366
//===----------------------------------------------------------------------===//

flang/test/Lower/OpenMP/Todo/omp-do-simd-linear.f90

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
! This test checks lowering of OpenMP linear clause
2+
3+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - 2>&1 | FileCheck %s
4+
5+
! CHECK-LABEL: func.func @_QPtestdolinear() {
6+
subroutine testDoLinear()
7+
implicit none
8+
integer :: i
9+
integer :: A(10)
10+
!CHECK: %[[C10:.*]] = arith.constant 10 : index
11+
!CHECK: %[[A:.*]] = fir.alloca !fir.array<10xi32> {bindc_name = "a", uniq_name = "_QFtestdolinearEa"}
12+
!CHECK: %[[S2:.*]] = fir.shape %[[C10]] : (index) -> !fir.shape<1>
13+
!CHECK: %[[A_DECL:.*]]:2 = hlfir.declare %[[A]](%[[S2]]) {uniq_name = "_QFtestdolinearEa"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
14+
!CHECK: %[[C2:.*]] = arith.constant 2 : i32
15+
!CHECK: omp.wsloop linear(%[[A_DECL]]#1 = %[[C2]] : !fir.ref<!fir.array<10xi32>>) {
16+
!$omp do linear(A:2)
17+
do i = 1, 10
18+
A(i) = i
19+
end do
20+
!$omp end do
21+
end subroutine testDoLinear
22+
23+
! CHECK-LABEL: func.func @_QPtestsimdlinear() {
24+
subroutine testSimdLinear()
25+
implicit none
26+
integer :: i
27+
integer :: A(10)
28+
!CHECK: %[[C10:.*]] = arith.constant 10 : index
29+
!CHECK: %[[A:.*]] = fir.alloca !fir.array<10xi32> {bindc_name = "a", uniq_name = "_QFtestsimdlinearEa"}
30+
!CHECK: %[[S2:.*]] = fir.shape %[[C10]] : (index) -> !fir.shape<1>
31+
!CHECK: %[[A_DECL:.*]]:2 = hlfir.declare %[[A]](%[[S2]]) {uniq_name = "_QFtestsimdlinearEa"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
32+
!CHECK: %[[C2:.*]] = arith.constant 2 : i32
33+
!CHECK: omp.simd linear(%[[A_DECL]]#1 = %[[C2]] : !fir.ref<!fir.array<10xi32>>) {
34+
!$omp simd linear(A:2)
35+
do i = 1, 10
36+
A(i) = i
37+
end do
38+
!$omp end simd
39+
end subroutine testSimdLinear
40+
41+
! CHECK-LABEL: func.func @_QPtestdosimdlinear() {
42+
subroutine testDoSimdLinear()
43+
implicit none
44+
integer :: i
45+
integer :: A(10)
46+
!CHECK: %[[C10:.*]] = arith.constant 10 : index
47+
!CHECK: %[[A:.*]] = fir.alloca !fir.array<10xi32> {bindc_name = "a", uniq_name = "_QFtestdosimdlinearEa"}
48+
!CHECK: %[[S2:.*]] = fir.shape %[[C10]] : (index) -> !fir.shape<1>
49+
!CHECK: %[[A_DECL:.*]]:2 = hlfir.declare %[[A]](%[[S2]]) {uniq_name = "_QFtestdosimdlinearEa"} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>)
50+
!CHECK: %[[C2:.*]] = arith.constant 2 : i32
51+
!CHECK: omp.wsloop {
52+
!CHECK: omp.simd linear(%[[A_DECL]]#1 = %[[C2]] : !fir.ref<!fir.array<10xi32>>) {
53+
!$omp do simd linear(A:2)
54+
do i = 1, 10
55+
A(i) = i
56+
end do
57+
!$omp end do simd
58+
end subroutine testDoSimdLinear

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ void SimdOp::build(OpBuilder &builder, OperationState &state,
20162016
// privateSyms, reductionVars, reductionByref, reductionSyms.
20172017
SimdOp::build(builder, state, clauses.alignedVars,
20182018
makeArrayAttr(ctx, clauses.alignments), clauses.ifExpr,
2019-
/*linear_vars=*/{}, /*linear_step_vars=*/{},
2019+
clauses.linearVars, clauses.linearStepVars,
20202020
clauses.nontemporalVars, clauses.order, clauses.orderMod,
20212021
/*private_vars=*/{}, /*private_syms=*/nullptr,
20222022
/*reduction_vars=*/{}, /*reduction_byref=*/nullptr,

0 commit comments

Comments
 (0)