[clang] [CIR] Use allocation size for argument dereferenceable attribute (PR #235138)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 15:02:48 PST 2026
https://github.com/andykaylor updated https://github.com/llvm/llvm-project/pull/235138
>From be9ec30405f58fc5578eceb69673b2da2c9d914d Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Thu, 22 Oct 2026 15:26:52 -0700
Subject: [PATCH 1/2] [CIR] Use allocation size for argument dereferenceable
attribute
This updates the CIR rewrite context to use an argument's allocation
size rather than type size when setting the dereferenceable attribute
for indirect arguments. There are no cases for x86 where these sizes
will be different, so I don't have a source-to-ir test for this. It
will be needed for AArch64 lowering, and it works in the new form for
other targets, so I'm making the change now.
Assisted-by: Cursor / various models
---
.../TargetLowering/CIRABIRewriteContext.cpp | 7 +++++--
.../abi-lowering/byval-sret-arg-attr-lowering.cir | 15 +++++++++++++++
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index c85f5421c21050..ed9fde6ad1b1e0 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -293,9 +293,12 @@ mlir::ArrayAttr updateArgAttrs(mlir::MLIRContext *ctx, mlir::ModuleOp module,
attrs.set(mlir::LLVM::LLVMDialect::getDeadOnReturnAttrName(),
builder.getI64IntegerAttr(static_cast<int64_t>(
llvm::DeadOnReturnInfo().toIntValue())));
+ // The object spans its allocation size, which is larger than its
+ // size for a vector whose size is not a power of two.
attrs.set(mlir::LLVM::LLVMDialect::getDereferenceableAttrName(),
- builder.getI64IntegerAttr(
- dl.getTypeSize(pointeeTy).getFixedValue()));
+ builder.getI64IntegerAttr(cir::CIRDataLayout(module)
+ .getTypeAllocSize(pointeeTy)
+ .getFixedValue()));
}
newArgAttrs.push_back(attrs.getDictionary(ctx));
} else {
diff --git a/clang/test/CIR/Transforms/abi-lowering/byval-sret-arg-attr-lowering.cir b/clang/test/CIR/Transforms/abi-lowering/byval-sret-arg-attr-lowering.cir
index 85c0660db03333..8c51f072294962 100644
--- a/clang/test/CIR/Transforms/abi-lowering/byval-sret-arg-attr-lowering.cir
+++ b/clang/test/CIR/Transforms/abi-lowering/byval-sret-arg-attr-lowering.cir
@@ -5,6 +5,7 @@
// RUN: | cir-translate -mlir-to-llvmir --allow-unregistered-dialect \
// RUN: | FileCheck %s --check-prefix=LLVM
+!s32i = !cir.int<s, 32>
!s64i = !cir.int<s, 64>
!rec_Big = !cir.struct<"Big" {data !s64i, data !s64i, data !s64i}>
@@ -23,6 +24,11 @@
args = [ { kind = "indirect", indirect_align = 8, byval = false } ]
}
+#non_byval_vec_arg = {
+ return = { kind = "direct" },
+ args = [ { kind = "indirect", indirect_align = 16, byval = false } ]
+}
+
#passthrough = {
return = { kind = "direct" },
args = [ ]
@@ -64,6 +70,15 @@ module attributes {
// CHECK: cir.func{{.*}} @takes_non_byval(%{{.*}}: !cir.ptr<!rec_Big> {{{.*}}llvm.dead_on_return = -1 : i64, llvm.dereferenceable = 24 : i64, llvm.nofreeobj, llvm.noundef{{.*}}})
// LLVM: define void @takes_non_byval(ptr nofreeobj noundef align 8 dead_on_return dereferenceable(24) %{{.+}})
+ // A three-element vector holds 12 bytes, and its allocation is padded to 16.
+ cir.func @takes_vec3_non_byval(%arg0: !cir.vector<3 x !s32i>)
+ attributes { test_classify = #non_byval_vec_arg } {
+ cir.return
+ }
+
+ // CHECK: cir.func{{.*}} @takes_vec3_non_byval(%{{.*}}: !cir.ptr<!cir.vector<3 x !s32i>> {{{.*}}llvm.dereferenceable = 16 : i64{{.*}}})
+ // LLVM: define void @takes_vec3_non_byval(ptr nofreeobj noundef align 16 dead_on_return dereferenceable(16) %{{.+}})
+
// The call site's byval operand attribute is converted too.
cir.func @caller(%s: !rec_Big) attributes { test_classify = #passthrough } {
cir.call @takes_big(%s) : (!rec_Big) -> ()
>From 969a5048c94293419f5a366899bbe870a5e08d38 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Tue, 3 Nov 2026 15:01:48 -0800
Subject: [PATCH 2/2] Address review comments
---
.../Transforms/TargetLowering/CIRABIRewriteContext.cpp | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index ed9fde6ad1b1e0..0638e5a03efa8e 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -293,12 +293,9 @@ mlir::ArrayAttr updateArgAttrs(mlir::MLIRContext *ctx, mlir::ModuleOp module,
attrs.set(mlir::LLVM::LLVMDialect::getDeadOnReturnAttrName(),
builder.getI64IntegerAttr(static_cast<int64_t>(
llvm::DeadOnReturnInfo().toIntValue())));
- // The object spans its allocation size, which is larger than its
- // size for a vector whose size is not a power of two.
attrs.set(mlir::LLVM::LLVMDialect::getDereferenceableAttrName(),
- builder.getI64IntegerAttr(cir::CIRDataLayout(module)
- .getTypeAllocSize(pointeeTy)
- .getFixedValue()));
+ builder.getI64IntegerAttr(
+ dl.getTypeAllocSize(pointeeTy).getFixedValue()));
}
newArgAttrs.push_back(attrs.getDictionary(ctx));
} else {
More information about the cfe-commits
mailing list