28#include "llvm/ABI/Types.h"
29#include "llvm/Support/Alignment.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/TypeSize.h"
41#define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId) \
42 case BuiltinType::Id: \
44#include "clang/Basic/AArch64ACLETypes.def"
54 return llvm::abi::VectorKind::SVEData;
56 return llvm::abi::VectorKind::SVEPredicate;
58 return llvm::abi::VectorKind::Generic;
79 auto It = TypeCache.find(QT);
80 if (It != TypeCache.end())
83 const llvm::abi::Type *
Result = convertTypeImpl(QT);
84 assert(
Result &&
"convertTypeImpl returned nullptr");
90const llvm::abi::Type *QualTypeMapper::convertTypeImpl(
QualType QT) {
94#define TYPE(Class, Base)
95#define ABSTRACT_TYPE(Class, Base)
96#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
97#define DEPENDENT_TYPE(Class, Base) case Type::Class:
98#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
99#include "clang/AST/TypeNodes.inc"
100 llvm::reportFatalInternalError(
101 "Non-canonical or dependent types should not reach ABI lowering");
107 case Type::LValueReference:
108 case Type::RValueReference:
109 return createPointerTypeForPointee(
111 case Type::ConstantArray:
112 case Type::ArrayParameter:
113 case Type::IncompleteArray:
114 case Type::VariableArray:
117 case Type::ExtVector:
127 return Builder.getAtomicType(
convertType(AT->getValueType()),
130 case Type::BlockPointer:
131 return createPointerTypeForPointee(ASTCtx.
VoidPtrTy);
133 return createOpenCLOpaqueType(QT.
getTypePtr());
134 case Type::ConstantMatrix: {
136 return Builder.getArrayType(
convertType(MT->getElementType()),
137 MT->getNumRows() * MT->getNumColumns(),
140 case Type::CooperativeMatrix: {
141 llvm::reportFatalInternalError(
142 "Cooperative Matrix type not supported in ABI lowering library");
144 case Type::MemberPointer:
148 return Builder.getIntegerType(BIT->getNumBits(), getTypeAlign(QT),
152 case Type::ObjCObject:
153 case Type::ObjCInterface:
154 case Type::ObjCObjectPointer:
157 return Builder.getPointerType(
161 case Type::OverflowBehavior:
164 case Type::DeducedTemplateSpecialization:
165 case Type::FunctionProto:
166 case Type::FunctionNoProto:
167 case Type::HLSLAttributedResource:
168 case Type::HLSLInlineSpirv:
169 llvm::reportFatalInternalError(
"Type not supported in ABI lowering");
171 llvm_unreachable(
"unhandled type class in convertTypeImpl");
177const llvm::abi::Type *
178QualTypeMapper::convertBuiltinType(
const BuiltinType *BT) {
181 switch (BT->getKind()) {
182 case BuiltinType::Void:
183 return Builder.getVoidType();
185 case BuiltinType::NullPtr:
187 return Builder.getPointerType(
192 case BuiltinType::Bool:
193 return Builder.getIntegerType(1, getTypeAlign(QT),
false,
196 case BuiltinType::MetaInfo:
197 llvm::reportFatalInternalError(
"std::meta::info is consteval-only type");
199 case BuiltinType::Char_S:
200 case BuiltinType::Char_U:
201 case BuiltinType::SChar:
202 case BuiltinType::UChar:
203 case BuiltinType::WChar_S:
204 case BuiltinType::WChar_U:
205 case BuiltinType::Char8:
206 case BuiltinType::Char16:
207 case BuiltinType::Char32:
208 case BuiltinType::Short:
209 case BuiltinType::UShort:
210 case BuiltinType::Int:
211 case BuiltinType::UInt:
212 case BuiltinType::Long:
213 case BuiltinType::ULong:
214 case BuiltinType::LongLong:
215 case BuiltinType::ULongLong:
216 case BuiltinType::Int128:
217 case BuiltinType::UInt128:
218 return Builder.getIntegerType(ASTCtx.getTypeSize(QT), getTypeAlign(QT),
219 BT->isSignedInteger(),
222 case BuiltinType::Half:
223 case BuiltinType::Float16:
224 case BuiltinType::BFloat16:
225 case BuiltinType::Float:
226 case BuiltinType::Double:
227 case BuiltinType::LongDouble:
228 case BuiltinType::Float128:
229 return Builder.getFloatType(ASTCtx.getFloatTypeSemantics(QT),
233 case BuiltinType::Ibm128:
234 llvm::reportFatalInternalError(
235 "IBM128 is not yet supported in the ABI lowering libary");
238 case BuiltinType::ShortAccum:
239 case BuiltinType::Accum:
240 case BuiltinType::LongAccum:
241 case BuiltinType::UShortAccum:
242 case BuiltinType::UAccum:
243 case BuiltinType::ULongAccum:
244 case BuiltinType::ShortFract:
245 case BuiltinType::Fract:
246 case BuiltinType::LongFract:
247 case BuiltinType::UShortFract:
248 case BuiltinType::UFract:
249 case BuiltinType::ULongFract:
250 case BuiltinType::SatShortAccum:
251 case BuiltinType::SatAccum:
252 case BuiltinType::SatLongAccum:
253 case BuiltinType::SatUShortAccum:
254 case BuiltinType::SatUAccum:
255 case BuiltinType::SatULongAccum:
256 case BuiltinType::SatShortFract:
257 case BuiltinType::SatFract:
258 case BuiltinType::SatLongFract:
259 case BuiltinType::SatUShortFract:
260 case BuiltinType::SatUFract:
261 case BuiltinType::SatULongFract:
262 llvm::reportFatalInternalError(
263 "Fixed Point types not yet implemented in the ABI lowering library");
266#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
267 case BuiltinType::Id:
268#include "clang/Basic/OpenCLImageTypes.def"
270#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) case BuiltinType::Id:
271#include "clang/Basic/OpenCLExtensionTypes.def"
272 case BuiltinType::OCLSampler:
273 case BuiltinType::OCLEvent:
274 case BuiltinType::OCLClkEvent:
275 case BuiltinType::OCLQueue:
276 case BuiltinType::OCLReserveID:
277 return createOpenCLOpaqueType(BT);
280 case BuiltinType::ObjCId:
281 case BuiltinType::ObjCClass:
282 case BuiltinType::ObjCSel:
283 return createPointerTypeForPointee(QT);
286#define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId) \
287 case BuiltinType::Id:
288#define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId) \
289 case BuiltinType::Id:
290#include "clang/Basic/AArch64ACLETypes.def"
291 return convertSVEBuiltinType(BT);
293 case BuiltinType::SveCount:
294 return Builder.getSveCountType(getTypeAlign(QT));
298 case BuiltinType::MFloat8:
299 return Builder.getOpaqueFloat8Type(getTypeAlign(QT));
302#define PPC_VECTOR_TYPE(Name, Id, Size) case BuiltinType::Id:
303#include "clang/Basic/PPCTypes.def"
304 llvm::reportFatalInternalError(
305 "PPC MMA types not yet supported in ABI lowering library");
306#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
307#include "clang/Basic/RISCVVTypes.def"
308 llvm::reportFatalInternalError(
309 "RISC-V vector types not yet supported in ABI lowering library");
310#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
311#include "clang/Basic/WebAssemblyReferenceTypes.def"
312 llvm::reportFatalInternalError(
"WebAssembly reference types not yet "
313 "supported in ABI lowering library");
314#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
315#include "clang/Basic/AMDGPUTypes.def"
316 llvm::reportFatalInternalError(
317 "AMDGPU types not yet supported in ABI lowering library");
318#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
319#include "clang/Basic/HLSLIntangibleTypes.def"
320 llvm::reportFatalInternalError(
321 "HLSL intangible types not yet Supported in ABI lowering library");
322#define HLSL_PACKED_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
323#include "clang/Basic/HLSLPackedTypes.def"
324 llvm::reportFatalInternalError(
325 "HLSL packed types not yet Supported in ABI lowering library");
326#define SPIRV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
327#include "clang/Basic/SPIRVTypes.def"
328 llvm::reportFatalInternalError(
329 "SPIR-V types not yet supported in ABI lowering library");
332#define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
333#define BUILTIN_TYPE(Id, SingletonId)
334#include "clang/AST/BuiltinTypes.def"
335 llvm::reportFatalInternalError(
336 "Placeholder type should not reach ABI lowering");
338 case BuiltinType::Dependent:
339 llvm::reportFatalInternalError(
340 "Dependent builtin type should not reach ABI lowering");
342 llvm_unreachable(
"unhandled builtin type kind in convertBuiltinType");
351const llvm::abi::Type *
352QualTypeMapper::convertArrayType(
const clang::ArrayType *AT) {
356 if (
const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
357 auto NumElements = CAT->getZExtSize();
358 return Builder.getArrayType(ElementType, NumElements, Size);
361 return Builder.getArrayType(ElementType, 0, 0);
362 if (
const auto *VAT = dyn_cast<VariableArrayType>(AT))
363 return createPointerTypeForPointee(VAT->getPointeeType());
364 llvm::reportFatalInternalError(
365 "unexpected array type in ABI lowering (dependent array types should be "
366 "resolved before reaching this point)");
369const llvm::abi::Type *QualTypeMapper::convertVectorType(
const VectorType *VT) {
372 const llvm::abi::Type *ElementType =
373 VT->getElementType()->isMFloat8Type()
374 ? Builder.getIntegerType(8, llvm::Align(1),
false)
376 QualType VectorQualType(VT, 0);
378 unsigned NElems = VT->getNumElements();
379 llvm::ElementCount NumElements = llvm::ElementCount::getFixed(NElems);
380 llvm::Align VectorAlign = getTypeAlign(VectorQualType);
386 return Builder.getVectorType(ElementType, NumElements, VectorAlign,
396const llvm::abi::Type *
397QualTypeMapper::convertSVEBuiltinType(
const BuiltinType *BT) {
398 ASTContext::BuiltinVectorTypeInfo Info = ASTCtx.getBuiltinVectorTypeInfo(BT);
400 "Expected 1, 2, 3 or 4 vectors!");
405 const llvm::abi::Type *ElementType =
407 ? Builder.getIntegerType(8, llvm::Align(1),
false)
411 ? llvm::abi::VectorKind::SVEPredicate
412 : llvm::abi::VectorKind::SVEData;
414 const llvm::abi::VectorType *VecTy = Builder.getVectorType(
415 ElementType, Info.
EC, getTypeAlign(QualType(BT, 0)), VecKind);
418 return Builder.getTupleType(VecTy, Info.
NumVectors);
427const llvm::abi::Type *
428QualTypeMapper::convertComplexType(
const ComplexType *CT) {
429 const llvm::abi::Type *ElementType =
convertType(CT->getElementType());
430 llvm::Align ComplexAlign = getTypeAlign(QualType(CT, 0));
432 return Builder.getComplexType(ElementType, ComplexAlign);
441const llvm::abi::Type *
442QualTypeMapper::convertMemberPointerType(
const clang::MemberPointerType *MPT) {
445 llvm::Align Align = getTypeAlign(QT);
449 return Builder.getMemberPointerType(IsFunctionPointer, Size, Align);
454 llvm::abi::RecordFlags RecFlags = llvm::abi::RecordFlags::IsCXXRecord;
456 RecFlags |= llvm::abi::RecordFlags::IsPolymorphic;
458 RecFlags |= llvm::abi::RecordFlags::IsCXXEmpty;
460 RecFlags |= llvm::abi::RecordFlags::HasNonTrivialCopyAssignment;
462 RecFlags |= llvm::abi::RecordFlags::HasNonTrivialDestructor;
464 RecFlags |= llvm::abi::RecordFlags::HasNonTrivialDefaultConstructor;
474const llvm::abi::Type *QualTypeMapper::convertRecordType(
const RecordType *RT) {
477 return Builder.getRecordType({}, llvm::TypeSize::getFixed(0),
482 return convertUnionType(RD);
485 auto *CXXRd = dyn_cast<CXXRecordDecl>(RD);
486 if (CXXRd && (CXXRd->getNumBases() > 0 || CXXRd->getNumVBases() > 0))
487 return convertCXXRecordType(CXXRd);
488 return convertStructType(RD);
499const llvm::abi::RecordType *
500QualTypeMapper::convertCXXRecordType(
const CXXRecordDecl *RD) {
501 const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(RD);
502 SmallVector<llvm::abi::FieldInfo, 16> Fields;
503 SmallVector<llvm::abi::FieldInfo, 8> BaseClasses;
504 SmallVector<llvm::abi::FieldInfo, 8> VirtualBaseClasses;
507 if (RD->isPolymorphic()) {
508 const llvm::abi::Type *VtablePointer =
509 createPointerTypeForPointee(ASTCtx.VoidPtrTy);
510 Fields.emplace_back(VtablePointer, 0);
513 for (
const auto &Base : RD->bases()) {
514 if (
Base.isVirtual())
517 const RecordType *BaseRT =
Base.getType()->castAs<RecordType>();
520 Layout.getBaseClassOffset(BaseRT->getAsCXXRecordDecl()).getQuantity() *
522 BaseClasses.emplace_back(BaseType, BaseOffset);
525 for (
const auto &VBase : RD->vbases()) {
526 const RecordType *VBaseRT = VBase.getType()->castAs<RecordType>();
527 const llvm::abi::Type *VBaseType =
convertType(VBase.getType());
529 Layout.getVBaseClassOffset(VBaseRT->getAsCXXRecordDecl())
532 VirtualBaseClasses.emplace_back(VBaseType, VBaseOffset);
535 computeFieldInfo(RD, Fields, Layout);
538 [](
const llvm::abi::FieldInfo &A,
const llvm::abi::FieldInfo &B) {
539 return A.OffsetInBits < B.OffsetInBits;
542 llvm::TypeSize
Size =
543 llvm::TypeSize::getFixed(Layout.getSize().getQuantity() * 8);
544 llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity());
545 llvm::Align UnadjustedAlign =
546 llvm::Align(Layout.getUnadjustedAlignment().getQuantity());
549 if (RD->canPassInRegisters())
550 RecFlags |= llvm::abi::RecordFlags::CanPassInRegisters;
551 if (RD->hasFlexibleArrayMember())
552 RecFlags |= llvm::abi::RecordFlags::HasFlexibleArrayMember;
554 return Builder.getRecordType(Fields, Size, Alignment, UnadjustedAlign,
555 llvm::abi::StructPacking::Default, BaseClasses,
556 VirtualBaseClasses, RecFlags);
565const llvm::abi::Type *
566QualTypeMapper::convertEnumType(
const clang::EnumType *ET) {
567 const EnumDecl *ED = ET->getDecl();
568 QualType UnderlyingType = ED->getIntegerType();
570 if (UnderlyingType.isNull())
571 UnderlyingType = ASTCtx.IntTy;
582const llvm::abi::RecordType *
583QualTypeMapper::convertStructType(
const clang::RecordDecl *RD) {
584 const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(RD);
586 SmallVector<llvm::abi::FieldInfo, 16> Fields;
587 computeFieldInfo(RD, Fields, Layout);
589 llvm::TypeSize
Size =
590 llvm::TypeSize::getFixed(Layout.getSize().getQuantity() * 8);
591 llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity());
592 llvm::Align UnadjustedAlign =
593 llvm::Align(Layout.getUnadjustedAlignment().getQuantity());
595 llvm::abi::RecordFlags RecFlags = llvm::abi::RecordFlags::None;
596 if (
const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
599 RecFlags |= llvm::abi::RecordFlags::CanPassInRegisters;
601 RecFlags |= llvm::abi::RecordFlags::HasFlexibleArrayMember;
603 return Builder.getRecordType(Fields, Size, Alignment, UnadjustedAlign,
604 llvm::abi::StructPacking::Default, {}, {},
614const llvm::abi::RecordType *
615QualTypeMapper::convertUnionType(
const clang::RecordDecl *RD) {
616 const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(RD);
618 SmallVector<llvm::abi::FieldInfo, 16> AllFields;
619 computeFieldInfo(RD, AllFields, Layout);
621 llvm::TypeSize
Size =
622 llvm::TypeSize::getFixed(Layout.getSize().getQuantity() * 8);
623 llvm::Align Alignment = llvm::Align(Layout.getAlignment().getQuantity());
624 llvm::Align UnadjustedAlign =
625 llvm::Align(Layout.getUnadjustedAlignment().getQuantity());
627 llvm::abi::RecordFlags RecFlags = llvm::abi::RecordFlags::None;
628 if (RD->
hasAttr<TransparentUnionAttr>())
629 RecFlags |= llvm::abi::RecordFlags::IsTransparent;
631 RecFlags |= llvm::abi::RecordFlags::CanPassInRegisters;
632 if (
const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
635 RecFlags |= llvm::abi::RecordFlags::HasFlexibleArrayMember;
637 return Builder.getUnionType(AllFields, Size, Alignment, UnadjustedAlign,
638 llvm::abi::StructPacking::Default, RecFlags);
641llvm::Align QualTypeMapper::getTypeAlign(QualType QT)
const {
643 return llvm::Align(ASTCtx.getTypeAlignInChars(QT).getQuantity());
646const llvm::abi::Type *
647QualTypeMapper::createOpenCLOpaqueType(
const clang::Type *T) {
650 LangAS AddrSpace = ASTCtx.getOpenCLTypeAddrSpace(T);
651 const clang::TargetInfo &TI = ASTCtx.getTargetInfo();
657const llvm::abi::Type *
658QualTypeMapper::createPointerTypeForPointee(QualType PointeeType) {
659 auto AddrSpace = PointeeType.getAddressSpace();
660 auto PointerSize = ASTCtx.getTargetInfo().getPointerWidth(AddrSpace);
661 llvm::Align Alignment =
662 llvm::Align(ASTCtx.getTargetInfo().getPointerAlign(AddrSpace));
666 unsigned TargetAddrSpace =
667 PointeeType->isFunctionType() && !PointeeType.hasAddressSpace()
668 ? DL.getProgramAddressSpace()
669 : ASTCtx.getTargetInfo().getTargetAddressSpace(AddrSpace);
672 return Builder.getPointerType(
673 PointerSize, llvm::Align(Alignment.value() / 8), TargetAddrSpace,
674 PointeeType.hasAddressSpace() || TargetAddrSpace != 0);
684void QualTypeMapper::computeFieldInfo(
685 const RecordDecl *RD, SmallVectorImpl<llvm::abi::FieldInfo> &Fields,
686 const ASTRecordLayout &Layout) {
687 unsigned FieldIndex = 0;
689 for (
const auto *FD : RD->fields()) {
690 const llvm::abi::Type *FieldType =
convertType(FD->getType());
691 uint64_t OffsetInBits = Layout.getFieldOffset(FieldIndex);
693 bool IsBitField = FD->isBitField();
695 bool IsUnnamedBitField =
false;
698 BitFieldWidth = FD->getBitWidthValue();
699 IsUnnamedBitField = FD->isUnnamedBitField();
702 bool HasNoUniqueAddress = FD->hasAttr<NoUniqueAddressAttr>();
703 Fields.emplace_back(FieldType, OffsetInBits, IsBitField, BitFieldWidth,
704 IsUnnamedBitField, HasNoUniqueAddress);
Defines the clang::ASTContext interface.
Forward declaration of all AST node types.
Provides definitions for the various language-specific address spaces.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Maps Clang QualType instances to corresponding LLVM ABI type representations.
static QualType getUnderlyingType(const SubRegion *R)
static QualType getPointeeType(const MemRegion *R)
C Language Family Type Representation.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
QualType getElementType() const
This class is used for builtin types like 'int'.
Represents a C++ struct/union/class.
bool hasNonTrivialCopyAssignment() const
Determine whether this class has a non-trivial copy assignment operator (C++ [class....
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
bool isPolymorphic() const
Whether this class is polymorphic (C++ [class.virtual]), which means that the class contains or inher...
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
bool hasNonTrivialDefaultConstructor() const
Determine whether this class has a non-trivial default constructor (C++11 [class.ctor]p5).
const llvm::abi::Type * convertType(clang::QualType QT)
Main entry point for converting Clang QualType to LLVM ABI Type.
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
QualType getCanonicalType() const
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Represents a struct/union/class.
bool canPassInRegisters() const
Determine whether this class can be passed in registers.
bool hasFlexibleArrayMember() const
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
uint64_t getPointerWidth(LangAS AddrSpace) const
Return the width of pointers on this target, for the specified address space.
unsigned getTargetAddressSpace(LangAS AS) const
uint64_t getPointerAlign(LangAS AddrSpace) const
bool isMemberFunctionPointerType() const
TypeClass getTypeClass() const
Defines the clang::TargetInfo interface.
static llvm::abi::RecordFlags getCXXRecordFlags(const CXXRecordDecl *RD)
Returns the record flags that describe C++ class properties of RD.
static llvm::abi::VectorKind getABIVectorKind(clang::VectorKind Kind)
Maps a Clang vector kind onto the ABI library's notion of a vector flavor.
static bool isSVEPredicateBuiltinType(const BuiltinType *BT)
Returns true if BT is one of the AArch64 SVE predicate types, i.e.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
@ Result
The result type of a method or function.
LangAS
Defines the address space values used by the address space qualifier of QualType.
@ SveFixedLengthData
is AArch64 SVE fixed-length data vector
@ SveFixedLengthPredicate
is AArch64 SVE fixed-length predicate vector
U cast(CodeGen::Address addr)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t