1//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is the code that handles AST -> LLVM type lowering.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenTypes.h"
14#include "CGCXXABI.h"
15#include "CGCall.h"
16#include "CGDebugInfo.h"
17#include "CGHLSLRuntime.h"
18#include "CGOpenCLRuntime.h"
19#include "CGRecordLayout.h"
20#include "TargetInfo.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/Expr.h"
25#include "clang/AST/MatrixUtils.h"
26#include "clang/AST/RecordLayout.h"
27#include "clang/CodeGen/CGFunctionInfo.h"
28#include "llvm/IR/DataLayout.h"
29#include "llvm/IR/DerivedTypes.h"
30#include "llvm/IR/Module.h"
31
32using namespace clang;
33using namespace CodeGen;
34
35CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
36 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
37 Target(cgm.getTarget()) {
38 SkippedLayout = false;
39 LongDoubleReferenced = false;
40}
41
42CodeGenTypes::~CodeGenTypes() {
43 for (llvm::FoldingSet<CGFunctionInfo>::iterator
44 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
45 delete &*I++;
46}
47
48CGCXXABI &CodeGenTypes::getCXXABI() const { return getCGM().getCXXABI(); }
49
50const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
51 return CGM.getCodeGenOpts();
52}
53
54void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
55 llvm::StructType *Ty,
56 StringRef suffix) {
57 SmallString<256> TypeName;
58 llvm::raw_svector_ostream OS(TypeName);
59 OS << RD->getKindName() << '.';
60
61 // FIXME: We probably want to make more tweaks to the printing policy. For
62 // example, we should probably enable PrintCanonicalTypes and
63 // FullyQualifiedNames.
64 PrintingPolicy Policy = RD->getASTContext().getPrintingPolicy();
65 Policy.SuppressInlineNamespace =
66 llvm::to_underlying(E: PrintingPolicy::SuppressInlineNamespaceMode::None);
67
68 // Name the codegen type after the typedef name
69 // if there is no tag type name available
70 if (RD->getIdentifier()) {
71 // FIXME: We should not have to check for a null decl context here.
72 // Right now we do it because the implicit Obj-C decls don't have one.
73 if (RD->getDeclContext())
74 RD->printQualifiedName(OS, Policy);
75 else
76 RD->printName(OS, Policy);
77 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
78 // FIXME: We should not have to check for a null decl context here.
79 // Right now we do it because the implicit Obj-C decls don't have one.
80 if (TDD->getDeclContext())
81 TDD->printQualifiedName(OS, Policy);
82 else
83 TDD->printName(OS);
84 } else
85 OS << "anon";
86
87 if (!suffix.empty())
88 OS << suffix;
89
90 Ty->setName(OS.str());
91}
92
93/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
94/// ConvertType in that it is used to convert to the memory representation for
95/// a type. For example, the scalar representation for _Bool is i1, but the
96/// memory representation is usually i8 or i32, depending on the target.
97///
98/// We generally assume that the alloc size of this type under the LLVM
99/// data layout is the same as the size of the AST type. The alignment
100/// does not have to match: Clang should always use explicit alignments
101/// and packed structs as necessary to produce the layout it needs.
102/// But the size does need to be exactly right or else things like struct
103/// layout will break.
104llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
105 if (T->isConstantMatrixType()) {
106 const Type *Ty = Context.getCanonicalType(T).getTypePtr();
107 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Val: Ty);
108 llvm::Type *IRElemTy = ConvertType(T: MT->getElementType());
109 if (Context.getLangOpts().HLSL) {
110 if (T->isConstantMatrixBoolType())
111 IRElemTy = ConvertTypeForMem(T: Context.BoolTy);
112
113 unsigned NumRows = MT->getNumRows();
114 unsigned NumCols = MT->getNumColumns();
115 bool IsRowMajor = isMatrixRowMajor(LangOpts: Context.getLangOpts(), T);
116 unsigned VecLen = IsRowMajor ? NumCols : NumRows;
117 unsigned ArrayLen = IsRowMajor ? NumRows : NumCols;
118 llvm::Type *VecTy = llvm::FixedVectorType::get(ElementType: IRElemTy, NumElts: VecLen);
119 return llvm::ArrayType::get(ElementType: VecTy, NumElements: ArrayLen);
120 }
121 return llvm::ArrayType::get(ElementType: IRElemTy, NumElements: MT->getNumElementsFlattened());
122 }
123
124 llvm::Type *R = ConvertType(T);
125
126 // Check for the boolean vector case.
127 if (T->isExtVectorBoolType()) {
128 auto *FixedVT = cast<llvm::FixedVectorType>(Val: R);
129
130 if (Context.getLangOpts().HLSL) {
131 llvm::Type *IRElemTy = ConvertTypeForMem(T: Context.BoolTy);
132 return llvm::FixedVectorType::get(ElementType: IRElemTy, NumElts: FixedVT->getNumElements());
133 }
134
135 // Pad to at least one byte.
136 uint64_t BytePadded = std::max<uint64_t>(a: FixedVT->getNumElements(), b: 8);
137 return llvm::IntegerType::get(C&: FixedVT->getContext(), NumBits: BytePadded);
138 }
139
140 // If T is _Bool or a _BitInt type, ConvertType will produce an IR type
141 // with the exact semantic bit-width of the AST type; for example,
142 // _BitInt(17) will turn into i17. In memory, however, we need to store
143 // such values extended to their full storage size as decided by AST
144 // layout; this is an ABI requirement. Ideally, we would always use an
145 // integer type that's just the bit-size of the AST type; for example, if
146 // sizeof(_BitInt(17)) == 4, _BitInt(17) would turn into i32. That is what's
147 // returned by convertTypeForLoadStore. However, that type does not
148 // always satisfy the size requirement on memory representation types
149 // describe above. For example, a 32-bit platform might reasonably set
150 // sizeof(_BitInt(65)) == 12, but i96 is likely to have to have an alloc size
151 // of 16 bytes in the LLVM data layout. In these cases, we simply return
152 // a byte array of the appropriate size.
153 if (T->isBitIntType()) {
154 if (typeRequiresSplitIntoByteArray(ASTTy: T, LLVMTy: R))
155 return llvm::ArrayType::get(ElementType: CGM.Int8Ty,
156 NumElements: Context.getTypeSizeInChars(T).getQuantity());
157 return llvm::IntegerType::get(C&: getLLVMContext(),
158 NumBits: (unsigned)Context.getTypeSize(T));
159 }
160
161 if (R->isIntegerTy(BitWidth: 1))
162 return llvm::IntegerType::get(C&: getLLVMContext(),
163 NumBits: (unsigned)Context.getTypeSize(T));
164
165 // Else, don't map it.
166 return R;
167}
168
169bool CodeGenTypes::typeRequiresSplitIntoByteArray(QualType ASTTy,
170 llvm::Type *LLVMTy) {
171 if (!LLVMTy)
172 LLVMTy = ConvertType(T: ASTTy);
173
174 CharUnits ASTSize = Context.getTypeSizeInChars(T: ASTTy);
175 CharUnits LLVMSize =
176 CharUnits::fromQuantity(Quantity: getDataLayout().getTypeAllocSize(Ty: LLVMTy));
177 return ASTSize != LLVMSize;
178}
179
180llvm::Type *CodeGenTypes::convertTypeForLoadStore(QualType T,
181 llvm::Type *LLVMTy) {
182 if (!LLVMTy)
183 LLVMTy = ConvertType(T);
184
185 if (T->isBitIntType())
186 return llvm::Type::getIntNTy(
187 C&: getLLVMContext(), N: Context.getTypeSizeInChars(T).getQuantity() * 8);
188
189 if (LLVMTy->isIntegerTy(BitWidth: 1))
190 return llvm::IntegerType::get(C&: getLLVMContext(),
191 NumBits: (unsigned)Context.getTypeSize(T));
192
193 if (T->isConstantMatrixBoolType()) {
194 // Matrices are loaded and stored atomically as vectors. Therefore we
195 // construct a FixedVectorType here instead of returning
196 // ConvertTypeForMem(T) which would return an ArrayType instead.
197 const Type *Ty = Context.getCanonicalType(T).getTypePtr();
198 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Val: Ty);
199 llvm::Type *IRElemTy = ConvertTypeForMem(T: MT->getElementType());
200 return llvm::FixedVectorType::get(ElementType: IRElemTy, NumElts: MT->getNumElementsFlattened());
201 }
202
203 if (T->isExtVectorBoolType())
204 return ConvertTypeForMem(T);
205
206 return LLVMTy;
207}
208
209/// isRecordLayoutComplete - Return true if the specified type is already
210/// completely laid out.
211bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
212 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
213 RecordDeclTypes.find(Val: Ty);
214 return I != RecordDeclTypes.end() && !I->second->isOpaque();
215}
216
217/// isFuncParamTypeConvertible - Return true if the specified type in a
218/// function parameter or result position can be converted to an IR type at this
219/// point. This boils down to being whether it is complete.
220bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
221 // Some ABIs cannot have their member pointers represented in IR unless
222 // certain circumstances have been reached.
223 if (const auto *MPT = Ty->getAs<MemberPointerType>())
224 return getCXXABI().isMemberPointerConvertible(MPT);
225
226 // If this isn't a tagged type, we can convert it!
227 const TagType *TT = Ty->getAs<TagType>();
228 if (!TT) return true;
229
230 // Incomplete types cannot be converted.
231 return !TT->isIncompleteType();
232}
233
234
235/// Code to verify a given function type is complete, i.e. the return type
236/// and all of the parameter types are complete. Also check to see if we are in
237/// a RS_StructPointer context, and if so whether any struct types have been
238/// pended. If so, we don't want to ask the ABI lowering code to handle a type
239/// that cannot be converted to an IR type.
240bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
241 if (!isFuncParamTypeConvertible(Ty: FT->getReturnType()))
242 return false;
243
244 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Val: FT))
245 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
246 if (!isFuncParamTypeConvertible(Ty: FPT->getParamType(i)))
247 return false;
248
249 return true;
250}
251
252/// UpdateCompletedType - When we find the full definition for a TagDecl,
253/// replace the 'opaque' type we previously made for it if applicable.
254void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
255 CanQualType T = CGM.getContext().getCanonicalTagType(TD);
256 // If this is an enum being completed, then we flush all non-struct types from
257 // the cache. This allows function types and other things that may be derived
258 // from the enum to be recomputed.
259 if (const EnumDecl *ED = dyn_cast<EnumDecl>(Val: TD)) {
260 // Only flush the cache if we've actually already converted this type.
261 if (TypeCache.count(Val: T->getTypePtr())) {
262 // Okay, we formed some types based on this. We speculated that the enum
263 // would be lowered to i32, so we only need to flush the cache if this
264 // didn't happen.
265 if (!ConvertType(T: ED->getIntegerType())->isIntegerTy(BitWidth: 32))
266 TypeCache.clear();
267 }
268 // If necessary, provide the full definition of a type only used with a
269 // declaration so far.
270 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
271 DI->completeType(ED);
272 return;
273 }
274
275 // If we completed a RecordDecl that we previously used and converted to an
276 // anonymous type, then go ahead and complete it now.
277 const RecordDecl *RD = cast<RecordDecl>(Val: TD);
278 if (RD->isDependentType()) return;
279
280 // Only complete it if we converted it already. If we haven't converted it
281 // yet, we'll just do it lazily.
282 if (RecordDeclTypes.count(Val: T.getTypePtr()))
283 ConvertRecordDeclType(TD: RD);
284
285 // If necessary, provide the full definition of a type only used with a
286 // declaration so far.
287 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
288 DI->completeType(RD);
289}
290
291void CodeGenTypes::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
292 CanQualType T = Context.getCanonicalTagType(TD: RD);
293 T = Context.getCanonicalType(T);
294
295 const Type *Ty = T.getTypePtr();
296 if (RecordsWithOpaqueMemberPointers.count(Val: Ty)) {
297 TypeCache.clear();
298 RecordsWithOpaqueMemberPointers.clear();
299 }
300}
301
302static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
303 const llvm::fltSemantics &format,
304 bool UseNativeHalf = false) {
305 if (&format == &llvm::APFloat::IEEEhalf()) {
306 if (UseNativeHalf)
307 return llvm::Type::getHalfTy(C&: VMContext);
308 else
309 return llvm::Type::getInt16Ty(C&: VMContext);
310 }
311 if (&format == &llvm::APFloat::BFloat())
312 return llvm::Type::getBFloatTy(C&: VMContext);
313 if (&format == &llvm::APFloat::IEEEsingle())
314 return llvm::Type::getFloatTy(C&: VMContext);
315 if (&format == &llvm::APFloat::IEEEdouble())
316 return llvm::Type::getDoubleTy(C&: VMContext);
317 if (&format == &llvm::APFloat::IEEEquad())
318 return llvm::Type::getFP128Ty(C&: VMContext);
319 if (&format == &llvm::APFloat::PPCDoubleDouble())
320 return llvm::Type::getPPC_FP128Ty(C&: VMContext);
321 if (&format == &llvm::APFloat::x87DoubleExtended())
322 return llvm::Type::getX86_FP80Ty(C&: VMContext);
323 llvm_unreachable("Unknown float format!");
324}
325
326llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
327 assert(QFT.isCanonical());
328 const FunctionType *FT = cast<FunctionType>(Val: QFT.getTypePtr());
329 // First, check whether we can build the full function type. If the
330 // function type depends on an incomplete type (e.g. a struct or enum), we
331 // cannot lower the function type.
332 if (!isFuncTypeConvertible(FT)) {
333 // This function's type depends on an incomplete tag type.
334
335 // Force conversion of all the relevant record types, to make sure
336 // we re-convert the FunctionType when appropriate.
337 if (const auto *RD = FT->getReturnType()->getAsRecordDecl())
338 ConvertRecordDeclType(TD: RD);
339 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Val: FT))
340 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
341 if (const auto *RD = FPT->getParamType(i)->getAsRecordDecl())
342 ConvertRecordDeclType(TD: RD);
343
344 SkippedLayout = true;
345
346 // Return a placeholder type.
347 return llvm::StructType::get(Context&: getLLVMContext());
348 }
349
350 // The function type can be built; call the appropriate routines to
351 // build it.
352 const CGFunctionInfo *FI;
353 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Val: FT)) {
354 FI = &arrangeFreeFunctionType(
355 Ty: CanQual<FunctionProtoType>::CreateUnsafe(Other: QualType(FPT, 0)));
356 } else {
357 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(Val: FT);
358 FI = &arrangeFreeFunctionType(
359 Ty: CanQual<FunctionNoProtoType>::CreateUnsafe(Other: QualType(FNPT, 0)));
360 }
361
362 llvm::Type *ResultType = nullptr;
363 // If there is something higher level prodding our CGFunctionInfo, then
364 // don't recurse into it again.
365 if (FunctionsBeingProcessed.count(Ptr: FI)) {
366
367 ResultType = llvm::StructType::get(Context&: getLLVMContext());
368 SkippedLayout = true;
369 } else {
370
371 // Otherwise, we're good to go, go ahead and convert it.
372 ResultType = GetFunctionType(Info: *FI);
373 }
374
375 return ResultType;
376}
377
378/// ConvertType - Convert the specified type to its LLVM form.
379llvm::Type *CodeGenTypes::ConvertType(QualType T) {
380 T = Context.getCanonicalType(T);
381
382 const Type *Ty = T.getTypePtr();
383
384 // For the device-side compilation, CUDA device builtin surface/texture types
385 // may be represented in different types.
386 if (Context.getLangOpts().CUDAIsDevice) {
387 if (T->isCUDADeviceBuiltinSurfaceType()) {
388 if (auto *Ty = CGM.getTargetCodeGenInfo()
389 .getCUDADeviceBuiltinSurfaceDeviceType())
390 return Ty;
391 } else if (T->isCUDADeviceBuiltinTextureType()) {
392 if (auto *Ty = CGM.getTargetCodeGenInfo()
393 .getCUDADeviceBuiltinTextureDeviceType())
394 return Ty;
395 }
396 }
397
398 // RecordTypes are cached and processed specially.
399 if (const auto *RT = dyn_cast<RecordType>(Val: Ty))
400 return ConvertRecordDeclType(TD: RT->getDecl()->getDefinitionOrSelf());
401
402 llvm::Type *CachedType = nullptr;
403 auto TCI = TypeCache.find(Val: Ty);
404 if (TCI != TypeCache.end())
405 CachedType = TCI->second;
406 // With expensive checks, check that the type we compute matches the
407 // cached type.
408#ifndef EXPENSIVE_CHECKS
409 if (CachedType)
410 return CachedType;
411#endif
412
413 // If we don't have it in the cache, convert it now.
414 llvm::Type *ResultType = nullptr;
415 switch (Ty->getTypeClass()) {
416 case Type::Record: // Handled above.
417#define TYPE(Class, Base)
418#define ABSTRACT_TYPE(Class, Base)
419#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
420#define DEPENDENT_TYPE(Class, Base) case Type::Class:
421#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
422#include "clang/AST/TypeNodes.inc"
423 llvm_unreachable("Non-canonical or dependent types aren't possible.");
424
425 case Type::Builtin: {
426 switch (cast<BuiltinType>(Val: Ty)->getKind()) {
427 case BuiltinType::Void:
428 case BuiltinType::ObjCId:
429 case BuiltinType::ObjCClass:
430 case BuiltinType::ObjCSel:
431 // LLVM void type can only be used as the result of a function call. Just
432 // map to the same as char.
433 ResultType = llvm::Type::getInt8Ty(C&: getLLVMContext());
434 break;
435
436 case BuiltinType::Bool:
437 // Note that we always return bool as i1 for use as a scalar type.
438 ResultType = llvm::Type::getInt1Ty(C&: getLLVMContext());
439 break;
440
441 case BuiltinType::Char_S:
442 case BuiltinType::Char_U:
443 case BuiltinType::SChar:
444 case BuiltinType::UChar:
445 case BuiltinType::Short:
446 case BuiltinType::UShort:
447 case BuiltinType::Int:
448 case BuiltinType::UInt:
449 case BuiltinType::Long:
450 case BuiltinType::ULong:
451 case BuiltinType::LongLong:
452 case BuiltinType::ULongLong:
453 case BuiltinType::WChar_S:
454 case BuiltinType::WChar_U:
455 case BuiltinType::Char8:
456 case BuiltinType::Char16:
457 case BuiltinType::Char32:
458 case BuiltinType::ShortAccum:
459 case BuiltinType::Accum:
460 case BuiltinType::LongAccum:
461 case BuiltinType::UShortAccum:
462 case BuiltinType::UAccum:
463 case BuiltinType::ULongAccum:
464 case BuiltinType::ShortFract:
465 case BuiltinType::Fract:
466 case BuiltinType::LongFract:
467 case BuiltinType::UShortFract:
468 case BuiltinType::UFract:
469 case BuiltinType::ULongFract:
470 case BuiltinType::SatShortAccum:
471 case BuiltinType::SatAccum:
472 case BuiltinType::SatLongAccum:
473 case BuiltinType::SatUShortAccum:
474 case BuiltinType::SatUAccum:
475 case BuiltinType::SatULongAccum:
476 case BuiltinType::SatShortFract:
477 case BuiltinType::SatFract:
478 case BuiltinType::SatLongFract:
479 case BuiltinType::SatUShortFract:
480 case BuiltinType::SatUFract:
481 case BuiltinType::SatULongFract:
482 ResultType = llvm::IntegerType::get(C&: getLLVMContext(),
483 NumBits: static_cast<unsigned>(Context.getTypeSize(T)));
484 break;
485
486 case BuiltinType::Float16:
487 ResultType =
488 getTypeForFormat(VMContext&: getLLVMContext(), format: Context.getFloatTypeSemantics(T),
489 /* UseNativeHalf = */ true);
490 break;
491
492 case BuiltinType::Half:
493 // Half FP can either be storage-only (lowered to i16) or native.
494 ResultType = getTypeForFormat(
495 VMContext&: getLLVMContext(), format: Context.getFloatTypeSemantics(T),
496 UseNativeHalf: Context.getLangOpts().NativeHalfType ||
497 !Context.getTargetInfo().useFP16ConversionIntrinsics());
498 break;
499 case BuiltinType::LongDouble:
500 LongDoubleReferenced = true;
501 [[fallthrough]];
502 case BuiltinType::BFloat16:
503 case BuiltinType::Float:
504 case BuiltinType::Double:
505 case BuiltinType::Float128:
506 case BuiltinType::Ibm128:
507 ResultType = getTypeForFormat(VMContext&: getLLVMContext(),
508 format: Context.getFloatTypeSemantics(T),
509 /* UseNativeHalf = */ false);
510 break;
511
512 case BuiltinType::NullPtr:
513 // Model std::nullptr_t as i8*
514 ResultType = llvm::PointerType::getUnqual(C&: getLLVMContext());
515 break;
516
517 case BuiltinType::UInt128:
518 case BuiltinType::Int128:
519 ResultType = llvm::IntegerType::get(C&: getLLVMContext(), NumBits: 128);
520 break;
521
522#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
523 case BuiltinType::Id:
524#include "clang/Basic/OpenCLImageTypes.def"
525#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
526 case BuiltinType::Id:
527#include "clang/Basic/OpenCLExtensionTypes.def"
528 case BuiltinType::OCLSampler:
529 case BuiltinType::OCLEvent:
530 case BuiltinType::OCLClkEvent:
531 case BuiltinType::OCLQueue:
532 case BuiltinType::OCLReserveID:
533 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(T: Ty);
534 break;
535#define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId) \
536 case BuiltinType::Id:
537#define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId) \
538 case BuiltinType::Id:
539#include "clang/Basic/AArch64ACLETypes.def"
540 {
541 ASTContext::BuiltinVectorTypeInfo Info =
542 Context.getBuiltinVectorTypeInfo(VecTy: cast<BuiltinType>(Val: Ty));
543 // The `__mfp8` type maps to `<1 x i8>` which can't be used to build
544 // a <N x i8> vector type, hence bypass the call to `ConvertType` for
545 // the element type and create the vector type directly.
546 auto *EltTy = Info.ElementType->isMFloat8Type()
547 ? llvm::Type::getInt8Ty(C&: getLLVMContext())
548 : ConvertType(T: Info.ElementType);
549 auto *VTy = llvm::VectorType::get(ElementType: EltTy, EC: Info.EC);
550 switch (Info.NumVectors) {
551 default:
552 llvm_unreachable("Expected 1, 2, 3 or 4 vectors!");
553 case 1:
554 return VTy;
555 case 2:
556 return llvm::StructType::get(elt1: VTy, elts: VTy);
557 case 3:
558 return llvm::StructType::get(elt1: VTy, elts: VTy, elts: VTy);
559 case 4:
560 return llvm::StructType::get(elt1: VTy, elts: VTy, elts: VTy, elts: VTy);
561 }
562 }
563 case BuiltinType::SveCount:
564 return llvm::TargetExtType::get(Context&: getLLVMContext(), Name: "aarch64.svcount");
565 case BuiltinType::MFloat8:
566 return llvm::VectorType::get(ElementType: llvm::Type::getInt8Ty(C&: getLLVMContext()), NumElements: 1,
567 Scalable: false);
568#define PPC_VECTOR_TYPE(Name, Id, Size) \
569 case BuiltinType::Id: \
570 ResultType = \
571 llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
572 break;
573#include "clang/Basic/PPCTypes.def"
574#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
575#include "clang/Basic/RISCVVTypes.def"
576 {
577 ASTContext::BuiltinVectorTypeInfo Info =
578 Context.getBuiltinVectorTypeInfo(VecTy: cast<BuiltinType>(Val: Ty));
579 if (Info.NumVectors != 1) {
580 unsigned I8EltCount =
581 Info.EC.getKnownMinValue() *
582 ConvertType(T: Info.ElementType)->getScalarSizeInBits() / 8;
583 return llvm::TargetExtType::get(
584 Context&: getLLVMContext(), Name: "riscv.vector.tuple",
585 Types: llvm::ScalableVectorType::get(
586 ElementType: llvm::Type::getInt8Ty(C&: getLLVMContext()), MinNumElts: I8EltCount),
587 Ints: Info.NumVectors);
588 }
589 return llvm::ScalableVectorType::get(ElementType: ConvertType(T: Info.ElementType),
590 MinNumElts: Info.EC.getKnownMinValue());
591 }
592#define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \
593 case BuiltinType::Id: { \
594 if (BuiltinType::Id == BuiltinType::WasmExternRef) \
595 ResultType = CGM.getTargetCodeGenInfo().getWasmExternrefReferenceType(); \
596 else \
597 llvm_unreachable("Unexpected wasm reference builtin type!"); \
598 } break;
599#include "clang/Basic/WebAssemblyReferenceTypes.def"
600#define AMDGPU_OPAQUE_PTR_TYPE(Name, Id, SingletonId, Width, Align, AS) \
601 case BuiltinType::Id: { \
602 if (BuiltinType::Id == BuiltinType::AMDGPUTexture) { \
603 return llvm::FixedVectorType::get( \
604 llvm::Type::getInt32Ty(getLLVMContext()), 8); \
605 } \
606 return llvm::PointerType::get(getLLVMContext(), AS); \
607 }
608#define AMDGPU_NAMED_BARRIER_TYPE(Name, Id, SingletonId, Width, Align, Scope) \
609 case BuiltinType::Id: \
610 return llvm::TargetExtType::get(getLLVMContext(), "amdgcn.named.barrier", \
611 {}, {Scope});
612#define AMDGPU_FEATURE_PREDICATE_TYPE(Name, Id, SingletonId, Width, Align) \
613 case BuiltinType::Id: \
614 return ConvertType(getContext().getLogicalOperationType());
615#include "clang/Basic/AMDGPUTypes.def"
616#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
617#include "clang/Basic/HLSLIntangibleTypes.def"
618 ResultType = CGM.getHLSLRuntime().convertHLSLSpecificType(T: Ty);
619 break;
620 case BuiltinType::Dependent:
621#define BUILTIN_TYPE(Id, SingletonId)
622#define PLACEHOLDER_TYPE(Id, SingletonId) \
623 case BuiltinType::Id:
624#include "clang/AST/BuiltinTypes.def"
625 llvm_unreachable("Unexpected placeholder builtin type!");
626 }
627 break;
628 }
629 case Type::Auto:
630 case Type::DeducedTemplateSpecialization:
631 llvm_unreachable("Unexpected undeduced type!");
632 case Type::Complex: {
633 llvm::Type *EltTy = ConvertType(T: cast<ComplexType>(Val: Ty)->getElementType());
634 ResultType = llvm::StructType::get(elt1: EltTy, elts: EltTy);
635 break;
636 }
637 case Type::LValueReference:
638 case Type::RValueReference: {
639 const ReferenceType *RTy = cast<ReferenceType>(Val: Ty);
640 QualType ETy = RTy->getPointeeType();
641 unsigned AS = getTargetAddressSpace(T: ETy);
642 ResultType = llvm::PointerType::get(C&: getLLVMContext(), AddressSpace: AS);
643 break;
644 }
645 case Type::Pointer: {
646 const PointerType *PTy = cast<PointerType>(Val: Ty);
647 QualType ETy = PTy->getPointeeType();
648 if (ETy.getAddressSpace() == LangAS::wasm_funcref) {
649 ResultType = CGM.getTargetCodeGenInfo().getWasmFuncrefReferenceType();
650 break;
651 }
652 unsigned AS = getTargetAddressSpace(T: ETy);
653 ResultType = llvm::PointerType::get(C&: getLLVMContext(), AddressSpace: AS);
654 break;
655 }
656
657 case Type::VariableArray: {
658 const VariableArrayType *A = cast<VariableArrayType>(Val: Ty);
659 assert(A->getIndexTypeCVRQualifiers() == 0 &&
660 "FIXME: We only handle trivial array types so far!");
661 // VLAs resolve to the innermost element type; this matches
662 // the return of alloca, and there isn't any obviously better choice.
663 ResultType = ConvertTypeForMem(T: A->getElementType());
664 break;
665 }
666 case Type::IncompleteArray: {
667 const IncompleteArrayType *A = cast<IncompleteArrayType>(Val: Ty);
668 assert(A->getIndexTypeCVRQualifiers() == 0 &&
669 "FIXME: We only handle trivial array types so far!");
670 // int X[] -> [0 x int], unless the element type is not sized. If it is
671 // unsized (e.g. an incomplete struct) just use [0 x i8].
672 ResultType = ConvertTypeForMem(T: A->getElementType());
673 if (!ResultType->isSized()) {
674 SkippedLayout = true;
675 ResultType = llvm::Type::getInt8Ty(C&: getLLVMContext());
676 }
677 ResultType = llvm::ArrayType::get(ElementType: ResultType, NumElements: 0);
678 break;
679 }
680 case Type::ArrayParameter:
681 case Type::ConstantArray: {
682 const ConstantArrayType *A = cast<ConstantArrayType>(Val: Ty);
683 llvm::Type *EltTy = ConvertTypeForMem(T: A->getElementType());
684
685 // Lower arrays of undefined struct type to arrays of i8 just to have a
686 // concrete type.
687 if (!EltTy->isSized()) {
688 SkippedLayout = true;
689 EltTy = llvm::Type::getInt8Ty(C&: getLLVMContext());
690 }
691
692 ResultType = llvm::ArrayType::get(ElementType: EltTy, NumElements: A->getZExtSize());
693 break;
694 }
695 case Type::ExtVector:
696 case Type::Vector: {
697 const auto *VT = cast<VectorType>(Val: Ty);
698 // An ext_vector_type of Bool is really a vector of bits.
699 llvm::Type *IRElemTy = VT->isPackedVectorBoolType(ctx: Context)
700 ? llvm::Type::getInt1Ty(C&: getLLVMContext())
701 : VT->getElementType()->isMFloat8Type()
702 ? llvm::Type::getInt8Ty(C&: getLLVMContext())
703 : ConvertType(T: VT->getElementType());
704 ResultType = llvm::FixedVectorType::get(ElementType: IRElemTy, NumElts: VT->getNumElements());
705 break;
706 }
707 case Type::ConstantMatrix: {
708 const ConstantMatrixType *MT = cast<ConstantMatrixType>(Val: Ty);
709 ResultType =
710 llvm::FixedVectorType::get(ElementType: ConvertType(T: MT->getElementType()),
711 NumElts: MT->getNumRows() * MT->getNumColumns());
712 break;
713 }
714 case Type::FunctionNoProto:
715 case Type::FunctionProto:
716 ResultType = ConvertFunctionTypeInternal(QFT: T);
717 break;
718 case Type::ObjCObject:
719 ResultType = ConvertType(T: cast<ObjCObjectType>(Val: Ty)->getBaseType());
720 break;
721
722 case Type::ObjCInterface: {
723 // Objective-C interfaces are always opaque (outside of the
724 // runtime, which can do whatever it likes); we never refine
725 // these.
726 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Val: Ty)];
727 if (!T)
728 T = llvm::StructType::create(Context&: getLLVMContext());
729 ResultType = T;
730 break;
731 }
732
733 case Type::ObjCObjectPointer:
734 ResultType = llvm::PointerType::getUnqual(C&: getLLVMContext());
735 break;
736
737 case Type::Enum: {
738 const auto *ED = Ty->castAsEnumDecl();
739 if (ED->isCompleteDefinition() || ED->isFixed())
740 return ConvertType(T: ED->getIntegerType());
741 // Return a placeholder 'i32' type. This can be changed later when the
742 // type is defined (see UpdateCompletedType), but is likely to be the
743 // "right" answer.
744 ResultType = llvm::Type::getInt32Ty(C&: getLLVMContext());
745 break;
746 }
747
748 case Type::BlockPointer: {
749 // Block pointers lower to function type. For function type,
750 // getTargetAddressSpace() returns default address space for
751 // function pointer i.e. program address space. Therefore, for block
752 // pointers, it is important to pass the pointee AST address space when
753 // calling getTargetAddressSpace(), to ensure that we get the LLVM IR
754 // address space for data pointers and not function pointers.
755 const QualType FTy = cast<BlockPointerType>(Val: Ty)->getPointeeType();
756 unsigned AS = Context.getTargetAddressSpace(AS: FTy.getAddressSpace());
757 ResultType = llvm::PointerType::get(C&: getLLVMContext(), AddressSpace: AS);
758 break;
759 }
760
761 case Type::MemberPointer: {
762 auto *MPTy = cast<MemberPointerType>(Val: Ty);
763 if (!getCXXABI().isMemberPointerConvertible(MPT: MPTy)) {
764 CanQualType T = CGM.getContext().getCanonicalTagType(
765 TD: MPTy->getMostRecentCXXRecordDecl());
766 auto Insertion =
767 RecordsWithOpaqueMemberPointers.try_emplace(Key: T.getTypePtr());
768 if (Insertion.second)
769 Insertion.first->second = llvm::StructType::create(Context&: getLLVMContext());
770 ResultType = Insertion.first->second;
771 } else {
772 ResultType = getCXXABI().ConvertMemberPointerType(MPT: MPTy);
773 }
774 break;
775 }
776
777 case Type::Atomic: {
778 QualType valueType = cast<AtomicType>(Val: Ty)->getValueType();
779 ResultType = ConvertTypeForMem(T: valueType);
780
781 // Pad out to the inflated size if necessary.
782 uint64_t valueSize = Context.getTypeSize(T: valueType);
783 uint64_t atomicSize = Context.getTypeSize(T: Ty);
784 if (valueSize != atomicSize) {
785 assert(valueSize < atomicSize);
786 llvm::Type *elts[] = {
787 ResultType,
788 llvm::ArrayType::get(ElementType: CGM.Int8Ty, NumElements: (atomicSize - valueSize) / 8)
789 };
790 ResultType =
791 llvm::StructType::get(Context&: getLLVMContext(), Elements: llvm::ArrayRef(elts));
792 }
793 break;
794 }
795 case Type::Pipe: {
796 ResultType = CGM.getOpenCLRuntime().getPipeType(T: cast<PipeType>(Val: Ty));
797 break;
798 }
799 case Type::BitInt: {
800 const auto &EIT = cast<BitIntType>(Val: Ty);
801 ResultType = llvm::Type::getIntNTy(C&: getLLVMContext(), N: EIT->getNumBits());
802 break;
803 }
804 case Type::HLSLAttributedResource:
805 case Type::HLSLInlineSpirv:
806 ResultType = CGM.getHLSLRuntime().convertHLSLSpecificType(T: Ty);
807 break;
808 case Type::OverflowBehavior:
809 ResultType =
810 ConvertType(T: dyn_cast<OverflowBehaviorType>(Val: Ty)->getUnderlyingType());
811 break;
812 }
813
814 assert(ResultType && "Didn't convert a type?");
815 assert((!CachedType || CachedType == ResultType) &&
816 "Cached type doesn't match computed type");
817
818 TypeCache[Ty] = ResultType;
819 return ResultType;
820}
821
822bool CodeGenModule::isPaddedAtomicType(QualType type) {
823 return isPaddedAtomicType(type: type->castAs<AtomicType>());
824}
825
826bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
827 return Context.getTypeSize(T: type) != Context.getTypeSize(T: type->getValueType());
828}
829
830/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
831llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
832 // TagDecl's are not necessarily unique, instead use the (clang)
833 // type connected to the decl.
834 const Type *Key = Context.getCanonicalTagType(TD: RD).getTypePtr();
835
836 llvm::StructType *&Entry = RecordDeclTypes[Key];
837
838 // If we don't have a StructType at all yet, create the forward declaration.
839 if (!Entry) {
840 Entry = llvm::StructType::create(Context&: getLLVMContext());
841 addRecordTypeName(RD, Ty: Entry, suffix: "");
842 }
843 llvm::StructType *Ty = Entry;
844
845 // If this is still a forward declaration, or the LLVM type is already
846 // complete, there's nothing more to do.
847 RD = RD->getDefinition();
848 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
849 return Ty;
850
851 // Force conversion of non-virtual base classes recursively.
852 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
853 for (const auto &I : CRD->bases()) {
854 if (I.isVirtual()) continue;
855 ConvertRecordDeclType(RD: I.getType()->castAsRecordDecl());
856 }
857 }
858
859 // Layout fields.
860 std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(D: RD, Ty);
861 CGRecordLayouts[Key] = std::move(Layout);
862
863 // If this struct blocked a FunctionType conversion, then recompute whatever
864 // was derived from that.
865 // FIXME: This is hugely overconservative.
866 if (SkippedLayout)
867 TypeCache.clear();
868
869 return Ty;
870}
871
872/// getCGRecordLayout - Return record layout info for the given record decl.
873const CGRecordLayout &
874CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
875 const Type *Key = Context.getCanonicalTagType(TD: RD).getTypePtr();
876
877 auto I = CGRecordLayouts.find(Val: Key);
878 if (I != CGRecordLayouts.end())
879 return *I->second;
880 // Compute the type information.
881 ConvertRecordDeclType(RD);
882
883 // Now try again.
884 I = CGRecordLayouts.find(Val: Key);
885
886 assert(I != CGRecordLayouts.end() &&
887 "Unable to find record layout information for type");
888 return *I->second;
889}
890
891bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
892 assert((T->isAnyPointerType() || T->isBlockPointerType() ||
893 T->isNullPtrType()) &&
894 "Invalid type");
895 return isZeroInitializable(T);
896}
897
898bool CodeGenTypes::isZeroInitializable(QualType T) {
899 if (T->getAs<PointerType>() || T->isNullPtrType())
900 return Context.getTargetNullPointerValue(QT: T) == 0;
901
902 if (const auto *AT = Context.getAsArrayType(T)) {
903 if (isa<IncompleteArrayType>(Val: AT))
904 return true;
905 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT))
906 if (Context.getConstantArrayElementCount(CA: CAT) == 0)
907 return true;
908 T = Context.getBaseElementType(QT: T);
909 }
910
911 // Records are non-zero-initializable if they contain any
912 // non-zero-initializable subobjects.
913 if (const auto *RD = T->getAsRecordDecl())
914 return isZeroInitializable(RD);
915
916 // We have to ask the ABI about member pointers.
917 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
918 return getCXXABI().isZeroInitializable(MPT);
919
920 // HLSL Inline SPIR-V types are non-zero-initializable.
921 if (T->getAs<HLSLInlineSpirvType>())
922 return false;
923
924 // Everything else is okay.
925 return true;
926}
927
928bool CodeGenTypes::isZeroInitializable(const RecordDecl *RD) {
929 return getCGRecordLayout(RD).isZeroInitializable();
930}
931
932unsigned CodeGenTypes::getTargetAddressSpace(QualType T) const {
933 // Return the address space for the type. If the type is a
934 // function type without an address space qualifier, the
935 // program address space is used. Otherwise, the target picks
936 // the best address space based on the type information
937 return T->isFunctionType() && !T.hasAddressSpace()
938 ? getDataLayout().getProgramAddressSpace()
939 : getContext().getTargetAddressSpace(AS: T.getAddressSpace());
940}
941