[llvm] [WIP] ABI Lowering Library (PR #140112)

via llvm-commits llvm-commits at lists.llvm.org
Mon May 26 14:37:45 PDT 2025


https://github.com/vortex73 updated https://github.com/llvm/llvm-project/pull/140112

>From 322c1cfb925f3073f3d3b30abe1b2e35ae7745f3 Mon Sep 17 00:00:00 2001
From: Narayan Sreekumar <nsreekumar6 at gmail.com>
Date: Thu, 15 May 2025 23:13:50 +0530
Subject: [PATCH 1/3] [LLVM ABI] The Typesystem

---
 llvm/include/llvm/ABI/Types.h | 121 ++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
 create mode 100644 llvm/include/llvm/ABI/Types.h

diff --git a/llvm/include/llvm/ABI/Types.h b/llvm/include/llvm/ABI/Types.h
new file mode 100644
index 0000000000000..443a6c1eab4e7
--- /dev/null
+++ b/llvm/include/llvm/ABI/Types.h
@@ -0,0 +1,121 @@
+#ifndef LLVM_ABI_TYPES_H
+#define LLVM_ABI_TYPES_H
+
+#include <cstdint>
+#include <memory>
+#include <string>
+
+namespace llvm {
+namespace abi {
+
+enum class TypeKind {
+  Void,
+  Integer,
+  Float,
+  Pointer,
+  Array,
+  Vector,
+  Struct,
+  Union,
+  Function
+};
+class Type {
+protected:
+  TypeKind Kind;
+  uint64_t SizeInBits;
+  uint64_t AlignInBits;
+  bool IsExplicitlyAligned;
+
+  Type(TypeKind K, uint64_t Size, uint64_t Align, bool ExplicitAlign = false)
+      : Kind(K), SizeInBits(Size), AlignInBits(Align),
+        IsExplicitlyAligned(ExplicitAlign) {}
+
+public:
+  virtual ~Type() = default;
+
+  TypeKind getKind() const { return Kind; }
+  uint64_t getSizeInBits() const { return SizeInBits; }
+  uint64_t getAlignInBits() const { return AlignInBits; }
+  bool hasExplicitAlignment() const { return IsExplicitlyAligned; }
+
+  void setExplicitAlignment(uint64_t Align) {
+    AlignInBits = Align;
+    IsExplicitlyAligned = true;
+  }
+
+  bool isVoid() const { return Kind == TypeKind::Void; }
+  bool isInteger() const { return Kind == TypeKind::Integer; }
+  bool isFloat() const { return Kind == TypeKind::Float; }
+  bool isPointer() const { return Kind == TypeKind::Pointer; }
+  bool isArray() const { return Kind == TypeKind::Array; }
+  bool isVector() const { return Kind == TypeKind::Vector; }
+  bool isStruct() const { return Kind == TypeKind::Struct; }
+  bool isUnion() const { return Kind == TypeKind::Union; }
+  bool isFunction() const { return Kind == TypeKind::Function; }
+
+  static bool classof(const Type *) { return true; }
+};
+class VoidType : public Type {
+public:
+  VoidType() : Type(TypeKind::Void, 0, 0) {}
+
+  static bool classof(const Type *T) { return T->getKind() == TypeKind::Void; }
+};
+
+class IntegerType : public Type {
+private:
+  bool IsSigned;
+  bool IsAltRepresentation;
+  std::string TypeName;
+
+public:
+  IntegerType(uint64_t BitWidth, uint64_t Align, bool Signed,
+              bool AltRep = false, const std::string &Name = "")
+      : Type(TypeKind::Integer, BitWidth, Align), IsSigned(Signed),
+        IsAltRepresentation(AltRep), TypeName(Name) {}
+
+  bool isSigned() const { return IsSigned; }
+  bool isAltRepresentation() const { return IsAltRepresentation; }
+  const std::string &getTypeName() const { return TypeName; }
+
+  static bool classof(const Type *T) {
+    return T->getKind() == TypeKind::Integer;
+  }
+};
+class FloatType : public Type {
+private:
+  std::string TypeName;
+
+public:
+  FloatType(uint64_t BitWidth, uint64_t Align, const std::string &Name)
+      : Type(TypeKind::Float, BitWidth, Align), TypeName(Name) {}
+
+  const std::string &getTypeName() const { return TypeName; }
+
+  static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
+};
+class PointerType : public Type {
+private:
+  std::unique_ptr<Type> PointeeType;
+  bool IsConst;
+  bool IsVolatile;
+
+public:
+  PointerType(std::unique_ptr<Type> Pointee, uint64_t Size, uint64_t Align,
+              bool Const = false, bool Volatile = false)
+      : Type(TypeKind::Pointer, Size, Align), PointeeType(std::move(Pointee)),
+        IsConst(Const), IsVolatile(Volatile) {}
+
+  const Type *getPointeeType() const { return PointeeType.get(); }
+  bool isConst() const { return IsConst; }
+  bool isVolatile() const { return IsVolatile; }
+
+  static bool classof(const Type *T) {
+    return T->getKind() == TypeKind::Pointer;
+  }
+};
+
+} // namespace abi
+} // namespace llvm
+
+#endif

>From 0858b1f327c7a49c2e2825124a8d5cabbd8654fd Mon Sep 17 00:00:00 2001
From: Narayan Sreekumar <nsreekumar6 at gmail.com>
Date: Fri, 23 May 2025 17:53:53 +0530
Subject: [PATCH 2/3] [LLVMABI] API for Creating types

---
 llvm/include/llvm/ABI/Types.h | 244 +++++++++++++++++++++++++++++-----
 1 file changed, 213 insertions(+), 31 deletions(-)

diff --git a/llvm/include/llvm/ABI/Types.h b/llvm/include/llvm/ABI/Types.h
index 443a6c1eab4e7..84cb586832dbd 100644
--- a/llvm/include/llvm/ABI/Types.h
+++ b/llvm/include/llvm/ABI/Types.h
@@ -1,9 +1,9 @@
 #ifndef LLVM_ABI_TYPES_H
 #define LLVM_ABI_TYPES_H
 
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/Allocator.h"
 #include <cstdint>
-#include <memory>
-#include <string>
 
 namespace llvm {
 namespace abi {
@@ -19,6 +19,7 @@ enum class TypeKind {
   Union,
   Function
 };
+
 class Type {
 protected:
   TypeKind Kind;
@@ -31,8 +32,6 @@ class Type {
         IsExplicitlyAligned(ExplicitAlign) {}
 
 public:
-  virtual ~Type() = default;
-
   TypeKind getKind() const { return Kind; }
   uint64_t getSizeInBits() const { return SizeInBits; }
   uint64_t getAlignInBits() const { return AlignInBits; }
@@ -52,9 +51,8 @@ class Type {
   bool isStruct() const { return Kind == TypeKind::Struct; }
   bool isUnion() const { return Kind == TypeKind::Union; }
   bool isFunction() const { return Kind == TypeKind::Function; }
-
-  static bool classof(const Type *) { return true; }
 };
+
 class VoidType : public Type {
 public:
   VoidType() : Type(TypeKind::Void, 0, 0) {}
@@ -65,53 +63,237 @@ class VoidType : public Type {
 class IntegerType : public Type {
 private:
   bool IsSigned;
-  bool IsAltRepresentation;
-  std::string TypeName;
 
 public:
-  IntegerType(uint64_t BitWidth, uint64_t Align, bool Signed,
-              bool AltRep = false, const std::string &Name = "")
-      : Type(TypeKind::Integer, BitWidth, Align), IsSigned(Signed),
-        IsAltRepresentation(AltRep), TypeName(Name) {}
+  IntegerType(uint64_t BitWidth, uint64_t Align, bool Signed)
+      : Type(TypeKind::Integer, BitWidth, Align), IsSigned(Signed) {}
 
   bool isSigned() const { return IsSigned; }
-  bool isAltRepresentation() const { return IsAltRepresentation; }
-  const std::string &getTypeName() const { return TypeName; }
 
   static bool classof(const Type *T) {
     return T->getKind() == TypeKind::Integer;
   }
 };
+
 class FloatType : public Type {
+public:
+  FloatType(uint64_t BitWidth, uint64_t Align)
+      : Type(TypeKind::Float, BitWidth, Align) {}
+
+  static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
+};
+
+class PointerType : public Type {
+public:
+  PointerType(uint64_t Size, uint64_t Align)
+      : Type(TypeKind::Pointer, Size, Align) {}
+
+  static bool classof(const Type *T) {
+    return T->getKind() == TypeKind::Pointer;
+  }
+};
+
+class ArrayType : public Type {
 private:
-  std::string TypeName;
+  const Type *ElementType;
+  uint64_t NumElements;
 
 public:
-  FloatType(uint64_t BitWidth, uint64_t Align, const std::string &Name)
-      : Type(TypeKind::Float, BitWidth, Align), TypeName(Name) {}
+  ArrayType(const Type *ElemType, uint64_t NumElems)
+      : Type(TypeKind::Array, ElemType->getSizeInBits() * NumElems,
+             ElemType->getAlignInBits()),
+        ElementType(ElemType), NumElements(NumElems) {}
 
-  const std::string &getTypeName() const { return TypeName; }
+  const Type *getElementType() const { return ElementType; }
+  uint64_t getNumElements() const { return NumElements; }
 
-  static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
+  static bool classof(const Type *T) { return T->getKind() == TypeKind::Array; }
 };
-class PointerType : public Type {
+
+class VectorType : public Type {
 private:
-  std::unique_ptr<Type> PointeeType;
-  bool IsConst;
-  bool IsVolatile;
+  const Type *ElementType;
+  uint64_t NumElements;
 
 public:
-  PointerType(std::unique_ptr<Type> Pointee, uint64_t Size, uint64_t Align,
-              bool Const = false, bool Volatile = false)
-      : Type(TypeKind::Pointer, Size, Align), PointeeType(std::move(Pointee)),
-        IsConst(Const), IsVolatile(Volatile) {}
+  VectorType(const Type *ElemType, uint64_t NumElems, uint64_t Align)
+      : Type(TypeKind::Vector, ElemType->getSizeInBits() * NumElems, Align),
+        ElementType(ElemType), NumElements(NumElems) {}
 
-  const Type *getPointeeType() const { return PointeeType.get(); }
-  bool isConst() const { return IsConst; }
-  bool isVolatile() const { return IsVolatile; }
+  const Type *getElementType() const { return ElementType; }
+  uint64_t getNumElements() const { return NumElements; }
 
   static bool classof(const Type *T) {
-    return T->getKind() == TypeKind::Pointer;
+    return T->getKind() == TypeKind::Vector;
+  }
+};
+
+struct FieldInfo {
+  const Type *FieldType;
+  uint64_t OffsetInBits;
+  bool IsBitField;
+  uint64_t BitFieldWidth;
+
+  FieldInfo(const Type *Type, uint64_t Offset = 0, bool BitField = false,
+            uint64_t BFWidth = 0)
+      : FieldType(Type), OffsetInBits(Offset), IsBitField(BitField),
+        BitFieldWidth(BFWidth) {}
+};
+
+enum class StructPacking { Default, Packed, ExplicitPacking };
+
+class StructType : public Type {
+private:
+  const FieldInfo *Fields;
+  uint32_t NumFields;
+  StructPacking Packing;
+
+public:
+  StructType(const FieldInfo *StructFields, uint32_t FieldCount, uint64_t Size,
+             uint64_t Align, StructPacking Pack = StructPacking::Default)
+      : Type(TypeKind::Struct, Size, Align), Fields(StructFields),
+        NumFields(FieldCount), Packing(Pack) {}
+
+  const FieldInfo *getFields() const { return Fields; }
+  uint32_t getNumFields() const { return NumFields; }
+  StructPacking getPacking() const { return Packing; }
+
+  static bool classof(const Type *T) {
+    return T->getKind() == TypeKind::Struct;
+  }
+};
+
+class UnionType : public Type {
+private:
+  const FieldInfo *Fields;
+  uint32_t NumFields;
+  StructPacking Packing;
+
+public:
+  UnionType(const FieldInfo *UnionFields, uint32_t FieldCount, uint64_t Size,
+            uint64_t Align, StructPacking Pack = StructPacking::Default)
+      : Type(TypeKind::Union, Size, Align), Fields(UnionFields),
+        NumFields(FieldCount), Packing(Pack) {}
+
+  const FieldInfo *getFields() const { return Fields; }
+  uint32_t getNumFields() const { return NumFields; }
+  StructPacking getPacking() const { return Packing; }
+
+  static bool classof(const Type *T) { return T->getKind() == TypeKind::Union; }
+};
+
+enum class CallConv {
+  C,
+  // TODO: extend for more CallConvs
+};
+
+class FunctionType : public Type {
+private:
+  const Type *ReturnType;
+  const Type *const *ParameterTypes;
+  uint32_t NumParams;
+  bool IsVarArg;
+  CallConv CC;
+
+public:
+  FunctionType(const Type *RetType, const Type *const *ParamTypes,
+               uint32_t ParamCount, bool VarArgs, CallConv CallConv)
+      : Type(TypeKind::Function, 0, 0), ReturnType(RetType),
+        ParameterTypes(ParamTypes), NumParams(ParamCount), IsVarArg(VarArgs),
+        CC(CallConv) {}
+
+  const Type *getReturnType() const { return ReturnType; }
+  const Type *const *getParameterTypes() const { return ParameterTypes; }
+  uint32_t getNumParameters() const { return NumParams; }
+  const Type *getParameterType(uint32_t Index) const {
+    assert(Index < NumParams && "Parameter index out of bounds");
+    return ParameterTypes[Index];
+  }
+  bool isVarArg() const { return IsVarArg; }
+  CallConv getCallingConv() const { return CC; }
+
+  static bool classof(const Type *T) {
+    return T->getKind() == TypeKind::Function;
+  }
+};
+
+// API for creating ABI Types
+class TypeBuilder {
+private:
+  BumpPtrAllocator &Allocator;
+
+public:
+  explicit TypeBuilder(BumpPtrAllocator &Alloc) : Allocator(Alloc) {}
+
+  const VoidType *getVoidType() {
+    return new (Allocator.Allocate<VoidType>()) VoidType();
+  }
+
+  const IntegerType *getIntegerType(uint64_t BitWidth, uint64_t Align,
+                                    bool Signed) {
+    return new (Allocator.Allocate<IntegerType>())
+        IntegerType(BitWidth, Align, Signed);
+  }
+
+  const FloatType *getFloatType(uint64_t BitWidth, uint64_t Align) {
+    return new (Allocator.Allocate<FloatType>()) FloatType(BitWidth, Align);
+  }
+
+  const PointerType *getPointerType(uint64_t Size, uint64_t Align) {
+    return new (Allocator.Allocate<PointerType>()) PointerType(Size, Align);
+  }
+
+  const ArrayType *getArrayType(const Type *ElementType, uint64_t NumElements) {
+    return new (Allocator.Allocate<ArrayType>())
+        ArrayType(ElementType, NumElements);
+  }
+
+  const VectorType *getVectorType(const Type *ElementType, uint64_t NumElements,
+                                  uint64_t Align) {
+    return new (Allocator.Allocate<VectorType>())
+        VectorType(ElementType, NumElements, Align);
+  }
+
+  const StructType *getStructType(ArrayRef<FieldInfo> Fields, uint64_t Size,
+                                  uint64_t Align,
+                                  StructPacking Pack = StructPacking::Default) {
+    FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
+
+    for (size_t I = 0; I < Fields.size(); ++I) {
+      new (&FieldArray[I]) FieldInfo(Fields[I]);
+    }
+
+    return new (Allocator.Allocate<StructType>()) StructType(
+        FieldArray, static_cast<uint32_t>(Fields.size()), Size, Align, Pack);
+  }
+
+  const UnionType *getUnionType(ArrayRef<FieldInfo> Fields, uint64_t Size,
+                                uint64_t Align,
+                                StructPacking Pack = StructPacking::Default) {
+    FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
+
+    for (size_t I = 0; I < Fields.size(); ++I) {
+      new (&FieldArray[I]) FieldInfo(Fields[I]);
+    }
+
+    return new (Allocator.Allocate<UnionType>()) UnionType(
+        FieldArray, static_cast<uint32_t>(Fields.size()), Size, Align, Pack);
+  }
+
+  const FunctionType *getFunctionType(const Type *ReturnType,
+                                      ArrayRef<const Type *> ParamTypes,
+                                      bool IsVarArg,
+                                      CallConv CC = CallConv::C) {
+    const Type **ParamArray =
+        Allocator.Allocate<const Type *>(ParamTypes.size());
+
+    for (size_t I = 0; I < ParamTypes.size(); ++I) {
+      ParamArray[I] = ParamTypes[I];
+    }
+
+    return new (Allocator.Allocate<FunctionType>())
+        FunctionType(ReturnType, ParamArray,
+                     static_cast<uint32_t>(ParamTypes.size()), IsVarArg, CC);
   }
 };
 

>From cab802d05fc69c46e6e8b6712e461c2685963527 Mon Sep 17 00:00:00 2001
From: Narayan Sreekumar <nsreekumar6 at gmail.com>
Date: Tue, 27 May 2025 03:07:20 +0530
Subject: [PATCH 3/3] [LLVMABI] Mapper Interface

---
 llvm/include/llvm/ABI/QualTypeMapper.h |  58 +++++++++++++
 llvm/include/llvm/ABI/Types.h          | 113 ++++++++-----------------
 llvm/lib/ABI/CMakeLists.txt            |  17 ++++
 llvm/lib/ABI/QualTypeMapper.cpp        |  18 ++++
 llvm/lib/CMakeLists.txt                |   1 +
 5 files changed, 129 insertions(+), 78 deletions(-)
 create mode 100644 llvm/include/llvm/ABI/QualTypeMapper.h
 create mode 100644 llvm/lib/ABI/CMakeLists.txt
 create mode 100644 llvm/lib/ABI/QualTypeMapper.cpp

diff --git a/llvm/include/llvm/ABI/QualTypeMapper.h b/llvm/include/llvm/ABI/QualTypeMapper.h
new file mode 100644
index 0000000000000..75aeaa07af858
--- /dev/null
+++ b/llvm/include/llvm/ABI/QualTypeMapper.h
@@ -0,0 +1,58 @@
+#ifndef LLVM_ABI_QUALTYPE_MAPPER_H
+#define LLVM_ABI_QUALTYPE_MAPPER_H
+
+#include "llvm/Support/Allocator.h"
+#include <clang/AST/ASTContext.h>
+#include <clang/AST/Type.h>
+#include <llvm/ABI/Types.h>
+#include <llvm/ADT/DenseMap.h>
+
+namespace llvm {
+	namespace abi {
+
+		class QualTypeMapper {
+			private:
+				clang::ASTContext &ASTCtx;
+				TypeBuilder Builder;
+
+				// llvm::DenseMap<clang::QualType	, const Type*> TypeCache;
+
+				const Type *convertBuiltinType(const clang::BuiltinType *BT);
+				const Type *convertPointerType(const clang::PointerType *PT);
+				const Type *convertArrayType(const clang::ArrayType *AT);
+				const Type *convertVectorType(const clang::VectorType *VT);
+				const Type *convertRecordType(const clang::RecordType *RT);
+				const Type *convertFunctionType(const clang::FunctionProtoType *FT);
+				const Type *convertEnumType(const clang::EnumType *ET);
+
+				void computeRecordLayout(const clang::RecordDecl *RD,
+						llvm::SmallVectorImpl<FieldInfo> &Fields,
+						uint64_t &TotalSize, uint64_t &Alignment,
+						StructPacking &Packing);
+
+
+				uint64_t getTypeSize(clang::QualType QT) const;
+				uint64_t getTypeAlign(clang::QualType QT) const;
+				uint64_t getPointerSize() const;
+				uint64_t getPointerAlign() const;
+
+
+			public:
+				
+				explicit QualTypeMapper(clang::ASTContext &Ctx, BumpPtrAllocator &Alloc)
+					: ASTCtx(Ctx), Builder(Alloc) {}
+
+				const Type *convertType(clang::QualType QT);
+
+				// void clearCache() {TypeCache.clear();}
+
+				TypeBuilder getTypeBuilder() {return Builder;}
+
+		};
+
+
+
+	} // namespace abi
+} // namespace llvm
+
+#endif // !LLVM_ABI_QUALTYPE_MAPPER_H
diff --git a/llvm/include/llvm/ABI/Types.h b/llvm/include/llvm/ABI/Types.h
index 84cb586832dbd..441d65220b007 100644
--- a/llvm/include/llvm/ABI/Types.h
+++ b/llvm/include/llvm/ABI/Types.h
@@ -1,8 +1,12 @@
 #ifndef LLVM_ABI_TYPES_H
 #define LLVM_ABI_TYPES_H
 
+#include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/ArrayRef.h"
+#include <llvm/IR/CallingConv.h>
+#include "llvm/Support/Alignment.h"
 #include "llvm/Support/Allocator.h"
+#include "llvm/Support/TypeSize.h"
 #include <cstdint>
 
 namespace llvm {
@@ -17,27 +21,26 @@ enum class TypeKind {
   Vector,
   Struct,
   Union,
-  Function
 };
 
 class Type {
 protected:
   TypeKind Kind;
-  uint64_t SizeInBits;
-  uint64_t AlignInBits;
+  TypeSize SizeInBits;
+  Align AlignInBits;
   bool IsExplicitlyAligned;
 
-  Type(TypeKind K, uint64_t Size, uint64_t Align, bool ExplicitAlign = false)
+  Type(TypeKind K, TypeSize Size, Align Align, bool ExplicitAlign = false)
       : Kind(K), SizeInBits(Size), AlignInBits(Align),
         IsExplicitlyAligned(ExplicitAlign) {}
 
 public:
   TypeKind getKind() const { return Kind; }
-  uint64_t getSizeInBits() const { return SizeInBits; }
-  uint64_t getAlignInBits() const { return AlignInBits; }
+  TypeSize getSizeInBits() const { return SizeInBits; }
+  Align getAlignInBits() const { return AlignInBits; }
   bool hasExplicitAlignment() const { return IsExplicitlyAligned; }
 
-  void setExplicitAlignment(uint64_t Align) {
+  void setExplicitAlignment(Align Align) {
     AlignInBits = Align;
     IsExplicitlyAligned = true;
   }
@@ -50,12 +53,11 @@ class Type {
   bool isVector() const { return Kind == TypeKind::Vector; }
   bool isStruct() const { return Kind == TypeKind::Struct; }
   bool isUnion() const { return Kind == TypeKind::Union; }
-  bool isFunction() const { return Kind == TypeKind::Function; }
 };
 
 class VoidType : public Type {
 public:
-  VoidType() : Type(TypeKind::Void, 0, 0) {}
+  VoidType() : Type(TypeKind::Void, TypeSize::getFixed(0), Align(1)) {}
 
   static bool classof(const Type *T) { return T->getKind() == TypeKind::Void; }
 };
@@ -65,8 +67,8 @@ class IntegerType : public Type {
   bool IsSigned;
 
 public:
-  IntegerType(uint64_t BitWidth, uint64_t Align, bool Signed)
-      : Type(TypeKind::Integer, BitWidth, Align), IsSigned(Signed) {}
+  IntegerType(uint64_t BitWidth, Align Align, bool Signed)
+      : Type(TypeKind::Integer, TypeSize::getFixed(BitWidth), Align), IsSigned(Signed) {}
 
   bool isSigned() const { return IsSigned; }
 
@@ -76,17 +78,21 @@ class IntegerType : public Type {
 };
 
 class FloatType : public Type {
+private:
+	const fltSemantics *Semantics;
+
 public:
-  FloatType(uint64_t BitWidth, uint64_t Align)
-      : Type(TypeKind::Float, BitWidth, Align) {}
+  FloatType(const fltSemantics &FloatSemantics, Align Align)
+      : Type(TypeKind::Float, TypeSize::getFixed(APFloat::getSizeInBits(FloatSemantics)), Align)
+  , Semantics(&FloatSemantics){}
 
   static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
 };
 
 class PointerType : public Type {
 public:
-  PointerType(uint64_t Size, uint64_t Align)
-      : Type(TypeKind::Pointer, Size, Align) {}
+  PointerType(uint64_t Size, Align Align)
+      : Type(TypeKind::Pointer, TypeSize::getFixed(Size), Align) {}
 
   static bool classof(const Type *T) {
     return T->getKind() == TypeKind::Pointer;
@@ -116,7 +122,7 @@ class VectorType : public Type {
   uint64_t NumElements;
 
 public:
-  VectorType(const Type *ElemType, uint64_t NumElems, uint64_t Align)
+  VectorType(const Type *ElemType, uint64_t NumElems, Align Align)
       : Type(TypeKind::Vector, ElemType->getSizeInBits() * NumElems, Align),
         ElementType(ElemType), NumElements(NumElems) {}
 
@@ -149,8 +155,8 @@ class StructType : public Type {
   StructPacking Packing;
 
 public:
-  StructType(const FieldInfo *StructFields, uint32_t FieldCount, uint64_t Size,
-             uint64_t Align, StructPacking Pack = StructPacking::Default)
+  StructType(const FieldInfo *StructFields, uint32_t FieldCount, TypeSize Size,
+             Align Align, StructPacking Pack = StructPacking::Default)
       : Type(TypeKind::Struct, Size, Align), Fields(StructFields),
         NumFields(FieldCount), Packing(Pack) {}
 
@@ -170,8 +176,8 @@ class UnionType : public Type {
   StructPacking Packing;
 
 public:
-  UnionType(const FieldInfo *UnionFields, uint32_t FieldCount, uint64_t Size,
-            uint64_t Align, StructPacking Pack = StructPacking::Default)
+  UnionType(const FieldInfo *UnionFields, uint32_t FieldCount, TypeSize Size,
+            Align Align, StructPacking Pack = StructPacking::Default)
       : Type(TypeKind::Union, Size, Align), Fields(UnionFields),
         NumFields(FieldCount), Packing(Pack) {}
 
@@ -182,40 +188,7 @@ class UnionType : public Type {
   static bool classof(const Type *T) { return T->getKind() == TypeKind::Union; }
 };
 
-enum class CallConv {
-  C,
-  // TODO: extend for more CallConvs
-};
-
-class FunctionType : public Type {
-private:
-  const Type *ReturnType;
-  const Type *const *ParameterTypes;
-  uint32_t NumParams;
-  bool IsVarArg;
-  CallConv CC;
-
-public:
-  FunctionType(const Type *RetType, const Type *const *ParamTypes,
-               uint32_t ParamCount, bool VarArgs, CallConv CallConv)
-      : Type(TypeKind::Function, 0, 0), ReturnType(RetType),
-        ParameterTypes(ParamTypes), NumParams(ParamCount), IsVarArg(VarArgs),
-        CC(CallConv) {}
-
-  const Type *getReturnType() const { return ReturnType; }
-  const Type *const *getParameterTypes() const { return ParameterTypes; }
-  uint32_t getNumParameters() const { return NumParams; }
-  const Type *getParameterType(uint32_t Index) const {
-    assert(Index < NumParams && "Parameter index out of bounds");
-    return ParameterTypes[Index];
-  }
-  bool isVarArg() const { return IsVarArg; }
-  CallConv getCallingConv() const { return CC; }
 
-  static bool classof(const Type *T) {
-    return T->getKind() == TypeKind::Function;
-  }
-};
 
 // API for creating ABI Types
 class TypeBuilder {
@@ -229,17 +202,17 @@ class TypeBuilder {
     return new (Allocator.Allocate<VoidType>()) VoidType();
   }
 
-  const IntegerType *getIntegerType(uint64_t BitWidth, uint64_t Align,
+  const IntegerType *getIntegerType(uint64_t BitWidth, Align Align,
                                     bool Signed) {
     return new (Allocator.Allocate<IntegerType>())
         IntegerType(BitWidth, Align, Signed);
   }
 
-  const FloatType *getFloatType(uint64_t BitWidth, uint64_t Align) {
-    return new (Allocator.Allocate<FloatType>()) FloatType(BitWidth, Align);
+  const FloatType *getFloatType(const fltSemantics &Semantics, Align Align) {
+    return new (Allocator.Allocate<FloatType>()) FloatType(Semantics, Align);
   }
 
-  const PointerType *getPointerType(uint64_t Size, uint64_t Align) {
+  const PointerType *getPointerType(uint64_t Size, Align Align) {
     return new (Allocator.Allocate<PointerType>()) PointerType(Size, Align);
   }
 
@@ -249,13 +222,13 @@ class TypeBuilder {
   }
 
   const VectorType *getVectorType(const Type *ElementType, uint64_t NumElements,
-                                  uint64_t Align) {
+                                  Align Align) {
     return new (Allocator.Allocate<VectorType>())
         VectorType(ElementType, NumElements, Align);
   }
 
-  const StructType *getStructType(ArrayRef<FieldInfo> Fields, uint64_t Size,
-                                  uint64_t Align,
+  const StructType *getStructType(ArrayRef<FieldInfo> Fields, TypeSize Size,
+                                  Align Align,
                                   StructPacking Pack = StructPacking::Default) {
     FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
 
@@ -267,8 +240,8 @@ class TypeBuilder {
         FieldArray, static_cast<uint32_t>(Fields.size()), Size, Align, Pack);
   }
 
-  const UnionType *getUnionType(ArrayRef<FieldInfo> Fields, uint64_t Size,
-                                uint64_t Align,
+  const UnionType *getUnionType(ArrayRef<FieldInfo> Fields, TypeSize Size,
+                                Align Align,
                                 StructPacking Pack = StructPacking::Default) {
     FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
 
@@ -279,22 +252,6 @@ class TypeBuilder {
     return new (Allocator.Allocate<UnionType>()) UnionType(
         FieldArray, static_cast<uint32_t>(Fields.size()), Size, Align, Pack);
   }
-
-  const FunctionType *getFunctionType(const Type *ReturnType,
-                                      ArrayRef<const Type *> ParamTypes,
-                                      bool IsVarArg,
-                                      CallConv CC = CallConv::C) {
-    const Type **ParamArray =
-        Allocator.Allocate<const Type *>(ParamTypes.size());
-
-    for (size_t I = 0; I < ParamTypes.size(); ++I) {
-      ParamArray[I] = ParamTypes[I];
-    }
-
-    return new (Allocator.Allocate<FunctionType>())
-        FunctionType(ReturnType, ParamArray,
-                     static_cast<uint32_t>(ParamTypes.size()), IsVarArg, CC);
-  }
 };
 
 } // namespace abi
diff --git a/llvm/lib/ABI/CMakeLists.txt b/llvm/lib/ABI/CMakeLists.txt
new file mode 100644
index 0000000000000..d6aa9b542cc05
--- /dev/null
+++ b/llvm/lib/ABI/CMakeLists.txt
@@ -0,0 +1,17 @@
+add_llvm_component_library(LLVMABI
+  QualTypeMapper.cpp
+
+  ADDITIONAL_HEADER_DIRS
+  ${LLVM_MAIN_INCLUDE_DIR}/llvm/ABI
+
+  DEPENDS
+  intrinsics_gen
+
+  LINK_COMPONENTS
+  Core
+  Support
+)
+
+target_include_directories(LLVMABI PRIVATE
+  ${LLVM_MAIN_INCLUDE_DIR}
+)
diff --git a/llvm/lib/ABI/QualTypeMapper.cpp b/llvm/lib/ABI/QualTypeMapper.cpp
new file mode 100644
index 0000000000000..2a72286c5c9e1
--- /dev/null
+++ b/llvm/lib/ABI/QualTypeMapper.cpp
@@ -0,0 +1,18 @@
+//===-- llvm/ABI/QualTypeMapper.cpp - QualType to ABI Mapping ---------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This File contains the logic for converting clang::Qualtype to 
+// llvm::abi::Type for ABI Lowering
+//
+//===----------------------------------------------------------------------===//
+
+
+#include <llvm/ABI/QualTypeMapper.h>
+
+
+// TODO: Implementation of Qualtype -> abi::Type Mapping
diff --git a/llvm/lib/CMakeLists.txt b/llvm/lib/CMakeLists.txt
index f6465612d30c0..b888f6637a925 100644
--- a/llvm/lib/CMakeLists.txt
+++ b/llvm/lib/CMakeLists.txt
@@ -3,6 +3,7 @@ include(LLVM-Build)
 # `Demangle', `Support' and `TableGen' libraries are added on the top-level
 # CMakeLists.txt
 
+add_subdirectory(ABI)
 add_subdirectory(IR)
 add_subdirectory(FuzzMutate)
 add_subdirectory(FileCheck)



More information about the llvm-commits mailing list