| 1 |
//===--- CIRGenModule.h - Per-Module state for CIR gen ----------*- 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 internal per-translation-unit state used for CIR translation. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H |
| 14 |
#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H |
| 15 |
|
| 16 |
#include "CIRGenBuilder.h" |
| 17 |
#include "CIRGenCall.h" |
| 18 |
#include "CIRGenTypeCache.h" |
| 19 |
#include "CIRGenTypes.h" |
| 20 |
#include "CIRGenVTables.h" |
| 21 |
#include "CIRGenValue.h" |
| 22 |
|
| 23 |
#include "clang/AST/CharUnits.h" |
| 24 |
#include "clang/CIR/Dialect/IR/CIRDataLayout.h" |
| 25 |
#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 26 |
|
| 27 |
#include "TargetInfo.h" |
| 28 |
#include "mlir/IR/Builders.h" |
| 29 |
#include "mlir/IR/BuiltinOps.h" |
| 30 |
#include "mlir/IR/MLIRContext.h" |
| 31 |
#include "clang/AST/Decl.h" |
| 32 |
#include "clang/Basic/SourceManager.h" |
| 33 |
#include "clang/Basic/TargetInfo.h" |
| 34 |
#include "clang/CIR/Dialect/IR/CIROpsEnums.h" |
| 35 |
#include "llvm/ADT/StringRef.h" |
| 36 |
#include "llvm/TargetParser/Triple.h" |
| 37 |
|
| 38 |
namespace clang { |
| 39 |
class ASTContext; |
| 40 |
class CodeGenOptions; |
| 41 |
class Decl; |
| 42 |
class GlobalDecl; |
| 43 |
class LangOptions; |
| 44 |
class TargetInfo; |
| 45 |
class VarDecl; |
| 46 |
|
| 47 |
namespace CIRGen { |
| 48 |
|
| 49 |
class CIRGenFunction; |
| 50 |
class CIRGenCXXABI; |
| 51 |
|
| 52 |
enum ForDefinition_t : bool { NotForDefinition = false, ForDefinition = true }; |
| 53 |
|
| 54 |
/// This class organizes the cross-function state that is used while generating |
| 55 |
/// CIR code. |
| 56 |
class CIRGenModule : public CIRGenTypeCache { |
| 57 |
CIRGenModule(CIRGenModule &) = delete; |
| 58 |
CIRGenModule &operator=(CIRGenModule &) = delete; |
| 59 |
|
| 60 |
public: |
| 61 |
CIRGenModule(mlir::MLIRContext &mlirContext, clang::ASTContext &astContext, |
| 62 |
const clang::CodeGenOptions &cgo, |
| 63 |
clang::DiagnosticsEngine &diags); |
| 64 |
|
| 65 |
~CIRGenModule(); |
| 66 |
|
| 67 |
private: |
| 68 |
mutable std::unique_ptr<TargetCIRGenInfo> theTargetCIRGenInfo; |
| 69 |
|
| 70 |
CIRGenBuilderTy builder; |
| 71 |
|
| 72 |
/// Hold Clang AST information. |
| 73 |
clang::ASTContext &astContext; |
| 74 |
|
| 75 |
const clang::LangOptions &langOpts; |
| 76 |
|
| 77 |
const clang::CodeGenOptions &codeGenOpts; |
| 78 |
|
| 79 |
/// A "module" matches a c/cpp source file: containing a list of functions. |
| 80 |
mlir::ModuleOp theModule; |
| 81 |
|
| 82 |
clang::DiagnosticsEngine &diags; |
| 83 |
|
| 84 |
const clang::TargetInfo ⌖ |
| 85 |
|
| 86 |
std::unique_ptr<CIRGenCXXABI> abi; |
| 87 |
|
| 88 |
CIRGenTypes genTypes; |
| 89 |
|
| 90 |
/// Holds information about C++ vtables. |
| 91 |
CIRGenVTables vtables; |
| 92 |
|
| 93 |
/// Per-function codegen information. Updated everytime emitCIR is called |
| 94 |
/// for FunctionDecls's. |
| 95 |
CIRGenFunction *curCGF = nullptr; |
| 96 |
|
| 97 |
llvm::SmallVector<mlir::Attribute> globalScopeAsm; |
| 98 |
|
| 99 |
public: |
| 100 |
mlir::ModuleOp getModule() const { return theModule; } |
| 101 |
CIRGenBuilderTy &getBuilder() { return builder; } |
| 102 |
clang::ASTContext &getASTContext() const { return astContext; } |
| 103 |
const clang::TargetInfo &getTarget() const { return target; } |
| 104 |
const clang::CodeGenOptions &getCodeGenOpts() const { return codeGenOpts; } |
| 105 |
clang::DiagnosticsEngine &getDiags() const { return diags; } |
| 106 |
CIRGenTypes &getTypes() { return genTypes; } |
| 107 |
const clang::LangOptions &getLangOpts() const { return langOpts; } |
| 108 |
|
| 109 |
CIRGenCXXABI &getCXXABI() const { return *abi; } |
| 110 |
mlir::MLIRContext &getMLIRContext() { return *builder.getContext(); } |
| 111 |
|
| 112 |
const cir::CIRDataLayout getDataLayout() const { |
| 113 |
// FIXME(cir): instead of creating a CIRDataLayout every time, set it as an |
| 114 |
// attribute for the CIRModule class. |
| 115 |
return cir::CIRDataLayout(theModule); |
| 116 |
} |
| 117 |
|
| 118 |
/// ------- |
| 119 |
/// Handling globals |
| 120 |
/// ------- |
| 121 |
|
| 122 |
mlir::Operation *lastGlobalOp = nullptr; |
| 123 |
|
| 124 |
/// Keep a map between lambda fields and names, this needs to be per module |
| 125 |
/// since lambdas might get generated later as part of defered work, and since |
| 126 |
/// the pointers are supposed to be uniqued, should be fine. Revisit this if |
| 127 |
/// it ends up taking too much memory. |
| 128 |
llvm::DenseMap<const clang::FieldDecl *, llvm::StringRef> lambdaFieldToName; |
| 129 |
/// Map BlockAddrInfoAttr (function name, label name) to the corresponding CIR |
| 130 |
/// LabelOp. This provides the main lookup table used to resolve block |
| 131 |
/// addresses into their label operations. |
| 132 |
llvm::DenseMap<cir::BlockAddrInfoAttr, cir::LabelOp> blockAddressInfoToLabel; |
| 133 |
/// Map CIR BlockAddressOps directly to their resolved LabelOps. |
| 134 |
/// Used once a block address has been successfully lowered to a label. |
| 135 |
llvm::MapVector<cir::BlockAddressOp, cir::LabelOp> blockAddressToLabel; |
| 136 |
/// Track CIR BlockAddressOps that cannot be resolved immediately |
| 137 |
/// because their LabelOp has not yet been emitted. These entries |
| 138 |
/// are solved later once the corresponding label is available. |
| 139 |
llvm::DenseSet<cir::BlockAddressOp> unresolvedBlockAddressToLabel; |
| 140 |
cir::LabelOp lookupBlockAddressInfo(cir::BlockAddrInfoAttr blockInfo); |
| 141 |
void mapBlockAddress(cir::BlockAddrInfoAttr blockInfo, cir::LabelOp label); |
| 142 |
void mapUnresolvedBlockAddress(cir::BlockAddressOp op); |
| 143 |
void mapResolvedBlockAddress(cir::BlockAddressOp op, cir::LabelOp); |
| 144 |
void updateResolvedBlockAddress(cir::BlockAddressOp op, |
| 145 |
cir::LabelOp newLabel); |
| 146 |
/// Tell the consumer that this variable has been instantiated. |
| 147 |
void handleCXXStaticMemberVarInstantiation(VarDecl *vd); |
| 148 |
|
| 149 |
llvm::DenseMap<const Decl *, cir::GlobalOp> staticLocalDeclMap; |
| 150 |
|
| 151 |
mlir::Operation *getGlobalValue(llvm::StringRef ref); |
| 152 |
|
| 153 |
cir::GlobalOp getStaticLocalDeclAddress(const VarDecl *d) { |
| 154 |
return staticLocalDeclMap[d]; |
| 155 |
} |
| 156 |
|
| 157 |
void setStaticLocalDeclAddress(const VarDecl *d, cir::GlobalOp c) { |
| 158 |
staticLocalDeclMap[d] = c; |
| 159 |
} |
| 160 |
|
| 161 |
cir::GlobalOp getOrCreateStaticVarDecl(const VarDecl &d, |
| 162 |
cir::GlobalLinkageKind linkage); |
| 163 |
|
| 164 |
/// If the specified mangled name is not in the module, create and return an |
| 165 |
/// mlir::GlobalOp value |
| 166 |
cir::GlobalOp getOrCreateCIRGlobal(llvm::StringRef mangledName, mlir::Type ty, |
| 167 |
LangAS langAS, const VarDecl *d, |
| 168 |
ForDefinition_t isForDefinition); |
| 169 |
|
| 170 |
cir::GlobalOp getOrCreateCIRGlobal(const VarDecl *d, mlir::Type ty, |
| 171 |
ForDefinition_t isForDefinition); |
| 172 |
|
| 173 |
static cir::GlobalOp createGlobalOp(CIRGenModule &cgm, mlir::Location loc, |
| 174 |
llvm::StringRef name, mlir::Type t, |
| 175 |
bool isConstant = false, |
| 176 |
mlir::Operation *insertPoint = nullptr); |
| 177 |
|
| 178 |
/// Add a global constructor or destructor to the module. |
| 179 |
/// The priority is optional, if not specified, the default priority is used. |
| 180 |
void addGlobalCtor(cir::FuncOp ctor, |
| 181 |
std::optional<int> priority = std::nullopt); |
| 182 |
void addGlobalDtor(cir::FuncOp dtor, |
| 183 |
std::optional<int> priority = std::nullopt); |
| 184 |
|
| 185 |
bool shouldZeroInitPadding() const { |
| 186 |
// In C23 (N3096) $6.7.10: |
| 187 |
// """ |
| 188 |
// If any object is initialized with an empty initializer, then it is |
| 189 |
// subject to default initialization: |
| 190 |
// - if it is an aggregate, every member is initialized (recursively) |
| 191 |
// according to these rules, and any padding is initialized to zero bits; |
| 192 |
// - if it is a union, the first named member is initialized (recursively) |
| 193 |
// according to these rules, and any padding is initialized to zero bits. |
| 194 |
// |
| 195 |
// If the aggregate or union contains elements or members that are |
| 196 |
// aggregates or unions, these rules apply recursively to the subaggregates |
| 197 |
// or contained unions. |
| 198 |
// |
| 199 |
// If there are fewer initializers in a brace-enclosed list than there are |
| 200 |
// elements or members of an aggregate, or fewer characters in a string |
| 201 |
// literal used to initialize an array of known size than there are elements |
| 202 |
// in the array, the remainder of the aggregate is subject to default |
| 203 |
// initialization. |
| 204 |
// """ |
| 205 |
// |
| 206 |
// The standard seems ambiguous in the following two areas: |
| 207 |
// 1. For a union type with empty initializer, if the first named member is |
| 208 |
// not the largest member, then the bytes comes after the first named member |
| 209 |
// but before padding are left unspecified. An example is: |
| 210 |
// union U { int a; long long b;}; |
| 211 |
// union U u = {}; // The first 4 bytes are 0, but 4-8 bytes are left |
| 212 |
// unspecified. |
| 213 |
// |
| 214 |
// 2. It only mentions padding for empty initializer, but doesn't mention |
| 215 |
// padding for a non empty initialization list. And if the aggregation or |
| 216 |
// union contains elements or members that are aggregates or unions, and |
| 217 |
// some are non empty initializers, while others are empty initializers, |
| 218 |
// the padding initialization is unclear. An example is: |
| 219 |
// struct S1 { int a; long long b; }; |
| 220 |
// struct S2 { char c; struct S1 s1; }; |
| 221 |
// // The values for paddings between s2.c and s2.s1.a, between s2.s1.a |
| 222 |
// and s2.s1.b are unclear. |
| 223 |
// struct S2 s2 = { 'c' }; |
| 224 |
// |
| 225 |
// Here we choose to zero initiailize left bytes of a union type because |
| 226 |
// projects like the Linux kernel are relying on this behavior. If we don't |
| 227 |
// explicitly zero initialize them, the undef values can be optimized to |
| 228 |
// return garbage data. We also choose to zero initialize paddings for |
| 229 |
// aggregates and unions, no matter they are initialized by empty |
| 230 |
// initializers or non empty initializers. This can provide a consistent |
| 231 |
// behavior. So projects like the Linux kernel can rely on it. |
| 232 |
return !getLangOpts().CPlusPlus; |
| 233 |
} |
| 234 |
|
| 235 |
llvm::StringMap<unsigned> cgGlobalNames; |
| 236 |
std::string getUniqueGlobalName(const std::string &baseName); |
| 237 |
|
| 238 |
/// Return the mlir::Value for the address of the given global variable. |
| 239 |
/// If Ty is non-null and if the global doesn't exist, then it will be created |
| 240 |
/// with the specified type instead of whatever the normal requested type |
| 241 |
/// would be. If IsForDefinition is true, it is guaranteed that an actual |
| 242 |
/// global with type Ty will be returned, not conversion of a variable with |
| 243 |
/// the same mangled name but some other type. |
| 244 |
mlir::Value |
| 245 |
getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty = {}, |
| 246 |
ForDefinition_t isForDefinition = NotForDefinition); |
| 247 |
|
| 248 |
/// Return the mlir::GlobalViewAttr for the address of the given global. |
| 249 |
cir::GlobalViewAttr getAddrOfGlobalVarAttr(const VarDecl *d); |
| 250 |
|
| 251 |
CharUnits computeNonVirtualBaseClassOffset( |
| 252 |
const CXXRecordDecl *derivedClass, |
| 253 |
llvm::iterator_range<CastExpr::path_const_iterator> path); |
| 254 |
|
| 255 |
/// Get the CIR attributes and calling convention to use for a particular |
| 256 |
/// function type. |
| 257 |
/// |
| 258 |
/// \param calleeInfo - The callee information these attributes are being |
| 259 |
/// constructed for. If valid, the attributes applied to this decl may |
| 260 |
/// contribute to the function attributes and calling convention. |
| 261 |
void constructAttributeList(CIRGenCalleeInfo calleeInfo, |
| 262 |
mlir::NamedAttrList &attrs); |
| 263 |
|
| 264 |
/// Will return a global variable of the given type. If a variable with a |
| 265 |
/// different type already exists then a new variable with the right type |
| 266 |
/// will be created and all uses of the old variable will be replaced with a |
| 267 |
/// bitcast to the new variable. |
| 268 |
cir::GlobalOp createOrReplaceCXXRuntimeVariable( |
| 269 |
mlir::Location loc, llvm::StringRef name, mlir::Type ty, |
| 270 |
cir::GlobalLinkageKind linkage, clang::CharUnits alignment); |
| 271 |
|
| 272 |
void emitVTable(const CXXRecordDecl *rd); |
| 273 |
|
| 274 |
/// Return the appropriate linkage for the vtable, VTT, and type information |
| 275 |
/// of the given class. |
| 276 |
cir::GlobalLinkageKind getVTableLinkage(const CXXRecordDecl *rd); |
| 277 |
|
| 278 |
/// Get the address of the RTTI descriptor for the given type. |
| 279 |
mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc, QualType ty, |
| 280 |
bool forEH = false); |
| 281 |
|
| 282 |
static mlir::SymbolTable::Visibility getMLIRVisibility(Visibility v) { |
| 283 |
switch (v) { |
| 284 |
case DefaultVisibility: |
| 285 |
return mlir::SymbolTable::Visibility::Public; |
| 286 |
case HiddenVisibility: |
| 287 |
return mlir::SymbolTable::Visibility::Private; |
| 288 |
case ProtectedVisibility: |
| 289 |
// The distinction between ProtectedVisibility and DefaultVisibility is |
| 290 |
// that symbols with ProtectedVisibility, while visible to the dynamic |
| 291 |
// linker like DefaultVisibility, are guaranteed to always dynamically |
| 292 |
// resolve to a symbol in the current shared object. There is currently no |
| 293 |
// equivalent MLIR visibility, so we fall back on the fact that the symbol |
| 294 |
// is visible. |
| 295 |
return mlir::SymbolTable::Visibility::Public; |
| 296 |
} |
| 297 |
llvm_unreachable("unknown visibility!"); |
| 298 |
} |
| 299 |
|
| 300 |
llvm::DenseMap<mlir::Attribute, cir::GlobalOp> constantStringMap; |
| 301 |
|
| 302 |
/// Return a constant array for the given string. |
| 303 |
mlir::Attribute getConstantArrayFromStringLiteral(const StringLiteral *e); |
| 304 |
|
| 305 |
/// Return a global symbol reference to a constant array for the given string |
| 306 |
/// literal. |
| 307 |
cir::GlobalOp getGlobalForStringLiteral(const StringLiteral *s, |
| 308 |
llvm::StringRef name = ".str"); |
| 309 |
|
| 310 |
/// Return a global symbol reference to a constant array for the given string |
| 311 |
/// literal. |
| 312 |
cir::GlobalViewAttr |
| 313 |
getAddrOfConstantStringFromLiteral(const StringLiteral *s, |
| 314 |
llvm::StringRef name = ".str"); |
| 315 |
|
| 316 |
/// Returns the address space for temporary allocations in the language. This |
| 317 |
/// ensures that the allocated variable's address space matches the |
| 318 |
/// expectations of the AST, rather than using the target's allocation address |
| 319 |
/// space, which may lead to type mismatches in other parts of the IR. |
| 320 |
LangAS getLangTempAllocaAddressSpace() const; |
| 321 |
|
| 322 |
/// Set attributes which are common to any form of a global definition (alias, |
| 323 |
/// Objective-C method, function, global variable). |
| 324 |
/// |
| 325 |
/// NOTE: This should only be called for definitions. |
| 326 |
void setCommonAttributes(GlobalDecl gd, mlir::Operation *op); |
| 327 |
|
| 328 |
const TargetCIRGenInfo &getTargetCIRGenInfo(); |
| 329 |
|
| 330 |
/// Helpers to convert the presumed location of Clang's SourceLocation to an |
| 331 |
/// MLIR Location. |
| 332 |
mlir::Location getLoc(clang::SourceLocation cLoc); |
| 333 |
mlir::Location getLoc(clang::SourceRange cRange); |
| 334 |
|
| 335 |
/// Return the best known alignment for an unknown pointer to a |
| 336 |
/// particular class. |
| 337 |
clang::CharUnits getClassPointerAlignment(const clang::CXXRecordDecl *rd); |
| 338 |
|
| 339 |
/// FIXME: this could likely be a common helper and not necessarily related |
| 340 |
/// with codegen. |
| 341 |
clang::CharUnits getNaturalTypeAlignment(clang::QualType t, |
| 342 |
LValueBaseInfo *baseInfo); |
| 343 |
|
| 344 |
/// TODO: Add TBAAAccessInfo |
| 345 |
CharUnits getDynamicOffsetAlignment(CharUnits actualBaseAlign, |
| 346 |
const CXXRecordDecl *baseDecl, |
| 347 |
CharUnits expectedTargetAlign); |
| 348 |
|
| 349 |
/// Returns the assumed alignment of a virtual base of a class. |
| 350 |
CharUnits getVBaseAlignment(CharUnits derivedAlign, |
| 351 |
const CXXRecordDecl *derived, |
| 352 |
const CXXRecordDecl *vbase); |
| 353 |
|
| 354 |
cir::FuncOp |
| 355 |
getAddrOfCXXStructor(clang::GlobalDecl gd, |
| 356 |
const CIRGenFunctionInfo *fnInfo = nullptr, |
| 357 |
cir::FuncType fnType = nullptr, bool dontDefer = false, |
| 358 |
ForDefinition_t isForDefinition = NotForDefinition) { |
| 359 |
return getAddrAndTypeOfCXXStructor(gd, fnInfo, fnType, dontDefer, |
| 360 |
isForDefinition) |
| 361 |
.second; |
| 362 |
} |
| 363 |
|
| 364 |
std::pair<cir::FuncType, cir::FuncOp> getAddrAndTypeOfCXXStructor( |
| 365 |
clang::GlobalDecl gd, const CIRGenFunctionInfo *fnInfo = nullptr, |
| 366 |
cir::FuncType fnType = nullptr, bool dontDefer = false, |
| 367 |
ForDefinition_t isForDefinition = NotForDefinition); |
| 368 |
|
| 369 |
mlir::Type getVTableComponentType(); |
| 370 |
CIRGenVTables &getVTables() { return vtables; } |
| 371 |
|
| 372 |
ItaniumVTableContext &getItaniumVTableContext() { |
| 373 |
return vtables.getItaniumVTableContext(); |
| 374 |
} |
| 375 |
const ItaniumVTableContext &getItaniumVTableContext() const { |
| 376 |
return vtables.getItaniumVTableContext(); |
| 377 |
} |
| 378 |
|
| 379 |
/// This contains all the decls which have definitions but which are deferred |
| 380 |
/// for emission and therefore should only be output if they are actually |
| 381 |
/// used. If a decl is in this, then it is known to have not been referenced |
| 382 |
/// yet. |
| 383 |
std::map<llvm::StringRef, clang::GlobalDecl> deferredDecls; |
| 384 |
|
| 385 |
// This is a list of deferred decls which we have seen that *are* actually |
| 386 |
// referenced. These get code generated when the module is done. |
| 387 |
std::vector<clang::GlobalDecl> deferredDeclsToEmit; |
| 388 |
void addDeferredDeclToEmit(clang::GlobalDecl GD) { |
| 389 |
deferredDeclsToEmit.emplace_back(GD); |
| 390 |
} |
| 391 |
|
| 392 |
void emitTopLevelDecl(clang::Decl *decl); |
| 393 |
|
| 394 |
/// Determine whether the definition must be emitted; if this returns \c |
| 395 |
/// false, the definition can be emitted lazily if it's used. |
| 396 |
bool mustBeEmitted(const clang::ValueDecl *d); |
| 397 |
|
| 398 |
/// Determine whether the definition can be emitted eagerly, or should be |
| 399 |
/// delayed until the end of the translation unit. This is relevant for |
| 400 |
/// definitions whose linkage can change, e.g. implicit function |
| 401 |
/// instantiations which may later be explicitly instantiated. |
| 402 |
bool mayBeEmittedEagerly(const clang::ValueDecl *d); |
| 403 |
|
| 404 |
bool verifyModule() const; |
| 405 |
|
| 406 |
/// Return the address of the given function. If funcType is non-null, then |
| 407 |
/// this function will use the specified type if it has to create it. |
| 408 |
// TODO: this is a bit weird as `GetAddr` given we give back a FuncOp? |
| 409 |
cir::FuncOp |
| 410 |
getAddrOfFunction(clang::GlobalDecl gd, mlir::Type funcType = nullptr, |
| 411 |
bool forVTable = false, bool dontDefer = false, |
| 412 |
ForDefinition_t isForDefinition = NotForDefinition); |
| 413 |
|
| 414 |
mlir::Operation * |
| 415 |
getAddrOfGlobal(clang::GlobalDecl gd, |
| 416 |
ForDefinition_t isForDefinition = NotForDefinition); |
| 417 |
|
| 418 |
// Return whether RTTI information should be emitted for this target. |
| 419 |
bool shouldEmitRTTI(bool forEH = false) { |
| 420 |
return (forEH || getLangOpts().RTTI) && !getLangOpts().CUDAIsDevice && |
| 421 |
!(getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice && |
| 422 |
getTriple().isNVPTX()); |
| 423 |
} |
| 424 |
|
| 425 |
/// Emit type info if type of an expression is a variably modified |
| 426 |
/// type. Also emit proper debug info for cast types. |
| 427 |
void emitExplicitCastExprType(const ExplicitCastExpr *e, |
| 428 |
CIRGenFunction *cgf = nullptr); |
| 429 |
|
| 430 |
/// Emit code for a single global function or variable declaration. Forward |
| 431 |
/// declarations are emitted lazily. |
| 432 |
void emitGlobal(clang::GlobalDecl gd); |
| 433 |
|
| 434 |
void emitAliasForGlobal(llvm::StringRef mangledName, mlir::Operation *op, |
| 435 |
GlobalDecl aliasGD, cir::FuncOp aliasee, |
| 436 |
cir::GlobalLinkageKind linkage); |
| 437 |
|
| 438 |
mlir::Type convertType(clang::QualType type); |
| 439 |
|
| 440 |
/// Set the visibility for the given global. |
| 441 |
void setGlobalVisibility(mlir::Operation *op, const NamedDecl *d) const; |
| 442 |
void setDSOLocal(mlir::Operation *op) const; |
| 443 |
void setDSOLocal(cir::CIRGlobalValueInterface gv) const; |
| 444 |
|
| 445 |
/// Set visibility, dllimport/dllexport and dso_local. |
| 446 |
/// This must be called after dllimport/dllexport is set. |
| 447 |
void setGVProperties(mlir::Operation *op, const NamedDecl *d) const; |
| 448 |
void setGVPropertiesAux(mlir::Operation *op, const NamedDecl *d) const; |
| 449 |
|
| 450 |
/// Set TLS mode for the given operation based on the given variable |
| 451 |
/// declaration. |
| 452 |
void setTLSMode(mlir::Operation *op, const VarDecl &d); |
| 453 |
|
| 454 |
/// Get TLS mode from CodeGenOptions. |
| 455 |
cir::TLS_Model getDefaultCIRTLSModel() const; |
| 456 |
|
| 457 |
/// Set function attributes for a function declaration. |
| 458 |
void setFunctionAttributes(GlobalDecl gd, cir::FuncOp f, |
| 459 |
bool isIncompleteFunction, bool isThunk); |
| 460 |
|
| 461 |
/// Set extra attributes (inline, etc.) for a function. |
| 462 |
void setCIRFunctionAttributesForDefinition(const clang::FunctionDecl *fd, |
| 463 |
cir::FuncOp f); |
| 464 |
|
| 465 |
void emitGlobalDefinition(clang::GlobalDecl gd, |
| 466 |
mlir::Operation *op = nullptr); |
| 467 |
void emitGlobalFunctionDefinition(clang::GlobalDecl gd, mlir::Operation *op); |
| 468 |
void emitGlobalVarDefinition(const clang::VarDecl *vd, |
| 469 |
bool isTentative = false); |
| 470 |
|
| 471 |
/// Emit the function that initializes the specified global |
| 472 |
void emitCXXGlobalVarDeclInit(const VarDecl *varDecl, cir::GlobalOp addr, |
| 473 |
bool performInit); |
| 474 |
|
| 475 |
void emitCXXGlobalVarDeclInitFunc(const VarDecl *vd, cir::GlobalOp addr, |
| 476 |
bool performInit); |
| 477 |
|
| 478 |
void emitGlobalOpenACCDecl(const clang::OpenACCConstructDecl *cd); |
| 479 |
void emitGlobalOpenACCRoutineDecl(const clang::OpenACCRoutineDecl *cd); |
| 480 |
void emitGlobalOpenACCDeclareDecl(const clang::OpenACCDeclareDecl *cd); |
| 481 |
template <typename BeforeOpTy, typename DataClauseTy> |
| 482 |
void emitGlobalOpenACCDeclareDataOperands(const Expr *varOperand, |
| 483 |
DataClauseTy dataClause, |
| 484 |
OpenACCModifierKind modifiers, |
| 485 |
bool structured, bool implicit, |
| 486 |
bool requiresDtor); |
| 487 |
// Each of the acc.routine operations must have a unique name, so we just use |
| 488 |
// an integer counter. This is how Flang does it, so it seems reasonable. |
| 489 |
unsigned routineCounter = 0; |
| 490 |
void emitOpenACCRoutineDecl(const clang::FunctionDecl *funcDecl, |
| 491 |
cir::FuncOp func, SourceLocation pragmaLoc, |
| 492 |
ArrayRef<const OpenACCClause *> clauses); |
| 493 |
|
| 494 |
void emitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *d); |
| 495 |
void emitOMPGroupPrivateDecl(const OMPGroupPrivateDecl *d); |
| 496 |
void emitOMPCapturedExpr(const OMPCapturedExprDecl *d); |
| 497 |
void emitOMPAllocateDecl(const OMPAllocateDecl *d); |
| 498 |
void emitOMPDeclareReduction(const OMPDeclareReductionDecl *d); |
| 499 |
void emitOMPDeclareMapper(const OMPDeclareMapperDecl *d); |
| 500 |
void emitOMPRequiresDecl(const OMPRequiresDecl *d); |
| 501 |
|
| 502 |
// C++ related functions. |
| 503 |
void emitDeclContext(const DeclContext *dc); |
| 504 |
|
| 505 |
/// Return the result of value-initializing the given type, i.e. a null |
| 506 |
/// expression of the given type. |
| 507 |
mlir::Value emitNullConstant(QualType t, mlir::Location loc); |
| 508 |
|
| 509 |
mlir::TypedAttr emitNullConstantAttr(QualType t); |
| 510 |
|
| 511 |
/// Return a null constant appropriate for zero-initializing a base class with |
| 512 |
/// the given type. This is usually, but not always, an LLVM null constant. |
| 513 |
mlir::TypedAttr emitNullConstantForBase(const CXXRecordDecl *record); |
| 514 |
|
| 515 |
mlir::Value emitMemberPointerConstant(const UnaryOperator *e); |
| 516 |
|
| 517 |
llvm::StringRef getMangledName(clang::GlobalDecl gd); |
| 518 |
// This function is to support the OpenACC 'bind' clause, which names an |
| 519 |
// alternate name for the function to be called by. This function mangles |
| 520 |
// `attachedFunction` as-if its name was actually `bindName` (that is, with |
| 521 |
// the same signature). It has some additional complications, as the 'bind' |
| 522 |
// target is always going to be a global function, so member functions need an |
| 523 |
// explicit instead of implicit 'this' parameter, and thus gets mangled |
| 524 |
// differently. |
| 525 |
std::string getOpenACCBindMangledName(const IdentifierInfo *bindName, |
| 526 |
const FunctionDecl *attachedFunction); |
| 527 |
|
| 528 |
void emitTentativeDefinition(const VarDecl *d); |
| 529 |
|
| 530 |
// Make sure that this type is translated. |
| 531 |
void updateCompletedType(const clang::TagDecl *td); |
| 532 |
|
| 533 |
// Produce code for this constructor/destructor. This method doesn't try to |
| 534 |
// apply any ABI rules about which other constructors/destructors are needed |
| 535 |
// or if they are alias to each other. |
| 536 |
cir::FuncOp codegenCXXStructor(clang::GlobalDecl gd); |
| 537 |
|
| 538 |
bool supportsCOMDAT() const; |
| 539 |
void maybeSetTrivialComdat(const clang::Decl &d, mlir::Operation *op); |
| 540 |
|
| 541 |
static void setInitializer(cir::GlobalOp &op, mlir::Attribute value); |
| 542 |
|
| 543 |
void replaceUsesOfNonProtoTypeWithRealFunction(mlir::Operation *old, |
| 544 |
cir::FuncOp newFn); |
| 545 |
|
| 546 |
cir::FuncOp |
| 547 |
getOrCreateCIRFunction(llvm::StringRef mangledName, mlir::Type funcType, |
| 548 |
clang::GlobalDecl gd, bool forVTable, |
| 549 |
bool dontDefer = false, bool isThunk = false, |
| 550 |
ForDefinition_t isForDefinition = NotForDefinition, |
| 551 |
mlir::ArrayAttr extraAttrs = {}); |
| 552 |
|
| 553 |
cir::FuncOp createCIRFunction(mlir::Location loc, llvm::StringRef name, |
| 554 |
cir::FuncType funcType, |
| 555 |
const clang::FunctionDecl *funcDecl); |
| 556 |
|
| 557 |
/// Create a CIR function with builtin attribute set. |
| 558 |
cir::FuncOp createCIRBuiltinFunction(mlir::Location loc, llvm::StringRef name, |
| 559 |
cir::FuncType ty, |
| 560 |
const clang::FunctionDecl *fd); |
| 561 |
|
| 562 |
/// Mark the function as a special member (e.g. constructor, destructor) |
| 563 |
void setCXXSpecialMemberAttr(cir::FuncOp funcOp, |
| 564 |
const clang::FunctionDecl *funcDecl); |
| 565 |
|
| 566 |
cir::FuncOp createRuntimeFunction(cir::FuncType ty, llvm::StringRef name, |
| 567 |
mlir::ArrayAttr = {}, bool isLocal = false, |
| 568 |
bool assumeConvergent = false); |
| 569 |
|
| 570 |
static constexpr const char *builtinCoroId = "__builtin_coro_id"; |
| 571 |
static constexpr const char *builtinCoroAlloc = "__builtin_coro_alloc"; |
| 572 |
static constexpr const char *builtinCoroBegin = "__builtin_coro_begin"; |
| 573 |
|
| 574 |
/// Given a builtin id for a function like "__builtin_fabsf", return a |
| 575 |
/// Function* for "fabsf". |
| 576 |
cir::FuncOp getBuiltinLibFunction(const FunctionDecl *fd, unsigned builtinID); |
| 577 |
|
| 578 |
mlir::IntegerAttr getSize(CharUnits size) { |
| 579 |
return builder.getSizeFromCharUnits(size); |
| 580 |
} |
| 581 |
|
| 582 |
/// Emit any needed decls for which code generation was deferred. |
| 583 |
void emitDeferred(); |
| 584 |
|
| 585 |
/// Helper for `emitDeferred` to apply actual codegen. |
| 586 |
void emitGlobalDecl(const clang::GlobalDecl &d); |
| 587 |
|
| 588 |
const llvm::Triple &getTriple() const { return target.getTriple(); } |
| 589 |
|
| 590 |
// Finalize CIR code generation. |
| 591 |
void release(); |
| 592 |
|
| 593 |
/// ------- |
| 594 |
/// Visibility and Linkage |
| 595 |
/// ------- |
| 596 |
|
| 597 |
static mlir::SymbolTable::Visibility |
| 598 |
getMLIRVisibilityFromCIRLinkage(cir::GlobalLinkageKind GLK); |
| 599 |
static cir::VisibilityKind getGlobalVisibilityKindFromClangVisibility( |
| 600 |
clang::VisibilityAttr::VisibilityType visibility); |
| 601 |
cir::VisibilityAttr getGlobalVisibilityAttrFromDecl(const Decl *decl); |
| 602 |
cir::GlobalLinkageKind getFunctionLinkage(GlobalDecl gd); |
| 603 |
static mlir::SymbolTable::Visibility getMLIRVisibility(cir::GlobalOp op); |
| 604 |
cir::GlobalLinkageKind getCIRLinkageForDeclarator(const DeclaratorDecl *dd, |
| 605 |
GVALinkage linkage, |
| 606 |
bool isConstantVariable); |
| 607 |
void setFunctionLinkage(GlobalDecl gd, cir::FuncOp f) { |
| 608 |
cir::GlobalLinkageKind l = getFunctionLinkage(gd); |
| 609 |
f.setLinkageAttr(cir::GlobalLinkageKindAttr::get(&getMLIRContext(), l)); |
| 610 |
mlir::SymbolTable::setSymbolVisibility(f, |
| 611 |
getMLIRVisibilityFromCIRLinkage(l)); |
| 612 |
} |
| 613 |
|
| 614 |
cir::GlobalLinkageKind getCIRLinkageVarDefinition(const VarDecl *vd, |
| 615 |
bool isConstant); |
| 616 |
|
| 617 |
void addReplacement(llvm::StringRef name, mlir::Operation *op); |
| 618 |
|
| 619 |
/// Helpers to emit "not yet implemented" error diagnostics |
| 620 |
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef); |
| 621 |
|
| 622 |
template <typename T> |
| 623 |
DiagnosticBuilder errorNYI(SourceLocation loc, llvm::StringRef feature, |
| 624 |
const T &name) { |
| 625 |
unsigned diagID = |
| 626 |
diags.getCustomDiagID(DiagnosticsEngine::Error, |
| 627 |
"ClangIR code gen Not Yet Implemented: %0: %1"); |
| 628 |
return diags.Report(loc, diagID) << feature << name; |
| 629 |
} |
| 630 |
|
| 631 |
DiagnosticBuilder errorNYI(mlir::Location loc, llvm::StringRef feature) { |
| 632 |
// TODO: Convert the location to a SourceLocation |
| 633 |
unsigned diagID = diags.getCustomDiagID( |
| 634 |
DiagnosticsEngine::Error, "ClangIR code gen Not Yet Implemented: %0"); |
| 635 |
return diags.Report(diagID) << feature; |
| 636 |
} |
| 637 |
|
| 638 |
DiagnosticBuilder errorNYI(llvm::StringRef feature) const { |
| 639 |
// TODO: Make a default location? currSrcLoc? |
| 640 |
unsigned diagID = diags.getCustomDiagID( |
| 641 |
DiagnosticsEngine::Error, "ClangIR code gen Not Yet Implemented: %0"); |
| 642 |
return diags.Report(diagID) << feature; |
| 643 |
} |
| 644 |
|
| 645 |
DiagnosticBuilder errorNYI(SourceRange, llvm::StringRef); |
| 646 |
|
| 647 |
template <typename T> |
| 648 |
DiagnosticBuilder errorNYI(SourceRange loc, llvm::StringRef feature, |
| 649 |
const T &name) { |
| 650 |
return errorNYI(loc.getBegin(), feature, name) << loc; |
| 651 |
} |
| 652 |
|
| 653 |
private: |
| 654 |
// An ordered map of canonical GlobalDecls to their mangled names. |
| 655 |
llvm::MapVector<clang::GlobalDecl, llvm::StringRef> mangledDeclNames; |
| 656 |
llvm::StringMap<clang::GlobalDecl, llvm::BumpPtrAllocator> manglings; |
| 657 |
|
| 658 |
// FIXME: should we use llvm::TrackingVH<mlir::Operation> here? |
| 659 |
typedef llvm::StringMap<mlir::Operation *> ReplacementsTy; |
| 660 |
ReplacementsTy replacements; |
| 661 |
/// Call replaceAllUsesWith on all pairs in replacements. |
| 662 |
void applyReplacements(); |
| 663 |
|
| 664 |
/// A helper function to replace all uses of OldF to NewF that replace |
| 665 |
/// the type of pointer arguments. This is not needed to tradtional |
| 666 |
/// pipeline since LLVM has opaque pointers but CIR not. |
| 667 |
void replacePointerTypeArgs(cir::FuncOp oldF, cir::FuncOp newF); |
| 668 |
|
| 669 |
void setNonAliasAttributes(GlobalDecl gd, mlir::Operation *op); |
| 670 |
|
| 671 |
/// Map source language used to a CIR attribute. |
| 672 |
std::optional<cir::SourceLanguage> getCIRSourceLanguage() const; |
| 673 |
}; |
| 674 |
} // namespace CIRGen |
| 675 |
|
| 676 |
} // namespace clang |
| 677 |
|
| 678 |
#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H |