Skip to content

Commit a74e9ce

Browse files
[OpenMP] atomic compare weak : Parser & AST support (llvm#79475)
This is a support for " #pragma omp atomic compare weak". It has Parser & AST support for now. --------- Authored-by: Sunil Kuravinakop <[email protected]>
1 parent 5cc87b4 commit a74e9ce

File tree

17 files changed

+128
-3
lines changed

17 files changed

+128
-3
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,6 +2513,46 @@ class OMPRelaxedClause final : public OMPClause {
25132513
}
25142514
};
25152515

2516+
/// This represents 'weak' clause in the '#pragma omp atomic'
2517+
/// directives.
2518+
///
2519+
/// \code
2520+
/// #pragma omp atomic compare weak
2521+
/// \endcode
2522+
/// In this example directive '#pragma omp atomic' has 'weak' clause.
2523+
class OMPWeakClause final : public OMPClause {
2524+
public:
2525+
/// Build 'weak' clause.
2526+
///
2527+
/// \param StartLoc Starting location of the clause.
2528+
/// \param EndLoc Ending location of the clause.
2529+
OMPWeakClause(SourceLocation StartLoc, SourceLocation EndLoc)
2530+
: OMPClause(llvm::omp::OMPC_weak, StartLoc, EndLoc) {}
2531+
2532+
/// Build an empty clause.
2533+
OMPWeakClause()
2534+
: OMPClause(llvm::omp::OMPC_weak, SourceLocation(), SourceLocation()) {}
2535+
2536+
child_range children() {
2537+
return child_range(child_iterator(), child_iterator());
2538+
}
2539+
2540+
const_child_range children() const {
2541+
return const_child_range(const_child_iterator(), const_child_iterator());
2542+
}
2543+
2544+
child_range used_children() {
2545+
return child_range(child_iterator(), child_iterator());
2546+
}
2547+
const_child_range used_children() const {
2548+
return const_child_range(const_child_iterator(), const_child_iterator());
2549+
}
2550+
2551+
static bool classof(const OMPClause *T) {
2552+
return T->getClauseKind() == llvm::omp::OMPC_weak;
2553+
}
2554+
};
2555+
25162556
/// This represents 'fail' clause in the '#pragma omp atomic'
25172557
/// directive.
25182558
///

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3436,6 +3436,11 @@ bool RecursiveASTVisitor<Derived>::VisitOMPRelaxedClause(OMPRelaxedClause *) {
34363436
return true;
34373437
}
34383438

3439+
template <typename Derived>
3440+
bool RecursiveASTVisitor<Derived>::VisitOMPWeakClause(OMPWeakClause *) {
3441+
return true;
3442+
}
3443+
34393444
template <typename Derived>
34403445
bool RecursiveASTVisitor<Derived>::VisitOMPThreadsClause(OMPThreadsClause *) {
34413446
return true;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11042,7 +11042,8 @@ def note_omp_atomic_compare: Note<
1104211042
"expect lvalue for result value|expect scalar value|expect integer value|unexpected 'else' statement|expect '==' operator|expect an assignment statement 'v = x'|"
1104311043
"expect a 'if' statement|expect no more than two statements|expect a compound statement|expect 'else' statement|expect a form 'r = x == e; if (r) ...'}0">;
1104411044
def err_omp_atomic_fail_wrong_or_no_clauses : Error<"expected a memory order clause">;
11045-
def err_omp_atomic_fail_no_compare : Error<"expected 'compare' clause with the 'fail' modifier">;
11045+
def err_omp_atomic_no_compare : Error<"expected 'compare' clause with the '%0' modifier">;
11046+
def err_omp_atomic_weak_no_equality : Error<"expected '==' operator for 'weak' clause">;
1104611047
def err_omp_atomic_several_clauses : Error<
1104711048
"directive '#pragma omp atomic' cannot contain more than one 'read', 'write', 'update', 'capture', or 'compare' clause">;
1104811049
def err_omp_several_mem_order_clauses : Error<

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12377,6 +12377,9 @@ class Sema final {
1237712377
/// Called on well-formed 'relaxed' clause.
1237812378
OMPClause *ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
1237912379
SourceLocation EndLoc);
12380+
/// Called on well-formed 'weak' clause.
12381+
OMPClause *ActOnOpenMPWeakClause(SourceLocation StartLoc,
12382+
SourceLocation EndLoc);
1238012383

1238112384
/// Called on well-formed 'init' clause.
1238212385
OMPClause *

clang/lib/AST/OpenMPClause.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,8 @@ void OMPClausePrinter::VisitOMPRelaxedClause(OMPRelaxedClause *) {
19571957
OS << "relaxed";
19581958
}
19591959

1960+
void OMPClausePrinter::VisitOMPWeakClause(OMPWeakClause *) { OS << "weak"; }
1961+
19601962
void OMPClausePrinter::VisitOMPThreadsClause(OMPThreadsClause *) {
19611963
OS << "threads";
19621964
}

clang/lib/AST/StmtProfile.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ void OMPClauseProfiler::VisitOMPReleaseClause(const OMPReleaseClause *) {}
594594

595595
void OMPClauseProfiler::VisitOMPRelaxedClause(const OMPRelaxedClause *) {}
596596

597+
void OMPClauseProfiler::VisitOMPWeakClause(const OMPWeakClause *) {}
598+
597599
void OMPClauseProfiler::VisitOMPThreadsClause(const OMPThreadsClause *) {}
598600

599601
void OMPClauseProfiler::VisitOMPSIMDClause(const OMPSIMDClause *) {}

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6546,6 +6546,9 @@ void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
65466546
// Find first clause (skip seq_cst|acq_rel|aqcuire|release|relaxed clause,
65476547
// if it is first).
65486548
OpenMPClauseKind K = C->getClauseKind();
6549+
// TBD
6550+
if (K == OMPC_weak)
6551+
return;
65496552
if (K == OMPC_seq_cst || K == OMPC_acq_rel || K == OMPC_acquire ||
65506553
K == OMPC_release || K == OMPC_relaxed || K == OMPC_hint)
65516554
continue;

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3314,6 +3314,7 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
33143314
case OMPC_acquire:
33153315
case OMPC_release:
33163316
case OMPC_relaxed:
3317+
case OMPC_weak:
33173318
case OMPC_threads:
33183319
case OMPC_simd:
33193320
case OMPC_nogroup:

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12708,9 +12708,11 @@ StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
1270812708
}
1270912709
break;
1271012710
}
12711+
case OMPC_weak:
1271112712
case OMPC_fail: {
1271212713
if (!EncounteredAtomicKinds.contains(OMPC_compare)) {
12713-
Diag(C->getBeginLoc(), diag::err_omp_atomic_fail_no_compare)
12714+
Diag(C->getBeginLoc(), diag::err_omp_atomic_no_compare)
12715+
<< getOpenMPClauseName(C->getClauseKind())
1271412716
<< SourceRange(C->getBeginLoc(), C->getEndLoc());
1271512717
return StmtError();
1271612718
}
@@ -13202,6 +13204,27 @@ StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
1320213204
E = Checker.getE();
1320313205
D = Checker.getD();
1320413206
CE = Checker.getCond();
13207+
// The weak clause may only appear if the resulting atomic operation is
13208+
// an atomic conditional update for which the comparison tests for
13209+
// equality. It was not possible to do this check in
13210+
// OpenMPAtomicCompareChecker::checkStmt() as the check for OMPC_weak
13211+
// could not be performed (Clauses are not available).
13212+
auto *It = find_if(Clauses, [](OMPClause *C) {
13213+
return C->getClauseKind() == llvm::omp::Clause::OMPC_weak;
13214+
});
13215+
if (It != Clauses.end()) {
13216+
auto *Cond = dyn_cast<BinaryOperator>(CE);
13217+
if (Cond->getOpcode() != BO_EQ) {
13218+
ErrorInfo.Error = Checker.ErrorTy::NotAnAssignment;
13219+
ErrorInfo.ErrorLoc = Cond->getExprLoc();
13220+
ErrorInfo.NoteLoc = Cond->getOperatorLoc();
13221+
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
13222+
13223+
Diag(ErrorInfo.ErrorLoc, diag::err_omp_atomic_weak_no_equality)
13224+
<< ErrorInfo.ErrorRange;
13225+
return StmtError();
13226+
}
13227+
}
1320513228
// We reuse IsXLHSInRHSPart to tell if it is in the form 'x ordop expr'.
1320613229
IsXLHSInRHSPart = Checker.isXBinopExpr();
1320713230
}
@@ -17593,6 +17616,9 @@ OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
1759317616
case OMPC_relaxed:
1759417617
Res = ActOnOpenMPRelaxedClause(StartLoc, EndLoc);
1759517618
break;
17619+
case OMPC_weak:
17620+
Res = ActOnOpenMPWeakClause(StartLoc, EndLoc);
17621+
break;
1759617622
case OMPC_threads:
1759717623
Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
1759817624
break;
@@ -17781,6 +17807,11 @@ OMPClause *Sema::ActOnOpenMPRelaxedClause(SourceLocation StartLoc,
1778117807
return new (Context) OMPRelaxedClause(StartLoc, EndLoc);
1778217808
}
1778317809

17810+
OMPClause *Sema::ActOnOpenMPWeakClause(SourceLocation StartLoc,
17811+
SourceLocation EndLoc) {
17812+
return new (Context) OMPWeakClause(StartLoc, EndLoc);
17813+
}
17814+
1778417815
OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
1778517816
SourceLocation EndLoc) {
1778617817
return new (Context) OMPThreadsClause(StartLoc, EndLoc);

clang/lib/Sema/TreeTransform.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10075,6 +10075,12 @@ TreeTransform<Derived>::TransformOMPRelaxedClause(OMPRelaxedClause *C) {
1007510075
return C;
1007610076
}
1007710077

10078+
template <typename Derived>
10079+
OMPClause *TreeTransform<Derived>::TransformOMPWeakClause(OMPWeakClause *C) {
10080+
// No need to rebuild this clause, no template-dependent parameters.
10081+
return C;
10082+
}
10083+
1007810084
template <typename Derived>
1007910085
OMPClause *
1008010086
TreeTransform<Derived>::TransformOMPThreadsClause(OMPThreadsClause *C) {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10349,6 +10349,9 @@ OMPClause *OMPClauseReader::readClause() {
1034910349
case llvm::omp::OMPC_relaxed:
1035010350
C = new (Context) OMPRelaxedClause();
1035110351
break;
10352+
case llvm::omp::OMPC_weak:
10353+
C = new (Context) OMPWeakClause();
10354+
break;
1035210355
case llvm::omp::OMPC_threads:
1035310356
C = new (Context) OMPThreadsClause();
1035410357
break;
@@ -10747,6 +10750,8 @@ void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
1074710750

1074810751
void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
1074910752

10753+
void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}
10754+
1075010755
void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
1075110756

1075210757
void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6698,6 +6698,8 @@ void OMPClauseWriter::VisitOMPReleaseClause(OMPReleaseClause *) {}
66986698

66996699
void OMPClauseWriter::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
67006700

6701+
void OMPClauseWriter::VisitOMPWeakClause(OMPWeakClause *) {}
6702+
67016703
void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
67026704

67036705
void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}

clang/test/OpenMP/atomic_ast_print.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ T foo(T argc) {
232232
{ if (a < c) { a = c; } }
233233
#pragma omp atomic compare fail(seq_cst)
234234
{ if (a < c) { a = c; } }
235+
#pragma omp atomic compare seq_cst weak
236+
{ if(a == b) { a = c; } }
235237
#endif
236238
return T();
237239
}
@@ -1111,6 +1113,8 @@ int main(int argc, char **argv) {
11111113
if(a < b) { a = b; }
11121114
#pragma omp atomic compare fail(seq_cst)
11131115
if(a < b) { a = b; }
1116+
#pragma omp atomic compare seq_cst weak
1117+
if(a == b) { a = c; }
11141118
#endif
11151119
// CHECK-NEXT: #pragma omp atomic
11161120
// CHECK-NEXT: a++;
@@ -1453,6 +1457,10 @@ int main(int argc, char **argv) {
14531457
// CHECK-51-NEXT: if (a < b) {
14541458
// CHECK-51-NEXT: a = b;
14551459
// CHECK-51-NEXT: }
1460+
// CHECK-51-NEXT: #pragma omp atomic compare seq_cst weak
1461+
// CHECK-51-NEXT: if (a == b) {
1462+
// CHECK-51-NEXT: a = c;
1463+
// CHECK-51-NEXT: }
14561464
// expect-note@+1 {{in instantiation of function template specialization 'foo<int>' requested here}}
14571465
return foo(a);
14581466
}

clang/test/OpenMP/atomic_messages.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,17 @@ int mixed() {
974974
// expected-error@+1 {{directive '#pragma omp atomic' cannot contain more than one 'fail' clause}}
975975
#pragma omp atomic compare fail(relaxed) fail(seq_cst)
976976
if(v < a) { v = a; }
977+
#pragma omp atomic compare seq_cst weak
978+
if(v == a) { v = a; }
979+
// expected-error@+1 {{expected 'compare' clause with the 'weak' modifier}}
980+
#pragma omp atomic weak
981+
if(v < a) { v = a; }
982+
#pragma omp atomic compare release weak
983+
// expected-error@+1 {{expected '==' operator for 'weak' clause}}
984+
if(v < a) { v = a; }
985+
// expected-error@+1 {{directive '#pragma omp atomic' cannot contain more than one 'weak' clause}}
986+
#pragma omp atomic compare release weak fail(seq_cst) weak
987+
if(v == a) { v = a; }
977988

978989

979990
#endif

clang/tools/libclang/CIndex.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,8 @@ void OMPClauseEnqueue::VisitOMPReleaseClause(const OMPReleaseClause *) {}
24252425

24262426
void OMPClauseEnqueue::VisitOMPRelaxedClause(const OMPRelaxedClause *) {}
24272427

2428+
void OMPClauseEnqueue::VisitOMPWeakClause(const OMPWeakClause *) {}
2429+
24282430
void OMPClauseEnqueue::VisitOMPThreadsClause(const OMPThreadsClause *) {}
24292431

24302432
void OMPClauseEnqueue::VisitOMPSIMDClause(const OMPSIMDClause *) {}

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,7 @@ CHECK_SIMPLE_CLAUSE(OmpxAttribute, OMPC_ompx_attribute)
22642264
CHECK_SIMPLE_CLAUSE(OmpxBare, OMPC_ompx_bare)
22652265
CHECK_SIMPLE_CLAUSE(Enter, OMPC_enter)
22662266
CHECK_SIMPLE_CLAUSE(Fail, OMPC_fail)
2267+
CHECK_SIMPLE_CLAUSE(Weak, OMPC_weak)
22672268

22682269
CHECK_REQ_SCALAR_INT_CLAUSE(Grainsize, OMPC_grainsize)
22692270
CHECK_REQ_SCALAR_INT_CLAUSE(NumTasks, OMPC_num_tasks)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def OMPC_AcqRel : Clause<"acq_rel"> { let clangClass = "OMPAcqRelClause"; }
215215
def OMPC_Acquire : Clause<"acquire"> { let clangClass = "OMPAcquireClause"; }
216216
def OMPC_Release : Clause<"release"> { let clangClass = "OMPReleaseClause"; }
217217
def OMPC_Relaxed : Clause<"relaxed"> { let clangClass = "OMPRelaxedClause"; }
218+
def OMPC_Weak : Clause<"weak"> { let clangClass = "OMPWeakClause"; }
218219
def OMPC_Depend : Clause<"depend"> {
219220
let clangClass = "OMPDependClause";
220221
let flangClass = "OmpDependClause";
@@ -642,7 +643,8 @@ def OMP_Atomic : Directive<"atomic"> {
642643
VersionedClause<OMPC_Release, 50>,
643644
VersionedClause<OMPC_Relaxed, 50>,
644645
VersionedClause<OMPC_Hint, 50>,
645-
VersionedClause<OMPC_Fail, 51>
646+
VersionedClause<OMPC_Fail, 51>,
647+
VersionedClause<OMPC_Weak, 51>
646648
];
647649
}
648650
def OMP_Target : Directive<"target"> {

0 commit comments

Comments
 (0)