clang 24.0.0git
CodeGenTypes.cpp
Go to the documentation of this file.
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"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/Expr.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
36 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
37 Target(cgm.getTarget()) {
38 SkippedLayout = false;
39 LongDoubleReferenced = false;
40}
41
43 for (llvm::FoldingSet<CGFunctionInfo>::iterator
44 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
45 delete &*I++;
46}
47
49
51 return CGM.getCodeGenOpts();
52}
53
55 llvm::StructType *Ty,
56 StringRef suffix) {
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.
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.
105 if (const auto *ArrayTy = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
106 llvm::Type *ElementTy = ConvertTypeForMem(ArrayTy->getElementType());
107 return llvm::ArrayType::get(ElementTy, ArrayTy->getZExtSize());
108 }
109
110 if (T->isConstantMatrixType()) {
111 const Type *Ty = Context.getCanonicalType(T).getTypePtr();
113 llvm::Type *IRElemTy = ConvertType(MT->getElementType());
114 if (Context.getLangOpts().HLSL) {
115 if (T->isConstantMatrixBoolType())
116 IRElemTy = ConvertTypeForMem(Context.BoolTy);
117
118 unsigned NumRows = MT->getNumRows();
119 unsigned NumCols = MT->getNumColumns();
120 bool IsRowMajor = isMatrixRowMajor(Context.getLangOpts(), T);
121 unsigned VecLen = IsRowMajor ? NumCols : NumRows;
122 unsigned ArrayLen = IsRowMajor ? NumRows : NumCols;
123 llvm::Type *VecTy = llvm::FixedVectorType::get(IRElemTy, VecLen);
124 return llvm::ArrayType::get(VecTy, ArrayLen);
125 }
126 return llvm::ArrayType::get(IRElemTy, MT->getNumElementsFlattened());
127 }
128
129 llvm::Type *R = ConvertType(T);
130
131 // Check for the boolean vector case.
132 if (T->isExtVectorBoolType()) {
133 auto *FixedVT = cast<llvm::FixedVectorType>(R);
134
135 if (Context.getLangOpts().HLSL) {
136 llvm::Type *IRElemTy = ConvertTypeForMem(Context.BoolTy);
137 return llvm::FixedVectorType::get(IRElemTy, FixedVT->getNumElements());
138 }
139
140 // Pad to at least one byte.
141 uint64_t BytePadded = std::max<uint64_t>(FixedVT->getNumElements(), 8);
142 return llvm::IntegerType::get(FixedVT->getContext(), BytePadded);
143 }
144
145 // If T is _Bool or a _BitInt type, ConvertType will produce an IR type
146 // with the exact semantic bit-width of the AST type; for example,
147 // _BitInt(17) will turn into i17. In memory, however, we need to store
148 // such values extended to their full storage size as decided by AST
149 // layout; this is an ABI requirement. Ideally, we would always use an
150 // integer type that's just the bit-size of the AST type; for example, if
151 // sizeof(_BitInt(17)) == 4, _BitInt(17) would turn into i32. That is what's
152 // returned by convertTypeForLoadStore. However, that type does not
153 // always satisfy the size requirement on memory representation types
154 // describe above. For example, a 32-bit platform might reasonably set
155 // sizeof(_BitInt(65)) == 12, but i96 is likely to have to have an alloc size
156 // of 16 bytes in the LLVM data layout. In these cases, we simply return
157 // a byte array of the appropriate size.
158 if (T->isBitIntType()) {
160 return llvm::ArrayType::get(CGM.Int8Ty,
161 Context.getTypeSizeInChars(T).getQuantity());
162 return llvm::IntegerType::get(getLLVMContext(),
163 (unsigned)Context.getTypeSize(T));
164 }
165
166 if (R->isIntegerTy(1))
167 return llvm::IntegerType::get(getLLVMContext(),
168 (unsigned)Context.getTypeSize(T));
169
170 // Else, don't map it.
171 return R;
172}
173
175 llvm::Type *LLVMTy) {
176 if (!LLVMTy)
177 LLVMTy = ConvertType(ASTTy);
178
179 CharUnits ASTSize = Context.getTypeSizeInChars(ASTTy);
180 CharUnits LLVMSize =
182 return ASTSize != LLVMSize;
183}
184
186 llvm::Type *LLVMTy) {
187 if (!LLVMTy)
188 LLVMTy = ConvertType(T);
189
190 if (T->isBitIntType())
191 return llvm::Type::getIntNTy(
192 getLLVMContext(), Context.getTypeSizeInChars(T).getQuantity() * 8);
193
194 if (LLVMTy->isIntegerTy(1))
195 return llvm::IntegerType::get(getLLVMContext(),
196 (unsigned)Context.getTypeSize(T));
197
198 if (T->isConstantMatrixBoolType()) {
199 // Matrices are loaded and stored atomically as vectors. Therefore we
200 // construct a FixedVectorType here instead of returning
201 // ConvertTypeForMem(T) which would return an ArrayType instead.
202 const Type *Ty = Context.getCanonicalType(T).getTypePtr();
204 llvm::Type *IRElemTy = ConvertTypeForMem(MT->getElementType());
205 return llvm::FixedVectorType::get(IRElemTy, MT->getNumElementsFlattened());
206 }
207
208 if (T->isExtVectorBoolType())
209 return ConvertTypeForMem(T);
210
211 return LLVMTy;
212}
213
214/// isRecordLayoutComplete - Return true if the specified type is already
215/// completely laid out.
217 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
218 RecordDeclTypes.find(Ty);
219 return I != RecordDeclTypes.end() && !I->second->isOpaque();
220}
221
222/// isFuncParamTypeConvertible - Return true if the specified type in a
223/// function parameter or result position can be converted to an IR type at this
224/// point. This boils down to being whether it is complete.
226 // Some ABIs cannot have their member pointers represented in IR unless
227 // certain circumstances have been reached.
228 if (const auto *MPT = Ty->getAs<MemberPointerType>())
230
231 // If this isn't a tagged type, we can convert it!
232 const TagType *TT = Ty->getAs<TagType>();
233 if (!TT) return true;
234
235 // Incomplete types cannot be converted.
236 return !TT->isIncompleteType();
237}
238
239
240/// Code to verify a given function type is complete, i.e. the return type
241/// and all of the parameter types are complete. Also check to see if we are in
242/// a RS_StructPointer context, and if so whether any struct types have been
243/// pended. If so, we don't want to ask the ABI lowering code to handle a type
244/// that cannot be converted to an IR type.
247 return false;
248
249 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
250 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
251 if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
252 return false;
253
254 return true;
255}
256
257/// UpdateCompletedType - When we find the full definition for a TagDecl,
258/// replace the 'opaque' type we previously made for it if applicable.
260 CanQualType T = CGM.getContext().getCanonicalTagType(TD);
261 // If this is an enum being completed, then we flush all non-struct types from
262 // the cache. This allows function types and other things that may be derived
263 // from the enum to be recomputed.
264 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
265 // Only flush the cache if we've actually already converted this type.
266 if (TypeCache.count(T->getTypePtr())) {
267 // Okay, we formed some types based on this. We speculated that the enum
268 // would be lowered to i32, so we only need to flush the cache if this
269 // didn't happen.
270 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
271 TypeCache.clear();
272 }
273 // If necessary, provide the full definition of a type only used with a
274 // declaration so far.
275 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
276 DI->completeType(ED);
277 return;
278 }
279
280 // If we completed a RecordDecl that we previously used and converted to an
281 // anonymous type, then go ahead and complete it now.
282 const RecordDecl *RD = cast<RecordDecl>(TD);
283 if (RD->isDependentType()) return;
284
285 // Only complete it if we converted it already. If we haven't converted it
286 // yet, we'll just do it lazily.
287 if (RecordDeclTypes.count(T.getTypePtr()))
289
290 // If necessary, provide the full definition of a type only used with a
291 // declaration so far.
292 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
293 DI->completeType(RD);
294}
295
297 CanQualType T = Context.getCanonicalTagType(RD);
298 T = Context.getCanonicalType(T);
299
300 const Type *Ty = T.getTypePtr();
301 if (RecordsWithOpaqueMemberPointers.count(Ty)) {
302 TypeCache.clear();
303 RecordsWithOpaqueMemberPointers.clear();
304 }
305}
306
307llvm::Type *CodeGenTypes::ConvertFunctionTypeInternal(QualType QFT) {
308 assert(QFT.isCanonical());
309 const FunctionType *FT = cast<FunctionType>(QFT.getTypePtr());
310 // First, check whether we can build the full function type. If the
311 // function type depends on an incomplete type (e.g. a struct or enum), we
312 // cannot lower the function type.
313 if (!isFuncTypeConvertible(FT)) {
314 // This function's type depends on an incomplete tag type.
315
316 // Force conversion of all the relevant record types, to make sure
317 // we re-convert the FunctionType when appropriate.
318 if (const auto *RD = FT->getReturnType()->getAsRecordDecl())
320 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
321 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
322 if (const auto *RD = FPT->getParamType(i)->getAsRecordDecl())
324
325 SkippedLayout = true;
326
327 // Return a placeholder type.
328 return llvm::StructType::get(getLLVMContext());
329 }
330
331 // The function type can be built; call the appropriate routines to
332 // build it.
333 const CGFunctionInfo *FI;
334 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
337 } else {
341 }
342
343 llvm::Type *ResultType = nullptr;
344 // If there is something higher level prodding our CGFunctionInfo, then
345 // don't recurse into it again.
346 if (FunctionsBeingProcessed.count(FI)) {
347
348 ResultType = llvm::StructType::get(getLLVMContext());
349 SkippedLayout = true;
350 } else {
351
352 // Otherwise, we're good to go, go ahead and convert it.
353 ResultType = GetFunctionType(*FI);
354 }
355
356 return ResultType;
357}
358
359/// ConvertType - Convert the specified type to its LLVM form.
361 T = Context.getCanonicalType(T);
362
363 const Type *Ty = T.getTypePtr();
364
365 // For the device-side compilation, CUDA device builtin surface/texture types
366 // may be represented in different types.
367 if (Context.getLangOpts().CUDAIsDevice) {
368 if (T->isCUDADeviceBuiltinSurfaceType()) {
369 if (auto *Ty = CGM.getTargetCodeGenInfo()
370 .getCUDADeviceBuiltinSurfaceDeviceType())
371 return Ty;
372 } else if (T->isCUDADeviceBuiltinTextureType()) {
373 if (auto *Ty = CGM.getTargetCodeGenInfo()
374 .getCUDADeviceBuiltinTextureDeviceType())
375 return Ty;
376 }
377 }
378
379 // RecordTypes are cached and processed specially.
380 if (const auto *RT = dyn_cast<RecordType>(Ty))
381 return ConvertRecordDeclType(RT->getDecl()->getDefinitionOrSelf());
382
383 llvm::Type *CachedType = nullptr;
384 auto TCI = TypeCache.find(Ty);
385 if (TCI != TypeCache.end())
386 CachedType = TCI->second;
387 // With expensive checks, check that the type we compute matches the
388 // cached type.
389#ifndef EXPENSIVE_CHECKS
390 if (CachedType)
391 return CachedType;
392#endif
393
394 // If we don't have it in the cache, convert it now.
395 llvm::Type *ResultType = nullptr;
396 switch (Ty->getTypeClass()) {
397 case Type::Record: // Handled above.
398#define TYPE(Class, Base)
399#define ABSTRACT_TYPE(Class, Base)
400#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
401#define DEPENDENT_TYPE(Class, Base) case Type::Class:
402#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
403#include "clang/AST/TypeNodes.inc"
404 llvm_unreachable("Non-canonical or dependent types aren't possible.");
405
406 case Type::Builtin: {
407 switch (cast<BuiltinType>(Ty)->getKind()) {
408 case BuiltinType::Void:
409 case BuiltinType::ObjCId:
410 case BuiltinType::ObjCClass:
411 case BuiltinType::ObjCSel:
412 // LLVM void type can only be used as the result of a function call. Just
413 // map to the same as char.
414 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
415 break;
416
417 case BuiltinType::Bool:
418 // Note that we always return bool as i1 for use as a scalar type.
419 ResultType = llvm::Type::getInt1Ty(getLLVMContext());
420 break;
421
422 case BuiltinType::Char_S:
423 case BuiltinType::Char_U:
424 case BuiltinType::SChar:
425 case BuiltinType::UChar:
426 case BuiltinType::Short:
427 case BuiltinType::UShort:
428 case BuiltinType::Int:
429 case BuiltinType::UInt:
430 case BuiltinType::Long:
431 case BuiltinType::ULong:
432 case BuiltinType::LongLong:
433 case BuiltinType::ULongLong:
434 case BuiltinType::WChar_S:
435 case BuiltinType::WChar_U:
436 case BuiltinType::Char8:
437 case BuiltinType::Char16:
438 case BuiltinType::Char32:
439 case BuiltinType::ShortAccum:
440 case BuiltinType::Accum:
441 case BuiltinType::LongAccum:
442 case BuiltinType::UShortAccum:
443 case BuiltinType::UAccum:
444 case BuiltinType::ULongAccum:
445 case BuiltinType::ShortFract:
446 case BuiltinType::Fract:
447 case BuiltinType::LongFract:
448 case BuiltinType::UShortFract:
449 case BuiltinType::UFract:
450 case BuiltinType::ULongFract:
451 case BuiltinType::SatShortAccum:
452 case BuiltinType::SatAccum:
453 case BuiltinType::SatLongAccum:
454 case BuiltinType::SatUShortAccum:
455 case BuiltinType::SatUAccum:
456 case BuiltinType::SatULongAccum:
457 case BuiltinType::SatShortFract:
458 case BuiltinType::SatFract:
459 case BuiltinType::SatLongFract:
460 case BuiltinType::SatUShortFract:
461 case BuiltinType::SatUFract:
462 case BuiltinType::SatULongFract:
463 ResultType = llvm::IntegerType::get(getLLVMContext(),
464 static_cast<unsigned>(Context.getTypeSize(T)));
465 break;
466
467 case BuiltinType::Float16:
468 ResultType = llvm::Type::getFloatingPointTy(
469 getLLVMContext(), Context.getFloatTypeSemantics(T));
470 break;
471
472 case BuiltinType::Half:
473 // Half FP can either be storage-only (lowered to i16 for ABI purposes) or
474 // native.
475 ResultType = llvm::Type::getFloatingPointTy(
476 getLLVMContext(), Context.getFloatTypeSemantics(T));
477 break;
478 case BuiltinType::LongDouble:
479 LongDoubleReferenced = true;
480 [[fallthrough]];
481 case BuiltinType::BFloat16:
482 case BuiltinType::Float:
483 case BuiltinType::Double:
484 case BuiltinType::Float128:
485 case BuiltinType::Ibm128:
486 ResultType = llvm::Type::getFloatingPointTy(
487 getLLVMContext(), Context.getFloatTypeSemantics(T));
488 break;
489
490 case BuiltinType::NullPtr:
491 // Model std::nullptr_t as i8*
492 ResultType = llvm::PointerType::getUnqual(getLLVMContext());
493 break;
494
495 case BuiltinType::MetaInfo:
496 // FIXME(Reflection): once consteval-only types are supported,
497 // make this an llvm_unreachable instead because reflection
498 // should not reach here
499 ResultType = llvm::IntegerType::get(getLLVMContext(), 64);
500 break;
501
502 case BuiltinType::UInt128:
503 case BuiltinType::Int128:
504 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
505 break;
506
507#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
508 case BuiltinType::Id:
509#include "clang/Basic/OpenCLImageTypes.def"
510#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
511 case BuiltinType::Id:
512#include "clang/Basic/OpenCLExtensionTypes.def"
513 case BuiltinType::OCLSampler:
514 case BuiltinType::OCLEvent:
515 case BuiltinType::OCLClkEvent:
516 case BuiltinType::OCLQueue:
517 case BuiltinType::OCLReserveID:
518 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
519 break;
520#define SVE_VECTOR_TYPE(Name, MangledName, Id, SingletonId) \
521 case BuiltinType::Id:
522#define SVE_PREDICATE_TYPE(Name, MangledName, Id, SingletonId) \
523 case BuiltinType::Id:
524#include "clang/Basic/AArch64ACLETypes.def"
525 {
527 Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
528 // The `__mfp8` type maps to `<1 x i8>` which can't be used to build
529 // a <N x i8> vector type, hence bypass the call to `ConvertType` for
530 // the element type and create the vector type directly.
531 auto *EltTy = Info.ElementType->isMFloat8Type()
532 ? llvm::Type::getInt8Ty(getLLVMContext())
533 : ConvertType(Info.ElementType);
534 auto *VTy = llvm::VectorType::get(EltTy, Info.EC);
535 switch (Info.NumVectors) {
536 default:
537 llvm_unreachable("Expected 1, 2, 3 or 4 vectors!");
538 case 1:
539 return VTy;
540 case 2:
541 return llvm::StructType::get(VTy, VTy);
542 case 3:
543 return llvm::StructType::get(VTy, VTy, VTy);
544 case 4:
545 return llvm::StructType::get(VTy, VTy, VTy, VTy);
546 }
547 }
548 case BuiltinType::SveCount:
549 return llvm::TargetExtType::get(getLLVMContext(), "aarch64.svcount");
550 case BuiltinType::MFloat8:
551 return llvm::VectorType::get(llvm::Type::getInt8Ty(getLLVMContext()), 1,
552 false);
553#define PPC_VECTOR_TYPE(Name, Id, Size) \
554 case BuiltinType::Id: \
555 ResultType = \
556 llvm::FixedVectorType::get(ConvertType(Context.BoolTy), Size); \
557 break;
558#include "clang/Basic/PPCTypes.def"
559#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
560#include "clang/Basic/RISCVVTypes.def"
561 {
563 Context.getBuiltinVectorTypeInfo(cast<BuiltinType>(Ty));
564 if (Info.NumVectors != 1) {
565 unsigned I8EltCount =
566 Info.EC.getKnownMinValue() *
567 ConvertType(Info.ElementType)->getScalarSizeInBits() / 8;
568 return llvm::TargetExtType::get(
569 getLLVMContext(), "riscv.vector.tuple",
570 llvm::ScalableVectorType::get(
571 llvm::Type::getInt8Ty(getLLVMContext()), I8EltCount),
572 Info.NumVectors);
573 }
574 return llvm::ScalableVectorType::get(ConvertType(Info.ElementType),
575 Info.EC.getKnownMinValue());
576 }
577#define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \
578 case BuiltinType::Id: { \
579 if (BuiltinType::Id == BuiltinType::WasmExternRef) \
580 ResultType = CGM.getTargetCodeGenInfo().getWasmExternrefReferenceType(); \
581 else \
582 llvm_unreachable("Unexpected wasm reference builtin type!"); \
583 } break;
584#include "clang/Basic/WebAssemblyReferenceTypes.def"
585#define AMDGPU_OPAQUE_PTR_TYPE(Name, Id, SingletonId, Width, Align, AS) \
586 case BuiltinType::Id: { \
587 if (BuiltinType::Id == BuiltinType::AMDGPUTexture) { \
588 return llvm::FixedVectorType::get( \
589 llvm::Type::getInt32Ty(getLLVMContext()), 8); \
590 } \
591 return llvm::PointerType::get(getLLVMContext(), AS); \
592 }
593#define AMDGPU_NAMED_BARRIER_TYPE(Name, Id, SingletonId, Width, Align, Scope) \
594 case BuiltinType::Id: \
595 return llvm::TargetExtType::get(getLLVMContext(), "amdgcn.named.barrier", \
596 {}, {Scope});
597#define AMDGPU_FEATURE_PREDICATE_TYPE(Name, Id, SingletonId, Width, Align) \
598 case BuiltinType::Id: \
599 return ConvertType(getContext().getLogicalOperationType());
600#include "clang/Basic/AMDGPUTypes.def"
601#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
602#include "clang/Basic/HLSLIntangibleTypes.def"
603 ResultType = CGM.getHLSLRuntime().convertHLSLSpecificType(Ty);
604 break;
605#define HLSL_PACKED_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
606#include "clang/Basic/HLSLPackedTypes.def"
607 ResultType = llvm::IntegerType::get(getLLVMContext(), 32);
608 break;
609#define SPIRV_TYPE(Name, Id, SingletonId) \
610 case BuiltinType::Id: \
611 return llvm::TargetExtType::get(getLLVMContext(), "spirv.Event");
612#include "clang/Basic/SPIRVTypes.def"
613 case BuiltinType::Dependent:
614#define BUILTIN_TYPE(Id, SingletonId)
615#define PLACEHOLDER_TYPE(Id, SingletonId) \
616 case BuiltinType::Id:
617#include "clang/AST/BuiltinTypes.def"
618 llvm_unreachable("Unexpected placeholder builtin type!");
619 }
620 break;
621 }
622 case Type::Auto:
623 case Type::DeducedTemplateSpecialization:
624 llvm_unreachable("Unexpected undeduced type!");
625 case Type::Complex: {
626 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
627 ResultType = llvm::StructType::get(EltTy, EltTy);
628 break;
629 }
630 case Type::LValueReference:
631 case Type::RValueReference: {
632 const ReferenceType *RTy = cast<ReferenceType>(Ty);
633 QualType ETy = RTy->getPointeeType();
634 unsigned AS = getTargetAddressSpace(ETy);
635 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
636 break;
637 }
638 case Type::Pointer: {
639 const PointerType *PTy = cast<PointerType>(Ty);
640 QualType ETy = PTy->getPointeeType();
642 ResultType = CGM.getTargetCodeGenInfo().getWasmFuncrefReferenceType();
643 break;
644 }
645 unsigned AS = getTargetAddressSpace(ETy);
646 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
647 break;
648 }
649
650 case Type::VariableArray: {
652 assert(A->getIndexTypeCVRQualifiers() == 0 &&
653 "FIXME: We only handle trivial array types so far!");
654 // VLAs resolve to the innermost element type; this matches
655 // the return of alloca, and there isn't any obviously better choice.
656 ResultType = ConvertTypeForMem(A->getElementType());
657 break;
658 }
659 case Type::IncompleteArray: {
661 assert(A->getIndexTypeCVRQualifiers() == 0 &&
662 "FIXME: We only handle trivial array types so far!");
663 // int X[] -> [0 x int], unless the element type is not sized. If it is
664 // unsized (e.g. an incomplete struct) just use [0 x i8].
665 ResultType = ConvertTypeForMem(A->getElementType());
666 if (!ResultType->isSized()) {
667 SkippedLayout = true;
668 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
669 }
670 ResultType = llvm::ArrayType::get(ResultType, 0);
671 break;
672 }
673 case Type::ArrayParameter:
674 case Type::ConstantArray: {
676 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
677
678 // Lower arrays of undefined struct type to arrays of i8 just to have a
679 // concrete type.
680 if (!EltTy->isSized()) {
681 SkippedLayout = true;
682 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
683 }
684
685 ResultType = llvm::ArrayType::get(EltTy, A->getZExtSize());
686 break;
687 }
688 case Type::ExtVector:
689 case Type::Vector: {
690 const auto *VT = cast<VectorType>(Ty);
691 // An ext_vector_type of Bool is really a vector of bits.
692 llvm::Type *IRElemTy = VT->isPackedVectorBoolType(Context)
693 ? llvm::Type::getInt1Ty(getLLVMContext())
694 : VT->getElementType()->isMFloat8Type()
695 ? llvm::Type::getInt8Ty(getLLVMContext())
696 : ConvertType(VT->getElementType());
697 ResultType = llvm::FixedVectorType::get(IRElemTy, VT->getNumElements());
698 break;
699 }
700 case Type::ConstantMatrix: {
702 ResultType =
703 llvm::FixedVectorType::get(ConvertType(MT->getElementType()),
704 MT->getNumRows() * MT->getNumColumns());
705 break;
706 }
707 case Type::FunctionNoProto:
708 case Type::FunctionProto:
709 ResultType = ConvertFunctionTypeInternal(T);
710 break;
711 case Type::ObjCObject:
712 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
713 break;
714
715 case Type::ObjCInterface: {
716 // Objective-C interfaces are always opaque (outside of the
717 // runtime, which can do whatever it likes); we never refine
718 // these.
719 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
720 if (!T)
721 T = llvm::StructType::create(getLLVMContext());
722 ResultType = T;
723 break;
724 }
725
726 case Type::ObjCObjectPointer:
727 ResultType = llvm::PointerType::getUnqual(getLLVMContext());
728 break;
729
730 case Type::Enum: {
731 const auto *ED = Ty->castAsEnumDecl();
732 if (ED->isCompleteDefinition() || ED->isFixed())
733 return ConvertType(ED->getIntegerType());
734 // Return a placeholder 'i32' type. This can be changed later when the
735 // type is defined (see UpdateCompletedType), but is likely to be the
736 // "right" answer.
737 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
738 break;
739 }
740
741 case Type::BlockPointer: {
742 // Block pointers lower to function type. For function type,
743 // getTargetAddressSpace() returns default address space for
744 // function pointer i.e. program address space. Therefore, for block
745 // pointers, it is important to pass the pointee AST address space when
746 // calling getTargetAddressSpace(), to ensure that we get the LLVM IR
747 // address space for data pointers and not function pointers.
748 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
749 unsigned AS = Context.getTargetAddressSpace(FTy.getAddressSpace());
750 ResultType = llvm::PointerType::get(getLLVMContext(), AS);
751 break;
752 }
753
754 case Type::MemberPointer: {
755 auto *MPTy = cast<MemberPointerType>(Ty);
756 if (!getCXXABI().isMemberPointerConvertible(MPTy)) {
757 CanQualType T = CGM.getContext().getCanonicalTagType(
758 MPTy->getMostRecentCXXRecordDecl());
759 auto Insertion =
760 RecordsWithOpaqueMemberPointers.try_emplace(T.getTypePtr());
761 if (Insertion.second)
762 Insertion.first->second = llvm::StructType::create(getLLVMContext());
763 ResultType = Insertion.first->second;
764 } else {
765 ResultType = getCXXABI().ConvertMemberPointerType(MPTy);
766 }
767 break;
768 }
769
770 case Type::Atomic: {
771 QualType valueType = cast<AtomicType>(Ty)->getValueType();
772 ResultType = ConvertTypeForMem(valueType);
773
774 // Pad out to the inflated size if necessary.
775 uint64_t valueSize = Context.getTypeSize(valueType);
776 uint64_t atomicSize = Context.getTypeSize(Ty);
777 if (valueSize != atomicSize) {
778 assert(valueSize < atomicSize);
779 llvm::Type *elts[] = {
780 ResultType,
781 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
782 };
783 ResultType =
784 llvm::StructType::get(getLLVMContext(), llvm::ArrayRef(elts));
785 }
786 break;
787 }
788 case Type::Pipe: {
789 ResultType = CGM.getOpenCLRuntime().getPipeType(cast<PipeType>(Ty));
790 break;
791 }
792 case Type::BitInt: {
793 const auto &EIT = cast<BitIntType>(Ty);
794 ResultType = llvm::Type::getIntNTy(getLLVMContext(), EIT->getNumBits());
795 break;
796 }
797 case Type::HLSLAttributedResource:
798 case Type::HLSLInlineSpirv:
799 ResultType = CGM.getHLSLRuntime().convertHLSLSpecificType(Ty);
800 break;
801 case Type::OverflowBehavior:
802 ResultType =
803 ConvertType(dyn_cast<OverflowBehaviorType>(Ty)->getUnderlyingType());
804 break;
805 }
806
807 assert(ResultType && "Didn't convert a type?");
808 assert((!CachedType || CachedType == ResultType) &&
809 "Cached type doesn't match computed type");
810
811 TypeCache[Ty] = ResultType;
812 return ResultType;
813}
814
818
820 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
821}
822
823/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
825 // TagDecl's are not necessarily unique, instead use the (clang)
826 // type connected to the decl.
827 const Type *Key = Context.getCanonicalTagType(RD).getTypePtr();
828
829 llvm::StructType *&Entry = RecordDeclTypes[Key];
830
831 // If we don't have a StructType at all yet, create the forward declaration.
832 if (!Entry) {
833 Entry = llvm::StructType::create(getLLVMContext());
834 addRecordTypeName(RD, Entry, "");
835 }
836 llvm::StructType *Ty = Entry;
837
838 // If this is still a forward declaration, or the LLVM type is already
839 // complete, there's nothing more to do.
840 RD = RD->getDefinition();
841 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
842 return Ty;
843
844 // Force conversion of non-virtual base classes recursively.
845 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
846 for (const auto &I : CRD->bases()) {
847 if (I.isVirtual()) continue;
848 ConvertRecordDeclType(I.getType()->castAsRecordDecl());
849 }
850 }
851
852 // Layout fields.
853 std::unique_ptr<CGRecordLayout> Layout = ComputeRecordLayout(RD, Ty);
854 CGRecordLayouts[Key] = std::move(Layout);
855
856 // If this struct blocked a FunctionType conversion, then recompute whatever
857 // was derived from that.
858 // FIXME: This is hugely overconservative.
859 if (SkippedLayout)
860 TypeCache.clear();
861
862 return Ty;
863}
864
865/// getCGRecordLayout - Return record layout info for the given record decl.
866const CGRecordLayout &
868 const Type *Key = Context.getCanonicalTagType(RD).getTypePtr();
869
870 auto I = CGRecordLayouts.find(Key);
871 if (I != CGRecordLayouts.end())
872 return *I->second;
873 // Compute the type information.
875
876 // Now try again.
877 I = CGRecordLayouts.find(Key);
878
879 assert(I != CGRecordLayouts.end() &&
880 "Unable to find record layout information for type");
881 return *I->second;
882}
883
885 assert((T->isAnyPointerType() || T->isBlockPointerType() ||
886 T->isNullPtrType()) &&
887 "Invalid type");
888 return isZeroInitializable(T);
889}
890
892 if (T->getAs<PointerType>() || T->isNullPtrType())
893 return Context.getTargetNullPointerValue(T) == 0;
894
895 if (const auto *AT = Context.getAsArrayType(T)) {
897 return true;
898 if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
899 if (Context.getConstantArrayElementCount(CAT) == 0)
900 return true;
901 T = Context.getBaseElementType(T);
902 }
903
904 // Records are non-zero-initializable if they contain any
905 // non-zero-initializable subobjects.
906 if (const auto *RD = T->getAsRecordDecl())
907 return isZeroInitializable(RD);
908
909 // We have to ask the ABI about member pointers.
910 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
911 return getCXXABI().isZeroInitializable(MPT);
912
913 // HLSL Inline SPIR-V types are non-zero-initializable.
914 if (T->getAs<HLSLInlineSpirvType>())
915 return false;
916
917 // Everything else is okay.
918 return true;
919}
920
924
926 // Return the address space for the type. If the type is a
927 // function type without an address space qualifier, the
928 // program address space is used. Otherwise, the target picks
929 // the best address space based on the type information
930 return T->isFunctionType() && !T.hasAddressSpace()
931 ? getDataLayout().getProgramAddressSpace()
932 : getContext().getTargetAddressSpace(T.getAddressSpace());
933}
Defines the clang::ASTContext interface.
static Decl::Kind getKind(const Decl *D)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines AST-level helper utilities for matrix types.
static QualType getUnderlyingType(const SubRegion *R)
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type)
const clang::PrintingPolicy & getPrintingPolicy() const
Definition ASTContext.h:909
unsigned getTargetAddressSpace(LangAS AS) const
QualType getElementType() const
Definition TypeBase.h:3832
unsigned getIndexTypeCVRQualifiers() const
Definition TypeBase.h:3842
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
static CanQual< T > CreateUnsafe(QualType Other)
Builds a canonical type from a QualType.
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:58
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Implements C++ ABI-specific code generation functions.
Definition CGCXXABI.h:43
virtual bool isMemberPointerConvertible(const MemberPointerType *MPT) const
Return whether or not a member pointers type is convertible to an IR type.
Definition CGCXXABI.h:213
virtual llvm::Type * ConvertMemberPointerType(const MemberPointerType *MPT)
Find the LLVM type used to represent the given member pointer type.
Definition CGCXXABI.cpp:42
virtual bool isZeroInitializable(const MemberPointerType *MPT)
Return true if the given member pointer can be zero-initialized (in the C++ sense) with an LLVM zeroi...
Definition CGCXXABI.cpp:120
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition CGDebugInfo.h:59
CGFunctionInfo - Class to encapsulate the information about a function definition.
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
bool isZeroInitializable() const
Check whether this struct can be C++ zero-initialized with a zeroinitializer.
This class organizes the cross-function state that is used while generating LLVM code.
bool isPaddedAtomicType(QualType type)
CodeGenTypes(CodeGenModule &cgm)
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
CGCXXABI & getCXXABI() const
bool isPointerZeroInitializable(QualType T)
Check if the pointer type can be zero-initialized (in the C++ sense) with an LLVM zeroinitializer.
const CodeGenOptions & getCodeGenOpts() const
ASTContext & getContext() const
const CGFunctionInfo & arrangeFreeFunctionType(CanQual< FunctionProtoType > Ty)
Arrange the argument and result information for a value of the given freestanding function type.
Definition CGCall.cpp:257
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition CGCall.cpp:2081
bool isFuncTypeConvertible(const FunctionType *FT)
isFuncTypeConvertible - Utility to check whether a function type can be converted to an LLVM type (i....
const TargetInfo & getTarget() const
std::unique_ptr< CGRecordLayout > ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty)
Compute a new LLVM record layout object for the given record.
llvm::Type * convertTypeForLoadStore(QualType T, llvm::Type *LLVMTy=nullptr)
Given that T is a scalar type, return the IR type that should be used for load and store operations.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
unsigned getTargetAddressSpace(QualType T) const
llvm::StructType * ConvertRecordDeclType(const RecordDecl *TD)
ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
void RefreshTypeCacheForClass(const CXXRecordDecl *RD)
Remove stale types from the type cache when an inheritance model gets assigned to a class.
bool isRecordLayoutComplete(const Type *Ty) const
isRecordLayoutComplete - Return true if the specified type is already completely laid out.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
CodeGenModule & getCGM() const
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
llvm::LLVMContext & getLLVMContext()
bool typeRequiresSplitIntoByteArray(QualType ASTTy, llvm::Type *LLVMTy=nullptr)
Check whether the given type needs to be laid out in memory using an opaque byte-array type because i...
const llvm::DataLayout & getDataLayout() const
bool isFuncParamTypeConvertible(QualType Ty)
isFuncParamTypeConvertible - Return true if the specified type in a function parameter or result posi...
bool isZeroInitializable(QualType T)
IsZeroInitializable - Return whether a type can be zero-initialized (in the C++ sense) with an LLVM z...
void addRecordTypeName(const RecordDecl *RD, llvm::StructType *Ty, StringRef suffix)
addRecordTypeName - Compute a name from the given record decl with an optional suffix and name the gi...
Represents the canonical version of C arrays with a specified constant size.
Definition TypeBase.h:3858
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition TypeBase.h:3934
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4490
unsigned getNumColumns() const
Returns the number of columns in the matrix.
Definition TypeBase.h:4512
unsigned getNumRows() const
Returns the number of rows in the matrix.
Definition TypeBase.h:4509
unsigned getNumElementsFlattened() const
Returns the number of elements required to embed the matrix into a vector.
Definition TypeBase.h:4517
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:550
DeclContext * getDeclContext()
Definition DeclBase.h:456
Represents an enum.
Definition Decl.h:4150
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Definition TypeBase.h:4994
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5416
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4612
QualType getReturnType() const
Definition TypeBase.h:4952
Represents a C array with an unspecified size.
Definition TypeBase.h:4007
QualType getElementType() const
Returns type of the elements being stored in the matrix.
Definition TypeBase.h:4454
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3751
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:296
void printQualifiedName(raw_ostream &OS) const
Returns a human-readable qualified name for this declaration, like A::B::i, for i being member of nam...
Definition Decl.cpp:1689
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3403
QualType getPointeeType() const
Definition TypeBase.h:3413
A (possibly-)qualified type.
Definition TypeBase.h:938
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8446
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8572
bool isCanonical() const
Definition TypeBase.h:8503
Represents a struct/union/class.
Definition Decl.h:4464
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition Decl.h:4648
Base for LValueReferenceType and RValueReferenceType.
Definition TypeBase.h:3678
QualType getPointeeType() const
Definition TypeBase.h:3700
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3856
StringRef getKindName() const
Definition Decl.h:4052
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3957
TypedefNameDecl * getTypedefNameForAnonDecl() const
Definition Decl.h:4093
void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override
Pretty-print the unqualified name of this declaration.
Definition Decl.cpp:5109
bool isDependentType() const
Whether this declaration declares a type that is dependent, i.e., a type that somehow depends on temp...
Definition Decl.h:4002
bool isMFloat8Type() const
Definition TypeBase.h:9097
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
EnumDecl * castAsEnumDecl() const
Definition Type.h:59
TypeClass getTypeClass() const
Definition TypeBase.h:2449
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9299
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3701
Represents a C array with a specified size that is not an integer-constant-expression.
Definition TypeBase.h:4064
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
Definition CGValue.h:155
const internal::VariadicAllOfMatcher< Type > type
Matches Types 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)
Definition Address.h:330
bool isMatrixRowMajor(const LangOptions &LangOpts, QualType T)
Returns true if matrices of T should be laid out in row-major order.
Definition MatrixUtils.h:25
const FunctionProtoType * T
U cast(CodeGen::Address addr)
Definition Address.h:327
Describes how types, statements, expressions, and declarations should be printed.
unsigned SuppressInlineNamespace
Suppress printing parts of scope specifiers that correspond to inline namespaces.