Author: Erich Keane
Date: 2026-07-02T15:16:40Z
New Revision: 8f18d86a33e4a2fa56d081d30ff35504dddf0119

URL: 
https://github.com/llvm/llvm-project/commit/8f18d86a33e4a2fa56d081d30ff35504dddf0119
DIFF: 
https://github.com/llvm/llvm-project/commit/8f18d86a33e4a2fa56d081d30ff35504dddf0119.diff

LOG: [OpenACC] Fix reduction var equality check for member variables (#207196)

OpenACC requires we check that a variable have the same operator in all
nested constructs with a reduction. The implementation for this checks
everything I could think of, but I apparently missed member expressions.
This patch adds checks for that as well as 'this' so we should correctly
get the checks.

Fixes: #207180

Added: 
    

Modified: 
    clang/lib/Sema/SemaOpenACCClause.cpp
    clang/test/SemaOpenACC/loop-construct-reduction-clause.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaOpenACCClause.cpp 
b/clang/lib/Sema/SemaOpenACCClause.cpp
index ea5a32c069e27..0409e2456895d 100644
--- a/clang/lib/Sema/SemaOpenACCClause.cpp
+++ b/clang/lib/Sema/SemaOpenACCClause.cpp
@@ -1846,6 +1846,20 @@ bool areVarsEqual(Expr *VarExpr1, Expr *VarExpr2) {
            Expr2DRE->getDecl()->getMostRecentDecl();
   }
 
+  // References to a member.
+  if (auto *Expr1ME = dyn_cast<MemberExpr>(VarExpr1)) {
+    auto *Expr2ME = dyn_cast<MemberExpr>(VarExpr2);
+    if (!Expr2ME)
+      return false;
+
+    return Expr1ME->getMemberDecl()->getMostRecentDecl() ==
+               Expr2ME->getMemberDecl()->getMostRecentDecl() &&
+           areVarsEqual(Expr1ME->getBase(), Expr2ME->getBase());
+  }
+
+  if (isa<CXXThisExpr>(VarExpr1))
+    return isa<CXXThisExpr>(VarExpr2);
+
   llvm_unreachable("Unknown variable type encountered");
 }
 } // namespace

diff  --git a/clang/test/SemaOpenACC/loop-construct-reduction-clause.cpp 
b/clang/test/SemaOpenACC/loop-construct-reduction-clause.cpp
index f2dd928331173..c4e5f3b5aeefa 100644
--- a/clang/test/SemaOpenACC/loop-construct-reduction-clause.cpp
+++ b/clang/test/SemaOpenACC/loop-construct-reduction-clause.cpp
@@ -377,4 +377,47 @@ void inst() {
   templ_uses<int, CompositeOfScalars, CompositeHasComposite, 1, 2>();
 }
 
+namespace gh207180 {
+struct InlineDefFunc {
+  int I;
+  void func() {
+#pragma acc loop reduction(+:I)
+    // expected-error@+3{{OpenACC 'reduction' variable must have the same 
operator in all nested constructs (& vs +)}}
+    for(int i = 0; i < 5; ++i)
+    // expected-note@-3{{previous 'reduction' clause is here}}
+#pragma acc loop reduction(&:I)
+    for(int j = 0; j < 5; ++j);
+  }
+};
+struct OutlineDefFunc {
+  int I;
+  void func();
+};
+void OutlineDefFunc::func() {
+#pragma acc loop reduction(+:I)
+  // expected-error@+3{{OpenACC 'reduction' variable must have the same 
operator in all nested constructs (& vs +)}}
+    for(int i = 0; i < 5; ++i)
+  // expected-note@-3{{previous 'reduction' clause is here}}
+#pragma acc loop reduction(&:I)
+  for(int j = 0; j < 5; ++j);
+}
+
+template<typename T>
+struct TemplDefFunc {
+  T I;
+  void func() {
+#pragma acc loop reduction(+:I)
+    // expected-error@+3{{OpenACC 'reduction' variable must have the same 
operator in all nested constructs (& vs +)}}
+    for(int i = 0; i < 5; ++i)
+    // expected-note@-3{{previous 'reduction' clause is here}}
+#pragma acc loop reduction(&:I)
+    for(int j = 0; j < 5; ++j);
+  }
+};
 
+void use() {
+  TemplDefFunc<int> d;
+  d.func(); // expected-note{{in instantiation of}}
+}
+
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to