Skip to content

Commit 7eb3af6

Browse files
committed
[Flang][OpenMP]Support for lowering Linear clause to mlir
1 parent 876f661 commit 7eb3af6

File tree

6 files changed

+93
-20
lines changed

6 files changed

+93
-20
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,36 @@ bool ClauseProcessor::processIsDevicePtr(
875875
});
876876
}
877877

878+
bool ClauseProcessor::processLinear(mlir::omp::LinearClauseOps &result) const {
879+
lower::StatementContext stmtCtx;
880+
return findRepeatableClause<omp::clause::Linear>(
881+
[&](const omp::clause::Linear &clause, const parser::CharBlock &) {
882+
auto &objects = std::get<omp::ObjectList>(clause.t);
883+
for (const omp::Object &object : objects) {
884+
semantics::Symbol *sym = object.sym();
885+
const mlir::Value variable = converter.getSymbolAddress(*sym);
886+
result.linearVars.push_back(variable);
887+
}
888+
if (objects.size()) {
889+
if (auto &mod = std::get<0>(clause.t)) {
890+
mlir::Value operand =
891+
fir::getBase(converter.genExprValue(*mod, stmtCtx));
892+
result.linearStepVars.append(objects.size(), operand);
893+
} else if (auto &mod = std::get<1>(clause.t)) {
894+
mlir::Value operand =
895+
fir::getBase(converter.genExprValue(*mod, stmtCtx));
896+
result.linearStepVars.append(objects.size(), operand);
897+
} else {
898+
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
899+
mlir::Location currentLocation = converter.getCurrentLocation();
900+
mlir::Value operand = firOpBuilder.createIntegerConstant(
901+
currentLocation, firOpBuilder.getI32Type(), 1);
902+
result.linearStepVars.append(objects.size(), operand);
903+
}
904+
}
905+
});
906+
}
907+
878908
bool ClauseProcessor::processLink(
879909
llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const {
880910
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
@@ -107,6 +107,7 @@ class ClauseProcessor {
107107
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes,
108108
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs,
109109
llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSymbols) const;
110+
bool processLinear(mlir::omp::LinearClauseOps &result) const;
110111
bool
111112
processLink(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;
112113

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,13 +1132,12 @@ static void genSimdClauses(lower::AbstractConverter &converter,
11321132
ClauseProcessor cp(converter, semaCtx, clauses);
11331133
cp.processAligned(clauseOps);
11341134
cp.processIf(llvm::omp::Directive::OMPD_simd, clauseOps);
1135+
cp.processLinear(clauseOps);
11351136
cp.processNontemporal(clauseOps);
11361137
cp.processOrder(clauseOps);
11371138
cp.processReduction(loc, clauseOps);
11381139
cp.processSafelen(clauseOps);
11391140
cp.processSimdlen(clauseOps);
1140-
1141-
cp.processTODO<clause::Linear>(loc, llvm::omp::Directive::OMPD_simd);
11421141
}
11431142

11441143
static void genSingleClauses(lower::AbstractConverter &converter,
@@ -1300,14 +1299,12 @@ static void genWsloopClauses(
13001299
llvm::SmallVectorImpl<mlir::Type> &reductionTypes,
13011300
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
13021301
ClauseProcessor cp(converter, semaCtx, clauses);
1302+
cp.processLinear(clauseOps);
13031303
cp.processNowait(clauseOps);
13041304
cp.processOrder(clauseOps);
13051305
cp.processOrdered(clauseOps);
13061306
cp.processReduction(loc, clauseOps, &reductionTypes, &reductionSyms);
13071307
cp.processSchedule(stmtCtx, clauseOps);
1308-
1309-
cp.processTODO<clause::Allocate, clause::Linear>(
1310-
loc, llvm::omp::Directive::OMPD_do);
13111308
}
13121309

13131310
//===----------------------------------------------------------------------===//

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

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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
59+

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)