Skip to content

[Flang][OpenMP] Support for lowering Linear clause to mlir #111085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions flang/lib/Lower/OpenMP/ClauseProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,36 @@ bool ClauseProcessor::processIsDevicePtr(
});
}

bool ClauseProcessor::processLinear(mlir::omp::LinearClauseOps &result) const {
lower::StatementContext stmtCtx;
return findRepeatableClause<omp::clause::Linear>(
[&](const omp::clause::Linear &clause, const parser::CharBlock &) {
auto &objects = std::get<omp::ObjectList>(clause.t);
for (const omp::Object &object : objects) {
semantics::Symbol *sym = object.sym();
const mlir::Value variable = converter.getSymbolAddress(*sym);
result.linearVars.push_back(variable);
}
if (objects.size()) {
if (auto &mod = std::get<0>(clause.t)) {
mlir::Value operand =
fir::getBase(converter.genExprValue(*mod, stmtCtx));
result.linearStepVars.append(objects.size(), operand);
} else if (auto &mod = std::get<1>(clause.t)) {
mlir::Value operand =
fir::getBase(converter.genExprValue(*mod, stmtCtx));
result.linearStepVars.append(objects.size(), operand);
} else {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
mlir::Location currentLocation = converter.getCurrentLocation();
mlir::Value operand = firOpBuilder.createIntegerConstant(
currentLocation, firOpBuilder.getI32Type(), 1);
result.linearStepVars.append(objects.size(), operand);
}
}
});
}

bool ClauseProcessor::processLink(
llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const {
return findRepeatableClause<omp::clause::Link>(
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Lower/OpenMP/ClauseProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ClauseProcessor {
bool processIsDevicePtr(
mlir::omp::IsDevicePtrClauseOps &result,
llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSyms) const;
bool processLinear(mlir::omp::LinearClauseOps &result) const;
bool
processLink(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;

Expand Down
7 changes: 3 additions & 4 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,13 +1201,12 @@ static void genSimdClauses(
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processAligned(clauseOps);
cp.processIf(llvm::omp::Directive::OMPD_simd, clauseOps);
cp.processLinear(clauseOps);
cp.processNontemporal(clauseOps);
cp.processOrder(clauseOps);
cp.processReduction(loc, clauseOps, reductionSyms);
cp.processSafelen(clauseOps);
cp.processSimdlen(clauseOps);

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

static void genSingleClauses(lower::AbstractConverter &converter,
Expand Down Expand Up @@ -1354,14 +1353,14 @@ static void genWsloopClauses(
mlir::Location loc, mlir::omp::WsloopOperands &clauseOps,
llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processLinear(clauseOps);
cp.processNowait(clauseOps);
cp.processOrder(clauseOps);
cp.processOrdered(clauseOps);
cp.processReduction(loc, clauseOps, reductionSyms);
cp.processSchedule(stmtCtx, clauseOps);

cp.processTODO<clause::Allocate, clause::Linear>(
loc, llvm::omp::Directive::OMPD_do);
cp.processTODO<clause::Allocate>(loc, llvm::omp::Directive::OMPD_do);
}

//===----------------------------------------------------------------------===//
Expand Down
14 changes: 0 additions & 14 deletions flang/test/Lower/OpenMP/Todo/omp-do-simd-linear.f90

This file was deleted.

58 changes: 58 additions & 0 deletions flang/test/Lower/OpenMP/linear-clause.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
! This test checks lowering of OpenMP linear clause

! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - 2>&1 | FileCheck %s

! CHECK-LABEL: func.func @_QPtestdolinear() {
subroutine testDoLinear()
implicit none
integer :: i
integer :: A(10)
!CHECK: %[[C10:.*]] = arith.constant 10 : index
!CHECK: %[[A:.*]] = fir.alloca !fir.array<10xi32> {bindc_name = "a", uniq_name = "_QFtestdolinearEa"}
!CHECK: %[[S2:.*]] = fir.shape %[[C10]] : (index) -> !fir.shape<1>
!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>>)
!CHECK: %[[C2:.*]] = arith.constant 2 : i32
!CHECK: omp.wsloop linear(%[[A_DECL]]#1 = %[[C2]] : !fir.ref<!fir.array<10xi32>>) {
!$omp do linear(A:2)
do i = 1, 10
A(i) = i
end do
!$omp end do
end subroutine testDoLinear

! CHECK-LABEL: func.func @_QPtestsimdlinear() {
subroutine testSimdLinear()
implicit none
integer :: i
integer :: A(10)
!CHECK: %[[C10:.*]] = arith.constant 10 : index
!CHECK: %[[A:.*]] = fir.alloca !fir.array<10xi32> {bindc_name = "a", uniq_name = "_QFtestsimdlinearEa"}
!CHECK: %[[S2:.*]] = fir.shape %[[C10]] : (index) -> !fir.shape<1>
!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>>)
!CHECK: %[[C2:.*]] = arith.constant 2 : i32
!CHECK: omp.simd linear(%[[A_DECL]]#1 = %[[C2]] : !fir.ref<!fir.array<10xi32>>) {
!$omp simd linear(A:2)
do i = 1, 10
A(i) = i
end do
!$omp end simd
end subroutine testSimdLinear

! CHECK-LABEL: func.func @_QPtestdosimdlinear() {
subroutine testDoSimdLinear()
implicit none
integer :: i
integer :: A(10)
!CHECK: %[[C10:.*]] = arith.constant 10 : index
!CHECK: %[[A:.*]] = fir.alloca !fir.array<10xi32> {bindc_name = "a", uniq_name = "_QFtestdosimdlinearEa"}
!CHECK: %[[S2:.*]] = fir.shape %[[C10]] : (index) -> !fir.shape<1>
!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>>)
!CHECK: %[[C2:.*]] = arith.constant 2 : i32
!CHECK: omp.wsloop {
!CHECK: omp.simd linear(%[[A_DECL]]#1 = %[[C2]] : !fir.ref<!fir.array<10xi32>>) {
!$omp do simd linear(A:2)
do i = 1, 10
A(i) = i
end do
!$omp end do simd
end subroutine testDoSimdLinear
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ void SimdOp::build(OpBuilder &builder, OperationState &state,
// privateSyms, reductionVars, reductionByref, reductionSyms.
SimdOp::build(builder, state, clauses.alignedVars,
makeArrayAttr(ctx, clauses.alignments), clauses.ifExpr,
/*linear_vars=*/{}, /*linear_step_vars=*/{},
clauses.linearVars, clauses.linearStepVars,
clauses.nontemporalVars, clauses.order, clauses.orderMod,
/*private_vars=*/{}, /*private_syms=*/nullptr,
/*reduction_vars=*/{}, /*reduction_byref=*/nullptr,
Expand Down
Loading