7#include "mlir/IR/BuiltinTypes.h"
24 builder(cgm.
getBuilder()), theCXXABI(cgm.getCXXABI()),
25 theABIInfo(cgm.getTargetCIRGenInfo().
getABIInfo()) {}
28 for (
auto i = functionInfos.begin(), e = functionInfos.end(); i != e;)
33 return *builder.getContext();
53 return !
tagType->isIncompleteType();
65 if (
const auto *fpt = dyn_cast<FunctionProtoType>(ft))
66 for (
unsigned i = 0, e = fpt->getNumParams(); i != e; i++)
73mlir::Type CIRGenTypes::convertFunctionTypeInternal(
QualType qft) {
86 if (
const auto *fpt = dyn_cast<FunctionProtoType>(ft)) {
105 llvm::raw_svector_ostream outStream(typeName);
110 policy.AlwaysIncludeTypeForTemplateArgument =
true;
111 policy.PrintAsCanonical =
true;
116 .
print(outStream, policy);
120 outStream << builder.getUniqueAnonRecordName();
125 return builder.getUniqueRecordName(std::string(typeName));
130 const auto it = recordDeclTypes.find(ty);
131 return it != recordDeclTypes.end() && it->second.isComplete();
138 llvm::SmallPtrSetImpl<const RecordDecl *> &alreadyChecked);
145 llvm::SmallPtrSetImpl<const RecordDecl *> &alreadyChecked) {
148 if (!alreadyChecked.insert(rd).second)
152 "Expect RecordDecl to be CompleteDefinition");
173 if (
const CXXRecordDecl *crd = dyn_cast<CXXRecordDecl>(rd)) {
176 ->castAs<RecordType>()
178 ->getDefinitionOrSelf(),
179 cgt, alreadyChecked))
201 llvm::SmallPtrSetImpl<const RecordDecl *> &alreadyChecked) {
204 qt = at->getValueType();
237 return astContext.getTypeSize(
type) !=
238 astContext.getTypeSize(
type->getValueType());
245 const Type *key = astContext.getCanonicalTagType(rd).getTypePtr();
253 entry = builder.getIncompleteRecordTy(name, rd);
254 recordDeclTypes[key] = entry;
263 deferredRecords.push_back(rd);
268 bool insertResult = recordsBeingLaidOut.insert(key).second;
270 assert(insertResult &&
"isSafeToCovert() should have caught this.");
273 safeToConvertCache.clear();
276 if (
const auto *
cxxRecordDecl = dyn_cast<CXXRecordDecl>(rd)) {
278 if (base.isVirtual())
286 recordDeclTypes[key] = entry;
287 cirGenRecordLayouts[key] = std::move(layout);
290 bool eraseResult = recordsBeingLaidOut.erase(key);
292 assert(eraseResult &&
"record not in RecordsBeingLaidOut set?");
295 safeToConvertCache.clear();
299 if (recordsBeingLaidOut.empty())
300 while (!deferredRecords.empty())
307 type = astContext.getCanonicalType(
type);
310 if (astContext.getLangOpts().CUDAIsDevice) {
311 if (
type->isCUDADeviceBuiltinSurfaceType()) {
313 cgm.getTargetCIRGenInfo().getCUDADeviceBuiltinSurfaceDeviceType())
315 }
else if (
type->isCUDADeviceBuiltinTextureType()) {
317 cgm.getTargetCIRGenInfo().getCUDADeviceBuiltinTextureDeviceType())
329 TypeCacheTy::iterator tci =
typeCache.find(ty);
336 mlir::Type resultType =
nullptr;
339 llvm_unreachable(
"Should have been handled above");
341 case Type::Builtin: {
344 case BuiltinType::Void:
345 resultType = cgm.voidTy;
349 case BuiltinType::Bool:
354 case BuiltinType::Char_S:
355 case BuiltinType::Int:
356 case BuiltinType::Int128:
357 case BuiltinType::Long:
358 case BuiltinType::LongLong:
359 case BuiltinType::SChar:
360 case BuiltinType::Short:
361 case BuiltinType::WChar_S:
362 case BuiltinType::Accum:
363 case BuiltinType::Fract:
364 case BuiltinType::LongAccum:
365 case BuiltinType::LongFract:
366 case BuiltinType::ShortAccum:
367 case BuiltinType::ShortFract:
369 case BuiltinType::SatAccum:
370 case BuiltinType::SatFract:
371 case BuiltinType::SatLongAccum:
372 case BuiltinType::SatLongFract:
373 case BuiltinType::SatShortAccum:
374 case BuiltinType::SatShortFract:
381 case BuiltinType::SveInt8:
383 cir::VectorType::get(builder.getSInt8Ty(), 16,
true);
385 case BuiltinType::SveUint8:
387 cir::VectorType::get(builder.getUInt8Ty(), 16,
true);
389 case BuiltinType::SveInt16:
391 cir::VectorType::get(builder.getSInt16Ty(), 8,
true);
393 case BuiltinType::SveUint16:
395 cir::VectorType::get(builder.getUInt16Ty(), 8,
true);
397 case BuiltinType::SveFloat16:
398 resultType = cir::VectorType::get(builder.getFp16Ty(), 8,
401 case BuiltinType::SveBFloat16:
402 resultType = cir::VectorType::get(builder.getBfloat16Ty(), 8,
405 case BuiltinType::SveInt32:
407 cir::VectorType::get(builder.getSInt32Ty(), 4,
true);
409 case BuiltinType::SveUint32:
411 cir::VectorType::get(builder.getUInt32Ty(), 4,
true);
413 case BuiltinType::SveFloat32:
414 resultType = cir::VectorType::get(builder.getSingleTy(), 4,
417 case BuiltinType::SveInt64:
419 cir::VectorType::get(builder.getSInt64Ty(), 2,
true);
421 case BuiltinType::SveUint64:
423 cir::VectorType::get(builder.getUInt64Ty(), 2,
true);
425 case BuiltinType::SveFloat64:
426 resultType = cir::VectorType::get(builder.getDoubleTy(), 2,
429 case BuiltinType::SveBool:
430 resultType = cir::VectorType::get(builder.getUIntNTy(1), 16,
435 case BuiltinType::Char8:
436 case BuiltinType::Char16:
437 case BuiltinType::Char32:
438 case BuiltinType::Char_U:
439 case BuiltinType::UChar:
440 case BuiltinType::UInt:
441 case BuiltinType::UInt128:
442 case BuiltinType::ULong:
443 case BuiltinType::ULongLong:
444 case BuiltinType::UShort:
445 case BuiltinType::WChar_U:
446 case BuiltinType::UAccum:
447 case BuiltinType::UFract:
448 case BuiltinType::ULongAccum:
449 case BuiltinType::ULongFract:
450 case BuiltinType::UShortAccum:
451 case BuiltinType::UShortFract:
453 case BuiltinType::SatUAccum:
454 case BuiltinType::SatUFract:
455 case BuiltinType::SatULongAccum:
456 case BuiltinType::SatULongFract:
457 case BuiltinType::SatUShortAccum:
458 case BuiltinType::SatUShortFract:
465 case BuiltinType::Float16:
466 resultType = cgm.fP16Ty;
468 case BuiltinType::Half:
469 resultType = cgm.fP16Ty;
471 case BuiltinType::BFloat16:
472 resultType = cgm.bFloat16Ty;
474 case BuiltinType::MFloat8:
475 resultType = cgm.uInt8Ty;
477 case BuiltinType::Float:
478 assert(&astContext.getFloatTypeSemantics(
type) ==
479 &llvm::APFloat::IEEEsingle() &&
480 "ClangIR NYI: 'float' in a format other than IEEE 32-bit");
481 resultType = cgm.floatTy;
483 case BuiltinType::Double:
484 assert(&astContext.getFloatTypeSemantics(
type) ==
485 &llvm::APFloat::IEEEdouble() &&
486 "ClangIR NYI: 'double' in a format other than IEEE 64-bit");
487 resultType = cgm.doubleTy;
489 case BuiltinType::LongDouble:
491 builder.getLongDoubleTy(astContext.getFloatTypeSemantics(
type));
493 case BuiltinType::Float128:
494 resultType = cgm.fP128Ty;
496 case BuiltinType::Ibm128:
498 resultType = cgm.sInt32Ty;
501 case BuiltinType::NullPtr:
506 resultType = builder.getVoidPtrTy();
509#define AMDGPU_OPAQUE_PTR_TYPE(Name, Id, SingletonId, Width, Align, AS) \
510 case BuiltinType::Id: { \
511 if (BuiltinType::Id == BuiltinType::AMDGPUTexture) { \
512 resultType = cir::VectorType::get(builder.getSInt32Ty(), 8); \
514 resultType = builder.getPointerTo( \
516 cir::TargetAddressSpaceAttr::get(&getMLIRContext(), AS)); \
520#define AMDGPU_NAMED_BARRIER_TYPE(Name, Id, SingletonId, Width, Align, Scope) \
521 case BuiltinType::Id: \
522 llvm_unreachable("NYI");
523#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) \
524 case BuiltinType::Id: \
525 llvm_unreachable("NYI");
526#include "clang/Basic/AMDGPUTypes.def"
530 resultType = cgm.sInt32Ty;
536 case Type::Complex: {
538 mlir::Type elementTy =
convertType(ct->getElementType());
539 resultType = cir::ComplexType::get(elementTy);
543 case Type::LValueReference:
544 case Type::RValueReference: {
550 assert(resultType &&
"Cannot get pointer type?");
554 case Type::Pointer: {
564 case Type::VariableArray: {
574 case Type::IncompleteArray: {
583 elemTy = cgm.sInt8Ty;
586 resultType = cir::ArrayType::get(elemTy, 0);
590 case Type::ConstantArray: {
598 resultType = cir::ArrayType::get(elemTy, arrTy->
getSize().getZExtValue());
602 case Type::ExtVector:
610 case Type::ConstantMatrix: {
620 if (
auto integerType = ed->getIntegerType(); !integerType.isNull())
625 resultType = cgm.uInt32Ty;
629 case Type::MemberPointer: {
633 auto clsTy = mlir::cast<cir::RecordType>(
635 if (mpt->isMemberDataPointer()) {
636 mlir::Type memberTy =
convertType(mpt->getPointeeType());
637 resultType = cir::DataMemberType::get(memberTy, clsTy);
639 auto memberFuncTy =
getFunctionType(cgm.getTypes().arrangeCXXMethodType(
643 resultType = cir::MethodType::get(memberFuncTy, clsTy);
648 case Type::FunctionNoProto:
649 case Type::FunctionProto:
650 resultType = convertFunctionTypeInternal(
type);
655 unsigned numBits = bitIntTy->getNumBits();
656 assert(numBits <= cir::IntType::maxBitwidth() &&
657 "_BitInt width exceeds CIR IntType maximum");
659 cir::IntType::get(&
getMLIRContext(), numBits, bitIntTy->isSigned(),
669 uint64_t valueSize = astContext.getTypeSize(valueType);
670 uint64_t atomicSize = astContext.getTypeSize(ty);
671 if (valueSize != atomicSize) {
672 assert(valueSize < atomicSize);
674 cir::ArrayType::get(cgm.sInt8Ty, (atomicSize - valueSize) / 8);
675 mlir::Type elements[] = {resultType, paddingArray};
676 cir::RecordMemberKind kinds[] = {cir::RecordMemberKind::Data,
677 cir::RecordMemberKind::Pad};
680 false,
false, kinds);
688 type->getTypeClassName());
689 resultType = cgm.sInt32Ty;
693 assert(resultType &&
"Type conversion not yet implemented");
701 if (astContext.getLangOpts().HLSL &&
qualType->isConstantMatrixType()) {
703 cgm.errorNYI(
"convertTypeForMem: HLSL & Matrix row major");
709 assert(!forBitField &&
"Bit fields NYI");
713 if (forBitField &&
qualType->isBitIntType())
714 assert(!
qualType->isBitIntType() &&
"Bit field with type _BitInt NYI");
716 return convertedType;
722 const auto *key = astContext.getCanonicalTagType(rd).getTypePtr();
725 auto it = cirGenRecordLayouts.find(key);
726 if (it != cirGenRecordLayouts.end())
733 it = cirGenRecordLayouts.find(key);
735 assert(it != cirGenRecordLayouts.end() &&
736 "Unable to find record layout information for type");
742 return astContext.getTargetNullPointerValue(t) == 0;
744 if (
const auto *at = astContext.getAsArrayType(t)) {
748 if (
const auto *cat = dyn_cast<ConstantArrayType>(at))
749 if (astContext.getConstantArrayElementCount(cat) == 0)
752 t = astContext.getBaseElementType(t);
759 return theCXXABI.isZeroInitializable(mpt);
761 if (t->
getAs<HLSLInlineSpirvType>())
763 "isZeroInitializable for HLSLInlineSpirvType");
777 if (cgm.getTriple().isSPIROrSPIRV())
778 return cir::CallingConv::SpirFunction;
779 return cir::CallingConv::C;
781 return cgm.getTargetCIRGenInfo().getDeviceKernelCallingConv();
786 return cir::CallingConv::C;
804 if (
const CXXRecordDecl *cxxRD = dyn_cast<CXXRecordDecl>(rd)) {
820 assert(llvm::all_of(argTypes,
825 if (containsAtomic(returnType) || llvm::any_of(argTypes, containsAtomic))
826 cgm.errorNYI(
"passing or returning atomic types");
828 llvm::FoldingSetNodeID id;
832 llvm::FoldingSetInsertToken insertToken;
840 "Bad match based on CIRGenFunctionInfo folding set id");
849 functionInfos.insert(fi, insertToken);
858 for (
const VarDecl *arg : args)
859 argTypes.push_back(astContext.getCanonicalParamType(arg->getType()));
869 assert(!dyn_cast<ObjCMethodDecl>(gd.
getDecl()) &&
870 "This is reported as a FIXME in LLVM codegen");
886 if ([[maybe_unused]]
const auto *ed = dyn_cast<EnumDecl>(td)) {
894 ed->getASTContext().getCanonicalTagType(ed)->getTypePtr()) ||
896 typeCache[ed->getASTContext().getCanonicalTagType(ed)->getTypePtr()]));
906 if (rd->isDependentType())
911 if (recordDeclTypes.count(astContext.getCanonicalTagType(rd).getTypePtr()))
919mlir::ptr::MemorySpaceAttrInterface
933 return cir::TargetAddressSpaceAttr::get(&
getMLIRContext(), targetAS);
942 ? cgm.getDataLayout().getProgramAddressSpace()
Defines the clang::ASTContext interface.
static bool typeContainsAtomicForABI(QualType ty, ASTContext &ctx)
Whether a by-value ABI type is _Atomic or contains an _Atomic member.
static bool isSafeToConvert(QualType qt, CIRGenTypes &cgt, llvm::SmallPtrSetImpl< const RecordDecl * > &alreadyChecked)
Return true if it is safe to convert this field type, which requires the record elements contained by...
static Decl::Kind getKind(const Decl *D)
Defines AST-level helper utilities for matrix types.
C Language Family Type Representation.
C++ view class that accepts both !cir.struct and !cir.union types.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
CanQualType getCanonicalTagType(const TagDecl *TD) const
unsigned getTargetAddressSpace(LangAS AS) const
Represents an array type, per C99 6.7.5.2 - Array Declarators.
QualType getElementType() const
unsigned getIndexTypeCVRQualifiers() const
CanQualType getReturnType() const
const_arg_iterator argTypesEnd() const
static CIRGenFunctionInfo * create(cir::CallingConv cirCC, FunctionType::ExtInfo info, bool instanceMethod, CanQualType resultType, llvm::ArrayRef< CanQualType > argTypes, RequiredArgs required)
static void Profile(llvm::FoldingSetNodeID &id, bool instanceMethod, FunctionType::ExtInfo info, RequiredArgs required, CanQualType resultType, llvm::ArrayRef< CanQualType > argTypes)
const_arg_iterator argTypesBegin() const
This class organizes the cross-function state that is used while generating CIR code.
bool isPaddedAtomicType(QualType type)
This class handles record and union layout info while lowering AST types to CIR types.
bool isZeroInitializable() const
Check whether this struct can be C++ zero-initialized with a zeroinitializer.
This class organizes the cross-module state that is used while lowering AST types to CIR types.
cir::CallingConv clangCallConvToCIRCallConv(clang::CallingConv cc)
Convert a clang calling convention to a CIR calling convention.
const CIRGenFunctionInfo & arrangeGlobalDeclaration(GlobalDecl gd)
unsigned getTargetAddressSpace(QualType ty) const
const CIRGenFunctionInfo & arrangeCXXStructorDeclaration(clang::GlobalDecl gd)
const CIRGenFunctionInfo & arrangeCIRFunctionInfo(CanQualType returnType, bool isInstanceMethod, llvm::ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, RequiredArgs required)
const CIRGenFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > fpt)
bool isZeroInitializable(clang::QualType ty)
Return whether a type can be zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
bool isFuncTypeConvertible(const clang::FunctionType *ft)
Utility to check whether a function type can be converted to a CIR type (i.e.
CIRGenTypes(CIRGenModule &cgm)
bool isRecordBeingLaidOut(const clang::Type *ty) const
mlir::ptr::MemorySpaceAttrInterface getPointerAddressSpace(clang::QualType pointeeTy) const
Returns the CIR address space for a pointer/reference to pointeeTy, or a null attribute for the defau...
CIRGenBuilderTy & getBuilder() const
mlir::MLIRContext & getMLIRContext() const
bool isCachedSafeToConvert(const clang::Type *key) const
Check if a record type key is in the safe-to-convert cache.
cir::FuncType getFunctionType(const CIRGenFunctionInfo &info)
Get the CIR function type for.
bool isFuncParamTypeConvertible(clang::QualType type)
Return true if the specified type in a function parameter or result position can be converted to a CI...
void updateCompletedType(const clang::TagDecl *td)
UpdateCompletedType - when we find the full definition for a TagDecl, replace the 'opaque' type we pr...
std::string getRecordTypeName(const clang::RecordDecl *, llvm::StringRef suffix)
bool noRecordsBeingLaidOut() const
const ABIInfo & getABIInfo() const
const CIRGenFunctionInfo & arrangeFunctionDeclaration(const clang::FunctionDecl *fd)
Free functions are functions that are compatible with an ordinary C function pointer type.
clang::ASTContext & getASTContext() const
bool isRecordLayoutComplete(const clang::Type *ty) const
Return true if the specified type is already completely laid out.
const CIRGenFunctionInfo & arrangeDeviceKernelCallerDeclaration(clang::QualType resultType, const FunctionArgList &args)
Arrange the function info for a device kernel caller entry point (e.g.
mlir::Type convertType(clang::QualType type)
Convert a Clang type into a mlir::Type.
const CIRGenRecordLayout & getCIRGenRecordLayout(const clang::RecordDecl *rd)
Return record layout info for the given record decl.
std::unique_ptr< CIRGenRecordLayout > computeRecordLayout(const clang::RecordDecl *rd, cir::RecordType *ty)
mlir::Type convertRecordDeclType(const clang::RecordDecl *recordDecl)
Lay out a tagged decl type like struct or union.
void cacheSafeToConvert(const clang::Type *key)
Add a record type key to the safe-to-convert cache.
mlir::Type convertTypeForMem(clang::QualType, bool forBitField=false)
Convert type T into an mlir::Type.
Type for representing both the decl and type of parameters to a function.
A class for recording the number of arguments that a function signature requires.
Represents a base class of a C++ class.
Represents a C++ struct/union/class.
static CanQual< T > CreateUnsafe(QualType Other)
Builds a canonical type from a QualType.
bool isCanonicalAsParam() const
Determines if this canonical type is furthermore canonical as a parameter.
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Represents the canonical version of C arrays with a specified constant size.
llvm::APInt getSize() const
Return the constant array size as an APInt.
Represents a concrete matrix type with constant number of rows and columns.
unsigned getNumColumns() const
Returns the number of columns in the matrix.
unsigned getNumRows() const
Returns the number of rows in the matrix.
Represents a member of a struct/union/class.
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Represents a prototype with parameter type info, e.g.
A class which abstracts out some details necessary for making a call.
FunctionType - C99 6.7.5.3 - Function Declarators.
static StringRef getNameForCallConv(CallingConv CC)
QualType getReturnType() const
GlobalDecl - represents a global declaration.
const Decl * getDecl() const
Represents a C array with an unspecified size.
QualType getElementType() const
Returns type of the elements being stored in the matrix.
A pointer to member type per C++ 8.3.3 - Pointers to members.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
CXXRecordDecl * getAsRecordDecl() const
Retrieve the record declaration stored in this nested name specifier, or null.
const Type * getAsType() const
PointerType - C99 6.7.5.1 - Pointer Declarators.
QualType getPointeeType() const
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
LangAS getAddressSpace() const
Return the address space of this type.
void print(raw_ostream &OS, const PrintingPolicy &Policy, const Twine &PlaceHolder=Twine(), unsigned Indentation=0) const
QualType getCanonicalType() const
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
bool hasAddressSpace() const
Check if this type has any address space qualifier.
Represents a struct/union/class.
field_range fields() const
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Base for LValueReferenceType and RValueReferenceType.
QualType getPointeeType() const
Encodes a location in the source.
Represents the declaration of a struct/union/class/enum.
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
The base class of the type hierarchy.
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
CanQualType getCanonicalTypeUnqualified() const
bool isAtomicType() const
EnumDecl * castAsEnumDecl() const
bool isFunctionType() const
TypeClass getTypeClass() const
const T * getAs() const
Member-template getAs<specific type>'.
bool isNullPtrType() const
Represents a variable declaration or definition.
Represents a C array with a specified size that is not an integer-constant-expression.
Represents a GCC generic vector type.
unsigned getNumElements() const
QualType getElementType() const
Defines the clang::TargetInfo interface.
mlir::ptr::MemorySpaceAttrInterface toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS)
Convert an AST LangAS to the appropriate CIR address space attribute interface.
bool isSized(mlir::Type ty)
Returns true if the type is a CIR sized type.
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
const internal::VariadicDynCastAllOfMatcher< Decl, TypedefNameDecl > typedefNameDecl
Matches typedef name declarations.
const AstTypeMatcher< TagType > tagType
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< RecordType > recordType
const internal::VariadicDynCastAllOfMatcher< Decl, CXXRecordDecl > cxxRecordDecl
Matches C++ class declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, RecordDecl > recordDecl
Matches class, struct, and union declarations.
const internal::VariadicAllOfMatcher< QualType > qualType
Matches QualTypes in the clang AST.
Top level wrappers for InstallAPI frontend operations.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
QualType pointeeType(QualType T)
bool isInstanceMethod(const Decl *D)
bool isMatrixRowMajor(const LangOptions &LangOpts, QualType T)
Returns true if matrices of T should be laid out in row-major order.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
U cast(CodeGen::Address addr)
static bool cudaTextureType()
static bool generateDebugInfo()
Describes how types, statements, expressions, and declarations should be printed.
unsigned SuppressTagKeyword
Whether type printing should skip printing the tag keyword.
unsigned SuppressInlineNamespace
Suppress printing parts of scope specifiers that correspond to inline namespaces.