https://github.com/skc7 updated https://github.com/llvm/llvm-project/pull/239158
>From 0fad01e6fe5443bb9236bbd6d5b81844e0906d4f Mon Sep 17 00:00:00 2001 From: skc7 <[email protected]> Date: Thu, 5 Nov 2026 15:10:21 +0530 Subject: [PATCH] [CIR][AMDGPU] Look through address_space views when forwarding indirect args --- .../TargetLowering/CIRABIRewriteContext.cpp | 49 +++++++++++-------- .../Transforms/abi-lowering/amdgpu-byref.cir | 16 ++++++ .../abi-lowering/indirect-byval.cir | 20 ++++---- .../indirect-non-byval-forward-param.cir | 47 ++++++++++++++++++ .../abi-lowering/indirect-non-byval-nyi.cir | 45 ----------------- 5 files changed, 101 insertions(+), 76 deletions(-) diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp index 577e56f3d3b4c..a36ae03bc7dbe 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp @@ -964,15 +964,16 @@ static cir::LoadOp getWholeRecordLoad(mlir::Value recordVal) { return load; } -/// Returns \p load if the address it read from can be passed in place of the -/// loaded value for an indirect argument that needs \p minAlign alignment, -/// and null otherwise, including when \p load is null. A byval or byref -/// argument also needs storageUnwrittenBetween. On null the caller reports a -/// non-byval argument or fills a slot of its own. +/// Returns the address \p load read from, as a pointer of type \p paramTy, if +/// it can be passed in place of the loaded value for an indirect argument that +/// needs \p minAlign alignment, and null otherwise, including when \p load is +/// null. A byval or byref argument also needs storageUnwrittenBetween. On +/// null the caller reports a non-byval argument or fills a slot of its own. /// /// The address has to have \p paramTy, the rewritten parameter's type, since a -/// cir.load pins the pointee type without pinning the address space. The -/// storage it names has to be a slot allocated here, reached through +/// cir.load pins the pointee type without pinning the address space. An +/// address_space cast of a \p paramTy pointer qualifies through its source. +/// The storage it names has to be a slot allocated here, reached through /// storage-preserving casts, already stating at least \p minAlign. The /// enclosing function's own non-byval parameter qualifies as well, since its /// slot stands until finalizeParameterSlots and prepareNonByvalParameters has @@ -984,13 +985,21 @@ static cir::LoadOp getWholeRecordLoad(mlir::Value recordVal) { /// Raising a slot's alignment here would not hold for one that stands in /// for a parameter, since finalizeParameterSlots replaces it with the /// incoming pointer, which promises only what the caller gave it. -static cir::LoadOp forwardableIndirectLoad(cir::LoadOp load, +static mlir::Value forwardableIndirectAddr(cir::LoadOp load, cir::PointerType paramTy, uint64_t minAlign) { - if (!load || load.getAddr().getType() != paramTy) + if (!load) return {}; - cir::AllocaOp slot = cir::getUnderlyingAlloca(load.getAddr()); - return slot && slot.getAlignment() >= minAlign ? load : cir::LoadOp(); + mlir::Value addr = load.getAddr(); + // CIRGen reads an alloca outside the default address space through a cast. + if (auto view = addr.getDefiningOp<cir::CastOp>(); + view && view.getKind() == cir::CastKind::address_space && + view.getSrc().getType() == paramTy) + addr = view.getSrc(); + if (addr.getType() != paramTy) + return {}; + cir::AllocaOp slot = cir::getUnderlyingAlloca(addr); + return slot && slot.getAlignment() >= minAlign ? addr : mlir::Value(); } /// True when \p aliasAnalysis shows that nothing between \p load and \p call @@ -2236,14 +2245,14 @@ CIRABIRewriteContext::rewriteCallSite(mlir::Operation *callOp, // loaded value, so a store to that storage after the load is visible to // the callee. byval fills a slot below, this cannot. if (!byCopy) { - cir::LoadOp fwdLoad = forwardableIndirectLoad(srcLoad, ptrTy, minAlign); - if (!fwdLoad) + mlir::Value fwdAddr = forwardableIndirectAddr(srcLoad, ptrTy, minAlign); + if (!fwdAddr) return call->emitOpError() << "non-byval indirect argument that does not name the " "caller's storage is not yet implemented in " "CallConvLowering"; - newArgs.push_back(fwdLoad.getAddr()); - deadRecordLoads.push_back(fwdLoad); + newArgs.push_back(fwdAddr); + deadRecordLoads.push_back(srcLoad); continue; } @@ -2253,12 +2262,12 @@ CIRABIRewriteContext::rewriteCallSite(mlir::Operation *callOp, // call, since the operand carries the value as of the load. A volatile // load is not forwarded, since the copy byval takes at the call is not // volatile. - if (cir::LoadOp fwdLoad = - forwardableIndirectLoad(srcLoad, ptrTy, minAlign); - fwdLoad && storageUnwrittenBetween(fwdLoad, call.getOperation(), + if (mlir::Value fwdAddr = + forwardableIndirectAddr(srcLoad, ptrTy, minAlign); + fwdAddr && storageUnwrittenBetween(srcLoad, call.getOperation(), aliasAnalysis)) { - newArgs.push_back(fwdLoad.getAddr()); - deadRecordLoads.push_back(fwdLoad); + newArgs.push_back(fwdAddr); + deadRecordLoads.push_back(srcLoad); continue; } diff --git a/clang/test/CIR/Transforms/abi-lowering/amdgpu-byref.cir b/clang/test/CIR/Transforms/abi-lowering/amdgpu-byref.cir index 45a4886a33ee5..374c5b446c5d5 100644 --- a/clang/test/CIR/Transforms/abi-lowering/amdgpu-byref.cir +++ b/clang/test/CIR/Transforms/abi-lowering/amdgpu-byref.cir @@ -52,4 +52,20 @@ module attributes { // CHECK-NEXT: %[[B_SLOT:.*]] = cir.alloca "b" align(4) : !cir.ptr<!rec_Big, target_address_space(5)> // CHECK-NEXT: cir.call @take_big(%[[B_SLOT]]) : (!cir.ptr<!rec_Big, target_address_space(5)> {llvm.align = 4 : i64, llvm.byref = !rec_Big, llvm.noundef}) -> () // CHECK-NEXT: cir.return + + // CIRGen reads the local through its default address space view. The + // addrspace(5) alloca under the view is passed, not a copy of it. + cir.func @call_big_view() requires_call_conv_lowering { + %0 = cir.alloca "b" align(4) : !cir.ptr<!rec_Big, target_address_space(5)> + %1 = cir.cast address_space %0 : !cir.ptr<!rec_Big, target_address_space(5)> -> !cir.ptr<!rec_Big> + %2 = cir.load %1 : !cir.ptr<!rec_Big>, !rec_Big + cir.call @take_big(%2) : (!rec_Big) -> () + cir.return + } + + // CHECK: cir.func{{.*}} @call_big_view() { + // CHECK-NEXT: %[[B_SLOT:.*]] = cir.alloca "b" align(4) : !cir.ptr<!rec_Big, target_address_space(5)> + // CHECK-NEXT: %{{.*}} = cir.cast address_space %[[B_SLOT]] : !cir.ptr<!rec_Big, target_address_space(5)> -> !cir.ptr<!rec_Big> + // CHECK-NEXT: cir.call @take_big(%[[B_SLOT]]) : (!cir.ptr<!rec_Big, target_address_space(5)> {llvm.align = 4 : i64, llvm.byref = !rec_Big, llvm.noundef}) -> () + // CHECK-NEXT: cir.return } diff --git a/clang/test/CIR/Transforms/abi-lowering/indirect-byval.cir b/clang/test/CIR/Transforms/abi-lowering/indirect-byval.cir index 4976580cc8fc0..87244540df0e5 100644 --- a/clang/test/CIR/Transforms/abi-lowering/indirect-byval.cir +++ b/clang/test/CIR/Transforms/abi-lowering/indirect-byval.cir @@ -802,9 +802,8 @@ module attributes { // CHECK-NEXT: cir.call @takes_big_align16(%[[SLOT]]) : (!cir.ptr<!rec_Big> // CHECK-SAME: llvm.align = 16 : i64, llvm.byval = !rec_Big, llvm.noundef - // The slot underneath the cast is aligned well enough, but the rewritten - // parameter is a pointer in the default address space, so the operand - // cannot be handed on and a slot of its own is filled instead. + // The operand is loaded through an address_space cast of a slot that already + // has the rewritten parameter's type, so the slot itself is handed on. cir.func @caller_byval_through_addrspace(%s: !rec_Big) requires_call_conv_lowering attributes { test_classify = #passthrough } { %raw = cir.alloca "raw" align(8) : !cir.ptr<!rec_Big> @@ -817,14 +816,13 @@ module attributes { cir.return } - // CHECK: cir.func{{.*}} @caller_byval_through_addrspace - // CHECK: %[[RAW:.*]] = cir.alloca "raw" align(8) : !cir.ptr<!rec_Big> - // CHECK: %[[AS:.*]] = cir.cast address_space %[[RAW]] - // CHECK: %[[SLOT:.*]] = cir.alloca "byval" align(8) : !cir.ptr<!rec_Big> - // CHECK-NEXT: cir.copy %[[AS]] align(8) to %[[SLOT]] align(8) - // CHECK-SAME: : !cir.ptr<!rec_Big, target_address_space(5)>, !cir.ptr<!rec_Big> - // CHECK-NEXT: %{{.*}} = cir.call @takes_big(%[[SLOT]]) : - // CHECK-SAME: llvm.byval = !rec_Big, llvm.noundef + // CHECK: cir.func{{.*}} @caller_byval_through_addrspace(%[[S:.*]]: !rec_Big) + // CHECK-NEXT: %[[RAW:.*]] = cir.alloca "raw" align(8) : !cir.ptr<!rec_Big> + // CHECK-NEXT: %[[AS:.*]] = cir.cast address_space %[[RAW]] : !cir.ptr<!rec_Big> -> !cir.ptr<!rec_Big, target_address_space(5)> + // CHECK-NEXT: cir.store %[[S]], %[[AS]] : !rec_Big, !cir.ptr<!rec_Big, target_address_space(5)> + // CHECK-NEXT: %{{.*}} = cir.call @takes_big(%[[RAW]]) : + // CHECK-SAME: (!cir.ptr<!rec_Big> {llvm.align = 8 : i64, llvm.byval = !rec_Big, llvm.noundef}) -> !s32i + // CHECK-NEXT: cir.return // One load feeding both byval operands is forwarded to both, and erased // once, since neither hand-off reads it. byval makes each callee a copy diff --git a/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-forward-param.cir b/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-forward-param.cir index f82fb321dce29..ad4cbcbdb1390 100644 --- a/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-forward-param.cir +++ b/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-forward-param.cir @@ -1040,3 +1040,50 @@ module attributes { requires_call_conv_lowering attributes { test_classify = #byval_arg } } + +// ----- + +!s64i = !cir.int<s, 64> +!rec_Big = !cir.struct<"Big" {data !s64i, data !s64i, data !s64i, data !s64i}> + +#non_byval_arg = { + return = { kind = "direct" }, + args = [ { kind = "indirect", indirect_align = 8, byval = false } ] +} + +#passthrough = { + return = { kind = "direct" }, + args = [ ] +} + +module attributes { + dlti.dl_spec = #dlti.dl_spec< + #dlti.dl_entry<i64, dense<64>: vector<2xi64>>> +} { + + cir.func private @takes_big_non_byval(%arg0: !rec_Big) + requires_call_conv_lowering attributes { test_classify = #non_byval_arg } + + // The operand is loaded through an address_space cast of a slot that already + // has the rewritten parameter's type, so the slot itself is handed on. + cir.func @caller_non_byval_through_addrspace(%s: !rec_Big) + requires_call_conv_lowering attributes { test_classify = #passthrough } { + %raw = cir.alloca "raw" align(8) : !cir.ptr<!rec_Big> + %slot = cir.cast address_space %raw + : !cir.ptr<!rec_Big> -> !cir.ptr<!rec_Big, target_address_space(5)> + cir.store %s, %slot : !rec_Big, !cir.ptr<!rec_Big, target_address_space(5)> + %asload = cir.load %slot + : !cir.ptr<!rec_Big, target_address_space(5)>, !rec_Big + cir.call @takes_big_non_byval(%asload) : (!rec_Big) -> () + cir.return + } + + // CHECK: cir.func{{.*}} @caller_non_byval_through_addrspace(%[[S:.*]]: !rec_Big) + // CHECK-NEXT: %[[RAW:.*]] = cir.alloca "raw" align(8) : !cir.ptr<!rec_Big> + // CHECK-NEXT: %[[AS:.*]] = cir.cast address_space %[[RAW]] : !cir.ptr<!rec_Big> -> !cir.ptr<!rec_Big, target_address_space(5)> + // CHECK-NEXT: cir.store %[[S]], %[[AS]] : !rec_Big, !cir.ptr<!rec_Big, target_address_space(5)> + // CHECK-NEXT: cir.call @takes_big_non_byval(%[[RAW]]) : + // CHECK-SAME: (!cir.ptr<!rec_Big> {llvm.align = 8 : i64, llvm.dead_on_return = -1 : i64, llvm.dereferenceable = 32 : i64, llvm.nofreeobj, llvm.noundef}) -> () + // CHECK-NEXT: cir.return + +} diff --git a/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-nyi.cir b/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-nyi.cir index 387160db5dfde..c125c07bb9319 100644 --- a/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-nyi.cir +++ b/clang/test/CIR/Transforms/abi-lowering/indirect-non-byval-nyi.cir @@ -470,51 +470,6 @@ module attributes { args = [ { kind = "indirect", indirect_align = 8, byval = false } ] } -#passthrough = { - return = { kind = "direct" }, - args = [ ] -} - -module attributes { - dlti.dl_spec = #dlti.dl_spec< - #dlti.dl_entry<i64, dense<64>: vector<2xi64>>> -} { - - cir.func private @takes_big_non_byval(%arg0: !rec_Big) - requires_call_conv_lowering attributes { test_classify = #non_byval_arg } - - // The slot underneath the cast is forwardable, but the rewritten parameter - // is a pointer in the default address space, so the operand cannot be - // handed on as it stands. - cir.func @caller_non_byval_through_addrspace(%s: !rec_Big) - requires_call_conv_lowering attributes { test_classify = #passthrough } { - %raw = cir.alloca "raw" align(8) : !cir.ptr<!rec_Big> - %slot = cir.cast address_space %raw - : !cir.ptr<!rec_Big> -> !cir.ptr<!rec_Big, target_address_space(5)> - cir.store %s, %slot : !rec_Big, !cir.ptr<!rec_Big, target_address_space(5)> - %asload = cir.load %slot - : !cir.ptr<!rec_Big, target_address_space(5)>, !rec_Big - cir.call @takes_big_non_byval(%asload) : (!rec_Big) -> () - cir.return - } - - // CHECK: error: 'cir.call' op non-byval indirect argument that - // CHECK-SAME: does not name the caller's storage is not yet - // CHECK-SAME: implemented in CallConvLowering - // CHECK-NEXT: cir.call @takes_big_non_byval(%asload) - -} - -// ----- - -!s64i = !cir.int<s, 64> -!rec_Big = !cir.struct<"Big" {data !s64i, data !s64i, data !s64i, data !s64i}> - -#non_byval_arg = { - return = { kind = "direct" }, - args = [ { kind = "indirect", indirect_align = 8, byval = false } ] -} - #direct_ptr = { return = { kind = "direct" }, args = [ { kind = "direct" } ] _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
