[Clang][OpenMP] Fix crash on a captured region inside a requires-expression or a declare reduction #247194
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Akash Manna (akash-manna-sky)
<details>
<summary>Changes</summary>
Fixes #<!-- -->210018
Sema::CreateCapturedStmtRecordDecl walks up from CurContext to the nearest function, record or file context for the captured record, and appended the CapturedDecl to that context too, although it was created in CurContext. With anything else in between, such as a requires-expression body or, before #<!-- -->223005, an expansion statement, addHiddenDecl asserts.
Add the CapturedDecl to the context it was created in, which is what PushDeclContext and PopDeclContext already assume; the captured record keeps its placement.
AI Aided!
---
Full diff: https://github.com/llvm/llvm-project/pull/247194.diff
5 Files Affected:
````````````diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e08c693c5d5a22..7334aa5f954c92 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1225,6 +1225,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
using ``__is_constructible`` on a nested class template inside the definition
of the containing class. (#GH215166)
+- Fixed a crash when an OpenMP directive appears inside a requires-expression or a C++26 expansion statement. (#GH210018)
+
- Fixed a crash issue when a value dependent recovery init appeared in constant
evaluation context in default constant evaluator.
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 602ef09280f794..9eb3c34cd4ac6e 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -4798,8 +4798,9 @@ Sema::CreateCapturedStmtRecordDecl(CapturedDecl *&CD, SourceLocation Loc,
RD->startDefinition();
assert(NumParams > 0 && "CapturedStmt requires context parameter");
+ // CurContext need not be DC (e.g. the body of a requires-expression).
CD = CapturedDecl::Create(Context, CurContext, NumParams);
- DC->addDecl(CD);
+ CurContext->addDecl(CD);
return RD;
}
diff --git a/clang/test/OpenMP/GH210018.cpp b/clang/test/OpenMP/GH210018.cpp
new file mode 100644
index 00000000000000..f628a04decd9d9
--- /dev/null
+++ b/clang/test/OpenMP/GH210018.cpp
@@ -0,0 +1,224 @@
+// RUN: %clang_cc1 -std=c++2c -fopenmp -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++2c -fopenmp-simd -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+int g(int);
+struct S { int a; long b; };
+
+void simd_in_expansion() {
+ template for (auto x : {1, 2}) {
+#pragma omp simd
+ for (int i = 0; i < 2; i++)
+ ;
+ }
+}
+
+void directives_in_expansion(int n) {
+ template for (auto x : {1, 2}) {
+#pragma omp parallel
+ g(x);
+#pragma omp parallel for
+ for (int i = 0; i < x; i++)
+ g(x + n);
+#pragma omp for
+ for (int i = 0; i < x; i++)
+ g(i);
+#pragma omp target
+ g(x);
+#pragma omp target teams distribute parallel for
+ for (int i = 0; i < x; i++)
+ g(x);
+#pragma omp teams
+ g(x);
+#pragma omp task
+ g(x);
+#pragma omp taskloop
+ for (int i = 0; i < x; i++)
+ g(x);
+#pragma omp sections
+ {
+#pragma omp section
+ g(x);
+ }
+#pragma omp single
+ g(x);
+#pragma omp critical
+ g(x);
+ int v = 0;
+#pragma omp atomic
+ v += x;
+#pragma omp parallel
+ {
+#pragma omp for
+ for (int i = 0; i < x; i++)
+ g(i);
+#pragma omp task
+ g(x);
+ }
+#pragma omp parallel firstprivate(n)
+ g(x + n);
+#pragma omp for
+#pragma omp unroll partial(2)
+ for (int i = 0; i < 8; i++)
+ g(x + i);
+#pragma omp tile sizes(2)
+ for (int i = 0; i < 8; i++)
+ g(x + i);
+ }
+}
+
+void nested_expansion() {
+ template for (auto x : {1, 2}) {
+ template for (auto y : {3, 4}) {
+#pragma omp parallel for
+ for (int i = 0; i < x; i++)
+ g(x + y);
+ }
+ }
+}
+
+void destructuring_expansion(S s) {
+ template for (auto x : s) {
+#pragma omp parallel for
+ for (int i = 0; i < 2; i++)
+ g(x);
+ }
+ template for (constexpr auto x : {1, 2}) {
+#pragma omp simd
+ for (int i = 0; i < x; i++)
+ g(x);
+ static_assert(x > 0);
+ }
+ constexpr int arr[] = {1, 2, 3};
+ template for (auto x : arr) {
+#pragma omp parallel
+ g(x);
+ }
+}
+
+template <typename T>
+void expansion_in_template(T t) {
+ T local = t;
+ template for (auto x : {1, 2}) {
+#pragma omp parallel for
+ for (int i = 0; i < 2; i++)
+ g(x + local + i);
+ }
+}
+template void expansion_in_template<int>(int);
+
+template <typename... Ts>
+void dependent_expansion(Ts... ts) {
+ template for (auto x : {ts...}) {
+#pragma omp parallel
+ g(x);
+#pragma omp simd
+ for (int i = 0; i < x; i++)
+ g(x + i);
+ }
+}
+template void dependent_expansion<int, int, int>(int, int, int);
+
+template <typename T>
+void dependent_destructuring(T t) {
+ template for (auto x : t) {
+#pragma omp parallel for
+ for (int i = 0; i < 2; i++)
+ g(x);
+ }
+}
+template void dependent_destructuring<S>(S);
+
+template <typename T>
+struct C {
+ T m;
+ void mem() {
+ template for (auto x : {1, 2}) {
+#pragma omp parallel
+ g(x + m);
+ }
+ }
+};
+template struct C<int>;
+
+void expansion_in_lambda() {
+ auto l = [] {
+ template for (auto x : {1, 2}) {
+#pragma omp parallel
+ g(x);
+ }
+ };
+ l();
+}
+
+void lambda_and_local_class_in_region() {
+ template for (auto x : {1, 2}) {
+#pragma omp parallel
+ {
+ struct L { int m; } l{x};
+ [&] { g(l.m + x); }();
+ }
+ }
+}
+
+struct R { int v; };
+#pragma omp declare reduction(plus : R : omp_out.v += omp_in.v) initializer(omp_priv = {0})
+
+void declare_reduction_in_expansion() {
+ template for (auto x : {1, 2}) {
+#pragma omp declare reduction(mine : int : omp_out += omp_in) initializer(omp_priv = 0)
+ int r = 0;
+#pragma omp parallel reduction(mine : r)
+ r += x;
+ R rr{0};
+#pragma omp parallel reduction(plus : rr)
+ rr.v += x;
+ }
+}
+
+void lambda_in_expansion_control() {
+ template for (auto x : {1, 2}) {
+ [&] { return x; }();
+ }
+}
+
+void region_in_requires_expression() {
+ bool b = requires {
+ ({
+#pragma omp parallel
+ g(1);
+ 7;
+ });
+ };
+ (void)b;
+}
+
+template <typename T>
+void region_in_requires_expression_in_template(T t) {
+ bool b = requires {
+ ({
+#pragma omp parallel for
+ for (int i = 0; i < 2; i++)
+ g(t + i);
+ 7;
+ });
+ };
+ (void)b;
+}
+template void region_in_requires_expression_in_template<int>(int);
+
+void controls(int n) {
+#pragma omp parallel
+ g(n);
+ [&] {
+#pragma omp parallel for
+ for (int i = 0; i < n; i++)
+ g(i);
+ }();
+ struct Local {
+ void f(int m) {
+#pragma omp parallel
+ g(m);
+ }
+ };
+}
diff --git a/clang/test/OpenMP/GH210018_codegen.cpp b/clang/test/OpenMP/GH210018_codegen.cpp
new file mode 100644
index 00000000000000..6b5078f86d2daf
--- /dev/null
+++ b/clang/test/OpenMP/GH210018_codegen.cpp
@@ -0,0 +1,65 @@
+// RUN: %clang_cc1 -std=c++2c -fopenmp -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OMP
+// RUN: %clang_cc1 -std=c++2c -fopenmp-simd -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,SIMD
+
+int g(int);
+
+void parallel_in_expansion(int n) {
+ template for (auto x : {1, 2}) {
+#pragma omp parallel
+ g(x + n);
+ }
+}
+
+void simd_in_expansion() {
+ template for (auto x : {1, 2}) {
+#pragma omp simd
+ for (int i = 0; i < x; i++)
+ g(i);
+ }
+}
+
+template <typename... Ts>
+void dependent_expansion(Ts... ts) {
+ template for (auto x : {ts...}) {
+#pragma omp parallel for
+ for (int i = 0; i < 2; i++)
+ g(x + i);
+ }
+}
+template void dependent_expansion<int, int>(int, int);
+
+// CHECK-LABEL: define {{.*}}void @_Z21parallel_in_expansioni(
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 2, ptr @[[OUTLINED1:_Z21parallel_in_expansioni.omp_outlined]], ptr %{{.+}}, ptr %{{.+}})
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 2, ptr @[[OUTLINED2:_Z21parallel_in_expansioni.omp_outlined.[0-9]+]], ptr %{{.+}}, ptr %{{.+}})
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+
+// OMP: define internal void @[[OUTLINED1]](
+// OMP: call {{.*}}i32 @_Z1gi(
+// OMP: define internal void @[[OUTLINED2]](
+// OMP: call {{.*}}i32 @_Z1gi(
+
+// CHECK-LABEL: define {{.*}}void @_Z17simd_in_expansionv(
+// CHECK: br label %{{.+}}, !llvm.loop ![[LOOP1:[0-9]+]]
+// CHECK: br label %{{.+}}, !llvm.loop ![[LOOP2:[0-9]+]]
+
+// CHECK-LABEL: define {{.*}}void @_Z19dependent_expansionIJiiEEvDpT_(
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 1, ptr @[[OUTLINED3:_Z19dependent_expansionIJiiEEvDpT_.omp_outlined]], ptr %{{.+}})
+// OMP: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 1, ptr @[[OUTLINED4:_Z19dependent_expansionIJiiEEvDpT_.omp_outlined.[0-9]+]], ptr %{{.+}})
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+// SIMD: call {{.*}}i32 @_Z1gi(
+// SIMD-NOT: @__kmpc_fork_call
+
+// OMP: define internal void @[[OUTLINED3]](
+// OMP: call void @__kmpc_for_static_init_4(
+// OMP: define internal void @[[OUTLINED4]](
+// OMP: call void @__kmpc_for_static_init_4(
+
+// CHECK: ![[LOOP1]] = distinct !{![[LOOP1]], !{{[0-9]+}}, ![[VEC:[0-9]+]]}
+// CHECK: ![[VEC]] = !{!"llvm.loop.vectorize.enable"}
+// CHECK: ![[LOOP2]] = distinct !{![[LOOP2]], !{{[0-9]+}}, ![[VEC]]}
diff --git a/clang/test/SemaCXX/captured-statements.cpp b/clang/test/SemaCXX/captured-statements.cpp
index d8f77e6017ab89..717ed5a04e4d91 100644
--- a/clang/test/SemaCXX/captured-statements.cpp
+++ b/clang/test/SemaCXX/captured-statements.cpp
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -fblocks
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++2c %s -fblocks
void test_nest_lambda() {
int x;
@@ -171,3 +172,50 @@ void test_capture_with_attributes() {
{
}
}
+
+#if __cplusplus > 202302L
+void test_capture_in_expansion_statement(int n) {
+ template for (auto x : {1, 2}) {
+ #pragma clang __debug captured
+ {
+ n += x;
+ }
+ template for (auto y : {3, 4}) {
+ #pragma clang __debug captured
+ {
+ n += x + y;
+ }
+ }
+ }
+}
+
+template <typename T>
+void test_capture_in_dependent_expansion_statement(T &t) {
+ template for (auto x : t) {
+ #pragma clang __debug captured
+ {
+ x += 1;
+ }
+ }
+}
+
+struct Pair { int a; long b; };
+void test_dependent_expansion_statement() {
+ Pair p{1, 2};
+ test_capture_in_dependent_expansion_statement(p);
+}
+
+void test_capture_in_requires_expression() {
+ bool b = requires {
+ ({
+ int n = 0;
+ #pragma clang __debug captured
+ {
+ n += 1;
+ }
+ n;
+ });
+ };
+ (void)b;
+}
+#endif
</details>
No, while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
DC = DC->getParent();
check shall exit in the correct DC, i.e. need to extend the check here
Done, the walk now also stops at a requires-expression body and the CapturedDecl goes to DC again.
1. Capturing enclosing locals/params inside a requires-expression fails, crashes on #pragma omp unroll, and silently makes PR's template test evaluate to false
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -20055,13 +20055,21 @@ static bool isVariableAlreadyCapturedInScopeInfo(CapturingScopeInfo *CSI,
// Only block literals, captured statements, and lambda expressions can
// capture; other scopes don't work.
static DeclContext *getParentOfCapturingContextOrNull(DeclContext *DC,
ValueDecl *Var,
+ DeclContext *VarDC,
SourceLocation Loc,
const bool Diagnose,
Sema &S) {
- if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC) || isLambdaCallOperator(DC))
- return getLambdaAwareParentOfDeclContext(DC);
+ if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC) || isLambdaCallOperator(DC)) {
+ DeclContext *ParentDC = getLambdaAwareParentOfDeclContext(DC);
+ if (isa<CapturedDecl>(DC)) {
+ while (ParentDC && !ParentDC->Equals(VarDC) &&
+ (ParentDC->isRequiresExprBody() || ParentDC->isExpansionStmt()))
+ ParentDC = ParentDC->getParent();
+ }
+ return ParentDC;
+ }
VarDecl *Underlying = Var->getPotentiallyDecomposedVarDecl();
if (Underlying) {
@@ -20572,6 +20580,8 @@ bool Sema::tryCaptureVariable(
"FunctionScopes.");
while (FSIndex != MaxFunctionScopesIndex) {
DC = getLambdaAwareParentOfDeclContext(DC);
+ while (DC && (DC->isRequiresExprBody() || DC->isExpansionStmt()))
+ DC = DC->getParent();
--FSIndex;
}
}
@@ -20629,7 +20639,7 @@ bool Sema::tryCaptureVariable(
DeclContext *ParentDC =
!IsInScopeDeclarationContext
? DC->getParent()
- : getParentOfCapturingContextOrNull(DC, Var, ExprLoc,
+ : getParentOfCapturingContextOrNull(DC, Var, VarDC, ExprLoc,
BuildAndDiagnose, *this);
// We need to check for the parent *first* because, if we *have*
// private-captured a global variable, we need to recursively capture it in
2. New llvm_unreachable crash exposed when CapturedDecl appears in a requires-expression inside file-scope #pragma omp declare reduction
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -923,7 +923,7 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
const Decl *D = Decl::castFromDeclContext(DC);
return ComputeName(IK, D);
}
- llvm_unreachable("CapturedDecl not inside a function or method");
+ return ComputeName(IK, CD->getASTContext().getTranslationUnitDecl());
}
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
3. OMPDeclareReductionDecl still hits the exact same addHiddenDecl assertion
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -4781,8 +4781,8 @@ RecordDecl*
Sema::CreateCapturedStmtRecordDecl(CapturedDecl *&CD, SourceLocation Loc,
unsigned NumParams) {
DeclContext *DC = CurContext;
- while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext() ||
- DC->isRequiresExprBody()))
+ while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext() ||
+ DC->isRequiresExprBody() || isa<OMPDeclareReductionDecl>(DC)))
DC = DC->getParent();
4. Wrong section, mid-section insertion, inaccurate scope
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1245,8 +1245,6 @@
using ``__is_constructible`` on a nested class template inside the definition
of the containing class. (#GH215166)
-- Fixed a crash when an OpenMP directive appears inside a requires-expression or a C++26 expansion statement. (#GH210018)
-
- Fixed a crash issue when a value dependent recovery init appeared in constant
evaluation context in default constant evaluator.
@@ -1944,6 +1942,9 @@
- Optimization remarks for a loop associated with an OpenMP loop directive now
point at the loop instead of the `#pragma omp` line.
+- Fixed a crash when an OpenMP directive or captured statement appears inside a
+ requires-expression. (#GH210018)
+
### SYCL Support
Capturing enclosing locals/params inside a requires-expression fails, crashes on #pragma omp unroll, and silently makes PR's template test evaluate to false
Applied, plus the same stop-at-VarDC guard on the top-level requires-body skip in tryCaptureVariable. A standalone unroll has no captured region; its loop variable and .capture_expr. live in the requires-expression body and were still reported as declared in an enclosing context without it.
New llvm_unreachable crash exposed when CapturedDecl appears in a requires-expression inside file-scope #pragma omp declare reduction
Done.
OMPDeclareReductionDecl still hits the exact same addHiddenDecl assertion
Done.
Wrong section, mid-section insertion, inaccurate scope
Moved to the end of the OpenMP section with that wording.
@alexey-bataev
What are they for?
Is it sane for people to write OpenMP clauses inside a requires expression?
PLEASE ask your AI to work on something else and stop wasting your token quota on this nonsense issue. Thanks.
alexey-bataev
akash-manna-sky
Fixes #210018
Sema::CreateCapturedStmtRecordDeclwalks up fromCurContextto the nearest function, record or file context and puts both the captured record and theCapturedDeclthere, although theCapturedDeclis created inCurContext. A requires-expression body or adeclare reductioncombiner is neither, soaddHiddenDeclasserts. Captures from such a region also stopped at the requires-expression body, so enclosing locals were rejected and the requires-expression silently evaluated to false.Stop the walk at those contexts, let the capture walk step over a requires-expression body or expansion statement unless the variable lives there, and give
__func__in a region with no enclosing function an empty name instead of hittingllvm_unreachable.AI Aided!