[clang] [mlir] [CIR] Add support for direct with align in calling convention lowering (PR #238929)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 17:55:05 PST 2026
https://github.com/andykaylor updated https://github.com/llvm/llvm-project/pull/238929
>From 44f00030135a1b038f31da55ab4d0c380fee9079 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Wed, 4 Nov 2026 11:29:23 -0800
Subject: [PATCH 1/3] [CIR] Add support for direct with align in calling
convention lowering
This change adds the infrastructure for handling arguments that are
classified as `Direct` with a required alignment. These arguments are
passed unmodified but required an `alignstack` attribute. There are no
cases where this is needed for x86, so these changes can only be tested
via unit tests. This support will be needed when AAPCS targets are supported.
See clang/test/CodeGen/AArch64/args-hfa.c for an example of this
classification in classic codegen.
Assisted-by: Cursor / various models
---
.../Dialect/Transforms/CIRABIClassifier.cpp | 22 +++++++----
.../TargetLowering/CIRABIRewriteContext.cpp | 29 ++++++++------
.../abi-lowering/direct-alignstack.cir | 39 +++++++++++++++++++
mlir/include/mlir/ABI/ABIRewriteContext.h | 23 ++++++++---
.../mlir/ABI/Targets/Test/TestTarget.h | 3 ++
mlir/lib/ABI/Targets/Test/TestTarget.cpp | 18 +++++++--
mlir/unittests/ABI/TestTargetTest.cpp | 34 ++++++++++++++++
7 files changed, 139 insertions(+), 29 deletions(-)
create mode 100644 clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
index 0af64bf105db64..bbb275ff413006 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
@@ -532,7 +532,8 @@ static const llvm::abi::Type *mapCIRType(mlir::Type type,
/// a null coercion is its own coerced type, so the rewriter splits it into one
/// wire argument per field as classic codegen does. A coercion this bridge
/// cannot represent yields std::nullopt so the caller reports NYI rather than
-/// silently passing the value unchanged.
+/// silently passing the value unchanged. The stack-slot alignment the
+/// classifier asks for, if any, is carried in directAlign.
///
/// Extend: bool or a sub-register integer needs a signext/zeroext attribute.
/// The classifiers for the targets the bridge serves only return Extend for
@@ -552,15 +553,17 @@ convertABIArgInfo(const llvm::abi::ArgInfo &info, MLIRContext *ctx,
mlir::Type origTy) {
if (info.isDirect()) {
unsigned offset = info.getDirectOffset();
+ llvm::MaybeAlign align = info.getDirectAlign();
// The classifier names a coerce type even where it matches the natural
// type, so a non-null coerce does not by itself mean a rewrite is needed.
const llvm::abi::Type *coerceAbi = info.getCoerceToType();
if (!coerceAbi) {
auto recTy = dyn_cast_if_present<cir::RecordType>(origTy);
if (!recTy || !recTy.isStruct() || !info.getCanBeFlattened())
- return ArgClassification::getDirect();
+ return ArgClassification::getDirect(/*coerced=*/nullptr, /*offset=*/0,
+ align);
ArgClassification classified =
- ArgClassification::getDirect(recTy, /*offset=*/0);
+ ArgClassification::getDirect(recTy, /*offset=*/0, align);
classified.canFlatten = true;
return classified;
}
@@ -590,7 +593,7 @@ convertABIArgInfo(const llvm::abi::ArgInfo &info, MLIRContext *ctx,
ArgClassification classified = ArgClassification::getDirect(
cir::PointerType::get(origPtr.getPointee(),
coercedPtr.getAddrSpace()),
- /*offset=*/0);
+ /*offset=*/0, align);
classified.canFlatten = info.getCanBeFlattened();
return classified;
}
@@ -610,7 +613,8 @@ convertABIArgInfo(const llvm::abi::ArgInfo &info, MLIRContext *ctx,
// an offset cannot take this path.
if (!offset && !isAggregate && !comparesAgainstCoerce &&
!coerceIsRegisterTuple && !coerceWidensScalar)
- return ArgClassification::getDirect();
+ return ArgClassification::getDirect(/*coerced=*/nullptr, /*offset=*/0,
+ align);
mlir::Type coerced = abiTypeToCIR(coerceAbi, ctx);
if (!coerced)
return std::nullopt;
@@ -622,9 +626,10 @@ convertABIArgInfo(const llvm::abi::ArgInfo &info, MLIRContext *ctx,
// Coercing a value to the type it already has would add a memory round
// trip for nothing.
if (comparesAgainstCoerce && coerceIsNatural)
- return ArgClassification::getDirect();
+ return ArgClassification::getDirect(/*coerced=*/nullptr, /*offset=*/0,
+ align);
ArgClassification classified =
- ArgClassification::getDirect(coerced, offset);
+ ArgClassification::getDirect(coerced, offset, align);
classified.canFlatten = info.getCanBeFlattened();
return classified;
}
@@ -737,7 +742,8 @@ std::optional<FunctionClassification> CIRABIClassifierCommon::classifySignature(
// The classifier passes a transparent union as its first member, and
// names no coerce type when it passes that member as its own type.
auto recTy = dyn_cast_if_present<cir::RecordType>(origArg);
- if (ac->isPassThrough() && recTy && recordIsTransparentUnion(modOp, recTy))
+ if (ac->kind == ArgKind::Direct && !ac->coercedType && recTy &&
+ recordIsTransparentUnion(modOp, recTy))
ac->coercedType = recTy.getMembers().front();
ac->neededIntRegs = argInfo.getNeededIntRegs();
ac->neededSseRegs = argInfo.getNeededSseRegs();
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index d329938b69cdc0..d8359bd46613db 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -243,26 +243,27 @@ static bool isCoercionWiderThanMemory(mlir::Type origTy, mlir::Type coercedTy,
/// slot is dropped), Extend (needs llvm.signext / llvm.zeroext), Indirect or
/// IndirectAliased (needs the pointer attributes), Expand (changes the argument
/// count), or a coerced Direct, which may be flattened or may lose
-/// llvm.noundef.
+/// llvm.noundef, or that has a stack-slot alignment, which needs
+/// llvm.alignstack.
static bool needsArgAttrUpdate(const FunctionClassification &fc) {
return llvm::any_of(fc.argInfos, [](const ArgClassification &ac) {
return ac.kind == ArgKind::Ignore || ac.kind == ArgKind::Extend ||
ac.kind == ArgKind::Indirect ||
- ac.kind == ArgKind::IndirectAliased || ac.kind == ArgKind::Expand ||
- (ac.kind == ArgKind::Direct && ac.coercedType);
+ ac.kind == ArgKind::IndirectAliased ||ac.kind == ArgKind::Expand ||
+ (ac.kind == ArgKind::Direct && (ac.coercedType || ac.directAlign));
});
}
/// Build an updated arg_attrs ArrayAttr that drops Ignore'd args, adds
/// llvm.signext / llvm.zeroext on Extend args, adds the pointer attributes
-/// in place of llvm.nofpclass for Indirect and IndirectAliased args,
-/// drops llvm.noundef from a coerced Direct arg that passes undefined bits,
-/// gives each field of a Direct+canFlatten argument the argument's attributes,
-/// and gives each field of an Expand argument an empty dict. When
-/// \p enableNoundefAttrs is set, Extend, Indirect and IndirectAliased args are
-/// also marked llvm.noundef. Direct and Extend slots keep their other
-/// existing attributes. \p origArgTypes provides the pre-rewrite type for
-/// each arg slot.
+/// for Indirect and IndirectAlias args in place of llvm.nofpclass, drops
+/// llvm.noundef from a coerced Direct arg that passes undefined bits,
+/// adds llvm.alignstack on a Direct arg with a stack-slot alignment, gives each
+/// field of a Direct+canFlatten argument the argument's attributes, and gives
+/// each field of an Expand argument an empty dict. When \p enableNoundefAttrs
+/// is set, Extend, Indirect and IndirectAliased args are also marked
+/// llvm.noundef. Direct and Extend slots keep their other existing attributes.
+/// \p origArgTypes provides the pre-rewrite type for each arg slot.
mlir::ArrayAttr updateArgAttrs(mlir::MLIRContext *ctx, mlir::ModuleOp module,
ArrayRef<mlir::Type> origArgTypes,
mlir::ArrayAttr existingArgAttrs,
@@ -284,6 +285,12 @@ mlir::ArrayAttr updateArgAttrs(mlir::MLIRContext *ctx, mlir::ModuleOp module,
attrs.erase(mlir::LLVM::LLVMDialect::getNoUndefAttrName());
existing = attrs.getDictionary(ctx);
}
+ if (ac.kind == ArgKind::Direct && ac.directAlign) {
+ mlir::NamedAttrList attrs(existing);
+ attrs.set(mlir::LLVM::LLVMDialect::getStackAlignmentAttrName(),
+ builder.getI64IntegerAttr(ac.directAlign->value()));
+ existing = attrs.getDictionary(ctx);
+ }
if (cir::RecordType flatTy = getFlattenedCoercedType(ac)) {
// Direct + canFlatten: as in classic CodeGen, each flattened field
// carries the attributes of the argument it came from.
diff --git a/clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir b/clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir
new file mode 100644
index 00000000000000..72bcbc170c098e
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir
@@ -0,0 +1,39 @@
+// RUN: cir-opt %s -cir-call-conv-lowering="classification-attr=test_classify" \
+// RUN: | FileCheck %s
+// RUN: cir-opt %s -cir-call-conv-lowering="classification-attr=test_classify" \
+// RUN: -cir-to-llvm -o - 2>/dev/null \
+// RUN: | cir-translate -mlir-to-llvmir --allow-unregistered-dialect \
+// RUN: | FileCheck %s --check-prefix=LLVM
+
+!s32i = !cir.int<s, 32>
+
+#direct_align = {
+ return = { kind = "direct" },
+ args = [ { kind = "direct", direct_align = 8 } ]
+}
+
+module attributes {
+ dlti.dl_spec = #dlti.dl_spec<
+ #dlti.dl_entry<i32, dense<32>: vector<2xi64>>>
+} {
+
+ cir.func @takes(%arg0: !s32i)
+ attributes { test_classify = #direct_align } {
+ cir.return
+ }
+
+ // CHECK: cir.func{{.*}} @takes(%arg0: !s32i {llvm.alignstack = 8 : i64})
+
+ cir.func @caller(%arg0: !s32i)
+ attributes { test_classify = #direct_align } {
+ cir.call @takes(%arg0) : (!s32i) -> ()
+ cir.return
+ }
+
+ // CHECK: cir.call @takes(%arg0) : (!s32i {llvm.alignstack = 8 : i64}) -> ()
+
+}
+
+// LLVM: define void @takes(i32 alignstack(8) %{{.+}})
+// LLVM: define void @caller(i32 alignstack(8) %{{.+}})
+// LLVM: call void @takes(i32 alignstack(8) %
diff --git a/mlir/include/mlir/ABI/ABIRewriteContext.h b/mlir/include/mlir/ABI/ABIRewriteContext.h
index 9c8d5f3f6e3466..e39e0c0d12a98d 100644
--- a/mlir/include/mlir/ABI/ABIRewriteContext.h
+++ b/mlir/include/mlir/ABI/ABIRewriteContext.h
@@ -90,6 +90,10 @@ struct ArgClassification {
/// NO_CLASS and the value is carried in a later eightbyte (x86-64 SysV).
unsigned directOffset = 0;
+ /// For Direct: the alignment of the argument's stack slot when the value
+ /// is passed in memory, if the ABI sets one apart from the type's.
+ llvm::MaybeAlign directAlign;
+
/// How many integer and vector argument registers the value occupies. Both
/// zero means it travels in memory, which is also what a target whose
/// classifier does not record the demand reports.
@@ -97,10 +101,14 @@ struct ArgClassification {
unsigned neededSseRegs = 0;
/// Whether the value is passed as-is, so a rewriter can leave it alone.
- /// Only an uncoerced Direct qualifies. Extend counts as needing a rewrite
- /// even though it only adds an attribute, because the attribute changes
- /// observable behavior.
- bool isPassThrough() const { return kind == ArgKind::Direct && !coercedType; }
+ /// Only an uncoerced Direct with no stack-slot alignment qualifies.
+ /// Extend counts as needing a rewrite even though it only adds an
+ /// attribute, because the attribute changes observable behavior, and a
+ /// Direct with a stack-slot alignment does too because it needs
+ /// llvm.alignstack.
+ bool isPassThrough() const {
+ return kind == ArgKind::Direct && !coercedType && !directAlign;
+ }
/// Whether two classifications describe the same wire format. Every field
/// participates, so a field added above must be added here as well.
@@ -111,6 +119,7 @@ struct ArgClassification {
byVal == other.byVal &&
indirectAddrSpace == other.indirectAddrSpace &&
directOffset == other.directOffset &&
+ directAlign == other.directAlign &&
neededIntRegs == other.neededIntRegs &&
neededSseRegs == other.neededSseRegs;
}
@@ -119,8 +128,9 @@ struct ArgClassification {
return getDirect(/*coerced=*/nullptr, /*offset=*/0);
}
- static ArgClassification getDirect(Type coerced, unsigned offset) {
- // isPassThrough reads only coercedType, so an offset with no coerced
+ static ArgClassification getDirect(Type coerced, unsigned offset,
+ llvm::MaybeAlign align = std::nullopt) {
+ // isPassThrough ignores directOffset, so an offset with no coerced
// type to read at it would be silently ignored.
assert((!offset || coerced) &&
"a direct offset needs a coerced type to read at it");
@@ -128,6 +138,7 @@ struct ArgClassification {
c.kind = ArgKind::Direct;
c.coercedType = coerced;
c.directOffset = offset;
+ c.directAlign = align;
return c;
}
diff --git a/mlir/include/mlir/ABI/Targets/Test/TestTarget.h b/mlir/include/mlir/ABI/Targets/Test/TestTarget.h
index f1e637fc1f1c6b..3898073f48b38a 100644
--- a/mlir/include/mlir/ABI/Targets/Test/TestTarget.h
+++ b/mlir/include/mlir/ABI/Targets/Test/TestTarget.h
@@ -65,6 +65,9 @@ FunctionClassification classify(TypeRange argTypes, Type returnType,
/// coerced_type: TypeAttr. ABI-coerced type, if different from the
/// original.
/// can_flatten: BoolAttr. Defaults to true.
+/// direct_align: IntegerAttr. Stack-slot alignment in bytes when the
+/// ABI sets one apart from the type's. When set, the
+/// rewriter emits llvm.alignstack.
///
/// For kind = "extend" (coerced_type required, sign_extend optional):
/// coerced_type: TypeAttr. Required; the extended integer type.
diff --git a/mlir/lib/ABI/Targets/Test/TestTarget.cpp b/mlir/lib/ABI/Targets/Test/TestTarget.cpp
index 4942397b046a8d..fad798b94f2109 100644
--- a/mlir/lib/ABI/Targets/Test/TestTarget.cpp
+++ b/mlir/lib/ABI/Targets/Test/TestTarget.cpp
@@ -125,8 +125,8 @@ namespace {
/// set causes a parse error (no silent ignore). Updated when new
/// optional keys are added to the schema.
constexpr StringRef knownArgKeys[] = {
- "kind", "coerced_type", "sign_extend", "can_flatten",
- "indirect_align", "byval", "indirect_addr_space",
+ "kind", "coerced_type", "sign_extend", "can_flatten",
+ "direct_align", "indirect_align", "byval", "indirect_addr_space",
};
bool isKnownArgKey(StringRef key) {
@@ -151,7 +151,8 @@ parseOne(DictionaryAttr argDict, function_ref<InFlightDiagnostic()> emitError) {
emitError() << "unknown key '" << na.getName().getValue()
<< "' in classification dictionary; allowed keys are "
<< "kind, coerced_type, sign_extend, can_flatten, "
- << "indirect_align, byval, indirect_addr_space";
+ << "direct_align, indirect_align, byval, "
+ << "indirect_addr_space";
return std::nullopt;
}
@@ -161,7 +162,16 @@ parseOne(DictionaryAttr argDict, function_ref<InFlightDiagnostic()> emitError) {
Type coerced;
if (auto t = argDict.getAs<TypeAttr>("coerced_type"))
coerced = t.getValue();
- auto c = ArgClassification::getDirect(coerced, /*offset=*/0);
+ llvm::MaybeAlign align;
+ if (auto da = argDict.getAs<IntegerAttr>("direct_align")) {
+ if (da.getInt() <= 0 || !llvm::isPowerOf2_64(da.getInt())) {
+ emitError() << "'direct_align' must be a positive power of 2; got "
+ << da.getInt();
+ return std::nullopt;
+ }
+ align = llvm::Align(da.getInt());
+ }
+ auto c = ArgClassification::getDirect(coerced, /*offset=*/0, align);
if (auto cf = argDict.getAs<BoolAttr>("can_flatten"))
c.canFlatten = cf.getValue();
return c;
diff --git a/mlir/unittests/ABI/TestTargetTest.cpp b/mlir/unittests/ABI/TestTargetTest.cpp
index 5fd77f09455084..bb81255a7946a9 100644
--- a/mlir/unittests/ABI/TestTargetTest.cpp
+++ b/mlir/unittests/ABI/TestTargetTest.cpp
@@ -148,6 +148,40 @@ class TestTargetParseTest : public ::testing::Test {
std::string lastError;
};
+TEST_F(TestTargetParseTest, ParsesDirectAlign) {
+ auto aligned = makeArg({
+ builder.getNamedAttr("kind", builder.getStringAttr("direct")),
+ builder.getNamedAttr("direct_align", builder.getI64IntegerAttr(8)),
+ });
+ auto direct =
+ makeArg({builder.getNamedAttr("kind", builder.getStringAttr("direct"))});
+ auto attr = builder.getDictionaryAttr({
+ builder.getNamedAttr("return", direct),
+ builder.getNamedAttr("args", builder.getArrayAttr({aligned})),
+ });
+
+ auto fc = parseOk(attr);
+ ASSERT_EQ(fc.argInfos.size(), 1u);
+ EXPECT_EQ(fc.argInfos[0].kind, ArgKind::Direct);
+ ASSERT_TRUE(fc.argInfos[0].directAlign);
+ EXPECT_EQ(*fc.argInfos[0].directAlign, llvm::Align(8));
+ EXPECT_FALSE(fc.returnInfo.directAlign);
+}
+
+TEST_F(TestTargetParseTest, RejectsDirectWithNonPowerOfTwoAlign) {
+ auto bad = makeArg({
+ builder.getNamedAttr("kind", builder.getStringAttr("direct")),
+ builder.getNamedAttr("direct_align", builder.getI64IntegerAttr(7)),
+ });
+ auto direct =
+ makeArg({builder.getNamedAttr("kind", builder.getStringAttr("direct"))});
+ auto attr = builder.getDictionaryAttr({
+ builder.getNamedAttr("return", direct),
+ builder.getNamedAttr("args", builder.getArrayAttr({bad})),
+ });
+ parseError(attr, "'direct_align' must be a positive power of 2");
+}
+
TEST_F(TestTargetParseTest, ParsesDirectReturnAndOneDirectArg) {
auto direct =
makeArg({builder.getNamedAttr("kind", builder.getStringAttr("direct"))});
>From a30b4177e70658bc628804815f51e380c19d6e7c Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Wed, 4 Nov 2026 14:15:09 -0800
Subject: [PATCH 2/3] Fix formatting
---
.../Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index d8359bd46613db..b91d4a214ddffb 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -249,7 +249,7 @@ static bool needsArgAttrUpdate(const FunctionClassification &fc) {
return llvm::any_of(fc.argInfos, [](const ArgClassification &ac) {
return ac.kind == ArgKind::Ignore || ac.kind == ArgKind::Extend ||
ac.kind == ArgKind::Indirect ||
- ac.kind == ArgKind::IndirectAliased ||ac.kind == ArgKind::Expand ||
+ ac.kind == ArgKind::IndirectAliased || ac.kind == ArgKind::Expand ||
(ac.kind == ArgKind::Direct && (ac.coercedType || ac.directAlign));
});
}
>From 1a0650fd08e068c7377e50c2a7fcf1ff037f3652 Mon Sep 17 00:00:00 2001
From: Andy Kaylor <akaylor at nvidia.com>
Date: Thu, 5 Nov 2026 17:51:30 -0800
Subject: [PATCH 3/3] Fix direct align test
---
clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir b/clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir
index 72bcbc170c098e..cbf70e233a7797 100644
--- a/clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir
+++ b/clang/test/CIR/Transforms/abi-lowering/direct-alignstack.cir
@@ -17,14 +17,14 @@ module attributes {
#dlti.dl_entry<i32, dense<32>: vector<2xi64>>>
} {
- cir.func @takes(%arg0: !s32i)
+ cir.func @takes(%arg0: !s32i) requires_call_conv_lowering
attributes { test_classify = #direct_align } {
cir.return
}
// CHECK: cir.func{{.*}} @takes(%arg0: !s32i {llvm.alignstack = 8 : i64})
- cir.func @caller(%arg0: !s32i)
+ cir.func @caller(%arg0: !s32i) requires_call_conv_lowering
attributes { test_classify = #direct_align } {
cir.call @takes(%arg0) : (!s32i) -> ()
cir.return
More information about the cfe-commits
mailing list