Skip to content

Commit ea3534b

Browse files
authored
[flang][OpenMP] Parse AFFINITY clause, lowering not supported yet (#113485)
Implement parsing of the AFFINITY clause on TASK construct, conversion from the parser class to omp::Clause. Lowering to HLFIR is unsupported, a TODO message is displayed.
1 parent 9575ab2 commit ea3534b

File tree

10 files changed

+129
-3
lines changed

10 files changed

+129
-3
lines changed

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ class ParseTreeDumper {
476476
NODE(parser, OldParameterStmt)
477477
NODE(parser, OmpIteratorSpecifier)
478478
NODE(parser, OmpIteratorModifier)
479+
NODE(parser, OmpAffinityClause)
479480
NODE(parser, OmpAlignedClause)
480481
NODE(parser, OmpAtomic)
481482
NODE(parser, OmpAtomicCapture)

flang/include/flang/Parser/parse-tree.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,6 +3458,13 @@ struct OmpReductionOperator {
34583458

34593459
// --- Clauses
34603460

3461+
// OMP 5.0 2.10.1 affinity([aff-modifier:] locator-list)
3462+
// aff-modifier: interator-modifier
3463+
struct OmpAffinityClause {
3464+
TUPLE_CLASS_BOILERPLATE(OmpAffinityClause);
3465+
std::tuple<std::optional<OmpIteratorModifier>, OmpObjectList> t;
3466+
};
3467+
34613468
// 2.8.1 aligned-clause -> ALIGNED (variable-name-list[ : scalar-constant])
34623469
struct OmpAlignedClause {
34633470
TUPLE_CLASS_BOILERPLATE(OmpAlignedClause);

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,15 @@ Absent make(const parser::OmpClause::Absent &inp,
352352

353353
Affinity make(const parser::OmpClause::Affinity &inp,
354354
semantics::SemanticsContext &semaCtx) {
355-
// inp -> empty
356-
llvm_unreachable("Empty: affinity");
355+
// inp.v -> parser::OmpAffinityClause
356+
auto &t0 = std::get<std::optional<parser::OmpIteratorModifier>>(inp.v.t);
357+
auto &t1 = std::get<parser::OmpObjectList>(inp.v.t);
358+
359+
auto &&maybeIter =
360+
maybeApply([&](auto &&s) { return makeIterator(s, semaCtx); }, t0);
361+
362+
return Affinity{{/*Iterator=*/std::move(maybeIter),
363+
/*LocatorList=*/makeObjects(t1, semaCtx)}};
357364
}
358365

359366
Align make(const parser::OmpClause::Align &inp,

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2769,7 +2769,8 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
27692769

27702770
for (const Clause &clause : clauses) {
27712771
mlir::Location clauseLocation = converter.genLocation(clause.source);
2772-
if (!std::holds_alternative<clause::Allocate>(clause.u) &&
2772+
if (!std::holds_alternative<clause::Affinity>(clause.u) &&
2773+
!std::holds_alternative<clause::Allocate>(clause.u) &&
27732774
!std::holds_alternative<clause::Copyin>(clause.u) &&
27742775
!std::holds_alternative<clause::Copyprivate>(clause.u) &&
27752776
!std::holds_alternative<clause::Default>(clause.u) &&

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ TYPE_PARSER(construct<OmpIteratorSpecifier>(
177177
TYPE_PARSER(construct<OmpIteratorModifier>("ITERATOR" >>
178178
parenthesized(nonemptyList(sourced(Parser<OmpIteratorSpecifier>{})))))
179179

180+
// [5.0] 2.10.1 affinity([aff-modifier:] locator-list)
181+
// aff-modifier: interator-modifier
182+
TYPE_PARSER(construct<OmpAffinityClause>(
183+
maybe(Parser<OmpIteratorModifier>{} / ":"), Parser<OmpObjectList>{}))
184+
180185
// 2.15.3.1 DEFAULT (PRIVATE | FIRSTPRIVATE | SHARED | NONE)
181186
TYPE_PARSER(construct<OmpDefaultClause>(
182187
"PRIVATE" >> pure(OmpDefaultClause::Type::Private) ||
@@ -415,6 +420,8 @@ TYPE_PARSER(construct<OmpLastprivateClause>(
415420
TYPE_PARSER(
416421
"ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
417422
"ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
423+
"AFFINITY" >> construct<OmpClause>(construct<OmpClause::Affinity>(
424+
parenthesized(Parser<OmpAffinityClause>{}))) ||
418425
"ALIGNED" >> construct<OmpClause>(construct<OmpClause::Aligned>(
419426
parenthesized(Parser<OmpAlignedClause>{}))) ||
420427
"ALLOCATE" >> construct<OmpClause>(construct<OmpClause::Allocate>(

flang/lib/Parser/unparse.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,10 @@ class UnparseVisitor {
21292129
Walk(std::get<std::optional<OmpDeviceClause::DeviceModifier>>(x.t), ":");
21302130
Walk(std::get<ScalarIntExpr>(x.t));
21312131
}
2132+
void Unparse(const OmpAffinityClause &x) {
2133+
Walk(std::get<std::optional<OmpIteratorModifier>>(x.t), ":");
2134+
Walk(std::get<OmpObjectList>(x.t));
2135+
}
21322136
void Unparse(const OmpAlignedClause &x) {
21332137
Walk(std::get<OmpObjectList>(x.t));
21342138
Put(",");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
!RUN: %not_todo_cmd bbc -emit-hlfir -fopenmp -fopenmp-version=50 -o - %s 2>&1 | FileCheck %s
2+
!RUN: %not_todo_cmd %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 -o - %s 2>&1 | FileCheck %s
3+
4+
!CHECK: not yet implemented: Unhandled clause AFFINITY in TASK construct
5+
subroutine f00(x)
6+
integer :: x(10)
7+
!$omp task affinity(x)
8+
x = x + 1
9+
!$omp end task
10+
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
!RUN: %flang_fc1 -fdebug-unparse -fopenmp -fopenmp-version=50 %s | FileCheck --ignore-case --check-prefix="UNPARSE" %s
2+
!RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp -fopenmp-version=50 %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
4+
subroutine f00(x)
5+
integer :: x(10)
6+
!$omp task affinity(x)
7+
x = x + 1
8+
!$omp end task
9+
end
10+
11+
!UNPARSE: SUBROUTINE f00 (x)
12+
!UNPARSE: INTEGER x(10_4)
13+
!UNPARSE: !$OMP TASK AFFINITY(x)
14+
!UNPARSE: x=x+1_4
15+
!UNPARSE: !$OMP END TASK
16+
!UNPARSE: END SUBROUTINE
17+
18+
!PARSE-TREE: OmpBeginBlockDirective
19+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = task
20+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Affinity -> OmpAffinityClause
21+
!PARSE-TREE: | | OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'x'
22+
23+
subroutine f01(x)
24+
integer :: x(10)
25+
!$omp task affinity(x(1), x(3))
26+
x = x + 1
27+
!$omp end task
28+
end
29+
30+
!UNPARSE: SUBROUTINE f01 (x)
31+
!UNPARSE: INTEGER x(10_4)
32+
!UNPARSE: !$OMP TASK AFFINITY(x(1_4),x(3_4))
33+
!UNPARSE: x=x+1_4
34+
!UNPARSE: !$OMP END TASK
35+
!UNPARSE: END SUBROUTINE
36+
37+
!PARSE-TREE: OmpBeginBlockDirective
38+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = task
39+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Affinity -> OmpAffinityClause
40+
!PARSE-TREE: | | OmpObjectList -> OmpObject -> Designator -> DataRef -> ArrayElement
41+
!PARSE-TREE: | | | DataRef -> Name = 'x'
42+
!PARSE-TREE: | | | SectionSubscript -> Integer -> Expr = '1_4'
43+
!PARSE-TREE: | | | | LiteralConstant -> IntLiteralConstant = '1'
44+
!PARSE-TREE: | | OmpObject -> Designator -> DataRef -> ArrayElement
45+
!PARSE-TREE: | | | DataRef -> Name = 'x'
46+
!PARSE-TREE: | | | SectionSubscript -> Integer -> Expr = '3_4'
47+
!PARSE-TREE: | | | | LiteralConstant -> IntLiteralConstant = '3'
48+
49+
subroutine f02(x)
50+
integer :: x(10)
51+
!$omp task affinity(iterator(i = 1:3): x(i))
52+
x = x + 1
53+
!$omp end task
54+
end
55+
56+
!UNPARSE: SUBROUTINE f02 (x)
57+
!UNPARSE: INTEGER x(10_4)
58+
!UNPARSE: !$OMP TASK AFFINITY(ITERATOR(INTEGER i = 1_4:3_4):x(i))
59+
!UNPARSE: x=x+1_4
60+
!UNPARSE: !$OMP END TASK
61+
!UNPARSE: END SUBROUTINE
62+
63+
!PARSE-TREE: OmpBeginBlockDirective
64+
!PARSE-TREE: | OmpBlockDirective -> llvm::omp::Directive = task
65+
!PARSE-TREE: | OmpClauseList -> OmpClause -> Affinity -> OmpAffinityClause
66+
!PARSE-TREE: | | OmpIteratorModifier -> OmpIteratorSpecifier
67+
!PARSE-TREE: | | | TypeDeclarationStmt
68+
!PARSE-TREE: | | | | DeclarationTypeSpec -> IntrinsicTypeSpec -> IntegerTypeSpec ->
69+
!PARSE-TREE: | | | | EntityDecl
70+
!PARSE-TREE: | | | | | Name = 'i'
71+
!PARSE-TREE: | | | SubscriptTriplet
72+
!PARSE-TREE: | | | | Scalar -> Integer -> Expr = '1_4'
73+
!PARSE-TREE: | | | | | LiteralConstant -> IntLiteralConstant = '1'
74+
!PARSE-TREE: | | | | Scalar -> Integer -> Expr = '3_4'
75+
!PARSE-TREE: | | | | | LiteralConstant -> IntLiteralConstant = '3'
76+
!PARSE-TREE: | | OmpObjectList -> OmpObject -> Designator -> DataRef -> ArrayElement
77+
!PARSE-TREE: | | | DataRef -> Name = 'x'
78+
!PARSE-TREE: | | | SectionSubscript -> Integer -> Expr = 'i'
79+
!PARSE-TREE: | | | | Designator -> DataRef -> Name = 'i'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=45
2+
3+
subroutine f00(x)
4+
integer :: x(10)
5+
!ERROR: AFFINITY clause is not allowed on directive TASK in OpenMP v4.5, try -fopenmp-version=50
6+
!$omp task affinity(x)
7+
x = x + 1
8+
!$omp end task
9+
end

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def OMPC_AdjustArgs : Clause<"adjust_args"> {
4545
}
4646
def OMPC_Affinity : Clause<"affinity"> {
4747
let clangClass = "OMPAffinityClause";
48+
let flangClass = "OmpAffinityClause";
4849
}
4950
def OMPC_Align : Clause<"align"> {
5051
let clangClass = "OMPAlignClause";

0 commit comments

Comments
 (0)