[clang] [Clang][OpenMP] Fix crash on an `omp allocate` local in unreachable code (PR #242302)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 05:55:50 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen
Author: Akash Manna (akash-manna-sky)
<details>
<summary>Changes</summary>
Fixes #<!-- -->140336
CodeGen visits declarations even after the insertion point has been cleared, but the `omp allocate` path still emitted the `__kmpc_alloc` call there. For a declaration nothing can reach, such as a `switch` body before its first `case`, the call ended up outside any basic block and the verifier rejected the module.
Both allocation hooks now return early when there is no insertion point and the variable keeps its plain alloca. Jumping into the scope of such a variable is now an error, as for `__attribute__((cleanup))` and as GCC does (Here I follow the GCC behavior, if you prefer we could change the approach in that case): the allocation would be skipped but the `__kmpc_free` at scope exit would still run.
AI assisted!
---
Full diff: https://github.com/llvm/llvm-project/pull/242302.diff
7 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+1)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+4)
- (modified) clang/lib/CodeGen/CGOpenMPRuntime.cpp (+3)
- (modified) clang/lib/CodeGen/CGStmtOpenMP.cpp (+3)
- (modified) clang/lib/Sema/JumpDiagnostics.cpp (+5)
- (added) clang/test/OpenMP/GH140336.c (+104)
- (modified) clang/test/OpenMP/allocate_messages.cpp (+23)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77..704d997934057 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1533,6 +1533,7 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
modifications to the bindings will not be reflected in the original variable.
To ensure correct behavior, either use the original variable directly in the
target region or map the bindings explicitly instead.
+- Fixed a crash when a variable with `#pragma omp allocate` is declared in unreachable code; jumping into its scope is now an error. (#GH140336)
- Fixed `-fopenmp` and `-fopenmp-simd` host compilations dropping diagnostics when no
offload target was configured, which accepted `try`/`throw` with exceptions disabled
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 56cc972178106..fb5987b530642 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7024,6 +7024,8 @@ def note_protected_by_variable_non_pod : Note<
"jump bypasses initialization of non-POD variable">;
def note_protected_by_cleanup : Note<
"jump bypasses initialization of variable with __attribute__((cleanup))">;
+def note_protected_by_omp_allocate : Note<
+ "jump bypasses allocation of variable with '#pragma omp allocate'">;
def note_protected_by_vla_typedef : Note<
"jump bypasses initialization of VLA typedef">;
def note_protected_by_vla_type_alias : Note<
@@ -7084,6 +7086,8 @@ def note_enters_defer_stmt : Note<"jump enters a defer statement">;
def note_exits_cleanup : Note<
"jump exits scope of variable with __attribute__((cleanup))">;
+def note_exits_omp_allocate : Note<
+ "jump exits scope of variable with '#pragma omp allocate'">;
def note_exits_dtor : Note<
"jump exits scope of variable with non-trivial destructor">;
def note_exits_temporary_dtor : Note<
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index d3a6ffee3ea74..591f1e8bcd2be 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -12799,6 +12799,9 @@ Address CGOpenMPRuntime::getAddressOfLocalVariable(CodeGenFunction &CGF,
// Use the default allocation.
if (!isAllocatableDecl(VD))
return UntiedAddr;
+ // Unreachable declaration: nothing can run the allocation.
+ if (!CGF.HaveInsertPoint())
+ return UntiedRealAddr.isValid() ? UntiedRealAddr : Address::invalid();
llvm::Value *Size;
CharUnits Align = CGM.getContext().getDeclAlign(CVD);
if (CVD->getType()->isVariablyModifiedType()) {
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index e63957df3e406..97b845d6c6ab8 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2202,6 +2202,9 @@ Address CodeGenFunction::OMPBuilderCBHelpers::getAddressOfLocalVariable(
const VarDecl *CVD = VD->getCanonicalDecl();
if (!isAllocatableDecl(CVD))
return Address::invalid();
+ // Unreachable declaration: nothing can run the allocation.
+ if (!CGF.HaveInsertPoint())
+ return Address::invalid();
llvm::Value *Size;
CharUnits Align = CGM.getContext().getDeclAlign(CVD);
if (CVD->getType()->isVariablyModifiedType()) {
diff --git a/clang/lib/Sema/JumpDiagnostics.cpp b/clang/lib/Sema/JumpDiagnostics.cpp
index bbe6f644609bf..7ef8ae07b346b 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -157,6 +157,11 @@ static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
return ScopePair(diag::note_protected_by_cleanup,
diag::note_exits_cleanup);
+ // Allocated at the declaration and freed at scope exit, like cleanup.
+ if (VD->hasLocalStorage() && VD->hasAttr<OMPAllocateDeclAttr>())
+ return ScopePair(diag::note_protected_by_omp_allocate,
+ diag::note_exits_omp_allocate);
+
if (VD->hasLocalStorage()) {
switch (VD->getType().isDestructedType()) {
case QualType::DK_objc_strong_lifetime:
diff --git a/clang/test/OpenMP/GH140336.c b/clang/test/OpenMP/GH140336.c
new file mode 100644
index 0000000000000..e9e0eb1bc6c7e
--- /dev/null
+++ b/clang/test/OpenMP/GH140336.c
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -verify -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fopenmp-enable-irbuilder -verify -emit-llvm -o - %s | FileCheck %s --check-prefix=IRB
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -x c++ -verify -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CXX
+// expected-no-diagnostics
+
+typedef enum omp_allocator_handle_t {
+ omp_null_allocator = 0,
+ omp_default_mem_alloc = 1,
+ omp_large_cap_mem_alloc = 2,
+ omp_const_mem_alloc = 3,
+ omp_high_bw_mem_alloc = 4,
+ omp_low_lat_mem_alloc = 5,
+ omp_cgroup_mem_alloc = 6,
+ omp_pteam_mem_alloc = 7,
+ omp_thread_mem_alloc = 8,
+ KMP_ALLOCATOR_MAX_HANDLE = __UINTPTR_MAX__
+} omp_allocator_handle_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void stop(void) __attribute__((noreturn));
+
+// CHECK-LABEL: define {{.*}}void @gh140336(
+// CHECK-NOT: @__kmpc_
+// CHECK: switch i32
+// CHECK-NOT: @__kmpc_
+// CHECK: ret void
+// IRB-LABEL: define {{.*}}void @gh140336(
+// IRB-NOT: @__kmpc_
+// IRB: ret void
+void gh140336(int i) {
+ switch (i) {
+ int j;
+#pragma omp allocate(j)
+ }
+}
+
+// CHECK-LABEL: define {{.*}}void @after_return(
+// CHECK-NOT: @__kmpc_
+// CHECK: ret void
+// IRB-LABEL: define {{.*}}void @after_return(
+// IRB-NOT: @__kmpc_
+// IRB: ret void
+void after_return(int i) {
+ return;
+ int j;
+#pragma omp allocate(j) allocator(omp_large_cap_mem_alloc)
+ j = i;
+}
+
+// CHECK-LABEL: define {{.*}}void @after_noreturn(
+// CHECK-NOT: @__kmpc_
+// CHECK: unreachable
+// IRB-LABEL: define {{.*}}void @after_noreturn(
+// IRB-NOT: @__kmpc_
+// IRB: unreachable
+void after_noreturn(void) {
+ stop();
+ int j;
+#pragma omp allocate(j) align(32) allocator(omp_large_cap_mem_alloc)
+}
+
+// CHECK-LABEL: define {{.*}}void @reachable(
+// CHECK: [[TID:%.+]] = call i32 @__kmpc_global_thread_num(ptr @{{.+}})
+// CHECK: [[J:%.+]] = call ptr @__kmpc_alloc(i32 [[TID]], i64 4, ptr inttoptr (i64 2 to ptr))
+// CHECK: [[K:%.+]] = call ptr @__kmpc_alloc(i32 [[TID]], i64 4, ptr null)
+// CHECK: store i32 %{{.+}}, ptr [[J]],
+// CHECK: store i32 %{{.+}}, ptr [[K]],
+// CHECK: call void @__kmpc_free(i32 [[TID]], ptr [[K]], ptr null)
+// CHECK: call void @__kmpc_free(i32 [[TID]], ptr [[J]], ptr inttoptr (i64 2 to ptr))
+// CHECK: ret void
+// IRB-LABEL: define {{.*}}void @reachable(
+// IRB: [[J:%.+]] = call ptr @__kmpc_alloc(i32 %{{.+}}, i64 4, ptr inttoptr (i64 2 to ptr))
+// IRB: store i32 %{{.+}}, ptr [[J]],
+// IRB: call void @__kmpc_free(i32 %{{.+}}, ptr [[J]], ptr inttoptr (i64 2 to ptr))
+// IRB: ret void
+void reachable(int i) {
+ int j;
+#pragma omp allocate(j) allocator(omp_large_cap_mem_alloc)
+ int k;
+#pragma omp allocate(k)
+ j = i;
+ k = i;
+}
+
+#ifdef __cplusplus
+}
+
+struct S {
+ ~S();
+};
+
+// CXX-LABEL: define {{.*}}void @_Z17after_return_dtorv(
+// CXX-NOT: @__kmpc_
+// CXX-NOT: @_ZN1SD
+// CXX: ret void
+void after_return_dtor() {
+ return;
+ S s;
+#pragma omp allocate(s) allocator(omp_large_cap_mem_alloc)
+}
+#endif
diff --git a/clang/test/OpenMP/allocate_messages.cpp b/clang/test/OpenMP/allocate_messages.cpp
index cde714244c643..ecda3aca98a42 100644
--- a/clang/test/OpenMP/allocate_messages.cpp
+++ b/clang/test/OpenMP/allocate_messages.cpp
@@ -149,3 +149,26 @@ int main(int argc, char **argv) {
#pragma omp parallel allocate(d) // expected-error {{the referenced item is not found in any private clause on the same directive}}
;
}
+
+void gh140336(int i) {
+ switch (i) {
+ int j; // expected-note {{jump bypasses allocation of variable with '#pragma omp allocate'}}
+#pragma omp allocate(j)
+ case 1: // expected-error {{cannot jump from switch statement to this case label}}
+ j = 3;
+ break;
+ }
+ goto label2; // expected-error {{cannot jump from this goto statement to its label}}
+ int k; // expected-note {{jump bypasses allocation of variable with '#pragma omp allocate'}}
+#pragma omp allocate(k)
+label2:
+ k = 4;
+ void *p = &&label3;
+ {
+ int l; // expected-note {{jump exits scope of variable with '#pragma omp allocate'}}
+#pragma omp allocate(l)
+ goto *p; // expected-error {{cannot jump from this indirect goto statement to one of its possible targets}}
+ }
+label3: // expected-note {{possible target of indirect goto statement}}
+ ;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/242302
More information about the cfe-commits
mailing list