[llvm] ABIInfo and TargetCodegenInfo (PR #190730)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 6 22:28:51 PDT 2026
https://github.com/vortex73 created https://github.com/llvm/llvm-project/pull/190730
This PR introduces ABIInfo and TargetCodeGenInfo, the abstract ABI lowering layer for the LLVM ABI library introduced in #158329.
This is a direct parallel to clang::ABIInfo and clang::TargetCodeGenInfo, but operating entirely on the llvm::abi type system rather than Clang's AST, keeping ABI logic independent of the frontend. It is a prerequisite for implementing the target specific lowering passes that lower FunctionInfo to LLVM IR calling conventions.
>From 24e92a5e1cc45da4733d2825ab0dc0de789ee420 Mon Sep 17 00:00:00 2001
From: Narayan Sreekumar <nsreekumar6 at gmail.com>
Date: Sun, 22 Mar 2026 00:30:10 +0530
Subject: [PATCH] ABIInfo and TargetCodegenInfo
---
llvm/include/llvm/ABI/ABIInfo.h | 91 +++++++++++++++++++
llvm/include/llvm/ABI/TargetCodegenInfo.h | 60 +++++++++++++
llvm/lib/ABI/ABIInfo.cpp | 102 ++++++++++++++++++++++
llvm/lib/ABI/CMakeLists.txt | 1 +
4 files changed, 254 insertions(+)
create mode 100644 llvm/include/llvm/ABI/ABIInfo.h
create mode 100644 llvm/include/llvm/ABI/TargetCodegenInfo.h
create mode 100644 llvm/lib/ABI/ABIInfo.cpp
diff --git a/llvm/include/llvm/ABI/ABIInfo.h b/llvm/include/llvm/ABI/ABIInfo.h
new file mode 100644
index 0000000000000..860abdc6abcb4
--- /dev/null
+++ b/llvm/include/llvm/ABI/ABIInfo.h
@@ -0,0 +1,91 @@
+//===- ABIInfo.h - ABI information access & encapsulation --------*- 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
+/// ABI information access & encapsulation
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ABI_ABIINFO_H
+#define LLVM_ABI_ABIINFO_H
+
+#include "llvm/ABI/FunctionInfo.h"
+#include "llvm/ABI/Types.h"
+#include <cassert>
+#include <climits>
+
+namespace llvm {
+namespace abi {
+
+enum RecordArgABI {
+ /// Pass it using the normal C aggregate rules for the ABI, potentially
+ /// introducing extra copies and passing some or all of it in registers.
+ RAA_Default = 0,
+
+ /// Pass it on the stack using its defined layout. The argument must be
+ /// evaluated directly into the correct stack position in the arguments area,
+ /// and the call machinery must not move it or introduce extra copies.
+ RAA_DirectInMemory,
+
+ /// Pass it as a pointer to temporary memory.
+ RAA_Indirect
+};
+
+struct ABICompatInfo {
+ unsigned Version = UINT_MAX;
+
+ struct ABIFlags {
+ bool PassInt128VectorsInMem : 1;
+ bool ReturnCXXRecordGreaterThan128InMem : 1;
+ bool ClassifyIntegerMMXAsSSE : 1;
+ bool HonorsRevision98 : 1;
+ bool Clang11Compat : 1;
+
+ ABIFlags()
+ : PassInt128VectorsInMem(true),
+ ReturnCXXRecordGreaterThan128InMem(true),
+ ClassifyIntegerMMXAsSSE(true), HonorsRevision98(true),
+ Clang11Compat(true) {}
+
+ } Flags;
+
+ ABICompatInfo() : Version(UINT_MAX) {}
+ ABICompatInfo(unsigned Ver) : Version(Ver) {}
+};
+
+class ABIInfo {
+private:
+ ABICompatInfo CompatInfo;
+
+public:
+ ABIInfo() : CompatInfo() {}
+ explicit ABIInfo(const ABICompatInfo &Info) : CompatInfo(Info) {}
+
+ virtual ~ABIInfo() = default;
+
+ RecordArgABI getRecordArgABI(const RecordType *RT) const;
+ RecordArgABI getRecordArgABI(const Type *Ty) const;
+ RecordArgABI getRecordArgABI(const RecordType *RT, bool IsCxxRecord) const;
+ bool isPromotableInteger(const IntegerType *IT) const;
+ virtual void computeInfo(FunctionInfo &FI) const = 0;
+ virtual bool isPassByRef(const Type *Ty) const { return false; }
+ const ABICompatInfo &getABICompatInfo() const { return CompatInfo; }
+ ArgInfo getNaturalAlignIndirect(const Type *Ty, bool ByVal = true) const;
+ bool isAggregateTypeForABI(const Type *Ty) const;
+ bool isZeroSizedType(const Type *Ty) const;
+ bool isEmptyRecord(const RecordType *RT) const;
+ bool isEmptyField(const FieldInfo &FI) const;
+
+ void setABICompatInfo(const ABICompatInfo &Info) { CompatInfo = Info; }
+};
+
+} // namespace abi
+} // namespace llvm
+
+#endif // LLVM_ABI_ABIINFO_H
diff --git a/llvm/include/llvm/ABI/TargetCodegenInfo.h b/llvm/include/llvm/ABI/TargetCodegenInfo.h
new file mode 100644
index 0000000000000..5b3d8d639d330
--- /dev/null
+++ b/llvm/include/llvm/ABI/TargetCodegenInfo.h
@@ -0,0 +1,60 @@
+//===- TargetCodegenInfo.h - Target code generation info ---------*- 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
+/// Target-specific code generation information and factory functions.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ABI_TARGETCODEGENINFO_H
+#define LLVM_ABI_TARGETCODEGENINFO_H
+
+#include "llvm/ABI/ABIInfo.h"
+#include "llvm/TargetParser/Triple.h"
+#include <memory>
+
+namespace llvm {
+namespace abi {
+
+class TargetCodeGenInfo {
+ std::unique_ptr<ABIInfo> Info;
+
+protected:
+ template <typename T> const T &getABIInfo() const {
+ return static_cast<const T &>(*Info);
+ }
+
+public:
+ TargetCodeGenInfo(std::unique_ptr<ABIInfo> Info) : Info(std::move(Info)) {}
+
+ virtual ~TargetCodeGenInfo() = default;
+
+ const ABIInfo &getABIInfo() const { return *Info; }
+};
+
+std::unique_ptr<TargetCodeGenInfo> createBPFTargetCodeGenInfo(TypeBuilder &TB);
+
+/// The AVX ABI level for X86 targets.
+enum class X86AVXABILevel {
+ None,
+ AVX,
+ AVX512,
+};
+
+std::unique_ptr<TargetCodeGenInfo>
+createX8664TargetCodeGenInfo(TypeBuilder &TB, const Triple &Triple,
+ X86AVXABILevel AVXLevel, bool Has64BitPointers,
+ const ABICompatInfo &Compat);
+
+// TODO: Add factory functions for further targets here.
+
+} // namespace abi
+} // namespace llvm
+
+#endif // LLVM_ABI_TARGETCODEGENINFO_H
diff --git a/llvm/lib/ABI/ABIInfo.cpp b/llvm/lib/ABI/ABIInfo.cpp
new file mode 100644
index 0000000000000..0e34b37e14132
--- /dev/null
+++ b/llvm/lib/ABI/ABIInfo.cpp
@@ -0,0 +1,102 @@
+//===- ABIInfo.cpp - ABI information access & encapsulation
+//-----------------===//
+//
+// 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/ABIInfo.h"
+
+using namespace llvm::abi;
+
+bool ABIInfo::isAggregateTypeForABI(const Type *Ty) const {
+ // Check for fundamental scalar types
+ if (Ty->isInteger() || Ty->isFloat() || Ty->isPointer() || Ty->isVector())
+ return false;
+
+ // Everything else is treated as aggregate
+ return true;
+}
+
+bool ABIInfo::isPromotableInteger(const IntegerType *IT) const {
+ unsigned BitWidth = IT->getSizeInBits().getFixedValue();
+ return BitWidth < 32;
+}
+
+// Create indirect return with natural alignment
+ArgInfo ABIInfo::getNaturalAlignIndirect(const Type *Ty, bool ByVal) const {
+ return ArgInfo::getIndirect(Ty->getAlignment(), ByVal);
+}
+RecordArgABI ABIInfo::getRecordArgABI(const RecordType *RT) const {
+ if (RT && !RT->canPassInRegisters())
+ return RAA_Indirect;
+ return RAA_Default;
+}
+
+RecordArgABI ABIInfo::getRecordArgABI(const RecordType *RT,
+ bool IsCxxRecord) const {
+ if (!IsCxxRecord) {
+ if (!RT->canPassInRegisters())
+ return RAA_Indirect;
+ return RAA_Default;
+ }
+ return getRecordArgABI(RT);
+}
+
+RecordArgABI ABIInfo::getRecordArgABI(const Type *Ty) const {
+ const RecordType *RT = dyn_cast<RecordType>(Ty);
+ if (!RT)
+ return RAA_Default;
+ return getRecordArgABI(RT, RT->isCXXRecord());
+}
+
+bool ABIInfo::isZeroSizedType(const Type *Ty) const {
+ return Ty->getSizeInBits().getFixedValue() == 0;
+}
+
+bool ABIInfo::isEmptyRecord(const RecordType *RT) const {
+ if (RT->hasFlexibleArrayMember() || RT->isPolymorphic() ||
+ RT->getNumVirtualBaseClasses() != 0)
+ return false;
+
+ for (unsigned I = 0; I < RT->getNumBaseClasses(); ++I) {
+ const Type *BaseTy = RT->getBaseClasses()[I].FieldType;
+ auto *BaseRT = dyn_cast<RecordType>(BaseTy);
+ if (!BaseRT || !isEmptyRecord(BaseRT))
+ return false;
+ }
+
+ for (unsigned I = 0; I < RT->getNumFields(); ++I) {
+ const FieldInfo &FI = RT->getFields()[I];
+
+ if (FI.IsBitField && FI.BitFieldWidth == 0)
+ continue;
+ if (FI.IsUnnamedBitfield)
+ continue;
+
+ if (!isZeroSizedType(FI.FieldType))
+ return false;
+ }
+ return true;
+}
+
+bool ABIInfo::isEmptyField(const FieldInfo &FI) const {
+ if (FI.IsUnnamedBitfield)
+ return true;
+ if (FI.IsBitField && FI.BitFieldWidth == 0)
+ return true;
+
+ const Type *Ty = FI.FieldType;
+ while (auto *AT = dyn_cast<ArrayType>(Ty)) {
+ if (AT->getNumElements() != 1)
+ break;
+ Ty = AT->getElementType();
+ }
+
+ if (auto *RT = dyn_cast<RecordType>(Ty))
+ return isEmptyRecord(RT);
+
+ return isZeroSizedType(Ty);
+}
diff --git a/llvm/lib/ABI/CMakeLists.txt b/llvm/lib/ABI/CMakeLists.txt
index 8ece1803b8aa1..c1aad73d92b68 100644
--- a/llvm/lib/ABI/CMakeLists.txt
+++ b/llvm/lib/ABI/CMakeLists.txt
@@ -1,6 +1,7 @@
add_llvm_component_library(LLVMABI
Types.cpp
FunctionInfo.cpp
+ ABIInfo.cpp
ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/ABI
More information about the llvm-commits
mailing list