[clang] [Clang][OpenMP] Fix crash on `cancel taskgroup` inside `masked taskloop` (PR #240335)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 9 03:13:09 PST 2026
https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/240335
Fixes #154552
`#pragma omp cancel taskgroup` inside a `masked taskloop` asserted in `getOMPCancelDestination`. The cancel is emitted in the outlined task function, whose exit target came from a list of directive kinds that stopped at `master taskloop`, so `OMPD_masked_taskloop` fell through to the worksharing assertion. `parallel masked taskloop` and `cancellation point taskgroup` hit it too.
Pick the target with `isOpenMPTaskingDirective`, so every task-based region returns from its outlined function, and read `HasCancel` from the masked `taskloop` directives like the master ones.
AI Aided!
>From b3d2896cbbfa9bf5b5349801dbee9470ae13c5be Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Mon, 9 Nov 2026 09:40:29 +0530
Subject: [PATCH] [Clang][OpenMP] Fix crash on `cancel taskgroup` inside
`masked taskloop`
Fixes #154552
`#pragma omp cancel taskgroup` inside a `masked taskloop` asserted in `getOMPCancelDestination`. The cancel is emitted in the outlined task function, whose exit target came from a list of directive kinds that stopped at `master taskloop`, so `OMPD_masked_taskloop` fell through to the worksharing assertion. `parallel masked taskloop` and `cancellation point taskgroup` hit it too.
Pick the target with `isOpenMPTaskingDirective`, so every task-based region returns from its outlined function, and read `HasCancel` from the masked taskloop directives like the master ones.
Assisted-by: LLMs.
---
clang/docs/ReleaseNotes.md | 1 +
clang/include/clang/Basic/OpenMPKinds.h | 5 +-
clang/lib/CodeGen/CGOpenMPRuntime.cpp | 4 +
clang/lib/CodeGen/CGStmtOpenMP.cpp | 5 +-
clang/test/OpenMP/GH154552.cpp | 110 ++++++++++++++++++
.../test/OpenMP/Inputs/nesting_of_regions.cpp | 28 +++++
6 files changed, 147 insertions(+), 6 deletions(-)
create mode 100644 clang/test/OpenMP/GH154552.cpp
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..b11c2fffd8dcce 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1471,6 +1471,7 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
- Fixed an OpenMP `requires` directive read from a PCH or module losing its effect on
semantic checks, which caused spurious `reverse_offload` errors.
+- Fixed a crash on `cancel taskgroup` inside a `masked taskloop` directive. (#GH154552)
- Added the OpenMP 6.1 `#pragma omp flatten` loop transformation and the
`depth` clause. Flatten combines perfectly nested canonical loops into one
loop. `depth(k)` selects how many outermost loops to combine (default 2).
diff --git a/clang/include/clang/Basic/OpenMPKinds.h b/clang/include/clang/Basic/OpenMPKinds.h
index 5e94b4fa582b27..a10d2d4bb34473 100644
--- a/clang/include/clang/Basic/OpenMPKinds.h
+++ b/clang/include/clang/Basic/OpenMPKinds.h
@@ -435,9 +435,8 @@ bool isOpenMPPrivate(OpenMPClauseKind Kind);
/// \return true - the clause is a threadprivate clause, otherwise - false.
bool isOpenMPThreadPrivate(OpenMPClauseKind Kind);
-/// Checks if the specified directive kind is one of tasking directives - task,
-/// taskloop, taksloop simd, master taskloop, parallel master taskloop, master
-/// taskloop simd, or parallel master taskloop simd.
+/// Checks if the specified directive kind is one of tasking directives - task
+/// or any taskloop-based directive.
bool isOpenMPTaskingDirective(OpenMPDirectiveKind Kind);
/// Checks if the specified directive kind is one of the composite or combined
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index d3a6ffee3ea748..3b28fcecc2cf1f 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -1334,8 +1334,12 @@ llvm::Function *CGOpenMPRuntime::emitTaskOutlinedFunction(
HasCancel = TD->hasCancel();
else if (const auto *TD = dyn_cast<OMPMasterTaskLoopDirective>(&D))
HasCancel = TD->hasCancel();
+ else if (const auto *TD = dyn_cast<OMPMaskedTaskLoopDirective>(&D))
+ HasCancel = TD->hasCancel();
else if (const auto *TD = dyn_cast<OMPParallelMasterTaskLoopDirective>(&D))
HasCancel = TD->hasCancel();
+ else if (const auto *TD = dyn_cast<OMPParallelMaskedTaskLoopDirective>(&D))
+ HasCancel = TD->hasCancel();
CodeGenFunction CGF(CGM, true);
CGOpenMPTaskOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen,
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index e63957df3e4069..6a42cebb63aa0e 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -8137,9 +8137,8 @@ void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
CodeGenFunction::JumpDest
CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
- if (Kind == OMPD_parallel || Kind == OMPD_task ||
- Kind == OMPD_target_parallel || Kind == OMPD_taskloop ||
- Kind == OMPD_master_taskloop || Kind == OMPD_parallel_master_taskloop)
+ if (Kind == OMPD_parallel || Kind == OMPD_target_parallel ||
+ isOpenMPTaskingDirective(Kind))
return ReturnBlock;
assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for ||
diff --git a/clang/test/OpenMP/GH154552.cpp b/clang/test/OpenMP/GH154552.cpp
new file mode 100644
index 00000000000000..d70b528a116326
--- /dev/null
+++ b/clang/test/OpenMP/GH154552.cpp
@@ -0,0 +1,110 @@
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-linux-gnu -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
+
+// RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
+// RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-unknown-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-unknown-linux-gnu -include-pch %t -verify %s -emit-llvm -o - | FileCheck --check-prefix SIMD-ONLY0 %s
+// SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
+// expected-no-diagnostics
+#ifndef HEADER
+#define HEADER
+
+// CHECK-LABEL: define {{.*}}void @_Z22masked_taskloop_cancelv()
+// CHECK: call i32 @__kmpc_masked(ptr [[DEFLOC:@.+]], i32 [[GTID:%.+]], i32 0)
+// CHECK: call void @__kmpc_taskgroup(ptr [[DEFLOC]], i32 [[GTID]])
+// CHECK: call ptr @__kmpc_omp_task_alloc(ptr [[DEFLOC]], i32 [[GTID]], i32 1, i64 80, i64 1, ptr [[TASK1:@.+]])
+// CHECK: call void @__kmpc_taskloop(ptr [[DEFLOC]], i32 [[GTID]],
+// CHECK: call void @__kmpc_end_taskgroup(ptr [[DEFLOC]], i32 [[GTID]])
+// CHECK: call void @__kmpc_end_masked(ptr [[DEFLOC]], i32 [[GTID]])
+void masked_taskloop_cancel() {
+#pragma omp masked taskloop
+ for (int j = 0; j < 42; j++) {
+#pragma omp cancel taskgroup
+ }
+}
+
+// CHECK: define internal {{.*}}i32 [[TASK1]](
+// CHECK: [[RES:%.+]] = call i32 @__kmpc_cancel(ptr @{{.+}}, i32 %{{.+}}, i32 4)
+// CHECK-NEXT: [[IS_CANCEL:%.+]] = icmp ne i32 [[RES]], 0
+// CHECK-NEXT: br i1 [[IS_CANCEL]], label %[[EXIT:.+]], label %{{.+}}
+// CHECK: [[EXIT]]:
+// CHECK-NEXT: store i32 1, ptr {{%.+}},
+// CHECK-NEXT: br label %[[DONE:[^,]+]]
+// CHECK: [[DONE]]:
+// CHECK: ret i32 0
+
+// CHECK-LABEL: define {{.*}}void @_Z34masked_taskloop_cancellation_pointv()
+// CHECK: call i32 @__kmpc_masked(ptr [[DEFLOC]], i32 [[GTID:%.+]], i32 0)
+// CHECK: call ptr @__kmpc_omp_task_alloc(ptr [[DEFLOC]], i32 [[GTID]], i32 1, i64 80, i64 1, ptr [[TASK2:@.+]])
+// CHECK: call void @__kmpc_taskloop(ptr [[DEFLOC]], i32 [[GTID]],
+// CHECK: call void @__kmpc_end_masked(ptr [[DEFLOC]], i32 [[GTID]])
+void masked_taskloop_cancellation_point() {
+#pragma omp masked taskloop
+ for (int j = 0; j < 42; j++) {
+#pragma omp cancellation point taskgroup
+ }
+}
+
+// CHECK: define internal {{.*}}i32 [[TASK2]](
+// CHECK: [[RES:%.+]] = call i32 @__kmpc_cancellationpoint(ptr @{{.+}}, i32 %{{.+}}, i32 4)
+// CHECK-NEXT: [[IS_CANCEL:%.+]] = icmp ne i32 [[RES]], 0
+// CHECK-NEXT: br i1 [[IS_CANCEL]], label %[[EXIT:.+]], label %{{.+}}
+// CHECK: [[EXIT]]:
+// CHECK-NEXT: store i32 1, ptr {{%.+}},
+// CHECK-NEXT: br label %[[DONE:[^,]+]]
+// CHECK: [[DONE]]:
+// CHECK: ret i32 0
+
+// CHECK-LABEL: define {{.*}}void @_Z31parallel_masked_taskloop_cancelv()
+// CHECK: call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @{{.+}}, i32 0, ptr [[OUTLINED:@.+]])
+void parallel_masked_taskloop_cancel() {
+#pragma omp parallel masked taskloop
+ for (int j = 0; j < 42; j++) {
+#pragma omp cancel taskgroup
+#pragma omp cancellation point taskgroup
+ }
+}
+
+// CHECK: define internal void [[OUTLINED]](
+// CHECK: call i32 @__kmpc_masked(ptr [[DEFLOC]], i32 [[GTID:%.+]], i32 0)
+// CHECK: call void @__kmpc_taskgroup(ptr [[DEFLOC]], i32 [[GTID]])
+// CHECK: call ptr @__kmpc_omp_task_alloc(ptr [[DEFLOC]], i32 [[GTID]], i32 1, i64 80, i64 1, ptr [[TASK3:@.+]])
+// CHECK: call void @__kmpc_taskloop(ptr [[DEFLOC]], i32 [[GTID]],
+// CHECK: call void @__kmpc_end_taskgroup(ptr [[DEFLOC]], i32 [[GTID]])
+// CHECK: call void @__kmpc_end_masked(ptr [[DEFLOC]], i32 [[GTID]])
+
+// CHECK: define internal {{.*}}i32 [[TASK3]](
+// CHECK: [[RES:%.+]] = call i32 @__kmpc_cancel(ptr @{{.+}}, i32 %{{.+}}, i32 4)
+// CHECK-NEXT: [[IS_CANCEL:%.+]] = icmp ne i32 [[RES]], 0
+// CHECK-NEXT: br i1 [[IS_CANCEL]], label %[[EXIT:.+]], label %{{.+}}
+// CHECK: [[EXIT]]:
+// CHECK-NEXT: store i32 1, ptr [[CLEANUP_SLOT:%.+]],
+// CHECK-NEXT: br label %[[DONE:[^,]+]]
+// CHECK: [[RES:%.+]] = call i32 @__kmpc_cancellationpoint(ptr @{{.+}}, i32 %{{.+}}, i32 4)
+// CHECK-NEXT: [[IS_CANCEL:%.+]] = icmp ne i32 [[RES]], 0
+// CHECK-NEXT: br i1 [[IS_CANCEL]], label %[[EXIT2:.+]], label %{{.+}}
+// CHECK: [[EXIT2]]:
+// CHECK-NEXT: store i32 1, ptr [[CLEANUP_SLOT]],
+// CHECK-NEXT: br label %[[DONE]]
+// CHECK: [[DONE]]:
+// CHECK: ret i32 0
+
+// CHECK-LABEL: define {{.*}}void @_Z22master_taskloop_cancelv()
+// CHECK: call i32 @__kmpc_master(ptr [[DEFLOC]], i32 [[GTID:%.+]])
+// CHECK: call ptr @__kmpc_omp_task_alloc(ptr [[DEFLOC]], i32 [[GTID]], i32 1, i64 80, i64 1, ptr [[TASK4:@.+]])
+// CHECK: call void @__kmpc_end_master(ptr [[DEFLOC]], i32 [[GTID]])
+void master_taskloop_cancel() {
+#pragma omp master taskloop
+ for (int j = 0; j < 42; j++) {
+#pragma omp cancel taskgroup
+#pragma omp cancellation point taskgroup
+ }
+}
+
+// CHECK: define internal {{.*}}i32 [[TASK4]](
+// CHECK: call i32 @__kmpc_cancel(ptr @{{.+}}, i32 %{{.+}}, i32 4)
+// CHECK: call i32 @__kmpc_cancellationpoint(ptr @{{.+}}, i32 %{{.+}}, i32 4)
+// CHECK: ret i32 0
+
+#endif
diff --git a/clang/test/OpenMP/Inputs/nesting_of_regions.cpp b/clang/test/OpenMP/Inputs/nesting_of_regions.cpp
index 6fff7a9e6025bb..f80ec455fa4981 100644
--- a/clang/test/OpenMP/Inputs/nesting_of_regions.cpp
+++ b/clang/test/OpenMP/Inputs/nesting_of_regions.cpp
@@ -6063,6 +6063,34 @@ void foo() {
#pragma omp cancel parallel // expected-error {{region cannot be closely nested inside 'parallel master taskloop' region}}
#pragma omp cancellation point parallel // expected-error {{region cannot be closely nested inside 'parallel master taskloop' region}}
}
+#pragma omp masked taskloop
+ for (int i = 0; i < 10; ++i) {
+#pragma omp cancel taskgroup // omp45-error {{region cannot be closely nested inside 'masked taskloop' region}}
+#pragma omp cancellation point taskgroup // omp45-error {{region cannot be closely nested inside 'masked taskloop' region}}
+ }
+#pragma omp masked taskloop
+ for (int i = 0; i < 10; ++i) {
+#pragma omp cancel parallel // expected-error {{region cannot be closely nested inside 'masked taskloop' region}}
+#pragma omp cancellation point parallel // expected-error {{region cannot be closely nested inside 'masked taskloop' region}}
+ }
+#pragma omp masked taskloop simd
+ for (int i = 0; i < 10; ++i) {
+#pragma omp cancel taskgroup // omp45-error {{OpenMP constructs may not be nested inside a simd region}} omp50-error {{OpenMP constructs may not be nested inside a simd region except for ordered simd, simd, scan, atomic, or loop directive}} omp51-error {{OpenMP constructs may not be nested inside a simd region except for ordered simd, simd, scan, atomic, or loop directive}}
+ }
+#pragma omp parallel masked taskloop
+ for (int i = 0; i < 10; ++i) {
+#pragma omp cancel taskgroup // omp45-error {{region cannot be closely nested inside 'parallel masked taskloop' region}}
+#pragma omp cancellation point taskgroup // omp45-error {{region cannot be closely nested inside 'parallel masked taskloop' region}}
+ }
+#pragma omp parallel masked taskloop
+ for (int i = 0; i < 10; ++i) {
+#pragma omp cancel parallel // expected-error {{region cannot be closely nested inside 'parallel masked taskloop' region}}
+#pragma omp cancellation point parallel // expected-error {{region cannot be closely nested inside 'parallel masked taskloop' region}}
+ }
+#pragma omp parallel masked taskloop simd
+ for (int i = 0; i < 10; ++i) {
+#pragma omp cancel taskgroup // omp45-error {{OpenMP constructs may not be nested inside a simd region}} omp50-error {{OpenMP constructs may not be nested inside a simd region except for ordered simd, simd, scan, atomic, or loop directive}} omp51-error {{OpenMP constructs may not be nested inside a simd region except for ordered simd, simd, scan, atomic, or loop directive}}
+ }
// DISTRIBUTE DIRECTIVE
#pragma omp target
More information about the cfe-commits
mailing list