[clang] [llvm] [WIP] ABI Lowering Library (PR #140112)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 9 05:06:49 PDT 2025
================
@@ -0,0 +1,531 @@
+//===- ABI/Types.h ----------------------------------------------*- 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
+/// This file defines the Types and related helper methods concerned to the
+/// LLVMABI library which mirrors ABI related type information from
+/// the LLVM frontend.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_ABI_TYPES_H
+#define LLVM_ABI_TYPES_H
+
+#include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Alignment.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/TypeSize.h"
+#include <algorithm>
+#include <cstdint>
+
+namespace llvm {
+namespace abi {
+
+enum class TypeKind {
+ Void,
+ MemberPointer,
+ Complex,
+ Integer,
+ Float,
+ Pointer,
+ Array,
+ Vector,
+ Record,
+};
+
+class Type {
+private:
+ TypeSize getTypeStoreSize() const {
+ TypeSize StoreSizeInBits = getTypeStoreSizeInBits();
+ return {StoreSizeInBits.getKnownMinValue() / 8,
+ StoreSizeInBits.isScalable()};
+ }
+ TypeSize getTypeStoreSizeInBits() const {
+ TypeSize BaseSize = getSizeInBits();
+ uint64_t AlignedSizeInBits =
+ alignToPowerOf2(BaseSize.getKnownMinValue(), 8);
+ return {AlignedSizeInBits, BaseSize.isScalable()};
+ }
+
+protected:
+ TypeKind Kind;
+ TypeSize SizeInBits;
+ Align ABIAlignment;
+
+ Type(TypeKind K, TypeSize SizeInBits, Align ABIAlign)
+ : Kind(K), SizeInBits(SizeInBits), ABIAlignment(ABIAlign) {}
+
+public:
+ TypeKind getKind() const { return Kind; }
+ TypeSize getSizeInBits() const { return SizeInBits; }
+ Align getAlignment() const { return ABIAlignment; }
+
+ TypeSize getTypeAllocSize() const {
+ return alignTo(getTypeStoreSize(), getAlignment().value());
+ }
+
+ 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 isRecord() const { return Kind == TypeKind::Record; }
+ bool isMemberPointer() const { return Kind == TypeKind::MemberPointer; }
+ bool isComplex() const { return Kind == TypeKind::Complex; }
+};
+
+class VoidType : public Type {
+public:
+ VoidType() : Type(TypeKind::Void, TypeSize::getFixed(0), Align(1)) {}
+
+ static bool classof(const Type *T) { return T->getKind() == TypeKind::Void; }
+};
+
+class ComplexType : public Type {
+public:
+ ComplexType(const Type *ElementType, uint64_t SizeInBits, Align Alignment)
+ : Type(TypeKind::Complex, TypeSize::getFixed(SizeInBits), Alignment),
+ ElementType(ElementType) {}
+
+ const Type *getElementType() const { return ElementType; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Complex;
+ }
+
+private:
+ const Type *ElementType;
+};
+
+class IntegerType : public Type {
+private:
+ bool IsSigned;
+ bool IsBitInt;
+
+public:
+ IntegerType(uint64_t BitWidth, Align ABIAlign, bool IsSigned,
+ bool IsBitInt = false)
+ : Type(TypeKind::Integer, TypeSize::getFixed(BitWidth), ABIAlign),
+ IsSigned(IsSigned), IsBitInt(IsBitInt) {}
+
+ bool isSigned() const { return IsSigned; }
+ bool isBitInt() const { return IsBitInt; }
+ bool isBool() const {
+ return getSizeInBits().getFixedValue() == 1 && !IsBitInt;
+ }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Integer;
+ }
+};
+
+class FloatType : public Type {
+private:
+ const fltSemantics *Semantics;
+
+public:
+ FloatType(const fltSemantics &FloatSemantics, Align ABIAlign)
+ : Type(TypeKind::Float,
+ TypeSize::getFixed(APFloat::getSizeInBits(FloatSemantics)),
+ ABIAlign),
+ Semantics(&FloatSemantics) {}
+
+ const fltSemantics *getSemantics() const { return Semantics; }
+ static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
+};
+
+class PointerLikeType : public Type {
+protected:
+ unsigned AddrSpace;
+ PointerLikeType(TypeKind K, TypeSize SizeInBits, Align ABIAlign, unsigned AS)
+ : Type(K, SizeInBits, ABIAlign), AddrSpace(AS) {}
+
+public:
+ unsigned getAddrSpace() const { return AddrSpace; }
+ bool isMemberPointer() const { return getKind() == TypeKind::MemberPointer; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Pointer ||
+ T->getKind() == TypeKind::MemberPointer;
+ }
+};
+
+class PointerType : public PointerLikeType {
+public:
+ PointerType(uint64_t Size, Align ABIAlign, unsigned AddressSpace = 0)
+ : PointerLikeType(TypeKind::Pointer, TypeSize::getFixed(Size), ABIAlign,
+ AddressSpace) {}
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Pointer;
+ }
+};
+
+class MemberPointerType : public PointerLikeType {
+private:
+ bool IsFunctionPointer;
+
+public:
+ MemberPointerType(bool IsFunctionPointer, uint64_t SizeInBits, Align ABIAlign,
+ unsigned AddressSpace = 0)
+ : PointerLikeType(TypeKind::MemberPointer, TypeSize::getFixed(SizeInBits),
+ ABIAlign, AddressSpace),
+ IsFunctionPointer(IsFunctionPointer) {}
+ bool isFunctionPointer() const { return IsFunctionPointer; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::MemberPointer;
+ }
+};
+
+class ArrayType : public Type {
+private:
+ const Type *ElementType;
+ uint64_t NumElements;
+ bool IsMatrix;
+
+public:
+ ArrayType(const Type *ElementType, uint64_t NumElements, uint64_t SizeInBits,
+ bool IsMatrixType = false)
+ : Type(TypeKind::Array, TypeSize::getFixed(SizeInBits),
+ ElementType->getAlignment()),
+ ElementType(ElementType), NumElements(NumElements),
+ IsMatrix(IsMatrixType) {}
+
+ const Type *getElementType() const { return ElementType; }
+ uint64_t getNumElements() const { return NumElements; }
+ bool isMatrixType() const { return IsMatrix; }
+
+ static bool classof(const Type *T) { return T->getKind() == TypeKind::Array; }
+};
+
+class VectorType : public Type {
+private:
+ const Type *ElementType;
+ ElementCount NumElements;
+
+public:
+ VectorType(const Type *ElementType, ElementCount NumElements, Align ABIAlign)
+ : Type(TypeKind::Vector,
+ TypeSize(ElementType->getSizeInBits().getFixedValue() *
+ NumElements.getKnownMinValue(),
+ NumElements.isScalable()),
+ ABIAlign),
+ ElementType(ElementType), NumElements(NumElements) {}
+
+ const Type *getElementType() const { return ElementType; }
+ ElementCount getNumElements() const { return NumElements; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Vector;
+ }
+};
+
+struct FieldInfo {
+ const Type *FieldType;
+ uint64_t OffsetInBits;
+ uint64_t BitFieldWidth;
+ bool IsBitField;
+ bool IsUnnamedBitfield;
+
+ FieldInfo(const Type *FieldType, uint64_t OffsetInBits = 0,
+ bool IsBitField = false, uint64_t BitFieldWidth = 0,
+ bool IsUnnamedBitField = false)
+ : FieldType(FieldType), OffsetInBits(OffsetInBits),
+ BitFieldWidth(BitFieldWidth), IsBitField(IsBitField),
+ IsUnnamedBitfield(IsUnnamedBitField) {}
+};
+
+enum class StructPacking { Default, Packed, ExplicitPacking };
+
+class RecordType : public Type {
+private:
+ ArrayRef<FieldInfo> Fields;
+ ArrayRef<FieldInfo> BaseClasses;
+ ArrayRef<FieldInfo> VirtualBaseClasses;
+ StructPacking Packing;
+ bool CanPassInRegisters;
+ bool IsCoercedRecord;
+ bool IsUnion;
+ bool IsTransparent;
----------------
nikic wrote:
You can use something like `LLVM_PREFERRED_TYPE(bool) unsigned IsTransparent : 1;` etc to pack these into bits.
https://github.com/llvm/llvm-project/pull/140112
More information about the llvm-commits
mailing list