1//===--- CIRGenTypes.h - Type translation for CIR CodeGen -------*- C++ -*-===//
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 -> CIR type lowering.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
14#define LLVM_CLANG_LIB_CODEGEN_CODEGENTYPES_H
15
16#include "ABIInfo.h"
17#include "CIRGenFunctionInfo.h"
18#include "CIRGenRecordLayout.h"
19
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Type.h"
22#include "clang/Basic/ABI.h"
23#include "clang/CIR/Dialect/IR/CIRTypes.h"
24
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/SmallPtrSet.h"
27
28namespace clang {
29class ASTContext;
30class FunctionType;
31class GlobalDecl;
32class QualType;
33class TargetInfo;
34class Type;
35} // namespace clang
36
37namespace mlir {
38class Type;
39}
40
41namespace clang::CIRGen {
42
43class CallArgList;
44class CIRGenBuilderTy;
45class CIRGenCXXABI;
46class CIRGenModule;
47class FunctionArgList;
48
49/// This class organizes the cross-module state that is used while lowering
50/// AST types to CIR types.
51class CIRGenTypes {
52 CIRGenModule &cgm;
53 clang::ASTContext &astContext;
54 CIRGenBuilderTy &builder;
55 CIRGenCXXABI &theCXXABI;
56
57 const ABIInfo &theABIInfo;
58
59 /// Contains the CIR type for any converted RecordDecl.
60 llvm::DenseMap<const clang::Type *, std::unique_ptr<CIRGenRecordLayout>>
61 cirGenRecordLayouts;
62
63 /// Contains the CIR type for any converted RecordDecl
64 llvm::DenseMap<const clang::Type *, cir::RecordType> recordDeclTypes;
65
66 /// Hold memoized CIRGenFunctionInfo results
67 llvm::FoldingSet<CIRGenFunctionInfo> functionInfos;
68
69 /// This set keeps track of records that we're currently converting to a CIR
70 /// type. For example, when converting:
71 /// struct A { struct B { int x; } } when processing 'x', the 'A' and 'B'
72 /// types will be in this set.
73 llvm::SmallPtrSet<const clang::Type *, 4> recordsBeingLaidOut;
74
75 llvm::SmallVector<const clang::RecordDecl *, 8> deferredRecords;
76
77 /// Cache of record type keys known to be safe to convert (i.e.,
78 /// isSafeToConvert returned true). Cleared whenever recordsBeingLaidOut
79 /// changes, since the safety result depends on which records are currently
80 /// being laid out.
81 llvm::DenseSet<const clang::Type *> safeToConvertCache;
82
83 /// Heper for convertType.
84 mlir::Type convertFunctionTypeInternal(clang::QualType ft);
85
86public:
87 CIRGenTypes(CIRGenModule &cgm);
88 ~CIRGenTypes();
89
90 CIRGenBuilderTy &getBuilder() const { return builder; }
91 CIRGenModule &getCGModule() const { return cgm; }
92
93 /// Utility to check whether a function type can be converted to a CIR type
94 /// (i.e. doesn't depend on an incomplete tag type).
95 bool isFuncTypeConvertible(const clang::FunctionType *ft);
96 bool isFuncParamTypeConvertible(clang::QualType type);
97
98 /// Derives the 'this' type for CIRGen purposes, i.e. ignoring method CVR
99 /// qualification.
100 clang::CanQualType deriveThisType(const clang::CXXRecordDecl *rd,
101 const clang::CXXMethodDecl *md);
102
103 /// This map of clang::Type to mlir::Type (which includes CIR type) is a
104 /// cache of types that have already been processed.
105 using TypeCacheTy = llvm::DenseMap<const clang::Type *, mlir::Type>;
106 TypeCacheTy typeCache;
107
108 mlir::MLIRContext &getMLIRContext() const;
109 clang::ASTContext &getASTContext() const { return astContext; }
110
111 bool isRecordLayoutComplete(const clang::Type *ty) const;
112 bool noRecordsBeingLaidOut() const { return recordsBeingLaidOut.empty(); }
113 bool isRecordBeingLaidOut(const clang::Type *ty) const {
114 return recordsBeingLaidOut.count(Ptr: ty);
115 }
116
117 /// Check if a record type key is in the safe-to-convert cache.
118 bool isCachedSafeToConvert(const clang::Type *key) const {
119 return safeToConvertCache.count(V: key);
120 }
121
122 /// Add a record type key to the safe-to-convert cache.
123 void cacheSafeToConvert(const clang::Type *key) {
124 safeToConvertCache.insert(V: key);
125 }
126
127 const ABIInfo &getABIInfo() const { return theABIInfo; }
128
129 /// Convert a Clang type into a mlir::Type.
130 mlir::Type convertType(clang::QualType type);
131
132 mlir::Type convertRecordDeclType(const clang::RecordDecl *recordDecl);
133
134 std::unique_ptr<CIRGenRecordLayout>
135 computeRecordLayout(const clang::RecordDecl *rd, cir::RecordType *ty);
136
137 std::string getRecordTypeName(const clang::RecordDecl *,
138 llvm::StringRef suffix);
139
140 const CIRGenRecordLayout &getCIRGenRecordLayout(const clang::RecordDecl *rd);
141
142 /// Convert type T into an mlir::Type. This differs from convertType in that
143 /// it is used to convert to the memory representation for a type. For
144 /// example, the scalar representation for bool is i1, but the memory
145 /// representation is usually i8 or i32, depending on the target.
146 // TODO: convert this comment to account for MLIR's equivalence
147 mlir::Type convertTypeForMem(clang::QualType, bool forBitField = false);
148
149 /// Get the CIR function type for \arg Info.
150 cir::FuncType getFunctionType(const CIRGenFunctionInfo &info);
151
152 cir::FuncType getFunctionType(clang::GlobalDecl gd);
153
154 /// Determine if a C++ inheriting constructor should have parameters matching
155 /// those of its inherited constructor.
156 bool inheritingCtorHasParams(const InheritedConstructor &inherited,
157 CXXCtorType type);
158
159 // The arrangement methods are split into three families:
160 // - those meant to drive the signature and prologue/epilogue
161 // of a function declaration or definition,
162 // - those meant for the computation of the CIR type for an abstract
163 // appearance of a function, and
164 // - those meant for performing the CIR-generation of a call.
165 // They differ mainly in how they deal with optional (i.e. variadic)
166 // arguments, as well as unprototyped functions.
167 //
168 // Key points:
169 // - The CIRGenFunctionInfo for emitting a specific call site must include
170 // entries for the optional arguments.
171 // - The function type used at the call site must reflect the formal
172 // signature
173 // of the declaration being called, or else the call will go away.
174 // - For the most part, unprototyped functions are called by casting to a
175 // formal signature inferred from the specific argument types used at the
176 // call-site. However, some targets (e.g. x86-64) screw with this for
177 // compatability reasons.
178
179 const CIRGenFunctionInfo &arrangeGlobalDeclaration(GlobalDecl gd);
180
181 /// UpdateCompletedType - when we find the full definition for a TagDecl,
182 /// replace the 'opaque' type we previously made for it if applicable.
183 void updateCompletedType(const clang::TagDecl *td);
184
185 /// Free functions are functions that are compatible with an ordinary C
186 /// function pointer type.
187 const CIRGenFunctionInfo &
188 arrangeFunctionDeclaration(const clang::FunctionDecl *fd);
189
190 /// Arrange the function info for a device kernel caller entry point (e.g. a
191 /// SYCL kernel caller).
192 const CIRGenFunctionInfo &
193 arrangeDeviceKernelCallerDeclaration(clang::QualType resultType,
194 const FunctionArgList &args);
195
196 /// A builtin function is a freestanding function using the default
197 /// C conventions.
198 const CIRGenFunctionInfo &arrangeBuiltinFunctionCall(QualType resultType,
199 const CallArgList &args);
200
201 /// Return whether a type can be zero-initialized (in the C++ sense) with an
202 /// LLVM zeroinitializer.
203 bool isZeroInitializable(clang::QualType ty);
204 bool isZeroInitializable(const RecordDecl *rd);
205
206 const CIRGenFunctionInfo &arrangeCXXConstructorCall(
207 const CallArgList &args, const clang::CXXConstructorDecl *d,
208 clang::CXXCtorType ctorKind, unsigned extraPrefixArgs,
209 unsigned extraSuffixArgs, bool passProtoArgs = true);
210
211 const CIRGenFunctionInfo &
212 arrangeCXXMethodCall(const CallArgList &args,
213 const clang::FunctionProtoType *type,
214 RequiredArgs required, unsigned numPrefixArgs);
215
216 /// C++ methods have some special rules and also have implicit parameters.
217 const CIRGenFunctionInfo &
218 arrangeCXXMethodDeclaration(const clang::CXXMethodDecl *md);
219 const CIRGenFunctionInfo &arrangeCXXStructorDeclaration(clang::GlobalDecl gd);
220
221 const CIRGenFunctionInfo &
222 arrangeCXXMethodType(const clang::CXXRecordDecl *rd,
223 const clang::FunctionProtoType *ftp,
224 const clang::CXXMethodDecl *md);
225
226 const CIRGenFunctionInfo &arrangeFreeFunctionCall(const CallArgList &args,
227 const FunctionType *fnType);
228
229 /// Convert a clang calling convention to a CIR calling convention.
230 cir::CallingConv clangCallConvToCIRCallConv(clang::CallingConv cc);
231
232 const CIRGenFunctionInfo &
233 arrangeCIRFunctionInfo(CanQualType returnType, bool isInstanceMethod,
234 llvm::ArrayRef<CanQualType> argTypes,
235 FunctionType::ExtInfo info, RequiredArgs required);
236
237 const CIRGenFunctionInfo &
238 arrangeFreeFunctionType(CanQual<FunctionProtoType> fpt);
239 const CIRGenFunctionInfo &
240 arrangeFreeFunctionType(CanQual<FunctionNoProtoType> fnpt);
241
242 unsigned getTargetAddressSpace(QualType ty) const;
243
244 /// Returns the CIR address space for a pointer/reference to \p pointeeTy, or
245 /// a null attribute for the default address space. A default-address-space
246 /// pointee is resolved through getTargetAddressSpace (e.g. generic for SYCL
247 /// device data, program address space for functions), matching classic
248 /// CodeGen.
249 mlir::ptr::MemorySpaceAttrInterface
250 getPointerAddressSpace(clang::QualType pointeeTy) const;
251};
252
253} // namespace clang::CIRGen
254
255#endif
256