[clang] [OpenCL] Suppress -Wreturn-stack-address for function-scope local variable (PR #181602)
Wenju He via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 18 18:55:45 PST 2026
https://github.com/wenju-he updated https://github.com/llvm/llvm-project/pull/181602
>From 2b014e12d738f03e1a6b17eae3f61c8401e287b5 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Mon, 16 Feb 2026 07:18:28 +0100
Subject: [PATCH 1/2] [OpenCL] Suppress -Wreturn-stack-address for
function-scope local variable
when __cl_clang_function_scope_local_variables extension is enabled.
OpenCL local variable has lifetime of work-group, not function call stack.
---
clang/lib/Sema/CheckExprLifetime.cpp | 7 +++++++
.../test/SemaOpenCL/function-scope-local-return.cl | 13 +++++++++++++
2 files changed, 20 insertions(+)
create mode 100644 clang/test/SemaOpenCL/function-scope-local-return.cl
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 5d4b4508541b7..c2f2aa8fc9487 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -1337,6 +1337,13 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
// expression.
if (LK == LK_StmtExprResult)
return false;
+ if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
+ if (VD->getType().getAddressSpace() == LangAS::opencl_local &&
+ SemaRef.getOpenCLOptions().isAvailableOption(
+ "__cl_clang_function_scope_local_variables",
+ SemaRef.getLangOpts()))
+ return false;
+ }
SemaRef.Diag(DiagLoc, diag::warn_ret_stack_addr_ref)
<< InitEntity->getType()->isReferenceType() << DRE->getDecl()
<< isa<ParmVarDecl>(DRE->getDecl()) << (LK == LK_MustTail)
diff --git a/clang/test/SemaOpenCL/function-scope-local-return.cl b/clang/test/SemaOpenCL/function-scope-local-return.cl
new file mode 100644
index 0000000000000..1de86b4293a9a
--- /dev/null
+++ b/clang/test/SemaOpenCL/function-scope-local-return.cl
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL3.0
+
+// Check returning a pointer to a local address space variable does not warn
+// if __cl_clang_function_scope_local_variables extension is enabled.
+
+// expected-no-diagnostics
+
+#pragma OPENCL EXTENSION __cl_clang_function_scope_local_variables : enable
+
+local int* get_group_scratch() {
+ local int data[64];
+ return data;
+}
>From 69dadbb4ffda1af0d1bc0e268facb0a6bc29ad76 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Thu, 19 Feb 2026 03:51:17 +0100
Subject: [PATCH 2/2] drop __cl_clang_function_scope_local_variables check
---
clang/lib/Sema/CheckExprLifetime.cpp | 8 ++------
clang/test/SemaOpenCL/function-scope-local-return.cl | 4 ++--
2 files changed, 4 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index c2f2aa8fc9487..4e050e9bf6045 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -1337,13 +1337,9 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
// expression.
if (LK == LK_StmtExprResult)
return false;
- if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
- if (VD->getType().getAddressSpace() == LangAS::opencl_local &&
- SemaRef.getOpenCLOptions().isAvailableOption(
- "__cl_clang_function_scope_local_variables",
- SemaRef.getLangOpts()))
+ if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
+ if (VD->getType().getAddressSpace() == LangAS::opencl_local)
return false;
- }
SemaRef.Diag(DiagLoc, diag::warn_ret_stack_addr_ref)
<< InitEntity->getType()->isReferenceType() << DRE->getDecl()
<< isa<ParmVarDecl>(DRE->getDecl()) << (LK == LK_MustTail)
diff --git a/clang/test/SemaOpenCL/function-scope-local-return.cl b/clang/test/SemaOpenCL/function-scope-local-return.cl
index 1de86b4293a9a..8b3c5e6cbe2f0 100644
--- a/clang/test/SemaOpenCL/function-scope-local-return.cl
+++ b/clang/test/SemaOpenCL/function-scope-local-return.cl
@@ -1,7 +1,7 @@
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL3.0
-// Check returning a pointer to a local address space variable does not warn
-// if __cl_clang_function_scope_local_variables extension is enabled.
+// Check tjat returning a pointer to a local address space variable does not
+// trigger -Wreturn-stack-address.
// expected-no-diagnostics
More information about the cfe-commits
mailing list