[clang] [OpenACC] Fix reduction var equality check for member variables (PR #207196)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 2 07:37:39 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Erich Keane (erichkeane)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/207196.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaOpenACCClause.cpp (+14) 
- (modified) clang/test/SemaOpenACC/loop-construct-reduction-clause.cpp (+43) 


``````````diff
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 at +3{{OpenACC 'reduction' variable must have the same operator in all nested constructs (& vs +)}}
+    for(int i = 0; i < 5; ++i)
+    // expected-note at -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 at +3{{OpenACC 'reduction' variable must have the same operator in all nested constructs (& vs +)}}
+    for(int i = 0; i < 5; ++i)
+  // expected-note at -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 at +3{{OpenACC 'reduction' variable must have the same operator in all nested constructs (& vs +)}}
+    for(int i = 0; i < 5; ++i)
+    // expected-note at -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}}
+}
+
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/207196


More information about the cfe-commits mailing list