clang 23.0.0git
CIRGenModule.cpp
Go to the documentation of this file.
1//===- CIRGenModule.cpp - Per-Module state for CIR generation -------------===//
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#include "CIRGenModule.h"
14#include "CIRGenCUDARuntime.h"
15#include "CIRGenCXXABI.h"
17#include "CIRGenFunction.h"
18
19#include "mlir/Dialect/OpenMP/OpenMPOffloadUtils.h"
21#include "clang/AST/ASTLambda.h"
22#include "clang/AST/Attrs.inc"
23#include "clang/AST/DeclBase.h"
35#include "llvm/ADT/StringRef.h"
36
37#include "CIRGenFunctionInfo.h"
38#include "TargetInfo.h"
39#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
40#include "mlir/IR/Attributes.h"
41#include "mlir/IR/BuiltinOps.h"
42#include "mlir/IR/Location.h"
43#include "mlir/IR/MLIRContext.h"
44#include "mlir/IR/Operation.h"
45#include "mlir/IR/Verifier.h"
46
47#include <algorithm>
48
49using namespace clang;
50using namespace clang::CIRGen;
51
53 switch (cgm.getASTContext().getCXXABIKind()) {
54 case TargetCXXABI::GenericItanium:
55 case TargetCXXABI::GenericAArch64:
56 case TargetCXXABI::AppleARM64:
57 return CreateCIRGenItaniumCXXABI(cgm);
58
59 case TargetCXXABI::Fuchsia:
60 case TargetCXXABI::GenericARM:
61 case TargetCXXABI::iOS:
62 case TargetCXXABI::WatchOS:
63 case TargetCXXABI::GenericMIPS:
64 case TargetCXXABI::WebAssembly:
65 case TargetCXXABI::XL:
66 case TargetCXXABI::Microsoft:
67 cgm.errorNYI("createCXXABI: C++ ABI kind");
68 return nullptr;
69 }
70
71 llvm_unreachable("invalid C++ ABI kind");
72}
73
74CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
75 clang::ASTContext &astContext,
76 const clang::CodeGenOptions &cgo,
77 DiagnosticsEngine &diags)
78 : builder(mlirContext, *this), astContext(astContext),
79 langOpts(astContext.getLangOpts()), codeGenOpts(cgo),
80 theModule{mlir::ModuleOp::create(mlir::UnknownLoc::get(&mlirContext))},
81 diags(diags), target(astContext.getTargetInfo()),
82 abi(createCXXABI(*this)), genTypes(*this), vtables(*this) {
83
84 // Initialize cached types
85 voidTy = cir::VoidType::get(&getMLIRContext());
86 voidPtrTy = cir::PointerType::get(voidTy);
87 sInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/true);
88 sInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/true);
89 sInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/true);
90 sInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/true);
91 sInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/true);
92 uInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
93 uInt8PtrTy = cir::PointerType::get(uInt8Ty);
95 uInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/false);
96 uInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/false);
97 uInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/false);
98 uInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/false);
99 fP16Ty = cir::FP16Type::get(&getMLIRContext());
100 bFloat16Ty = cir::BF16Type::get(&getMLIRContext());
101 floatTy = cir::SingleType::get(&getMLIRContext());
102 doubleTy = cir::DoubleType::get(&getMLIRContext());
103 fP80Ty = cir::FP80Type::get(&getMLIRContext());
104 fP128Ty = cir::FP128Type::get(&getMLIRContext());
105
106 allocaInt8PtrTy = cir::PointerType::get(uInt8Ty, cirAllocaAddressSpace);
107
109 astContext
110 .toCharUnitsFromBits(
111 astContext.getTargetInfo().getPointerAlign(LangAS::Default))
112 .getQuantity();
113
114 const unsigned charSize = astContext.getTargetInfo().getCharWidth();
115 uCharTy = cir::IntType::get(&getMLIRContext(), charSize, /*isSigned=*/false);
116
117 // TODO(CIR): Should be updated once TypeSizeInfoAttr is upstreamed
118 const unsigned sizeTypeSize =
119 astContext.getTypeSize(astContext.getSignedSizeType());
120 SizeSizeInBytes = astContext.toCharUnitsFromBits(sizeTypeSize).getQuantity();
121 // In CIRGenTypeCache, UIntPtrTy and SizeType are fields of the same union
122 uIntPtrTy =
123 cir::IntType::get(&getMLIRContext(), sizeTypeSize, /*isSigned=*/false);
124 ptrDiffTy =
125 cir::IntType::get(&getMLIRContext(), sizeTypeSize, /*isSigned=*/true);
126
127 std::optional<cir::SourceLanguage> sourceLanguage = getCIRSourceLanguage();
128 if (sourceLanguage)
129 theModule->setAttr(
130 cir::CIRDialect::getSourceLanguageAttrName(),
131 cir::SourceLanguageAttr::get(&mlirContext, *sourceLanguage));
132 theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
133 builder.getStringAttr(getTriple().str()));
134
135 if (cgo.OptimizationLevel > 0 || cgo.OptimizeSize > 0)
136 theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
137 cir::OptInfoAttr::get(&mlirContext,
138 cgo.OptimizationLevel,
139 cgo.OptimizeSize));
140
141 if (langOpts.OpenMP) {
142 mlir::omp::OffloadModuleOpts ompOpts(
143 langOpts.OpenMPTargetDebug, langOpts.OpenMPTeamSubscription,
144 langOpts.OpenMPThreadSubscription, langOpts.OpenMPNoThreadState,
145 langOpts.OpenMPNoNestedParallelism, langOpts.OpenMPIsTargetDevice,
146 getTriple().isGPU(), langOpts.OpenMPForceUSM, langOpts.OpenMP,
147 langOpts.OMPHostIRFile, langOpts.OMPTargetTriples, langOpts.NoGPULib);
148 mlir::omp::setOffloadModuleInterfaceAttributes(theModule, ompOpts);
149 }
150
151 if (langOpts.CUDA)
152 createCUDARuntime();
153
154 // Set the module name to be the name of the main file. TranslationUnitDecl
155 // often contains invalid source locations and isn't a reliable source for the
156 // module location.
157 FileID mainFileId = astContext.getSourceManager().getMainFileID();
158 const FileEntry &mainFile =
159 *astContext.getSourceManager().getFileEntryForID(mainFileId);
160 StringRef path = mainFile.tryGetRealPathName();
161 if (!path.empty()) {
162 theModule.setSymName(path);
163 theModule->setLoc(mlir::FileLineColLoc::get(&mlirContext, path,
164 /*line=*/0,
165 /*column=*/0));
166 }
167
168 // Set CUDA GPU binary handle.
169 if (langOpts.CUDA) {
170 llvm::StringRef cudaBinaryName = codeGenOpts.CudaGpuBinaryFileName;
171 if (!cudaBinaryName.empty()) {
172 theModule->setAttr(cir::CIRDialect::getCUDABinaryHandleAttrName(),
173 cir::CUDABinaryHandleAttr::get(
174 &mlirContext, mlir::StringAttr::get(
175 &mlirContext, cudaBinaryName)));
176 }
177 }
178}
179
181
182void CIRGenModule::createCUDARuntime() {
183 cudaRuntime.reset(createNVCUDARuntime(*this));
184}
185
186/// FIXME: this could likely be a common helper and not necessarily related
187/// with codegen.
188/// Return the best known alignment for an unknown pointer to a
189/// particular class.
191 if (!rd->hasDefinition())
192 return CharUnits::One(); // Hopefully won't be used anywhere.
193
194 auto &layout = astContext.getASTRecordLayout(rd);
195
196 // If the class is final, then we know that the pointer points to an
197 // object of that type and can use the full alignment.
198 if (rd->isEffectivelyFinal())
199 return layout.getAlignment();
200
201 // Otherwise, we have to assume it could be a subclass.
202 return layout.getNonVirtualAlignment();
203}
204
206 LValueBaseInfo *baseInfo,
207 bool forPointeeType) {
209
210 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown, but
211 // that doesn't return the information we need to compute baseInfo.
212
213 // Honor alignment typedef attributes even on incomplete types.
214 // We also honor them straight for C++ class types, even as pointees;
215 // there's an expressivity gap here.
216 if (const auto *tt = t->getAs<TypedefType>()) {
217 if (unsigned align = tt->getDecl()->getMaxAlignment()) {
218 if (baseInfo)
220 return astContext.toCharUnitsFromBits(align);
221 }
222 }
223
224 bool alignForArray = t->isArrayType();
225
226 // Analyze the base element type, so we don't get confused by incomplete
227 // array types.
228 t = astContext.getBaseElementType(t);
229
230 if (t->isIncompleteType()) {
231 // We could try to replicate the logic from
232 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
233 // type is incomplete, so it's impossible to test. We could try to reuse
234 // getTypeAlignIfKnown, but that doesn't return the information we need
235 // to set baseInfo. So just ignore the possibility that the alignment is
236 // greater than one.
237 if (baseInfo)
239 return CharUnits::One();
240 }
241
242 if (baseInfo)
244
245 CharUnits alignment;
246 const CXXRecordDecl *rd = nullptr;
247 if (t.getQualifiers().hasUnaligned()) {
248 alignment = CharUnits::One();
249 } else if (forPointeeType && !alignForArray &&
250 (rd = t->getAsCXXRecordDecl())) {
251 alignment = getClassPointerAlignment(rd);
252 } else {
253 alignment = astContext.getTypeAlignInChars(t);
254 }
255
256 // Cap to the global maximum type alignment unless the alignment
257 // was somehow explicit on the type.
258 if (unsigned maxAlign = astContext.getLangOpts().MaxTypeAlign) {
259 if (alignment.getQuantity() > maxAlign &&
260 !astContext.isAlignmentRequired(t))
261 alignment = CharUnits::fromQuantity(maxAlign);
262 }
263 return alignment;
264}
265
268 LValueBaseInfo *baseInfo) {
269 return getNaturalTypeAlignment(t->getPointeeType(), baseInfo,
270 /*forPointeeType=*/true);
271}
272
274 if (theTargetCIRGenInfo)
275 return *theTargetCIRGenInfo;
276
277 const llvm::Triple &triple = getTarget().getTriple();
278 switch (triple.getArch()) {
279 default:
281
282 // Currently we just fall through to x86_64.
283 [[fallthrough]];
284
285 case llvm::Triple::x86_64: {
286 switch (triple.getOS()) {
287 default:
289
290 // Currently we just fall through to x86_64.
291 [[fallthrough]];
292
293 case llvm::Triple::Linux:
294 theTargetCIRGenInfo = createX8664TargetCIRGenInfo(genTypes);
295 return *theTargetCIRGenInfo;
296 }
297 }
298 case llvm::Triple::nvptx:
299 case llvm::Triple::nvptx64:
300 theTargetCIRGenInfo = createNVPTXTargetCIRGenInfo(genTypes);
301 return *theTargetCIRGenInfo;
302 case llvm::Triple::amdgcn: {
303 theTargetCIRGenInfo = createAMDGPUTargetCIRGenInfo(genTypes);
304 return *theTargetCIRGenInfo;
305 }
306 }
307}
308
310 assert(cLoc.isValid() && "expected valid source location");
311 const SourceManager &sm = astContext.getSourceManager();
312 PresumedLoc pLoc = sm.getPresumedLoc(cLoc);
313 StringRef filename = pLoc.getFilename();
314 return mlir::FileLineColLoc::get(builder.getStringAttr(filename),
315 pLoc.getLine(), pLoc.getColumn());
316}
317
318mlir::Location CIRGenModule::getLoc(SourceRange cRange) {
319 assert(cRange.isValid() && "expected a valid source range");
320 mlir::Location begin = getLoc(cRange.getBegin());
321 mlir::Location end = getLoc(cRange.getEnd());
322 mlir::Attribute metadata;
323 return mlir::FusedLoc::get({begin, end}, metadata, builder.getContext());
324}
325
326mlir::Operation *
328 const Decl *d = gd.getDecl();
329
331 return getAddrOfCXXStructor(gd, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
332 /*DontDefer=*/false, isForDefinition);
333
334 if (isa<CXXMethodDecl>(d)) {
335 const CIRGenFunctionInfo &fi =
337 cir::FuncType ty = getTypes().getFunctionType(fi);
338 return getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false,
339 isForDefinition);
340 }
341
342 if (isa<FunctionDecl>(d)) {
344 cir::FuncType ty = getTypes().getFunctionType(fi);
345 return getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false,
346 isForDefinition);
347 }
348
349 return getAddrOfGlobalVar(cast<VarDecl>(d), /*ty=*/nullptr, isForDefinition)
350 .getDefiningOp();
351}
352
354 // We call getAddrOfGlobal with isForDefinition set to ForDefinition in
355 // order to get a Value with exactly the type we need, not something that
356 // might have been created for another decl with the same mangled name but
357 // different type.
358 mlir::Operation *op = getAddrOfGlobal(d, ForDefinition);
359
360 // In case of different address spaces, we may still get a cast, even with
361 // IsForDefinition equal to ForDefinition. Query mangled names table to get
362 // GlobalValue.
363 if (!op)
365
366 assert(op && "expected a valid global op");
367
368 // Check to see if we've already emitted this. This is necessary for a
369 // couple of reasons: first, decls can end up in deferred-decls queue
370 // multiple times, and second, decls can end up with definitions in unusual
371 // ways (e.g. by an extern inline function acquiring a strong function
372 // redefinition). Just ignore those cases.
373 // TODO: Not sure what to map this to for MLIR
374 mlir::Operation *globalValueOp = op;
375 if (auto gv = dyn_cast<cir::GetGlobalOp>(op))
376 globalValueOp =
377 mlir::SymbolTable::lookupSymbolIn(getModule(), gv.getNameAttr());
378
379 if (auto cirGlobalValue =
380 dyn_cast<cir::CIRGlobalValueInterface>(globalValueOp))
381 if (!cirGlobalValue.isDeclaration())
382 return;
383
384 // If this is OpenMP, check if it is legal to emit this global normally.
386
387 // Otherwise, emit the definition and move on to the next one.
389}
390
392 // Emit code for any potentially referenced deferred decls. Since a previously
393 // unused static decl may become used during the generation of code for a
394 // static function, iterate until no changes are made.
395
397
399 // Emitting a vtable doesn't directly cause more vtables to
400 // become deferred, although it can cause functions to be
401 // emitted that then need those vtables.
402 assert(deferredVTables.empty());
403
405
406 // Stop if we're out of both deferred vtables and deferred declarations.
407 if (deferredDeclsToEmit.empty())
408 return;
409
410 // Grab the list of decls to emit. If emitGlobalDefinition schedules more
411 // work, it will not interfere with this.
412 std::vector<GlobalDecl> curDeclsToEmit;
413 curDeclsToEmit.swap(deferredDeclsToEmit);
414
415 for (const GlobalDecl &d : curDeclsToEmit) {
417
418 // If we found out that we need to emit more decls, do that recursively.
419 // This has the advantage that the decls are emitted in a DFS and related
420 // ones are close together, which is convenient for testing.
421 if (!deferredVTables.empty() || !deferredDeclsToEmit.empty()) {
422 emitDeferred();
423 assert(deferredVTables.empty() && deferredDeclsToEmit.empty());
424 }
425 }
426}
427
428template <typename AttrT> static bool hasImplicitAttr(const ValueDecl *decl) {
429 if (!decl)
430 return false;
431 if (auto *attr = decl->getAttr<AttrT>())
432 return attr->isImplicit();
433 return decl->isImplicit();
434}
435
436// TODO(cir): This should be shared with OG Codegen.
438 assert(langOpts.CUDA && "Should not be called by non-CUDA languages");
439 // We need to emit host-side 'shadows' for all global
440 // device-side variables because the CUDA runtime needs their
441 // size and host-side address in order to provide access to
442 // their device-side incarnations.
443 return !langOpts.CUDAIsDevice || global->hasAttr<CUDADeviceAttr>() ||
444 global->hasAttr<CUDAConstantAttr>() ||
445 global->hasAttr<CUDASharedAttr>() ||
448}
449
451 if (const auto *cd = dyn_cast<clang::OpenACCConstructDecl>(gd.getDecl())) {
453 return;
454 }
455
456 // TODO(OMP): The logic in this function for the 'rest' of the OpenMP
457 // declarative declarations is complicated and needs to be done on a per-kind
458 // basis, so all of that needs to be added when we implement the individual
459 // global-allowed declarations. See uses of `cir::MissingFeatures::openMP
460 // throughout this function.
461
462 const auto *global = cast<ValueDecl>(gd.getDecl());
463
464 // If this is CUDA, be selective about which declarations we emit.
465 // Non-constexpr non-lambda implicit host device functions are not emitted
466 // unless they are used on device side.
467 if (langOpts.CUDA) {
468 assert((isa<FunctionDecl>(global) || isa<VarDecl>(global)) &&
469 "Expected Variable or Function");
470 if (const auto *varDecl = dyn_cast<VarDecl>(global)) {
472 return;
473 // TODO(cir): This should be shared with OG Codegen.
474 } else if (langOpts.CUDAIsDevice) {
475 const auto *functionDecl = dyn_cast<FunctionDecl>(global);
476 if ((!global->hasAttr<CUDADeviceAttr>() ||
477 (langOpts.OffloadImplicitHostDeviceTemplates &&
480 !functionDecl->isConstexpr() &&
482 !getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.count(
483 functionDecl))) &&
484 !global->hasAttr<CUDAGlobalAttr>() &&
485 !(langOpts.HIPStdPar && isa<FunctionDecl>(global) &&
486 !global->hasAttr<CUDAHostAttr>()))
487 return;
488 // Device-only functions are the only things we skip.
489 } else if (!global->hasAttr<CUDAHostAttr>() &&
490 global->hasAttr<CUDADeviceAttr>())
491 return;
492 }
493
494 if (const auto *fd = dyn_cast<FunctionDecl>(global)) {
495 // Update deferred annotations with the latest declaration if the function
496 // was already used or defined.
497 if (fd->hasAttr<AnnotateAttr>())
498 errorNYI(fd->getSourceRange(), "deferredAnnotations");
499 if (!fd->doesThisDeclarationHaveABody()) {
500 if (!fd->doesDeclarationForceExternallyVisibleDefinition())
501 return;
502
503 errorNYI(fd->getSourceRange(),
504 "function declaration that forces code gen");
505 return;
506 }
507 } else {
508 const auto *vd = cast<VarDecl>(global);
509 assert(vd->isFileVarDecl() && "Cannot emit local var decl as global.");
510 if (vd->isThisDeclarationADefinition() != VarDecl::Definition &&
511 !astContext.isMSStaticDataMemberInlineDefinition(vd)) {
513 // If this declaration may have caused an inline variable definition to
514 // change linkage, make sure that it's emitted.
515 if (astContext.getInlineVariableDefinitionKind(vd) ==
518 // Otherwise, we can ignore this declaration. The variable will be emitted
519 // on its first use.
520 return;
521 }
522 }
523
524 // Defer code generation to first use when possible, e.g. if this is an inline
525 // function. If the global must always be emitted, do it eagerly if possible
526 // to benefit from cache locality. Deferring code generation is necessary to
527 // avoid adding initializers to external declarations.
528 if (mustBeEmitted(global) && mayBeEmittedEagerly(global)) {
529 // Emit the definition if it can't be deferred.
531 return;
532 }
533
534 // If we're deferring emission of a C++ variable with an initializer, remember
535 // the order in which it appeared on the file.
537
538 llvm::StringRef mangledName = getMangledName(gd);
539 if (getGlobalValue(mangledName) != nullptr) {
540 // The value has already been used and should therefore be emitted.
542 } else if (mustBeEmitted(global)) {
543 // The value must be emitted, but cannot be emitted eagerly.
544 assert(!mayBeEmittedEagerly(global));
546 } else {
547 // Otherwise, remember that we saw a deferred decl with this name. The first
548 // use of the mangled name will cause it to move into deferredDeclsToEmit.
549 deferredDecls[mangledName] = gd;
550 }
551}
552
554 mlir::Operation *op) {
555 auto const *funcDecl = cast<FunctionDecl>(gd.getDecl());
557 cir::FuncType funcType = getTypes().getFunctionType(fi);
558 cir::FuncOp funcOp = dyn_cast_if_present<cir::FuncOp>(op);
559 if (!funcOp || funcOp.getFunctionType() != funcType) {
560 funcOp = getAddrOfFunction(gd, funcType, /*ForVTable=*/false,
561 /*DontDefer=*/true, ForDefinition);
562 }
563
564 // Already emitted.
565 if (!funcOp.isDeclaration())
566 return;
567
568 setFunctionLinkage(gd, funcOp);
569 setGVProperties(funcOp, funcDecl);
571 maybeSetTrivialComdat(*funcDecl, funcOp);
573
574 CIRGenFunction cgf(*this, builder);
575 curCGF = &cgf;
576 {
577 mlir::OpBuilder::InsertionGuard guard(builder);
578 cgf.generateCode(gd, funcOp, funcType);
579 }
580 curCGF = nullptr;
581
582 setNonAliasAttributes(gd, funcOp);
584
585 auto getPriority = [this](const auto *attr) -> int {
586 Expr *e = attr->getPriority();
587 if (e)
588 return e->EvaluateKnownConstInt(this->getASTContext()).getExtValue();
589 return attr->DefaultPriority;
590 };
591
592 if (const ConstructorAttr *ca = funcDecl->getAttr<ConstructorAttr>())
593 addGlobalCtor(funcOp, getPriority(ca));
594 if (const DestructorAttr *da = funcDecl->getAttr<DestructorAttr>())
595 addGlobalDtor(funcOp, getPriority(da));
596
597 if (funcDecl->getAttr<AnnotateAttr>())
598 errorNYI(funcDecl->getSourceRange(), "deferredAnnotations");
599}
600
601/// Track functions to be called before main() runs.
602void CIRGenModule::addGlobalCtor(cir::FuncOp ctor,
603 std::optional<int> priority) {
606
607 // Traditional LLVM codegen directly adds the function to the list of global
608 // ctors. In CIR we just add a global_ctor attribute to the function. The
609 // global list is created in LoweringPrepare.
610 //
611 // FIXME(from traditional LLVM): Type coercion of void()* types.
612 ctor.setGlobalCtorPriority(priority);
613}
614
615/// Add a function to the list that will be called when the module is unloaded.
616void CIRGenModule::addGlobalDtor(cir::FuncOp dtor,
617 std::optional<int> priority) {
618 if (codeGenOpts.RegisterGlobalDtorsWithAtExit &&
619 (!getASTContext().getTargetInfo().getTriple().isOSAIX()))
620 errorNYI(dtor.getLoc(), "registerGlobalDtorsWithAtExit");
621
622 // FIXME(from traditional LLVM): Type coercion of void()* types.
623 dtor.setGlobalDtorPriority(priority);
624}
625
628 if (dk == VarDecl::Definition && vd->hasAttr<DLLImportAttr>())
629 return;
630
632 // If we have a definition, this might be a deferred decl. If the
633 // instantiation is explicit, make sure we emit it at the end.
636
638}
639
640mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) {
641 return mlir::SymbolTable::lookupSymbolIn(theModule, name);
642}
643
644cir::GlobalOp
645CIRGenModule::createGlobalOp(CIRGenModule &cgm, mlir::Location loc,
646 StringRef name, mlir::Type t, bool isConstant,
647 mlir::ptr::MemorySpaceAttrInterface addrSpace,
648 mlir::Operation *insertPoint) {
649 cir::GlobalOp g;
650 CIRGenBuilderTy &builder = cgm.getBuilder();
651
652 {
653 mlir::OpBuilder::InsertionGuard guard(builder);
654
655 // If an insertion point is provided, we're replacing an existing global,
656 // otherwise, create the new global immediately after the last gloabl we
657 // emitted.
658 if (insertPoint) {
659 builder.setInsertionPoint(insertPoint);
660 } else {
661 // Group global operations together at the top of the module.
662 if (cgm.lastGlobalOp)
663 builder.setInsertionPointAfter(cgm.lastGlobalOp);
664 else
665 builder.setInsertionPointToStart(cgm.getModule().getBody());
666 }
667
668 g = cir::GlobalOp::create(builder, loc, name, t, isConstant, addrSpace);
669 if (!insertPoint)
670 cgm.lastGlobalOp = g;
671
672 // Default to private until we can judge based on the initializer,
673 // since MLIR doesn't allow public declarations.
674 mlir::SymbolTable::setSymbolVisibility(
675 g, mlir::SymbolTable::Visibility::Private);
676 }
677 return g;
678}
679
680void CIRGenModule::setCommonAttributes(GlobalDecl gd, mlir::Operation *gv) {
681 const Decl *d = gd.getDecl();
682 if (isa_and_nonnull<NamedDecl>(d))
683 setGVProperties(gv, dyn_cast<NamedDecl>(d));
685
686 if (auto gvi = mlir::dyn_cast<cir::CIRGlobalValueInterface>(gv)) {
687 if (d && d->hasAttr<UsedAttr>())
689
690 if (const auto *vd = dyn_cast_if_present<VarDecl>(d);
691 vd && ((codeGenOpts.KeepPersistentStorageVariables &&
692 (vd->getStorageDuration() == SD_Static ||
693 vd->getStorageDuration() == SD_Thread)) ||
694 (codeGenOpts.KeepStaticConsts &&
695 vd->getStorageDuration() == SD_Static &&
696 vd->getType().isConstQualified())))
698 }
699}
700
701void CIRGenModule::setNonAliasAttributes(GlobalDecl gd, mlir::Operation *op) {
702 setCommonAttributes(gd, op);
703
704 const Decl *d = gd.getDecl();
705 if (d) {
706 if (auto gvi = mlir::dyn_cast<cir::CIRGlobalValueInterface>(op)) {
707 if (const auto *sa = d->getAttr<SectionAttr>())
708 gvi.setSection(builder.getStringAttr(sa->getName()));
709 if (d->hasAttr<RetainAttr>())
710 addUsedGlobal(gvi);
711 }
712 }
713
716
718}
719
720std::optional<cir::SourceLanguage> CIRGenModule::getCIRSourceLanguage() const {
721 using ClangStd = clang::LangStandard;
722 using CIRLang = cir::SourceLanguage;
723 auto opts = getLangOpts();
724
725 if (opts.CPlusPlus)
726 return CIRLang::CXX;
727 if (opts.C99 || opts.C11 || opts.C17 || opts.C23 || opts.C2y ||
728 opts.LangStd == ClangStd::lang_c89 ||
729 opts.LangStd == ClangStd::lang_gnu89)
730 return CIRLang::C;
731
732 // TODO(cir): support remaining source languages.
734 errorNYI("CIR does not yet support the given source language");
735 return std::nullopt;
736}
737
738LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *d) {
739 if (langOpts.OpenCL) {
744 return as;
745 }
746
747 if (langOpts.SYCLIsDevice &&
748 (!d || d->getType().getAddressSpace() == LangAS::Default))
749 errorNYI("SYCL global address space");
750
751 if (langOpts.CUDA && langOpts.CUDAIsDevice) {
752 if (d) {
753 if (d->hasAttr<CUDAConstantAttr>())
755 if (d->hasAttr<CUDASharedAttr>())
756 return LangAS::cuda_shared;
757 if (d->hasAttr<CUDADeviceAttr>())
758 return LangAS::cuda_device;
759 if (d->getType().isConstQualified())
761 }
762 return LangAS::cuda_device;
763 }
764
765 if (langOpts.OpenMP)
766 errorNYI("OpenMP global address space");
767
769}
770
771static void setLinkageForGV(cir::GlobalOp &gv, const NamedDecl *nd) {
772 // Set linkage and visibility in case we never see a definition.
774 // Don't set internal linkage on declarations.
775 // "extern_weak" is overloaded in LLVM; we probably should have
776 // separate linkage types for this.
778 (nd->hasAttr<WeakAttr>() || nd->isWeakImported()))
779 gv.setLinkage(cir::GlobalLinkageKind::ExternalWeakLinkage);
780}
781
782static llvm::SmallVector<int64_t> indexesOfArrayAttr(mlir::ArrayAttr indexes) {
784 for (mlir::Attribute i : indexes) {
785 auto ind = mlir::cast<mlir::IntegerAttr>(i);
786 inds.push_back(ind.getValue().getSExtValue());
787 }
788 return inds;
789}
790
791static bool isViewOnGlobal(cir::GlobalOp glob, cir::GlobalViewAttr view) {
792 return view.getSymbol().getValue() == glob.getSymName();
793}
794
795static cir::GlobalViewAttr createNewGlobalView(CIRGenModule &cgm,
796 cir::GlobalOp newGlob,
797 cir::GlobalViewAttr attr,
798 mlir::Type oldTy) {
799 // If the attribute does not require indexes or it is not a global view on
800 // the global we're replacing, keep the original attribute.
801 if (!attr.getIndices() || !isViewOnGlobal(newGlob, attr))
802 return attr;
803
804 llvm::SmallVector<int64_t> oldInds = indexesOfArrayAttr(attr.getIndices());
806 CIRGenBuilderTy &bld = cgm.getBuilder();
807 const cir::CIRDataLayout &layout = cgm.getDataLayout();
808 mlir::Type newTy = newGlob.getSymType();
809
810 uint64_t offset =
811 bld.computeOffsetFromGlobalViewIndices(layout, oldTy, oldInds);
812 bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds);
813 cir::PointerType newPtrTy;
814
815 if (isa<cir::RecordType>(oldTy))
816 newPtrTy = cir::PointerType::get(newTy);
817 else if (isa<cir::ArrayType>(oldTy))
818 newPtrTy = cast<cir::PointerType>(attr.getType());
819
820 if (newPtrTy)
821 return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds);
822
823 // This may be unreachable in practice, but keep it as errorNYI while CIR
824 // is still under development.
825 cgm.errorNYI("Unhandled type in createNewGlobalView");
826 return {};
827}
828
829static mlir::Attribute getNewInitValue(CIRGenModule &cgm, cir::GlobalOp newGlob,
830 mlir::Type oldTy,
831 mlir::Attribute oldInit) {
832 if (auto oldView = mlir::dyn_cast<cir::GlobalViewAttr>(oldInit))
833 return createNewGlobalView(cgm, newGlob, oldView, oldTy);
834
835 auto getNewInitElements =
836 [&](mlir::ArrayAttr oldElements) -> mlir::ArrayAttr {
838 for (mlir::Attribute elt : oldElements) {
839 if (auto view = mlir::dyn_cast<cir::GlobalViewAttr>(elt))
840 newElements.push_back(createNewGlobalView(cgm, newGlob, view, oldTy));
841 else if (mlir::isa<cir::ConstArrayAttr, cir::ConstRecordAttr>(elt))
842 newElements.push_back(getNewInitValue(cgm, newGlob, oldTy, elt));
843 else
844 newElements.push_back(elt);
845 }
846 return mlir::ArrayAttr::get(cgm.getBuilder().getContext(), newElements);
847 };
848
849 if (auto oldArray = mlir::dyn_cast<cir::ConstArrayAttr>(oldInit)) {
850 mlir::Attribute newElements =
851 getNewInitElements(mlir::cast<mlir::ArrayAttr>(oldArray.getElts()));
852 return cgm.getBuilder().getConstArray(
853 newElements, mlir::cast<cir::ArrayType>(oldArray.getType()));
854 }
855 if (auto oldRecord = mlir::dyn_cast<cir::ConstRecordAttr>(oldInit)) {
856 mlir::ArrayAttr newMembers = getNewInitElements(oldRecord.getMembers());
857 auto recordTy = mlir::cast<cir::RecordType>(oldRecord.getType());
859 newMembers, recordTy.getPacked(), recordTy.getPadded(), recordTy);
860 }
861
862 // This may be unreachable in practice, but keep it as errorNYI while CIR
863 // is still under development.
864 cgm.errorNYI("Unhandled type in getNewInitValue");
865 return {};
866}
867
868// We want to replace a global value, but because of CIR's typed pointers,
869// we need to update the existing uses to reflect the new type, not just replace
870// them directly.
871void CIRGenModule::replaceGlobal(cir::GlobalOp oldGV, cir::GlobalOp newGV) {
872 assert(oldGV.getSymName() == newGV.getSymName() && "symbol names must match");
873
874 mlir::Type oldTy = oldGV.getSymType();
875 mlir::Type newTy = newGV.getSymType();
876
878
879 // If the type didn't change, why are we here?
880 assert(oldTy != newTy && "expected type change in replaceGlobal");
881
882 // Visit all uses and add handling to fix up the types.
883 std::optional<mlir::SymbolTable::UseRange> oldSymUses =
884 oldGV.getSymbolUses(theModule);
885 for (mlir::SymbolTable::SymbolUse use : *oldSymUses) {
886 mlir::Operation *userOp = use.getUser();
887 assert(
888 (mlir::isa<cir::GetGlobalOp, cir::GlobalOp, cir::ConstantOp>(userOp)) &&
889 "Unexpected user for global op");
890
891 if (auto getGlobalOp = dyn_cast<cir::GetGlobalOp>(use.getUser())) {
892 mlir::Value useOpResultValue = getGlobalOp.getAddr();
893 useOpResultValue.setType(cir::PointerType::get(newTy));
894
895 mlir::OpBuilder::InsertionGuard guard(builder);
896 builder.setInsertionPointAfter(getGlobalOp);
897 mlir::Type ptrTy = builder.getPointerTo(oldTy);
898 mlir::Value cast =
899 builder.createBitcast(getGlobalOp->getLoc(), useOpResultValue, ptrTy);
900 useOpResultValue.replaceAllUsesExcept(cast, cast.getDefiningOp());
901 } else if (auto glob = dyn_cast<cir::GlobalOp>(userOp)) {
902 if (auto init = glob.getInitialValue()) {
903 mlir::Attribute nw = getNewInitValue(*this, newGV, oldTy, init.value());
904 glob.setInitialValueAttr(nw);
905 }
906 } else if (auto c = dyn_cast<cir::ConstantOp>(userOp)) {
907 mlir::Attribute init = getNewInitValue(*this, newGV, oldTy, c.getValue());
908 auto typedAttr = mlir::cast<mlir::TypedAttr>(init);
909 mlir::OpBuilder::InsertionGuard guard(builder);
910 builder.setInsertionPointAfter(c);
911 auto newUser = cir::ConstantOp::create(builder, c.getLoc(), typedAttr);
912 c.replaceAllUsesWith(newUser.getOperation());
913 c.erase();
914 }
915 }
916
917 oldGV.erase();
918}
919
920/// If the specified mangled name is not in the module,
921/// create and return an mlir GlobalOp with the specified type (TODO(cir):
922/// address space).
923///
924/// TODO(cir):
925/// 1. If there is something in the module with the specified name, return
926/// it potentially bitcasted to the right type.
927///
928/// 2. If \p d is non-null, it specifies a decl that correspond to this. This
929/// is used to set the attributes on the global when it is first created.
930///
931/// 3. If \p isForDefinition is true, it is guaranteed that an actual global
932/// with type \p ty will be returned, not conversion of a variable with the same
933/// mangled name but some other type.
934cir::GlobalOp
935CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
936 LangAS langAS, const VarDecl *d,
937 ForDefinition_t isForDefinition) {
938
939 // Lookup the entry, lazily creating it if necessary.
940 cir::GlobalOp entry;
941 if (mlir::Operation *v = getGlobalValue(mangledName)) {
942 if (!isa<cir::GlobalOp>(v))
944 "getOrCreateCIRGlobal: global with non-GlobalOp type");
945 entry = cast<cir::GlobalOp>(v);
946 }
947
948 if (entry) {
949 mlir::ptr::MemorySpaceAttrInterface entryCIRAS = entry.getAddrSpaceAttr();
951
954
955 if (entry.getSymType() == ty &&
956 cir::isMatchingAddressSpace(entryCIRAS, langAS))
957 return entry;
958
959 // If there are two attempts to define the same mangled name, issue an
960 // error.
961 //
962 // TODO(cir): look at mlir::GlobalValue::isDeclaration for all aspects of
963 // recognizing the global as a declaration, for now only check if
964 // initializer is present.
965 if (isForDefinition && !entry.isDeclaration()) {
967 "getOrCreateCIRGlobal: global with conflicting type");
968 }
969
970 // Address space check removed because it is unnecessary because CIR records
971 // address space info in types.
972
973 // (If global is requested for a definition, we always need to create a new
974 // global, not just return a bitcast.)
975 if (!isForDefinition)
976 return entry;
977 }
978
979 mlir::Location loc = getLoc(d->getSourceRange());
980
981 // Calculate constant storage flag before creating the global. This was moved
982 // from after the global creation to ensure the constant flag is set correctly
983 // at creation time, matching the logic used in emitCXXGlobalVarDeclInit.
984 bool isConstant = false;
985 if (d) {
986 bool needsDtor =
988 isConstant = d->getType().isConstantStorage(
989 astContext, /*ExcludeCtor=*/true, /*ExcludeDtor=*/!needsDtor);
990 }
991
992 mlir::ptr::MemorySpaceAttrInterface declCIRAS =
993 cir::toCIRAddressSpaceAttr(getMLIRContext(), getGlobalVarAddressSpace(d));
994
995 // mlir::SymbolTable::Visibility::Public is the default, no need to explicitly
996 // mark it as such.
997 cir::GlobalOp gv = CIRGenModule::createGlobalOp(
998 *this, loc, mangledName, ty, isConstant, declCIRAS,
999 /*insertPoint=*/entry.getOperation());
1000
1001 // If we already created a global with the same mangled name (but different
1002 // type) before, remove it from its parent.
1003 if (entry)
1004 replaceGlobal(entry, gv);
1005
1006 // This is the first use or definition of a mangled name. If there is a
1007 // deferred decl with this name, remember that we need to emit it at the end
1008 // of the file.
1009 auto ddi = deferredDecls.find(mangledName);
1010 if (ddi != deferredDecls.end()) {
1011 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
1012 // list, and remove it from DeferredDecls (since we don't need it anymore).
1013 addDeferredDeclToEmit(ddi->second);
1014 deferredDecls.erase(ddi);
1015 }
1016
1017 // Handle things which are present even on external declarations.
1018 if (d) {
1019 if (langOpts.OpenMP && !langOpts.OpenMPSimd)
1021 "getOrCreateCIRGlobal: OpenMP target global variable");
1022
1023 gv.setAlignmentAttr(getSize(astContext.getDeclAlign(d)));
1024
1025 setLinkageForGV(gv, d);
1026
1027 if (d->getTLSKind()) {
1028 if (d->getTLSKind() == VarDecl::TLS_Dynamic)
1029 errorNYI(d->getSourceRange(), "getOrCreateCIRGlobal: TLS dynamic");
1030 setTLSMode(gv, *d);
1031 }
1032
1033 setGVProperties(gv, d);
1034
1035 // If required by the ABI, treat declarations of static data members with
1036 // inline initializers as definitions.
1037 if (astContext.isMSStaticDataMemberInlineDefinition(d))
1039 "getOrCreateCIRGlobal: MS static data member inline definition");
1040
1041 // Emit section information for extern variables.
1042 if (d->hasExternalStorage()) {
1043 if (const SectionAttr *sa = d->getAttr<SectionAttr>())
1044 gv.setSectionAttr(builder.getStringAttr(sa->getName()));
1045 }
1046 gv.setGlobalVisibility(getGlobalVisibilityAttrFromDecl(d).getValue());
1047
1048 // Handle XCore specific ABI requirements.
1049 if (getTriple().getArch() == llvm::Triple::xcore)
1051 "getOrCreateCIRGlobal: XCore specific ABI requirements");
1052
1053 // Check if we a have a const declaration with an initializer, we may be
1054 // able to emit it as available_externally to expose it's value to the
1055 // optimizer.
1056 if (getLangOpts().CPlusPlus && gv.isPublic() &&
1057 d->getType().isConstQualified() && gv.isDeclaration() &&
1058 !d->hasDefinition() && d->hasInit() && !d->hasAttr<DLLImportAttr>())
1059 errorNYI(
1060 d->getSourceRange(),
1061 "getOrCreateCIRGlobal: external const declaration with initializer");
1062 }
1063
1064 if (d &&
1067 // TODO(cir): set target attributes
1068 // External HIP managed variables needed to be recorded for transformation
1069 // in both device and host compilations.
1070 if (getLangOpts().CUDA && d && d->hasAttr<HIPManagedAttr>() &&
1071 d->hasExternalStorage())
1073 "getOrCreateCIRGlobal: HIP managed attribute");
1074 }
1075
1077 return gv;
1078}
1079
1080cir::GlobalOp
1082 ForDefinition_t isForDefinition) {
1083 assert(d->hasGlobalStorage() && "Not a global variable");
1084 QualType astTy = d->getType();
1085 if (!ty)
1086 ty = getTypes().convertTypeForMem(astTy);
1087
1088 StringRef mangledName = getMangledName(d);
1089 return getOrCreateCIRGlobal(mangledName, ty, getGlobalVarAddressSpace(d), d,
1090 isForDefinition);
1091}
1092
1093/// Return the mlir::Value for the address of the given global variable. If
1094/// \p ty is non-null and if the global doesn't exist, then it will be created
1095/// with the specified type instead of whatever the normal requested type would
1096/// be. If \p isForDefinition is true, it is guaranteed that an actual global
1097/// with type \p ty will be returned, not conversion of a variable with the same
1098/// mangled name but some other type.
1099mlir::Value CIRGenModule::getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty,
1100 ForDefinition_t isForDefinition) {
1101 assert(d->hasGlobalStorage() && "Not a global variable");
1102 QualType astTy = d->getType();
1103 if (!ty)
1104 ty = getTypes().convertTypeForMem(astTy);
1105
1106 bool tlsAccess = d->getTLSKind() != VarDecl::TLS_None;
1107 cir::GlobalOp g = getOrCreateCIRGlobal(d, ty, isForDefinition);
1108 mlir::Type ptrTy = builder.getPointerTo(g.getSymType(), g.getAddrSpaceAttr());
1109 return cir::GetGlobalOp::create(
1110 builder, getLoc(d->getSourceRange()), ptrTy, g.getSymNameAttr(),
1111 tlsAccess,
1112 /*static_local=*/g.getStaticLocalGuard().has_value());
1113}
1114
1115cir::GlobalViewAttr CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *d) {
1116 assert(d->hasGlobalStorage() && "Not a global variable");
1117 mlir::Type ty = getTypes().convertTypeForMem(d->getType());
1118
1119 cir::GlobalOp globalOp = getOrCreateCIRGlobal(d, ty, NotForDefinition);
1120 cir::PointerType ptrTy =
1121 builder.getPointerTo(globalOp.getSymType(), globalOp.getAddrSpaceAttr());
1122 return builder.getGlobalViewAttr(ptrTy, globalOp);
1123}
1124
1125void CIRGenModule::addUsedGlobal(cir::CIRGlobalValueInterface gv) {
1126 assert((mlir::isa<cir::FuncOp>(gv.getOperation()) ||
1127 !gv.isDeclarationForLinker()) &&
1128 "Only globals with definition can force usage.");
1129 llvmUsed.emplace_back(gv);
1130}
1131
1132void CIRGenModule::addCompilerUsedGlobal(cir::CIRGlobalValueInterface gv) {
1133 assert(!gv.isDeclarationForLinker() &&
1134 "Only globals with definition can force usage.");
1135 llvmCompilerUsed.emplace_back(gv);
1136}
1137
1139 cir::CIRGlobalValueInterface gv) {
1140 assert((mlir::isa<cir::FuncOp>(gv.getOperation()) ||
1141 !gv.isDeclarationForLinker()) &&
1142 "Only globals with definition can force usage.");
1143 if (getTriple().isOSBinFormatELF())
1144 llvmCompilerUsed.emplace_back(gv);
1145 else
1146 llvmUsed.emplace_back(gv);
1147}
1148
1149static void emitUsed(CIRGenModule &cgm, StringRef name,
1150 std::vector<cir::CIRGlobalValueInterface> &list) {
1151 if (list.empty())
1152 return;
1153
1154 CIRGenBuilderTy &builder = cgm.getBuilder();
1155 mlir::Location loc = builder.getUnknownLoc();
1157 usedArray.resize(list.size());
1158 for (auto [i, op] : llvm::enumerate(list)) {
1159 usedArray[i] = cir::GlobalViewAttr::get(
1160 cgm.voidPtrTy, mlir::FlatSymbolRefAttr::get(op.getNameAttr()));
1161 }
1162
1163 cir::ArrayType arrayTy = cir::ArrayType::get(cgm.voidPtrTy, usedArray.size());
1164
1165 cir::ConstArrayAttr initAttr = cir::ConstArrayAttr::get(
1166 arrayTy, mlir::ArrayAttr::get(&cgm.getMLIRContext(), usedArray));
1167
1168 cir::GlobalOp gv = CIRGenModule::createGlobalOp(cgm, loc, name, arrayTy,
1169 /*isConstant=*/false);
1170 gv.setLinkage(cir::GlobalLinkageKind::AppendingLinkage);
1171 gv.setInitialValueAttr(initAttr);
1172 gv.setSectionAttr(builder.getStringAttr("llvm.metadata"));
1173}
1174
1176 emitUsed(*this, "llvm.used", llvmUsed);
1177 emitUsed(*this, "llvm.compiler.used", llvmCompilerUsed);
1178}
1179
1181 bool isTentative) {
1182 if (getLangOpts().OpenCL || getLangOpts().OpenMPIsTargetDevice) {
1184 "emitGlobalVarDefinition: emit OpenCL/OpenMP global variable");
1185 return;
1186 }
1187
1188 // Whether the definition of the variable is available externally.
1189 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
1190 // since this is the job for its original source.
1191 bool isDefinitionAvailableExternally =
1192 astContext.GetGVALinkageForVariable(vd) == GVA_AvailableExternally;
1193
1194 // It is useless to emit the definition for an available_externally variable
1195 // which can't be marked as const.
1196 if (isDefinitionAvailableExternally &&
1197 (!vd->hasConstantInitialization() ||
1198 // TODO: Update this when we have interface to check constexpr
1199 // destructor.
1200 vd->needsDestruction(astContext) ||
1201 !vd->getType().isConstantStorage(astContext, true, true)))
1202 return;
1203
1204 mlir::Attribute init;
1205 bool needsGlobalCtor = false;
1206 bool needsGlobalDtor =
1207 !isDefinitionAvailableExternally &&
1209 const VarDecl *initDecl;
1210 const Expr *initExpr = vd->getAnyInitializer(initDecl);
1211
1212 std::optional<ConstantEmitter> emitter;
1213
1214 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
1215 // as part of their declaration." Sema has already checked for
1216 // error cases, so we just need to set Init to PoisonValue.
1217 bool isCUDASharedVar =
1218 getLangOpts().CUDAIsDevice && vd->hasAttr<CUDASharedAttr>();
1219 // Shadows of initialized device-side global variables are also left
1220 // undefined.
1221 // Managed Variables should be initialized on both host side and device side.
1222 bool isCUDAShadowVar =
1223 !getLangOpts().CUDAIsDevice && !vd->hasAttr<HIPManagedAttr>() &&
1224 (vd->hasAttr<CUDAConstantAttr>() || vd->hasAttr<CUDADeviceAttr>() ||
1225 vd->hasAttr<CUDASharedAttr>());
1226 bool isCUDADeviceShadowVar =
1227 getLangOpts().CUDAIsDevice && !vd->hasAttr<HIPManagedAttr>() &&
1230
1231 if (getLangOpts().CUDA &&
1232 (isCUDASharedVar || isCUDAShadowVar || isCUDADeviceShadowVar)) {
1233 init = cir::PoisonAttr::get(convertType(vd->getType()));
1234 } else if (vd->hasAttr<LoaderUninitializedAttr>()) {
1236 "emitGlobalVarDefinition: loader uninitialized attribute");
1237 } else if (!initExpr) {
1238 // This is a tentative definition; tentative definitions are
1239 // implicitly initialized with { 0 }.
1240 //
1241 // Note that tentative definitions are only emitted at the end of
1242 // a translation unit, so they should never have incomplete
1243 // type. In addition, EmitTentativeDefinition makes sure that we
1244 // never attempt to emit a tentative definition if a real one
1245 // exists. A use may still exists, however, so we still may need
1246 // to do a RAUW.
1247 assert(!vd->getType()->isIncompleteType() && "Unexpected incomplete type");
1248 init = builder.getZeroInitAttr(convertType(vd->getType()));
1249 } else {
1250 emitter.emplace(*this);
1251 mlir::Attribute initializer = emitter->tryEmitForInitializer(*initDecl);
1252 if (!initializer) {
1253 QualType qt = initExpr->getType();
1254 if (vd->getType()->isReferenceType())
1255 qt = vd->getType();
1256
1257 if (getLangOpts().CPlusPlus) {
1258 if (initDecl->hasFlexibleArrayInit(astContext))
1260 "emitGlobalVarDefinition: flexible array initializer");
1261 init = builder.getZeroInitAttr(convertType(qt));
1262 if (!isDefinitionAvailableExternally)
1263 needsGlobalCtor = true;
1264 } else {
1266 "emitGlobalVarDefinition: static initializer");
1267 }
1268 } else {
1269 init = initializer;
1270 // We don't need an initializer, so remove the entry for the delayed
1271 // initializer position (just in case this entry was delayed) if we
1272 // also don't need to register a destructor.
1274 }
1275 }
1276
1277 mlir::Type initType;
1278 if (mlir::isa<mlir::SymbolRefAttr>(init)) {
1279 errorNYI(
1280 vd->getSourceRange(),
1281 "emitGlobalVarDefinition: global initializer is a symbol reference");
1282 return;
1283 } else {
1284 assert(mlir::isa<mlir::TypedAttr>(init) && "This should have a type");
1285 auto typedInitAttr = mlir::cast<mlir::TypedAttr>(init);
1286 initType = typedInitAttr.getType();
1287 }
1288 assert(!mlir::isa<mlir::NoneType>(initType) && "Should have a type by now");
1289
1290 cir::GlobalOp gv =
1291 getOrCreateCIRGlobal(vd, initType, ForDefinition_t(!isTentative));
1292 // TODO(cir): Strip off pointer casts from Entry if we get them?
1293
1294 if (!gv || gv.getSymType() != initType) {
1296 "emitGlobalVarDefinition: global initializer with type mismatch");
1297 return;
1298 }
1299
1301
1302 if (vd->hasAttr<AnnotateAttr>()) {
1304 "emitGlobalVarDefinition: annotate global variable");
1305 }
1306
1307 // Set CIR's linkage type as appropriate.
1308 cir::GlobalLinkageKind linkage = getCIRLinkageVarDefinition(vd);
1309
1310 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
1311 // the device. [...]"
1312 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
1313 // __device__, declares a variable that: [...]
1314 // Is accessible from all the threads within the grid and from the host
1315 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
1316 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
1317 if (langOpts.CUDA) {
1318 if (langOpts.CUDAIsDevice) {
1319 // __shared__ variables is not marked as externally initialized,
1320 // because they must not be initialized.
1321 if (linkage != cir::GlobalLinkageKind::InternalLinkage &&
1322 !vd->isConstexpr() && !vd->getType().isConstQualified() &&
1323 (vd->hasAttr<CUDADeviceAttr>() || vd->hasAttr<CUDAConstantAttr>() ||
1326 gv->setAttr(cir::CUDAExternallyInitializedAttr::getMnemonic(),
1327 cir::CUDAExternallyInitializedAttr::get(&getMLIRContext()));
1328 }
1329 } else {
1330 // TODO(cir):
1331 // Adjust linkage of shadow variables in host compilation
1332 // getCUDARuntime().internalizeDeviceSideVar(vd, linkage);
1333 }
1334 // TODO(cir):
1335 // Handle variable registration
1336 // getCUDARuntime().handleVarRegistration(vd, gv);
1337 }
1338
1339 // Set initializer and finalize emission
1341 if (emitter)
1342 emitter->finalize(gv);
1343
1344 // If it is safe to mark the global 'constant', do so now.
1345 // Use the same logic as classic codegen EmitGlobalVarDefinition.
1346 gv.setConstant((vd->hasAttr<CUDAConstantAttr>() && langOpts.CUDAIsDevice) ||
1347 (!needsGlobalCtor && !needsGlobalDtor &&
1348 vd->getType().isConstantStorage(astContext,
1349 /*ExcludeCtor=*/true,
1350 /*ExcludeDtor=*/true)));
1351 // If it is in a read-only section, mark it 'constant'.
1352 if (const SectionAttr *sa = vd->getAttr<SectionAttr>()) {
1353 const ASTContext::SectionInfo &si = astContext.SectionInfos[sa->getName()];
1354 if ((si.SectionFlags & ASTContext::PSF_Write) == 0)
1355 gv.setConstant(true);
1356 }
1357
1358 // Set CIR linkage and DLL storage class.
1359 gv.setLinkage(linkage);
1360 // FIXME(cir): setLinkage should likely set MLIR's visibility automatically.
1361 gv.setVisibility(getMLIRVisibilityFromCIRLinkage(linkage));
1363 if (linkage == cir::GlobalLinkageKind::CommonLinkage) {
1364 // common vars aren't constant even if declared const.
1365 gv.setConstant(false);
1366 // Tentative definition of global variables may be initialized with
1367 // non-zero null pointers. In this case they should have weak linkage
1368 // since common linkage must have zero initializer and must not have
1369 // explicit section therefore cannot have non-zero initial value.
1370 std::optional<mlir::Attribute> initializer = gv.getInitialValue();
1371 if (initializer && !getBuilder().isNullValue(*initializer))
1372 gv.setLinkage(cir::GlobalLinkageKind::WeakAnyLinkage);
1373 }
1374
1375 setNonAliasAttributes(vd, gv);
1376
1378
1379 maybeSetTrivialComdat(*vd, gv);
1380
1381 // Emit the initializer function if necessary.
1382 if (needsGlobalCtor || needsGlobalDtor)
1383 emitCXXGlobalVarDeclInitFunc(vd, gv, needsGlobalCtor);
1384}
1385
1387 mlir::Operation *op) {
1388 const auto *decl = cast<ValueDecl>(gd.getDecl());
1389 if (const auto *fd = dyn_cast<FunctionDecl>(decl)) {
1390 // TODO(CIR): Skip generation of CIR for functions with available_externally
1391 // linkage at -O0.
1392
1393 if (const auto *method = dyn_cast<CXXMethodDecl>(decl)) {
1394 // Make sure to emit the definition(s) before we emit the thunks. This is
1395 // necessary for the generation of certain thunks.
1396 if (isa<CXXConstructorDecl>(method) || isa<CXXDestructorDecl>(method))
1397 abi->emitCXXStructor(gd);
1398 else if (fd->isMultiVersion())
1399 errorNYI(method->getSourceRange(), "multiversion functions");
1400 else
1402
1403 if (method->isVirtual())
1404 getVTables().emitThunks(gd);
1405
1406 return;
1407 }
1408
1409 if (fd->isMultiVersion())
1410 errorNYI(fd->getSourceRange(), "multiversion functions");
1412 return;
1413 }
1414
1415 if (const auto *vd = dyn_cast<VarDecl>(decl))
1416 return emitGlobalVarDefinition(vd, !vd->hasDefinition());
1417
1418 llvm_unreachable("Invalid argument to CIRGenModule::emitGlobalDefinition");
1419}
1420
1421mlir::Attribute
1423 assert(!e->getType()->isPointerType() && "Strings are always arrays");
1424
1425 // Don't emit it as the address of the string, emit the string data itself
1426 // as an inline array.
1427 if (e->getCharByteWidth() == 1) {
1428 SmallString<64> str(e->getString());
1429
1430 // Resize the string to the right size, which is indicated by its type.
1431 const ConstantArrayType *cat =
1432 astContext.getAsConstantArrayType(e->getType());
1433 uint64_t finalSize = cat->getZExtSize();
1434 str.resize(finalSize);
1435
1436 mlir::Type eltTy = convertType(cat->getElementType());
1437 return builder.getString(str, eltTy, finalSize, /*ensureNullTerm=*/false);
1438 }
1439
1440 auto arrayTy = mlir::cast<cir::ArrayType>(convertType(e->getType()));
1441
1442 auto arrayEltTy = mlir::cast<cir::IntType>(arrayTy.getElementType());
1443
1444 uint64_t arraySize = arrayTy.getSize();
1445 unsigned literalSize = e->getLength();
1446 assert(arraySize == literalSize + 1 &&
1447 "wide string literal array size must be literal length plus null "
1448 "terminator");
1449
1450 // Check if the string is all null bytes before building the vector.
1451 // In most non-zero cases, this will break out on the first element.
1452 bool isAllZero = true;
1453 for (unsigned i = 0; i < literalSize; ++i) {
1454 if (e->getCodeUnit(i) != 0) {
1455 isAllZero = false;
1456 break;
1457 }
1458 }
1459
1460 if (isAllZero)
1461 return cir::ZeroAttr::get(arrayTy);
1462
1463 // Otherwise emit a constant array holding the characters.
1465 elements.reserve(arraySize);
1466 for (unsigned i = 0; i < literalSize; ++i)
1467 elements.push_back(cir::IntAttr::get(arrayEltTy, e->getCodeUnit(i)));
1468 // Add null terminator
1469 elements.push_back(cir::IntAttr::get(arrayEltTy, 0));
1470
1471 auto elementsAttr = mlir::ArrayAttr::get(&getMLIRContext(), elements);
1472 return builder.getConstArray(elementsAttr, arrayTy);
1473}
1474
1476 return getTriple().supportsCOMDAT();
1477}
1478
1479static bool shouldBeInCOMDAT(CIRGenModule &cgm, const Decl &d) {
1480 if (!cgm.supportsCOMDAT())
1481 return false;
1482
1483 if (d.hasAttr<SelectAnyAttr>())
1484 return true;
1485
1486 GVALinkage linkage;
1487 if (auto *vd = dyn_cast<VarDecl>(&d))
1488 linkage = cgm.getASTContext().GetGVALinkageForVariable(vd);
1489 else
1490 linkage =
1492
1493 switch (linkage) {
1497 return false;
1500 return true;
1501 }
1502 llvm_unreachable("No such linkage");
1503}
1504
1505void CIRGenModule::maybeSetTrivialComdat(const Decl &d, mlir::Operation *op) {
1506 if (!shouldBeInCOMDAT(*this, d))
1507 return;
1508 if (auto globalOp = dyn_cast_or_null<cir::GlobalOp>(op)) {
1509 globalOp.setComdat(true);
1510 } else {
1511 auto funcOp = cast<cir::FuncOp>(op);
1512 funcOp.setComdat(true);
1513 }
1514}
1515
1517 // Make sure that this type is translated.
1518 genTypes.updateCompletedType(td);
1519}
1520
1521void CIRGenModule::addReplacement(StringRef name, mlir::Operation *op) {
1522 replacements[name] = op;
1523}
1524
1525#ifndef NDEBUG
1526static bool verifyPointerTypeArgs(mlir::ModuleOp modOp, cir::FuncOp oldF,
1527 cir::FuncOp newF) {
1528 std::optional<mlir::SymbolTable::UseRange> optionalUseRange =
1529 oldF.getSymbolUses(modOp);
1530 if (!optionalUseRange)
1531 return true;
1532
1533 for (const mlir::SymbolTable::SymbolUse &u : *optionalUseRange) {
1534 auto call = mlir::dyn_cast<cir::CallOp>(u.getUser());
1535 if (!call)
1536 continue;
1537
1538 for (auto [argOp, fnArgType] :
1539 llvm::zip(call.getArgs(), newF.getFunctionType().getInputs())) {
1540 if (argOp.getType() != fnArgType)
1541 return false;
1542 }
1543 }
1544
1545 return true;
1546}
1547#endif // NDEBUG
1548
1549void CIRGenModule::applyReplacements() {
1550 for (auto &i : replacements) {
1551 StringRef mangledName = i.first;
1552 mlir::Operation *replacement = i.second;
1553 mlir::Operation *entry = getGlobalValue(mangledName);
1554 if (!entry)
1555 continue;
1556 assert(isa<cir::FuncOp>(entry) && "expected function");
1557 auto oldF = cast<cir::FuncOp>(entry);
1558 auto newF = dyn_cast<cir::FuncOp>(replacement);
1559 if (!newF) {
1560 // In classic codegen, this can be a global alias, a bitcast, or a GEP.
1561 errorNYI(replacement->getLoc(), "replacement is not a function");
1562 continue;
1563 }
1564
1565 assert(verifyPointerTypeArgs(theModule, oldF, newF) &&
1566 "call argument types do not match replacement function");
1567
1568 // Replace old with new, but keep the old order.
1569 if (oldF.replaceAllSymbolUses(newF.getSymNameAttr(), theModule).failed())
1570 llvm_unreachable("internal error, cannot RAUW symbol");
1571 if (newF) {
1572 newF->moveBefore(oldF);
1573 oldF->erase();
1574 }
1575 }
1576}
1577
1579 mlir::Location loc, StringRef name, mlir::Type ty,
1580 cir::GlobalLinkageKind linkage, clang::CharUnits alignment) {
1581 auto gv = mlir::dyn_cast_or_null<cir::GlobalOp>(
1582 mlir::SymbolTable::lookupSymbolIn(theModule, name));
1583
1584 if (gv) {
1585 // Check if the variable has the right type.
1586 if (gv.getSymType() == ty)
1587 return gv;
1588
1589 // Because of C++ name mangling, the only way we can end up with an already
1590 // existing global with the same name is if it has been declared extern
1591 // "C".
1592 assert(gv.isDeclaration() && "Declaration has wrong type!");
1593
1594 errorNYI(loc, "createOrReplaceCXXRuntimeVariable: declaration exists with "
1595 "wrong type");
1596 return gv;
1597 }
1598
1599 // Create a new variable.
1600 gv = createGlobalOp(*this, loc, name, ty);
1601
1602 // Set up extra information and add to the module
1603 gv.setLinkageAttr(
1604 cir::GlobalLinkageKindAttr::get(&getMLIRContext(), linkage));
1605 mlir::SymbolTable::setSymbolVisibility(gv,
1607
1608 if (supportsCOMDAT() && cir::isWeakForLinker(linkage) &&
1609 !gv.hasAvailableExternallyLinkage()) {
1610 gv.setComdat(true);
1611 }
1612
1613 gv.setAlignmentAttr(getSize(alignment));
1614 setDSOLocal(static_cast<mlir::Operation *>(gv));
1615 return gv;
1616}
1617
1618// TODO(CIR): this could be a common method between LLVM codegen.
1619static bool isVarDeclStrongDefinition(const ASTContext &astContext,
1620 CIRGenModule &cgm, const VarDecl *vd,
1621 bool noCommon) {
1622 // Don't give variables common linkage if -fno-common was specified unless it
1623 // was overridden by a NoCommon attribute.
1624 if ((noCommon || vd->hasAttr<NoCommonAttr>()) && !vd->hasAttr<CommonAttr>())
1625 return true;
1626
1627 // C11 6.9.2/2:
1628 // A declaration of an identifier for an object that has file scope without
1629 // an initializer, and without a storage-class specifier or with the
1630 // storage-class specifier static, constitutes a tentative definition.
1631 if (vd->getInit() || vd->hasExternalStorage())
1632 return true;
1633
1634 // A variable cannot be both common and exist in a section.
1635 if (vd->hasAttr<SectionAttr>())
1636 return true;
1637
1638 // A variable cannot be both common and exist in a section.
1639 // We don't try to determine which is the right section in the front-end.
1640 // If no specialized section name is applicable, it will resort to default.
1641 if (vd->hasAttr<PragmaClangBSSSectionAttr>() ||
1642 vd->hasAttr<PragmaClangDataSectionAttr>() ||
1643 vd->hasAttr<PragmaClangRelroSectionAttr>() ||
1644 vd->hasAttr<PragmaClangRodataSectionAttr>())
1645 return true;
1646
1647 // Thread local vars aren't considered common linkage.
1648 if (vd->getTLSKind())
1649 return true;
1650
1651 // Tentative definitions marked with WeakImportAttr are true definitions.
1652 if (vd->hasAttr<WeakImportAttr>())
1653 return true;
1654
1655 // A variable cannot be both common and exist in a comdat.
1656 if (shouldBeInCOMDAT(cgm, *vd))
1657 return true;
1658
1659 // Declarations with a required alignment do not have common linkage in MSVC
1660 // mode.
1661 if (astContext.getTargetInfo().getCXXABI().isMicrosoft()) {
1662 if (vd->hasAttr<AlignedAttr>())
1663 return true;
1664 QualType varType = vd->getType();
1665 if (astContext.isAlignmentRequired(varType))
1666 return true;
1667
1668 if (const auto *rd = varType->getAsRecordDecl()) {
1669 for (const FieldDecl *fd : rd->fields()) {
1670 if (fd->isBitField())
1671 continue;
1672 if (fd->hasAttr<AlignedAttr>())
1673 return true;
1674 if (astContext.isAlignmentRequired(fd->getType()))
1675 return true;
1676 }
1677 }
1678 }
1679
1680 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
1681 // common symbols, so symbols with greater alignment requirements cannot be
1682 // common.
1683 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
1684 // alignments for common symbols via the aligncomm directive, so this
1685 // restriction only applies to MSVC environments.
1686 if (astContext.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
1687 astContext.getTypeAlignIfKnown(vd->getType()) >
1688 astContext.toBits(CharUnits::fromQuantity(32)))
1689 return true;
1690
1691 return false;
1692}
1693
1694cir::GlobalLinkageKind
1696 GVALinkage linkage) {
1697 if (linkage == GVA_Internal)
1698 return cir::GlobalLinkageKind::InternalLinkage;
1699
1700 if (dd->hasAttr<WeakAttr>())
1701 return cir::GlobalLinkageKind::WeakAnyLinkage;
1702
1703 if (const auto *fd = dd->getAsFunction())
1704 if (fd->isMultiVersion() && linkage == GVA_AvailableExternally)
1705 return cir::GlobalLinkageKind::LinkOnceAnyLinkage;
1706
1707 // We are guaranteed to have a strong definition somewhere else,
1708 // so we can use available_externally linkage.
1709 if (linkage == GVA_AvailableExternally)
1710 return cir::GlobalLinkageKind::AvailableExternallyLinkage;
1711
1712 // Note that Apple's kernel linker doesn't support symbol
1713 // coalescing, so we need to avoid linkonce and weak linkages there.
1714 // Normally, this means we just map to internal, but for explicit
1715 // instantiations we'll map to external.
1716
1717 // In C++, the compiler has to emit a definition in every translation unit
1718 // that references the function. We should use linkonce_odr because
1719 // a) if all references in this translation unit are optimized away, we
1720 // don't need to codegen it. b) if the function persists, it needs to be
1721 // merged with other definitions. c) C++ has the ODR, so we know the
1722 // definition is dependable.
1723 if (linkage == GVA_DiscardableODR)
1724 return !astContext.getLangOpts().AppleKext
1725 ? cir::GlobalLinkageKind::LinkOnceODRLinkage
1726 : cir::GlobalLinkageKind::InternalLinkage;
1727
1728 // An explicit instantiation of a template has weak linkage, since
1729 // explicit instantiations can occur in multiple translation units
1730 // and must all be equivalent. However, we are not allowed to
1731 // throw away these explicit instantiations.
1732 //
1733 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
1734 // so say that CUDA templates are either external (for kernels) or internal.
1735 // This lets llvm perform aggressive inter-procedural optimizations. For
1736 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
1737 // therefore we need to follow the normal linkage paradigm.
1738 if (linkage == GVA_StrongODR) {
1739 if (getLangOpts().AppleKext)
1740 return cir::GlobalLinkageKind::ExternalLinkage;
1741 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
1742 !getLangOpts().GPURelocatableDeviceCode)
1743 return dd->hasAttr<CUDAGlobalAttr>()
1744 ? cir::GlobalLinkageKind::ExternalLinkage
1745 : cir::GlobalLinkageKind::InternalLinkage;
1746 return cir::GlobalLinkageKind::WeakODRLinkage;
1747 }
1748
1749 // C++ doesn't have tentative definitions and thus cannot have common
1750 // linkage.
1751 if (!getLangOpts().CPlusPlus && isa<VarDecl>(dd) &&
1752 !isVarDeclStrongDefinition(astContext, *this, cast<VarDecl>(dd),
1753 getCodeGenOpts().NoCommon))
1754 return cir::GlobalLinkageKind::CommonLinkage;
1755
1756 // selectany symbols are externally visible, so use weak instead of
1757 // linkonce. MSVC optimizes away references to const selectany globals, so
1758 // all definitions should be the same and ODR linkage should be used.
1759 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
1760 if (dd->hasAttr<SelectAnyAttr>())
1761 return cir::GlobalLinkageKind::WeakODRLinkage;
1762
1763 // Otherwise, we have strong external linkage.
1764 assert(linkage == GVA_StrongExternal);
1765 return cir::GlobalLinkageKind::ExternalLinkage;
1766}
1767
1768/// This function is called when we implement a function with no prototype, e.g.
1769/// "int foo() {}". If there are existing call uses of the old function in the
1770/// module, this adjusts them to call the new function directly.
1771///
1772/// This is not just a cleanup: the always_inline pass requires direct calls to
1773/// functions to be able to inline them. If there is a bitcast in the way, it
1774/// won't inline them. Instcombine normally deletes these calls, but it isn't
1775/// run at -O0.
1777 mlir::Operation *old, cir::FuncOp newFn) {
1778 // If we're redefining a global as a function, don't transform it.
1779 auto oldFn = mlir::dyn_cast<cir::FuncOp>(old);
1780 if (!oldFn)
1781 return;
1782
1783 // TODO(cir): this RAUW ignores the features below.
1787 if (oldFn->getAttrs().size() <= 1)
1788 errorNYI(old->getLoc(),
1789 "replaceUsesOfNonProtoTypeWithRealFunction: Attribute forwarding");
1790
1791 // Mark new function as originated from a no-proto declaration.
1792 newFn.setNoProto(oldFn.getNoProto());
1793
1794 // Iterate through all calls of the no-proto function.
1795 std::optional<mlir::SymbolTable::UseRange> symUses =
1796 oldFn.getSymbolUses(oldFn->getParentOp());
1797 for (const mlir::SymbolTable::SymbolUse &use : symUses.value()) {
1798 mlir::OpBuilder::InsertionGuard guard(builder);
1799
1800 if (auto noProtoCallOp = mlir::dyn_cast<cir::CallOp>(use.getUser())) {
1801 builder.setInsertionPoint(noProtoCallOp);
1802
1803 // Patch call type with the real function type.
1804 cir::CallOp realCallOp = builder.createCallOp(
1805 noProtoCallOp.getLoc(), newFn, noProtoCallOp.getOperands());
1806
1807 // Replace old no proto call with fixed call.
1808 noProtoCallOp.replaceAllUsesWith(realCallOp);
1809 noProtoCallOp.erase();
1810 } else if (auto getGlobalOp =
1811 mlir::dyn_cast<cir::GetGlobalOp>(use.getUser())) {
1812 // Replace type
1813 getGlobalOp.getAddr().setType(
1814 cir::PointerType::get(newFn.getFunctionType()));
1815 } else if (mlir::isa<cir::GlobalOp>(use.getUser())) {
1816 // Function addresses in global initializers use GlobalViewAttrs typed to
1817 // the initializer context (e.g. struct field type), not the FuncOp type,
1818 // so no update is required when the no-proto FuncOp is replaced.
1819 } else {
1820 llvm_unreachable(
1821 "replaceUsesOfNonProtoTypeWithRealFunction: unexpected use type");
1822 }
1823 }
1824}
1825
1826cir::GlobalLinkageKind
1828 GVALinkage linkage = astContext.GetGVALinkageForVariable(vd);
1829 return getCIRLinkageForDeclarator(vd, linkage);
1830}
1831
1833 const auto *d = cast<FunctionDecl>(gd.getDecl());
1834
1835 GVALinkage linkage = astContext.GetGVALinkageForFunction(d);
1836
1837 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(d))
1838 return getCXXABI().getCXXDestructorLinkage(linkage, dtor, gd.getDtorType());
1839
1840 return getCIRLinkageForDeclarator(d, linkage);
1841}
1842
1843static cir::GlobalOp
1844generateStringLiteral(mlir::Location loc, mlir::TypedAttr c,
1845 cir::GlobalLinkageKind lt, CIRGenModule &cgm,
1846 StringRef globalName, CharUnits alignment) {
1848
1849 // Create a global variable for this string
1850 // FIXME(cir): check for insertion point in module level.
1851 cir::GlobalOp gv = CIRGenModule::createGlobalOp(
1852 cgm, loc, globalName, c.getType(), !cgm.getLangOpts().WritableStrings);
1853
1854 // Set up extra information and add to the module
1855 gv.setAlignmentAttr(cgm.getSize(alignment));
1856 gv.setLinkageAttr(
1857 cir::GlobalLinkageKindAttr::get(cgm.getBuilder().getContext(), lt));
1861 if (gv.isWeakForLinker()) {
1862 assert(cgm.supportsCOMDAT() && "Only COFF uses weak string literals");
1863 gv.setComdat(true);
1864 }
1865 cgm.setDSOLocal(static_cast<mlir::Operation *>(gv));
1866 return gv;
1867}
1868
1869// LLVM IR automatically uniques names when new llvm::GlobalVariables are
1870// created. This is handy, for example, when creating globals for string
1871// literals. Since we don't do that when creating cir::GlobalOp's, we need
1872// a mechanism to generate a unique name in advance.
1873//
1874// For now, this mechanism is only used in cases where we know that the
1875// name is compiler-generated, so we don't use the MLIR symbol table for
1876// the lookup.
1877std::string CIRGenModule::getUniqueGlobalName(const std::string &baseName) {
1878 // If this is the first time we've generated a name for this basename, use
1879 // it as is and start a counter for this base name.
1880 auto it = cgGlobalNames.find(baseName);
1881 if (it == cgGlobalNames.end()) {
1882 cgGlobalNames[baseName] = 1;
1883 return baseName;
1884 }
1885
1886 std::string result =
1887 baseName + "." + std::to_string(cgGlobalNames[baseName]++);
1888 // There should not be any symbol with this name in the module.
1889 assert(!mlir::SymbolTable::lookupSymbolIn(theModule, result));
1890 return result;
1891}
1892
1893/// Return a pointer to a constant array for the given string literal.
1895 StringRef name) {
1896 CharUnits alignment =
1897 astContext.getAlignOfGlobalVarInChars(s->getType(), /*VD=*/nullptr);
1898
1899 mlir::Attribute c = getConstantArrayFromStringLiteral(s);
1900
1901 cir::GlobalOp gv;
1902 if (!getLangOpts().WritableStrings && constantStringMap.count(c)) {
1903 gv = constantStringMap[c];
1904 // The bigger alignment always wins.
1905 if (!gv.getAlignment() ||
1906 uint64_t(alignment.getQuantity()) > *gv.getAlignment())
1907 gv.setAlignmentAttr(getSize(alignment));
1908 } else {
1909 // Mangle the string literal if that's how the ABI merges duplicate strings.
1910 // Don't do it if they are writable, since we don't want writes in one TU to
1911 // affect strings in another.
1912 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(s) &&
1913 !getLangOpts().WritableStrings) {
1914 errorNYI(s->getSourceRange(),
1915 "getGlobalForStringLiteral: mangle string literals");
1916 }
1917
1918 // Unlike LLVM IR, CIR doesn't automatically unique names for globals, so
1919 // we need to do that explicitly.
1920 std::string uniqueName = getUniqueGlobalName(name.str());
1921 // Synthetic string literals (e.g., from SourceLocExpr) may not have valid
1922 // source locations. Use unknown location in those cases.
1923 mlir::Location loc = s->getBeginLoc().isValid()
1924 ? getLoc(s->getSourceRange())
1925 : builder.getUnknownLoc();
1926 auto typedC = llvm::cast<mlir::TypedAttr>(c);
1927 gv = generateStringLiteral(loc, typedC,
1928 cir::GlobalLinkageKind::PrivateLinkage, *this,
1929 uniqueName, alignment);
1930 setDSOLocal(static_cast<mlir::Operation *>(gv));
1931 constantStringMap[c] = gv;
1932
1934 }
1935 return gv;
1936}
1937
1938/// Return a pointer to a constant array for the given string literal.
1939cir::GlobalViewAttr
1941 StringRef name) {
1942 cir::GlobalOp gv = getGlobalForStringLiteral(s, name);
1943 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(gv.getSymType());
1944 assert(arrayTy && "String literal must be array");
1946 cir::PointerType ptrTy = getBuilder().getPointerTo(arrayTy.getElementType());
1947
1948 return builder.getGlobalViewAttr(ptrTy, gv);
1949}
1950
1951// TODO(cir): this could be a common AST helper for both CIR and LLVM codegen.
1953 if (getLangOpts().OpenCL)
1955
1956 // For temporaries inside functions, CUDA treats them as normal variables.
1957 // LangAS::cuda_device, on the other hand, is reserved for those variables
1958 // explicitly marked with __device__.
1959 if (getLangOpts().CUDAIsDevice)
1960 return LangAS::Default;
1961
1962 if (getLangOpts().SYCLIsDevice ||
1963 (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice))
1964 errorNYI("SYCL or OpenMP temp address space");
1965 return LangAS::Default;
1966}
1967
1969 CIRGenFunction *cgf) {
1970 if (cgf && e->getType()->isVariablyModifiedType())
1972
1974 "emitExplicitCastExprType");
1975}
1976
1978 const MemberPointerType *mpt) {
1979 if (mpt->isMemberFunctionPointerType()) {
1980 auto ty = mlir::cast<cir::MethodType>(convertType(destTy));
1981 return builder.getNullMethodAttr(ty);
1982 }
1983
1984 auto ty = mlir::cast<cir::DataMemberType>(convertType(destTy));
1985 return builder.getNullDataMemberAttr(ty);
1986}
1987
1990
1991 mlir::Location loc = getLoc(e->getSourceRange());
1992
1993 const auto *decl = cast<DeclRefExpr>(e->getSubExpr())->getDecl();
1994
1995 // A member function pointer.
1996 if (const auto *methodDecl = dyn_cast<CXXMethodDecl>(decl)) {
1997 auto ty = mlir::cast<cir::MethodType>(convertType(e->getType()));
1998 if (methodDecl->isVirtual())
1999 return cir::ConstantOp::create(
2000 builder, loc, getCXXABI().buildVirtualMethodAttr(ty, methodDecl));
2001
2002 const CIRGenFunctionInfo &fi =
2004 cir::FuncType funcTy = getTypes().getFunctionType(fi);
2005 cir::FuncOp methodFuncOp = getAddrOfFunction(methodDecl, funcTy);
2006 return cir::ConstantOp::create(builder, loc,
2007 builder.getMethodAttr(ty, methodFuncOp));
2008 }
2009
2010 // Otherwise, a member data pointer.
2011 auto ty = mlir::cast<cir::DataMemberType>(convertType(e->getType()));
2012 const auto *fieldDecl = cast<FieldDecl>(decl);
2013 return cir::ConstantOp::create(
2014 builder, loc, builder.getDataMemberAttr(ty, fieldDecl->getFieldIndex()));
2015}
2016
2018 for (Decl *decl : dc->decls()) {
2019 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
2020 // are themselves considered "top-level", so EmitTopLevelDecl on an
2021 // ObjCImplDecl does not recursively visit them. We need to do that in
2022 // case they're nested inside another construct (LinkageSpecDecl /
2023 // ExportDecl) that does stop them from being considered "top-level".
2024 if (auto *oid = dyn_cast<ObjCImplDecl>(decl))
2025 errorNYI(oid->getSourceRange(), "emitDeclConext: ObjCImplDecl");
2026
2028 }
2029}
2030
2031// Emit code for a single top level declaration.
2033
2034 // Ignore dependent declarations.
2035 if (decl->isTemplated())
2036 return;
2037
2038 switch (decl->getKind()) {
2039 default:
2040 errorNYI(decl->getBeginLoc(), "declaration of kind",
2041 decl->getDeclKindName());
2042 break;
2043
2044 case Decl::CXXConversion:
2045 case Decl::CXXMethod:
2046 case Decl::Function: {
2047 auto *fd = cast<FunctionDecl>(decl);
2048 // Consteval functions shouldn't be emitted.
2049 if (!fd->isConsteval())
2050 emitGlobal(fd);
2051 break;
2052 }
2053 case Decl::Export:
2055 break;
2056
2057 case Decl::Var:
2058 case Decl::Decomposition:
2059 case Decl::VarTemplateSpecialization: {
2061 if (auto *decomp = dyn_cast<DecompositionDecl>(decl))
2062 for (auto *binding : decomp->flat_bindings())
2063 if (auto *holdingVar = binding->getHoldingVar())
2064 emitGlobal(holdingVar);
2065 break;
2066 }
2067 case Decl::OpenACCRoutine:
2069 break;
2070 case Decl::OpenACCDeclare:
2072 break;
2073 case Decl::OMPThreadPrivate:
2075 break;
2076 case Decl::OMPGroupPrivate:
2078 break;
2079 case Decl::OMPAllocate:
2081 break;
2082 case Decl::OMPCapturedExpr:
2084 break;
2085 case Decl::OMPDeclareReduction:
2087 break;
2088 case Decl::OMPDeclareMapper:
2090 break;
2091 case Decl::OMPRequires:
2093 break;
2094 case Decl::Enum:
2095 case Decl::Using: // using X; [C++]
2096 case Decl::UsingDirective: // using namespace X; [C++]
2097 case Decl::UsingEnum: // using enum X; [C++]
2098 case Decl::NamespaceAlias:
2099 case Decl::Typedef:
2100 case Decl::TypeAlias: // using foo = bar; [C++11]
2101 case Decl::Record:
2103 break;
2104
2105 // No code generation needed.
2106 case Decl::ClassTemplate:
2107 case Decl::Concept:
2108 case Decl::CXXDeductionGuide:
2109 case Decl::Empty:
2110 case Decl::FunctionTemplate:
2111 case Decl::StaticAssert:
2112 case Decl::TypeAliasTemplate:
2113 case Decl::UsingShadow:
2114 case Decl::VarTemplate:
2115 case Decl::VarTemplatePartialSpecialization:
2116 break;
2117
2118 case Decl::CXXConstructor:
2120 break;
2121 case Decl::CXXDestructor:
2123 break;
2124
2125 // C++ Decls
2126 case Decl::LinkageSpec:
2127 case Decl::Namespace:
2129 break;
2130
2131 case Decl::ClassTemplateSpecialization:
2132 case Decl::CXXRecord: {
2135 for (auto *childDecl : crd->decls())
2137 emitTopLevelDecl(childDecl);
2138 break;
2139 }
2140
2141 case Decl::FileScopeAsm:
2142 // File-scope asm is ignored during device-side CUDA compilation.
2143 if (langOpts.CUDA && langOpts.CUDAIsDevice)
2144 break;
2145 // File-scope asm is ignored during device-side OpenMP compilation.
2146 if (langOpts.OpenMPIsTargetDevice)
2147 break;
2148 // File-scope asm is ignored during device-side SYCL compilation.
2149 if (langOpts.SYCLIsDevice)
2150 break;
2151 auto *file_asm = cast<FileScopeAsmDecl>(decl);
2152 std::string line = file_asm->getAsmString();
2153 globalScopeAsm.push_back(builder.getStringAttr(line));
2154 break;
2155 }
2156}
2157
2158void CIRGenModule::setInitializer(cir::GlobalOp &op, mlir::Attribute value) {
2159 // Recompute visibility when updating initializer.
2160 op.setInitialValueAttr(value);
2162}
2163
2164std::pair<cir::FuncType, cir::FuncOp> CIRGenModule::getAddrAndTypeOfCXXStructor(
2165 GlobalDecl gd, const CIRGenFunctionInfo *fnInfo, cir::FuncType fnType,
2166 bool dontDefer, ForDefinition_t isForDefinition) {
2167 auto *md = cast<CXXMethodDecl>(gd.getDecl());
2168
2169 if (isa<CXXDestructorDecl>(md)) {
2170 // Always alias equivalent complete destructors to base destructors in the
2171 // MS ABI.
2172 if (getTarget().getCXXABI().isMicrosoft() &&
2173 gd.getDtorType() == Dtor_Complete &&
2174 md->getParent()->getNumVBases() == 0)
2175 errorNYI(md->getSourceRange(),
2176 "getAddrAndTypeOfCXXStructor: MS ABI complete destructor");
2177 }
2178
2179 if (!fnType) {
2180 if (!fnInfo)
2182 fnType = getTypes().getFunctionType(*fnInfo);
2183 }
2184
2185 auto fn = getOrCreateCIRFunction(getMangledName(gd), fnType, gd,
2186 /*ForVtable=*/false, dontDefer,
2187 /*IsThunk=*/false, isForDefinition);
2188
2189 return {fnType, fn};
2190}
2191
2193 mlir::Type funcType, bool forVTable,
2194 bool dontDefer,
2195 ForDefinition_t isForDefinition) {
2196 assert(!cast<FunctionDecl>(gd.getDecl())->isConsteval() &&
2197 "consteval function should never be emitted");
2198
2199 if (!funcType) {
2200 const auto *fd = cast<FunctionDecl>(gd.getDecl());
2201 funcType = convertType(fd->getType());
2202 }
2203
2204 // Devirtualized destructor calls may come through here instead of via
2205 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
2206 // of the complete destructor when necessary.
2207 if (const auto *dd = dyn_cast<CXXDestructorDecl>(gd.getDecl())) {
2208 if (getTarget().getCXXABI().isMicrosoft() &&
2209 gd.getDtorType() == Dtor_Complete &&
2210 dd->getParent()->getNumVBases() == 0)
2211 errorNYI(dd->getSourceRange(),
2212 "getAddrOfFunction: MS ABI complete destructor");
2213 }
2214
2215 StringRef mangledName = getMangledName(gd);
2216 cir::FuncOp func =
2217 getOrCreateCIRFunction(mangledName, funcType, gd, forVTable, dontDefer,
2218 /*isThunk=*/false, isForDefinition);
2219 // Returns kernel handle for HIP kernel stub function.
2220 if (langOpts.CUDA && !langOpts.CUDAIsDevice &&
2221 cast<FunctionDecl>(gd.getDecl())->hasAttr<CUDAGlobalAttr>()) {
2222 mlir::Operation *handle = getCUDARuntime().getKernelHandle(func, gd);
2223
2224 // For HIP the kernel handle is a GlobalOp, which cannot be cast to
2225 // FuncOp. Return the stub directly in that case.
2226 bool isHIPHandle = mlir::isa<cir::GlobalOp>(*handle);
2227 if (isForDefinition || isHIPHandle)
2228 return func;
2229 return mlir::dyn_cast<cir::FuncOp>(*handle);
2230 }
2231 return func;
2232}
2233
2234static std::string getMangledNameImpl(CIRGenModule &cgm, GlobalDecl gd,
2235 const NamedDecl *nd) {
2236 SmallString<256> buffer;
2237
2238 llvm::raw_svector_ostream out(buffer);
2240
2242
2243 if (mc.shouldMangleDeclName(nd)) {
2244 mc.mangleName(gd.getWithDecl(nd), out);
2245 } else {
2246 IdentifierInfo *ii = nd->getIdentifier();
2247 assert(ii && "Attempt to mangle unnamed decl.");
2248
2249 const auto *fd = dyn_cast<FunctionDecl>(nd);
2250 if (fd &&
2251 fd->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
2252 cgm.errorNYI(nd->getSourceRange(), "getMangledName: X86RegCall");
2253 } else if (fd && fd->hasAttr<CUDAGlobalAttr>() &&
2255 out << "__device_stub__" << ii->getName();
2256 } else if (fd &&
2257 DeviceKernelAttr::isOpenCLSpelling(
2258 fd->getAttr<DeviceKernelAttr>()) &&
2260 cgm.errorNYI(nd->getSourceRange(), "getMangledName: OpenCL Stub");
2261 } else {
2262 out << ii->getName();
2263 }
2264 }
2265
2266 // Check if the module name hash should be appended for internal linkage
2267 // symbols. This should come before multi-version target suffixes are
2268 // appendded. This is to keep the name and module hash suffix of the internal
2269 // linkage function together. The unique suffix should only be added when name
2270 // mangling is done to make sure that the final name can be properly
2271 // demangled. For example, for C functions without prototypes, name mangling
2272 // is not done and the unique suffix should not be appended then.
2274
2275 if (const auto *fd = dyn_cast<FunctionDecl>(nd)) {
2276 if (fd->isMultiVersion()) {
2277 cgm.errorNYI(nd->getSourceRange(),
2278 "getMangledName: multi-version functions");
2279 }
2280 }
2281 if (cgm.getLangOpts().GPURelocatableDeviceCode) {
2282 cgm.errorNYI(nd->getSourceRange(),
2283 "getMangledName: GPU relocatable device code");
2284 }
2285
2286 return std::string(out.str());
2287}
2288
2289static FunctionDecl *
2291 const FunctionDecl *protoFunc) {
2292 // If this is a C no-prototype function, we can take the 'easy' way out and
2293 // just create a function with no arguments/functions, etc.
2294 if (!protoFunc->hasPrototype())
2295 return FunctionDecl::Create(
2296 ctx, /*DC=*/ctx.getTranslationUnitDecl(),
2297 /*StartLoc=*/SourceLocation{}, /*NLoc=*/SourceLocation{}, bindName,
2298 protoFunc->getType(), /*TInfo=*/nullptr, StorageClass::SC_None);
2299
2300 QualType funcTy = protoFunc->getType();
2301 auto *fpt = cast<FunctionProtoType>(protoFunc->getType());
2302
2303 // If this is a member function, add an explicit 'this' to the function type.
2304 if (auto *methodDecl = dyn_cast<CXXMethodDecl>(protoFunc);
2305 methodDecl && methodDecl->isImplicitObjectMemberFunction()) {
2306 llvm::SmallVector<QualType> paramTypes{fpt->getParamTypes()};
2307 paramTypes.insert(paramTypes.begin(), methodDecl->getThisType());
2308
2309 funcTy = ctx.getFunctionType(fpt->getReturnType(), paramTypes,
2310 fpt->getExtProtoInfo());
2311 fpt = cast<FunctionProtoType>(funcTy);
2312 }
2313
2314 auto *tempFunc =
2316 /*StartLoc=*/SourceLocation{},
2317 /*NLoc=*/SourceLocation{}, bindName, funcTy,
2318 /*TInfo=*/nullptr, StorageClass::SC_None);
2319
2321 params.reserve(fpt->getNumParams());
2322
2323 // Add all of the parameters.
2324 for (unsigned i = 0, e = fpt->getNumParams(); i != e; ++i) {
2326 ctx, tempFunc, /*StartLoc=*/SourceLocation{},
2327 /*IdLoc=*/SourceLocation{},
2328 /*Id=*/nullptr, fpt->getParamType(i), /*TInfo=*/nullptr,
2329 StorageClass::SC_None, /*DefArg=*/nullptr);
2330 parm->setScopeInfo(0, i);
2331 params.push_back(parm);
2332 }
2333
2334 tempFunc->setParams(params);
2335
2336 return tempFunc;
2337}
2338
2339std::string
2341 const FunctionDecl *attachedFunction) {
2343 getASTContext(), bindName, attachedFunction);
2344
2345 std::string ret = getMangledNameImpl(*this, GlobalDecl(tempFunc), tempFunc);
2346
2347 // This does nothing (it is a do-nothing function), since this is a
2348 // slab-allocator, but leave a call in to immediately destroy this in case we
2349 // ever come up with a way of getting allocations back.
2350 getASTContext().Deallocate(tempFunc);
2351 return ret;
2352}
2353
2355 GlobalDecl canonicalGd = gd.getCanonicalDecl();
2356
2357 // Some ABIs don't have constructor variants. Make sure that base and complete
2358 // constructors get mangled the same.
2359 if (const auto *cd = dyn_cast<CXXConstructorDecl>(canonicalGd.getDecl())) {
2360 if (!getTarget().getCXXABI().hasConstructorVariants()) {
2361 errorNYI(cd->getSourceRange(),
2362 "getMangledName: C++ constructor without variants");
2363 return cast<NamedDecl>(gd.getDecl())->getIdentifier()->getName();
2364 }
2365 }
2366
2367 // Keep the first result in the case of a mangling collision.
2368 const auto *nd = cast<NamedDecl>(gd.getDecl());
2369 std::string mangledName = getMangledNameImpl(*this, gd, nd);
2370
2371 auto result = manglings.insert(std::make_pair(mangledName, gd));
2372 return mangledDeclNames[canonicalGd] = result.first->first();
2373}
2374
2376 assert(!d->getInit() && "Cannot emit definite definitions here!");
2377
2378 StringRef mangledName = getMangledName(d);
2379 mlir::Operation *gv = getGlobalValue(mangledName);
2380
2381 // If we already have a definition, not declaration, with the same mangled
2382 // name, emitting of declaration is not required (and would actually overwrite
2383 // the emitted definition).
2384 if (gv && !mlir::cast<cir::GlobalOp>(gv).isDeclaration())
2385 return;
2386
2387 // If we have not seen a reference to this variable yet, place it into the
2388 // deferred declarations table to be emitted if needed later.
2389 if (!mustBeEmitted(d) && !gv) {
2390 deferredDecls[mangledName] = d;
2391 return;
2392 }
2393
2394 // The tentative definition is the only definition.
2396}
2397
2399 // Never defer when EmitAllDecls is specified.
2400 if (langOpts.EmitAllDecls)
2401 return true;
2402
2403 const auto *vd = dyn_cast<VarDecl>(global);
2404 if (vd &&
2405 ((codeGenOpts.KeepPersistentStorageVariables &&
2406 (vd->getStorageDuration() == SD_Static ||
2407 vd->getStorageDuration() == SD_Thread)) ||
2408 (codeGenOpts.KeepStaticConsts && vd->getStorageDuration() == SD_Static &&
2409 vd->getType().isConstQualified())))
2410 return true;
2411
2412 return getASTContext().DeclMustBeEmitted(global);
2413}
2414
2416 // In OpenMP 5.0 variables and function may be marked as
2417 // device_type(host/nohost) and we should not emit them eagerly unless we sure
2418 // that they must be emitted on the host/device. To be sure we need to have
2419 // seen a declare target with an explicit mentioning of the function, we know
2420 // we have if the level of the declare target attribute is -1. Note that we
2421 // check somewhere else if we should emit this at all.
2422 if (langOpts.OpenMP >= 50 && !langOpts.OpenMPSimd) {
2423 std::optional<OMPDeclareTargetDeclAttr *> activeAttr =
2424 OMPDeclareTargetDeclAttr::getActiveAttr(global);
2425 if (!activeAttr || (*activeAttr)->getLevel() != (unsigned)-1)
2426 return false;
2427 }
2428
2429 const auto *fd = dyn_cast<FunctionDecl>(global);
2430 if (fd) {
2431 // Implicit template instantiations may change linkage if they are later
2432 // explicitly instantiated, so they should not be emitted eagerly.
2433 if (fd->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
2434 return false;
2435 // Defer until all versions have been semantically checked.
2436 if (fd->hasAttr<TargetVersionAttr>() && !fd->isMultiVersion())
2437 return false;
2438 if (langOpts.SYCLIsDevice) {
2439 errorNYI(fd->getSourceRange(), "mayBeEmittedEagerly: SYCL");
2440 return false;
2441 }
2442 }
2443 const auto *vd = dyn_cast<VarDecl>(global);
2444 if (vd)
2445 if (astContext.getInlineVariableDefinitionKind(vd) ==
2447 // A definition of an inline constexpr static data member may change
2448 // linkage later if it's redeclared outside the class.
2449 return false;
2450
2451 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
2452 // codegen for global variables, because they may be marked as threadprivate.
2453 if (langOpts.OpenMP && langOpts.OpenMPUseTLS &&
2454 astContext.getTargetInfo().isTLSSupported() && isa<VarDecl>(global) &&
2455 !global->getType().isConstantStorage(astContext, false, false) &&
2456 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(global))
2457 return false;
2458
2459 assert((fd || vd) &&
2460 "Only FunctionDecl and VarDecl should hit this path so far.");
2461 return true;
2462}
2463
2464static bool shouldAssumeDSOLocal(const CIRGenModule &cgm,
2465 cir::CIRGlobalValueInterface gv) {
2466 if (gv.hasLocalLinkage())
2467 return true;
2468
2469 if (!gv.hasDefaultVisibility() && !gv.hasExternalWeakLinkage())
2470 return true;
2471
2472 // DLLImport explicitly marks the GV as external.
2473 // so it shouldn't be dso_local
2474 // But we don't have the info set now
2476
2477 const llvm::Triple &tt = cgm.getTriple();
2478 const CodeGenOptions &cgOpts = cgm.getCodeGenOpts();
2479 if (tt.isOSCygMing()) {
2480 // In MinGW and Cygwin, variables without DLLImport can still be
2481 // automatically imported from a DLL by the linker; don't mark variables
2482 // that potentially could come from another DLL as DSO local.
2483
2484 // With EmulatedTLS, TLS variables can be autoimported from other DLLs
2485 // (and this actually happens in the public interface of libstdc++), so
2486 // such variables can't be marked as DSO local. (Native TLS variables
2487 // can't be dllimported at all, though.)
2488 cgm.errorNYI("shouldAssumeDSOLocal: MinGW");
2489 }
2490
2491 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
2492 // remain unresolved in the link, they can be resolved to zero, which is
2493 // outside the current DSO.
2494 if (tt.isOSBinFormatCOFF() && gv.hasExternalWeakLinkage())
2495 return false;
2496
2497 // Every other GV is local on COFF.
2498 // Make an exception for windows OS in the triple: Some firmware builds use
2499 // *-win32-macho triples. This (accidentally?) produced windows relocations
2500 // without GOT tables in older clang versions; Keep this behaviour.
2501 // FIXME: even thread local variables?
2502 if (tt.isOSBinFormatCOFF() || (tt.isOSWindows() && tt.isOSBinFormatMachO()))
2503 return true;
2504
2505 // Only handle COFF and ELF for now.
2506 if (!tt.isOSBinFormatELF())
2507 return false;
2508
2509 llvm::Reloc::Model rm = cgOpts.RelocationModel;
2510 const LangOptions &lOpts = cgm.getLangOpts();
2511 if (rm != llvm::Reloc::Static && !lOpts.PIE) {
2512 // On ELF, if -fno-semantic-interposition is specified and the target
2513 // supports local aliases, there will be neither CC1
2514 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
2515 // dso_local on the function if using a local alias is preferable (can avoid
2516 // PLT indirection).
2517 if (!(isa<cir::FuncOp>(gv) && gv.canBenefitFromLocalAlias()))
2518 return false;
2519 return !(lOpts.SemanticInterposition || lOpts.HalfNoSemanticInterposition);
2520 }
2521
2522 // A definition cannot be preempted from an executable.
2523 if (!gv.isDeclarationForLinker())
2524 return true;
2525
2526 // Most PIC code sequences that assume that a symbol is local cannot produce a
2527 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
2528 // depended, it seems worth it to handle it here.
2529 if (rm == llvm::Reloc::PIC_ && gv.hasExternalWeakLinkage())
2530 return false;
2531
2532 // PowerPC64 prefers TOC indirection to avoid copy relocations.
2533 if (tt.isPPC64())
2534 return false;
2535
2536 if (cgOpts.DirectAccessExternalData) {
2537 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
2538 // for non-thread-local variables. If the symbol is not defined in the
2539 // executable, a copy relocation will be needed at link time. dso_local is
2540 // excluded for thread-local variables because they generally don't support
2541 // copy relocations.
2542 if (auto globalOp = dyn_cast<cir::GlobalOp>(gv.getOperation())) {
2543 // Assume variables are not thread-local until that support is added.
2545 return true;
2546 }
2547
2548 // -fno-pic sets dso_local on a function declaration to allow direct
2549 // accesses when taking its address (similar to a data symbol). If the
2550 // function is not defined in the executable, a canonical PLT entry will be
2551 // needed at link time. -fno-direct-access-external-data can avoid the
2552 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
2553 // it could just cause trouble without providing perceptible benefits.
2554 if (isa<cir::FuncOp>(gv) && !cgOpts.NoPLT && rm == llvm::Reloc::Static)
2555 return true;
2556 }
2557
2558 // If we can use copy relocations we can assume it is local.
2559
2560 // Otherwise don't assume it is local.
2561
2562 return false;
2563}
2564
2565void CIRGenModule::setGlobalVisibility(mlir::Operation *gv,
2566 const NamedDecl *d) const {
2568}
2569
2570void CIRGenModule::setDSOLocal(cir::CIRGlobalValueInterface gv) const {
2571 gv.setDSOLocal(shouldAssumeDSOLocal(*this, gv));
2572}
2573
2574void CIRGenModule::setDSOLocal(mlir::Operation *op) const {
2575 if (auto globalValue = dyn_cast<cir::CIRGlobalValueInterface>(op))
2576 setDSOLocal(globalValue);
2577}
2578
2579void CIRGenModule::setGVProperties(mlir::Operation *op,
2580 const NamedDecl *d) const {
2582 setGVPropertiesAux(op, d);
2583}
2584
2585void CIRGenModule::setGVPropertiesAux(mlir::Operation *op,
2586 const NamedDecl *d) const {
2587 setGlobalVisibility(op, d);
2588 setDSOLocal(op);
2590}
2591
2593 GlobalDecl &result) const {
2594 auto res = manglings.find(mangledName);
2595 if (res == manglings.end())
2596 return false;
2597 result = res->getValue();
2598 return true;
2599}
2600
2602 switch (getCodeGenOpts().getDefaultTLSModel()) {
2604 return cir::TLS_Model::GeneralDynamic;
2606 return cir::TLS_Model::LocalDynamic;
2608 return cir::TLS_Model::InitialExec;
2610 return cir::TLS_Model::LocalExec;
2611 }
2612 llvm_unreachable("Invalid TLS model!");
2613}
2614
2615void CIRGenModule::setTLSMode(mlir::Operation *op, const VarDecl &d) {
2616 assert(d.getTLSKind() && "setting TLS mode on non-TLS var!");
2617
2618 cir::TLS_Model tlm = getDefaultCIRTLSModel();
2619
2620 // Override the TLS model if it is explicitly specified.
2621 if (d.getAttr<TLSModelAttr>())
2622 errorNYI(d.getSourceRange(), "TLS model attribute");
2623
2624 auto global = cast<cir::GlobalOp>(op);
2625 global.setTlsModel(tlm);
2626}
2627
2629 const CIRGenFunctionInfo &info,
2630 cir::FuncOp func, bool isThunk) {
2631 // TODO(cir): More logic of constructAttributeList is needed.
2632 cir::CallingConv callingConv;
2633 cir::SideEffect sideEffect;
2634
2635 // TODO(cir): The current list should be initialized with the extra function
2636 // attributes, but we don't have those yet. For now, the PAL is initialized
2637 // with nothing.
2639 // Initialize PAL with existing attributes to merge attributes.
2640 mlir::NamedAttrList pal{};
2641 std::vector<mlir::NamedAttrList> argAttrs(info.arguments().size());
2642 mlir::NamedAttrList retAttrs{};
2643 constructAttributeList(func.getName(), info, globalDecl, pal, argAttrs,
2644 retAttrs, callingConv, sideEffect,
2645 /*attrOnCallSite=*/false, isThunk);
2646
2647 for (mlir::NamedAttribute attr : pal)
2648 func->setAttr(attr.getName(), attr.getValue());
2649
2650 llvm::for_each(llvm::enumerate(argAttrs), [func](auto idx_arg_pair) {
2651 mlir::function_interface_impl::setArgAttrs(func, idx_arg_pair.index(),
2652 idx_arg_pair.value());
2653 });
2654 if (!retAttrs.empty())
2655 mlir::function_interface_impl::setResultAttrs(func, 0, retAttrs);
2656
2657 // TODO(cir): Check X86_VectorCall incompatibility wiht WinARM64EC
2658
2659 // TODO(cir): typically the calling conv is set right here, but since
2660 // cir::CallingConv is empty and we've not yet added calling-conv to FuncOop,
2661 // this isn't really useful here. This should call func.setCallingConv/etc
2662 // later.
2664}
2665
2667 cir::FuncOp func,
2668 bool isIncompleteFunction,
2669 bool isThunk) {
2670 // NOTE(cir): Original CodeGen checks if this is an intrinsic. In CIR we
2671 // represent them in dedicated ops. The correct attributes are ensured during
2672 // translation to LLVM. Thus, we don't need to check for them here.
2673
2674 const auto *funcDecl = cast<FunctionDecl>(globalDecl.getDecl());
2675
2676 if (!isIncompleteFunction)
2677 setCIRFunctionAttributes(globalDecl,
2678 getTypes().arrangeGlobalDeclaration(globalDecl),
2679 func, isThunk);
2680
2681 if (!isIncompleteFunction && func.isDeclaration())
2682 getTargetCIRGenInfo().setTargetAttributes(funcDecl, func, *this);
2683
2684 // TODO(cir): This needs a lot of work to better match CodeGen. That
2685 // ultimately ends up in setGlobalVisibility, which already has the linkage of
2686 // the LLVM GV (corresponding to our FuncOp) computed, so it doesn't have to
2687 // recompute it here. This is a minimal fix for now.
2688 if (!isLocalLinkage(getFunctionLinkage(globalDecl))) {
2689 const Decl *decl = globalDecl.getDecl();
2690 func.setGlobalVisibility(getGlobalVisibilityAttrFromDecl(decl).getValue());
2691 }
2692
2693 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2694 if (funcDecl->isInlineBuiltinDeclaration()) {
2695 const FunctionDecl *fdBody;
2696 bool hasBody = funcDecl->hasBody(fdBody);
2697 (void)hasBody;
2698 assert(hasBody && "Inline builtin declarations should always have an "
2699 "available body!");
2701 }
2702
2703 if (funcDecl->isReplaceableGlobalAllocationFunction()) {
2704 // A replaceable global allocation function does not act like a builtin by
2705 // default, only if it is invoked by a new-expression or delete-expression.
2706 func->setAttr(cir::CIRDialect::getNoBuiltinAttrName(),
2707 mlir::UnitAttr::get(&getMLIRContext()));
2708 }
2709}
2710
2711/// Determines whether the language options require us to model
2712/// unwind exceptions. We treat -fexceptions as mandating this
2713/// except under the fragile ObjC ABI with only ObjC exceptions
2714/// enabled. This means, for example, that C with -fexceptions
2715/// enables this.
2716static bool hasUnwindExceptions(const LangOptions &langOpts) {
2717 // If exceptions are completely disabled, obviously this is false.
2718 if (!langOpts.Exceptions)
2719 return false;
2720 // If C++ exceptions are enabled, this is true.
2721 if (langOpts.CXXExceptions)
2722 return true;
2723 // If ObjC exceptions are enabled, this depends on the ABI.
2724 if (langOpts.ObjCExceptions)
2725 return langOpts.ObjCRuntime.hasUnwindExceptions();
2726 return true;
2727}
2728
2730 const clang::FunctionDecl *decl, cir::FuncOp f) {
2733
2734 if (!hasUnwindExceptions(langOpts))
2735 f->setAttr(cir::CIRDialect::getNoThrowAttrName(),
2736 mlir::UnitAttr::get(&getMLIRContext()));
2737
2738 std::optional<cir::InlineKind> existingInlineKind = f.getInlineKind();
2739 bool isNoInline =
2740 existingInlineKind && *existingInlineKind == cir::InlineKind::NoInline;
2741 bool isAlwaysInline = existingInlineKind &&
2742 *existingInlineKind == cir::InlineKind::AlwaysInline;
2743 if (!decl) {
2744 assert(!cir::MissingFeatures::hlsl());
2745
2746 if (!isAlwaysInline &&
2747 codeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2748 // If inlining is disabled and we don't have a declaration to control
2749 // inlining, mark the function as 'noinline' unless it is explicitly
2750 // marked as 'alwaysinline'.
2751 f.setInlineKind(cir::InlineKind::NoInline);
2752 }
2753
2754 return;
2755 }
2756
2763 assert(!cir::MissingFeatures::hlsl());
2764
2765 // Handle inline attributes
2766 if (decl->hasAttr<NoInlineAttr>() && !isAlwaysInline) {
2767 // Add noinline if the function isn't always_inline.
2768 f.setInlineKind(cir::InlineKind::NoInline);
2769 } else if (decl->hasAttr<AlwaysInlineAttr>() && !isNoInline) {
2770 // Don't override AlwaysInline with NoInline, or vice versa, since we can't
2771 // specify both in IR.
2772 f.setInlineKind(cir::InlineKind::AlwaysInline);
2773 } else if (codeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2774 // If inlining is disabled, force everything that isn't always_inline
2775 // to carry an explicit noinline attribute.
2776 if (!isAlwaysInline)
2777 f.setInlineKind(cir::InlineKind::NoInline);
2778 } else {
2779 // Otherwise, propagate the inline hint attribute and potentially use its
2780 // absence to mark things as noinline.
2781 // Search function and template pattern redeclarations for inline.
2782 if (auto *fd = dyn_cast<FunctionDecl>(decl)) {
2783 // TODO: Share this checkForInline implementation with classic codegen.
2784 // This logic is likely to change over time, so sharing would help ensure
2785 // consistency.
2786 auto checkForInline = [](const FunctionDecl *decl) {
2787 auto checkRedeclForInline = [](const FunctionDecl *redecl) {
2788 return redecl->isInlineSpecified();
2789 };
2790 if (any_of(decl->redecls(), checkRedeclForInline))
2791 return true;
2792 const FunctionDecl *pattern = decl->getTemplateInstantiationPattern();
2793 if (!pattern)
2794 return false;
2795 return any_of(pattern->redecls(), checkRedeclForInline);
2796 };
2797 if (checkForInline(fd)) {
2798 f.setInlineKind(cir::InlineKind::InlineHint);
2799 } else if (codeGenOpts.getInlining() ==
2801 !fd->isInlined() && !isAlwaysInline) {
2802 f.setInlineKind(cir::InlineKind::NoInline);
2803 }
2804 }
2805 }
2806
2808}
2809
2811 StringRef mangledName, mlir::Type funcType, GlobalDecl gd, bool forVTable,
2812 bool dontDefer, bool isThunk, ForDefinition_t isForDefinition,
2813 mlir::NamedAttrList extraAttrs) {
2814 const Decl *d = gd.getDecl();
2815
2816 if (const auto *fd = cast_or_null<FunctionDecl>(d)) {
2817 // For the device mark the function as one that should be emitted.
2818 if (getLangOpts().OpenMPIsTargetDevice && fd->isDefined() && !dontDefer &&
2819 !isForDefinition)
2820 errorNYI(fd->getSourceRange(),
2821 "getOrCreateCIRFunction: OpenMP target function");
2822
2823 // Any attempts to use a MultiVersion function should result in retrieving
2824 // the iFunc instead. Name mangling will handle the rest of the changes.
2825 if (fd->isMultiVersion())
2826 errorNYI(fd->getSourceRange(), "getOrCreateCIRFunction: multi-version");
2827 }
2828
2829 // Lookup the entry, lazily creating it if necessary.
2830 mlir::Operation *entry = getGlobalValue(mangledName);
2831 if (entry) {
2832 assert(mlir::isa<cir::FuncOp>(entry));
2833
2835
2836 // Handle dropped DLL attributes.
2837 if (d && !d->hasAttr<DLLImportAttr>() && !d->hasAttr<DLLExportAttr>()) {
2839 setDSOLocal(entry);
2840 }
2841
2842 // If there are two attempts to define the same mangled name, issue an
2843 // error.
2844 auto fn = cast<cir::FuncOp>(entry);
2845 if (isForDefinition && fn && !fn.isDeclaration()) {
2846 GlobalDecl otherGd;
2847 // Check that GD is not yet in DiagnosedConflictingDefinitions is required
2848 // to make sure that we issue an error only once.
2849 if (lookupRepresentativeDecl(mangledName, otherGd) &&
2850 (gd.getCanonicalDecl().getDecl() !=
2851 otherGd.getCanonicalDecl().getDecl()) &&
2852 diagnosedConflictingDefinitions.insert(gd).second) {
2853 getDiags().Report(d->getLocation(), diag::err_duplicate_mangled_name)
2854 << mangledName;
2855 getDiags().Report(otherGd.getDecl()->getLocation(),
2856 diag::note_previous_definition);
2857 }
2858 }
2859
2860 if (fn && fn.getFunctionType() == funcType) {
2861 return fn;
2862 }
2863
2864 if (!isForDefinition) {
2865 return fn;
2866 }
2867
2868 // TODO(cir): classic codegen checks here if this is a llvm::GlobalAlias.
2869 // How will we support this?
2870 }
2871
2872 auto *funcDecl = llvm::cast_or_null<FunctionDecl>(gd.getDecl());
2873 bool invalidLoc = !funcDecl ||
2874 funcDecl->getSourceRange().getBegin().isInvalid() ||
2875 funcDecl->getSourceRange().getEnd().isInvalid();
2876 cir::FuncOp funcOp = createCIRFunction(
2877 invalidLoc ? theModule->getLoc() : getLoc(funcDecl->getSourceRange()),
2878 mangledName, mlir::cast<cir::FuncType>(funcType), funcDecl);
2879
2880 // If we already created a function with the same mangled name (but different
2881 // type) before, take its name and add it to the list of functions to be
2882 // replaced with F at the end of CodeGen.
2883 //
2884 // This happens if there is a prototype for a function (e.g. "int f()") and
2885 // then a definition of a different type (e.g. "int f(int x)").
2886 if (entry) {
2887
2888 // Fetch a generic symbol-defining operation and its uses.
2889 auto symbolOp = mlir::cast<mlir::SymbolOpInterface>(entry);
2890
2891 // This might be an implementation of a function without a prototype, in
2892 // which case, try to do special replacement of calls which match the new
2893 // prototype. The really key thing here is that we also potentially drop
2894 // arguments from the call site so as to make a direct call, which makes the
2895 // inliner happier and suppresses a number of optimizer warnings (!) about
2896 // dropping arguments.
2897 if (symbolOp.getSymbolUses(symbolOp->getParentOp()))
2899
2900 // Obliterate no-proto declaration.
2901 entry->erase();
2902 }
2903
2904 if (d)
2905 setFunctionAttributes(gd, funcOp, /*isIncompleteFunction=*/false, isThunk);
2906 if (!extraAttrs.empty()) {
2907 extraAttrs.append(funcOp->getAttrs());
2908 funcOp->setAttrs(extraAttrs);
2909 }
2910
2911 // 'dontDefer' actually means don't move this to the deferredDeclsToEmit list.
2912 if (dontDefer) {
2913 // TODO(cir): This assertion will need an additional condition when we
2914 // support incomplete functions.
2915 assert(funcOp.getFunctionType() == funcType);
2916 return funcOp;
2917 }
2918
2919 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
2920 // each other bottoming out wiht the base dtor. Therefore we emit non-base
2921 // dtors on usage, even if there is no dtor definition in the TU.
2922 if (isa_and_nonnull<CXXDestructorDecl>(d) &&
2923 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(d),
2924 gd.getDtorType()))
2925 errorNYI(d->getSourceRange(), "getOrCreateCIRFunction: dtor");
2926
2927 // This is the first use or definition of a mangled name. If there is a
2928 // deferred decl with this name, remember that we need to emit it at the end
2929 // of the file.
2930 auto ddi = deferredDecls.find(mangledName);
2931 if (ddi != deferredDecls.end()) {
2932 // Move the potentially referenced deferred decl to the
2933 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
2934 // don't need it anymore).
2935 addDeferredDeclToEmit(ddi->second);
2936 deferredDecls.erase(ddi);
2937
2938 // Otherwise, there are cases we have to worry about where we're using a
2939 // declaration for which we must emit a definition but where we might not
2940 // find a top-level definition.
2941 // - member functions defined inline in their classes
2942 // - friend functions defined inline in some class
2943 // - special member functions with implicit definitions
2944 // If we ever change our AST traversal to walk into class methods, this
2945 // will be unnecessary.
2946 //
2947 // We also don't emit a definition for a function if it's going to be an
2948 // entry in a vtable, unless it's already marked as used.
2949 } else if (getLangOpts().CPlusPlus && d) {
2950 // Look for a declaration that's lexically in a record.
2951 for (const auto *fd = cast<FunctionDecl>(d)->getMostRecentDecl(); fd;
2952 fd = fd->getPreviousDecl()) {
2953 if (isa<CXXRecordDecl>(fd->getLexicalDeclContext())) {
2954 if (fd->doesThisDeclarationHaveABody()) {
2956 break;
2957 }
2958 }
2959 }
2960 }
2961
2962 return funcOp;
2963}
2964
2965cir::FuncOp
2966CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
2967 cir::FuncType funcType,
2968 const clang::FunctionDecl *funcDecl) {
2969 cir::FuncOp func;
2970 {
2971 mlir::OpBuilder::InsertionGuard guard(builder);
2972
2973 // Some global emissions are triggered while emitting a function, e.g.
2974 // void s() { x.method() }
2975 //
2976 // Be sure to insert a new function before a current one.
2977 CIRGenFunction *cgf = this->curCGF;
2978 if (cgf)
2979 builder.setInsertionPoint(cgf->curFn);
2980
2981 func = cir::FuncOp::create(builder, loc, name, funcType);
2982
2984
2985 if (funcDecl && !funcDecl->hasPrototype())
2986 func.setNoProto(true);
2987
2988 assert(func.isDeclaration() && "expected empty body");
2989
2990 // A declaration gets private visibility by default, but external linkage
2991 // as the default linkage.
2992 func.setLinkageAttr(cir::GlobalLinkageKindAttr::get(
2993 &getMLIRContext(), cir::GlobalLinkageKind::ExternalLinkage));
2994 mlir::SymbolTable::setSymbolVisibility(
2995 func, mlir::SymbolTable::Visibility::Private);
2996
2998
2999 // Mark C++ special member functions (Constructor, Destructor etc.)
3000 setCXXSpecialMemberAttr(func, funcDecl);
3001
3002 if (!cgf)
3003 theModule.push_back(func);
3004
3005 if (this->getLangOpts().OpenACC) {
3006 // We only have to handle this attribute, since OpenACCAnnotAttrs are
3007 // handled via the end-of-TU work.
3008 for (const auto *attr :
3009 funcDecl->specific_attrs<OpenACCRoutineDeclAttr>())
3010 emitOpenACCRoutineDecl(funcDecl, func, attr->getLocation(),
3011 attr->Clauses);
3012 }
3013 }
3014 return func;
3015}
3016
3017cir::FuncOp
3018CIRGenModule::createCIRBuiltinFunction(mlir::Location loc, StringRef name,
3019 cir::FuncType ty,
3020 const clang::FunctionDecl *fd) {
3021 cir::FuncOp fnOp = createCIRFunction(loc, name, ty, fd);
3022 fnOp.setBuiltin(true);
3023 return fnOp;
3024}
3025
3026static cir::CtorKind getCtorKindFromDecl(const CXXConstructorDecl *ctor) {
3027 if (ctor->isDefaultConstructor())
3028 return cir::CtorKind::Default;
3029 if (ctor->isCopyConstructor())
3030 return cir::CtorKind::Copy;
3031 if (ctor->isMoveConstructor())
3032 return cir::CtorKind::Move;
3033 return cir::CtorKind::Custom;
3034}
3035
3036static cir::AssignKind getAssignKindFromDecl(const CXXMethodDecl *method) {
3037 if (method->isCopyAssignmentOperator())
3038 return cir::AssignKind::Copy;
3039 if (method->isMoveAssignmentOperator())
3040 return cir::AssignKind::Move;
3041 llvm_unreachable("not a copy or move assignment operator");
3042}
3043
3045 cir::FuncOp funcOp, const clang::FunctionDecl *funcDecl) {
3046 if (!funcDecl)
3047 return;
3048
3049 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(funcDecl)) {
3050 auto cxxDtor = cir::CXXDtorAttr::get(
3051 convertType(getASTContext().getCanonicalTagType(dtor->getParent())),
3052 dtor->isTrivial());
3053 funcOp.setCxxSpecialMemberAttr(cxxDtor);
3054 return;
3055 }
3056
3057 if (const auto *ctor = dyn_cast<CXXConstructorDecl>(funcDecl)) {
3058 cir::CtorKind kind = getCtorKindFromDecl(ctor);
3059 auto cxxCtor = cir::CXXCtorAttr::get(
3060 convertType(getASTContext().getCanonicalTagType(ctor->getParent())),
3061 kind, ctor->isTrivial());
3062 funcOp.setCxxSpecialMemberAttr(cxxCtor);
3063 return;
3064 }
3065
3066 const auto *method = dyn_cast<CXXMethodDecl>(funcDecl);
3067 if (method && (method->isCopyAssignmentOperator() ||
3068 method->isMoveAssignmentOperator())) {
3069 cir::AssignKind assignKind = getAssignKindFromDecl(method);
3070 auto cxxAssign = cir::CXXAssignAttr::get(
3071 convertType(getASTContext().getCanonicalTagType(method->getParent())),
3072 assignKind, method->isTrivial());
3073 funcOp.setCxxSpecialMemberAttr(cxxAssign);
3074 return;
3075 }
3076}
3077
3078static void setWindowsItaniumDLLImport(CIRGenModule &cgm, bool isLocal,
3079 cir::FuncOp funcOp, StringRef name) {
3080 // In Windows Itanium environments, try to mark runtime functions
3081 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
3082 // will link their standard library statically or dynamically. Marking
3083 // functions imported when they are not imported can cause linker errors
3084 // and warnings.
3085 if (!isLocal && cgm.getTarget().getTriple().isWindowsItaniumEnvironment() &&
3086 !cgm.getCodeGenOpts().LTOVisibilityPublicStd) {
3090 }
3091}
3092
3093cir::FuncOp CIRGenModule::createRuntimeFunction(cir::FuncType ty,
3094 StringRef name,
3095 mlir::NamedAttrList extraAttrs,
3096 bool isLocal,
3097 bool assumeConvergent) {
3098 if (assumeConvergent)
3099 errorNYI("createRuntimeFunction: assumeConvergent");
3100
3101 cir::FuncOp entry = getOrCreateCIRFunction(name, ty, GlobalDecl(),
3102 /*forVtable=*/false, extraAttrs);
3103
3104 if (entry) {
3105 // TODO(cir): set the attributes of the function.
3108 setWindowsItaniumDLLImport(*this, isLocal, entry, name);
3109 entry.setDSOLocal(true);
3110 }
3111
3112 return entry;
3113}
3114
3115mlir::SymbolTable::Visibility
3117 // MLIR doesn't accept public symbols declarations (only
3118 // definitions).
3119 if (op.isDeclaration())
3120 return mlir::SymbolTable::Visibility::Private;
3121 return getMLIRVisibilityFromCIRLinkage(op.getLinkage());
3122}
3123
3124mlir::SymbolTable::Visibility
3126 switch (glk) {
3127 case cir::GlobalLinkageKind::InternalLinkage:
3128 case cir::GlobalLinkageKind::PrivateLinkage:
3129 return mlir::SymbolTable::Visibility::Private;
3130 case cir::GlobalLinkageKind::ExternalLinkage:
3131 case cir::GlobalLinkageKind::ExternalWeakLinkage:
3132 case cir::GlobalLinkageKind::LinkOnceODRLinkage:
3133 case cir::GlobalLinkageKind::AvailableExternallyLinkage:
3134 case cir::GlobalLinkageKind::CommonLinkage:
3135 case cir::GlobalLinkageKind::WeakAnyLinkage:
3136 case cir::GlobalLinkageKind::WeakODRLinkage:
3137 return mlir::SymbolTable::Visibility::Public;
3138 default: {
3139 llvm::errs() << "visibility not implemented for '"
3140 << stringifyGlobalLinkageKind(glk) << "'\n";
3141 assert(0 && "not implemented");
3142 }
3143 }
3144 llvm_unreachable("linkage should be handled above!");
3145}
3146
3148 clang::VisibilityAttr::VisibilityType visibility) {
3149 switch (visibility) {
3150 case clang::VisibilityAttr::VisibilityType::Default:
3151 return cir::VisibilityKind::Default;
3152 case clang::VisibilityAttr::VisibilityType::Hidden:
3153 return cir::VisibilityKind::Hidden;
3154 case clang::VisibilityAttr::VisibilityType::Protected:
3155 return cir::VisibilityKind::Protected;
3156 }
3157 llvm_unreachable("unexpected visibility value");
3158}
3159
3160cir::VisibilityAttr
3162 const clang::VisibilityAttr *va = decl->getAttr<clang::VisibilityAttr>();
3163 cir::VisibilityAttr cirVisibility =
3164 cir::VisibilityAttr::get(&getMLIRContext());
3165 if (va) {
3166 cirVisibility = cir::VisibilityAttr::get(
3167 &getMLIRContext(),
3168 getGlobalVisibilityKindFromClangVisibility(va->getVisibility()));
3169 }
3170 return cirVisibility;
3171}
3172
3174 emitDeferred();
3176 applyReplacements();
3177
3178 theModule->setAttr(cir::CIRDialect::getModuleLevelAsmAttrName(),
3179 builder.getArrayAttr(globalScopeAsm));
3180
3181 if (!recordLayoutEntries.empty())
3182 theModule->setAttr(
3183 cir::CIRDialect::getRecordLayoutsAttrName(),
3184 mlir::DictionaryAttr::get(&getMLIRContext(), recordLayoutEntries));
3185
3186 if (getTriple().isAMDGPU() ||
3187 (getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD))
3189
3190 if (getLangOpts().HIP) {
3191 // Emit a unique ID so that host and device binaries from the same
3192 // compilation unit can be associated.
3193 std::string cuidName =
3194 ("__hip_cuid_" + getASTContext().getCUIDHash()).str();
3195 auto int8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
3196 auto loc = builder.getUnknownLoc();
3197 mlir::ptr::MemorySpaceAttrInterface addrSpace =
3198 cir::LangAddressSpaceAttr::get(&getMLIRContext(),
3199 getGlobalVarAddressSpace(nullptr));
3200
3201 auto gv = createGlobalOp(*this, loc, cuidName, int8Ty,
3202 /*isConstant=*/false, addrSpace);
3203 gv.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
3204 // Initialize with zero
3205 auto zeroAttr = cir::IntAttr::get(int8Ty, 0);
3206 gv.setInitialValueAttr(zeroAttr);
3207 // External linkage requires public visibility
3208 mlir::SymbolTable::setSymbolVisibility(
3209 gv, mlir::SymbolTable::Visibility::Public);
3210
3212 }
3213
3214 emitLLVMUsed();
3215
3216 // There's a lot of code that is not implemented yet.
3218}
3219
3220void CIRGenModule::emitAliasForGlobal(StringRef mangledName,
3221 mlir::Operation *op, GlobalDecl aliasGD,
3222 cir::FuncOp aliasee,
3223 cir::GlobalLinkageKind linkage) {
3224
3225 auto *aliasFD = dyn_cast<FunctionDecl>(aliasGD.getDecl());
3226 assert(aliasFD && "expected FunctionDecl");
3227
3228 // The aliasee function type is different from the alias one, this difference
3229 // is specific to CIR because in LLVM the ptr types are already erased at this
3230 // point.
3231 const CIRGenFunctionInfo &fnInfo =
3233 cir::FuncType fnType = getTypes().getFunctionType(fnInfo);
3234
3235 cir::FuncOp alias =
3237 mangledName, fnType, aliasFD);
3238 alias.setAliasee(aliasee.getName());
3239 alias.setLinkage(linkage);
3240 // Declarations cannot have public MLIR visibility, just mark them private
3241 // but this really should have no meaning since CIR should not be using
3242 // this information to derive linkage information.
3243 mlir::SymbolTable::setSymbolVisibility(
3244 alias, mlir::SymbolTable::Visibility::Private);
3245
3246 // Alias constructors and destructors are always unnamed_addr.
3248
3249 if (op) {
3250 // Any existing users of the existing function declaration will be
3251 // referencing the function by flat symbol reference (i.e. the name), so
3252 // those uses will automatically resolve to the alias now that we've
3253 // replaced the function declaration. We can safely erase the existing
3254 // function declaration.
3255 assert(cast<cir::FuncOp>(op).getFunctionType() == alias.getFunctionType() &&
3256 "declaration exists with different type");
3257 op->erase();
3258 } else {
3259 // Name already set by createCIRFunction
3260 }
3261
3262 // Finally, set up the alias with its proper name and attributes.
3263 setCommonAttributes(aliasGD, alias);
3264}
3265
3267 return genTypes.convertType(type);
3268}
3269
3271 // Verify the module after we have finished constructing it, this will
3272 // check the structural properties of the IR and invoke any specific
3273 // verifiers we have on the CIR operations.
3274 return mlir::verify(theModule).succeeded();
3275}
3276
3277mlir::Attribute CIRGenModule::getAddrOfRTTIDescriptor(mlir::Location loc,
3278 QualType ty, bool forEh) {
3279 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
3280 // FIXME: should we even be calling this method if RTTI is disabled
3281 // and it's not for EH?
3282 if (!shouldEmitRTTI(forEh))
3283 return builder.getConstNullPtrAttr(builder.getUInt8PtrTy());
3284
3285 if (forEh && ty->isObjCObjectPointerType() &&
3286 langOpts.ObjCRuntime.isGNUFamily()) {
3287 errorNYI(loc, "getAddrOfRTTIDescriptor: Objc PtrType & Objc RT GUN");
3288 return {};
3289 }
3290
3291 return getCXXABI().getAddrOfRTTIDescriptor(loc, ty);
3292}
3293
3294// TODO(cir): this can be shared with LLVM codegen.
3296 const CXXRecordDecl *derivedClass,
3297 llvm::iterator_range<CastExpr::path_const_iterator> path) {
3298 CharUnits offset = CharUnits::Zero();
3299
3300 const ASTContext &astContext = getASTContext();
3301 const CXXRecordDecl *rd = derivedClass;
3302
3303 for (const CXXBaseSpecifier *base : path) {
3304 assert(!base->isVirtual() && "Should not see virtual bases here!");
3305
3306 // Get the layout.
3307 const ASTRecordLayout &layout = astContext.getASTRecordLayout(rd);
3308
3309 const auto *baseDecl = base->getType()->castAsCXXRecordDecl();
3310
3311 // Add the offset.
3312 offset += layout.getBaseClassOffset(baseDecl);
3313
3314 rd = baseDecl;
3315 }
3316
3317 return offset;
3318}
3319
3321 llvm::StringRef feature) {
3322 unsigned diagID = diags.getCustomDiagID(
3323 DiagnosticsEngine::Error, "ClangIR code gen Not Yet Implemented: %0");
3324 return diags.Report(loc, diagID) << feature;
3325}
3326
3328 llvm::StringRef feature) {
3329 return errorNYI(loc.getBegin(), feature) << loc;
3330}
3331
3333 unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
3334 getDiags().Report(astContext.getFullLoc(loc), diagID) << error;
3335}
3336
3337/// Print out an error that codegen doesn't support the specified stmt yet.
3338void CIRGenModule::errorUnsupported(const Stmt *s, llvm::StringRef type) {
3339 unsigned diagId = diags.getCustomDiagID(DiagnosticsEngine::Error,
3340 "cannot compile this %0 yet");
3341 diags.Report(astContext.getFullLoc(s->getBeginLoc()), diagId)
3342 << type << s->getSourceRange();
3343}
3344
3345/// Print out an error that codegen doesn't support the specified decl yet.
3346void CIRGenModule::errorUnsupported(const Decl *d, llvm::StringRef type) {
3347 unsigned diagId = diags.getCustomDiagID(DiagnosticsEngine::Error,
3348 "cannot compile this %0 yet");
3349 diags.Report(astContext.getFullLoc(d->getLocation()), diagId) << type;
3350}
3351
3352void CIRGenModule::mapBlockAddress(cir::BlockAddrInfoAttr blockInfo,
3353 cir::LabelOp label) {
3354 [[maybe_unused]] auto result =
3355 blockAddressInfoToLabel.try_emplace(blockInfo, label);
3356 assert(result.second &&
3357 "attempting to map a blockaddress info that is already mapped");
3358}
3359
3360void CIRGenModule::mapUnresolvedBlockAddress(cir::BlockAddressOp op) {
3361 [[maybe_unused]] auto result = unresolvedBlockAddressToLabel.insert(op);
3362 assert(result.second &&
3363 "attempting to map a blockaddress operation that is already mapped");
3364}
3365
3366void CIRGenModule::mapResolvedBlockAddress(cir::BlockAddressOp op,
3367 cir::LabelOp label) {
3368 [[maybe_unused]] auto result = blockAddressToLabel.try_emplace(op, label);
3369 assert(result.second &&
3370 "attempting to map a blockaddress operation that is already mapped");
3371}
3372
3374 cir::LabelOp newLabel) {
3375 auto *it = blockAddressToLabel.find(op);
3376 assert(it != blockAddressToLabel.end() &&
3377 "trying to update a blockaddress not previously mapped");
3378 assert(!it->second && "blockaddress already has a resolved label");
3379
3380 it->second = newLabel;
3381}
3382
3383cir::LabelOp
3384CIRGenModule::lookupBlockAddressInfo(cir::BlockAddrInfoAttr blockInfo) {
3385 return blockAddressInfoToLabel.lookup(blockInfo);
3386}
3387
3388mlir::Operation *
3390 const Expr *init) {
3391 assert((mte->getStorageDuration() == SD_Static ||
3392 mte->getStorageDuration() == SD_Thread) &&
3393 "not a global temporary");
3394 const auto *varDecl = cast<VarDecl>(mte->getExtendingDecl());
3395
3396 // Use the MaterializeTemporaryExpr's type if it has the same unqualified
3397 // base type as Init. This preserves cv-qualifiers (e.g. const from a
3398 // constexpr or const-ref binding) that skipRValueSubobjectAdjustments may
3399 // have dropped via NoOp casts, while correctly falling back to Init's type
3400 // when a real subobject adjustment changed the type (e.g. member access or
3401 // base-class cast in C++98), where E->getType() reflects the reference type,
3402 // not the actual storage type.
3403 QualType materializedType = init->getType();
3404 if (getASTContext().hasSameUnqualifiedType(mte->getType(), materializedType))
3405 materializedType = mte->getType();
3406
3407 CharUnits align = getASTContext().getTypeAlignInChars(materializedType);
3408
3409 auto insertResult = materializedGlobalTemporaryMap.insert({mte, nullptr});
3410 if (!insertResult.second)
3411 errorNYI(mte->getSourceRange(), "duplicate materialized temporaries");
3412
3413 // FIXME: If an externally-visible declaration extends multiple temporaries,
3414 // we need to give each temporary the same name in every translation unit (and
3415 // we also need to make the temporaries externally-visible).
3417 llvm::raw_svector_ostream out(name);
3419 varDecl, mte->getManglingNumber(), out);
3420
3421 APValue *value = nullptr;
3422 if (mte->getStorageDuration() == SD_Static && varDecl->evaluateValue()) {
3423 // If the initializer of the extending declaration is a constant
3424 // initializer, we should have a cached constant initializer for this
3425 // temporay. Note taht this m ight have a different value from the value
3426 // computed by evaluating the initializer if the surrounding constant
3427 // expression modifies the temporary.
3428 value = mte->getOrCreateValue(/*MayCreate=*/false);
3429 }
3430
3431 // Try evaluating it now, it might have a constant initializer
3432 Expr::EvalResult evalResult;
3433 if (!value && init->EvaluateAsRValue(evalResult, getASTContext()) &&
3434 !evalResult.hasSideEffects())
3435 value = &evalResult.Val;
3436
3438
3439 std::optional<ConstantEmitter> emitter;
3440 mlir::Attribute initialValue = nullptr;
3441 bool isConstant = false;
3442 mlir::Type type;
3443
3444 if (value) {
3445 emitter.emplace(*this);
3446 initialValue = emitter->emitForInitializer(*value, materializedType);
3447
3448 isConstant = materializedType.isConstantStorage(
3449 getASTContext(), /*ExcludeCtor=*/value, /*ExcludeDtor=*/false);
3450
3451 type = mlir::cast<mlir::TypedAttr>(initialValue).getType();
3452 } else {
3453 // No initializer, the initialization will be provided when we initialize
3454 // the declaration which performed lifetime extension.
3455 type = getTypes().convertTypeForMem(materializedType);
3456 }
3457
3458 // Create a global variable for this lifetime-extended temporary.
3459 cir::GlobalLinkageKind linkage = getCIRLinkageVarDefinition(varDecl);
3460 if (linkage == cir::GlobalLinkageKind::ExternalLinkage) {
3461 const VarDecl *initVD;
3462 if (varDecl->isStaticDataMember() && varDecl->getAnyInitializer(initVD) &&
3464 // Temporaries defined inside a class get linkonce_odr linkage because the
3465 // calss can be defined in multiple translation units.
3466 errorNYI(mte->getSourceRange(), "static data member initialization");
3467 } else {
3468 // There is no need for this temporary to have external linkage if the
3469 // VarDecl has external linkage.
3470 linkage = cir::GlobalLinkageKind::InternalLinkage;
3471 }
3472 }
3473 mlir::Location loc = getLoc(mte->getSourceRange());
3474 cir::GlobalOp gv = createGlobalOp(*this, loc, name, type, isConstant);
3475 gv.setInitialValueAttr(initialValue);
3476
3477 if (emitter)
3478 emitter->finalize(gv);
3479 // Don't assign dllimport or dllexport to local linkage globals
3480 if (!gv.hasLocalLinkage()) {
3483 }
3484
3485 gv.setAlignment(align.getAsAlign().value());
3486 if (supportsCOMDAT() && gv.isWeakForLinker())
3487 errorNYI(mte->getSourceRange(),
3488 "Global temporary with comdat/weak linkage");
3489 if (varDecl->getTLSKind())
3490 errorNYI(mte->getSourceRange(),
3491 "Global temporary with thread local storage");
3492 mlir::Operation *cv = gv;
3493
3495
3496 // Update the map with the new temporary. If we created a placeholder above,
3497 // replace it with the new global now.
3498 mlir::Operation *&entry = materializedGlobalTemporaryMap[mte];
3499 if (entry) {
3500 entry->replaceAllUsesWith(cv);
3501 entry->erase();
3502 }
3503 entry = cv;
3504
3505 return cv;
3506}
Defines the clang::ASTContext interface.
This file provides some common utility functions for processing Lambda related AST Constructs.
static bool shouldAssumeDSOLocal(const CIRGenModule &cgm, cir::CIRGlobalValueInterface gv)
static cir::AssignKind getAssignKindFromDecl(const CXXMethodDecl *method)
static FunctionDecl * createOpenACCBindTempFunction(ASTContext &ctx, const IdentifierInfo *bindName, const FunctionDecl *protoFunc)
static bool shouldBeInCOMDAT(CIRGenModule &cgm, const Decl &d)
static mlir::Attribute getNewInitValue(CIRGenModule &cgm, cir::GlobalOp newGlob, mlir::Type oldTy, mlir::Attribute oldInit)
static bool hasUnwindExceptions(const LangOptions &langOpts)
Determines whether the language options require us to model unwind exceptions.
static void setWindowsItaniumDLLImport(CIRGenModule &cgm, bool isLocal, cir::FuncOp funcOp, StringRef name)
static std::string getMangledNameImpl(CIRGenModule &cgm, GlobalDecl gd, const NamedDecl *nd)
static llvm::SmallVector< int64_t > indexesOfArrayAttr(mlir::ArrayAttr indexes)
static bool isViewOnGlobal(cir::GlobalOp glob, cir::GlobalViewAttr view)
static cir::GlobalOp generateStringLiteral(mlir::Location loc, mlir::TypedAttr c, cir::GlobalLinkageKind lt, CIRGenModule &cgm, StringRef globalName, CharUnits alignment)
static bool hasImplicitAttr(const ValueDecl *decl)
static CIRGenCXXABI * createCXXABI(CIRGenModule &cgm)
static bool isVarDeclStrongDefinition(const ASTContext &astContext, CIRGenModule &cgm, const VarDecl *vd, bool noCommon)
static void setLinkageForGV(cir::GlobalOp &gv, const NamedDecl *nd)
static cir::CtorKind getCtorKindFromDecl(const CXXConstructorDecl *ctor)
static void emitUsed(CIRGenModule &cgm, StringRef name, std::vector< cir::CIRGlobalValueInterface > &list)
static bool verifyPointerTypeArgs(mlir::ModuleOp modOp, cir::FuncOp oldF, cir::FuncOp newF)
static cir::GlobalViewAttr createNewGlobalView(CIRGenModule &cgm, cir::GlobalOp newGlob, cir::GlobalViewAttr attr, mlir::Type oldTy)
This file defines OpenACC nodes for declarative directives.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
Defines the SourceManager interface.
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
cir::GlobalViewAttr getGlobalViewAttr(cir::GlobalOp globalOp, mlir::ArrayAttr indices={})
Get constant address of a global variable as an MLIR attribute.
cir::PointerType getPointerTo(mlir::Type ty)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
TranslationUnitDecl * getTranslationUnitDecl() const
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
@ WeakUnknown
Weak for now, might become strong later in this TU.
bool DeclMustBeEmitted(const Decl *D)
Determines if the decl can be CodeGen'ed or deserialized from PCH lazily, only when used; this is onl...
StringRef getCUIDHash() const
void Deallocate(void *Ptr) const
Definition ASTContext.h:878
GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD) const
bool isAlignmentRequired(const Type *T) const
Determine if the alignment the type has was required using an alignment attribute.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
GVALinkage GetGVALinkageForVariable(const VarDecl *VD) const
unsigned getTypeAlignIfKnown(QualType T, bool NeedsPreferredAlignment=false) const
Return the alignment of a type, in bits, or 0 if the type is incomplete and we cannot determine the a...
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:917
TargetCXXABI::Kind getCXXABIKind() const
Return the C++ ABI kind that should be used.
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
mlir::Attribute getConstRecordOrZeroAttr(mlir::ArrayAttr arrayAttr, bool packed=false, bool padded=false, mlir::Type type={})
uint64_t computeOffsetFromGlobalViewIndices(const cir::CIRDataLayout &layout, mlir::Type ty, llvm::ArrayRef< int64_t > indices)
void computeGlobalViewIndicesFromFlatOffset(int64_t offset, mlir::Type ty, cir::CIRDataLayout layout, llvm::SmallVectorImpl< int64_t > &indices)
cir::ConstArrayAttr getConstArray(mlir::Attribute attrs, cir::ArrayType arrayTy) const
virtual mlir::Operation * getKernelHandle(cir::FuncOp fn, GlobalDecl gd)=0
Implements C++ ABI-specific code generation functions.
virtual mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc, QualType ty)=0
virtual void emitCXXConstructors(const clang::CXXConstructorDecl *d)=0
Emit constructor variants required by this ABI.
virtual void emitCXXDestructors(const clang::CXXDestructorDecl *d)=0
Emit dtor variants required by this ABI.
clang::MangleContext & getMangleContext()
Gets the mangle context.
virtual cir::GlobalLinkageKind getCXXDestructorLinkage(GVALinkage linkage, const CXXDestructorDecl *dtor, CXXDtorType dt) const
llvm::ArrayRef< CanQualType > arguments() const
cir::FuncOp generateCode(clang::GlobalDecl gd, cir::FuncOp fn, cir::FuncType funcType)
void emitVariablyModifiedType(QualType ty)
mlir::Operation * curFn
The current function or global initializer that is generated code for.
This class organizes the cross-function state that is used while generating CIR code.
void updateResolvedBlockAddress(cir::BlockAddressOp op, cir::LabelOp newLabel)
void addUsedOrCompilerUsedGlobal(cir::CIRGlobalValueInterface gv)
Add a global to a list to be added to the llvm.compiler.used metadata.
void replaceUsesOfNonProtoTypeWithRealFunction(mlir::Operation *old, cir::FuncOp newFn)
This function is called when we implement a function with no prototype, e.g.
static cir::GlobalOp createGlobalOp(CIRGenModule &cgm, mlir::Location loc, llvm::StringRef name, mlir::Type t, bool isConstant=false, mlir::ptr::MemorySpaceAttrInterface addrSpace={}, mlir::Operation *insertPoint=nullptr)
llvm::StringRef getMangledName(clang::GlobalDecl gd)
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *derivedClass, llvm::iterator_range< CastExpr::path_const_iterator > path)
void setGlobalVisibility(mlir::Operation *op, const NamedDecl *d) const
Set the visibility for the given global.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
void emitDeferred()
Emit any needed decls for which code generation was deferred.
cir::GlobalLinkageKind getCIRLinkageVarDefinition(const VarDecl *vd)
clang::ASTContext & getASTContext() const
cir::FuncOp getAddrOfCXXStructor(clang::GlobalDecl gd, const CIRGenFunctionInfo *fnInfo=nullptr, cir::FuncType fnType=nullptr, bool dontDefer=false, ForDefinition_t isForDefinition=NotForDefinition)
CIRGenCUDARuntime & getCUDARuntime()
llvm::DenseMap< cir::BlockAddrInfoAttr, cir::LabelOp > blockAddressInfoToLabel
Map BlockAddrInfoAttr (function name, label name) to the corresponding CIR LabelOp.
void emitTopLevelDecl(clang::Decl *decl)
void emitOMPDeclareMapper(const OMPDeclareMapperDecl *d)
void addReplacement(llvm::StringRef name, mlir::Operation *op)
mlir::Type convertType(clang::QualType type)
bool shouldEmitRTTI(bool forEH=false)
cir::GlobalOp getGlobalForStringLiteral(const StringLiteral *s, llvm::StringRef name=".str")
Return a global symbol reference to a constant array for the given string literal.
std::vector< cir::CIRGlobalValueInterface > llvmUsed
List of global values which are required to be present in the object file; This is used for forcing v...
void emitOMPCapturedExpr(const OMPCapturedExprDecl *d)
void mapUnresolvedBlockAddress(cir::BlockAddressOp op)
bool mustBeEmitted(const clang::ValueDecl *d)
Determine whether the definition must be emitted; if this returns false, the definition can be emitte...
void emitGlobalOpenACCDeclareDecl(const clang::OpenACCDeclareDecl *cd)
mlir::IntegerAttr getSize(CharUnits size)
CIRGenBuilderTy & getBuilder()
void setDSOLocal(mlir::Operation *op) const
std::string getUniqueGlobalName(const std::string &baseName)
std::pair< cir::FuncType, cir::FuncOp > getAddrAndTypeOfCXXStructor(clang::GlobalDecl gd, const CIRGenFunctionInfo *fnInfo=nullptr, cir::FuncType fnType=nullptr, bool dontDefer=false, ForDefinition_t isForDefinition=NotForDefinition)
void setGVProperties(mlir::Operation *op, const NamedDecl *d) const
Set visibility, dllimport/dllexport and dso_local.
cir::GlobalOp getOrCreateCIRGlobal(llvm::StringRef mangledName, mlir::Type ty, LangAS langAS, const VarDecl *d, ForDefinition_t isForDefinition)
If the specified mangled name is not in the module, create and return an mlir::GlobalOp value.
cir::FuncOp createCIRBuiltinFunction(mlir::Location loc, llvm::StringRef name, cir::FuncType ty, const clang::FunctionDecl *fd)
Create a CIR function with builtin attribute set.
void emitGlobalOpenACCRoutineDecl(const clang::OpenACCRoutineDecl *cd)
clang::CharUnits getClassPointerAlignment(const clang::CXXRecordDecl *rd)
Return the best known alignment for an unknown pointer to a particular class.
void handleCXXStaticMemberVarInstantiation(VarDecl *vd)
Tell the consumer that this variable has been instantiated.
std::vector< cir::CIRGlobalValueInterface > llvmCompilerUsed
void emitOMPRequiresDecl(const OMPRequiresDecl *d)
void emitGlobalDefinition(clang::GlobalDecl gd, mlir::Operation *op=nullptr)
void mapResolvedBlockAddress(cir::BlockAddressOp op, cir::LabelOp)
clang::DiagnosticsEngine & getDiags() const
cir::GlobalLinkageKind getCIRLinkageForDeclarator(const DeclaratorDecl *dd, GVALinkage linkage)
mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc, QualType ty, bool forEH=false)
Get the address of the RTTI descriptor for the given type.
void setFunctionAttributes(GlobalDecl gd, cir::FuncOp f, bool isIncompleteFunction, bool isThunk)
Set function attributes for a function declaration.
static mlir::SymbolTable::Visibility getMLIRVisibilityFromCIRLinkage(cir::GlobalLinkageKind GLK)
const clang::TargetInfo & getTarget() const
void setCIRFunctionAttributes(GlobalDecl gd, const CIRGenFunctionInfo &info, cir::FuncOp func, bool isThunk)
Set the CIR function attributes (Sext, zext, etc).
const llvm::Triple & getTriple() const
static mlir::SymbolTable::Visibility getMLIRVisibility(Visibility v)
void emitTentativeDefinition(const VarDecl *d)
void addUsedGlobal(cir::CIRGlobalValueInterface gv)
Add a global value to the llvmUsed list.
cir::GlobalOp createOrReplaceCXXRuntimeVariable(mlir::Location loc, llvm::StringRef name, mlir::Type ty, cir::GlobalLinkageKind linkage, clang::CharUnits alignment)
Will return a global variable of the given type.
void emitOMPAllocateDecl(const OMPAllocateDecl *d)
void error(SourceLocation loc, llvm::StringRef error)
Emit a general error that something can't be done.
void emitGlobalDecl(const clang::GlobalDecl &d)
Helper for emitDeferred to apply actual codegen.
void emitGlobalVarDefinition(const clang::VarDecl *vd, bool isTentative=false)
cir::FuncOp createRuntimeFunction(cir::FuncType ty, llvm::StringRef name, mlir::NamedAttrList extraAttrs={}, bool isLocal=false, bool assumeConvergent=false)
void setTLSMode(mlir::Operation *op, const VarDecl &d)
Set TLS mode for the given operation based on the given variable declaration.
cir::FuncOp getAddrOfFunction(clang::GlobalDecl gd, mlir::Type funcType=nullptr, bool forVTable=false, bool dontDefer=false, ForDefinition_t isForDefinition=NotForDefinition)
Return the address of the given function.
void emitAliasForGlobal(llvm::StringRef mangledName, mlir::Operation *op, GlobalDecl aliasGD, cir::FuncOp aliasee, cir::GlobalLinkageKind linkage)
void emitLLVMUsed()
Emit llvm.used and llvm.compiler.used globals.
mlir::Value emitMemberPointerConstant(const UnaryOperator *e)
void emitGlobalOpenACCDecl(const clang::OpenACCConstructDecl *cd)
void emitExplicitCastExprType(const ExplicitCastExpr *e, CIRGenFunction *cgf=nullptr)
Emit type info if type of an expression is a variably modified type.
const cir::CIRDataLayout getDataLayout() const
mlir::Operation * getAddrOfGlobalTemporary(const MaterializeTemporaryExpr *mte, const Expr *init)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
std::map< llvm::StringRef, clang::GlobalDecl > deferredDecls
This contains all the decls which have definitions but which are deferred for emission and therefore ...
void errorUnsupported(const Stmt *s, llvm::StringRef type)
Print out an error that codegen doesn't support the specified stmt yet.
mlir::Value getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty={}, ForDefinition_t isForDefinition=NotForDefinition)
Return the mlir::Value for the address of the given global variable.
static void setInitializer(cir::GlobalOp &op, mlir::Attribute value)
cir::GlobalViewAttr getAddrOfGlobalVarAttr(const VarDecl *d)
Return the mlir::GlobalViewAttr for the address of the given global.
void addGlobalCtor(cir::FuncOp ctor, std::optional< int > priority=std::nullopt)
Add a global constructor or destructor to the module.
cir::GlobalLinkageKind getFunctionLinkage(GlobalDecl gd)
void updateCompletedType(const clang::TagDecl *td)
const clang::CodeGenOptions & getCodeGenOpts() const
void emitDeferredVTables()
Emit any vtables which we deferred and still have a use for.
const clang::LangOptions & getLangOpts() const
void constructAttributeList(llvm::StringRef name, const CIRGenFunctionInfo &info, CIRGenCalleeInfo calleeInfo, mlir::NamedAttrList &attrs, llvm::MutableArrayRef< mlir::NamedAttrList > argAttrs, mlir::NamedAttrList &retAttrs, cir::CallingConv &callingConv, cir::SideEffect &sideEffect, bool attrOnCallSite, bool isThunk)
Get the CIR attributes and calling convention to use for a particular function type.
cir::FuncOp getOrCreateCIRFunction(llvm::StringRef mangledName, mlir::Type funcType, clang::GlobalDecl gd, bool forVTable, bool dontDefer=false, bool isThunk=false, ForDefinition_t isForDefinition=NotForDefinition, mlir::NamedAttrList extraAttrs={})
void emitOpenACCRoutineDecl(const clang::FunctionDecl *funcDecl, cir::FuncOp func, SourceLocation pragmaLoc, ArrayRef< const OpenACCClause * > clauses)
void emitVTablesOpportunistically()
Try to emit external vtables as available_externally if they have emitted all inlined virtual functio...
cir::TLS_Model getDefaultCIRTLSModel() const
Get TLS mode from CodeGenOptions.
void addGlobalDtor(cir::FuncOp dtor, std::optional< int > priority=std::nullopt)
Add a function to the list that will be called when the module is unloaded.
void addDeferredDeclToEmit(clang::GlobalDecl GD)
bool shouldEmitCUDAGlobalVar(const VarDecl *global) const
cir::FuncOp createCIRFunction(mlir::Location loc, llvm::StringRef name, cir::FuncType funcType, const clang::FunctionDecl *funcDecl)
const TargetCIRGenInfo & getTargetCIRGenInfo()
void emitCXXGlobalVarDeclInitFunc(const VarDecl *vd, cir::GlobalOp addr, bool performInit)
void setGVPropertiesAux(mlir::Operation *op, const NamedDecl *d) const
LangAS getLangTempAllocaAddressSpace() const
Returns the address space for temporary allocations in the language.
llvm::DenseSet< cir::BlockAddressOp > unresolvedBlockAddressToLabel
Track CIR BlockAddressOps that cannot be resolved immediately because their LabelOp has not yet been ...
mlir::Location getLoc(clang::SourceLocation cLoc)
Helpers to convert the presumed location of Clang's SourceLocation to an MLIR Location.
llvm::DenseMap< mlir::Attribute, cir::GlobalOp > constantStringMap
mlir::Operation * lastGlobalOp
void replaceGlobal(cir::GlobalOp oldGV, cir::GlobalOp newGV)
Replace all uses of the old global with the new global, updating types and references as needed.
static cir::VisibilityKind getGlobalVisibilityKindFromClangVisibility(clang::VisibilityAttr::VisibilityType visibility)
llvm::StringMap< unsigned > cgGlobalNames
void setCXXSpecialMemberAttr(cir::FuncOp funcOp, const clang::FunctionDecl *funcDecl)
Mark the function as a special member (e.g. constructor, destructor)
mlir::TypedAttr emitNullMemberAttr(QualType t, const MemberPointerType *mpt)
Returns a null attribute to represent either a null method or null data member, depending on the type...
mlir::Operation * getGlobalValue(llvm::StringRef ref)
void emitOMPDeclareReduction(const OMPDeclareReductionDecl *d)
mlir::ModuleOp getModule() const
void addCompilerUsedGlobal(cir::CIRGlobalValueInterface gv)
Add a global value to the llvmCompilerUsed list.
clang::CharUnits getNaturalTypeAlignment(clang::QualType t, LValueBaseInfo *baseInfo=nullptr, bool forPointeeType=false)
FIXME: this could likely be a common helper and not necessarily related with codegen.
mlir::MLIRContext & getMLIRContext()
mlir::Operation * getAddrOfGlobal(clang::GlobalDecl gd, ForDefinition_t isForDefinition=NotForDefinition)
void maybeSetTrivialComdat(const clang::Decl &d, mlir::Operation *op)
CIRGenCXXABI & getCXXABI() const
cir::GlobalViewAttr getAddrOfConstantStringFromLiteral(const StringLiteral *s, llvm::StringRef name=".str")
Return a global symbol reference to a constant array for the given string literal.
llvm::MapVector< cir::BlockAddressOp, cir::LabelOp > blockAddressToLabel
Map CIR BlockAddressOps directly to their resolved LabelOps.
bool lookupRepresentativeDecl(llvm::StringRef mangledName, clang::GlobalDecl &gd) const
void emitDeclContext(const DeclContext *dc)
clang::CharUnits getNaturalPointeeTypeAlignment(clang::QualType t, LValueBaseInfo *baseInfo=nullptr)
void emitGlobal(clang::GlobalDecl gd)
Emit code for a single global function or variable declaration.
cir::LabelOp lookupBlockAddressInfo(cir::BlockAddrInfoAttr blockInfo)
bool mayBeEmittedEagerly(const clang::ValueDecl *d)
Determine whether the definition can be emitted eagerly, or should be delayed until the end of the tr...
void mapBlockAddress(cir::BlockAddrInfoAttr blockInfo, cir::LabelOp label)
void setCIRFunctionAttributesForDefinition(const clang::FunctionDecl *fd, cir::FuncOp f)
Set extra attributes (inline, etc.) for a function.
std::string getOpenACCBindMangledName(const IdentifierInfo *bindName, const FunctionDecl *attachedFunction)
void emitGlobalFunctionDefinition(clang::GlobalDecl gd, mlir::Operation *op)
CIRGenVTables & getVTables()
void setFunctionLinkage(GlobalDecl gd, cir::FuncOp f)
std::vector< clang::GlobalDecl > deferredDeclsToEmit
void emitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *d)
void emitAMDGPUMetadata()
Emits AMDGPU specific Metadata.
void emitOMPGroupPrivateDecl(const OMPGroupPrivateDecl *d)
mlir::Attribute getConstantArrayFromStringLiteral(const StringLiteral *e)
Return a constant array for the given string.
cir::VisibilityAttr getGlobalVisibilityAttrFromDecl(const Decl *decl)
void setCommonAttributes(GlobalDecl gd, mlir::Operation *op)
Set attributes which are common to any form of a global definition (alias, Objective-C method,...
const CIRGenFunctionInfo & arrangeGlobalDeclaration(GlobalDecl gd)
const CIRGenFunctionInfo & arrangeCXXMethodDeclaration(const clang::CXXMethodDecl *md)
C++ methods have some special rules and also have implicit parameters.
const CIRGenFunctionInfo & arrangeCXXStructorDeclaration(clang::GlobalDecl gd)
cir::FuncType getFunctionType(const CIRGenFunctionInfo &info)
Get the CIR function type for.
mlir::Type convertTypeForMem(clang::QualType, bool forBitField=false)
Convert type T into an mlir::Type.
void emitThunks(GlobalDecl gd)
Emit the associated thunks for the given global decl.
virtual clang::LangAS getGlobalVarAddressSpace(CIRGenModule &cgm, const clang::VarDecl *d) const
Get target favored AST address space of a global variable for languages other than OpenCL and CUDA.
virtual mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const
Get the address space for alloca.
Definition TargetInfo.h:61
virtual void setTargetAttributes(const clang::Decl *decl, mlir::Operation *global, CIRGenModule &module) const
Provides a convenient hook to handle extra target-specific attributes for the given global.
Definition TargetInfo.h:115
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a C++ constructor within a class.
Definition DeclCXX.h:2624
bool isMoveConstructor(unsigned &TypeQuals) const
Determine whether this constructor is a move constructor (C++11 [class.copy]p3), which can be used to...
Definition DeclCXX.cpp:3065
bool isCopyConstructor(unsigned &TypeQuals) const
Whether this constructor is a copy constructor (C++ [class.copy]p2, which can be used to copy the cla...
Definition DeclCXX.cpp:3060
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition DeclCXX.cpp:3051
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition DeclCXX.cpp:2753
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition DeclCXX.cpp:2732
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool isEffectivelyFinal() const
Determine whether it's impossible for a class to be derived from this class.
Definition DeclCXX.cpp:2343
bool hasDefinition() const
Definition DeclCXX.h:561
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
llvm::Reloc::Model RelocationModel
The name of the relocation model to use.
Represents the canonical version of C arrays with a specified constant size.
Definition TypeBase.h:3810
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1462
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2386
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:581
bool isWeakImported() const
Determine whether this is a weak-imported symbol.
Definition DeclBase.cpp:873
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition DeclBase.cpp:273
static DeclContext * castToDeclContext(const Decl *)
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
Definition DeclBase.h:567
SourceLocation getLocation() const
Definition DeclBase.h:447
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition DeclBase.h:931
bool hasAttr() const
Definition DeclBase.h:585
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition DeclBase.h:435
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:780
A little helper class used to produce diagnostics.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Definition Diagnostic.h:914
ExplicitCastExpr - An explicit cast written in the source code.
Definition Expr.h:3931
This represents one expression.
Definition Expr.h:112
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3175
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:302
StringRef tryGetRealPathName() const
Definition FileEntry.h:327
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Represents a function declaration or definition.
Definition Decl.h:2015
static FunctionDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation NLoc, DeclarationName N, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin=false, bool isInlineSpecified=false, bool hasWrittenPrototype=true, ConstexprSpecKind ConstexprKind=ConstexprSpecKind::Unspecified, const AssociatedConstraint &TrailingRequiresClause={})
Definition Decl.h:2204
bool hasPrototype() const
Whether this function has a prototype, either because one was explicitly written or because it was "i...
Definition Decl.h:2458
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3201
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4553
CallingConv getCallConv() const
Definition TypeBase.h:4908
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
GlobalDecl getCanonicalDecl() const
Definition GlobalDecl.h:97
KernelReferenceKind getKernelReferenceKind() const
Definition GlobalDecl.h:135
GlobalDecl getWithDecl(const Decl *D)
Definition GlobalDecl.h:172
CXXDtorType getDtorType() const
Definition GlobalDecl.h:113
const Decl * getDecl() const
Definition GlobalDecl.h:106
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
clang::ObjCRuntime ObjCRuntime
void setLinkage(Linkage L)
Definition Visibility.h:92
Linkage getLinkage() const
Definition Visibility.h:88
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition Mangle.h:56
bool shouldMangleDeclName(const NamedDecl *D)
Definition Mangle.cpp:127
void mangleName(GlobalDecl GD, raw_ostream &)
Definition Mangle.cpp:190
virtual void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber, raw_ostream &)=0
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4920
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4945
APValue * getOrCreateValue(bool MayCreate) const
Get the storage for the constant value of a materialized temporary of static storage duration.
Definition ExprCXX.h:4953
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Definition ExprCXX.h:4970
unsigned getManglingNumber() const
Definition ExprCXX.h:4981
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3703
This represents a decl that may have a name.
Definition Decl.h:274
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
LinkageInfo getLinkageAndVisibility() const
Determines the linkage and visibility of this entity.
Definition Decl.cpp:1227
bool hasUnwindExceptions() const
Does this runtime use zero-cost exceptions?
Represents a parameter to a function.
Definition Decl.h:1805
void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex)
Definition Decl.h:1838
static ParmVarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
Definition Decl.cpp:2959
Represents an unpacked "presumed" location which can be presented to the user.
unsigned getColumn() const
Return the presumed column number of this location.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLine() const
Return the presumed line number of this location.
A (possibly-)qualified type.
Definition TypeBase.h:937
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8557
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8471
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8504
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition TypeBase.h:1036
bool hasUnaligned() const
Definition TypeBase.h:511
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
Definition Stmt.h:86
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1802
unsigned getLength() const
Definition Expr.h:1912
uint32_t getCodeUnit(size_t i) const
Definition Expr.h:1885
StringRef getString() const
Definition Expr.h:1870
unsigned getCharByteWidth() const
Definition Expr.h:1913
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3732
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isArrayType() const
Definition TypeBase.h:8767
bool isPointerType() const
Definition TypeBase.h:8668
bool isReferenceType() const
Definition TypeBase.h:8692
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition Type.cpp:5412
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:754
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2850
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition Type.cpp:5421
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition Type.cpp:2480
bool isObjCObjectPointerType() const
Definition TypeBase.h:8847
bool isMemberFunctionPointerType() const
Definition TypeBase.h:8753
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9261
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
Expr * getSubExpr() const
Definition Expr.h:2288
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:926
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1584
TLSKind getTLSKind() const
Definition Decl.cpp:2181
bool hasInit() const
Definition Decl.cpp:2411
DefinitionKind isThisDeclarationADefinition(ASTContext &) const
Check whether this declaration is a definition.
Definition Decl.cpp:2273
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2203
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
Definition Decl.cpp:2875
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1241
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2661
VarDecl * getDefinition(ASTContext &)
Get the real (not just tentative) definition for this declaration.
Definition Decl.cpp:2379
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition Decl.cpp:2864
const Expr * getInit() const
Definition Decl.h:1383
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition Decl.h:1232
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition Decl.h:952
@ TLS_None
Not a TLS variable.
Definition Decl.h:946
@ DeclarationOnly
This declaration is only a declaration.
Definition Decl.h:1310
@ Definition
This declaration is definitely a definition.
Definition Decl.h:1316
DefinitionKind hasDefinition(ASTContext &) const
Check whether this variable is defined in this translation unit.
Definition Decl.cpp:2388
TemplateSpecializationKind getTemplateSpecializationKind() const
If this variable is an instantiation of a variable template or a static data member of a class templa...
Definition Decl.cpp:2792
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1373
bool isMatchingAddressSpace(mlir::ptr::MemorySpaceAttrInterface cirAS, clang::LangAS as)
mlir::ptr::MemorySpaceAttrInterface toCIRAddressSpaceAttr(mlir::MLIRContext &ctx, clang::LangAS langAS)
Convert an AST LangAS to the appropriate CIR address space attribute interface.
static bool isWeakForLinker(GlobalLinkageKind linkage)
Whether the definition of this global may be replaced at link time.
@ AttributedType
The l-value was considered opaque, so the alignment was determined from a type, but that type was an ...
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
std::unique_ptr< TargetCIRGenInfo > createAMDGPUTargetCIRGenInfo(CIRGenTypes &cgt)
std::unique_ptr< TargetCIRGenInfo > createNVPTXTargetCIRGenInfo(CIRGenTypes &cgt)
CIRGenCXXABI * CreateCIRGenItaniumCXXABI(CIRGenModule &cgm)
Creates and Itanium-family ABI.
std::unique_ptr< TargetCIRGenInfo > createX8664TargetCIRGenInfo(CIRGenTypes &cgt)
CIRGenCUDARuntime * createNVCUDARuntime(CIRGenModule &cgm)
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Decl, FieldDecl > fieldDecl
Matches field declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl
Matches function declarations.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
GVALinkage
A more specific kind of linkage than enum Linkage.
Definition Linkage.h:72
@ GVA_StrongODR
Definition Linkage.h:77
@ GVA_StrongExternal
Definition Linkage.h:76
@ GVA_AvailableExternally
Definition Linkage.h:74
@ GVA_DiscardableODR
Definition Linkage.h:75
@ GVA_Internal
Definition Linkage.h:73
@ SC_None
Definition Specifiers.h:251
@ SD_Thread
Thread storage duration.
Definition Specifiers.h:343
@ SD_Static
Static storage duration.
Definition Specifiers.h:344
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition ASTLambda.h:28
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
LangAS
Defines the address space values used by the address space qualifier of QualType.
TemplateSpecializationKind
Describes the kind of template specialization that a particular template specialization declaration r...
Definition Specifiers.h:189
@ TSK_ExplicitInstantiationDefinition
This template specialization was instantiated from a template due to an explicit instantiation defini...
Definition Specifiers.h:207
@ TSK_ImplicitInstantiation
This template specialization was implicitly instantiated from a template.
Definition Specifiers.h:195
@ CC_X86RegCall
Definition Specifiers.h:288
U cast(CodeGen::Address addr)
Definition Address.h:327
bool isExternallyVisible(Linkage L)
Definition Linkage.h:90
static bool globalCtorLexOrder()
static bool opFuncArmNewAttr()
static bool getRuntimeFunctionDecl()
static bool weakRefReference()
static bool opFuncOptNoneAttr()
static bool addressSpace()
static bool opFuncMinSizeAttr()
static bool opGlobalUnnamedAddr()
static bool opGlobalThreadLocal()
static bool sourceLanguageCases()
static bool opFuncAstDeclAttr()
static bool opFuncNoDuplicateAttr()
static bool stackProtector()
static bool moduleNameHash()
static bool opGlobalVisibility()
static bool setDLLStorageClass()
static bool opFuncUnwindTablesAttr()
static bool opFuncParameterAttributes()
static bool targetCIRGenInfoArch()
static bool opFuncExtraAttrs()
static bool opFuncNakedAttr()
static bool attributeNoBuiltin()
static bool opGlobalDLLImportExport()
static bool opGlobalPartition()
static bool opGlobalPragmaClangSection()
static bool opGlobalWeakRef()
static bool deferredCXXGlobalInit()
static bool opFuncOperandBundles()
static bool opFuncCallingConv()
static bool globalCtorAssociatedData()
static bool defaultVisibility()
static bool opFuncColdHotAttr()
static bool opFuncExceptions()
static bool opFuncArmStreamingAttr()
static bool cudaSupport()
static bool opFuncMaybeHandleStaticInExternC()
static bool generateDebugInfo()
static bool targetCIRGenInfoOS()
static bool opFuncCPUAndFeaturesAttributes()
static bool maybeHandleStaticInExternC()
static bool setLLVMFunctionFEnvAttributes()
mlir::Type uCharTy
ClangIR char.
cir::PointerType allocaInt8PtrTy
void* in alloca address space
mlir::ptr::MemorySpaceAttrInterface cirAllocaAddressSpace
cir::PointerType voidPtrTy
void* in address space 0
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:648
APValue Val
Val - This is the value the expression can be folded to.
Definition Expr.h:650
bool hasSideEffects() const
Return true if the evaluated expression has side effects.
Definition Expr.h:642