https://github.com/Fznamznon updated 
https://github.com/llvm/llvm-project/pull/244210

>From c2cf33a27b5a7f0370c7ac274ba7107276e2203b Mon Sep 17 00:00:00 2001
From: Mariya Podchishchaeva <[email protected]>
Date: Thu, 19 Nov 2026 07:37:55 -0600
Subject: [PATCH 1/2] [CIR] Fix address space of __ret_operand_tmp created by
 FlattenCFG

On AMDGPU (and other address space aware targets) allocas must
reside on specific address space.
---
 .../lib/CIR/Dialect/Transforms/FlattenCFG.cpp |  4 +-
 .../CodeGenHIP/cleanup-alloca-addrspace.hip   | 40 ++++++++++++++-----
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp 
b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
index c4630a5ca200a1..f11af3f4c6467b 100644
--- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
@@ -1243,7 +1243,9 @@ class CIRCleanupScopeOpFlattening
               funcOp->getParentOfType<mlir::ModuleOp>());
           uint64_t alignment =
               dataLayout.getAlignment(operand.getType(), true).value();
-          cir::PointerType ptrType = cir::PointerType::get(operand.getType());
+          cir::PointerType ptrType = cir::PointerType::get(
+              operand.getType(),
+              dataLayout.getAllocaAddrSpace(rewriter.getContext()));
           alloca =
               cir::AllocaOp::create(rewriter, loc, ptrType, 
"__ret_operand_tmp",
                                     rewriter.getI64IntegerAttr(alignment));
diff --git a/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip 
b/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip
index 7cbb8ff8301ec0..c0ece467c7488f 100644
--- a/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip
+++ b/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip
@@ -1,8 +1,13 @@
 // RUN: %clang_cc1 -triple amdgpu7.00-amd-amdhsa -x hip -fclangir 
-fcuda-is-device -emit-cir %s -o %t.cir
 // RUN: cir-opt --cir-flatten-cfg %t.cir -o %t-flat.cir
-// RUN: FileCheck --input-file=%t-flat.cir %s --check-prefix=CIR-FLAT
+// RUN: FileCheck --input-file=%t-flat.cir %s 
--check-prefix=CIR-FLAT,CIR-FLATO0
 // RUN: %clang_cc1 -triple amdgpu7.00-amd-amdhsa -x hip -fclangir 
-fcuda-is-device -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
 
+// RUN: %clang_cc1 -triple amdgpu7.00-amd-amdhsa -x hip -fclangir 
-fcuda-is-device -O1 -emit-cir %s -o %to1.cir
+// RUN: cir-opt --cir-flatten-cfg %to1.cir -o %t-flato1.cir
+// RUN: FileCheck --input-file=%t-flato1.cir %s 
--check-prefix=CIR-FLAT,CIR-FLATO1
+// RUN: %clang_cc1 -triple amdgpu7.00-amd-amdhsa -x hip -fclangir 
-fcuda-is-device -O1 -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVMOPT
+
 // Check that alloca created for cleanup destination slot for a function has
 // correct address space after flatten cfg. This is important for address-space
 // aware targets like amdgpu.
@@ -10,25 +15,36 @@
 // S is not trivially destructible, so f returns it through a generic sret
 // pointer rather than a __retval alloca.
 // CIR-FLAT-LABEL: cir.func {{.*}} @_Z1fv(%arg0: !cir.ptr<!rec_S> 
{{.*}}llvm.sret = !rec_S
-// CIR-FLAT:         cir.alloca "__cleanup_dest_slot" {{.*}} : !cir.ptr<!s32i, 
target_address_space(5)>
-// CIR-FLAT:         cir.alloca "nrvo" {{.*}} : !cir.ptr<!cir.bool, 
target_address_space(5)>
+// CIR-FLAT:       cir.alloca "__cleanup_dest_slot" {{.*}} : !cir.ptr<!s32i, 
target_address_space(5)>
+// CIR-FLAT:       cir.alloca "nrvo" {{.*}} : !cir.ptr<!cir.bool, 
target_address_space(5)>
+
+// CIR-FLATO0-LABEL: cir.func {{.*}} @_ZN1SD1Ev
+// CIR-FLATO0:       cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>, 
target_address_space(5)>
 
-// CIR-FLAT-LABEL: cir.func {{.*}} @_ZN1SD1Ev
-// CIR-FLAT:         cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>, 
target_address_space(5)>
+// CIR-FLATO0-LABEL: cir.func {{.*}} @_ZN1SD2Ev
+// CIR-FLATO0:       cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>, 
target_address_space(5)>
 
-// CIR-FLAT-LABEL: cir.func {{.*}} @_ZN1SD2Ev
-// CIR-FLAT:         cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>, 
target_address_space(5)>
+// CIR-FLATO1-LABEL: cir.func {{.*}} @_Z2f1i
+// CIR-FLATO1:       cir.alloca "__ret_operand_tmp" align(4) : !cir.ptr<!s32i>
+// CIR-FLATO1:       cir.alloca "__cleanup_dest_slot" align(4) 
cleanup_dest_slot : !cir.ptr<!s32i, target_address_space(5)>
+// CIR-FLATO1:       cir.alloca "__retval" align(4) : !cir.ptr<!rec_S1, 
target_address_space(5)>
+// CIR-FLATO1:       cir.alloca "a" align(4) init : !cir.ptr<!s32i, 
target_address_space(5)>
+// CIR-FLATO1:       cir.alloca "c" align(4) init const : !cir.ptr<!s32i, 
target_address_space(5)>
 
 // LLVM-LABEL: define {{.*}} void @_Z1fv(ptr {{.*}}sret(%struct.S)
 // LLVM:         alloca i32, align 4, addrspace(5)
 // LLVM:         alloca i8, align 1, addrspace(5)
 
-// LLVM-LABEL: define {{.*}} @_ZN1SD1Ev
+// LLVM-O0: define {{.*}} @_ZN1SD1Ev
 // LLVM:         alloca ptr, align 8, addrspace(5)
 
-// LLVM-LABEL: define {{.*}} @_ZN1SD2Ev
+// LLVM-O0: define {{.*}} @_ZN1SD2Ev
 // LLVM:         alloca ptr, align 8, addrspace(5)
 
+// Everything is optimized away but make sure verifiers agree with the 
lowering.
+// LLVMOPT-LABED: define {{.*}} i32 @_Z2f1i(i32 noundef returned %0)
+// LLVMOPT-NEXT:     ret i32 %0
+
 struct S {
   __attribute__((device)) ~S() {}
 };
@@ -37,3 +53,9 @@ __attribute__((device)) S f() {
   S s;
   return s;
 }
+
+struct S1 { int x; };
+__attribute__((device)) S1 f1(int a) {
+  const int c = a;
+  return {c};
+}

>From bdc69622ca613349388f344d832ee1302c6fdfa2 Mon Sep 17 00:00:00 2001
From: Mariya Podchishchaeva <[email protected]>
Date: Thu, 19 Nov 2026 07:46:06 -0600
Subject: [PATCH 2/2] Need more sleep

---
 clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip 
b/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip
index c0ece467c7488f..2c620e00207bed 100644
--- a/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip
+++ b/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip
@@ -25,7 +25,7 @@
 // CIR-FLATO0:       cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>, 
target_address_space(5)>
 
 // CIR-FLATO1-LABEL: cir.func {{.*}} @_Z2f1i
-// CIR-FLATO1:       cir.alloca "__ret_operand_tmp" align(4) : !cir.ptr<!s32i>
+// CIR-FLATO1:       cir.alloca "__ret_operand_tmp" align(4) : !cir.ptr<!s32i, 
target_address_space(5)>
 // CIR-FLATO1:       cir.alloca "__cleanup_dest_slot" align(4) 
cleanup_dest_slot : !cir.ptr<!s32i, target_address_space(5)>
 // CIR-FLATO1:       cir.alloca "__retval" align(4) : !cir.ptr<!rec_S1, 
target_address_space(5)>
 // CIR-FLATO1:       cir.alloca "a" align(4) init : !cir.ptr<!s32i, 
target_address_space(5)>
@@ -42,7 +42,7 @@
 // LLVM:         alloca ptr, align 8, addrspace(5)
 
 // Everything is optimized away but make sure verifiers agree with the 
lowering.
-// LLVMOPT-LABED: define {{.*}} i32 @_Z2f1i(i32 noundef returned %0)
+// LLVMOPT-LABEL: define {{.*}} i32 @_Z2f1i(i32 noundef returned %0)
 // LLVMOPT-NEXT:     ret i32 %0
 
 struct S {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to