[clang] [llvm] [clang] Integrate LLVMABI for function call ABI lowering (PR #194460)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 1 03:48:27 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-driver

Author: Narayan (vortex73)

<details>
<summary>Changes</summary>

This PR wires the LLVM ABI library (prototyped in https://github.com/llvm/llvm-project/pull/140112) into Clang's function call ABI lowering pipeline, behind a new `-fexperimental-abi-lowering` cc1 flag.

When the flag is enabled and the active target has an LLVMABI implementation, `CodeGenTypes::arrangeLLVMFunctionInfo` constructs an `llvm::abi::FunctionInfo` from the call's argument and result types (using QualTypeMapper(https://github.com/llvm/llvm-project/pull/174634) to translate Clang QualTypes into ABI types).

Asks the target's `llvm::abi::TargetInfo` to classify it, and then translates each `llvm::abi::ArgInfo` back into the ABIArgInfo consumed by the rest of CodeGen. The translation is handled by a new `convertABIArgInfo` helper covering the Direct, Extend, Indirect, and Ignore kinds, with coerce-to types lifted back into LLVM IR via a new IRTypeMapper.

The integration is intentionally narrow and existing code paths are untouched. `CodeGenModule::shouldUseLLVMABILowering` only opts in for targets explicitly wired to the new library(currently only BPF).. 





---

Patch is 21.19 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/194460.diff


13 Files Affected:

- (modified) clang/include/clang/Basic/CodeGenOptions.def (+4) 
- (modified) clang/include/clang/Options/Options.td (+4) 
- (modified) clang/lib/CodeGen/CGCall.cpp (+63-1) 
- (modified) clang/lib/CodeGen/CMakeLists.txt (+1) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+19) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+19) 
- (modified) clang/lib/CodeGen/CodeGenTypes.cpp (+4-1) 
- (modified) clang/lib/CodeGen/CodeGenTypes.h (+19) 
- (modified) clang/lib/CodeGen/QualTypeMapper.h (+1-1) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+2) 
- (added) llvm/include/llvm/ABI/IRTypeMapper.h (+60) 
- (modified) llvm/lib/ABI/CMakeLists.txt (+1) 
- (added) llvm/lib/ABI/IRTypeMapper.cpp (+177) 


``````````diff
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index 0d1bf83eaec9f..fd0b6d9096f50 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -28,6 +28,10 @@ CODEGENOPT(Name, Bits, Default, Compatibility)
 #endif
 
 CODEGENOPT(DisableIntegratedAS, 1, 0, Benign) ///< -no-integrated-as
+CODEGENOPT(ExperimentalABILowering, 1, 0, Benign) ///< -fexperimental-abi-lowering:
+                                                  ///< route function ABI lowering
+                                                  ///< through the in-tree LLVMABI
+                                                  ///< library where supported.
 CODEGENOPT(Crel, 1, 0, Benign) ///< -Wa,--crel
 ENUM_CODEGENOPT(RelocSectionSym, RelocSectionSymType, 2,
                 RelocSectionSymType::All, Benign) ///< -Wa,--reloc-section-sym=
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index c16c41ad4057d..a642c5e201932 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -3588,6 +3588,10 @@ defm modules_reduced_bmi : BoolOption<"f", "modules-reduced-bmi",
 def experimental_modules_reduced_bmi : Flag<["-"], "fexperimental-modules-reduced-bmi">,
   Group<f_Group>, Visibility<[ClangOption, CC1Option]>, Alias<fmodules_reduced_bmi>;
 
+def fexperimental_abi_lowering : Flag<["-"], "fexperimental-abi-lowering">,
+  Group<f_Group>, Visibility<[CC1Option]>,
+  MarshallingInfoFlag<CodeGenOpts<"ExperimentalABILowering">>;
+
 def fmodules_embed_all_files : Joined<["-"], "fmodules-embed-all-files">,
   Visibility<[ClangOption, CC1Option, CLOption]>,
   HelpText<"Embed the contents of all files read by this compilation into "
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3f1ece5fe5347..b2e2afc57f70e 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -32,6 +32,9 @@
 #include "clang/Basic/TargetInfo.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
 #include "clang/CodeGen/SwiftCallingConv.h"
+#include "llvm/ABI/FunctionInfo.h"
+#include "llvm/ABI/TargetInfo.h"
+#include "llvm/ABI/Types.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Analysis/ValueTracking.h"
@@ -824,9 +827,45 @@ const CGFunctionInfo &CodeGenTypes::arrangeCall(const CGFunctionInfo &signature,
 namespace clang {
 namespace CodeGen {
 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI);
-}
+} // namespace CodeGen
 } // namespace clang
 
+ABIArgInfo CodeGenTypes::convertABIArgInfo(const llvm::abi::ArgInfo &AbiInfo,
+                                           QualType Type) {
+  switch (AbiInfo.getKind()) {
+  case llvm::abi::ArgInfo::Direct: {
+    llvm::Type *CoercedType = nullptr;
+    if (AbiInfo.getCoerceToType())
+      CoercedType = AbiReverseMapper.convertType(AbiInfo.getCoerceToType());
+    if (!CoercedType)
+      CoercedType = ConvertType(Type);
+    return ABIArgInfo::getDirect(CoercedType, AbiInfo.getDirectOffset());
+  }
+  case llvm::abi::ArgInfo::Extend: {
+    llvm::Type *CoercedType = nullptr;
+    if (AbiInfo.getCoerceToType())
+      CoercedType = AbiReverseMapper.convertType(AbiInfo.getCoerceToType());
+    if (!CoercedType)
+      CoercedType = ConvertType(Type);
+    if (AbiInfo.isSignExt())
+      return ABIArgInfo::getSignExtend(Type, CoercedType);
+    if (AbiInfo.isZeroExt())
+      return ABIArgInfo::getZeroExtend(Type, CoercedType);
+    return ABIArgInfo::getExtend(Type, CoercedType);
+  }
+  case llvm::abi::ArgInfo::Indirect: {
+    CharUnits Alignment =
+        CharUnits::fromQuantity(AbiInfo.getIndirectAlign().value());
+    return ABIArgInfo::getIndirect(Alignment, AbiInfo.getIndirectAddrSpace(),
+                                   AbiInfo.getIndirectByVal(),
+                                   AbiInfo.getIndirectRealign());
+  }
+  case llvm::abi::ArgInfo::Ignore:
+    return ABIArgInfo::getIgnore();
+  }
+  llvm_unreachable("Unexpected llvm::abi::ArgInfo kind");
+}
+
 /// Arrange the argument and result information for an abstract value
 /// of a given function type.  This is the method which all of the
 /// above functions ultimately defer to.
@@ -876,6 +915,29 @@ const CGFunctionInfo &CodeGenTypes::arrangeLLVMFunctionInfo(
     computeSPIRKernelABIInfo(CGM, *FI);
   } else if (info.getCC() == CC_Swift || info.getCC() == CC_SwiftAsync) {
     swiftcall::computeABIInfo(CGM, *FI);
+  } else if (CGM.shouldUseLLVMABILowering()) {
+    SmallVector<const llvm::abi::Type *, 8> MappedArgTypes;
+    MappedArgTypes.reserve(argTypes.size());
+    for (CanQualType ArgType : argTypes)
+      MappedArgTypes.push_back(AbiMapper.convertType(ArgType));
+
+    std::optional<unsigned> NumRequired;
+    if (required.allowsOptionalArgs())
+      NumRequired = required.getNumRequiredArgs();
+
+    llvm::abi::FunctionInfo *AbiFI = llvm::abi::FunctionInfo::create(
+        CC, AbiMapper.convertType(resultType), MappedArgTypes, NumRequired);
+
+    CGM.getLLVMABITargetInfo(AbiMapper.getTypeBuilder()).computeInfo(*AbiFI);
+
+    FI->getReturnInfo() =
+        convertABIArgInfo(AbiFI->getReturnInfo(), FI->getReturnType());
+
+    auto AbiArgs = AbiFI->arguments();
+    for (auto [ArgIdx, CGArg] : llvm::enumerate(FI->arguments())) {
+      assert(ArgIdx < AbiArgs.size() && "ABI arg count mismatch");
+      CGArg.info = convertABIArgInfo(AbiArgs[ArgIdx].Info, CGArg.type);
+    }
   } else {
     CGM.getABIInfo().computeInfo(*FI);
   }
diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt
index 434781b3c4f02..117438c616ab5 100644
--- a/clang/lib/CodeGen/CMakeLists.txt
+++ b/clang/lib/CodeGen/CMakeLists.txt
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  ABI
   AggressiveInstCombine
   Analysis
   BitReader
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index ba51e7a3ff678..75d1f8c888e51 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -47,6 +47,7 @@
 #include "clang/Basic/Version.h"
 #include "clang/CodeGen/BackendUtil.h"
 #include "clang/CodeGen/ConstantInitBuilder.h"
+#include "llvm/ABI/TargetInfo.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -336,6 +337,24 @@ const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
   return *TheTargetCodeGenInfo;
 }
 
+bool CodeGenModule::shouldUseLLVMABILowering() const {
+  if (!CodeGenOpts.ExperimentalABILowering)
+    return false;
+  // BPF is the only target wired to the LLVMABI library at the moment.
+  return getTriple().isBPF();
+}
+
+const llvm::abi::TargetInfo &
+CodeGenModule::getLLVMABITargetInfo(llvm::abi::TypeBuilder &TB) {
+  if (TheLLVMABITargetInfo)
+    return *TheLLVMABITargetInfo;
+
+  assert(getTriple().isBPF() &&
+         "LLVMABI lowering requested for an unsupported target");
+  TheLLVMABITargetInfo = llvm::abi::createBPFTargetInfo(TB);
+  return *TheLLVMABITargetInfo;
+}
+
 static void checkDataLayoutConsistency(const TargetInfo &Target,
                                        llvm::LLVMContext &Context,
                                        const LangOptions &Opts) {
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index d62707a3355c9..b90a32f614b5d 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -55,6 +55,11 @@ class IndexedInstrProfReader;
 namespace vfs {
 class FileSystem;
 }
+
+namespace abi {
+class TargetInfo;
+class TypeBuilder;
+} // namespace abi
 }
 
 namespace clang {
@@ -363,6 +368,10 @@ class CodeGenModule : public CodeGenTypeCache {
 
   mutable std::unique_ptr<TargetCodeGenInfo> TheTargetCodeGenInfo;
 
+  /// Cached LLVMABI target lowering info, lazily constructed when the
+  /// experimental ABI lowering path is taken.
+  mutable std::unique_ptr<llvm::abi::TargetInfo> TheLLVMABITargetInfo;
+
   // This should not be moved earlier, since its initialization depends on some
   // of the previous reference members being already initialized and also checks
   // if TheTargetCodeGenInfo is NULL
@@ -877,6 +886,16 @@ class CodeGenModule : public CodeGenTypeCache {
   void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
 
   const ABIInfo &getABIInfo();
+
+  /// Lazily build and return the LLVMABI library's TargetInfo for the current
+  /// target. Used by the experimental ABI lowering path
+  /// (-fexperimental-abi-lowering).
+  const llvm::abi::TargetInfo &getLLVMABITargetInfo(llvm::abi::TypeBuilder &TB);
+
+  /// True when -fexperimental-abi-lowering is in effect AND the active target
+  /// has an LLVMABI implementation we can route to.
+  bool shouldUseLLVMABILowering() const;
+
   CGCXXABI &getCXXABI() const { return *ABI; }
   llvm::LLVMContext &getLLVMContext() { return VMContext; }
 
diff --git a/clang/lib/CodeGen/CodeGenTypes.cpp b/clang/lib/CodeGen/CodeGenTypes.cpp
index e76b1e8608d33..6009316861738 100644
--- a/clang/lib/CodeGen/CodeGenTypes.cpp
+++ b/clang/lib/CodeGen/CodeGenTypes.cpp
@@ -33,7 +33,10 @@ using namespace CodeGen;
 
 CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
     : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
-      Target(cgm.getTarget()) {
+      Target(cgm.getTarget()),
+      AbiMapper(cgm.getContext(), cgm.getModule().getDataLayout(), AbiAlloc),
+      AbiReverseMapper(cgm.getModule().getContext(),
+                       cgm.getModule().getDataLayout()) {
   SkippedLayout = false;
   LongDoubleReferenced = false;
 }
diff --git a/clang/lib/CodeGen/CodeGenTypes.h b/clang/lib/CodeGen/CodeGenTypes.h
index 9de7e0a83579d..e5e39f7061d64 100644
--- a/clang/lib/CodeGen/CodeGenTypes.h
+++ b/clang/lib/CodeGen/CodeGenTypes.h
@@ -14,10 +14,15 @@
 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
 
 #include "CGCall.h"
+#include "QualTypeMapper.h"
 #include "clang/Basic/ABI.h"
 #include "clang/CodeGen/CGFunctionInfo.h"
+#include "llvm/ABI/FunctionInfo.h"
+#include "llvm/ABI/IRTypeMapper.h"
+#include "llvm/ABI/Types.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/IR/Module.h"
+#include "llvm/Support/Allocator.h"
 
 namespace llvm {
 class FunctionType;
@@ -92,6 +97,14 @@ class CodeGenTypes {
   /// Helper for ConvertType.
   llvm::Type *ConvertFunctionTypeInternal(QualType FT);
 
+  /// Allocator and mappers used by the experimental LLVMABI-based lowering
+  /// path (gated on -fexperimental-abi-lowering). Constructed unconditionally
+  /// so the path can be entered without re-checking initialization, but the
+  /// caches stay empty when the flag is off.
+  mutable llvm::BumpPtrAllocator AbiAlloc;
+  mutable QualTypeMapper AbiMapper;
+  llvm::abi::IRTypeMapper AbiReverseMapper;
+
 public:
   CodeGenTypes(CodeGenModule &cgm);
   ~CodeGenTypes();
@@ -273,6 +286,12 @@ class CodeGenTypes {
                                              const FunctionProtoType *FTP,
                                              const CXXMethodDecl *MD);
 
+  /// Translate an llvm::abi::ArgInfo (computed by the LLVMABI library) into
+  /// the clang ABIArgInfo consumed by the rest of CodeGen. Used by the
+  /// experimental ABI lowering path.
+  ABIArgInfo convertABIArgInfo(const llvm::abi::ArgInfo &AbiInfo,
+                               QualType Type);
+
   /// "Arrange" the LLVM information for a call or type with the given
   /// signature.  This is largely an internal method; other clients
   /// should use one of the above routines, which ultimately defer to
diff --git a/clang/lib/CodeGen/QualTypeMapper.h b/clang/lib/CodeGen/QualTypeMapper.h
index 11e8ba86cfcdf..35876f44f3aba 100644
--- a/clang/lib/CodeGen/QualTypeMapper.h
+++ b/clang/lib/CodeGen/QualTypeMapper.h
@@ -71,7 +71,7 @@ class QualTypeMapper {
 
   void clearCache() { TypeCache.clear(); }
 
-  llvm::abi::TypeBuilder getTypeBuilder() { return Builder; }
+  llvm::abi::TypeBuilder &getTypeBuilder() { return Builder; }
 };
 
 } // namespace CodeGen
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index cf5a29f19aaff..37cd5fb162681 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -6596,6 +6596,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
 
   Args.AddLastArg(CmdArgs, options::OPT_fexperimental_library);
 
+  Args.AddLastArg(CmdArgs, options::OPT_fexperimental_abi_lowering);
+
   if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter))
     CmdArgs.push_back("-fexperimental-new-constant-interpreter");
 
diff --git a/llvm/include/llvm/ABI/IRTypeMapper.h b/llvm/include/llvm/ABI/IRTypeMapper.h
new file mode 100644
index 0000000000000..1a77cfd91bca9
--- /dev/null
+++ b/llvm/include/llvm/ABI/IRTypeMapper.h
@@ -0,0 +1,60 @@
+//===---- IRTypeMapper.h - Maps LLVM ABI Types to LLVM IR Types ---------===//
+//
+// 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
+/// Maps LLVM ABI type representations back to corresponding LLVM IR types.
+/// Used by frontends after the ABI library has computed argument/return
+/// classification: coerce-to types in the ABI representation must be
+/// translated to llvm::Type before being handed back to the IR builder.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ABI_IRTYPEMAPPER_H
+#define LLVM_ABI_IRTYPEMAPPER_H
+
+#include "llvm/ABI/Types.h"
+#include "llvm/ADT/DenseMap.h"
+
+namespace llvm {
+class LLVMContext;
+class Type;
+class StructType;
+class DataLayout;
+
+namespace abi {
+
+class IRTypeMapper {
+public:
+  IRTypeMapper(LLVMContext &Ctx, const DataLayout &DL) : Context(Ctx), DL(DL) {}
+
+  llvm::Type *convertType(const abi::Type *ABIType);
+
+  void clearCache() { TypeCache.clear(); }
+
+private:
+  LLVMContext &Context;
+  const DataLayout &DL;
+
+  llvm::DenseMap<const abi::Type *, llvm::Type *> TypeCache;
+
+  llvm::Type *convertArrayType(const abi::ArrayType *AT);
+  llvm::Type *convertVectorType(const abi::VectorType *VT);
+  llvm::Type *convertRecordType(const abi::RecordType *RT);
+  llvm::Type *convertComplexType(const abi::ComplexType *CT);
+  llvm::Type *convertMemberPointerType(const abi::MemberPointerType *MPT);
+
+  llvm::StructType *createStructFromFields(ArrayRef<abi::FieldInfo> Fields,
+                                           TypeSize Size, Align Alignment,
+                                           bool IsUnion);
+  llvm::Type *createPaddingType(uint64_t PaddingBits);
+};
+
+} // namespace abi
+} // namespace llvm
+
+#endif // LLVM_ABI_ABITYPEMAPPER_H
diff --git a/llvm/lib/ABI/CMakeLists.txt b/llvm/lib/ABI/CMakeLists.txt
index eb1d6042a25cd..a5c102b57d074 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
+  IRTypeMapper.cpp
   Targets/BPF.cpp
 
   ADDITIONAL_HEADER_DIRS
diff --git a/llvm/lib/ABI/IRTypeMapper.cpp b/llvm/lib/ABI/IRTypeMapper.cpp
new file mode 100644
index 0000000000000..17e72056313cd
--- /dev/null
+++ b/llvm/lib/ABI/IRTypeMapper.cpp
@@ -0,0 +1,177 @@
+//===---- IRTypeMapper.cpp - Maps LLVM ABI Types to LLVM IR Types -------===//
+//
+// 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/IRTypeMapper.h"
+#include "llvm/ABI/Types.h"
+#include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Type.h"
+
+using namespace llvm::abi;
+
+llvm::Type *IRTypeMapper::convertType(const abi::Type *ABIType) {
+  assert(ABIType && "convertType requires a non-null ABI type");
+
+  auto It = TypeCache.find(ABIType);
+  if (It != TypeCache.end())
+    return It->second;
+
+  llvm::Type *Result = nullptr;
+
+  switch (ABIType->getKind()) {
+  case abi::TypeKind::Void:
+    Result = llvm::Type::getVoidTy(Context);
+    break;
+  case abi::TypeKind::Integer: {
+    const auto *IT = cast<abi::IntegerType>(ABIType);
+    Result =
+        llvm::IntegerType::get(Context, IT->getSizeInBits().getFixedValue());
+    break;
+  }
+  case abi::TypeKind::Float: {
+    const llvm::fltSemantics *Semantics =
+        cast<abi::FloatType>(ABIType)->getSemantics();
+    Result = llvm::Type::getFloatingPointTy(Context, *Semantics);
+    break;
+  }
+  case abi::TypeKind::Pointer:
+    Result = llvm::PointerType::get(
+        Context, cast<abi::PointerType>(ABIType)->getAddrSpace());
+    break;
+  case abi::TypeKind::Array:
+    Result = convertArrayType(cast<abi::ArrayType>(ABIType));
+    break;
+  case abi::TypeKind::Vector:
+    Result = convertVectorType(cast<abi::VectorType>(ABIType));
+    break;
+  case abi::TypeKind::Record:
+    Result = convertRecordType(cast<abi::RecordType>(ABIType));
+    break;
+  case abi::TypeKind::Complex:
+    Result = convertComplexType(cast<abi::ComplexType>(ABIType));
+    break;
+  case abi::TypeKind::MemberPointer:
+    Result = convertMemberPointerType(cast<abi::MemberPointerType>(ABIType));
+    break;
+  }
+
+  if (Result)
+    TypeCache[ABIType] = Result;
+  return Result;
+}
+
+llvm::Type *IRTypeMapper::convertArrayType(const abi::ArrayType *AT) {
+  llvm::Type *ElementType = convertType(AT->getElementType());
+  if (!ElementType)
+    return nullptr;
+  uint64_t NumElements = AT->getNumElements();
+  if (AT->isMatrixType())
+    return llvm::VectorType::get(ElementType,
+                                 ElementCount::getFixed(NumElements));
+  return llvm::ArrayType::get(ElementType, NumElements);
+}
+
+llvm::Type *IRTypeMapper::convertVectorType(const abi::VectorType *VT) {
+  llvm::Type *ElementType = convertType(VT->getElementType());
+  if (!ElementType)
+    return nullptr;
+  return llvm::VectorType::get(ElementType, VT->getNumElements());
+}
+
+llvm::Type *IRTypeMapper::convertRecordType(const abi::RecordType *RT) {
+  return createStructFromFields(RT->getFields(), RT->getSizeInBits(),
+                                RT->getAlignment(), RT->isUnion());
+}
+
+llvm::Type *IRTypeMapper::convertComplexType(const abi::ComplexType *CT) {
+  llvm::Type *ElementType = convertType(CT->getElementType());
+  if (!ElementType)
+    return nullptr;
+  llvm::Type *Fields[] = {ElementType, ElementType};
+  return llvm::StructType::get(Context, Fields, /*isPacked=*/false);
+}
+
+llvm::Type *
+IRTypeMapper::convertMemberPointerType(const abi::MemberPointerType *MPT) {
+  llvm::Type *IntPtrTy = DL.getIntPtrType(Context);
+  if (MPT->isFunctionPointer()) {
+    llvm::Type *Fields[] = {IntPtrTy, IntPtrTy};
+    return llvm::StructType::get(Context, Fields, /*isPacked=*/false);
+  }
+  return IntPtrTy;
+}
+
+llvm::Type *IRTypeMapper::createPaddingType(uint64_t PaddingBits) {
+  if (PaddingBits == 0)
+    return nullptr;
+  assert(PaddingBits % 8 == 0 &&
+         "sub-byte padding cannot be expressed as an llvm::Type");
+  return llvm::ArrayType::get(llvm::IntegerType::get(Context, 8),
+                              PaddingBits / 8);
+}
+
+llvm::StructType *
+IRTypeMapper::createStructFromFields(ArrayRef<abi::FieldInfo> Fields,
+                                     TypeSize Size, Align Alignment,
+                                     bool IsUnion) {
+  SmallVector<llvm::Type *, 16> FieldTypes;
+
+  if (IsUnion) {
+    llvm::Type *LargestFieldType = nullptr;
+    uint64_t LargestFieldSize = 0;
+    for (const auto &Field : Fields) {
+      llvm::Type *FieldType = convertType(Field.FieldType);
+      if (!FieldType)
+        continue;
+      uint64_t FieldSize = Field.FieldType->getSizeInBits().getFixedValue();
+      if (FieldSize > LargestFieldSize) {
+        LargestFieldSize = FieldSize;
+        LargestFieldType = FieldType;
+      }
+    }
+    if (LargestFieldType) {
+      FieldTypes.push_back(LargestFieldType);
+      uint64_t UnionSizeBits = Size.getFixedValue();
+      if (LargestFieldSize < UnionSizeBits) {
+        if (llvm::Type *PaddingType =
+                ...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/194460


More information about the cfe-commits mailing list