Skip to content

Commit fe6a28b

Browse files
committed
[llvm][mlir][OpenMP] Support translation for linear clause in omp.wsloop and omp.simd
1 parent 8565fbc commit fe6a28b

File tree

9 files changed

+220
-112
lines changed

9 files changed

+220
-112
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,11 +1175,16 @@ bool ClauseProcessor::processLinear(mlir::omp::LinearClauseOps &result) const {
11751175
omp::clause::Linear>([&](const omp::clause::Linear &clause,
11761176
const parser::CharBlock &) {
11771177
auto &objects = std::get<omp::ObjectList>(clause.t);
1178+
std::vector<mlir::Attribute> typeAttrs;
11781179
for (const omp::Object &object : objects) {
11791180
semantics::Symbol *sym = object.sym();
11801181
const mlir::Value variable = converter.getSymbolAddress(*sym);
11811182
result.linearVars.push_back(variable);
1183+
mlir::Type ty = converter.genType(*sym);
1184+
typeAttrs.push_back(mlir::TypeAttr::get(ty));
11821185
}
1186+
result.linearVarTypes =
1187+
std::move(mlir::ArrayAttr::get(&converter.getMLIRContext(), typeAttrs));
11831188
if (objects.size()) {
11841189
if (auto &mod =
11851190
std::get<std::optional<omp::clause::Linear::StepComplexModifier>>(

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,8 +1637,7 @@ static void genSimdClauses(
16371637
cp.processReduction(loc, clauseOps, reductionSyms);
16381638
cp.processSafelen(clauseOps);
16391639
cp.processSimdlen(clauseOps);
1640-
1641-
cp.processTODO<clause::Linear>(loc, llvm::omp::Directive::OMPD_simd);
1640+
cp.processLinear(clauseOps);
16421641
}
16431642

16441643
static void genSingleClauses(lower::AbstractConverter &converter,
@@ -1828,9 +1827,9 @@ static void genWsloopClauses(
18281827
cp.processOrdered(clauseOps);
18291828
cp.processReduction(loc, clauseOps, reductionSyms);
18301829
cp.processSchedule(stmtCtx, clauseOps);
1830+
cp.processLinear(clauseOps);
18311831

1832-
cp.processTODO<clause::Allocate, clause::Linear>(
1833-
loc, llvm::omp::Directive::OMPD_do);
1832+
cp.processTODO<clause::Allocate>(loc, llvm::omp::Directive::OMPD_do);
18341833
}
18351834

18361835
//===----------------------------------------------------------------------===//

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

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
! This test checks lowering of OpenMP DO Directive (Worksharing)
2+
! with linear clause
3+
4+
! RUN: %flang_fc1 -fopenmp -emit-hlfir %s -o - 2>&1 | FileCheck %s
5+
6+
!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFsimple_linearEx"}
7+
!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFsimple_linearEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
8+
!CHECK: %[[const:.*]] = arith.constant 1 : i32
9+
subroutine simple_linear
10+
implicit none
11+
integer :: x, y, i
12+
!CHECK: omp.simd linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
13+
!$omp simd linear(x)
14+
!CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
15+
!CHECK: %[[const:.*]] = arith.constant 2 : i32
16+
!CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32
17+
do i = 1, 10
18+
y = x + 2
19+
end do
20+
!CHECK: } {linear_var_types = [i32]}
21+
end subroutine
22+
23+
24+
!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_stepEx"}
25+
!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_stepEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
26+
subroutine linear_step
27+
implicit none
28+
integer :: x, y, i
29+
!CHECK: %[[const:.*]] = arith.constant 4 : i32
30+
!CHECK: omp.simd linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
31+
!$omp simd linear(x:4)
32+
!CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
33+
!CHECK: %[[const:.*]] = arith.constant 2 : i32
34+
!CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32
35+
do i = 1, 10
36+
y = x + 2
37+
end do
38+
!CHECK: } {linear_var_types = [i32]}
39+
end subroutine
40+
41+
!CHECK: %[[A_alloca:.*]] = fir.alloca i32 {bindc_name = "a", uniq_name = "_QFlinear_exprEa"}
42+
!CHECK: %[[A:.*]]:2 = hlfir.declare %[[A_alloca]] {uniq_name = "_QFlinear_exprEa"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
43+
!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_exprEx"}
44+
!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_exprEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
45+
subroutine linear_expr
46+
implicit none
47+
integer :: x, y, i, a
48+
!CHECK: %[[LOAD_A:.*]] = fir.load %[[A]]#0 : !fir.ref<i32>
49+
!CHECK: %[[const:.*]] = arith.constant 4 : i32
50+
!CHECK: %[[LINEAR_EXPR:.*]] = arith.addi %[[LOAD_A]], %[[const]] : i32
51+
!CHECK: omp.simd linear(%[[X]]#0 = %[[LINEAR_EXPR]] : !fir.ref<i32>) {{.*}}
52+
!$omp simd linear(x:a+4)
53+
do i = 1, 10
54+
y = x + 2
55+
end do
56+
!CHECK: } {linear_var_types = [i32]}
57+
end subroutine
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
! This test checks lowering of OpenMP DO Directive (Worksharing)
2+
! with linear clause
3+
4+
! RUN: %flang_fc1 -fopenmp -emit-hlfir %s -o - 2>&1 | FileCheck %s
5+
6+
!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFsimple_linearEx"}
7+
!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFsimple_linearEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
8+
!CHECK: %[[const:.*]] = arith.constant 1 : i32
9+
subroutine simple_linear
10+
implicit none
11+
integer :: x, y, i
12+
!CHECK: omp.wsloop linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
13+
!$omp do linear(x)
14+
!CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
15+
!CHECK: %[[const:.*]] = arith.constant 2 : i32
16+
!CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32
17+
do i = 1, 10
18+
y = x + 2
19+
end do
20+
!$omp end do
21+
!CHECK: } {linear_var_types = [i32]}
22+
end subroutine
23+
24+
25+
!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_stepEx"}
26+
!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_stepEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
27+
subroutine linear_step
28+
implicit none
29+
integer :: x, y, i
30+
!CHECK: %[[const:.*]] = arith.constant 4 : i32
31+
!CHECK: omp.wsloop linear(%[[X]]#0 = %[[const]] : !fir.ref<i32>) {{.*}}
32+
!$omp do linear(x:4)
33+
!CHECK: %[[LOAD:.*]] = fir.load %[[X]]#0 : !fir.ref<i32>
34+
!CHECK: %[[const:.*]] = arith.constant 2 : i32
35+
!CHECK: %[[RESULT:.*]] = arith.addi %[[LOAD]], %[[const]] : i32
36+
do i = 1, 10
37+
y = x + 2
38+
end do
39+
!$omp end do
40+
!CHECK: } {linear_var_types = [i32]}
41+
end subroutine
42+
43+
!CHECK: %[[A_alloca:.*]] = fir.alloca i32 {bindc_name = "a", uniq_name = "_QFlinear_exprEa"}
44+
!CHECK: %[[A:.*]]:2 = hlfir.declare %[[A_alloca]] {uniq_name = "_QFlinear_exprEa"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
45+
!CHECK: %[[X_alloca:.*]] = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFlinear_exprEx"}
46+
!CHECK: %[[X:.*]]:2 = hlfir.declare %[[X_alloca]] {uniq_name = "_QFlinear_exprEx"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
47+
subroutine linear_expr
48+
implicit none
49+
integer :: x, y, i, a
50+
!CHECK: %[[LOAD_A:.*]] = fir.load %[[A]]#0 : !fir.ref<i32>
51+
!CHECK: %[[const:.*]] = arith.constant 4 : i32
52+
!CHECK: %[[LINEAR_EXPR:.*]] = arith.addi %[[LOAD_A]], %[[const]] : i32
53+
!CHECK: omp.wsloop linear(%[[X]]#0 = %[[LINEAR_EXPR]] : !fir.ref<i32>) {{.*}}
54+
!$omp do linear(x:a+4)
55+
do i = 1, 10
56+
y = x + 2
57+
end do
58+
!$omp end do
59+
!CHECK: } {linear_var_types = [i32]}
60+
end subroutine

mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
include "mlir/Dialect/OpenMP/OpenMPOpBase.td"
2323
include "mlir/IR/SymbolInterfaces.td"
24+
include "mlir/IR/BuiltinAttributes.td"
2425

2526
//===----------------------------------------------------------------------===//
2627
// V5.2: [6.3] `align` clause
@@ -723,10 +724,9 @@ class OpenMP_LinearClauseSkip<
723724
bit description = false, bit extraClassDeclaration = false
724725
> : OpenMP_Clause<traits, arguments, assemblyFormat, description,
725726
extraClassDeclaration> {
726-
let arguments = (ins
727-
Variadic<AnyType>:$linear_vars,
728-
Variadic<I32>:$linear_step_vars
729-
);
727+
let arguments = (ins Variadic<AnyType>:$linear_vars,
728+
Variadic<I32>:$linear_step_vars,
729+
OptionalAttr<ArrayAttr>:$linear_var_types);
730730

731731
let optAssemblyFormat = [{
732732
`linear` `(`

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,6 +2824,7 @@ void WsloopOp::build(OpBuilder &builder, OperationState &state,
28242824
ArrayRef<NamedAttribute> attributes) {
28252825
build(builder, state, /*allocate_vars=*/{}, /*allocator_vars=*/{},
28262826
/*linear_vars=*/ValueRange(), /*linear_step_vars=*/ValueRange(),
2827+
/*linear_var_types*/ nullptr,
28272828
/*nowait=*/false, /*order=*/nullptr, /*order_mod=*/nullptr,
28282829
/*ordered=*/nullptr, /*private_vars=*/{}, /*private_syms=*/nullptr,
28292830
/*private_needs_barrier=*/false,
@@ -2842,8 +2843,8 @@ void WsloopOp::build(OpBuilder &builder, OperationState &state,
28422843
WsloopOp::build(
28432844
builder, state,
28442845
/*allocate_vars=*/{}, /*allocator_vars=*/{}, clauses.linearVars,
2845-
clauses.linearStepVars, clauses.nowait, clauses.order, clauses.orderMod,
2846-
clauses.ordered, clauses.privateVars,
2846+
clauses.linearStepVars, clauses.linearVarTypes, clauses.nowait,
2847+
clauses.order, clauses.orderMod, clauses.ordered, clauses.privateVars,
28472848
makeArrayAttr(ctx, clauses.privateSyms), clauses.privateNeedsBarrier,
28482849
clauses.reductionMod, clauses.reductionVars,
28492850
makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
@@ -2888,17 +2889,16 @@ LogicalResult WsloopOp::verifyRegions() {
28882889
void SimdOp::build(OpBuilder &builder, OperationState &state,
28892890
const SimdOperands &clauses) {
28902891
MLIRContext *ctx = builder.getContext();
2891-
// TODO Store clauses in op: linearVars, linearStepVars
2892-
SimdOp::build(builder, state, clauses.alignedVars,
2893-
makeArrayAttr(ctx, clauses.alignments), clauses.ifExpr,
2894-
/*linear_vars=*/{}, /*linear_step_vars=*/{},
2895-
clauses.nontemporalVars, clauses.order, clauses.orderMod,
2896-
clauses.privateVars, makeArrayAttr(ctx, clauses.privateSyms),
2897-
clauses.privateNeedsBarrier, clauses.reductionMod,
2898-
clauses.reductionVars,
2899-
makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
2900-
makeArrayAttr(ctx, clauses.reductionSyms), clauses.safelen,
2901-
clauses.simdlen);
2892+
SimdOp::build(
2893+
builder, state, clauses.alignedVars,
2894+
makeArrayAttr(ctx, clauses.alignments), clauses.ifExpr,
2895+
clauses.linearVars, clauses.linearStepVars, clauses.linearVarTypes,
2896+
clauses.nontemporalVars, clauses.order, clauses.orderMod,
2897+
clauses.privateVars, makeArrayAttr(ctx, clauses.privateSyms),
2898+
clauses.privateNeedsBarrier, clauses.reductionMod, clauses.reductionVars,
2899+
makeDenseBoolArrayAttr(ctx, clauses.reductionByref),
2900+
makeArrayAttr(ctx, clauses.reductionSyms), clauses.safelen,
2901+
clauses.simdlen);
29022902
}
29032903

29042904
LogicalResult SimdOp::verify() {

0 commit comments

Comments
 (0)