Skip to content

Commit 05b7e97

Browse files
authored
[flang][OpenMP] Extend common::AtomicDefaultMemOrderType enumeration (llvm#136312)
Add "Acquire" and "Release", and rename it to OmpMemoryOrderType, since memory order type is a concept extending beyond the ATOMIC_DEFAULT_MEM_ORDER clause. When processing a REQUIRES directive (in rewrite-directives.cpp), do not add Acquire or Release to ATOMIC constructs, because handling of those types depends on the OpenMP version, which is not available in that file. This issue will be addressed later.
1 parent 71ce9e2 commit 05b7e97

File tree

11 files changed

+84
-69
lines changed

11 files changed

+84
-69
lines changed

flang/examples/FeatureList/FeatureList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,11 @@ struct NodeVisitor {
564564
READ_FEATURE(OpenMPDeclareReductionConstruct)
565565
READ_FEATURE(OpenMPDeclareSimdConstruct)
566566
READ_FEATURE(OpenMPDeclareTargetConstruct)
567+
READ_FEATURE(OmpMemoryOrderType)
567568
READ_FEATURE(OmpMemoryOrderClause)
568569
READ_FEATURE(OmpAtomicClause)
569570
READ_FEATURE(OmpAtomicClauseList)
570571
READ_FEATURE(OmpAtomicDefaultMemOrderClause)
571-
READ_FEATURE(OmpAtomicDefaultMemOrderType)
572572
READ_FEATURE(OpenMPFlushConstruct)
573573
READ_FEATURE(OpenMPLoopConstruct)
574574
READ_FEATURE(OpenMPExecutableAllocate)

flang/include/flang/Lower/DirectivesCommon.h

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,42 @@ static inline void genOmpAtomicHintAndMemoryOrderClauses(
5555
mlir::omp::ClauseMemoryOrderKindAttr &memoryOrder) {
5656
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
5757
for (const Fortran::parser::OmpAtomicClause &clause : clauseList.v) {
58-
if (const auto *hintClause =
59-
std::get_if<Fortran::parser::OmpHintClause>(&clause.u)) {
60-
const auto *expr = Fortran::semantics::GetExpr(hintClause->v);
61-
uint64_t hintExprValue = *Fortran::evaluate::ToInt64(*expr);
62-
hint = firOpBuilder.getI64IntegerAttr(hintExprValue);
63-
} else if (const auto *ompMemoryOrderClause =
64-
std::get_if<Fortran::parser::OmpMemoryOrderClause>(
65-
&clause.u)) {
66-
if (std::get_if<Fortran::parser::OmpClause::Acquire>(
67-
&ompMemoryOrderClause->v.u)) {
68-
memoryOrder = mlir::omp::ClauseMemoryOrderKindAttr::get(
69-
firOpBuilder.getContext(),
70-
mlir::omp::ClauseMemoryOrderKind::Acquire);
71-
} else if (std::get_if<Fortran::parser::OmpClause::Relaxed>(
72-
&ompMemoryOrderClause->v.u)) {
73-
memoryOrder = mlir::omp::ClauseMemoryOrderKindAttr::get(
74-
firOpBuilder.getContext(),
75-
mlir::omp::ClauseMemoryOrderKind::Relaxed);
76-
} else if (std::get_if<Fortran::parser::OmpClause::SeqCst>(
77-
&ompMemoryOrderClause->v.u)) {
78-
memoryOrder = mlir::omp::ClauseMemoryOrderKindAttr::get(
79-
firOpBuilder.getContext(),
80-
mlir::omp::ClauseMemoryOrderKind::Seq_cst);
81-
} else if (std::get_if<Fortran::parser::OmpClause::Release>(
82-
&ompMemoryOrderClause->v.u)) {
83-
memoryOrder = mlir::omp::ClauseMemoryOrderKindAttr::get(
84-
firOpBuilder.getContext(),
85-
mlir::omp::ClauseMemoryOrderKind::Release);
86-
}
87-
}
58+
common::visit(
59+
common::visitors{
60+
[&](const parser::OmpMemoryOrderClause &s) {
61+
auto kind = common::visit(
62+
common::visitors{
63+
[&](const parser::OmpClause::AcqRel &) {
64+
return mlir::omp::ClauseMemoryOrderKind::Acq_rel;
65+
},
66+
[&](const parser::OmpClause::Acquire &) {
67+
return mlir::omp::ClauseMemoryOrderKind::Acquire;
68+
},
69+
[&](const parser::OmpClause::Relaxed &) {
70+
return mlir::omp::ClauseMemoryOrderKind::Relaxed;
71+
},
72+
[&](const parser::OmpClause::Release &) {
73+
return mlir::omp::ClauseMemoryOrderKind::Release;
74+
},
75+
[&](const parser::OmpClause::SeqCst &) {
76+
return mlir::omp::ClauseMemoryOrderKind::Seq_cst;
77+
},
78+
[&](auto &&) -> mlir::omp::ClauseMemoryOrderKind {
79+
llvm_unreachable("Unexpected clause");
80+
},
81+
},
82+
s.v.u);
83+
memoryOrder = mlir::omp::ClauseMemoryOrderKindAttr::get(
84+
firOpBuilder.getContext(), kind);
85+
},
86+
[&](const parser::OmpHintClause &s) {
87+
const auto *expr = Fortran::semantics::GetExpr(s.v);
88+
uint64_t hintExprValue = *Fortran::evaluate::ToInt64(*expr);
89+
hint = firOpBuilder.getI64IntegerAttr(hintExprValue);
90+
},
91+
[&](const parser::OmpFailClause &) {},
92+
},
93+
clause.u);
8894
}
8995
}
9096

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,11 @@ class ParseTreeDumper {
707707
NODE(parser, OpenMPDeclareSimdConstruct)
708708
NODE(parser, OpenMPDeclareTargetConstruct)
709709
NODE(parser, OpenMPDeclareMapperConstruct)
710+
NODE_ENUM(common, OmpMemoryOrderType)
710711
NODE(parser, OmpMemoryOrderClause)
711712
NODE(parser, OmpAtomicClause)
712713
NODE(parser, OmpAtomicClauseList)
713714
NODE(parser, OmpAtomicDefaultMemOrderClause)
714-
NODE_ENUM(common, OmpAtomicDefaultMemOrderType)
715715
NODE(parser, OpenMPDepobjConstruct)
716716
NODE(parser, OpenMPUtilityConstruct)
717717
NODE(parser, OpenMPDispatchConstruct)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4071,7 +4071,7 @@ struct OmpAtClause {
40714071
// SEQ_CST | ACQ_REL | RELAXED | // since 5.0
40724072
// ACQUIRE | RELEASE // since 5.2
40734073
struct OmpAtomicDefaultMemOrderClause {
4074-
using MemoryOrder = common::OmpAtomicDefaultMemOrderType;
4074+
using MemoryOrder = common::OmpMemoryOrderType;
40754075
WRAPPER_CLASS_BOILERPLATE(OmpAtomicDefaultMemOrderClause, MemoryOrder);
40764076
};
40774077

@@ -4822,10 +4822,10 @@ struct OpenMPAllocatorsConstruct {
48224822

48234823
// 2.17.7 Atomic construct/2.17.8 Flush construct [OpenMP 5.0]
48244824
// memory-order-clause -> acq_rel
4825-
// release
48264825
// acquire
4827-
// seq_cst
4826+
// release
48284827
// relaxed
4828+
// seq_cst
48294829
struct OmpMemoryOrderClause {
48304830
WRAPPER_CLASS_BOILERPLATE(OmpMemoryOrderClause, OmpClause);
48314831
CharBlock source;

flang/include/flang/Semantics/symbol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ using MutableSymbolVector = std::vector<MutableSymbolRef>;
4848

4949
// Mixin for details with OpenMP declarative constructs.
5050
class WithOmpDeclarative {
51-
using OmpAtomicOrderType = common::OmpAtomicDefaultMemOrderType;
51+
using OmpAtomicOrderType = common::OmpMemoryOrderType;
5252

5353
public:
5454
ENUM_CLASS(RequiresFlag, ReverseOffload, UnifiedAddress, UnifiedSharedMemory,

flang/include/flang/Support/Fortran.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ ENUM_CLASS(
7272
ENUM_CLASS(
7373
OpenACCDeviceType, Star, Default, Nvidia, Radeon, Host, Multicore, None)
7474

75-
// OpenMP atomic_default_mem_order clause allowed values
76-
ENUM_CLASS(OmpAtomicDefaultMemOrderType, SeqCst, AcqRel, Relaxed)
75+
// OpenMP memory-order types
76+
ENUM_CLASS(OmpMemoryOrderType, Acq_Rel, Acquire, Relaxed, Release, Seq_Cst)
7777

7878
// Fortran names may have up to 63 characters (See Fortran 2018 C601).
7979
static constexpr int maxNameLen{63};

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,13 @@ AtomicDefaultMemOrder make(const parser::OmpClause::AtomicDefaultMemOrder &inp,
494494
semantics::SemanticsContext &semaCtx) {
495495
// inp.v -> parser::OmpAtomicDefaultMemOrderClause
496496
CLAUSET_ENUM_CONVERT( //
497-
convert, common::OmpAtomicDefaultMemOrderType,
498-
AtomicDefaultMemOrder::MemoryOrder,
497+
convert, common::OmpMemoryOrderType, AtomicDefaultMemOrder::MemoryOrder,
499498
// clang-format off
500-
MS(AcqRel, AcqRel)
499+
MS(Acq_Rel, AcqRel)
500+
MS(Acquire, Acquire)
501501
MS(Relaxed, Relaxed)
502-
MS(SeqCst, SeqCst)
502+
MS(Release, Release)
503+
MS(Seq_Cst, SeqCst)
503504
// clang-format on
504505
);
505506

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,20 @@ TYPE_PARSER(construct<OmpAffinityClause>(
636636
maybe(nonemptyList(Parser<OmpAffinityClause::Modifier>{}) / ":"),
637637
Parser<OmpObjectList>{}))
638638

639+
// 2.4 Requires construct [OpenMP 5.0]
640+
// atomic-default-mem-order-clause ->
641+
// acq_rel
642+
// acquire
643+
// relaxed
644+
// release
645+
// seq_cst
646+
TYPE_PARSER(construct<OmpAtomicDefaultMemOrderClause>(
647+
"ACQ_REL" >> pure(common::OmpMemoryOrderType::Acq_Rel) ||
648+
"ACQUIRE" >> pure(common::OmpMemoryOrderType::Acquire) ||
649+
"RELAXED" >> pure(common::OmpMemoryOrderType::Relaxed) ||
650+
"RELEASE" >> pure(common::OmpMemoryOrderType::Release) ||
651+
"SEQ_CST" >> pure(common::OmpMemoryOrderType::Seq_Cst)))
652+
639653
TYPE_PARSER(construct<OmpCancellationConstructTypeClause>(
640654
OmpDirectiveNameParser{}, maybe(parenthesized(scalarLogicalExpr))))
641655

@@ -1192,27 +1206,17 @@ TYPE_PARSER(sourced(construct<OmpFailClause>(
11921206

11931207
// 2.17.7 Atomic construct/2.17.8 Flush construct [OpenMP 5.0]
11941208
// memory-order-clause ->
1195-
// seq_cst
11961209
// acq_rel
1197-
// release
11981210
// acquire
11991211
// relaxed
1212+
// release
1213+
// seq_cst
12001214
TYPE_PARSER(sourced(construct<OmpMemoryOrderClause>(
1201-
sourced("SEQ_CST" >> construct<OmpClause>(construct<OmpClause::SeqCst>()) ||
1202-
"ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
1203-
"RELEASE" >> construct<OmpClause>(construct<OmpClause::Release>()) ||
1215+
sourced("ACQ_REL" >> construct<OmpClause>(construct<OmpClause::AcqRel>()) ||
12041216
"ACQUIRE" >> construct<OmpClause>(construct<OmpClause::Acquire>()) ||
1205-
"RELAXED" >> construct<OmpClause>(construct<OmpClause::Relaxed>())))))
1206-
1207-
// 2.4 Requires construct [OpenMP 5.0]
1208-
// atomic-default-mem-order-clause ->
1209-
// seq_cst
1210-
// acq_rel
1211-
// relaxed
1212-
TYPE_PARSER(construct<OmpAtomicDefaultMemOrderClause>(
1213-
"SEQ_CST" >> pure(common::OmpAtomicDefaultMemOrderType::SeqCst) ||
1214-
"ACQ_REL" >> pure(common::OmpAtomicDefaultMemOrderType::AcqRel) ||
1215-
"RELAXED" >> pure(common::OmpAtomicDefaultMemOrderType::Relaxed)))
1217+
"RELAXED" >> construct<OmpClause>(construct<OmpClause::Relaxed>()) ||
1218+
"RELEASE" >> construct<OmpClause>(construct<OmpClause::Release>()) ||
1219+
"SEQ_CST" >> construct<OmpClause>(construct<OmpClause::SeqCst>())))))
12161220

12171221
// 2.17.7 Atomic construct
12181222
// atomic-clause -> memory-order-clause | HINT(hint-expression)

flang/lib/Parser/unparse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,8 +2558,8 @@ class UnparseVisitor {
25582558
}
25592559
}
25602560

2561-
void Unparse(const OmpAtomicDefaultMemOrderClause &x) {
2562-
Word(ToUpperCaseLetters(common::EnumToString(x.v)));
2561+
void Unparse(const common::OmpMemoryOrderType &x) {
2562+
Word(ToUpperCaseLetters(common::EnumToString(x)));
25632563
}
25642564

25652565
void Unparse(const OmpAtomicClauseList &x) { Walk(" ", x.v, " "); }

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
416416

417417
// Gather information from the clauses.
418418
Flags flags;
419-
std::optional<common::OmpAtomicDefaultMemOrderType> memOrder;
419+
std::optional<common::OmpMemoryOrderType> memOrder;
420420
for (const auto &clause : std::get<parser::OmpClauseList>(x.t).v) {
421421
flags |= common::visit(
422422
common::visitors{
@@ -799,7 +799,7 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
799799
std::int64_t ordCollapseLevel{0};
800800

801801
void AddOmpRequiresToScope(Scope &, WithOmpDeclarative::RequiresFlags,
802-
std::optional<common::OmpAtomicDefaultMemOrderType>);
802+
std::optional<common::OmpMemoryOrderType>);
803803
void IssueNonConformanceWarning(
804804
llvm::omp::Directive D, parser::CharBlock source);
805805

@@ -2721,7 +2721,7 @@ void ResolveOmpTopLevelParts(
27212721
// program units. Modules are skipped because their REQUIRES clauses should be
27222722
// propagated via USE statements instead.
27232723
WithOmpDeclarative::RequiresFlags combinedFlags;
2724-
std::optional<common::OmpAtomicDefaultMemOrderType> combinedMemOrder;
2724+
std::optional<common::OmpMemoryOrderType> combinedMemOrder;
27252725

27262726
// Function to go through non-module top level program units and extract
27272727
// REQUIRES information to be processed by a function-like argument.
@@ -2764,7 +2764,7 @@ void ResolveOmpTopLevelParts(
27642764
flags{details.ompRequires()}) {
27652765
combinedFlags |= *flags;
27662766
}
2767-
if (const common::OmpAtomicDefaultMemOrderType *
2767+
if (const common::OmpMemoryOrderType *
27682768
memOrder{details.ompAtomicDefaultMemOrder()}) {
27692769
if (combinedMemOrder && *combinedMemOrder != *memOrder) {
27702770
context.Say(symbol.scope()->sourceRange(),
@@ -2983,7 +2983,7 @@ void OmpAttributeVisitor::CheckNameInAllocateStmt(
29832983

29842984
void OmpAttributeVisitor::AddOmpRequiresToScope(Scope &scope,
29852985
WithOmpDeclarative::RequiresFlags flags,
2986-
std::optional<common::OmpAtomicDefaultMemOrderType> memOrder) {
2986+
std::optional<common::OmpMemoryOrderType> memOrder) {
29872987
Scope *scopeIter = &scope;
29882988
do {
29892989
if (Symbol * symbol{scopeIter->symbol()}) {

flang/lib/Semantics/rewrite-directives.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bool OmpRewriteMutator::Pre(parser::OpenMPAtomicConstruct &x) {
7070
x.u)};
7171

7272
// Get the `atomic_default_mem_order` clause from the top-level parent.
73-
std::optional<common::OmpAtomicDefaultMemOrderType> defaultMemOrder;
73+
std::optional<common::OmpMemoryOrderType> defaultMemOrder;
7474
common::visit(
7575
[&](auto &details) {
7676
if constexpr (std::is_convertible_v<decltype(&details),
@@ -119,7 +119,7 @@ bool OmpRewriteMutator::Pre(parser::OpenMPAtomicConstruct &x) {
119119
if (clauseList) {
120120
atomicDirectiveDefaultOrderFound_ = true;
121121
switch (*defaultMemOrder) {
122-
case common::OmpAtomicDefaultMemOrderType::AcqRel:
122+
case common::OmpMemoryOrderType::Acq_Rel:
123123
clauseList->emplace_back<parser::OmpMemoryOrderClause>(common::visit(
124124
common::visitors{[](parser::OmpAtomicRead &) -> parser::OmpClause {
125125
return parser::OmpClause::Acquire{};
@@ -133,14 +133,18 @@ bool OmpRewriteMutator::Pre(parser::OpenMPAtomicConstruct &x) {
133133
}},
134134
x.u));
135135
break;
136-
case common::OmpAtomicDefaultMemOrderType::Relaxed:
136+
case common::OmpMemoryOrderType::Relaxed:
137137
clauseList->emplace_back<parser::OmpMemoryOrderClause>(
138138
parser::OmpClause{parser::OmpClause::Relaxed{}});
139139
break;
140-
case common::OmpAtomicDefaultMemOrderType::SeqCst:
140+
case common::OmpMemoryOrderType::Seq_Cst:
141141
clauseList->emplace_back<parser::OmpMemoryOrderClause>(
142142
parser::OmpClause{parser::OmpClause::SeqCst{}});
143143
break;
144+
default:
145+
// FIXME: Don't process other values at the moment since their validity
146+
// depends on the OpenMP version (which is unavailable here).
147+
break;
144148
}
145149
}
146150

0 commit comments

Comments
 (0)