[clang] [Clang][OpenMP] Fix crash on a tuple-like structured binding in a task (PR #241357)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 04:54:57 PST 2026
https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/241357
>From dcb4cb6f987e0b55cd0ba7d40c728df1e0e58ce9 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Wed, 11 Nov 2026 11:20:11 +0530
Subject: [PATCH 1/3] [Clang][OpenMP] Fix crash on a tuple-like structured
binding in a task
Fixes #116258
A tuple-like structured binding (`std::pair`, `std::tuple`) referenced in a `task` or named in `firstprivate` crashed clang right after the "not yet supported" error. `isOpenMPCapturedDecl` maps struct and array bindings to their `DecompositionDecl` but let tuple-like ones fall into the data-member path, which dereferences a private copy that a binding's DSA never has.
A tuple-like binding has no variable the region can capture, so `isOpenMPCapturedDecl` now returns null for it, as it already did when the binding's DSA was shared.
LLM tools were used for this contribution.
---
clang/lib/Sema/SemaOpenMP.cpp | 7 ++--
.../OpenMP/structured-bindings-messages.cpp | 32 +++++++++++++++++++
2 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 9acf3b3724527..f4ff8bc86b606 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -2476,8 +2476,11 @@ VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
D = getCanonicalDecl(D);
if (auto *BD = dyn_cast<BindingDecl>(D)) {
- if (!BD->getHoldingVar())
- D = cast<VarDecl>(BD->getDecomposedDecl());
+ // Tuple-like bindings are not capturable; the non-VarDecl paths below
+ // expect a data member with a private copy.
+ if (BD->getHoldingVar())
+ return nullptr;
+ D = cast<VarDecl>(BD->getDecomposedDecl());
}
auto *VD = dyn_cast<VarDecl>(D);
// Do not capture constexpr variables.
diff --git a/clang/test/OpenMP/structured-bindings-messages.cpp b/clang/test/OpenMP/structured-bindings-messages.cpp
index 88316d317ef98..b29eaf0f6def2 100644
--- a/clang/test/OpenMP/structured-bindings-messages.cpp
+++ b/clang/test/OpenMP/structured-bindings-messages.cpp
@@ -147,6 +147,38 @@ void test_pair_map() {
}
}
+// GH116258
+void test_pair_task() {
+ auto [a, b] = std::make_pair(1, 2);
+ // expected-note at -1 4{{'a' declared here}}
+ // expected-note at -2{{'b' declared here}}
+#pragma omp task
+ {
+ use(a);
+ // expected-error at -1{{capturing tuple-like structured binding 'a' is not yet supported in OpenMP}}
+ }
+ [[omp::directive(task)]]
+ {
+ use(b);
+ // expected-error at -1{{capturing tuple-like structured binding 'b' is not yet supported in OpenMP}}
+ }
+#pragma omp taskloop
+ for (int i = 0; i < 2; ++i) {
+ use(a);
+ // expected-error at -1{{capturing tuple-like structured binding 'a' is not yet supported in OpenMP}}
+ }
+#pragma omp task firstprivate(a)
+ {
+ use(a);
+ // expected-error at -1{{capturing tuple-like structured binding 'a' is not yet supported in OpenMP}}
+ }
+#pragma omp parallel firstprivate(a)
+ {
+ use(a);
+ // expected-error at -1{{capturing tuple-like structured binding 'a' is not yet supported in OpenMP}}
+ }
+}
+
void test_tuple() {
std::tuple<int, int, int> t = {1, 2, 3};
auto [x, y, z] = t;
>From 300435d8dcee80db1bc8324361d2b1b36297156c Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Thu, 12 Nov 2026 09:15:05 +0530
Subject: [PATCH 2/3] [Clang][OpenMP] Report the holding variable for a
privatized tuple-like binding
Returning null let a namespace-scope binding take the global early return in `tryCaptureVariable`, so its private copy was bypassed. A privatized tuple-like binding now reports its holding variable from `isOpenMPCapturedDecl`, and its uses still reach the capture checks.
---
clang/lib/Sema/SemaOpenMP.cpp | 19 +++++++----
.../OpenMP/structured-bindings-messages.cpp | 33 +++++++++++++++++++
2 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index f4ff8bc86b606..f9215afd2bb42 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -2475,12 +2475,13 @@ VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
assert(getLangOpts().OpenMP && "OpenMP is not allowed");
D = getCanonicalDecl(D);
+ // A tuple-like binding has no private copy. A privatized one reports its
+ // holding variable instead, so its uses still go through the capture checks.
+ VarDecl *HoldingVar = nullptr;
if (auto *BD = dyn_cast<BindingDecl>(D)) {
- // Tuple-like bindings are not capturable; the non-VarDecl paths below
- // expect a data member with a private copy.
- if (BD->getHoldingVar())
- return nullptr;
- D = cast<VarDecl>(BD->getDecomposedDecl());
+ HoldingVar = BD->getHoldingVar();
+ if (!HoldingVar)
+ D = cast<VarDecl>(BD->getDecomposedDecl());
}
auto *VD = dyn_cast<VarDecl>(D);
// Do not capture constexpr variables.
@@ -2572,7 +2573,9 @@ VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
if (DVarTop.CKind != OMPC_unknown && isOpenMPPrivate(DVarTop.CKind) &&
(!VD || VD->hasLocalStorage() ||
!(DVarTop.AppliedToPointee && DVarTop.CKind != OMPC_reduction)))
- return VD ? VD : cast<VarDecl>(DVarTop.PrivateCopy->getDecl());
+ return VD ? VD
+ : HoldingVar ? HoldingVar
+ : cast<VarDecl>(DVarTop.PrivateCopy->getDecl());
// Threadprivate variables must not be captured.
if (isOpenMPThreadPrivate(DVarTop.CKind))
return nullptr;
@@ -2635,7 +2638,9 @@ VarDecl *SemaOpenMP::isOpenMPCapturedDecl(ValueDecl *D, bool CheckScopeInfo,
(VD && (DSAStack->getDefaultDSA() == DSA_none ||
DSAStack->getDefaultDSA() == DSA_private ||
DSAStack->getDefaultDSA() == DSA_firstprivate)))
- return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
+ return VD ? VD
+ : HoldingVar ? HoldingVar
+ : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
}
return nullptr;
}
diff --git a/clang/test/OpenMP/structured-bindings-messages.cpp b/clang/test/OpenMP/structured-bindings-messages.cpp
index b29eaf0f6def2..d3fda0095089a 100644
--- a/clang/test/OpenMP/structured-bindings-messages.cpp
+++ b/clang/test/OpenMP/structured-bindings-messages.cpp
@@ -179,6 +179,39 @@ void test_pair_task() {
}
}
+auto [ga, gb] = std::make_pair(1, 2);
+// expected-note at -1 2{{'ga' declared here}}
+
+void test_pair_global() {
+#pragma omp parallel private(ga)
+ use(ga);
+#pragma omp task firstprivate(ga)
+ {
+ use(ga);
+ // expected-error at -1{{capturing tuple-like structured binding 'ga' is not yet supported in OpenMP}}
+ }
+#pragma omp parallel for lastprivate(ga)
+ for (int i = 0; i < 2; ++i) {
+ use(ga);
+ // expected-error at -1{{capturing tuple-like structured binding 'ga' is not yet supported in OpenMP}}
+ }
+}
+
+template <typename T> void test_pair_task_template(T p) {
+ auto [a, b] = p;
+ // expected-note at -1{{'a' declared here}}
+#pragma omp task
+ {
+ use(a);
+ // expected-error at -1{{capturing tuple-like structured binding 'a' is not yet supported in OpenMP}}
+ }
+}
+
+void instantiate_pair_task_template() {
+ test_pair_task_template(std::make_pair(1, 2));
+ // expected-note at -1{{in instantiation of function template specialization}}
+}
+
void test_tuple() {
std::tuple<int, int, int> t = {1, 2, 3};
auto [x, y, z] = t;
>From e1af33a495f5f42f0e9c1c7adaae2df93b55e2bf Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Thu, 12 Nov 2026 18:24:05 +0530
Subject: [PATCH 3/3] [Clang][OpenMP] Classify a binding by the storage of its
decomposed variable.
---
clang/lib/Sema/SemaOpenMP.cpp | 9 +++++++--
clang/test/OpenMP/structured-bindings-messages.cpp | 2 ++
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index f9215afd2bb42..b9fe3447a47e2 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -1350,19 +1350,24 @@ DSAStackTy::DSAVarData DSAStackTy::getDSA(const_iterator &Iter,
const auto *FD = dyn_cast<FieldDecl>(D);
DSAVarData DVar;
if (Iter == end()) {
+ // A binding has the storage of the variable it decomposes.
+ const VarDecl *StorageVD = VD;
+ if (const auto *BD = dyn_cast<BindingDecl>(D))
+ StorageVD = BD->getDecomposedDecl();
// OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
// in a region but not in construct]
// File-scope or namespace-scope variables referenced in called routines
// in the region are shared unless they appear in a threadprivate
// directive.
- if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
+ if (StorageVD && !StorageVD->isFunctionOrMethodVarDecl() &&
+ !isa<ParmVarDecl>(StorageVD))
DVar.CKind = OMPC_shared;
// OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
// in a region but not in construct]
// Variables with static storage duration that are declared in called
// routines in the region are shared.
- if (VD && VD->hasGlobalStorage())
+ if (StorageVD && StorageVD->hasGlobalStorage())
DVar.CKind = OMPC_shared;
// Non-static data members are shared by default.
diff --git a/clang/test/OpenMP/structured-bindings-messages.cpp b/clang/test/OpenMP/structured-bindings-messages.cpp
index d3fda0095089a..d98a429386a2e 100644
--- a/clang/test/OpenMP/structured-bindings-messages.cpp
+++ b/clang/test/OpenMP/structured-bindings-messages.cpp
@@ -185,6 +185,8 @@ auto [ga, gb] = std::make_pair(1, 2);
void test_pair_global() {
#pragma omp parallel private(ga)
use(ga);
+#pragma omp task
+ use(ga);
#pragma omp task firstprivate(ga)
{
use(ga);
More information about the cfe-commits
mailing list