From ede47a3b13e4afcbe77db65d2165f42867c4b866 Mon Sep 17 00:00:00 2001 From: Wenju He Date: Fri, 20 Feb 2026 10:08:25 +0800 Subject: [PATCH] [OpenCL] Suppress -Wreturn-stack-address for function-scope local variable (#181602) OpenCL local variable has lifetime of work-group, not function call stack. --- clang/lib/Sema/CheckExprLifetime.cpp | 3 +++ .../test/SemaOpenCL/function-scope-local-return.cl | 13 +++++++++++++ 2 files changed, 16 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 5d4b4508541b..4e050e9bf604 100644 --- a/clang/lib/Sema/CheckExprLifetime.cpp +++ b/clang/lib/Sema/CheckExprLifetime.cpp @@ -1337,6 +1337,9 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity, // expression. if (LK == LK_StmtExprResult) return false; + if (auto *VD = dyn_cast(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(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 000000000000..99dabfdc5827 --- /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 that returning a pointer to a local address space variable does not +// trigger -Wreturn-stack-address. + +// expected-no-diagnostics + +#pragma OPENCL EXTENSION __cl_clang_function_scope_local_variables : enable + +local int* get_group_scratch() { + local int data[64]; + return data; +}