[clang] 46ca555 - [CIR][AMDGPU] Look through address_space views of __retval in insertSRetStores (#239151)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 22:50:48 PST 2026
Author: Chaitanya
Date: 2026-11-06T12:20:43+05:30
New Revision: 46ca55573ce669e89f3c452a745d4f471b74f20c
URL: https://github.com/llvm/llvm-project/commit/46ca55573ce669e89f3c452a745d4f471b74f20c
DIFF: https://github.com/llvm/llvm-project/commit/46ca55573ce669e89f3c452a745d4f471b74f20c.diff
LOG: [CIR][AMDGPU] Look through address_space views of __retval in insertSRetStores (#239151)
Summary:
- On AMDGPU, a non-trivial record is returned through a default address
space `sret` pointer, but `CIRGen` puts `__retval` in addrspace(5) and
uses it through an `address_space` cast.
- `insertSRetStores` replaced the alloca with the sret pointer even
though the types differ. The cast then became an identity cast:
`%0` = cir.cast address_space %arg0 : !cir.ptr<!rec_NT> ->
!cir.ptr<!rec_NT>`
- When the types differ, replace the alloca's `address_space` views with
the sret pointer instead, and erase the alloca once its return loads are
gone.
- Test: `amdgpu-sret-nontrivial.cir`
Relates to: https://github.com/llvm/llvm-project/issues/220471
Assisted by : claude opus 5.5
Added:
clang/test/CIR/Transforms/abi-lowering/amdgpu-sret-nontrivial.cir
Modified:
clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
Removed:
################################################################################
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index d329938b69cdc..577e56f3d3b4c 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -1406,6 +1406,19 @@ insertArgCoercion(mlir::FunctionOpInterface funcOp,
return mlir::success();
}
+/// Replace each address_space cast of \p slot to the type of \p ptr with
+/// \p ptr.
+void replaceAddrSpaceViews(cir::AllocaOp slot, mlir::Value ptr) {
+ for (mlir::Operation *user : llvm::make_early_inc_range(slot->getUsers())) {
+ auto view = mlir::dyn_cast<cir::CastOp>(user);
+ if (view && view.getKind() == cir::CastKind::address_space &&
+ view.getType() == ptr.getType()) {
+ view.getResult().replaceAllUsesWith(ptr);
+ view->erase();
+ }
+ }
+}
+
/// Rewrite each cir.return so the return value flows through the sret
/// pointer (the prepended first block argument) and the function returns
/// void.
@@ -1434,6 +1447,9 @@ insertArgCoercion(mlir::FunctionOpInterface funcOp,
/// `cir.return (cir.load <alloca>)` shape is an invariant guaranteed by
/// CIRGen, so it is asserted via `cast<>` rather than guarded with a
/// fallback.
+///
+/// A `__retval` outside the sret pointer's address space is not replaced
+/// itself. Its address_space casts to the sret pointer's type are.
void insertSRetStores(mlir::FunctionOpInterface funcOp, mlir::Type origRetTy,
mlir::OpBuilder &builder) {
mlir::Value sretPtr = funcOp.getArguments()[0];
@@ -1454,12 +1470,13 @@ void insertSRetStores(mlir::FunctionOpInterface funcOp, mlir::Type origRetTy,
// Rewire the shared `__retval` alloca to the sret pointer once.
// replaceAllUsesWith updates every load of the alloca (including those
// feeding the other cir.return ops) to read from sretPtr instead, so
- // all returns are covered by this single rewiring. Only then is the
- // now-unused alloca safe to erase.
+ // all returns are covered by this single rewiring.
if (!retAlloca) {
retAlloca = mlir::cast<cir::AllocaOp>(retLoad.getAddr().getDefiningOp());
- retAlloca.getResult().replaceAllUsesWith(sretPtr);
- retAlloca->erase();
+ if (retAlloca.getType() == sretPtr.getType())
+ retAlloca.getResult().replaceAllUsesWith(sretPtr);
+ else
+ replaceAddrSpaceViews(retAlloca, sretPtr);
}
// The sret slot now holds the return value directly; replace the
@@ -1470,6 +1487,12 @@ void insertSRetStores(mlir::FunctionOpInterface funcOp, mlir::Type origRetTy,
if (retLoad.use_empty())
retLoad->erase();
}
+
+ if (retAlloca) {
+ assert(retAlloca->use_empty() &&
+ "__retval used other than by its returns and address_space views");
+ retAlloca->erase();
+ }
}
/// Build the attribute dictionary for the sret slot (slot 0 of an
diff --git a/clang/test/CIR/Transforms/abi-lowering/amdgpu-sret-nontrivial.cir b/clang/test/CIR/Transforms/abi-lowering/amdgpu-sret-nontrivial.cir
new file mode 100644
index 0000000000000..48b0b907ff772
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/amdgpu-sret-nontrivial.cir
@@ -0,0 +1,54 @@
+// RUN: cir-opt %s -cir-call-conv-lowering=target=amdgpu | FileCheck %s
+
+// A non-trivial record is returned through a default address space sret on
+// AMDGPU, while CIRGen puts `__retval` in addrspace(5) and views it through an
+// address_space cast. The view becomes the sret pointer itself.
+
+!s32i = !cir.int<s, 32>
+!rec_NT = !cir.struct<"NT" {data !s32i}>
+
+module attributes {
+ cir.record_layouts = {NT = #cir.record_layout<
+ arg_passing_kind = cannot_pass_in_regs, has_trivial_dtor = false,
+ record_align = 4>},
+ dlti.dl_spec = #dlti.dl_spec<
+ #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+ #dlti.dl_entry<i64, dense<64>: vector<2xi64>>,
+ #dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui64>>
+} {
+
+ cir.func private @_ZN2NTC1Ev(!cir.ptr<!rec_NT>)
+ cir.func private @make() -> !rec_NT requires_call_conv_lowering
+
+ // CHECK: cir.func private @_ZN2NTC1Ev(!cir.ptr<!rec_NT>)
+ // CHECK-NEXT: cir.func private @make(!cir.ptr<!rec_NT> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.sret = !rec_NT, llvm.writable})
+
+ // The constructor builds the result in the caller's sret slot.
+ cir.func @ret_ctor() -> !rec_NT requires_call_conv_lowering {
+ %0 = cir.alloca "__retval" align(4) init : !cir.ptr<!rec_NT, target_address_space(5)>
+ %1 = cir.cast address_space %0 : !cir.ptr<!rec_NT, target_address_space(5)> -> !cir.ptr<!rec_NT>
+ cir.call @_ZN2NTC1Ev(%1) : (!cir.ptr<!rec_NT>) -> ()
+ %2 = cir.load %0 : !cir.ptr<!rec_NT, target_address_space(5)>, !rec_NT
+ cir.return %2 : !rec_NT
+ }
+
+ // CHECK-NEXT: cir.func @ret_ctor(%[[SRET:.*]]: !cir.ptr<!rec_NT> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.noalias, llvm.sret = !rec_NT, llvm.writable}) {
+ // CHECK-NEXT: cir.call @_ZN2NTC1Ev(%[[SRET]]) : (!cir.ptr<!rec_NT>) -> ()
+ // CHECK-NEXT: cir.return
+ // CHECK-NEXT: }
+
+ // The returned call writes straight into this function's own sret slot.
+ cir.func @ret_call() -> !rec_NT requires_call_conv_lowering {
+ %0 = cir.alloca "__retval" align(4) : !cir.ptr<!rec_NT, target_address_space(5)>
+ %1 = cir.cast address_space %0 : !cir.ptr<!rec_NT, target_address_space(5)> -> !cir.ptr<!rec_NT>
+ %2 = cir.call @make() : () -> !rec_NT
+ cir.store align(4) %2, %1 : !rec_NT, !cir.ptr<!rec_NT>
+ %3 = cir.load %0 : !cir.ptr<!rec_NT, target_address_space(5)>, !rec_NT
+ cir.return %3 : !rec_NT
+ }
+
+ // CHECK-NEXT: cir.func @ret_call(%[[SRET:.*]]: !cir.ptr<!rec_NT> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.noalias, llvm.sret = !rec_NT, llvm.writable}) {
+ // CHECK-NEXT: cir.call @make(%[[SRET]]) : (!cir.ptr<!rec_NT> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.sret = !rec_NT, llvm.writable}) -> ()
+ // CHECK-NEXT: cir.return
+ // CHECK-NEXT: }
+}
More information about the cfe-commits
mailing list