[llvm] [ABI] Add DefaultTargetInfo for default argument/return classification (PR #225009)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 03:18:06 PDT 2026


https://github.com/skc7 updated https://github.com/llvm/llvm-project/pull/225009

>From fc5e28c472d09ca23f4509081412fbe772fb48ba Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Mon, 21 Sep 2026 11:45:48 +0530
Subject: [PATCH 1/2] [ABI] Add DefaultTargetInfo for default argument/return
 classification

---
 llvm/include/llvm/ABI/DefaultTargetInfo.h |  38 ++++++
 llvm/include/llvm/ABI/TargetInfo.h        |   9 ++
 llvm/lib/ABI/CMakeLists.txt               |   1 +
 llvm/lib/ABI/DefaultTargetInfo.cpp        |  63 +++++++++
 llvm/lib/ABI/TargetInfo.cpp               |   4 +
 llvm/unittests/ABI/TargetInfoTest.cpp     | 156 ++++++++++++++++++++--
 6 files changed, 262 insertions(+), 9 deletions(-)
 create mode 100644 llvm/include/llvm/ABI/DefaultTargetInfo.h
 create mode 100644 llvm/lib/ABI/DefaultTargetInfo.cpp

diff --git a/llvm/include/llvm/ABI/DefaultTargetInfo.h b/llvm/include/llvm/ABI/DefaultTargetInfo.h
new file mode 100644
index 00000000000000..1df4191aca43cb
--- /dev/null
+++ b/llvm/include/llvm/ABI/DefaultTargetInfo.h
@@ -0,0 +1,38 @@
+//===----- DefaultTargetInfo.h - Default ABI classification ------- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Default ABI classification shared by targets without special rules. This is
+/// the llvm::abi analogue of clang::CodeGen::DefaultABIInfo: targets delegate
+/// to it for the cases they do not handle themselves.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ABI_DEFAULTTARGETINFO_H
+#define LLVM_ABI_DEFAULTTARGETINFO_H
+
+#include "llvm/ABI/TargetInfo.h"
+
+namespace llvm {
+namespace abi {
+
+/// Self-consistent classification that conforms to no particular ABI.
+class DefaultTargetInfo : public TargetInfo {
+public:
+  using TargetInfo::TargetInfo;
+
+  LLVM_ABI ArgInfo classifyArgumentType(const Type *Ty) const;
+  LLVM_ABI ArgInfo classifyReturnType(const Type *RetTy) const;
+
+  LLVM_ABI void computeInfo(FunctionInfo &FI) const override;
+};
+
+} // namespace abi
+} // namespace llvm
+
+#endif // LLVM_ABI_DEFAULTTARGETINFO_H
diff --git a/llvm/include/llvm/ABI/TargetInfo.h b/llvm/include/llvm/ABI/TargetInfo.h
index b9dd3087bfaa84..edf9ecd0e6ee25 100644
--- a/llvm/include/llvm/ABI/TargetInfo.h
+++ b/llvm/include/llvm/ABI/TargetInfo.h
@@ -84,10 +84,19 @@ class TargetInfo {
   /// alloca/stack space).
   virtual unsigned getAllocaAddrSpace() const { return 0; }
 
+  /// Whether the target has a 128-bit integer type. Default true.
+  virtual bool hasInt128Type() const { return true; }
+
+  /// Width of `long long` in bits. Default 64.
+  virtual unsigned getLongLongWidth() const { return 64; }
+
 protected:
   LLVM_ABI RecordArgABI getRecordArgABI(const RecordType *RT) const;
   LLVM_ABI RecordArgABI getRecordArgABI(const Type *Ty) const;
   LLVM_ABI bool isPromotableInteger(const IntegerType *IT) const;
+
+  /// Bit width above which a _BitInt cannot stay in registers.
+  LLVM_ABI unsigned getBitIntRegThreshold() const;
   LLVM_ABI ArgInfo getNaturalAlignIndirect(const Type *Ty, unsigned AddrSpace,
                                            bool ByVal = true) const;
   LLVM_ABI bool isAggregateTypeForABI(const Type *Ty) const;
diff --git a/llvm/lib/ABI/CMakeLists.txt b/llvm/lib/ABI/CMakeLists.txt
index 39e725ca9fd3b7..831ea0715f7c0d 100644
--- a/llvm/lib/ABI/CMakeLists.txt
+++ b/llvm/lib/ABI/CMakeLists.txt
@@ -2,6 +2,7 @@ add_llvm_component_library(LLVMABI
   Types.cpp
   FunctionInfo.cpp
   TargetInfo.cpp
+  DefaultTargetInfo.cpp
   IRTypeMapper.cpp
   Targets/AArch64.cpp
   Targets/BPF.cpp
diff --git a/llvm/lib/ABI/DefaultTargetInfo.cpp b/llvm/lib/ABI/DefaultTargetInfo.cpp
new file mode 100644
index 00000000000000..5aad4393d8b1ae
--- /dev/null
+++ b/llvm/lib/ABI/DefaultTargetInfo.cpp
@@ -0,0 +1,63 @@
+//===- DefaultTargetInfo.cpp - Default ABI classification -----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ABI/DefaultTargetInfo.h"
+#include "llvm/Support/Casting.h"
+
+using namespace llvm::abi;
+using llvm::dyn_cast;
+
+ArgInfo DefaultTargetInfo::classifyArgumentType(const Type *Ty) const {
+  Ty = useFirstFieldIfTransparentUnion(Ty);
+
+  if (isAggregateTypeForABI(Ty)) {
+    // Records with non-trivial destructors/copy-constructors should not be
+    // passed by value.
+    if (RecordArgABI RAA = getRecordArgABI(Ty))
+      return getNaturalAlignIndirect(Ty, getAllocaAddrSpace(),
+                                     /*ByVal=*/RAA == RAA_DirectInMemory);
+    return getNaturalAlignIndirect(Ty, getAllocaAddrSpace());
+  }
+
+  if (const auto *IT = dyn_cast<IntegerType>(Ty)) {
+    // A _BitInt too wide for the largest integer register goes indirect.
+    if (IT->isBitInt() &&
+        IT->getSizeInBits().getFixedValue() > getBitIntRegThreshold())
+      return getNaturalAlignIndirect(Ty, getAllocaAddrSpace());
+    if (isPromotableInteger(IT))
+      return ArgInfo::getExtend(Ty);
+  }
+
+  return ArgInfo::getDirect();
+}
+
+ArgInfo DefaultTargetInfo::classifyReturnType(const Type *RetTy) const {
+  if (RetTy->isVoid())
+    return ArgInfo::getIgnore();
+
+  if (isAggregateTypeForABI(RetTy))
+    return getNaturalAlignIndirect(RetTy, getAllocaAddrSpace());
+
+  if (const auto *IT = dyn_cast<IntegerType>(RetTy)) {
+    // A _BitInt too wide for the largest integer register goes indirect.
+    if (IT->isBitInt() &&
+        IT->getSizeInBits().getFixedValue() > getBitIntRegThreshold())
+      return getNaturalAlignIndirect(RetTy, getAllocaAddrSpace());
+    if (isPromotableInteger(IT))
+      return ArgInfo::getExtend(RetTy);
+  }
+
+  return ArgInfo::getDirect();
+}
+
+void DefaultTargetInfo::computeInfo(FunctionInfo &FI) const {
+  if (!maybeCommonClassifyReturnType(FI))
+    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
+  for (auto &I : FI.arguments())
+    I.Info = classifyArgumentType(I.ABIType);
+}
diff --git a/llvm/lib/ABI/TargetInfo.cpp b/llvm/lib/ABI/TargetInfo.cpp
index 1e03d98880734f..888f7201c19457 100644
--- a/llvm/lib/ABI/TargetInfo.cpp
+++ b/llvm/lib/ABI/TargetInfo.cpp
@@ -41,6 +41,10 @@ bool TargetInfo::isPromotableInteger(const IntegerType *IT) const {
   return BitWidth < 32;
 }
 
+unsigned TargetInfo::getBitIntRegThreshold() const {
+  return hasInt128Type() ? 128 : getLongLongWidth();
+}
+
 ArgInfo TargetInfo::getNaturalAlignIndirect(const Type *Ty, unsigned AddrSpace,
                                             bool ByVal) const {
   return ArgInfo::getIndirect(Ty->getAlignment(), ByVal, AddrSpace);
diff --git a/llvm/unittests/ABI/TargetInfoTest.cpp b/llvm/unittests/ABI/TargetInfoTest.cpp
index e3a03e3b2dfb3f..a165303cdaec46 100644
--- a/llvm/unittests/ABI/TargetInfoTest.cpp
+++ b/llvm/unittests/ABI/TargetInfoTest.cpp
@@ -6,13 +6,15 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// Tests for target-independent helpers on the TargetInfo base class. These are
-// exercised through a minimal concrete target so any new shared default lands
-// here rather than in a per-target test file.
+// Tests for target-independent helpers on TargetInfo and the shared default
+// classification in DefaultTargetInfo. These are exercised through a minimal
+// concrete target so any new shared default lands here rather than in a
+// per-target test file.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ABI/TargetInfo.h"
+#include "llvm/ABI/DefaultTargetInfo.h"
 #include "llvm/ABI/FunctionInfo.h"
 #include "llvm/ABI/Types.h"
 #include "llvm/ADT/APFloat.h"
@@ -28,6 +30,7 @@ using namespace llvm;
 
 using ABIType = llvm::abi::Type;
 using llvm::abi::ArgInfo;
+using llvm::abi::DefaultTargetInfo;
 using llvm::abi::FieldInfo;
 using llvm::abi::FunctionInfo;
 using llvm::abi::RecordFlags;
@@ -35,12 +38,12 @@ using llvm::abi::StructPacking;
 using llvm::abi::TargetInfo;
 using llvm::abi::TypeBuilder;
 
-// A minimal concrete target that re-exposes the shared, protected helpers so
-// they can be exercised directly, independent of any real target.
-class TestTargetInfo : public TargetInfo {
+// A minimal concrete target that inherits the shared default classifiers so
+// they can be exercised directly, independent of any real target. Only
+// getABICompatInfo() is left to supply.
+class TestTargetInfo : public DefaultTargetInfo {
 public:
-  explicit TestTargetInfo(TypeBuilder &Builder) : TargetInfo(Builder) {}
-  void computeInfo(FunctionInfo &) const override {}
+  explicit TestTargetInfo(TypeBuilder &Builder) : DefaultTargetInfo(Builder) {}
   const llvm::abi::ABICompatInfo &getABICompatInfo() const override {
     return Compat;
   }
@@ -67,6 +70,14 @@ class AllocaAS5TargetInfo : public TargetInfo {
   llvm::abi::ABICompatInfo Compat;
 };
 
+// A target lacking a 128-bit integer type, so the _BitInt register threshold
+// is the width of `long long`.
+class NoInt128TargetInfo : public TestTargetInfo {
+public:
+  using TestTargetInfo::TestTargetInfo;
+  bool hasInt128Type() const override { return false; }
+};
+
 class TargetInfoTest : public ::testing::Test {
 protected:
   llvm::BumpPtrAllocator Alloc;
@@ -74,17 +85,58 @@ class TargetInfoTest : public ::testing::Test {
   const ABIType *I16;
   const ABIType *I32;
   const ABIType *F32;
+  const ABIType *Void;
   /// An empty class: a record with no fields, one byte wide.
   const ABIType *Empty;
+  /// A _BitInt wider than 128 bits, which cannot be passed in registers.
+  const ABIType *WideBitInt;
+  /// A _BitInt between `long long` (64) and 128 bits wide.
+  const ABIType *MidBitInt;
 
   TargetInfoTest()
       : TB(Alloc), I16(TB.getIntegerType(16, llvm::Align(2), /*Signed=*/true)),
         I32(TB.getIntegerType(32, llvm::Align(4), /*Signed=*/true)),
         F32(TB.getFloatType(llvm::APFloat::IEEEsingle(), llvm::Align(4))),
+        Void(TB.getVoidType()),
         Empty(TB.getRecordType({}, llvm::TypeSize::getFixed(8), llvm::Align(1),
                                /*UnadjustedAlign=*/llvm::Align(1),
                                StructPacking::Default, {}, {},
-                               RecordFlags::CanPassInRegisters)) {}
+                               RecordFlags::CanPassInRegisters)),
+        WideBitInt(TB.getIntegerType(129, llvm::Align(8), /*Signed=*/true,
+                                     /*IsBitInt=*/true)),
+        MidBitInt(TB.getIntegerType(100, llvm::Align(8), /*Signed=*/true,
+                                    /*IsBitInt=*/true)) {}
+
+  /// A record with a single int field, passable in registers.
+  const ABIType *recordInReg() {
+    return TB.getRecordType({FieldInfo(I32, 0)}, llvm::TypeSize::getFixed(32),
+                            llvm::Align(4), /*UnadjustedAlign=*/llvm::Align(4),
+                            StructPacking::Default, {}, {},
+                            RecordFlags::CanPassInRegisters);
+  }
+
+  /// The same record marked as unable to pass in registers, e.g. a non-trivial
+  /// C++ type. This takes the RAA_Indirect path.
+  const ABIType *recordInMemory() {
+    return TB.getRecordType({FieldInfo(I32, 0)}, llvm::TypeSize::getFixed(32),
+                            llvm::Align(4), /*UnadjustedAlign=*/llvm::Align(4),
+                            StructPacking::Default, {}, {}, RecordFlags::None);
+  }
+
+  ArgInfo classifyArg(const ABIType *Ty) {
+    TestTargetInfo TI(TB);
+    return TI.classifyArgumentType(Ty);
+  }
+
+  ArgInfo classifyRet(const ABIType *Ty) {
+    TestTargetInfo TI(TB);
+    return TI.classifyReturnType(Ty);
+  }
+
+  ArgInfo classifyArgNoInt128(const ABIType *Ty) {
+    NoInt128TargetInfo TI(TB);
+    return TI.classifyArgumentType(Ty);
+  }
 
   const ABIType *recordOf(llvm::ArrayRef<FieldInfo> Fields, uint64_t SizeInBits,
                           llvm::Align Alignment) {
@@ -101,6 +153,92 @@ class TargetInfoTest : public ::testing::Test {
   }
 };
 
+// --- Argument classification -------------------------------------------------
+
+// A word-sized integer is passed directly.
+TEST_F(TargetInfoTest, DefaultArgIntIsDirect) {
+  ArgInfo Info = classifyArg(I32);
+  EXPECT_TRUE(Info.isDirect());
+}
+
+// A sub-word integer is promoted.
+TEST_F(TargetInfoTest, DefaultArgSmallIntIsExtended) {
+  EXPECT_TRUE(classifyArg(I16).isExtend());
+}
+
+// A record that fits in registers is passed indirectly by value.
+TEST_F(TargetInfoTest, DefaultArgRecordInRegIsIndirectByVal) {
+  ArgInfo Info = classifyArg(recordInReg());
+  ASSERT_TRUE(Info.isIndirect());
+  EXPECT_TRUE(Info.getIndirectByVal());
+}
+
+// A record that cannot pass in registers goes indirect without ByVal.
+TEST_F(TargetInfoTest, DefaultArgRecordInMemoryIsIndirectNoByVal) {
+  ArgInfo Info = classifyArg(recordInMemory());
+  ASSERT_TRUE(Info.isIndirect());
+  EXPECT_FALSE(Info.getIndirectByVal());
+}
+
+// A _BitInt wider than 128 bits is passed indirectly.
+TEST_F(TargetInfoTest, DefaultArgWideBitIntIsIndirect) {
+  EXPECT_TRUE(classifyArg(WideBitInt).isIndirect());
+}
+
+// With a 128-bit integer type, a _BitInt no wider than 128 stays direct.
+TEST_F(TargetInfoTest, DefaultArgMidBitIntIsDirectWithInt128) {
+  EXPECT_TRUE(classifyArg(MidBitInt).isDirect());
+}
+
+// Without a 128-bit integer type the threshold drops to long long (64), so the
+// same _BitInt is passed indirectly.
+TEST_F(TargetInfoTest, DefaultArgMidBitIntIsIndirectWithoutInt128) {
+  EXPECT_TRUE(classifyArgNoInt128(MidBitInt).isIndirect());
+}
+
+// A transparent union is classified as its first field, so a union of one int
+// is passed directly rather than as an aggregate.
+TEST_F(TargetInfoTest, DefaultArgTransparentUnionUsesFirstField) {
+  const ABIType *U = TB.getUnionType(
+      {FieldInfo(I32, 0)}, llvm::TypeSize::getFixed(32), llvm::Align(4),
+      /*UnadjustedAlign=*/llvm::Align(4), StructPacking::Default,
+      RecordFlags::IsTransparent | RecordFlags::CanPassInRegisters);
+  EXPECT_TRUE(classifyArg(U).isDirect());
+}
+
+// --- Return classification ---------------------------------------------------
+
+// Void returns are ignored.
+TEST_F(TargetInfoTest, DefaultReturnVoidIsIgnored) {
+  EXPECT_TRUE(classifyRet(Void).isIgnore());
+}
+
+// A word-sized integer is returned directly.
+TEST_F(TargetInfoTest, DefaultReturnIntIsDirect) {
+  EXPECT_TRUE(classifyRet(I32).isDirect());
+}
+
+// A sub-word integer is promoted on return.
+TEST_F(TargetInfoTest, DefaultReturnSmallIntIsExtended) {
+  EXPECT_TRUE(classifyRet(I16).isExtend());
+}
+
+// An aggregate is returned indirectly, with ByVal=true.
+TEST_F(TargetInfoTest, DefaultReturnRecordIsIndirect) {
+  ArgInfo Info = classifyRet(recordInReg());
+  ASSERT_TRUE(Info.isIndirect());
+  EXPECT_TRUE(Info.getIndirectByVal());
+}
+
+// A _BitInt wider than 128 bits is returned indirectly.
+TEST_F(TargetInfoTest, DefaultReturnWideBitIntIsIndirect) {
+  ArgInfo Info = classifyRet(WideBitInt);
+  ASSERT_TRUE(Info.isIndirect());
+  EXPECT_TRUE(Info.getIndirectByVal());
+}
+
+// --- Single-element struct reduction -----------------------------------------
+
 // The shared single-element-struct reduction, used by getByteVectorType and by
 // other targets, looks through single-element wrappers to the scalar element.
 

>From 79a3d731ebc423aede8cc922d6ff3165ebff5e13 Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Wed, 23 Sep 2026 15:46:36 +0530
Subject: [PATCH 2/2] add test to check defaultTargetInfo indirect alloca AS

---
 llvm/unittests/ABI/TargetInfoTest.cpp | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/llvm/unittests/ABI/TargetInfoTest.cpp b/llvm/unittests/ABI/TargetInfoTest.cpp
index a165303cdaec46..9e3195ec1f1071 100644
--- a/llvm/unittests/ABI/TargetInfoTest.cpp
+++ b/llvm/unittests/ABI/TargetInfoTest.cpp
@@ -78,6 +78,14 @@ class NoInt128TargetInfo : public TestTargetInfo {
   bool hasInt128Type() const override { return false; }
 };
 
+// A default-classifying target whose alloca space is non-zero, so indirect
+// classifications must carry that address space.
+class AllocaAS5DefaultTargetInfo : public TestTargetInfo {
+public:
+  using TestTargetInfo::TestTargetInfo;
+  unsigned getAllocaAddrSpace() const override { return 5; }
+};
+
 class TargetInfoTest : public ::testing::Test {
 protected:
   llvm::BumpPtrAllocator Alloc;
@@ -180,6 +188,15 @@ TEST_F(TargetInfoTest, DefaultArgRecordInMemoryIsIndirectNoByVal) {
   EXPECT_FALSE(Info.getIndirectByVal());
 }
 
+// The default classifier routes indirect args through the target's alloca
+// space.
+TEST_F(TargetInfoTest, DefaultArgIndirectUsesAllocaAddrSpace) {
+  AllocaAS5DefaultTargetInfo TI(TB);
+  ArgInfo Info = TI.classifyArgumentType(recordInMemory());
+  ASSERT_TRUE(Info.isIndirect());
+  EXPECT_EQ(Info.getIndirectAddrSpace(), 5u);
+}
+
 // A _BitInt wider than 128 bits is passed indirectly.
 TEST_F(TargetInfoTest, DefaultArgWideBitIntIsIndirect) {
   EXPECT_TRUE(classifyArg(WideBitInt).isIndirect());



More information about the llvm-commits mailing list