Skip to content

[OpenMP] Fix atomic compare handling with overloaded operators #141142

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

Merged
merged 1 commit into from
May 29, 2025
Merged
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
162 changes: 97 additions & 65 deletions clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11763,51 +11763,61 @@ bool OpenMPAtomicCompareChecker::checkCondUpdateStmt(IfStmt *S,
X = BO->getLHS();

auto *Cond = dyn_cast<BinaryOperator>(S->getCond());
if (!Cond) {
auto *Call = dyn_cast<CXXOperatorCallExpr>(S->getCond());
Expr *LHS = nullptr;
Expr *RHS = nullptr;
if (Cond) {
LHS = Cond->getLHS();
RHS = Cond->getRHS();
} else if (Call) {
LHS = Call->getArg(0);
RHS = Call->getArg(1);
} else {
ErrorInfo.Error = ErrorTy::NotABinaryOp;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
return false;
}

switch (Cond->getOpcode()) {
case BO_EQ: {
C = Cond;
if ((Cond && Cond->getOpcode() == BO_EQ) ||
(Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
C = S->getCond();
D = BO->getRHS();
if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getLHS())) {
E = Cond->getRHS();
} else if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getRHS())) {
E = Cond->getLHS();
if (checkIfTwoExprsAreSame(ContextRef, X, LHS)) {
E = RHS;
} else if (checkIfTwoExprsAreSame(ContextRef, X, RHS)) {
E = LHS;
} else {
ErrorInfo.Error = ErrorTy::InvalidComparison;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
S->getCond()->getSourceRange();
return false;
}
break;
}
case BO_LT:
case BO_GT: {
} else if ((Cond &&
(Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
(Call &&
(Call->getOperator() == OverloadedOperatorKind::OO_Less ||
Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {
E = BO->getRHS();
if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getLHS()) &&
checkIfTwoExprsAreSame(ContextRef, E, Cond->getRHS())) {
C = Cond;
} else if (checkIfTwoExprsAreSame(ContextRef, E, Cond->getLHS()) &&
checkIfTwoExprsAreSame(ContextRef, X, Cond->getRHS())) {
C = Cond;
if (checkIfTwoExprsAreSame(ContextRef, X, LHS) &&
checkIfTwoExprsAreSame(ContextRef, E, RHS)) {
C = S->getCond();
} else if (checkIfTwoExprsAreSame(ContextRef, E, LHS) &&
checkIfTwoExprsAreSame(ContextRef, X, RHS)) {
C = S->getCond();
IsXBinopExpr = false;
} else {
ErrorInfo.Error = ErrorTy::InvalidComparison;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
S->getCond()->getSourceRange();
return false;
}
break;
}
default:
} else {
ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
return false;
}

Expand Down Expand Up @@ -11857,52 +11867,64 @@ bool OpenMPAtomicCompareChecker::checkCondExprStmt(Stmt *S,
}

auto *Cond = dyn_cast<BinaryOperator>(CO->getCond());
if (!Cond) {
auto *Call = dyn_cast<CXXOperatorCallExpr>(CO->getCond());
Expr *LHS = nullptr;
Expr *RHS = nullptr;
if (Cond) {
LHS = Cond->getLHS();
RHS = Cond->getRHS();
} else if (Call) {
LHS = Call->getArg(0);
RHS = Call->getArg(1);
} else {
ErrorInfo.Error = ErrorTy::NotABinaryOp;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
CO->getCond()->getSourceRange();
return false;
}

switch (Cond->getOpcode()) {
case BO_EQ: {
C = Cond;
if ((Cond && Cond->getOpcode() == BO_EQ) ||
(Call && Call->getOperator() == OverloadedOperatorKind::OO_EqualEqual)) {
C = CO->getCond();
D = CO->getTrueExpr();
if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getLHS())) {
E = Cond->getRHS();
} else if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getRHS())) {
E = Cond->getLHS();
if (checkIfTwoExprsAreSame(ContextRef, X, LHS)) {
E = RHS;
} else if (checkIfTwoExprsAreSame(ContextRef, X, RHS)) {
E = LHS;
} else {
ErrorInfo.Error = ErrorTy::InvalidComparison;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
CO->getCond()->getSourceRange();
return false;
}
break;
}
case BO_LT:
case BO_GT: {
} else if ((Cond &&
(Cond->getOpcode() == BO_LT || Cond->getOpcode() == BO_GT)) ||
(Call &&
(Call->getOperator() == OverloadedOperatorKind::OO_Less ||
Call->getOperator() == OverloadedOperatorKind::OO_Greater))) {

E = CO->getTrueExpr();
if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getLHS()) &&
checkIfTwoExprsAreSame(ContextRef, E, Cond->getRHS())) {
C = Cond;
} else if (checkIfTwoExprsAreSame(ContextRef, E, Cond->getLHS()) &&
checkIfTwoExprsAreSame(ContextRef, X, Cond->getRHS())) {
C = Cond;
if (checkIfTwoExprsAreSame(ContextRef, X, LHS) &&
checkIfTwoExprsAreSame(ContextRef, E, RHS)) {
C = CO->getCond();
} else if (checkIfTwoExprsAreSame(ContextRef, E, LHS) &&
checkIfTwoExprsAreSame(ContextRef, X, RHS)) {
C = CO->getCond();
IsXBinopExpr = false;
} else {
ErrorInfo.Error = ErrorTy::InvalidComparison;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
CO->getCond()->getSourceRange();
return false;
}
break;
}
default:
} else {
ErrorInfo.Error = ErrorTy::InvalidBinaryOp;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = CO->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange =
CO->getCond()->getSourceRange();
return false;
}

Expand Down Expand Up @@ -12063,31 +12085,41 @@ bool OpenMPAtomicCompareCaptureChecker::checkForm3(IfStmt *S,
D = BO->getRHS();

auto *Cond = dyn_cast<BinaryOperator>(S->getCond());
if (!Cond) {
auto *Call = dyn_cast<CXXOperatorCallExpr>(S->getCond());
Expr *LHS = nullptr;
Expr *RHS = nullptr;
if (Cond) {
LHS = Cond->getLHS();
RHS = Cond->getRHS();
} else if (Call) {
LHS = Call->getArg(0);
RHS = Call->getArg(1);
} else {
ErrorInfo.Error = ErrorTy::NotABinaryOp;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
return false;
}
if (Cond->getOpcode() != BO_EQ) {
if ((Cond && Cond->getOpcode() != BO_EQ) ||
(Call && Call->getOperator() != OverloadedOperatorKind::OO_EqualEqual)) {
ErrorInfo.Error = ErrorTy::NotEQ;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
return false;
}

if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getLHS())) {
E = Cond->getRHS();
} else if (checkIfTwoExprsAreSame(ContextRef, X, Cond->getRHS())) {
E = Cond->getLHS();
if (checkIfTwoExprsAreSame(ContextRef, X, LHS)) {
E = RHS;
} else if (checkIfTwoExprsAreSame(ContextRef, X, RHS)) {
E = LHS;
} else {
ErrorInfo.Error = ErrorTy::InvalidComparison;
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = Cond->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = Cond->getSourceRange();
ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getCond()->getExprLoc();
ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getCond()->getSourceRange();
return false;
}

C = Cond;
C = S->getCond();

if (!S->getElse()) {
ErrorInfo.Error = ErrorTy::NoElse;
Expand Down
31 changes: 31 additions & 0 deletions clang/test/OpenMP/atomic_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,3 +991,34 @@ int mixed() {
// expected-note@+1 {{in instantiation of function template specialization 'mixed<int>' requested here}}
return mixed<int>();
}

#ifdef OMP51
struct U {};
struct U operator<(U, U);
struct U operator>(U, U);
struct U operator==(U, U);

template <typename T> void templated() {
T cx, cv, ce, cd;
#pragma omp atomic compare capture
if (cx == ce) {
cx = cd;
} else {
cv = cx;
}
#pragma omp atomic compare capture
{
cv = cx;
if (ce > cx) {
cx = ce;
}
}
#pragma omp atomic compare capture
{
cv = cx;
if (cx < ce) {
cx = ce;
}
}
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a similar test as a codegen test as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codegen wasn't the issue here, if you manually instantiated it the code would work fine, the issue was just Sema incorrectly rejecting this when there was no type given.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, fair, I guess.

Loading