[clang] 528e673 - [Clang][CodeGen] Fix sret lifetime marker AS mismatch after #186275 (#193850)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 23 17:46:19 PDT 2026


Author: Vigneshwar Jayakumar
Date: 2026-04-24T00:46:14Z
New Revision: 528e673fec477b087bf55da282585a9ed20b809a

URL: https://github.com/llvm/llvm-project/commit/528e673fec477b087bf55da282585a9ed20b809a
DIFF: https://github.com/llvm/llvm-project/commit/528e673fec477b087bf55da282585a9ed20b809a.diff

LOG: [Clang][CodeGen] Fix sret lifetime marker AS mismatch after #186275 (#193850)

After #186275, the sret address space can differ from the alloca address
space (e.g., AS 0 vs AS 5 on AMDGPU). In CGCall.cpp EmitCall(), when a
discarded-value sret temporary is created, SRetPtr is allocated in the
alloca AS and a lifetime.start is emitted. The pointer is then
addrspacecast'd to match the sret AS, but the CallLifetimeEnd cleanup
was using the addrspacecast'd pointer, triggering an assertion in
EmitLifetimeEnd ("Pointer should be in alloca address space").

Saves the original alloca pointer before the addrspacecast and uses it
for the lifetime-end cleanup.

Fixes buildbot failure: hip-third-party-libs-tests

Added: 
    clang/test/CodeGenHIP/sret-lifetime-markers.cpp

Modified: 
    clang/lib/CodeGen/CGCall.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 203ae746d6863..c584f2eeef937 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -5427,6 +5427,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
   // If the call returns a temporary with struct return, create a temporary
   // alloca to hold the result, unless one is given to us.
   Address SRetPtr = Address::invalid();
+  // Original alloca for lifetime markers
+  Address SRetAlloca = Address::invalid();
   bool NeedSRetLifetimeEnd = false;
   if (RetAI.isIndirect() || RetAI.isInAlloca() || RetAI.isCoerceAndExpand()) {
     // For virtual function pointer thunks and musttail calls, we must always
@@ -5441,8 +5443,11 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
       SRetPtr = ReturnValue.getAddress();
     } else {
       SRetPtr = CreateMemTempWithoutCast(RetTy, "tmp");
-      if (HaveInsertPoint() && ReturnValue.isUnused())
+      if (HaveInsertPoint() && ReturnValue.isUnused()) {
         NeedSRetLifetimeEnd = EmitLifetimeStart(SRetPtr.getBasePointer());
+        if (NeedSRetLifetimeEnd)
+          SRetAlloca = SRetPtr;
+      }
     }
     if (IRFunctionArgs.hasSRetArg()) {
       // A mismatch between the allocated return value's AS and the target's
@@ -6027,8 +6032,11 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
   // can't depend on being inside of an ExprWithCleanups, so we need to manually
   // pop this cleanup later on. Being eager about this is OK, since this
   // temporary is 'invisible' outside of the callee.
+  // Use the original alloca pointer (before any addrspacecast) for the
+  // lifetime end marker, since lifetime intrinsics must reference the alloca
+  // address space.
   if (NeedSRetLifetimeEnd)
-    pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetPtr);
+    pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca);
 
   llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest();
 

diff  --git a/clang/test/CodeGenHIP/sret-lifetime-markers.cpp b/clang/test/CodeGenHIP/sret-lifetime-markers.cpp
new file mode 100644
index 0000000000000..3752a08b5e364
--- /dev/null
+++ b/clang/test/CodeGenHIP/sret-lifetime-markers.cpp
@@ -0,0 +1,60 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --functions ".*" --include-generated-funcs --version 6
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -emit-llvm -O1 -x c++ -std=c++2b %s -o - | FileCheck --check-prefix=AMDGCN %s
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -emit-llvm -O1 -x c++ -std=c++2b %s -o - | FileCheck --check-prefix=SPIRV %s
+// REQUIRES: amdgpu-registered-target
+
+// Verify that lifetime markers for an sret temporary use the alloca address
+// space (5), even when the sret pointer itself is in the default address
+// space (0). Regression test for an assertion failure in EmitLifetimeEnd
+// when the return value of a function returning a non-trivially-copyable
+// type is discarded.
+
+template <int N>
+struct SuperScalar {
+    double val[N];
+
+    SuperScalar() {
+        for (int i = 0; i < N; i++) val[i] = 0.0;
+    }
+    SuperScalar(const SuperScalar& rhs) {
+        for (int i = 0; i < N; i++) val[i] = rhs.val[i];
+    }
+    SuperScalar& operator=(const SuperScalar& rhs) {
+        for (int i = 0; i < N; i++) val[i] = rhs.val[i];
+        return *this;
+    }
+};
+
+template <int N>
+SuperScalar<N> atomic_fetch_add(SuperScalar<N>* dest, const SuperScalar<N>& val);
+
+template <int N>
+void add_functor(SuperScalar<N>* data, int i, SuperScalar<N>& update) {
+    atomic_fetch_add(&data[i], update);
+}
+
+template void add_functor<4>(SuperScalar<4>*, int, SuperScalar<4>&);
+// AMDGCN-LABEL: define weak_odr void @_Z11add_functorILi4EEvP11SuperScalarIXT_EEiRS1_(
+// AMDGCN-SAME: ptr noundef [[DATA:%.*]], i32 noundef [[I:%.*]], ptr noundef nonnull align 8 dereferenceable(32) [[UPDATE:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] comdat {
+// AMDGCN-NEXT:  [[ENTRY:.*:]]
+// AMDGCN-NEXT:    [[TMP:%.*]] = alloca [[STRUCT_SUPERSCALAR:%.*]], align 8, addrspace(5)
+// AMDGCN-NEXT:    [[IDXPROM:%.*]] = sext i32 [[I]] to i64
+// AMDGCN-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [32 x i8], ptr [[DATA]], i64 [[IDXPROM]]
+// AMDGCN-NEXT:    call void @llvm.lifetime.start.p5(ptr addrspace(5) [[TMP]]) #[[ATTR3:[0-9]+]]
+// AMDGCN-NEXT:    [[TMP_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[TMP]] to ptr
+// AMDGCN-NEXT:    call void @_Z16atomic_fetch_addILi4EE11SuperScalarIXT_EEPS1_RKS1_(ptr dead_on_unwind writable sret([[STRUCT_SUPERSCALAR]]) align 8 [[TMP_ASCAST]], ptr noundef [[ARRAYIDX]], ptr noundef nonnull align 8 dereferenceable(32) [[UPDATE]]) #[[ATTR4:[0-9]+]]
+// AMDGCN-NEXT:    call void @llvm.lifetime.end.p5(ptr addrspace(5) [[TMP]]) #[[ATTR3]]
+// AMDGCN-NEXT:    ret void
+//
+//
+// SPIRV-LABEL: define weak_odr spir_func void @_Z11add_functorILi4EEvP11SuperScalarIXT_EEiRS1_(
+// SPIRV-SAME: ptr addrspace(4) noundef [[DATA:%.*]], i32 noundef [[I:%.*]], ptr addrspace(4) noundef align 8 dereferenceable(32) [[UPDATE:%.*]]) local_unnamed_addr addrspace(4) #[[ATTR0:[0-9]+]] comdat {
+// SPIRV-NEXT:  [[ENTRY:.*:]]
+// SPIRV-NEXT:    [[TMP:%.*]] = alloca [[STRUCT_SUPERSCALAR:%.*]], align 8
+// SPIRV-NEXT:    [[IDXPROM:%.*]] = sext i32 [[I]] to i64
+// SPIRV-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [32 x i8], ptr addrspace(4) [[DATA]], i64 [[IDXPROM]]
+// SPIRV-NEXT:    call addrspace(4) void @llvm.lifetime.start.p0(ptr nonnull [[TMP]]) #[[ATTR3:[0-9]+]]
+// SPIRV-NEXT:    call spir_func addrspace(4) void @_Z16atomic_fetch_addILi4EE11SuperScalarIXT_EEPS1_RKS1_(ptr dead_on_unwind nonnull writable sret([[STRUCT_SUPERSCALAR]]) align 8 [[TMP]], ptr addrspace(4) noundef [[ARRAYIDX]], ptr addrspace(4) noundef align 8 dereferenceable(32) [[UPDATE]]) #[[ATTR3]]
+// SPIRV-NEXT:    call addrspace(4) void @llvm.lifetime.end.p0(ptr nonnull [[TMP]]) #[[ATTR3]]
+// SPIRV-NEXT:    ret void
+//


        


More information about the cfe-commits mailing list