[Mlir-commits] [mlir] [MLIR][LLVMIR] Preserve byval alignment in memcpy after inlining (PR #185433)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Mar 9 07:55:36 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-llvm
Author: Berke Ates (Berke-Ates)
<details>
<summary>Changes</summary>
This PR adds alignment attributes to the generated memcpy intrinsics after inlining functions with byval arguments.
---
Full diff: https://github.com/llvm/llvm-project/pull/185433.diff
2 Files Affected:
- (modified) mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp (+12-1)
- (modified) mlir/test/Dialect/LLVMIR/inlining.mlir (+20)
``````````diff
diff --git a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
index b61138ad4678b..c9b6359ecc433 100644
--- a/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
@@ -623,8 +623,19 @@ static Value handleByValArgumentInit(OpBuilder &builder, Location loc,
Value copySize =
LLVM::ConstantOp::create(builder, loc, builder.getI64Type(),
builder.getI64IntegerAttr(elementTypeSize));
+ // Preserve the alignment of the destination (alloca) in the memcpy's
+ // arg_attrs.
+ MLIRContext *ctx = builder.getContext();
+ NamedAttribute dstAlignAttr =
+ builder.getNamedAttr(LLVM::LLVMDialect::getAlignAttrName(),
+ builder.getI64IntegerAttr(targetAlignment));
+ ArrayAttr argAttrs =
+ builder.getArrayAttr({DictionaryAttr::get(ctx, {dstAlignAttr})});
LLVM::MemcpyOp::create(builder, loc, allocaOp, argument, copySize,
- /*isVolatile=*/false);
+ /*isVolatile=*/false,
+ /*access_groups=*/nullptr, /*alias_scopes=*/nullptr,
+ /*noalias_scopes=*/nullptr, /*tbaa=*/nullptr, argAttrs,
+ /*res_attrs=*/nullptr);
return allocaOp;
}
diff --git a/mlir/test/Dialect/LLVMIR/inlining.mlir b/mlir/test/Dialect/LLVMIR/inlining.mlir
index cc3600af431ea..e2c60da47e180 100644
--- a/mlir/test/Dialect/LLVMIR/inlining.mlir
+++ b/mlir/test/Dialect/LLVMIR/inlining.mlir
@@ -570,6 +570,26 @@ llvm.func @test_byval_global() {
// -----
+// Check that alignment information is preserved in the memcpy when inlining
+// byval arguments.
+
+llvm.func @byval_aligned_arg(%ptr : !llvm.ptr { llvm.byval = i32, llvm.align = 16 }) {
+ llvm.return
+}
+
+// CHECK-LABEL: llvm.func @test_byval_memcpy_alignment
+// CHECK-SAME: %[[PTR:[a-zA-Z0-9_]+]]: !llvm.ptr
+llvm.func @test_byval_memcpy_alignment(%ptr : !llvm.ptr) {
+ // Verify the memcpy carries the alignment info from the byval attribute.
+ // CHECK: %[[ALLOCA:.+]] = llvm.alloca{{.+}}alignment = 16
+ // CHECK: "llvm.intr.memcpy"(%[[ALLOCA]], %[[PTR]]
+ // CHECK-SAME: {llvm.align = 16 : i64}
+ llvm.call @byval_aligned_arg(%ptr) : (!llvm.ptr) -> ()
+ llvm.return
+}
+
+// -----
+
// Check that inlining does not hoist byval allocas out of automatic allocation
// scopes, such as parallel forall regions. Each parallel iteration must have
// its own private copy of the byval argument.
``````````
</details>
https://github.com/llvm/llvm-project/pull/185433
More information about the Mlir-commits
mailing list