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"
20#include "mlir/IR/SymbolTable.h"
22#include "clang/AST/ASTLambda.h"
23#include "clang/AST/Attrs.inc"
24#include "clang/AST/DeclBase.h"
37#include "llvm/ADT/STLExtras.h"
38#include "llvm/ADT/StringExtras.h"
39#include "llvm/ADT/StringRef.h"
40#include "llvm/Support/raw_ostream.h"
41
42#include "CIRGenFunctionInfo.h"
43#include "TargetInfo.h"
44#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
45#include "mlir/IR/Attributes.h"
46#include "mlir/IR/BuiltinOps.h"
47#include "mlir/IR/Location.h"
48#include "mlir/IR/MLIRContext.h"
49#include "mlir/IR/Operation.h"
50#include "mlir/IR/Verifier.h"
51
52#include <algorithm>
53
54using namespace clang;
55using namespace clang::CIRGen;
56
58 switch (cgm.getASTContext().getCXXABIKind()) {
59 case TargetCXXABI::GenericItanium:
60 case TargetCXXABI::GenericAArch64:
61 case TargetCXXABI::AppleARM64:
62 case TargetCXXABI::GenericARM:
63 return CreateCIRGenItaniumCXXABI(cgm);
64
65 case TargetCXXABI::Fuchsia:
66 case TargetCXXABI::iOS:
67 case TargetCXXABI::WatchOS:
68 case TargetCXXABI::GenericMIPS:
69 case TargetCXXABI::WebAssembly:
70 case TargetCXXABI::XL:
71 case TargetCXXABI::Microsoft:
72 cgm.errorNYI("createCXXABI: C++ ABI kind");
73 return nullptr;
74 }
75
76 llvm_unreachable("invalid C++ ABI kind");
77}
78
79CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
80 clang::ASTContext &astContext,
81 const clang::CodeGenOptions &cgo,
82 DiagnosticsEngine &diags)
83 : builder(mlirContext, *this), astContext(astContext),
84 langOpts(astContext.getLangOpts()), codeGenOpts(cgo),
85 theModule{mlir::ModuleOp::create(mlir::UnknownLoc::get(&mlirContext))},
86 diags(diags), target(astContext.getTargetInfo()),
87 abi(createCXXABI(*this)), genTypes(*this), vtables(*this) {
88
89 // Initialize cached types
90 voidTy = cir::VoidType::get(&getMLIRContext());
91 voidPtrTy = cir::PointerType::get(voidTy);
92 sInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/true);
93 sInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/true);
94 sInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/true);
95 sInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/true);
96 sInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/true);
97 uInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
98 uInt8PtrTy = cir::PointerType::get(uInt8Ty);
100 uInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/false);
101 uInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/false);
102 uInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/false);
103 uInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/false);
104 fP16Ty = cir::FP16Type::get(&getMLIRContext());
105 bFloat16Ty = cir::BF16Type::get(&getMLIRContext());
106 floatTy = cir::SingleType::get(&getMLIRContext());
107 doubleTy = cir::DoubleType::get(&getMLIRContext());
108 fP80Ty = cir::FP80Type::get(&getMLIRContext());
109 fP128Ty = cir::FP128Type::get(&getMLIRContext());
110
111 allocaInt8PtrTy = cir::PointerType::get(uInt8Ty, cirAllocaAddressSpace);
112
114 astContext
115 .toCharUnitsFromBits(
116 astContext.getTargetInfo().getPointerAlign(LangAS::Default))
117 .getQuantity();
118
119 const unsigned charSize = astContext.getTargetInfo().getCharWidth();
120 uCharTy = cir::IntType::get(&getMLIRContext(), charSize, /*isSigned=*/false);
121
122 // TODO(CIR): Should be updated once TypeSizeInfoAttr is upstreamed
123 const unsigned sizeTypeSize =
124 astContext.getTypeSize(astContext.getSignedSizeType());
125 SizeSizeInBytes = astContext.toCharUnitsFromBits(sizeTypeSize).getQuantity();
126 // In CIRGenTypeCache, UIntPtrTy and SizeType are fields of the same union
127 uIntPtrTy =
128 cir::IntType::get(&getMLIRContext(), sizeTypeSize, /*isSigned=*/false);
129 ptrDiffTy =
130 cir::IntType::get(&getMLIRContext(), sizeTypeSize, /*isSigned=*/true);
131
132 std::optional<cir::SourceLanguage> sourceLanguage = getCIRSourceLanguage();
133 if (sourceLanguage)
134 theModule->setAttr(
135 cir::CIRDialect::getSourceLanguageAttrName(),
136 cir::SourceLanguageAttr::get(&mlirContext, *sourceLanguage));
137 theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
138 builder.getStringAttr(getTriple().str()));
139
140 if (cgo.OptimizationLevel > 0 || cgo.OptimizeSize > 0)
141 theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
142 cir::OptInfoAttr::get(&mlirContext,
143 cgo.OptimizationLevel,
144 cgo.OptimizeSize));
145
146 if (langOpts.OpenMP) {
147 mlir::omp::OffloadModuleOpts ompOpts(
148 langOpts.OpenMPTargetDebug, langOpts.OpenMPTeamSubscription,
149 langOpts.OpenMPThreadSubscription, langOpts.OpenMPNoThreadState,
150 langOpts.OpenMPNoNestedParallelism, langOpts.OpenMPIsTargetDevice,
151 getTriple().isGPU(), langOpts.OpenMPForceUSM, langOpts.OpenMP,
152 langOpts.OMPHostIRFile, langOpts.OMPTargetTriples, langOpts.NoGPULib);
153 mlir::omp::setOffloadModuleInterfaceAttributes(theModule, ompOpts);
154 }
155
156 if (langOpts.CUDA)
157 createCUDARuntime();
158 if (langOpts.OpenMP)
159 createOpenMPRuntime();
160
161 // Set the module name to be the name of the main file. TranslationUnitDecl
162 // often contains invalid source locations and isn't a reliable source for the
163 // module location.
164 FileID mainFileId = astContext.getSourceManager().getMainFileID();
165 const FileEntry &mainFile =
166 *astContext.getSourceManager().getFileEntryForID(mainFileId);
167 StringRef path = mainFile.tryGetRealPathName();
168 if (!path.empty()) {
169 theModule.setSymName(path);
170 theModule->setLoc(mlir::FileLineColLoc::get(&mlirContext, path,
171 /*line=*/0,
172 /*column=*/0));
173 }
174
175 // Set CUDA GPU binary handle.
176 if (langOpts.CUDA) {
177 llvm::StringRef cudaBinaryName = codeGenOpts.CudaGpuBinaryFileName;
178 if (!cudaBinaryName.empty()) {
179 theModule->setAttr(cir::CIRDialect::getCUDABinaryHandleAttrName(),
180 cir::CUDABinaryHandleAttr::get(
181 &mlirContext, mlir::StringAttr::get(
182 &mlirContext, cudaBinaryName)));
183 }
184 }
185}
186
188
189void CIRGenModule::createCUDARuntime() {
190 cudaRuntime.reset(createNVCUDARuntime(*this));
191}
192
193void CIRGenModule::createOpenMPRuntime() {
194 openMPRuntime = std::make_unique<CIRGenOpenMPRuntime>(*this);
195}
196
197/// FIXME: this could likely be a common helper and not necessarily related
198/// with codegen.
199/// Return the best known alignment for an unknown pointer to a
200/// particular class.
202 if (!rd->hasDefinition())
203 return CharUnits::One(); // Hopefully won't be used anywhere.
204
205 auto &layout = astContext.getASTRecordLayout(rd);
206
207 // If the class is final, then we know that the pointer points to an
208 // object of that type and can use the full alignment.
209 if (rd->isEffectivelyFinal())
210 return layout.getAlignment();
211
212 // Otherwise, we have to assume it could be a subclass.
213 return layout.getNonVirtualAlignment();
214}
215
217 LValueBaseInfo *baseInfo,
218 bool forPointeeType) {
220
221 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown, but
222 // that doesn't return the information we need to compute baseInfo.
223
224 // Honor alignment typedef attributes even on incomplete types.
225 // We also honor them straight for C++ class types, even as pointees;
226 // there's an expressivity gap here.
227 if (const auto *tt = t->getAs<TypedefType>()) {
228 if (unsigned align = tt->getDecl()->getMaxAlignment()) {
229 if (baseInfo)
231 return astContext.toCharUnitsFromBits(align);
232 }
233 }
234
235 bool alignForArray = t->isArrayType();
236
237 // Analyze the base element type, so we don't get confused by incomplete
238 // array types.
239 t = astContext.getBaseElementType(t);
240
241 if (t->isIncompleteType()) {
242 // We could try to replicate the logic from
243 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
244 // type is incomplete, so it's impossible to test. We could try to reuse
245 // getTypeAlignIfKnown, but that doesn't return the information we need
246 // to set baseInfo. So just ignore the possibility that the alignment is
247 // greater than one.
248 if (baseInfo)
250 return CharUnits::One();
251 }
252
253 if (baseInfo)
255
256 CharUnits alignment;
257 const CXXRecordDecl *rd = nullptr;
258 if (t.getQualifiers().hasUnaligned()) {
259 alignment = CharUnits::One();
260 } else if (forPointeeType && !alignForArray &&
261 (rd = t->getAsCXXRecordDecl())) {
262 alignment = getClassPointerAlignment(rd);
263 } else {
264 alignment = astContext.getTypeAlignInChars(t);
265 }
266
267 // Cap to the global maximum type alignment unless the alignment
268 // was somehow explicit on the type.
269 if (unsigned maxAlign = astContext.getLangOpts().MaxTypeAlign) {
270 if (alignment.getQuantity() > maxAlign &&
271 !astContext.isAlignmentRequired(t))
272 alignment = CharUnits::fromQuantity(maxAlign);
273 }
274 return alignment;
275}
276
279 LValueBaseInfo *baseInfo) {
280 return getNaturalTypeAlignment(t->getPointeeType(), baseInfo,
281 /*forPointeeType=*/true);
282}
283
285 if (theTargetCIRGenInfo)
286 return *theTargetCIRGenInfo;
287
288 const llvm::Triple &triple = getTarget().getTriple();
289 switch (triple.getArch()) {
290 default:
292
293 // Currently we just fall through to x86_64.
294 [[fallthrough]];
295
296 case llvm::Triple::x86_64: {
297 switch (triple.getOS()) {
298 default:
300
301 // Currently we just fall through to x86_64.
302 [[fallthrough]];
303
304 case llvm::Triple::Linux:
305 theTargetCIRGenInfo = createX8664TargetCIRGenInfo(genTypes);
306 return *theTargetCIRGenInfo;
307 }
308 }
309 case llvm::Triple::nvptx:
310 case llvm::Triple::nvptx64:
311 theTargetCIRGenInfo = createNVPTXTargetCIRGenInfo(genTypes);
312 return *theTargetCIRGenInfo;
313 case llvm::Triple::amdgcn: {
314 theTargetCIRGenInfo = createAMDGPUTargetCIRGenInfo(genTypes);
315 return *theTargetCIRGenInfo;
316 }
317 case llvm::Triple::spirv:
318 case llvm::Triple::spirv32:
319 case llvm::Triple::spirv64:
320 theTargetCIRGenInfo = createSPIRVTargetCIRGenInfo(genTypes);
321 return *theTargetCIRGenInfo;
322 }
323}
324
326 assert(cLoc.isValid() && "expected valid source location");
327 const SourceManager &sm = astContext.getSourceManager();
328 PresumedLoc pLoc = sm.getPresumedLoc(cLoc);
329 StringRef filename = pLoc.getFilename();
330 return mlir::FileLineColLoc::get(builder.getStringAttr(filename),
331 pLoc.getLine(), pLoc.getColumn());
332}
333
334mlir::Location CIRGenModule::getLoc(SourceRange cRange) {
335 assert(cRange.isValid() && "expected a valid source range");
336 mlir::Location begin = getLoc(cRange.getBegin());
337 mlir::Location end = getLoc(cRange.getEnd());
338 mlir::Attribute metadata;
339 return mlir::FusedLoc::get({begin, end}, metadata, builder.getContext());
340}
341
342mlir::Operation *
344 const Decl *d = gd.getDecl();
345
347 return getAddrOfCXXStructor(gd, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
348 /*DontDefer=*/false, isForDefinition);
349
350 if (isa<CXXMethodDecl>(d)) {
351 const CIRGenFunctionInfo &fi =
353 cir::FuncType ty = getTypes().getFunctionType(fi);
354 return getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false,
355 isForDefinition);
356 }
357
358 if (isa<FunctionDecl>(d)) {
360 cir::FuncType ty = getTypes().getFunctionType(fi);
361 return getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false,
362 isForDefinition);
363 }
364
365 return getAddrOfGlobalVar(cast<VarDecl>(d), /*ty=*/nullptr, isForDefinition)
366 .getDefiningOp();
367}
368
370 // We call getAddrOfGlobal with isForDefinition set to ForDefinition in
371 // order to get a Value with exactly the type we need, not something that
372 // might have been created for another decl with the same mangled name but
373 // different type.
374 mlir::Operation *op = getAddrOfGlobal(d, ForDefinition);
375
376 // In case of different address spaces, we may still get a cast, even with
377 // IsForDefinition equal to ForDefinition. Query mangled names table to get
378 // GlobalValue.
379 if (!op)
381
382 assert(op && "expected a valid global op");
383
384 // Check to see if we've already emitted this. This is necessary for a
385 // couple of reasons: first, decls can end up in deferred-decls queue
386 // multiple times, and second, decls can end up with definitions in unusual
387 // ways (e.g. by an extern inline function acquiring a strong function
388 // redefinition). Just ignore those cases.
389 // TODO: Not sure what to map this to for MLIR
390 mlir::Operation *globalValueOp = op;
391 if (auto gv = dyn_cast<cir::GetGlobalOp>(op)) {
392 globalValueOp = getGlobalValue(gv.getName());
393 assert(globalValueOp && "expected a valid global op");
394 }
395
396 if (auto cirGlobalValue =
397 dyn_cast<cir::CIRGlobalValueInterface>(globalValueOp))
398 if (!cirGlobalValue.isDeclaration())
399 return;
400
401 // If this is OpenMP, check if it is legal to emit this global normally.
403
404 // Otherwise, emit the definition and move on to the next one.
406}
407
409 // Emit code for any potentially referenced deferred decls. Since a previously
410 // unused static decl may become used during the generation of code for a
411 // static function, iterate until no changes are made.
412
414
416 // Emitting a vtable doesn't directly cause more vtables to
417 // become deferred, although it can cause functions to be
418 // emitted that then need those vtables.
419 assert(deferredVTables.empty());
420
422
423 // Stop if we're out of both deferred vtables and deferred declarations.
424 if (deferredDeclsToEmit.empty())
425 return;
426
427 // Grab the list of decls to emit. If emitGlobalDefinition schedules more
428 // work, it will not interfere with this.
429 std::vector<GlobalDecl> curDeclsToEmit;
430 curDeclsToEmit.swap(deferredDeclsToEmit);
431
432 for (const GlobalDecl &d : curDeclsToEmit) {
434
435 // If we found out that we need to emit more decls, do that recursively.
436 // This has the advantage that the decls are emitted in a DFS and related
437 // ones are close together, which is convenient for testing.
438 if (!deferredVTables.empty() || !deferredDeclsToEmit.empty()) {
439 emitDeferred();
440 assert(deferredVTables.empty() && deferredDeclsToEmit.empty());
441 }
442 }
443}
444
445template <typename AttrT> static bool hasImplicitAttr(const ValueDecl *decl) {
446 if (!decl)
447 return false;
448 if (auto *attr = decl->getAttr<AttrT>())
449 return attr->isImplicit();
450 return decl->isImplicit();
451}
452
453// TODO(cir): This should be shared with OG Codegen.
455 assert(langOpts.CUDA && "Should not be called by non-CUDA languages");
456 // We need to emit host-side 'shadows' for all global
457 // device-side variables because the CUDA runtime needs their
458 // size and host-side address in order to provide access to
459 // their device-side incarnations.
460 return !langOpts.CUDAIsDevice || global->hasAttr<CUDADeviceAttr>() ||
461 global->hasAttr<CUDAConstantAttr>() ||
462 global->hasAttr<CUDASharedAttr>() ||
465}
466
468 const Decl *d) {
469 // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers
470 // postfix beginning with '.' since the symbol name can be demangled.
471 if (langOpts.HIP)
472 os << (isa<VarDecl>(d) ? ".static." : ".intern.");
473 else
474 os << (isa<VarDecl>(d) ? "__static__" : "__intern__");
475
476 // If the CUID is not specified we try to generate a unique postfix.
477 if (getLangOpts().CUID.empty()) {
478 // TODO: Once we add 'PreprocessorOpts' into CIRGenModule this part can be
479 // brought in from OG.
481 "printPostfixForExternalizedDecl: CUID is not specified");
482 } else {
483 os << getASTContext().getCUIDHash();
484 }
485}
486
488 if (const auto *cd = dyn_cast<clang::OpenACCConstructDecl>(gd.getDecl())) {
490 return;
491 }
492
493 const auto *global = cast<ValueDecl>(gd.getDecl());
494
495 // Weak references don't produce any output by themselves.
496 if (global->hasAttr<WeakRefAttr>())
497 return;
498
499 // If this is an alias definition (which otherwise looks like a declaration)
500 // emit it now.
501 if (global->hasAttr<AliasAttr>()) {
502 // Classic codegen calls shouldSkipAliasEmission here to skip alias
503 // emission for OpenMP target device and CUDA configurations.
506 return;
507 }
508
509 // If this is CUDA, be selective about which declarations we emit.
510 // Non-constexpr non-lambda implicit host device functions are not emitted
511 // unless they are used on device side.
512 if (langOpts.CUDA) {
513 assert((isa<FunctionDecl>(global) || isa<VarDecl>(global)) &&
514 "Expected Variable or Function");
515 if (const auto *varDecl = dyn_cast<VarDecl>(global)) {
517 return;
518 // TODO(cir): This should be shared with OG Codegen.
519 } else if (langOpts.CUDAIsDevice) {
520 const auto *functionDecl = dyn_cast<FunctionDecl>(global);
521 if ((!global->hasAttr<CUDADeviceAttr>() ||
522 (langOpts.OffloadImplicitHostDeviceTemplates &&
525 !functionDecl->isConstexpr() &&
527 !getASTContext().CUDAImplicitHostDeviceFunUsedByDevice.count(
528 functionDecl))) &&
529 !global->hasAttr<CUDAGlobalAttr>() &&
530 !(langOpts.HIPStdPar && isa<FunctionDecl>(global) &&
531 !global->hasAttr<CUDAHostAttr>()))
532 return;
533 // Device-only functions are the only things we skip.
534 } else if (!global->hasAttr<CUDAHostAttr>() &&
535 global->hasAttr<CUDADeviceAttr>())
536 return;
537 }
538
539 if (langOpts.OpenMP) {
540 // If this is OpenMP, check if it is legal to emit this global normally.
541 if (openMPRuntime && openMPRuntime->emitTargetGlobal(gd))
542 return;
543 if (auto *drd = dyn_cast<OMPDeclareReductionDecl>(global)) {
544 if (mustBeEmitted(global))
546 return;
547 }
548 if (auto *dmd = dyn_cast<OMPDeclareMapperDecl>(global)) {
549 if (mustBeEmitted(global))
551 return;
552 }
553 }
554
555 if (const auto *fd = dyn_cast<FunctionDecl>(global)) {
556 // Update deferred annotations with the latest declaration if the function
557 // was already used or defined.
558 if (fd->hasAttr<AnnotateAttr>()) {
559 StringRef mangledName = getMangledName(gd);
560 if (getGlobalValue(mangledName))
561 deferredAnnotations[mangledName] = fd;
562 }
563 if (!fd->doesThisDeclarationHaveABody()) {
564 if (!fd->doesDeclarationForceExternallyVisibleDefinition() &&
565 (!fd->isMultiVersion() || !getTarget().getTriple().isAArch64()))
566 return;
567
569 cir::FuncType ty = getTypes().getFunctionType(fi);
570 getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false);
571 return;
572 }
573 } else {
574 const auto *vd = cast<VarDecl>(global);
575 assert(vd->isFileVarDecl() && "Cannot emit local var decl as global.");
576 if (vd->isThisDeclarationADefinition() != VarDecl::Definition &&
577 !astContext.isMSStaticDataMemberInlineDefinition(vd)) {
579 // If this declaration may have caused an inline variable definition to
580 // change linkage, make sure that it's emitted.
581 if (astContext.getInlineVariableDefinitionKind(vd) ==
584 // Otherwise, we can ignore this declaration. The variable will be emitted
585 // on its first use.
586 return;
587 }
588 }
589
590 // Defer code generation to first use when possible, e.g. if this is an inline
591 // function. If the global must always be emitted, do it eagerly if possible
592 // to benefit from cache locality. Deferring code generation is necessary to
593 // avoid adding initializers to external declarations.
594 if (mustBeEmitted(global) && mayBeEmittedEagerly(global)) {
595 // Emit the definition if it can't be deferred.
597 return;
598 }
599
600 // If we're deferring emission of a C++ variable with an initializer, remember
601 // the order in which it appeared on the file.
603
604 llvm::StringRef mangledName = getMangledName(gd);
605 if (getGlobalValue(mangledName) != nullptr) {
606 // The value has already been used and should therefore be emitted.
608 } else if (mustBeEmitted(global)) {
609 // The value must be emitted, but cannot be emitted eagerly.
610 assert(!mayBeEmittedEagerly(global));
612 } else {
613 // Otherwise, remember that we saw a deferred decl with this name. The first
614 // use of the mangled name will cause it to move into deferredDeclsToEmit.
615 deferredDecls[mangledName] = gd;
616 }
617}
618
620 mlir::Operation *op) {
621 auto const *funcDecl = cast<FunctionDecl>(gd.getDecl());
623 cir::FuncType funcType = getTypes().getFunctionType(fi);
624 cir::FuncOp funcOp = dyn_cast_if_present<cir::FuncOp>(op);
625 if (!funcOp || funcOp.getFunctionType() != funcType) {
626 funcOp = getAddrOfFunction(gd, funcType, /*ForVTable=*/false,
627 /*DontDefer=*/true, ForDefinition);
628 }
629
630 // Already emitted.
631 if (!funcOp.isDeclaration())
632 return;
633
634 setFunctionLinkage(gd, funcOp);
635 setGVProperties(funcOp, funcDecl);
637 maybeSetTrivialComdat(*funcDecl, funcOp);
639
640 CIRGenFunction cgf(*this, builder);
641 curCGF = &cgf;
642 {
643 mlir::OpBuilder::InsertionGuard guard(builder);
644 cgf.generateCode(gd, funcOp, funcType);
645 }
646 curCGF = nullptr;
647
648 setNonAliasAttributes(gd, funcOp);
650
651 auto getPriority = [this](const auto *attr) -> int {
652 Expr *e = attr->getPriority();
653 if (e)
654 return e->EvaluateKnownConstInt(this->getASTContext()).getExtValue();
655 return attr->DefaultPriority;
656 };
657
658 if (const ConstructorAttr *ca = funcDecl->getAttr<ConstructorAttr>())
659 addGlobalCtor(funcOp, getPriority(ca));
660 if (const DestructorAttr *da = funcDecl->getAttr<DestructorAttr>())
661 addGlobalDtor(funcOp, getPriority(da));
662
663 if (funcDecl->getAttr<AnnotateAttr>())
664 deferredAnnotations[getMangledName(gd)] = funcDecl;
665
666 if (getLangOpts().OpenMP && funcDecl->hasAttr<OMPDeclareTargetDeclAttr>())
668}
669
670/// Track functions to be called before main() runs.
671void CIRGenModule::addGlobalCtor(cir::FuncOp ctor,
672 std::optional<int> priority) {
675
676 // Traditional LLVM codegen directly adds the function to the list of global
677 // ctors. In CIR we just add a global_ctor attribute to the function. The
678 // global list is created in LoweringPrepare.
679 //
680 // FIXME(from traditional LLVM): Type coercion of void()* types.
681 ctor.setGlobalCtorPriority(priority);
682}
683
684/// Add a function to the list that will be called when the module is unloaded.
685void CIRGenModule::addGlobalDtor(cir::FuncOp dtor,
686 std::optional<int> priority) {
687 if (codeGenOpts.RegisterGlobalDtorsWithAtExit &&
688 (!getASTContext().getTargetInfo().getTriple().isOSAIX()))
689 errorNYI(dtor.getLoc(), "registerGlobalDtorsWithAtExit");
690
691 // FIXME(from traditional LLVM): Type coercion of void()* types.
692 dtor.setGlobalDtorPriority(priority);
693}
694
697 if (dk == VarDecl::Definition && vd->hasAttr<DLLImportAttr>())
698 return;
699
701 // If we have a definition, this might be a deferred decl. If the
702 // instantiation is explicit, make sure we emit it at the end.
705
707}
708
709mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) {
710 auto it = symbolLookupCache.find(name);
711 return it != symbolLookupCache.end() ? it->second : nullptr;
712}
713
714cir::GlobalOp
715CIRGenModule::createGlobalOp(mlir::Location loc, StringRef name, mlir::Type t,
716 bool isConstant,
717 mlir::ptr::MemorySpaceAttrInterface addrSpace,
718 mlir::Operation *insertPoint) {
719 cir::GlobalOp g;
720 CIRGenBuilderTy &builder = getBuilder();
721
722 {
723 mlir::OpBuilder::InsertionGuard guard(builder);
724
725 // If an insertion point is provided, we're replacing an existing global,
726 // otherwise, create the new global immediately after the last gloabl we
727 // emitted.
728 if (insertPoint) {
729 builder.setInsertionPoint(insertPoint);
730 } else {
731 // Group global operations together at the top of the module.
732 if (lastGlobalOp)
733 builder.setInsertionPointAfter(lastGlobalOp);
734 else
735 builder.setInsertionPointToStart(getModule().getBody());
736 }
737
738 g = cir::GlobalOp::create(builder, loc, name, t, isConstant, addrSpace);
739 if (!insertPoint)
740 lastGlobalOp = g;
741
742 // Default to private until we can judge based on the initializer,
743 // since MLIR doesn't allow public declarations.
744 mlir::SymbolTable::setSymbolVisibility(
745 g, mlir::SymbolTable::Visibility::Private);
746 }
747 symbolLookupCache[g.getSymNameAttr()] = g;
748 return g;
749}
750
751void CIRGenModule::setCommonAttributes(GlobalDecl gd, mlir::Operation *gv) {
752 const Decl *d = gd.getDecl();
753 if (isa_and_nonnull<NamedDecl>(d))
754 setGVProperties(gv, dyn_cast<NamedDecl>(d));
756
757 if (auto gvi = mlir::dyn_cast<cir::CIRGlobalValueInterface>(gv)) {
758 if (d && d->hasAttr<UsedAttr>())
760
761 if (const auto *vd = dyn_cast_if_present<VarDecl>(d);
762 vd && ((codeGenOpts.KeepPersistentStorageVariables &&
763 (vd->getStorageDuration() == SD_Static ||
764 vd->getStorageDuration() == SD_Thread)) ||
765 (codeGenOpts.KeepStaticConsts &&
766 vd->getStorageDuration() == SD_Static &&
767 vd->getType().isConstQualified())))
769 }
770}
771
772/// Get the feature delta from the default feature map for the given target CPU.
773static std::vector<std::string>
774getFeatureDeltaFromDefault(const CIRGenModule &cgm, llvm::StringRef targetCPU,
775 llvm::StringMap<bool> &featureMap) {
776 llvm::StringMap<bool> defaultFeatureMap;
778 defaultFeatureMap, cgm.getASTContext().getDiagnostics(), targetCPU, {});
779
780 std::vector<std::string> delta;
781 for (const auto &[k, v] : featureMap) {
782 auto defaultIt = defaultFeatureMap.find(k);
783 if (defaultIt == defaultFeatureMap.end() || defaultIt->getValue() != v)
784 delta.push_back((v ? "+" : "-") + k.str());
785 }
786
787 return delta;
788}
789
790bool CIRGenModule::getCPUAndFeaturesAttributes(
791 GlobalDecl gd, llvm::StringMap<std::string> &attrs,
792 bool setTargetFeatures) {
793 // Add target-cpu and target-features attributes to functions. If
794 // we have a decl for the function and it has a target attribute then
795 // parse that and add it to the feature set.
796 llvm::StringRef targetCPU = getTarget().getTargetOpts().CPU;
797 llvm::StringRef tuneCPU = getTarget().getTargetOpts().TuneCPU;
798 std::vector<std::string> features;
799 // `fd` may be null when emitting attributes for globals that don't have a
800 // FunctionDecl. The AMDGPU branch below handles
801 // the null case via initFeatureMap.
802 const auto *fd = dyn_cast_or_null<FunctionDecl>(gd.getDecl());
803 fd = fd ? fd->getMostRecentDecl() : fd;
804 const auto *td = fd ? fd->getAttr<TargetAttr>() : nullptr;
805 const auto *tv = fd ? fd->getAttr<TargetVersionAttr>() : nullptr;
806 assert((!td || !tv) && "both target_version and target specified");
807 const auto *sd = fd ? fd->getAttr<CPUSpecificAttr>() : nullptr;
808 const auto *tc = fd ? fd->getAttr<TargetClonesAttr>() : nullptr;
809 bool addedAttr = false;
810 if (td || tv || sd || tc) {
811 llvm::StringMap<bool> featureMap;
812 astContext.getFunctionFeatureMap(featureMap, gd);
813
814 // Now add the target-cpu and target-features to the function.
815 // While we populated the feature map above, we still need to
816 // get and parse the target/target_clones attribute so we can
817 // get the cpu for the function.
818 llvm::StringRef featureStr = td ? td->getFeaturesStr() : llvm::StringRef();
819 if (tc && (getTriple().isOSAIX() || getTriple().isX86()))
820 featureStr = tc->getFeatureStr(gd.getMultiVersionIndex());
821 if (!featureStr.empty()) {
822 clang::ParsedTargetAttr parsedAttr =
823 getTarget().parseTargetAttr(featureStr);
824 if (!parsedAttr.CPU.empty() &&
825 getTarget().isValidCPUName(parsedAttr.CPU)) {
826 targetCPU = parsedAttr.CPU;
827 tuneCPU = ""; // Clear the tune CPU.
828 }
829 if (!parsedAttr.Tune.empty() &&
830 getTarget().isValidCPUName(parsedAttr.Tune))
831 tuneCPU = parsedAttr.Tune;
832 }
833
834 if (sd) {
835 // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
836 // favor this processor.
837 tuneCPU = sd->getCPUName(gd.getMultiVersionIndex())->getName();
838 }
839
840 // For AMDGPU, only emit delta features (features that differ from the
841 // target CPU's defaults). Other targets might want to follow a similar
842 // pattern.
843 if (getTarget().getTriple().isAMDGPU()) {
844 features = getFeatureDeltaFromDefault(*this, targetCPU, featureMap);
845 } else {
846 // Produce the canonical string for this set of features.
847 features.reserve(features.size() + featureMap.size());
848 for (const auto &entry : featureMap)
849 features.push_back((entry.getValue() ? "+" : "-") +
850 entry.getKey().str());
851 }
852 } else {
853 // Just add the existing target cpu and target features to the function.
854 if (setTargetFeatures && getTarget().getTriple().isAMDGPU()) {
855 llvm::StringMap<bool> featureMap;
856 if (fd)
857 astContext.getFunctionFeatureMap(featureMap, gd);
858 else
859 getTarget().initFeatureMap(featureMap, astContext.getDiagnostics(),
860 targetCPU,
861 getTarget().getTargetOpts().Features);
862 features = getFeatureDeltaFromDefault(*this, targetCPU, featureMap);
863 } else {
864 features = getTarget().getTargetOpts().Features;
865 }
866 }
867
868 if (!targetCPU.empty()) {
869 attrs["cir.target-cpu"] = targetCPU.str();
870 addedAttr = true;
871 }
872 if (!tuneCPU.empty()) {
873 attrs["cir.tune-cpu"] = tuneCPU.str();
874 addedAttr = true;
875 }
876 if (!features.empty() && setTargetFeatures) {
877 llvm::erase_if(features, [&](const std::string &f) {
878 assert(!f.empty() && (f[0] == '+' || f[0] == '-') &&
879 "feature string must start with '+' or '-'");
880 return getTarget().isReadOnlyFeature(f.substr(1));
881 });
882 llvm::sort(features);
883 attrs["cir.target-features"] = llvm::join(features, ",");
884 addedAttr = true;
885 }
886 // TODO(cir): add metadata for AArch64 Function Multi Versioning.
888 return addedAttr;
889}
890
891void CIRGenModule::setNonAliasAttributes(GlobalDecl gd, mlir::Operation *op) {
892 setCommonAttributes(gd, op);
893
894 const Decl *d = gd.getDecl();
895 if (d) {
896 if (auto gvi = mlir::dyn_cast<cir::CIRGlobalValueInterface>(op)) {
897 if (const auto *sa = d->getAttr<SectionAttr>())
898 gvi.setSection(builder.getStringAttr(sa->getName()));
899 if (d->hasAttr<RetainAttr>())
900 addUsedGlobal(gvi);
901
902 if (auto func = dyn_cast<cir::FuncOp>(op)) {
903 llvm::StringMap<std::string> attrs;
904 if (getCPUAndFeaturesAttributes(gd, attrs)) {
905 // TODO(cir): Classic codegen removes the existing target-cpu,
906 // target-features, tune-cpu and fmv-features attributes here
907 // before adding the new ones.
908 for (const auto &[key, val] : attrs)
909 func->setAttr(key, builder.getStringAttr(val));
910 }
911 }
912 }
913 }
914
917}
918
919std::optional<cir::SourceLanguage> CIRGenModule::getCIRSourceLanguage() const {
920 using ClangStd = clang::LangStandard;
921 using CIRLang = cir::SourceLanguage;
922 auto opts = getLangOpts();
923
924 if (opts.CPlusPlus)
925 return CIRLang::CXX;
926 if (opts.C99 || opts.C11 || opts.C17 || opts.C23 || opts.C2y ||
927 opts.LangStd == ClangStd::lang_c89 ||
928 opts.LangStd == ClangStd::lang_gnu89)
929 return CIRLang::C;
930
931 // TODO(cir): support remaining source languages.
933 errorNYI("CIR does not yet support the given source language");
934 return std::nullopt;
935}
936
937LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *d) {
938 if (langOpts.OpenCL) {
943 return as;
944 }
945
946 if (langOpts.SYCLIsDevice &&
947 (!d || d->getType().getAddressSpace() == LangAS::Default))
948 errorNYI("SYCL global address space");
949
950 if (langOpts.CUDA && langOpts.CUDAIsDevice) {
951 if (d) {
952 if (d->hasAttr<CUDAConstantAttr>())
954 if (d->hasAttr<CUDASharedAttr>())
955 return LangAS::cuda_shared;
956 if (d->hasAttr<CUDADeviceAttr>())
957 return LangAS::cuda_device;
958 if (d->getType().isConstQualified())
960 }
961 return LangAS::cuda_device;
962 }
963
964 if (langOpts.OpenMP)
965 errorNYI("OpenMP global address space");
966
968}
969
970static void setLinkageForGV(cir::GlobalOp &gv, const NamedDecl *nd) {
971 // Set linkage and visibility in case we never see a definition.
973 // Don't set internal linkage on declarations.
974 // "extern_weak" is overloaded in LLVM; we probably should have
975 // separate linkage types for this.
977 (nd->hasAttr<WeakAttr>() || nd->isWeakImported()))
978 gv.setLinkage(cir::GlobalLinkageKind::ExternalWeakLinkage);
979}
980
981static void setLinkageForFunction(CIRGenModule &cgm, cir::FuncOp &func,
982 const NamedDecl *nd) {
983 // Mirrors CodeGenModule::setLinkageForGV for function declarations.
986 (nd->hasAttr<WeakAttr>() || nd->isWeakImported())) {
987 auto linkage = cir::GlobalLinkageKind::ExternalWeakLinkage;
988 func.setLinkage(linkage);
989 func.setLinkageAttr(
990 cir::GlobalLinkageKindAttr::get(&cgm.getMLIRContext(), linkage));
991 // Declarations must keep 'private' MLIR visibility; only update for defs.
992 if (!func.isDeclaration())
993 mlir::SymbolTable::setSymbolVisibility(
994 func, cgm.getMLIRVisibilityFromCIRLinkage(linkage));
995 }
996}
997
998static llvm::SmallVector<int64_t> indexesOfArrayAttr(mlir::ArrayAttr indexes) {
1000 for (mlir::Attribute i : indexes) {
1001 auto ind = mlir::cast<mlir::IntegerAttr>(i);
1002 inds.push_back(ind.getValue().getSExtValue());
1003 }
1004 return inds;
1005}
1006
1007static bool isViewOnGlobal(cir::GlobalOp glob, cir::GlobalViewAttr view) {
1008 return view.getSymbol().getValue() == glob.getSymName();
1009}
1010
1011static cir::GlobalViewAttr createNewGlobalView(CIRGenModule &cgm,
1012 cir::GlobalOp newGlob,
1013 cir::GlobalViewAttr attr,
1014 mlir::Type oldTy) {
1015 // If the attribute does not require indexes or it is not a global view on
1016 // the global we're replacing, keep the original attribute.
1017 if (!attr.getIndices() || !isViewOnGlobal(newGlob, attr))
1018 return attr;
1019
1020 llvm::SmallVector<int64_t> oldInds = indexesOfArrayAttr(attr.getIndices());
1022 CIRGenBuilderTy &bld = cgm.getBuilder();
1023 const cir::CIRDataLayout &layout = cgm.getDataLayout();
1024 mlir::Type newTy = newGlob.getSymType();
1025
1026 uint64_t offset =
1027 bld.computeOffsetFromGlobalViewIndices(layout, oldTy, oldInds);
1028 bld.computeGlobalViewIndicesFromFlatOffset(offset, newTy, layout, newInds);
1029 cir::PointerType newPtrTy;
1030
1031 if (isa<cir::RecordType>(oldTy))
1032 newPtrTy = cir::PointerType::get(newTy);
1033 else if (isa<cir::ArrayType>(oldTy))
1034 newPtrTy = cast<cir::PointerType>(attr.getType());
1035
1036 if (newPtrTy)
1037 return bld.getGlobalViewAttr(newPtrTy, newGlob, newInds);
1038
1039 // This may be unreachable in practice, but keep it as errorNYI while CIR
1040 // is still under development.
1041 cgm.errorNYI("Unhandled type in createNewGlobalView");
1042 return {};
1043}
1044
1045static mlir::Attribute getNewInitValue(CIRGenModule &cgm, cir::GlobalOp newGlob,
1046 mlir::Type oldTy,
1047 mlir::Attribute oldInit) {
1048 if (auto oldView = mlir::dyn_cast<cir::GlobalViewAttr>(oldInit))
1049 return createNewGlobalView(cgm, newGlob, oldView, oldTy);
1050
1051 auto getNewInitElements =
1052 [&](mlir::ArrayAttr oldElements) -> mlir::ArrayAttr {
1054 for (mlir::Attribute elt : oldElements) {
1055 if (auto view = mlir::dyn_cast<cir::GlobalViewAttr>(elt))
1056 newElements.push_back(createNewGlobalView(cgm, newGlob, view, oldTy));
1057 else if (mlir::isa<cir::ConstArrayAttr, cir::ConstRecordAttr>(elt))
1058 newElements.push_back(getNewInitValue(cgm, newGlob, oldTy, elt));
1059 else
1060 newElements.push_back(elt);
1061 }
1062 return mlir::ArrayAttr::get(cgm.getBuilder().getContext(), newElements);
1063 };
1064
1065 if (auto oldArray = mlir::dyn_cast<cir::ConstArrayAttr>(oldInit)) {
1066 mlir::Attribute newElements =
1067 getNewInitElements(mlir::cast<mlir::ArrayAttr>(oldArray.getElts()));
1068 return cgm.getBuilder().getConstArray(
1069 newElements, mlir::cast<cir::ArrayType>(oldArray.getType()));
1070 }
1071 if (auto oldRecord = mlir::dyn_cast<cir::ConstRecordAttr>(oldInit)) {
1072 mlir::ArrayAttr newMembers = getNewInitElements(oldRecord.getMembers());
1073 auto recordTy = mlir::cast<cir::RecordType>(oldRecord.getType());
1075 newMembers, recordTy.getPacked(), recordTy.getPadded(), recordTy);
1076 }
1077
1078 // This may be unreachable in practice, but keep it as errorNYI while CIR
1079 // is still under development.
1080 cgm.errorNYI("Unhandled type in getNewInitValue");
1081 return {};
1082}
1083
1084// We want to replace a global value, but because of CIR's typed pointers,
1085// we need to update the existing uses to reflect the new type, not just replace
1086// them directly.
1087void CIRGenModule::replaceGlobal(cir::GlobalOp oldGV, cir::GlobalOp newGV) {
1088 assert(oldGV.getSymName() == newGV.getSymName() && "symbol names must match");
1089
1090 mlir::Type oldTy = oldGV.getSymType();
1091 mlir::Type newTy = newGV.getSymType();
1092
1094
1095 // If the type didn't change, why are we here?
1096 assert(oldTy != newTy && "expected type change in replaceGlobal");
1097
1098 // Visit all uses and add handling to fix up the types.
1099 std::optional<mlir::SymbolTable::UseRange> oldSymUses =
1100 oldGV.getSymbolUses(theModule);
1101 for (mlir::SymbolTable::SymbolUse use : *oldSymUses) {
1102 mlir::Operation *userOp = use.getUser();
1103 assert(
1104 (mlir::isa<cir::GetGlobalOp, cir::GlobalOp, cir::ConstantOp>(userOp)) &&
1105 "Unexpected user for global op");
1106
1107 if (auto getGlobalOp = dyn_cast<cir::GetGlobalOp>(use.getUser())) {
1108 mlir::Value useOpResultValue = getGlobalOp.getAddr();
1109 useOpResultValue.setType(cir::PointerType::get(newTy));
1110
1111 mlir::OpBuilder::InsertionGuard guard(builder);
1112 builder.setInsertionPointAfter(getGlobalOp);
1113 mlir::Type ptrTy = builder.getPointerTo(oldTy);
1114 mlir::Value cast =
1115 builder.createBitcast(getGlobalOp->getLoc(), useOpResultValue, ptrTy);
1116 useOpResultValue.replaceAllUsesExcept(cast, cast.getDefiningOp());
1117 } else if (auto glob = dyn_cast<cir::GlobalOp>(userOp)) {
1118 if (auto init = glob.getInitialValue()) {
1119 mlir::Attribute nw = getNewInitValue(*this, newGV, oldTy, init.value());
1120 glob.setInitialValueAttr(nw);
1121 }
1122 } else if (auto c = dyn_cast<cir::ConstantOp>(userOp)) {
1123 mlir::Attribute init = getNewInitValue(*this, newGV, oldTy, c.getValue());
1124 auto typedAttr = mlir::cast<mlir::TypedAttr>(init);
1125 mlir::OpBuilder::InsertionGuard guard(builder);
1126 builder.setInsertionPointAfter(c);
1127 auto newUser = cir::ConstantOp::create(builder, c.getLoc(), typedAttr);
1128 c.replaceAllUsesWith(newUser.getOperation());
1129 c.erase();
1130 }
1131 }
1132
1133 // If the old global is being tracked as the most-recently-created global,
1134 // update it so that subsequent globals are not inserted after a (now
1135 // erased) operation, which would leave them detached from the module.
1136 if (lastGlobalOp == oldGV)
1137 lastGlobalOp = newGV;
1138 if (getLangOpts().CUDA)
1139 getCUDARuntime().handleGlobalReplace(oldGV, newGV);
1140 eraseGlobalSymbol(oldGV);
1141 oldGV.erase();
1142}
1143
1144/// If the specified mangled name is not in the module,
1145/// create and return an mlir GlobalOp with the specified type (TODO(cir):
1146/// address space).
1147///
1148/// TODO(cir):
1149/// 1. If there is something in the module with the specified name, return
1150/// it potentially bitcasted to the right type.
1151///
1152/// 2. If \p d is non-null, it specifies a decl that correspond to this. This
1153/// is used to set the attributes on the global when it is first created.
1154///
1155/// 3. If \p isForDefinition is true, it is guaranteed that an actual global
1156/// with type \p ty will be returned, not conversion of a variable with the same
1157/// mangled name but some other type.
1158cir::GlobalOp
1159CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
1160 LangAS langAS, const VarDecl *d,
1161 ForDefinition_t isForDefinition) {
1162
1163 // Lookup the entry, lazily creating it if necessary.
1164 cir::GlobalOp entry;
1165 if (mlir::Operation *v = getGlobalValue(mangledName)) {
1166 if (!isa<cir::GlobalOp>(v))
1168 "getOrCreateCIRGlobal: global with non-GlobalOp type");
1169 entry = cast<cir::GlobalOp>(v);
1170 }
1171
1172 if (entry) {
1173 mlir::ptr::MemorySpaceAttrInterface entryCIRAS = entry.getAddrSpaceAttr();
1175
1178
1179 if (entry.getSymType() == ty &&
1180 cir::isMatchingAddressSpace(entryCIRAS, langAS))
1181 return entry;
1182
1183 // If there are two attempts to define the same mangled name, issue an
1184 // error.
1185 //
1186 // TODO(cir): look at mlir::GlobalValue::isDeclaration for all aspects of
1187 // recognizing the global as a declaration, for now only check if
1188 // initializer is present.
1189 if (isForDefinition && !entry.isDeclaration()) {
1191 "getOrCreateCIRGlobal: global with conflicting type");
1192 }
1193
1194 // Address space check removed because it is unnecessary because CIR records
1195 // address space info in types.
1196
1197 // (If global is requested for a definition, we always need to create a new
1198 // global, not just return a bitcast.)
1199 if (!isForDefinition)
1200 return entry;
1201 }
1202
1203 mlir::Location loc = getLoc(d->getSourceRange());
1204
1205 // Calculate constant storage flag before creating the global. This was moved
1206 // from after the global creation to ensure the constant flag is set correctly
1207 // at creation time, matching the logic used in emitCXXGlobalVarDeclInit.
1208 bool isConstant = false;
1209 if (d) {
1210 bool needsDtor =
1212 isConstant = d->getType().isConstantStorage(
1213 astContext, /*ExcludeCtor=*/true, /*ExcludeDtor=*/!needsDtor);
1214 }
1215
1216 mlir::ptr::MemorySpaceAttrInterface declCIRAS =
1217 cir::toCIRAddressSpaceAttr(getMLIRContext(), getGlobalVarAddressSpace(d));
1218
1219 // mlir::SymbolTable::Visibility::Public is the default, no need to explicitly
1220 // mark it as such.
1221 cir::GlobalOp gv = createGlobalOp(loc, mangledName, ty, isConstant, declCIRAS,
1222 /*insertPoint=*/entry.getOperation());
1223
1224 // If we already created a global with the same mangled name (but different
1225 // type) before, remove it from its parent.
1226 if (entry)
1227 replaceGlobal(entry, gv);
1228
1229 // This is the first use or definition of a mangled name. If there is a
1230 // deferred decl with this name, remember that we need to emit it at the end
1231 // of the file.
1232 auto ddi = deferredDecls.find(mangledName);
1233 if (ddi != deferredDecls.end()) {
1234 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
1235 // list, and remove it from DeferredDecls (since we don't need it anymore).
1236 addDeferredDeclToEmit(ddi->second);
1237 deferredDecls.erase(ddi);
1238 }
1239
1240 // Handle things which are present even on external declarations.
1241 if (d) {
1242 if (langOpts.OpenMP && !langOpts.OpenMPSimd)
1244 "getOrCreateCIRGlobal: OpenMP target global variable");
1245
1246 gv.setAlignmentAttr(getSize(astContext.getDeclAlign(d)));
1247
1248 setLinkageForGV(gv, d);
1249
1250 if (d->getTLSKind())
1251 setTLSMode(gv, *d);
1252
1253 setGVProperties(gv, d);
1254
1255 // If required by the ABI, treat declarations of static data members with
1256 // inline initializers as definitions.
1257 if (astContext.isMSStaticDataMemberInlineDefinition(d))
1259 "getOrCreateCIRGlobal: MS static data member inline definition");
1260
1261 // Emit section information for extern variables.
1262 if (d->hasExternalStorage()) {
1263 if (const SectionAttr *sa = d->getAttr<SectionAttr>())
1264 gv.setSectionAttr(builder.getStringAttr(sa->getName()));
1265 }
1266
1267 // Handle XCore specific ABI requirements.
1268 if (getTriple().getArch() == llvm::Triple::xcore)
1270 "getOrCreateCIRGlobal: XCore specific ABI requirements");
1271
1272 // Check if we a have a const declaration with an initializer, we may be
1273 // able to emit it as available_externally to expose it's value to the
1274 // optimizer.
1275 if (getLangOpts().CPlusPlus && gv.isPublic() &&
1276 d->getType().isConstQualified() && gv.isDeclaration() &&
1277 !d->hasDefinition() && d->hasInit() && !d->hasAttr<DLLImportAttr>())
1278 errorNYI(
1279 d->getSourceRange(),
1280 "getOrCreateCIRGlobal: external const declaration with initializer");
1281 }
1282
1283 if (d &&
1286 // TODO(cir): set target attributes
1287 // External HIP managed variables needed to be recorded for transformation
1288 // in both device and host compilations.
1289 if (getLangOpts().CUDA && d && d->hasAttr<HIPManagedAttr>() &&
1290 d->hasExternalStorage())
1292 "getOrCreateCIRGlobal: HIP managed attribute");
1293 }
1294
1296 return gv;
1297}
1298
1299cir::GlobalOp
1301 ForDefinition_t isForDefinition) {
1302 assert(d->hasGlobalStorage() && "Not a global variable");
1303 QualType astTy = d->getType();
1304 if (!ty)
1305 ty = getTypes().convertTypeForMem(astTy);
1306
1307 StringRef mangledName = getMangledName(d);
1308 return getOrCreateCIRGlobal(mangledName, ty, getGlobalVarAddressSpace(d), d,
1309 isForDefinition);
1310}
1311
1312/// Return the mlir::Value for the address of the given global variable. If
1313/// \p ty is non-null and if the global doesn't exist, then it will be created
1314/// with the specified type instead of whatever the normal requested type would
1315/// be. If \p isForDefinition is true, it is guaranteed that an actual global
1316/// with type \p ty will be returned, not conversion of a variable with the same
1317/// mangled name but some other type.
1318mlir::Value CIRGenModule::getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty,
1319 ForDefinition_t isForDefinition) {
1320 assert(d->hasGlobalStorage() && "Not a global variable");
1321 QualType astTy = d->getType();
1322 if (!ty)
1323 ty = getTypes().convertTypeForMem(astTy);
1324
1325 bool tlsAccess = d->getTLSKind() != VarDecl::TLS_None;
1326 cir::GlobalOp g = getOrCreateCIRGlobal(d, ty, isForDefinition);
1327 mlir::Type ptrTy = builder.getPointerTo(g.getSymType(), g.getAddrSpaceAttr());
1328 return cir::GetGlobalOp::create(
1329 builder, getLoc(d->getSourceRange()), ptrTy, g.getSymNameAttr(),
1330 tlsAccess,
1331 /*static_local=*/g.getStaticLocalGuard().has_value());
1332}
1333
1334cir::GlobalViewAttr CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *d) {
1335 assert(d->hasGlobalStorage() && "Not a global variable");
1336 mlir::Type ty = getTypes().convertTypeForMem(d->getType());
1337
1338 cir::GlobalOp globalOp = getOrCreateCIRGlobal(d, ty, NotForDefinition);
1339 cir::PointerType ptrTy =
1340 builder.getPointerTo(globalOp.getSymType(), globalOp.getAddrSpaceAttr());
1341 return builder.getGlobalViewAttr(ptrTy, globalOp);
1342}
1343
1344void CIRGenModule::addUsedGlobal(cir::CIRGlobalValueInterface gv) {
1345 assert((mlir::isa<cir::FuncOp>(gv.getOperation()) ||
1346 !gv.isDeclarationForLinker()) &&
1347 "Only globals with definition can force usage.");
1348 llvmUsed.emplace_back(gv);
1349}
1350
1351void CIRGenModule::addCompilerUsedGlobal(cir::CIRGlobalValueInterface gv) {
1352 assert(!gv.isDeclarationForLinker() &&
1353 "Only globals with definition can force usage.");
1354 llvmCompilerUsed.emplace_back(gv);
1355}
1356
1358 cir::CIRGlobalValueInterface gv) {
1359 assert((mlir::isa<cir::FuncOp>(gv.getOperation()) ||
1360 !gv.isDeclarationForLinker()) &&
1361 "Only globals with definition can force usage.");
1362 if (getTriple().isOSBinFormatELF())
1363 llvmCompilerUsed.emplace_back(gv);
1364 else
1365 llvmUsed.emplace_back(gv);
1366}
1367
1368static void emitUsed(CIRGenModule &cgm, StringRef name,
1369 std::vector<cir::CIRGlobalValueInterface> &list) {
1370 if (list.empty())
1371 return;
1372
1373 CIRGenBuilderTy &builder = cgm.getBuilder();
1374 mlir::Location loc = builder.getUnknownLoc();
1376 usedArray.resize(list.size());
1377 for (auto [i, op] : llvm::enumerate(list)) {
1378 usedArray[i] = cir::GlobalViewAttr::get(
1379 cgm.voidPtrTy, mlir::FlatSymbolRefAttr::get(op.getNameAttr()));
1380 }
1381
1382 cir::ArrayType arrayTy = cir::ArrayType::get(cgm.voidPtrTy, usedArray.size());
1383
1384 cir::ConstArrayAttr initAttr = cir::ConstArrayAttr::get(
1385 arrayTy, mlir::ArrayAttr::get(&cgm.getMLIRContext(), usedArray));
1386
1387 cir::GlobalOp gv = cgm.createGlobalOp(loc, name, arrayTy,
1388 /*isConstant=*/false);
1389 gv.setLinkage(cir::GlobalLinkageKind::AppendingLinkage);
1390 gv.setInitialValueAttr(initAttr);
1391 gv.setSectionAttr(builder.getStringAttr("llvm.metadata"));
1392}
1393
1395 emitUsed(*this, "llvm.used", llvmUsed);
1396 emitUsed(*this, "llvm.compiler.used", llvmCompilerUsed);
1397}
1398
1400 bool isTentative) {
1401 if (getLangOpts().OpenCL || getLangOpts().OpenMPIsTargetDevice) {
1403 "emitGlobalVarDefinition: emit OpenCL/OpenMP global variable");
1404 return;
1405 }
1406
1407 // Whether the definition of the variable is available externally.
1408 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
1409 // since this is the job for its original source.
1410 bool isDefinitionAvailableExternally =
1411 astContext.GetGVALinkageForVariable(vd) == GVA_AvailableExternally;
1412
1413 // It is useless to emit the definition for an available_externally variable
1414 // which can't be marked as const.
1415 if (isDefinitionAvailableExternally &&
1416 (!vd->hasConstantInitialization() ||
1417 // TODO: Update this when we have interface to check constexpr
1418 // destructor.
1419 vd->needsDestruction(astContext) ||
1420 !vd->getType().isConstantStorage(astContext, true, true)))
1421 return;
1422
1423 mlir::Attribute init;
1424 bool needsGlobalCtor = false;
1425 bool needsGlobalDtor =
1426 !isDefinitionAvailableExternally &&
1428 const VarDecl *initDecl;
1429 const Expr *initExpr = vd->getAnyInitializer(initDecl);
1430
1431 std::optional<ConstantEmitter> emitter;
1432
1433 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
1434 // as part of their declaration." Sema has already checked for
1435 // error cases, so we just need to set Init to PoisonValue.
1436 bool isCUDASharedVar =
1437 getLangOpts().CUDAIsDevice && vd->hasAttr<CUDASharedAttr>();
1438 // Shadows of initialized device-side global variables are also left
1439 // undefined.
1440 // Managed Variables should be initialized on both host side and device side.
1441 bool isCUDAShadowVar =
1442 !getLangOpts().CUDAIsDevice && !vd->hasAttr<HIPManagedAttr>() &&
1443 (vd->hasAttr<CUDAConstantAttr>() || vd->hasAttr<CUDADeviceAttr>() ||
1444 vd->hasAttr<CUDASharedAttr>());
1445 bool isCUDADeviceShadowVar =
1446 getLangOpts().CUDAIsDevice && !vd->hasAttr<HIPManagedAttr>() &&
1449
1450 if (getLangOpts().CUDA &&
1451 (isCUDASharedVar || isCUDAShadowVar || isCUDADeviceShadowVar)) {
1452 init = cir::PoisonAttr::get(convertType(vd->getType()));
1453 } else if (vd->hasAttr<LoaderUninitializedAttr>()) {
1455 "emitGlobalVarDefinition: loader uninitialized attribute");
1456 } else if (!initExpr) {
1457 // This is a tentative definition; tentative definitions are
1458 // implicitly initialized with { 0 }.
1459 //
1460 // Note that tentative definitions are only emitted at the end of
1461 // a translation unit, so they should never have incomplete
1462 // type. In addition, EmitTentativeDefinition makes sure that we
1463 // never attempt to emit a tentative definition if a real one
1464 // exists. A use may still exists, however, so we still may need
1465 // to do a RAUW.
1466 assert(!vd->getType()->isIncompleteType() && "Unexpected incomplete type");
1467 init = builder.getZeroInitAttr(convertType(vd->getType()));
1468 } else {
1469 emitter.emplace(*this);
1470 mlir::Attribute initializer = emitter->tryEmitForInitializer(*initDecl);
1471 if (!initializer) {
1472 QualType qt = initExpr->getType();
1473 if (vd->getType()->isReferenceType())
1474 qt = vd->getType();
1475
1476 if (getLangOpts().CPlusPlus) {
1477 if (initDecl->hasFlexibleArrayInit(astContext))
1479 "emitGlobalVarDefinition: flexible array initializer");
1480 init = builder.getZeroInitAttr(convertType(qt));
1481 if (!isDefinitionAvailableExternally)
1482 needsGlobalCtor = true;
1483 } else {
1485 "emitGlobalVarDefinition: static initializer");
1486 }
1487 } else {
1488 init = initializer;
1489 // We don't need an initializer, so remove the entry for the delayed
1490 // initializer position (just in case this entry was delayed) if we
1491 // also don't need to register a destructor.
1493 }
1494 }
1495
1496 mlir::Type initType;
1497 if (mlir::isa<mlir::SymbolRefAttr>(init)) {
1498 errorNYI(
1499 vd->getSourceRange(),
1500 "emitGlobalVarDefinition: global initializer is a symbol reference");
1501 return;
1502 } else {
1503 assert(mlir::isa<mlir::TypedAttr>(init) && "This should have a type");
1504 auto typedInitAttr = mlir::cast<mlir::TypedAttr>(init);
1505 initType = typedInitAttr.getType();
1506 }
1507 assert(!mlir::isa<mlir::NoneType>(initType) && "Should have a type by now");
1508
1509 cir::GlobalOp gv =
1510 getOrCreateCIRGlobal(vd, initType, ForDefinition_t(!isTentative));
1511 // TODO(cir): Strip off pointer casts from Entry if we get them?
1512
1513 if (!gv || gv.getSymType() != initType) {
1515 "emitGlobalVarDefinition: global initializer with type mismatch");
1516 return;
1517 }
1518
1520
1521 if (vd->hasAttr<AnnotateAttr>())
1522 addGlobalAnnotations(vd, gv);
1523
1524 // Set CIR's linkage type as appropriate.
1525 cir::GlobalLinkageKind linkage = getCIRLinkageVarDefinition(vd);
1526
1527 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
1528 // the device. [...]"
1529 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
1530 // __device__, declares a variable that: [...]
1531 // Is accessible from all the threads within the grid and from the host
1532 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
1533 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
1534 if (langOpts.CUDA) {
1535 if (langOpts.CUDAIsDevice) {
1536 // __shared__ variables is not marked as externally initialized,
1537 // because they must not be initialized.
1538 if (linkage != cir::GlobalLinkageKind::InternalLinkage &&
1539 !vd->isConstexpr() && !vd->getType().isConstQualified() &&
1540 (vd->hasAttr<CUDADeviceAttr>() || vd->hasAttr<CUDAConstantAttr>() ||
1543 gv->setAttr(cir::CUDAExternallyInitializedAttr::getMnemonic(),
1544 cir::CUDAExternallyInitializedAttr::get(&getMLIRContext()));
1545 }
1546 } else {
1547 // Adjust linkage of shadow variables in host compilation
1549 }
1551 }
1552
1553 // Set initializer and finalize emission
1555 if (emitter)
1556 emitter->finalize(gv);
1557
1558 // If it is safe to mark the global 'constant', do so now.
1559 // Use the same logic as classic codegen EmitGlobalVarDefinition.
1560 gv.setConstant((vd->hasAttr<CUDAConstantAttr>() && langOpts.CUDAIsDevice) ||
1561 (!needsGlobalCtor && !needsGlobalDtor &&
1562 vd->getType().isConstantStorage(astContext,
1563 /*ExcludeCtor=*/true,
1564 /*ExcludeDtor=*/true)));
1565 // If it is in a read-only section, mark it 'constant'.
1566 if (const SectionAttr *sa = vd->getAttr<SectionAttr>()) {
1567 const ASTContext::SectionInfo &si = astContext.SectionInfos[sa->getName()];
1568 if ((si.SectionFlags & ASTContext::PSF_Write) == 0)
1569 gv.setConstant(true);
1570 }
1571
1572 // Set CIR linkage and DLL storage class.
1573 gv.setLinkage(linkage);
1574 // FIXME(cir): setLinkage should likely set MLIR's visibility automatically.
1575 gv.setVisibility(getMLIRVisibilityFromCIRLinkage(linkage));
1577 if (linkage == cir::GlobalLinkageKind::CommonLinkage) {
1578 // common vars aren't constant even if declared const.
1579 gv.setConstant(false);
1580 // Tentative definition of global variables may be initialized with
1581 // non-zero null pointers. In this case they should have weak linkage
1582 // since common linkage must have zero initializer and must not have
1583 // explicit section therefore cannot have non-zero initial value.
1584 std::optional<mlir::Attribute> initializer = gv.getInitialValue();
1585 if (initializer && !getBuilder().isNullValue(*initializer))
1586 gv.setLinkage(cir::GlobalLinkageKind::WeakAnyLinkage);
1587 }
1588
1589 setNonAliasAttributes(vd, gv);
1590
1591 if (vd->getTLSKind() && !vd->isStaticLocal())
1592 setTLSMode(gv, *vd);
1593
1594 maybeSetTrivialComdat(*vd, gv);
1595
1596 // Emit the initializer function if necessary.
1597 if (needsGlobalCtor || needsGlobalDtor)
1598 emitCXXGlobalVarDeclInitFunc(vd, gv, needsGlobalCtor);
1599}
1600
1602 mlir::Operation *op) {
1603 const auto *decl = cast<ValueDecl>(gd.getDecl());
1604 if (const auto *fd = dyn_cast<FunctionDecl>(decl)) {
1605 // TODO(CIR): Skip generation of CIR for functions with available_externally
1606 // linkage at -O0.
1607
1608 if (const auto *method = dyn_cast<CXXMethodDecl>(decl)) {
1609 // Make sure to emit the definition(s) before we emit the thunks. This is
1610 // necessary for the generation of certain thunks.
1611 if (isa<CXXConstructorDecl>(method) || isa<CXXDestructorDecl>(method))
1612 abi->emitCXXStructor(gd);
1613 else if (fd->isMultiVersion())
1614 errorNYI(method->getSourceRange(), "multiversion functions");
1615 else
1617
1618 if (method->isVirtual())
1619 getVTables().emitThunks(gd);
1620
1621 return;
1622 }
1623
1624 if (fd->isMultiVersion())
1625 errorNYI(fd->getSourceRange(), "multiversion functions");
1627 return;
1628 }
1629
1630 if (const auto *vd = dyn_cast<VarDecl>(decl))
1631 return emitGlobalVarDefinition(vd, !vd->hasDefinition());
1632
1633 llvm_unreachable("Invalid argument to CIRGenModule::emitGlobalDefinition");
1634}
1635
1636mlir::Attribute
1638 assert(!e->getType()->isPointerType() && "Strings are always arrays");
1639
1640 // Don't emit it as the address of the string, emit the string data itself
1641 // as an inline array.
1642 if (e->getCharByteWidth() == 1) {
1643 SmallString<64> str(e->getString());
1644
1645 // Resize the string to the right size, which is indicated by its type.
1646 const ConstantArrayType *cat =
1647 astContext.getAsConstantArrayType(e->getType());
1648 uint64_t finalSize = cat->getZExtSize();
1649 str.resize(finalSize);
1650
1651 mlir::Type eltTy = convertType(cat->getElementType());
1652 return builder.getString(str, eltTy, finalSize, /*ensureNullTerm=*/false);
1653 }
1654
1655 auto arrayTy = mlir::cast<cir::ArrayType>(convertType(e->getType()));
1656
1657 auto arrayEltTy = mlir::cast<cir::IntType>(arrayTy.getElementType());
1658
1659 uint64_t arraySize = arrayTy.getSize();
1660 unsigned literalSize = e->getLength();
1661 assert(arraySize > literalSize &&
1662 "wide string literal array size must have room for null terminator?");
1663
1664 // Check if the string is all null bytes before building the vector.
1665 // In most non-zero cases, this will break out on the first element.
1666 bool isAllZero = true;
1667 for (unsigned i = 0; i < literalSize; ++i) {
1668 if (e->getCodeUnit(i) != 0) {
1669 isAllZero = false;
1670 break;
1671 }
1672 }
1673
1674 if (isAllZero)
1675 return cir::ZeroAttr::get(arrayTy);
1676
1677 // Otherwise emit a constant array holding the characters.
1679 elements.reserve(arraySize);
1680 for (unsigned i = 0; i < literalSize; ++i)
1681 elements.push_back(cir::IntAttr::get(arrayEltTy, e->getCodeUnit(i)));
1682
1683 auto elementsAttr = mlir::ArrayAttr::get(&getMLIRContext(), elements);
1684 return builder.getConstArray(elementsAttr, arrayTy);
1685}
1686
1688 return getTriple().supportsCOMDAT();
1689}
1690
1691static bool shouldBeInCOMDAT(CIRGenModule &cgm, const Decl &d) {
1692 if (!cgm.supportsCOMDAT())
1693 return false;
1694
1695 if (d.hasAttr<SelectAnyAttr>())
1696 return true;
1697
1698 GVALinkage linkage;
1699 if (auto *vd = dyn_cast<VarDecl>(&d))
1700 linkage = cgm.getASTContext().GetGVALinkageForVariable(vd);
1701 else
1702 linkage =
1704
1705 switch (linkage) {
1709 return false;
1712 return true;
1713 }
1714 llvm_unreachable("No such linkage");
1715}
1716
1717void CIRGenModule::maybeSetTrivialComdat(const Decl &d, mlir::Operation *op) {
1718 if (!shouldBeInCOMDAT(*this, d))
1719 return;
1720 if (auto globalOp = dyn_cast_or_null<cir::GlobalOp>(op)) {
1721 globalOp.setComdat(true);
1722 } else {
1723 auto funcOp = cast<cir::FuncOp>(op);
1724 funcOp.setComdat(true);
1725 }
1726}
1727
1729 // Make sure that this type is translated.
1730 genTypes.updateCompletedType(td);
1731}
1732
1733void CIRGenModule::addReplacement(StringRef name, mlir::Operation *op) {
1734 replacements[name] = op;
1735}
1736
1737#ifndef NDEBUG
1738static bool verifyPointerTypeArgs(cir::FuncOp oldF, cir::FuncOp newF,
1739 mlir::SymbolUserMap &userMap) {
1740 for (mlir::Operation *user : userMap.getUsers(oldF)) {
1741 auto call = mlir::dyn_cast<cir::CallOp>(user);
1742 if (!call)
1743 continue;
1744
1745 for (auto [argOp, fnArgType] :
1746 llvm::zip(call.getArgs(), newF.getFunctionType().getInputs())) {
1747 if (argOp.getType() != fnArgType)
1748 return false;
1749 }
1750 }
1751
1752 return true;
1753}
1754#endif // NDEBUG
1755
1756void CIRGenModule::applyReplacements() {
1757 if (replacements.empty())
1758 return;
1759
1760 // Build a symbol user map once — this walks the module O(M) one time.
1761 // Previously, each replaceAllSymbolUses call walked the entire module,
1762 // giving O(R × M) quadratic behavior for R replacements.
1763 mlir::SymbolTableCollection symbolTableCollection;
1764 mlir::SymbolUserMap userMap(symbolTableCollection, theModule);
1765
1766 for (auto &i : replacements) {
1767 StringRef mangledName = i.first;
1768 mlir::Operation *replacement = i.second;
1769 mlir::Operation *entry = getGlobalValue(mangledName);
1770 if (!entry)
1771 continue;
1772 assert(isa<cir::FuncOp>(entry) && "expected function");
1773 auto oldF = cast<cir::FuncOp>(entry);
1774 auto newF = dyn_cast<cir::FuncOp>(replacement);
1775 if (!newF) {
1776 // In classic codegen, this can be a global alias, a bitcast, or a GEP.
1777 errorNYI(replacement->getLoc(), "replacement is not a function");
1778 continue;
1779 }
1780
1781 assert(verifyPointerTypeArgs(oldF, newF, userMap) &&
1782 "call argument types do not match replacement function");
1783
1784 // Replace old with new, but keep the old order. Uses
1785 // SymbolUserMap to touch only actual users, not the whole module.
1786 userMap.replaceAllUsesWith(oldF, newF.getSymNameAttr());
1787 newF->moveBefore(oldF);
1788 eraseGlobalSymbol(oldF);
1789 oldF->erase();
1790 }
1791}
1792
1794 mlir::Location loc, StringRef name, mlir::Type ty,
1795 cir::GlobalLinkageKind linkage, clang::CharUnits alignment) {
1796 auto gv = mlir::dyn_cast_or_null<cir::GlobalOp>(getGlobalValue(name));
1797
1798 if (gv) {
1799 // Check if the variable has the right type.
1800 if (gv.getSymType() == ty)
1801 return gv;
1802
1803 // Because of C++ name mangling, the only way we can end up with an already
1804 // existing global with the same name is if it has been declared extern
1805 // "C".
1806 assert(gv.isDeclaration() && "Declaration has wrong type!");
1807
1808 errorNYI(loc, "createOrReplaceCXXRuntimeVariable: declaration exists with "
1809 "wrong type");
1810 return gv;
1811 }
1812
1813 // Create a new variable.
1814 gv = createGlobalOp(loc, name, ty);
1815
1816 // Set up extra information and add to the module
1817 gv.setLinkageAttr(
1818 cir::GlobalLinkageKindAttr::get(&getMLIRContext(), linkage));
1819 mlir::SymbolTable::setSymbolVisibility(gv,
1821
1822 if (supportsCOMDAT() && cir::isWeakForLinker(linkage) &&
1823 !gv.hasAvailableExternallyLinkage()) {
1824 gv.setComdat(true);
1825 }
1826
1827 gv.setAlignmentAttr(getSize(alignment));
1828 setDSOLocal(static_cast<mlir::Operation *>(gv));
1829 return gv;
1830}
1831
1832// TODO(CIR): this could be a common method between LLVM codegen.
1833static bool isVarDeclStrongDefinition(const ASTContext &astContext,
1834 CIRGenModule &cgm, const VarDecl *vd,
1835 bool noCommon) {
1836 // Don't give variables common linkage if -fno-common was specified unless it
1837 // was overridden by a NoCommon attribute.
1838 if ((noCommon || vd->hasAttr<NoCommonAttr>()) && !vd->hasAttr<CommonAttr>())
1839 return true;
1840
1841 // C11 6.9.2/2:
1842 // A declaration of an identifier for an object that has file scope without
1843 // an initializer, and without a storage-class specifier or with the
1844 // storage-class specifier static, constitutes a tentative definition.
1845 if (vd->getInit() || vd->hasExternalStorage())
1846 return true;
1847
1848 // A variable cannot be both common and exist in a section.
1849 if (vd->hasAttr<SectionAttr>())
1850 return true;
1851
1852 // A variable cannot be both common and exist in a section.
1853 // We don't try to determine which is the right section in the front-end.
1854 // If no specialized section name is applicable, it will resort to default.
1855 if (vd->hasAttr<PragmaClangBSSSectionAttr>() ||
1856 vd->hasAttr<PragmaClangDataSectionAttr>() ||
1857 vd->hasAttr<PragmaClangRelroSectionAttr>() ||
1858 vd->hasAttr<PragmaClangRodataSectionAttr>())
1859 return true;
1860
1861 // Thread local vars aren't considered common linkage.
1862 if (vd->getTLSKind())
1863 return true;
1864
1865 // Tentative definitions marked with WeakImportAttr are true definitions.
1866 if (vd->hasAttr<WeakImportAttr>())
1867 return true;
1868
1869 // A variable cannot be both common and exist in a comdat.
1870 if (shouldBeInCOMDAT(cgm, *vd))
1871 return true;
1872
1873 // Declarations with a required alignment do not have common linkage in MSVC
1874 // mode.
1875 if (astContext.getTargetInfo().getCXXABI().isMicrosoft()) {
1876 if (vd->hasAttr<AlignedAttr>())
1877 return true;
1878 QualType varType = vd->getType();
1879 if (astContext.isAlignmentRequired(varType))
1880 return true;
1881
1882 if (const auto *rd = varType->getAsRecordDecl()) {
1883 for (const FieldDecl *fd : rd->fields()) {
1884 if (fd->isBitField())
1885 continue;
1886 if (fd->hasAttr<AlignedAttr>())
1887 return true;
1888 if (astContext.isAlignmentRequired(fd->getType()))
1889 return true;
1890 }
1891 }
1892 }
1893
1894 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
1895 // common symbols, so symbols with greater alignment requirements cannot be
1896 // common.
1897 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
1898 // alignments for common symbols via the aligncomm directive, so this
1899 // restriction only applies to MSVC environments.
1900 if (astContext.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
1901 astContext.getTypeAlignIfKnown(vd->getType()) >
1902 astContext.toBits(CharUnits::fromQuantity(32)))
1903 return true;
1904
1905 return false;
1906}
1907
1908cir::GlobalLinkageKind
1910 GVALinkage linkage) {
1911 if (linkage == GVA_Internal)
1912 return cir::GlobalLinkageKind::InternalLinkage;
1913
1914 if (dd->hasAttr<WeakAttr>())
1915 return cir::GlobalLinkageKind::WeakAnyLinkage;
1916
1917 if (const auto *fd = dd->getAsFunction())
1918 if (fd->isMultiVersion() && linkage == GVA_AvailableExternally)
1919 return cir::GlobalLinkageKind::LinkOnceAnyLinkage;
1920
1921 // We are guaranteed to have a strong definition somewhere else,
1922 // so we can use available_externally linkage.
1923 if (linkage == GVA_AvailableExternally)
1924 return cir::GlobalLinkageKind::AvailableExternallyLinkage;
1925
1926 // Note that Apple's kernel linker doesn't support symbol
1927 // coalescing, so we need to avoid linkonce and weak linkages there.
1928 // Normally, this means we just map to internal, but for explicit
1929 // instantiations we'll map to external.
1930
1931 // In C++, the compiler has to emit a definition in every translation unit
1932 // that references the function. We should use linkonce_odr because
1933 // a) if all references in this translation unit are optimized away, we
1934 // don't need to codegen it. b) if the function persists, it needs to be
1935 // merged with other definitions. c) C++ has the ODR, so we know the
1936 // definition is dependable.
1937 if (linkage == GVA_DiscardableODR)
1938 return !astContext.getLangOpts().AppleKext
1939 ? cir::GlobalLinkageKind::LinkOnceODRLinkage
1940 : cir::GlobalLinkageKind::InternalLinkage;
1941
1942 // An explicit instantiation of a template has weak linkage, since
1943 // explicit instantiations can occur in multiple translation units
1944 // and must all be equivalent. However, we are not allowed to
1945 // throw away these explicit instantiations.
1946 //
1947 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
1948 // so say that CUDA templates are either external (for kernels) or internal.
1949 // This lets llvm perform aggressive inter-procedural optimizations. For
1950 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
1951 // therefore we need to follow the normal linkage paradigm.
1952 if (linkage == GVA_StrongODR) {
1953 if (getLangOpts().AppleKext)
1954 return cir::GlobalLinkageKind::ExternalLinkage;
1955 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
1956 !getLangOpts().GPURelocatableDeviceCode)
1957 return dd->hasAttr<CUDAGlobalAttr>()
1958 ? cir::GlobalLinkageKind::ExternalLinkage
1959 : cir::GlobalLinkageKind::InternalLinkage;
1960 return cir::GlobalLinkageKind::WeakODRLinkage;
1961 }
1962
1963 // C++ doesn't have tentative definitions and thus cannot have common
1964 // linkage.
1965 if (!getLangOpts().CPlusPlus && isa<VarDecl>(dd) &&
1966 !isVarDeclStrongDefinition(astContext, *this, cast<VarDecl>(dd),
1967 getCodeGenOpts().NoCommon))
1968 return cir::GlobalLinkageKind::CommonLinkage;
1969
1970 // selectany symbols are externally visible, so use weak instead of
1971 // linkonce. MSVC optimizes away references to const selectany globals, so
1972 // all definitions should be the same and ODR linkage should be used.
1973 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
1974 if (dd->hasAttr<SelectAnyAttr>())
1975 return cir::GlobalLinkageKind::WeakODRLinkage;
1976
1977 // Otherwise, we have strong external linkage.
1978 assert(linkage == GVA_StrongExternal);
1979 return cir::GlobalLinkageKind::ExternalLinkage;
1980}
1981
1982/// This function is called when we implement a function with no prototype, e.g.
1983/// "int foo() {}". If there are existing call uses of the old function in the
1984/// module, this adjusts them to call the new function directly.
1985///
1986/// This is not just a cleanup: the always_inline pass requires direct calls to
1987/// functions to be able to inline them. If there is a bitcast in the way, it
1988/// won't inline them. Instcombine normally deletes these calls, but it isn't
1989/// run at -O0.
1991 mlir::Operation *old, cir::FuncOp newFn) {
1992 // If we're redefining a global as a function, don't transform it.
1993 auto oldFn = mlir::dyn_cast<cir::FuncOp>(old);
1994 if (!oldFn)
1995 return;
1996
1997 // TODO(cir): this RAUW ignores the features below.
2001 if (oldFn->getAttrs().size() <= 1)
2002 errorNYI(old->getLoc(),
2003 "replaceUsesOfNonProtoTypeWithRealFunction: Attribute forwarding");
2004
2005 // Mark new function as originated from a no-proto declaration.
2006 newFn.setNoProto(oldFn.getNoProto());
2007
2008 // Iterate through all calls of the no-proto function.
2009 std::optional<mlir::SymbolTable::UseRange> symUses =
2010 oldFn.getSymbolUses(oldFn->getParentOp());
2011 for (const mlir::SymbolTable::SymbolUse &use : symUses.value()) {
2012 mlir::OpBuilder::InsertionGuard guard(builder);
2013
2014 if (auto noProtoCallOp = mlir::dyn_cast<cir::CallOp>(use.getUser())) {
2015 builder.setInsertionPoint(noProtoCallOp);
2016
2017 // Patch call type with the real function type.
2018 cir::FuncType newFnType = newFn.getFunctionType();
2019 mlir::OperandRange callOperands = noProtoCallOp.getOperands();
2020 bool returnTypeMatches =
2021 newFnType.hasVoidReturn()
2022 ? noProtoCallOp.getNumResults() == 0
2023 : noProtoCallOp.getNumResults() == 1 &&
2024 noProtoCallOp.getResultTypes().front() ==
2025 newFnType.getReturnType();
2026 bool typesMatch = !newFn.getNoProto() && returnTypeMatches &&
2027 callOperands.size() == newFnType.getNumInputs();
2028 for (unsigned i = 0, e = newFnType.getNumInputs(); typesMatch && i != e;
2029 ++i) {
2030 if (callOperands[i].getType() != newFnType.getInput(i))
2031 typesMatch = false;
2032 }
2033
2034 cir::CallOp realCallOp;
2035 if (typesMatch) {
2036 // Patch call type with the real function type.
2037 realCallOp =
2038 builder.createCallOp(noProtoCallOp.getLoc(), newFn, callOperands);
2039 } else {
2040 // Build an indirect call whose function-pointer signature matches
2041 // the existing call site.
2042 cir::FuncType origFnType = oldFn.getFunctionType();
2043 cir::FuncType callFnType =
2044 origFnType.isVarArg()
2045 ? cir::FuncType::get(origFnType.getInputs(),
2046 origFnType.getReturnType(),
2047 /*isVarArg=*/false)
2048 : origFnType;
2049 mlir::Value addr = cir::GetGlobalOp::create(
2050 builder, noProtoCallOp.getLoc(), cir::PointerType::get(newFnType),
2051 newFn.getSymName());
2052 mlir::Value casted =
2053 builder.createBitcast(addr, cir::PointerType::get(callFnType));
2054 realCallOp = builder.createIndirectCallOp(
2055 noProtoCallOp.getLoc(), casted, callFnType, callOperands);
2056 }
2057
2058 // Replace old no proto call with fixed call.
2059 noProtoCallOp.replaceAllUsesWith(realCallOp);
2060 noProtoCallOp.erase();
2061 } else if (auto getGlobalOp =
2062 mlir::dyn_cast<cir::GetGlobalOp>(use.getUser())) {
2063 // The GetGlobal was emitted with the no-proto FuncType. Uses of this
2064 // operation (cir.store, cir.cast) were built for that pointer type. When
2065 // we re-type the result to the real FuncType, we need to add a bit the
2066 // old pointer type so those uses are still valid. This can lead to
2067 // some redundant bitcast chains, but those will be cleaned up by the
2068 // canonicalizer.
2069 mlir::Value res = getGlobalOp.getAddr();
2070 const mlir::Type oldResTy = res.getType();
2071 const auto newPtrTy = cir::PointerType::get(newFn.getFunctionType());
2072 if (oldResTy != newPtrTy) {
2073 res.setType(newPtrTy);
2074 builder.setInsertionPointAfter(getGlobalOp.getOperation());
2075 mlir::Value castRes =
2076 cir::CastOp::create(builder, getGlobalOp.getLoc(), oldResTy,
2077 cir::CastKind::bitcast, res);
2078 res.replaceAllUsesExcept(castRes, castRes.getDefiningOp());
2079 }
2080 } else if (mlir::isa<cir::GlobalOp>(use.getUser())) {
2081 // Function addresses in global initializers use GlobalViewAttrs typed to
2082 // the initializer context (e.g. struct field type), not the FuncOp type,
2083 // so no update is required when the no-proto FuncOp is replaced.
2084 } else {
2085 llvm_unreachable(
2086 "replaceUsesOfNonProtoTypeWithRealFunction: unexpected use type");
2087 }
2088 }
2089}
2090
2091cir::GlobalLinkageKind
2093 GVALinkage linkage = astContext.GetGVALinkageForVariable(vd);
2094 return getCIRLinkageForDeclarator(vd, linkage);
2095}
2096
2098 const auto *d = cast<FunctionDecl>(gd.getDecl());
2099
2100 GVALinkage linkage = astContext.GetGVALinkageForFunction(d);
2101
2102 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(d))
2103 return getCXXABI().getCXXDestructorLinkage(linkage, dtor, gd.getDtorType());
2104
2105 return getCIRLinkageForDeclarator(d, linkage);
2106}
2107
2108static cir::GlobalOp
2109generateStringLiteral(mlir::Location loc, mlir::TypedAttr c,
2110 cir::GlobalLinkageKind lt, CIRGenModule &cgm,
2111 StringRef globalName, CharUnits alignment) {
2113
2114 // Create a global variable for this string
2115 // FIXME(cir): check for insertion point in module level.
2116 cir::GlobalOp gv = cgm.createGlobalOp(loc, globalName, c.getType(),
2117 !cgm.getLangOpts().WritableStrings);
2118
2119 // Set up extra information and add to the module
2120 gv.setAlignmentAttr(cgm.getSize(alignment));
2121 gv.setLinkageAttr(
2122 cir::GlobalLinkageKindAttr::get(cgm.getBuilder().getContext(), lt));
2126 if (gv.isWeakForLinker()) {
2127 assert(cgm.supportsCOMDAT() && "Only COFF uses weak string literals");
2128 gv.setComdat(true);
2129 }
2130 cgm.setDSOLocal(static_cast<mlir::Operation *>(gv));
2131 return gv;
2132}
2133
2134// LLVM IR automatically uniques names when new llvm::GlobalVariables are
2135// created. This is handy, for example, when creating globals for string
2136// literals. Since we don't do that when creating cir::GlobalOp's, we need
2137// a mechanism to generate a unique name in advance.
2138//
2139// For now, this mechanism is only used in cases where we know that the
2140// name is compiler-generated, so we don't use the MLIR symbol table for
2141// the lookup.
2142std::string CIRGenModule::getUniqueGlobalName(const std::string &baseName) {
2143 // If this is the first time we've generated a name for this basename, use
2144 // it as is and start a counter for this base name.
2145 auto it = cgGlobalNames.find(baseName);
2146 if (it == cgGlobalNames.end()) {
2147 cgGlobalNames[baseName] = 1;
2148 return baseName;
2149 }
2150
2151 std::string result =
2152 baseName + "." + std::to_string(cgGlobalNames[baseName]++);
2153 // There should not be any symbol with this name in the module.
2154 assert(!getGlobalValue(result));
2155 return result;
2156}
2157
2158/// Return a pointer to a constant array for the given string literal.
2160 StringRef name) {
2161 CharUnits alignment =
2162 astContext.getAlignOfGlobalVarInChars(s->getType(), /*VD=*/nullptr);
2163
2164 mlir::Attribute c = getConstantArrayFromStringLiteral(s);
2165
2166 cir::GlobalOp gv;
2167 if (!getLangOpts().WritableStrings && constantStringMap.count(c)) {
2168 gv = constantStringMap[c];
2169 // The bigger alignment always wins.
2170 if (!gv.getAlignment() ||
2171 uint64_t(alignment.getQuantity()) > *gv.getAlignment())
2172 gv.setAlignmentAttr(getSize(alignment));
2173 } else {
2174 // Mangle the string literal if that's how the ABI merges duplicate strings.
2175 // Don't do it if they are writable, since we don't want writes in one TU to
2176 // affect strings in another.
2177 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(s) &&
2178 !getLangOpts().WritableStrings) {
2180 "getGlobalForStringLiteral: mangle string literals");
2181 }
2182
2183 // Unlike LLVM IR, CIR doesn't automatically unique names for globals, so
2184 // we need to do that explicitly.
2185 std::string uniqueName = getUniqueGlobalName(name.str());
2186 // Synthetic string literals (e.g., from SourceLocExpr) may not have valid
2187 // source locations. Use unknown location in those cases.
2188 mlir::Location loc = s->getBeginLoc().isValid()
2189 ? getLoc(s->getSourceRange())
2190 : builder.getUnknownLoc();
2191 auto typedC = llvm::cast<mlir::TypedAttr>(c);
2192 gv = generateStringLiteral(loc, typedC,
2193 cir::GlobalLinkageKind::PrivateLinkage, *this,
2194 uniqueName, alignment);
2195 setDSOLocal(static_cast<mlir::Operation *>(gv));
2196 constantStringMap[c] = gv;
2197
2199 }
2200 return gv;
2201}
2202
2203/// Return a pointer to a constant array for the given string literal.
2204cir::GlobalViewAttr
2206 StringRef name) {
2207 cir::GlobalOp gv = getGlobalForStringLiteral(s, name);
2208 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(gv.getSymType());
2209 assert(arrayTy && "String literal must be array");
2211 cir::PointerType ptrTy = getBuilder().getPointerTo(arrayTy.getElementType());
2212
2213 return builder.getGlobalViewAttr(ptrTy, gv);
2214}
2215
2216// TODO(cir): this could be a common AST helper for both CIR and LLVM codegen.
2218 if (getLangOpts().OpenCL)
2220
2221 // For temporaries inside functions, CUDA treats them as normal variables.
2222 // LangAS::cuda_device, on the other hand, is reserved for those variables
2223 // explicitly marked with __device__.
2224 if (getLangOpts().CUDAIsDevice)
2225 return LangAS::Default;
2226
2227 if (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice)
2229 if (getLangOpts().SYCLIsDevice)
2230 errorNYI("SYCL temp address space");
2231
2232 return LangAS::Default;
2233}
2234
2236 CIRGenFunction *cgf) {
2237 if (cgf && e->getType()->isVariablyModifiedType())
2239
2241 "emitExplicitCastExprType");
2242}
2243
2245 const MemberPointerType *mpt) {
2246 if (mpt->isMemberFunctionPointerType()) {
2247 auto ty = mlir::cast<cir::MethodType>(convertType(destTy));
2248 return builder.getNullMethodAttr(ty);
2249 }
2250
2251 auto ty = mlir::cast<cir::DataMemberType>(convertType(destTy));
2252 return builder.getNullDataMemberAttr(ty);
2253}
2254
2257
2258 mlir::Location loc = getLoc(e->getSourceRange());
2259
2260 const auto *decl = cast<DeclRefExpr>(e->getSubExpr())->getDecl();
2261
2262 // A member function pointer.
2263 if (const auto *methodDecl = dyn_cast<CXXMethodDecl>(decl)) {
2264 auto ty = mlir::cast<cir::MethodType>(convertType(e->getType()));
2265 if (methodDecl->isVirtual())
2266 return cir::ConstantOp::create(
2267 builder, loc, getCXXABI().buildVirtualMethodAttr(ty, methodDecl));
2268
2269 const CIRGenFunctionInfo &fi =
2271 cir::FuncType funcTy = getTypes().getFunctionType(fi);
2272 cir::FuncOp methodFuncOp = getAddrOfFunction(methodDecl, funcTy);
2273 return cir::ConstantOp::create(builder, loc,
2274 builder.getMethodAttr(ty, methodFuncOp));
2275 }
2276
2277 // Otherwise, a member data pointer.
2278 auto ty = mlir::cast<cir::DataMemberType>(convertType(e->getType()));
2279 const auto *fieldDecl = cast<FieldDecl>(decl);
2280 return cir::ConstantOp::create(
2281 builder, loc, builder.getDataMemberAttr(ty, fieldDecl->getFieldIndex()));
2282}
2283
2285 for (Decl *decl : dc->decls()) {
2286 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
2287 // are themselves considered "top-level", so EmitTopLevelDecl on an
2288 // ObjCImplDecl does not recursively visit them. We need to do that in
2289 // case they're nested inside another construct (LinkageSpecDecl /
2290 // ExportDecl) that does stop them from being considered "top-level".
2291 if (auto *oid = dyn_cast<ObjCImplDecl>(decl))
2292 errorNYI(oid->getSourceRange(), "emitDeclConext: ObjCImplDecl");
2293
2295 }
2296}
2297
2298// Emit code for a single top level declaration.
2300
2301 // Ignore dependent declarations.
2302 if (decl->isTemplated())
2303 return;
2304
2305 switch (decl->getKind()) {
2306 default:
2307 errorNYI(decl->getBeginLoc(), "declaration of kind",
2308 decl->getDeclKindName());
2309 break;
2310
2311 case Decl::CXXConversion:
2312 case Decl::CXXMethod:
2313 case Decl::Function: {
2314 auto *fd = cast<FunctionDecl>(decl);
2315 // Consteval functions shouldn't be emitted.
2316 if (!fd->isConsteval())
2317 emitGlobal(fd);
2318 break;
2319 }
2320 case Decl::Export:
2322 break;
2323
2324 case Decl::Var:
2325 case Decl::Decomposition:
2326 case Decl::VarTemplateSpecialization: {
2328 if (auto *decomp = dyn_cast<DecompositionDecl>(decl))
2329 for (auto *binding : decomp->flat_bindings())
2330 if (auto *holdingVar = binding->getHoldingVar())
2331 emitGlobal(holdingVar);
2332 break;
2333 }
2334 case Decl::OpenACCRoutine:
2336 break;
2337 case Decl::OpenACCDeclare:
2339 break;
2340 case Decl::OMPThreadPrivate:
2342 break;
2343 case Decl::OMPGroupPrivate:
2345 break;
2346 case Decl::OMPAllocate:
2348 break;
2349 case Decl::OMPCapturedExpr:
2351 break;
2352 case Decl::OMPDeclareReduction:
2354 break;
2355 case Decl::OMPDeclareMapper:
2357 break;
2358 case Decl::OMPRequires:
2360 break;
2361 case Decl::Enum:
2362 case Decl::Using: // using X; [C++]
2363 case Decl::UsingDirective: // using namespace X; [C++]
2364 case Decl::UsingEnum: // using enum X; [C++]
2365 case Decl::NamespaceAlias:
2366 case Decl::Typedef:
2367 case Decl::TypeAlias: // using foo = bar; [C++11]
2368 case Decl::Record:
2370 break;
2371
2372 // No code generation needed.
2373 case Decl::ClassTemplate:
2374 case Decl::Concept:
2375 case Decl::CXXDeductionGuide:
2376 case Decl::Empty:
2377 case Decl::ExplicitInstantiation:
2378 case Decl::FunctionTemplate:
2379 case Decl::StaticAssert:
2380 case Decl::TypeAliasTemplate:
2381 case Decl::UsingShadow:
2382 case Decl::VarTemplate:
2383 case Decl::VarTemplatePartialSpecialization:
2384 break;
2385
2386 case Decl::CXXConstructor:
2388 break;
2389 case Decl::CXXDestructor:
2391 break;
2392
2393 // C++ Decls
2394 case Decl::LinkageSpec:
2395 case Decl::Namespace:
2397 break;
2398
2399 case Decl::ClassTemplateSpecialization:
2400 case Decl::CXXRecord: {
2403 for (auto *childDecl : crd->decls())
2405 emitTopLevelDecl(childDecl);
2406 break;
2407 }
2408
2409 case Decl::FileScopeAsm:
2410 // File-scope asm is ignored during device-side CUDA compilation.
2411 if (langOpts.CUDA && langOpts.CUDAIsDevice)
2412 break;
2413 // File-scope asm is ignored during device-side OpenMP compilation.
2414 if (langOpts.OpenMPIsTargetDevice)
2415 break;
2416 // File-scope asm is ignored during device-side SYCL compilation.
2417 if (langOpts.SYCLIsDevice)
2418 break;
2419 auto *file_asm = cast<FileScopeAsmDecl>(decl);
2420 std::string line = file_asm->getAsmString();
2421 globalScopeAsm.push_back(builder.getStringAttr(line));
2422 break;
2423 }
2424}
2425
2426void CIRGenModule::setInitializer(cir::GlobalOp &op, mlir::Attribute value) {
2427 // Recompute visibility when updating initializer.
2428 op.setInitialValueAttr(value);
2430}
2431
2432std::pair<cir::FuncType, cir::FuncOp> CIRGenModule::getAddrAndTypeOfCXXStructor(
2433 GlobalDecl gd, const CIRGenFunctionInfo *fnInfo, cir::FuncType fnType,
2434 bool dontDefer, ForDefinition_t isForDefinition) {
2435 auto *md = cast<CXXMethodDecl>(gd.getDecl());
2436
2437 if (isa<CXXDestructorDecl>(md)) {
2438 // Always alias equivalent complete destructors to base destructors in the
2439 // MS ABI.
2440 if (getTarget().getCXXABI().isMicrosoft() &&
2441 gd.getDtorType() == Dtor_Complete &&
2442 md->getParent()->getNumVBases() == 0)
2443 errorNYI(md->getSourceRange(),
2444 "getAddrAndTypeOfCXXStructor: MS ABI complete destructor");
2445 }
2446
2447 if (!fnType) {
2448 if (!fnInfo)
2450 fnType = getTypes().getFunctionType(*fnInfo);
2451 }
2452
2453 auto fn = getOrCreateCIRFunction(getMangledName(gd), fnType, gd,
2454 /*ForVtable=*/false, dontDefer,
2455 /*IsThunk=*/false, isForDefinition);
2456
2457 return {fnType, fn};
2458}
2459
2461 mlir::Type funcType, bool forVTable,
2462 bool dontDefer,
2463 ForDefinition_t isForDefinition) {
2464 assert(!cast<FunctionDecl>(gd.getDecl())->isConsteval() &&
2465 "consteval function should never be emitted");
2466
2467 if (!funcType) {
2468 const auto *fd = cast<FunctionDecl>(gd.getDecl());
2469 funcType = convertType(fd->getType());
2470 }
2471
2472 // Devirtualized destructor calls may come through here instead of via
2473 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
2474 // of the complete destructor when necessary.
2475 if (const auto *dd = dyn_cast<CXXDestructorDecl>(gd.getDecl())) {
2476 if (getTarget().getCXXABI().isMicrosoft() &&
2477 gd.getDtorType() == Dtor_Complete &&
2478 dd->getParent()->getNumVBases() == 0)
2479 errorNYI(dd->getSourceRange(),
2480 "getAddrOfFunction: MS ABI complete destructor");
2481 }
2482
2483 StringRef mangledName = getMangledName(gd);
2484 cir::FuncOp func =
2485 getOrCreateCIRFunction(mangledName, funcType, gd, forVTable, dontDefer,
2486 /*isThunk=*/false, isForDefinition);
2487 // Returns kernel handle for HIP kernel stub function.
2488 if (langOpts.CUDA && !langOpts.CUDAIsDevice &&
2489 cast<FunctionDecl>(gd.getDecl())->hasAttr<CUDAGlobalAttr>()) {
2490 mlir::Operation *handle = getCUDARuntime().getKernelHandle(func, gd);
2491
2492 // For HIP the kernel handle is a GlobalOp, which cannot be cast to
2493 // FuncOp. Return the stub directly in that case.
2494 bool isHIPHandle = mlir::isa<cir::GlobalOp>(*handle);
2495 if (isForDefinition || isHIPHandle)
2496 return func;
2497 return mlir::dyn_cast<cir::FuncOp>(*handle);
2498 }
2499 return func;
2500}
2501
2502static std::string getMangledNameImpl(CIRGenModule &cgm, GlobalDecl gd,
2503 const NamedDecl *nd) {
2504 SmallString<256> buffer;
2505
2506 llvm::raw_svector_ostream out(buffer);
2508
2510
2511 if (mc.shouldMangleDeclName(nd)) {
2512 mc.mangleName(gd.getWithDecl(nd), out);
2513 } else {
2514 IdentifierInfo *ii = nd->getIdentifier();
2515 assert(ii && "Attempt to mangle unnamed decl.");
2516
2517 const auto *fd = dyn_cast<FunctionDecl>(nd);
2518 if (fd &&
2519 fd->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
2520 cgm.errorNYI(nd->getSourceRange(), "getMangledName: X86RegCall");
2521 } else if (fd && fd->hasAttr<CUDAGlobalAttr>() &&
2523 out << "__device_stub__" << ii->getName();
2524 } else if (fd &&
2525 DeviceKernelAttr::isOpenCLSpelling(
2526 fd->getAttr<DeviceKernelAttr>()) &&
2528 cgm.errorNYI(nd->getSourceRange(), "getMangledName: OpenCL Stub");
2529 } else {
2530 out << ii->getName();
2531 }
2532 }
2533
2534 // Check if the module name hash should be appended for internal linkage
2535 // symbols. This should come before multi-version target suffixes are
2536 // appendded. This is to keep the name and module hash suffix of the internal
2537 // linkage function together. The unique suffix should only be added when name
2538 // mangling is done to make sure that the final name can be properly
2539 // demangled. For example, for C functions without prototypes, name mangling
2540 // is not done and the unique suffix should not be appended then.
2542
2543 if (const auto *fd = dyn_cast<FunctionDecl>(nd)) {
2544 if (fd->isMultiVersion()) {
2545 cgm.errorNYI(nd->getSourceRange(),
2546 "getMangledName: multi-version functions");
2547 }
2548 }
2549 if (cgm.getLangOpts().GPURelocatableDeviceCode) {
2550 cgm.errorNYI(nd->getSourceRange(),
2551 "getMangledName: GPU relocatable device code");
2552 }
2553
2554 return std::string(out.str());
2555}
2556
2557static FunctionDecl *
2559 const FunctionDecl *protoFunc) {
2560 // If this is a C no-prototype function, we can take the 'easy' way out and
2561 // just create a function with no arguments/functions, etc.
2562 if (!protoFunc->hasPrototype())
2563 return FunctionDecl::Create(
2564 ctx, /*DC=*/ctx.getTranslationUnitDecl(),
2565 /*StartLoc=*/SourceLocation{}, /*NLoc=*/SourceLocation{}, bindName,
2566 protoFunc->getType(), /*TInfo=*/nullptr, StorageClass::SC_None);
2567
2568 QualType funcTy = protoFunc->getType();
2569 auto *fpt = cast<FunctionProtoType>(protoFunc->getType());
2570
2571 // If this is a member function, add an explicit 'this' to the function type.
2572 if (auto *methodDecl = dyn_cast<CXXMethodDecl>(protoFunc);
2573 methodDecl && methodDecl->isImplicitObjectMemberFunction()) {
2574 llvm::SmallVector<QualType> paramTypes{fpt->getParamTypes()};
2575 paramTypes.insert(paramTypes.begin(), methodDecl->getThisType());
2576
2577 funcTy = ctx.getFunctionType(fpt->getReturnType(), paramTypes,
2578 fpt->getExtProtoInfo());
2579 fpt = cast<FunctionProtoType>(funcTy);
2580 }
2581
2582 auto *tempFunc =
2584 /*StartLoc=*/SourceLocation{},
2585 /*NLoc=*/SourceLocation{}, bindName, funcTy,
2586 /*TInfo=*/nullptr, StorageClass::SC_None);
2587
2589 params.reserve(fpt->getNumParams());
2590
2591 // Add all of the parameters.
2592 for (unsigned i = 0, e = fpt->getNumParams(); i != e; ++i) {
2594 ctx, tempFunc, /*StartLoc=*/SourceLocation{},
2595 /*IdLoc=*/SourceLocation{},
2596 /*Id=*/nullptr, fpt->getParamType(i), /*TInfo=*/nullptr,
2597 StorageClass::SC_None, /*DefArg=*/nullptr);
2598 parm->setScopeInfo(0, i);
2599 params.push_back(parm);
2600 }
2601
2602 tempFunc->setParams(params);
2603
2604 return tempFunc;
2605}
2606
2607std::string
2609 const FunctionDecl *attachedFunction) {
2611 getASTContext(), bindName, attachedFunction);
2612
2613 std::string ret = getMangledNameImpl(*this, GlobalDecl(tempFunc), tempFunc);
2614
2615 // This does nothing (it is a do-nothing function), since this is a
2616 // slab-allocator, but leave a call in to immediately destroy this in case we
2617 // ever come up with a way of getting allocations back.
2618 getASTContext().Deallocate(tempFunc);
2619 return ret;
2620}
2621
2623 GlobalDecl canonicalGd = gd.getCanonicalDecl();
2624
2625 // Some ABIs don't have constructor variants. Make sure that base and complete
2626 // constructors get mangled the same.
2627 if (const auto *cd = dyn_cast<CXXConstructorDecl>(canonicalGd.getDecl())) {
2628 if (!getTarget().getCXXABI().hasConstructorVariants()) {
2629 errorNYI(cd->getSourceRange(),
2630 "getMangledName: C++ constructor without variants");
2631 return cast<NamedDecl>(gd.getDecl())->getIdentifier()->getName();
2632 }
2633 }
2634
2635 // Keep the first result in the case of a mangling collision.
2636 const auto *nd = cast<NamedDecl>(gd.getDecl());
2637 std::string mangledName = getMangledNameImpl(*this, gd, nd);
2638
2639 auto result = manglings.insert(std::make_pair(mangledName, gd));
2640 return mangledDeclNames[canonicalGd] = result.first->first();
2641}
2642
2644 assert(!d->getInit() && "Cannot emit definite definitions here!");
2645
2646 StringRef mangledName = getMangledName(d);
2647 mlir::Operation *gv = getGlobalValue(mangledName);
2648
2649 // If we already have a definition, not declaration, with the same mangled
2650 // name, emitting of declaration is not required (and would actually overwrite
2651 // the emitted definition).
2652 if (gv && !mlir::cast<cir::GlobalOp>(gv).isDeclaration())
2653 return;
2654
2655 // If we have not seen a reference to this variable yet, place it into the
2656 // deferred declarations table to be emitted if needed later.
2657 if (!mustBeEmitted(d) && !gv) {
2658 deferredDecls[mangledName] = d;
2659 return;
2660 }
2661
2662 // The tentative definition is the only definition.
2664}
2665
2667 // Never defer when EmitAllDecls is specified.
2668 if (langOpts.EmitAllDecls)
2669 return true;
2670
2671 const auto *vd = dyn_cast<VarDecl>(global);
2672 if (vd &&
2673 ((codeGenOpts.KeepPersistentStorageVariables &&
2674 (vd->getStorageDuration() == SD_Static ||
2675 vd->getStorageDuration() == SD_Thread)) ||
2676 (codeGenOpts.KeepStaticConsts && vd->getStorageDuration() == SD_Static &&
2677 vd->getType().isConstQualified())))
2678 return true;
2679
2680 return getASTContext().DeclMustBeEmitted(global);
2681}
2682
2684 // In OpenMP 5.0 variables and function may be marked as
2685 // device_type(host/nohost) and we should not emit them eagerly unless we sure
2686 // that they must be emitted on the host/device. To be sure we need to have
2687 // seen a declare target with an explicit mentioning of the function, we know
2688 // we have if the level of the declare target attribute is -1. Note that we
2689 // check somewhere else if we should emit this at all.
2690 if (langOpts.OpenMP >= 50 && !langOpts.OpenMPSimd) {
2691 std::optional<OMPDeclareTargetDeclAttr *> activeAttr =
2692 OMPDeclareTargetDeclAttr::getActiveAttr(global);
2693 if (!activeAttr || (*activeAttr)->getLevel() != (unsigned)-1)
2694 return false;
2695 }
2696
2697 const auto *fd = dyn_cast<FunctionDecl>(global);
2698 if (fd) {
2699 // Implicit template instantiations may change linkage if they are later
2700 // explicitly instantiated, so they should not be emitted eagerly.
2701 if (fd->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
2702 return false;
2703 // Defer until all versions have been semantically checked.
2704 if (fd->hasAttr<TargetVersionAttr>() && !fd->isMultiVersion())
2705 return false;
2706 if (langOpts.SYCLIsDevice) {
2707 errorNYI(fd->getSourceRange(), "mayBeEmittedEagerly: SYCL");
2708 return false;
2709 }
2710 }
2711 const auto *vd = dyn_cast<VarDecl>(global);
2712 if (vd)
2713 if (astContext.getInlineVariableDefinitionKind(vd) ==
2715 // A definition of an inline constexpr static data member may change
2716 // linkage later if it's redeclared outside the class.
2717 return false;
2718
2719 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
2720 // codegen for global variables, because they may be marked as threadprivate.
2721 if (langOpts.OpenMP && langOpts.OpenMPUseTLS &&
2722 astContext.getTargetInfo().isTLSSupported() && isa<VarDecl>(global) &&
2723 !global->getType().isConstantStorage(astContext, false, false) &&
2724 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(global))
2725 return false;
2726
2727 assert((fd || vd) &&
2728 "Only FunctionDecl and VarDecl should hit this path so far.");
2729 return true;
2730}
2731
2732static bool shouldAssumeDSOLocal(const CIRGenModule &cgm,
2733 cir::CIRGlobalValueInterface gv) {
2734 if (gv.hasLocalLinkage())
2735 return true;
2736
2737 if (!gv.hasDefaultVisibility() && !gv.hasExternalWeakLinkage())
2738 return true;
2739
2740 // DLLImport explicitly marks the GV as external.
2741 // so it shouldn't be dso_local
2742 // But we don't have the info set now
2744
2745 const llvm::Triple &tt = cgm.getTriple();
2746 const CodeGenOptions &cgOpts = cgm.getCodeGenOpts();
2747 if (tt.isOSCygMing()) {
2748 // In MinGW and Cygwin, variables without DLLImport can still be
2749 // automatically imported from a DLL by the linker; don't mark variables
2750 // that potentially could come from another DLL as DSO local.
2751
2752 // With EmulatedTLS, TLS variables can be autoimported from other DLLs
2753 // (and this actually happens in the public interface of libstdc++), so
2754 // such variables can't be marked as DSO local. (Native TLS variables
2755 // can't be dllimported at all, though.)
2756 cgm.errorNYI("shouldAssumeDSOLocal: MinGW");
2757 }
2758
2759 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
2760 // remain unresolved in the link, they can be resolved to zero, which is
2761 // outside the current DSO.
2762 if (tt.isOSBinFormatCOFF() && gv.hasExternalWeakLinkage())
2763 return false;
2764
2765 // Every other GV is local on COFF.
2766 // Make an exception for windows OS in the triple: Some firmware builds use
2767 // *-win32-macho triples. This (accidentally?) produced windows relocations
2768 // without GOT tables in older clang versions; Keep this behaviour.
2769 // FIXME: even thread local variables?
2770 if (tt.isOSBinFormatCOFF() || (tt.isOSWindows() && tt.isOSBinFormatMachO()))
2771 return true;
2772
2773 // Only handle COFF and ELF for now.
2774 if (!tt.isOSBinFormatELF())
2775 return false;
2776
2777 llvm::Reloc::Model rm = cgOpts.RelocationModel;
2778 const LangOptions &lOpts = cgm.getLangOpts();
2779 if (rm != llvm::Reloc::Static && !lOpts.PIE) {
2780 // On ELF, if -fno-semantic-interposition is specified and the target
2781 // supports local aliases, there will be neither CC1
2782 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
2783 // dso_local on the function if using a local alias is preferable (can avoid
2784 // PLT indirection).
2785 if (!(isa<cir::FuncOp>(gv) && gv.canBenefitFromLocalAlias()))
2786 return false;
2787 return !(lOpts.SemanticInterposition || lOpts.HalfNoSemanticInterposition);
2788 }
2789
2790 // A definition cannot be preempted from an executable.
2791 if (!gv.isDeclarationForLinker())
2792 return true;
2793
2794 // Most PIC code sequences that assume that a symbol is local cannot produce a
2795 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
2796 // depended, it seems worth it to handle it here.
2797 if (rm == llvm::Reloc::PIC_ && gv.hasExternalWeakLinkage())
2798 return false;
2799
2800 // PowerPC64 prefers TOC indirection to avoid copy relocations.
2801 if (tt.isPPC64())
2802 return false;
2803
2804 if (cgOpts.DirectAccessExternalData) {
2805 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
2806 // for non-thread-local variables. If the symbol is not defined in the
2807 // executable, a copy relocation will be needed at link time. dso_local is
2808 // excluded for thread-local variables because they generally don't support
2809 // copy relocations.
2810 if (auto globalOp = dyn_cast<cir::GlobalOp>(gv.getOperation())) {
2811 // Assume variables are not thread-local until that support is added.
2813 return true;
2814 }
2815
2816 // -fno-pic sets dso_local on a function declaration to allow direct
2817 // accesses when taking its address (similar to a data symbol). If the
2818 // function is not defined in the executable, a canonical PLT entry will be
2819 // needed at link time. -fno-direct-access-external-data can avoid the
2820 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
2821 // it could just cause trouble without providing perceptible benefits.
2822 if (isa<cir::FuncOp>(gv) && !cgOpts.NoPLT && rm == llvm::Reloc::Static)
2823 return true;
2824 }
2825
2826 // If we can use copy relocations we can assume it is local.
2827
2828 // Otherwise don't assume it is local.
2829
2830 return false;
2831}
2832
2833void CIRGenModule::setGlobalVisibility(cir::CIRGlobalValueInterface gv,
2834 const NamedDecl *d) const {
2835 // Internal definitions always have default visibility.
2836 if (gv.hasLocalLinkage()) {
2837 gv.setGlobalVisibility(cir::VisibilityKind::Default);
2838 return;
2839 }
2840 if (!d)
2841 return;
2842
2843 // Set visibility for definitions, and for declarations if requested globally
2844 // or set explicitly.
2846
2847 // OpenMP declare target variables must be visible to the host so they can
2848 // be registered. We require protected visibility unless the variable has
2849 // the DT_nohost modifier and does not need to be registered.
2850 if (getASTContext().getLangOpts().OpenMP &&
2851 getASTContext().getLangOpts().OpenMPIsTargetDevice && isa<VarDecl>(d) &&
2852 d->hasAttr<OMPDeclareTargetDeclAttr>() &&
2853 d->getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
2854 OMPDeclareTargetDeclAttr::DT_NoHost &&
2856 llvm_unreachable("setGlobalVisibility: OpenMP is NYI");
2857 return;
2858 }
2859
2860 // CUDA/HIP device kernels and global variables must be visible to the host
2861 // so they can be registered / initialized. We require protected visibility
2862 // unless the user explicitly requested hidden via an attribute.
2863 if (getASTContext().getLangOpts().CUDAIsDevice &&
2865 !d->hasAttr<OMPDeclareTargetDeclAttr>()) {
2866 bool needsProtected = false;
2867 if (isa<FunctionDecl>(d)) {
2868 needsProtected =
2869 d->hasAttr<CUDAGlobalAttr>() || d->hasAttr<DeviceKernelAttr>();
2870 } else if (const auto *vd = dyn_cast<VarDecl>(d)) {
2871 needsProtected = vd->hasAttr<CUDADeviceAttr>() ||
2872 vd->hasAttr<CUDAConstantAttr>() ||
2873 vd->getType()->isCUDADeviceBuiltinSurfaceType() ||
2874 vd->getType()->isCUDADeviceBuiltinTextureType();
2875 }
2876 if (needsProtected) {
2877 gv.setGlobalVisibility(cir::VisibilityKind::Protected);
2878 return;
2879 }
2880 }
2881
2883 gv.setGlobalVisibility(cir::VisibilityKind::Hidden);
2884 return;
2885 }
2886
2888
2889 if (lv.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
2890 !gv.isDeclarationForLinker())
2891 gv.setGlobalVisibility(getCIRVisibilityKind(lv.getVisibility()));
2892}
2893
2894void CIRGenModule::setDSOLocal(cir::CIRGlobalValueInterface gv) const {
2895 gv.setDSOLocal(shouldAssumeDSOLocal(*this, gv));
2896}
2897
2898void CIRGenModule::setDSOLocal(mlir::Operation *op) const {
2899 if (auto globalValue = dyn_cast<cir::CIRGlobalValueInterface>(op))
2900 setDSOLocal(globalValue);
2901}
2902
2903void CIRGenModule::setGVProperties(mlir::Operation *op,
2904 const NamedDecl *d) const {
2906 setGVPropertiesAux(op, d);
2907}
2908
2909void CIRGenModule::setGVPropertiesAux(mlir::Operation *op,
2910 const NamedDecl *d) const {
2912 setDSOLocal(op);
2914}
2915
2917 GlobalDecl &result) const {
2918 auto res = manglings.find(mangledName);
2919 if (res == manglings.end())
2920 return false;
2921 result = res->getValue();
2922 return true;
2923}
2924
2926 switch (getCodeGenOpts().getDefaultTLSModel()) {
2928 return cir::TLS_Model::GeneralDynamic;
2930 return cir::TLS_Model::LocalDynamic;
2932 return cir::TLS_Model::InitialExec;
2934 return cir::TLS_Model::LocalExec;
2935 }
2936 llvm_unreachable("Invalid TLS model!");
2937}
2938
2939void CIRGenModule::setTLSMode(mlir::Operation *op, const VarDecl &d,
2940 bool isExtendingDecl) {
2941 assert(d.getTLSKind() && "setting TLS mode on non-TLS var!");
2942
2943 cir::TLS_Model tlm = getDefaultCIRTLSModel();
2944
2945 // Override the TLS model if it is explicitly specified.
2946 if (d.getAttr<TLSModelAttr>())
2947 errorNYI(d.getSourceRange(), "TLS model attribute");
2948
2949 auto global = cast<cir::GlobalOp>(op);
2950 global.setTlsModel(tlm);
2951
2952 // For namespace-scope dyanmic TLS we need to set the wrapper, int, or guard
2953 // info.
2954 if (d.isStaticLocal() || tlm != cir::TLS_Model::GeneralDynamic)
2955 return;
2956
2957 // If this function was called to set the TLS mode for a temporary whose
2958 // lifetime is extended by the variable declared by `d`, don't emit the
2959 // wrapper, init, and guard info.
2960 if (isExtendingDecl)
2961 return;
2962
2963 setGlobalTlsReferences(d, global);
2964}
2965
2967 const CIRGenFunctionInfo &info,
2968 cir::FuncOp func, bool isThunk) {
2969 // TODO(cir): More logic of constructAttributeList is needed.
2970 cir::CallingConv callingConv;
2971 cir::SideEffect sideEffect;
2972
2973 // TODO(cir): The current list should be initialized with the extra function
2974 // attributes, but we don't have those yet. For now, the PAL is initialized
2975 // with nothing.
2977 // Initialize PAL with existing attributes to merge attributes.
2978 mlir::NamedAttrList pal{};
2979 std::vector<mlir::NamedAttrList> argAttrs(info.arguments().size());
2980 mlir::NamedAttrList retAttrs{};
2981 constructAttributeList(func.getName(), info, globalDecl, pal, argAttrs,
2982 retAttrs, callingConv, sideEffect,
2983 /*attrOnCallSite=*/false, isThunk);
2984
2985 for (mlir::NamedAttribute attr : pal)
2986 func->setAttr(attr.getName(), attr.getValue());
2987
2988 llvm::for_each(llvm::enumerate(argAttrs), [func](auto idx_arg_pair) {
2989 mlir::function_interface_impl::setArgAttrs(func, idx_arg_pair.index(),
2990 idx_arg_pair.value());
2991 });
2992 if (!retAttrs.empty())
2993 mlir::function_interface_impl::setResultAttrs(func, 0, retAttrs);
2994
2995 // TODO(cir): Check X86_VectorCall incompatibility wiht WinARM64EC
2996
2997 // TODO(cir): Set the calling convention computed by constructAttributeList
2998 // on the function. FuncOp supports calling_conv, but target-specific
2999 // CodeGen is needed to set it correctly (e.g., AMDGPU kernel functions
3000 // should be marked with AMDGPUKernel).
3002}
3003
3005 cir::FuncOp func,
3006 bool isIncompleteFunction,
3007 bool isThunk) {
3008 // NOTE(cir): Original CodeGen checks if this is an intrinsic. In CIR we
3009 // represent them in dedicated ops. The correct attributes are ensured during
3010 // translation to LLVM. Thus, we don't need to check for them here.
3011
3012 const auto *funcDecl = cast<FunctionDecl>(globalDecl.getDecl());
3013
3014 if (!isIncompleteFunction)
3015 setCIRFunctionAttributes(globalDecl,
3016 getTypes().arrangeGlobalDeclaration(globalDecl),
3017 func, isThunk);
3018
3019 if (!isIncompleteFunction && func.isDeclaration())
3020 getTargetCIRGenInfo().setTargetAttributes(funcDecl, func, *this);
3021
3022 // Mirrors setLinkageForGV in CodeGenModule::SetFunctionAttributes.
3023 setLinkageForFunction(*this, func, funcDecl);
3024
3025 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
3026 if (funcDecl->isInlineBuiltinDeclaration()) {
3027 const FunctionDecl *fdBody;
3028 bool hasBody = funcDecl->hasBody(fdBody);
3029 (void)hasBody;
3030 assert(hasBody && "Inline builtin declarations should always have an "
3031 "available body!");
3033 }
3034
3035 if (funcDecl->isReplaceableGlobalAllocationFunction()) {
3036 // A replaceable global allocation function does not act like a builtin by
3037 // default, only if it is invoked by a new-expression or delete-expression.
3038 func->setAttr(cir::CIRDialect::getNoBuiltinAttrName(),
3039 mlir::UnitAttr::get(&getMLIRContext()));
3040 }
3041}
3042
3043/// Determines whether the language options require us to model
3044/// unwind exceptions. We treat -fexceptions as mandating this
3045/// except under the fragile ObjC ABI with only ObjC exceptions
3046/// enabled. This means, for example, that C with -fexceptions
3047/// enables this.
3048static bool hasUnwindExceptions(const LangOptions &langOpts) {
3049 // If exceptions are completely disabled, obviously this is false.
3050 if (!langOpts.Exceptions)
3051 return false;
3052 // If C++ exceptions are enabled, this is true.
3053 if (langOpts.CXXExceptions)
3054 return true;
3055 // If ObjC exceptions are enabled, this depends on the ABI.
3056 if (langOpts.ObjCExceptions)
3057 return langOpts.ObjCRuntime.hasUnwindExceptions();
3058 return true;
3059}
3060
3062 const clang::FunctionDecl *decl, cir::FuncOp f) {
3065
3066 if (!hasUnwindExceptions(langOpts))
3067 f->setAttr(cir::CIRDialect::getNoThrowAttrName(),
3068 mlir::UnitAttr::get(&getMLIRContext()));
3069
3070 std::optional<cir::InlineKind> existingInlineKind = f.getInlineKind();
3071 bool isNoInline =
3072 existingInlineKind && *existingInlineKind == cir::InlineKind::NoInline;
3073 bool isAlwaysInline = existingInlineKind &&
3074 *existingInlineKind == cir::InlineKind::AlwaysInline;
3075 if (!decl) {
3076 assert(!cir::MissingFeatures::hlsl());
3077
3078 if (!isAlwaysInline &&
3079 codeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
3080 // If inlining is disabled and we don't have a declaration to control
3081 // inlining, mark the function as 'noinline' unless it is explicitly
3082 // marked as 'alwaysinline'.
3083 f.setInlineKind(cir::InlineKind::NoInline);
3084 }
3085
3086 return;
3087 }
3088
3095 assert(!cir::MissingFeatures::hlsl());
3096
3097 // Handle inline attributes
3098 if (decl->hasAttr<NoInlineAttr>() && !isAlwaysInline) {
3099 // Add noinline if the function isn't always_inline.
3100 f.setInlineKind(cir::InlineKind::NoInline);
3101 } else if (decl->hasAttr<AlwaysInlineAttr>() && !isNoInline) {
3102 // Don't override AlwaysInline with NoInline, or vice versa, since we can't
3103 // specify both in IR.
3104 f.setInlineKind(cir::InlineKind::AlwaysInline);
3105 } else if (codeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
3106 // If inlining is disabled, force everything that isn't always_inline
3107 // to carry an explicit noinline attribute.
3108 if (!isAlwaysInline)
3109 f.setInlineKind(cir::InlineKind::NoInline);
3110 } else {
3111 // Otherwise, propagate the inline hint attribute and potentially use its
3112 // absence to mark things as noinline.
3113 // Search function and template pattern redeclarations for inline.
3114 if (auto *fd = dyn_cast<FunctionDecl>(decl)) {
3115 // TODO: Share this checkForInline implementation with classic codegen.
3116 // This logic is likely to change over time, so sharing would help ensure
3117 // consistency.
3118 auto checkForInline = [](const FunctionDecl *decl) {
3119 auto checkRedeclForInline = [](const FunctionDecl *redecl) {
3120 return redecl->isInlineSpecified();
3121 };
3122 if (any_of(decl->redecls(), checkRedeclForInline))
3123 return true;
3124 const FunctionDecl *pattern = decl->getTemplateInstantiationPattern();
3125 if (!pattern)
3126 return false;
3127 return any_of(pattern->redecls(), checkRedeclForInline);
3128 };
3129 if (checkForInline(fd)) {
3130 f.setInlineKind(cir::InlineKind::InlineHint);
3131 } else if (codeGenOpts.getInlining() ==
3133 !fd->isInlined() && !isAlwaysInline) {
3134 f.setInlineKind(cir::InlineKind::NoInline);
3135 }
3136 }
3137 }
3138
3140}
3141
3143 StringRef mangledName, mlir::Type funcType, GlobalDecl gd, bool forVTable,
3144 bool dontDefer, bool isThunk, ForDefinition_t isForDefinition,
3145 mlir::NamedAttrList extraAttrs) {
3146 const Decl *d = gd.getDecl();
3147
3148 if (const auto *fd = cast_or_null<FunctionDecl>(d)) {
3149 // For the device, mark the function as one that should be emitted.
3150 if (getLangOpts().OpenMPIsTargetDevice && openMPRuntime &&
3151 !getOpenMPRuntime().markAsGlobalTarget(gd) && fd->isDefined() &&
3152 !dontDefer && !isForDefinition) {
3153 if (const FunctionDecl *fdDef = fd->getDefinition()) {
3154 GlobalDecl gdDef;
3155 if (const auto *cd = dyn_cast<CXXConstructorDecl>(fdDef))
3156 gdDef = GlobalDecl(cd, gd.getCtorType());
3157 else if (const auto *dd = dyn_cast<CXXDestructorDecl>(fdDef))
3158 gdDef = GlobalDecl(dd, gd.getDtorType());
3159 else
3160 gdDef = GlobalDecl(fdDef);
3161 emitGlobal(gdDef);
3162 }
3163 }
3164
3165 // Any attempts to use a MultiVersion function should result in retrieving
3166 // the iFunc instead. Name mangling will handle the rest of the changes.
3167 if (fd->isMultiVersion())
3168 errorNYI(fd->getSourceRange(), "getOrCreateCIRFunction: multi-version");
3169 }
3170
3171 // Lookup the entry, lazily creating it if necessary.
3172 mlir::Operation *entry = getGlobalValue(mangledName);
3173 if (entry) {
3174 assert(mlir::isa<cir::FuncOp>(entry));
3175
3177
3178 // Handle dropped DLL attributes.
3179 if (d && !d->hasAttr<DLLImportAttr>() && !d->hasAttr<DLLExportAttr>()) {
3181 setDSOLocal(entry);
3182 }
3183
3184 // If there are two attempts to define the same mangled name, issue an
3185 // error.
3186 auto fn = cast<cir::FuncOp>(entry);
3187 if (isForDefinition && fn && !fn.isDeclaration()) {
3188 GlobalDecl otherGd;
3189 // Check that GD is not yet in DiagnosedConflictingDefinitions is required
3190 // to make sure that we issue an error only once.
3191 if (lookupRepresentativeDecl(mangledName, otherGd) &&
3192 (gd.getCanonicalDecl().getDecl() !=
3193 otherGd.getCanonicalDecl().getDecl()) &&
3194 diagnosedConflictingDefinitions.insert(gd).second) {
3195 getDiags().Report(d->getLocation(), diag::err_duplicate_mangled_name)
3196 << mangledName;
3197 getDiags().Report(otherGd.getDecl()->getLocation(),
3198 diag::note_previous_definition);
3199 }
3200 }
3201
3202 if (fn && fn.getFunctionType() == funcType) {
3203 return fn;
3204 }
3205
3206 if (!isForDefinition) {
3207 return fn;
3208 }
3209
3210 // TODO(cir): classic codegen checks here if this is a llvm::GlobalAlias.
3211 // How will we support this?
3212 }
3213
3214 auto *funcDecl = llvm::cast_or_null<FunctionDecl>(gd.getDecl());
3215 bool invalidLoc = !funcDecl ||
3216 funcDecl->getSourceRange().getBegin().isInvalid() ||
3217 funcDecl->getSourceRange().getEnd().isInvalid();
3218 cir::FuncOp funcOp = createCIRFunction(
3219 invalidLoc ? theModule->getLoc() : getLoc(funcDecl->getSourceRange()),
3220 mangledName, mlir::cast<cir::FuncType>(funcType), funcDecl);
3221
3222 if (funcDecl && funcDecl->hasAttr<AnnotateAttr>())
3223 deferredAnnotations[mangledName] = funcDecl;
3224
3225 // If we already created a function with the same mangled name (but different
3226 // type) before, take its name and add it to the list of functions to be
3227 // replaced with F at the end of CodeGen.
3228 //
3229 // This happens if there is a prototype for a function (e.g. "int f()") and
3230 // then a definition of a different type (e.g. "int f(int x)").
3231 if (entry) {
3232
3233 // Fetch a generic symbol-defining operation and its uses.
3234 auto symbolOp = mlir::cast<mlir::SymbolOpInterface>(entry);
3235
3236 // This might be an implementation of a function without a prototype, in
3237 // which case, try to do special replacement of calls which match the new
3238 // prototype. The really key thing here is that we also potentially drop
3239 // arguments from the call site so as to make a direct call, which makes the
3240 // inliner happier and suppresses a number of optimizer warnings (!) about
3241 // dropping arguments.
3242 if (symbolOp.getSymbolUses(symbolOp->getParentOp()))
3244
3245 // Obliterate no-proto declaration.
3246 eraseGlobalSymbol(entry);
3247 entry->erase();
3248 }
3249
3250 if (d)
3251 setFunctionAttributes(gd, funcOp, /*isIncompleteFunction=*/false, isThunk);
3252 if (!extraAttrs.empty()) {
3253 extraAttrs.append(funcOp->getAttrs());
3254 funcOp->setAttrs(extraAttrs);
3255 }
3256
3257 // 'dontDefer' actually means don't move this to the deferredDeclsToEmit list.
3258 if (dontDefer) {
3259 // TODO(cir): This assertion will need an additional condition when we
3260 // support incomplete functions.
3261 assert(funcOp.getFunctionType() == funcType);
3262 return funcOp;
3263 }
3264
3265 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
3266 // each other bottoming out wiht the base dtor. Therefore we emit non-base
3267 // dtors on usage, even if there is no dtor definition in the TU.
3268 if (isa_and_nonnull<CXXDestructorDecl>(d) &&
3269 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(d),
3270 gd.getDtorType()))
3271 errorNYI(d->getSourceRange(), "getOrCreateCIRFunction: dtor");
3272
3273 // This is the first use or definition of a mangled name. If there is a
3274 // deferred decl with this name, remember that we need to emit it at the end
3275 // of the file.
3276 auto ddi = deferredDecls.find(mangledName);
3277 if (ddi != deferredDecls.end()) {
3278 // Move the potentially referenced deferred decl to the
3279 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
3280 // don't need it anymore).
3281 addDeferredDeclToEmit(ddi->second);
3282 deferredDecls.erase(ddi);
3283
3284 // Otherwise, there are cases we have to worry about where we're using a
3285 // declaration for which we must emit a definition but where we might not
3286 // find a top-level definition.
3287 // - member functions defined inline in their classes
3288 // - friend functions defined inline in some class
3289 // - special member functions with implicit definitions
3290 // If we ever change our AST traversal to walk into class methods, this
3291 // will be unnecessary.
3292 //
3293 // We also don't emit a definition for a function if it's going to be an
3294 // entry in a vtable, unless it's already marked as used.
3295 } else if (getLangOpts().CPlusPlus && d) {
3296 // Look for a declaration that's lexically in a record.
3297 for (const auto *fd = cast<FunctionDecl>(d)->getMostRecentDecl(); fd;
3298 fd = fd->getPreviousDecl()) {
3299 if (isa<CXXRecordDecl>(fd->getLexicalDeclContext())) {
3300 if (fd->doesThisDeclarationHaveABody()) {
3302 break;
3303 }
3304 }
3305 }
3306 }
3307
3308 return funcOp;
3309}
3310
3311cir::FuncOp
3312CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
3313 cir::FuncType funcType,
3314 const clang::FunctionDecl *funcDecl) {
3315 cir::FuncOp func;
3316 {
3317 mlir::OpBuilder::InsertionGuard guard(builder);
3318
3319 // Some global emissions are triggered while emitting a function, e.g.
3320 // void s() { x.method() }
3321 //
3322 // Be sure to insert a new function before a current one.
3323 CIRGenFunction *cgf = this->curCGF;
3324 if (cgf)
3325 builder.setInsertionPoint(cgf->curFn);
3326
3327 func = cir::FuncOp::create(builder, loc, name, funcType);
3328
3329 symbolLookupCache[func.getSymNameAttr()] = func;
3330
3332
3333 if (funcDecl && !funcDecl->hasPrototype())
3334 func.setNoProto(true);
3335
3336 assert(func.isDeclaration() && "expected empty body");
3337
3338 // A declaration gets private visibility by default, but external linkage
3339 // as the default linkage.
3340 func.setLinkageAttr(cir::GlobalLinkageKindAttr::get(
3341 &getMLIRContext(), cir::GlobalLinkageKind::ExternalLinkage));
3342 mlir::SymbolTable::setSymbolVisibility(
3343 func, mlir::SymbolTable::Visibility::Private);
3344
3346
3347 // Mark C++ special member functions (Constructor, Destructor etc.)
3348 setCXXSpecialMemberAttr(func, funcDecl);
3349
3350 if (!cgf)
3351 theModule.push_back(func);
3352
3353 if (this->getLangOpts().OpenACC) {
3354 // We only have to handle this attribute, since OpenACCAnnotAttrs are
3355 // handled via the end-of-TU work.
3356 for (const auto *attr :
3357 funcDecl->specific_attrs<OpenACCRoutineDeclAttr>())
3358 emitOpenACCRoutineDecl(funcDecl, func, attr->getLocation(),
3359 attr->Clauses);
3360 }
3361 }
3362 return func;
3363}
3364
3365cir::FuncOp
3366CIRGenModule::createCIRBuiltinFunction(mlir::Location loc, StringRef name,
3367 cir::FuncType ty,
3368 const clang::FunctionDecl *fd) {
3369 cir::FuncOp fnOp = createCIRFunction(loc, name, ty, fd);
3370 fnOp.setBuiltin(true);
3371 return fnOp;
3372}
3373
3374static cir::CtorKind getCtorKindFromDecl(const CXXConstructorDecl *ctor) {
3375 if (ctor->isDefaultConstructor())
3376 return cir::CtorKind::Default;
3377 if (ctor->isCopyConstructor())
3378 return cir::CtorKind::Copy;
3379 if (ctor->isMoveConstructor())
3380 return cir::CtorKind::Move;
3381 return cir::CtorKind::Custom;
3382}
3383
3384static cir::AssignKind getAssignKindFromDecl(const CXXMethodDecl *method) {
3385 if (method->isCopyAssignmentOperator())
3386 return cir::AssignKind::Copy;
3387 if (method->isMoveAssignmentOperator())
3388 return cir::AssignKind::Move;
3389 llvm_unreachable("not a copy or move assignment operator");
3390}
3391
3393 cir::FuncOp funcOp, const clang::FunctionDecl *funcDecl) {
3394 if (!funcDecl)
3395 return;
3396
3397 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(funcDecl)) {
3398 auto cxxDtor = cir::CXXDtorAttr::get(
3399 convertType(getASTContext().getCanonicalTagType(dtor->getParent())),
3400 dtor->isTrivial());
3401 funcOp.setCxxSpecialMemberAttr(cxxDtor);
3402 return;
3403 }
3404
3405 if (const auto *ctor = dyn_cast<CXXConstructorDecl>(funcDecl)) {
3406 cir::CtorKind kind = getCtorKindFromDecl(ctor);
3407 auto cxxCtor = cir::CXXCtorAttr::get(
3408 convertType(getASTContext().getCanonicalTagType(ctor->getParent())),
3409 kind, ctor->isTrivial());
3410 funcOp.setCxxSpecialMemberAttr(cxxCtor);
3411 return;
3412 }
3413
3414 const auto *method = dyn_cast<CXXMethodDecl>(funcDecl);
3415 if (method && (method->isCopyAssignmentOperator() ||
3416 method->isMoveAssignmentOperator())) {
3417 cir::AssignKind assignKind = getAssignKindFromDecl(method);
3418 auto cxxAssign = cir::CXXAssignAttr::get(
3419 convertType(getASTContext().getCanonicalTagType(method->getParent())),
3420 assignKind, method->isTrivial());
3421 funcOp.setCxxSpecialMemberAttr(cxxAssign);
3422 return;
3423 }
3424}
3425
3426static void setWindowsItaniumDLLImport(CIRGenModule &cgm, bool isLocal,
3427 cir::FuncOp funcOp, StringRef name) {
3428 // In Windows Itanium environments, try to mark runtime functions
3429 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
3430 // will link their standard library statically or dynamically. Marking
3431 // functions imported when they are not imported can cause linker errors
3432 // and warnings.
3433 if (!isLocal && cgm.getTarget().getTriple().isWindowsItaniumEnvironment() &&
3434 !cgm.getCodeGenOpts().LTOVisibilityPublicStd) {
3438 }
3439}
3440
3441cir::FuncOp CIRGenModule::createRuntimeFunction(cir::FuncType ty,
3442 StringRef name,
3443 mlir::NamedAttrList extraAttrs,
3444 bool isLocal,
3445 bool assumeConvergent) {
3446 if (assumeConvergent)
3447 errorNYI("createRuntimeFunction: assumeConvergent");
3448
3449 cir::FuncOp entry = getOrCreateCIRFunction(name, ty, GlobalDecl(),
3450 /*forVtable=*/false, extraAttrs);
3451
3452 if (entry) {
3453 // TODO(cir): set the attributes of the function.
3456 setWindowsItaniumDLLImport(*this, isLocal, entry, name);
3457 entry.setDSOLocal(true);
3458 }
3459
3460 return entry;
3461}
3462
3463mlir::SymbolTable::Visibility
3465 // MLIR doesn't accept public symbols declarations (only
3466 // definitions).
3467 if (op.isDeclaration())
3468 return mlir::SymbolTable::Visibility::Private;
3469 return getMLIRVisibilityFromCIRLinkage(op.getLinkage());
3470}
3471
3472mlir::SymbolTable::Visibility
3474 switch (glk) {
3475 case cir::GlobalLinkageKind::InternalLinkage:
3476 case cir::GlobalLinkageKind::PrivateLinkage:
3477 return mlir::SymbolTable::Visibility::Private;
3478 case cir::GlobalLinkageKind::ExternalLinkage:
3479 case cir::GlobalLinkageKind::ExternalWeakLinkage:
3480 case cir::GlobalLinkageKind::LinkOnceODRLinkage:
3481 case cir::GlobalLinkageKind::AvailableExternallyLinkage:
3482 case cir::GlobalLinkageKind::CommonLinkage:
3483 case cir::GlobalLinkageKind::WeakAnyLinkage:
3484 case cir::GlobalLinkageKind::WeakODRLinkage:
3485 return mlir::SymbolTable::Visibility::Public;
3486 default: {
3487 llvm::errs() << "visibility not implemented for '"
3488 << stringifyGlobalLinkageKind(glk) << "'\n";
3489 assert(0 && "not implemented");
3490 }
3491 }
3492 llvm_unreachable("linkage should be handled above!");
3493}
3494
3496 clang::VisibilityAttr::VisibilityType visibility) {
3497 switch (visibility) {
3498 case clang::VisibilityAttr::VisibilityType::Default:
3499 return cir::VisibilityKind::Default;
3500 case clang::VisibilityAttr::VisibilityType::Hidden:
3501 return cir::VisibilityKind::Hidden;
3502 case clang::VisibilityAttr::VisibilityType::Protected:
3503 return cir::VisibilityKind::Protected;
3504 }
3505 llvm_unreachable("unexpected visibility value");
3506}
3507
3508cir::VisibilityAttr
3510 const clang::VisibilityAttr *va = decl->getAttr<clang::VisibilityAttr>();
3511 cir::VisibilityAttr cirVisibility =
3512 cir::VisibilityAttr::get(&getMLIRContext());
3513 if (va) {
3514 cirVisibility = cir::VisibilityAttr::get(
3515 &getMLIRContext(),
3516 getGlobalVisibilityKindFromClangVisibility(va->getVisibility()));
3517 }
3518 return cirVisibility;
3519}
3520
3522 emitDeferred();
3524 applyReplacements();
3525
3526 theModule->setAttr(cir::CIRDialect::getModuleLevelAsmAttrName(),
3527 builder.getArrayAttr(globalScopeAsm));
3528
3529 emitGlobalAnnotations();
3530
3531 if (!recordLayoutEntries.empty())
3532 theModule->setAttr(
3533 cir::CIRDialect::getRecordLayoutsAttrName(),
3534 mlir::DictionaryAttr::get(&getMLIRContext(), recordLayoutEntries));
3535
3536 if (getTriple().isAMDGPU() ||
3537 (getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD))
3539
3540 if (getLangOpts().HIP) {
3541 // Emit a unique ID so that host and device binaries from the same
3542 // compilation unit can be associated.
3543 std::string cuidName =
3544 ("__hip_cuid_" + getASTContext().getCUIDHash()).str();
3545 auto int8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
3546 auto loc = builder.getUnknownLoc();
3547 mlir::ptr::MemorySpaceAttrInterface addrSpace =
3548 cir::LangAddressSpaceAttr::get(&getMLIRContext(),
3549 getGlobalVarAddressSpace(nullptr));
3550
3551 auto gv = createGlobalOp(loc, cuidName, int8Ty,
3552 /*isConstant=*/false, addrSpace);
3553 gv.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
3554 // Initialize with zero
3555 auto zeroAttr = cir::IntAttr::get(int8Ty, 0);
3556 gv.setInitialValueAttr(zeroAttr);
3557 // External linkage requires public visibility
3558 mlir::SymbolTable::setSymbolVisibility(
3559 gv, mlir::SymbolTable::Visibility::Public);
3560
3562 }
3563
3564 if (astContext.getLangOpts().CUDA && cudaRuntime)
3566
3567 emitLLVMUsed();
3568
3569 // Classic codegen calls `checkAliases` here to validate any alias
3570 // definitions emitted during codegen.
3572
3573 // There's a lot of code that is not implemented yet.
3575}
3576
3578 const auto *d = cast<ValueDecl>(gd.getDecl());
3579 const AliasAttr *aa = d->getAttr<AliasAttr>();
3580 assert(aa && "Not an alias?");
3581
3582 StringRef mangledName = getMangledName(gd);
3583
3584 if (aa->getAliasee() == mangledName) {
3585 diags.Report(aa->getLocation(), diag::err_cyclic_alias) << 0;
3586 return;
3587 }
3588
3589 // If there is a definition in the module, then it wins over the alias.
3590 // This is dubious, but allow it to be safe. Just ignore the alias.
3591 mlir::Operation *entry = getGlobalValue(mangledName);
3592 if (entry) {
3593 auto entryGV = mlir::dyn_cast<cir::CIRGlobalValueInterface>(entry);
3594 if (entryGV && entryGV.isDefinition())
3595 return;
3596 }
3597
3598 // Classic codegen pushes the alias onto an `Aliases` list at this point so
3599 // that `checkAliases` can later validate the alias and recover on error.
3601
3602 mlir::Location loc = getLoc(d->getSourceRange());
3603 bool isFunction = isa<FunctionDecl>(d);
3604
3605 // Get the linkage and the type of the alias.
3606 mlir::Type declTy;
3607 cir::GlobalLinkageKind linkage;
3608 if (isFunction) {
3609 declTy = getTypes().getFunctionType(gd);
3610 linkage = getFunctionLinkage(gd);
3611 } else {
3612 declTy = getTypes().convertTypeForMem(d->getType());
3613 const auto *vd = cast<VarDecl>(d);
3614 linkage = getCIRLinkageVarDefinition(vd);
3615 }
3616
3617 // Aliases that target weak symbols must themselves be marked weak.
3618 if (d->hasAttr<WeakAttr>() || d->hasAttr<WeakRefAttr>() ||
3619 d->isWeakImported())
3620 linkage = cir::GlobalLinkageKind::WeakAnyLinkage;
3621
3622 // Create the alias op. If there is an existing declaration with the same
3623 // name, erase it: any references to it via flat symbol reference will
3624 // automatically resolve to the new alias.
3625 if (entry) {
3626 eraseGlobalSymbol(entry);
3627 entry->erase();
3628 }
3629
3630 // Aliases are always definitions, so the MLIR visibility should match the
3631 // linkage rather than defaulting to private.
3632 mlir::SymbolTable::Visibility visibility =
3634
3635 // TODO(cir): Make GlobalAlias a separate op.
3636 cir::CIRGlobalValueInterface alias =
3637 isFunction ? mlir::cast<cir::CIRGlobalValueInterface>(
3638 createCIRFunction(loc, mangledName,
3639 mlir::cast<cir::FuncType>(declTy),
3641 .getOperation())
3642 : mlir::cast<cir::CIRGlobalValueInterface>(
3643 createGlobalOp(loc, mangledName, declTy).getOperation());
3644 alias.setAliasee(aa->getAliasee());
3645 alias.setLinkage(linkage);
3646 mlir::SymbolTable::setSymbolVisibility(alias, visibility);
3648 setCommonAttributes(gd, alias);
3650}
3651
3652void CIRGenModule::emitAliasForGlobal(StringRef mangledName,
3653 mlir::Operation *op, GlobalDecl aliasGD,
3654 cir::FuncOp aliasee,
3655 cir::GlobalLinkageKind linkage) {
3656
3657 auto *aliasFD = dyn_cast<FunctionDecl>(aliasGD.getDecl());
3658 assert(aliasFD && "expected FunctionDecl");
3659
3660 // The aliasee function type is different from the alias one, this difference
3661 // is specific to CIR because in LLVM the ptr types are already erased at this
3662 // point.
3663 const CIRGenFunctionInfo &fnInfo =
3665 cir::FuncType fnType = getTypes().getFunctionType(fnInfo);
3666
3667 cir::FuncOp alias =
3669 mangledName, fnType, aliasFD);
3670 alias.setAliasee(aliasee.getName());
3671 alias.setLinkage(linkage);
3672 // Declarations cannot have public MLIR visibility, just mark them private
3673 // but this really should have no meaning since CIR should not be using
3674 // this information to derive linkage information.
3675 mlir::SymbolTable::setSymbolVisibility(
3676 alias, mlir::SymbolTable::Visibility::Private);
3677
3678 // Alias constructors and destructors are always unnamed_addr.
3680
3681 if (op) {
3682 // Any existing users of the existing function declaration will be
3683 // referencing the function by flat symbol reference (i.e. the name), so
3684 // those uses will automatically resolve to the alias now that we've
3685 // replaced the function declaration. We can safely erase the existing
3686 // function declaration.
3687 assert(cast<cir::FuncOp>(op).getFunctionType() == alias.getFunctionType() &&
3688 "declaration exists with different type");
3690 op->erase();
3691 } else {
3692 // Name already set by createCIRFunction
3693 }
3694
3695 // Finally, set up the alias with its proper name and attributes.
3696 setCommonAttributes(aliasGD, alias);
3697}
3698
3700 return genTypes.convertType(type);
3701}
3702
3704 // Verify the module after we have finished constructing it, this will
3705 // check the structural properties of the IR and invoke any specific
3706 // verifiers we have on the CIR operations.
3707 return mlir::verify(theModule).succeeded();
3708}
3709
3710mlir::Attribute CIRGenModule::getAddrOfRTTIDescriptor(mlir::Location loc,
3711 QualType ty, bool forEh) {
3712 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
3713 // FIXME: should we even be calling this method if RTTI is disabled
3714 // and it's not for EH?
3715 if (!shouldEmitRTTI(forEh))
3716 return builder.getConstNullPtrAttr(builder.getUInt8PtrTy());
3717
3718 if (forEh && ty->isObjCObjectPointerType() &&
3719 langOpts.ObjCRuntime.isGNUFamily()) {
3720 errorNYI(loc, "getAddrOfRTTIDescriptor: Objc PtrType & Objc RT GUN");
3721 return {};
3722 }
3723
3724 return getCXXABI().getAddrOfRTTIDescriptor(loc, ty);
3725}
3726
3727// TODO(cir): this can be shared with LLVM codegen.
3729 const CXXRecordDecl *derivedClass,
3730 llvm::iterator_range<CastExpr::path_const_iterator> path) {
3731 CharUnits offset = CharUnits::Zero();
3732
3733 const ASTContext &astContext = getASTContext();
3734 const CXXRecordDecl *rd = derivedClass;
3735
3736 for (const CXXBaseSpecifier *base : path) {
3737 assert(!base->isVirtual() && "Should not see virtual bases here!");
3738
3739 // Get the layout.
3740 const ASTRecordLayout &layout = astContext.getASTRecordLayout(rd);
3741
3742 const auto *baseDecl = base->getType()->castAsCXXRecordDecl();
3743
3744 // Add the offset.
3745 offset += layout.getBaseClassOffset(baseDecl);
3746
3747 rd = baseDecl;
3748 }
3749
3750 return offset;
3751}
3752
3754 llvm::StringRef feature) {
3755 unsigned diagID = diags.getCustomDiagID(
3756 DiagnosticsEngine::Error, "ClangIR code gen Not Yet Implemented: %0");
3757 return diags.Report(loc, diagID) << feature;
3758}
3759
3761 llvm::StringRef feature) {
3762 return errorNYI(loc.getBegin(), feature) << loc;
3763}
3764
3766 unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
3767 getDiags().Report(astContext.getFullLoc(loc), diagID) << error;
3768}
3769
3770/// Print out an error that codegen doesn't support the specified stmt yet.
3771void CIRGenModule::errorUnsupported(const Stmt *s, llvm::StringRef type) {
3772 unsigned diagId = diags.getCustomDiagID(DiagnosticsEngine::Error,
3773 "cannot compile this %0 yet");
3774 diags.Report(astContext.getFullLoc(s->getBeginLoc()), diagId)
3775 << type << s->getSourceRange();
3776}
3777
3778/// Print out an error that codegen doesn't support the specified decl yet.
3779void CIRGenModule::errorUnsupported(const Decl *d, llvm::StringRef type) {
3780 unsigned diagId = diags.getCustomDiagID(DiagnosticsEngine::Error,
3781 "cannot compile this %0 yet");
3782 diags.Report(astContext.getFullLoc(d->getLocation()), diagId) << type;
3783}
3784
3785void CIRGenModule::mapBlockAddress(cir::BlockAddrInfoAttr blockInfo,
3786 cir::LabelOp label) {
3787 [[maybe_unused]] auto result =
3788 blockAddressInfoToLabel.try_emplace(blockInfo, label);
3789 assert(result.second &&
3790 "attempting to map a blockaddress info that is already mapped");
3791}
3792
3793void CIRGenModule::mapUnresolvedBlockAddress(cir::BlockAddressOp op) {
3794 [[maybe_unused]] auto result = unresolvedBlockAddressToLabel.insert(op);
3795 assert(result.second &&
3796 "attempting to map a blockaddress operation that is already mapped");
3797}
3798
3799void CIRGenModule::mapResolvedBlockAddress(cir::BlockAddressOp op,
3800 cir::LabelOp label) {
3801 [[maybe_unused]] auto result = blockAddressToLabel.try_emplace(op, label);
3802 assert(result.second &&
3803 "attempting to map a blockaddress operation that is already mapped");
3804}
3805
3807 cir::LabelOp newLabel) {
3808 auto *it = blockAddressToLabel.find(op);
3809 assert(it != blockAddressToLabel.end() &&
3810 "trying to update a blockaddress not previously mapped");
3811 assert(!it->second && "blockaddress already has a resolved label");
3812
3813 it->second = newLabel;
3814}
3815
3816cir::LabelOp
3817CIRGenModule::lookupBlockAddressInfo(cir::BlockAddrInfoAttr blockInfo) {
3818 return blockAddressInfoToLabel.lookup(blockInfo);
3819}
3820
3821mlir::Operation *
3823 const Expr *init) {
3824 assert((mte->getStorageDuration() == SD_Static ||
3825 mte->getStorageDuration() == SD_Thread) &&
3826 "not a global temporary");
3827 const auto *varDecl = cast<VarDecl>(mte->getExtendingDecl());
3828
3829 // Use the MaterializeTemporaryExpr's type if it has the same unqualified
3830 // base type as Init. This preserves cv-qualifiers (e.g. const from a
3831 // constexpr or const-ref binding) that skipRValueSubobjectAdjustments may
3832 // have dropped via NoOp casts, while correctly falling back to Init's type
3833 // when a real subobject adjustment changed the type (e.g. member access or
3834 // base-class cast in C++98), where E->getType() reflects the reference type,
3835 // not the actual storage type.
3836 QualType materializedType = init->getType();
3837 if (getASTContext().hasSameUnqualifiedType(mte->getType(), materializedType))
3838 materializedType = mte->getType();
3839
3840 CharUnits align = getASTContext().getTypeAlignInChars(materializedType);
3841 mlir::Location loc = getLoc(mte->getSourceRange());
3842
3843 // FIXME: If an externally-visible declaration extends multiple temporaries,
3844 // we need to give each temporary the same name in every translation unit (and
3845 // we also need to make the temporaries externally-visible).
3847 llvm::raw_svector_ostream out(name);
3849 varDecl, mte->getManglingNumber(), out);
3850
3851 auto insertResult = materializedGlobalTemporaryMap.insert({mte, nullptr});
3852 if (!insertResult.second) {
3853 mlir::Type type = getTypes().convertTypeForMem(materializedType);
3854 // We've seen this before: either we already created it or we're in the
3855 // process of doing so.
3856 if (!insertResult.first->second) {
3857 // We recursively re-entered this function, probably during emission of
3858 // the initializer. Create a placeholder.
3859 insertResult.first->second =
3860 createGlobalOp(loc, name, type, /*isConstant=*/false);
3861 }
3862 return insertResult.first->second;
3863 }
3864
3865 APValue *value = nullptr;
3866 if (mte->getStorageDuration() == SD_Static && varDecl->evaluateValue()) {
3867 // If the initializer of the extending declaration is a constant
3868 // initializer, we should have a cached constant initializer for this
3869 // temporay. Note taht this m ight have a different value from the value
3870 // computed by evaluating the initializer if the surrounding constant
3871 // expression modifies the temporary.
3872 value = mte->getOrCreateValue(/*MayCreate=*/false);
3873 }
3874
3875 // Try evaluating it now, it might have a constant initializer
3876 Expr::EvalResult evalResult;
3877 if (!value && init->EvaluateAsRValue(evalResult, getASTContext()) &&
3878 !evalResult.hasSideEffects())
3879 value = &evalResult.Val;
3880
3882
3883 std::optional<ConstantEmitter> emitter;
3884 mlir::Attribute initialValue = nullptr;
3885 bool isConstant = false;
3886 mlir::Type type;
3887
3888 if (value) {
3889 emitter.emplace(*this);
3890 initialValue = emitter->emitForInitializer(*value, materializedType);
3891
3892 isConstant = materializedType.isConstantStorage(
3893 getASTContext(), /*ExcludeCtor=*/value, /*ExcludeDtor=*/false);
3894
3895 type = mlir::cast<mlir::TypedAttr>(initialValue).getType();
3896 } else {
3897 // No initializer, the initialization will be provided when we initialize
3898 // the declaration which performed lifetime extension.
3899 type = getTypes().convertTypeForMem(materializedType);
3900 }
3901
3902 // Create a global variable for this lifetime-extended temporary.
3903 cir::GlobalLinkageKind linkage = getCIRLinkageVarDefinition(varDecl);
3904 if (linkage == cir::GlobalLinkageKind::ExternalLinkage) {
3905 const VarDecl *initVD;
3906 if (varDecl->isStaticDataMember() && varDecl->getAnyInitializer(initVD) &&
3908 // Temporaries defined inside a class get linkonce_odr linkage because the
3909 // calss can be defined in multiple translation units.
3910 errorNYI(mte->getSourceRange(), "static data member initialization");
3911 } else {
3912 // There is no need for this temporary to have external linkage if the
3913 // VarDecl has external linkage.
3914 linkage = cir::GlobalLinkageKind::InternalLinkage;
3915 }
3916 }
3917 cir::GlobalOp gv = createGlobalOp(loc, name, type, isConstant);
3918 gv.setInitialValueAttr(initialValue);
3919 gv.setLinkage(linkage);
3920 gv.setVisibility(getMLIRVisibilityFromCIRLinkage(linkage));
3921
3922 if (emitter)
3923 emitter->finalize(gv);
3924 // Don't assign dllimport or dllexport to local linkage globals
3925 if (!gv.hasLocalLinkage()) {
3928 }
3929
3930 gv.setAlignment(align.getAsAlign().value());
3931 if (supportsCOMDAT() && gv.isWeakForLinker())
3932 errorNYI(mte->getSourceRange(),
3933 "Global temporary with comdat/weak linkage");
3934 if (varDecl->getTLSKind())
3935 setTLSMode(gv, *varDecl, /*isExtendingDecl=*/true);
3936 mlir::Operation *cv = gv;
3937
3939
3940 // Update the map with the new temporary. If we created a placeholder above,
3941 // erase it as well, the name will have been the same, so our symbol
3942 // references would have been correct. We still do a 'replaceAllUsesWith' in
3943 // case some sort of expression formed a reference to the placeholder
3944 // temporary.
3945 mlir::Operation *&entry = materializedGlobalTemporaryMap[mte];
3946 if (entry) {
3947 entry->replaceAllUsesWith(cv);
3948 eraseGlobalSymbol(entry);
3949 entry->erase();
3950 }
3951 entry = cv;
3952
3953 return cv;
3954}
3955
3957 const UnnamedGlobalConstantDecl *gcd) {
3958 unsigned numEntries = unnamedGlobalConstantDeclMap.size();
3959 cir::GlobalOp *globalOpEntry = &unnamedGlobalConstantDeclMap[gcd];
3960
3961 if (*globalOpEntry)
3962 return *globalOpEntry;
3963
3964 ConstantEmitter emitter(*this);
3965
3966 const APValue &value = gcd->getValue();
3967 assert(!value.isAbsent());
3969 "emitForInitializer should take gcd->getType().getAddressSpace()");
3970 mlir::Attribute init = emitter.emitForInitializer(value, gcd->getType());
3971 auto typedInit = dyn_cast<mlir::TypedAttr>(init);
3972
3973 if (!typedInit)
3974 errorNYI(gcd->getSourceRange(),
3975 "getAddrOfUnnamedGlobalConstantDecl: non-typed initializer");
3976
3978
3979 // Classic codegen always creates these with .constant, then counts on the
3980 // auto-addition of '.#'. CIR global doesn't have this, so we'll just auto-add
3981 // one if this isn't the first. We could probably choose a better name than
3982 // .constant to be unique for this type of decl, but this is consistent with
3983 // classic codegen.
3984 std::string name = numEntries == 0
3985 ? ".constant"
3986 : (Twine(".constant.") + Twine(numEntries)).str();
3987 auto globalOp = createGlobalOp(builder.getUnknownLoc(), name,
3988 typedInit.getType(), /*is_constant=*/true);
3989 globalOp.setLinkage(cir::GlobalLinkageKind::PrivateLinkage);
3990
3991 CharUnits alignment = getASTContext().getTypeAlignInChars(gcd->getType());
3992 globalOp.setAlignment(alignment.getAsAlign().value());
3993 CIRGenModule::setInitializer(globalOp, init);
3994
3995 emitter.finalize(globalOp);
3996 *globalOpEntry = globalOp;
3997 return globalOp;
3998}
3999
4000cir::GlobalOp
4002 StringRef name = getMangledName(tpo);
4003 CharUnits alignment = getNaturalTypeAlignment(tpo->getType());
4004
4005 if (auto globalOp =
4006 mlir::dyn_cast_or_null<cir::GlobalOp>(getGlobalValue(name)))
4007 return globalOp;
4008
4009 ConstantEmitter emitter(*this);
4011 "emitForInitializer should take tpo->getType().getAddressSpace()");
4012 mlir::Attribute init =
4013 emitter.emitForInitializer(tpo->getValue(), tpo->getType());
4014
4015 if (!init) {
4016 errorUnsupported(tpo, "template parameter object");
4017 return {};
4018 }
4019
4020 mlir::TypedAttr typedInit = cast<mlir::TypedAttr>(init);
4021
4022 cir::GlobalLinkageKind linkage =
4024 ? cir::GlobalLinkageKind::LinkOnceODRLinkage
4025 : cir::GlobalLinkageKind::InternalLinkage;
4026
4028 auto globalOp = createGlobalOp(builder.getUnknownLoc(), name,
4029 typedInit.getType(), /*is_constant=*/true);
4030 globalOp.setLinkage(linkage);
4031 globalOp.setAlignment(alignment.getAsAlign().value());
4032 globalOp.setComdat(supportsCOMDAT() &&
4033 linkage == cir::GlobalLinkageKind::LinkOnceODRLinkage);
4034
4035 CIRGenModule::setInitializer(globalOp, init);
4036 emitter.finalize(globalOp);
4037
4038 insertGlobalSymbol(globalOp);
4039
4040 return globalOp;
4041}
4042
4043//===----------------------------------------------------------------------===//
4044// Annotations
4045//===----------------------------------------------------------------------===//
4046
4047mlir::ArrayAttr
4048CIRGenModule::getOrCreateAnnotationArgs(const AnnotateAttr *attr) {
4049 ArrayRef<Expr *> exprs = {attr->args_begin(), attr->args_size()};
4050 // Return a null attr for no-args annotations so OptionalParameter omits
4051 // the args portion entirely from the printed IR.
4052 if (exprs.empty())
4053 return {};
4054
4055 llvm::FoldingSetNodeID id;
4056 for (Expr *e : exprs)
4057 id.Add(cast<clang::ConstantExpr>(e)->getAPValueResult());
4058
4059 mlir::ArrayAttr &lookup = annotationArgs[id.ComputeHash()];
4060 if (lookup)
4061 return lookup;
4062
4064 args.reserve(exprs.size());
4065 for (Expr *e : exprs) {
4066 if (auto *strE = dyn_cast<clang::StringLiteral>(e->IgnoreParenCasts())) {
4067 args.push_back(builder.getStringAttr(strE->getString()));
4068 } else if (auto *intE =
4069 dyn_cast<clang::IntegerLiteral>(e->IgnoreParenCasts())) {
4070 auto intTy = builder.getIntegerType(intE->getValue().getBitWidth());
4071 args.push_back(builder.getIntegerAttr(intTy, intE->getValue()));
4072 } else {
4073 errorNYI(e->getExprLoc(), "annotation argument expression");
4074 }
4075 }
4076
4077 return lookup = builder.getArrayAttr(args);
4078}
4079
4080cir::AnnotationAttr CIRGenModule::emitAnnotateAttr(const AnnotateAttr *aa) {
4081 mlir::StringAttr annoGV = builder.getStringAttr(aa->getAnnotation());
4082 mlir::ArrayAttr args = getOrCreateAnnotationArgs(aa);
4083 return cir::AnnotationAttr::get(&getMLIRContext(), annoGV, args);
4084}
4085
4087 mlir::Operation *gv) {
4088 assert(d->hasAttr<AnnotateAttr>() && "no annotate attribute");
4089 assert((isa<cir::GlobalOp>(gv) || isa<cir::FuncOp>(gv)) &&
4090 "annotation only on globals");
4092 for (const auto *i : d->specific_attrs<AnnotateAttr>())
4093 annotations.push_back(emitAnnotateAttr(i));
4094 if (auto global = dyn_cast<cir::GlobalOp>(gv))
4095 global.setAnnotationsAttr(builder.getArrayAttr(annotations));
4096 else if (auto func = dyn_cast<cir::FuncOp>(gv))
4097 func.setAnnotationsAttr(builder.getArrayAttr(annotations));
4098}
4099
4100void CIRGenModule::emitGlobalAnnotations() {
4101 for (const auto &[mangledName, vd] : deferredAnnotations) {
4102 mlir::Operation *gv = getGlobalValue(mangledName);
4103 if (gv)
4104 addGlobalAnnotations(vd, gv);
4105 }
4106 deferredAnnotations.clear();
4107}
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 void setLinkageForFunction(CIRGenModule &cgm, cir::FuncOp &func, const NamedDecl *nd)
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 std::vector< std::string > getFeatureDeltaFromDefault(const CIRGenModule &cgm, llvm::StringRef targetCPU, llvm::StringMap< bool > &featureMap)
Get the feature delta from the default feature map for the given target CPU.
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 bool verifyPointerTypeArgs(cir::FuncOp oldF, cir::FuncOp newF, mlir::SymbolUserMap &userMap)
static cir::CtorKind getCtorKindFromDecl(const CXXConstructorDecl *ctor)
static void emitUsed(CIRGenModule &cgm, StringRef name, std::vector< cir::CIRGlobalValueInterface > &list)
static cir::GlobalViewAttr createNewGlobalView(CIRGenModule &cgm, cir::GlobalOp newGlob, cir::GlobalViewAttr attr, mlir::Type oldTy)
This file defines OpenACC nodes for declarative directives.
TokenType getType() const
Returns the token's type, e.g.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
Defines the SourceManager interface.
This file defines OpenMP AST classes for executable directives and clauses.
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
bool isAbsent() const
Definition APValue.h:481
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
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:882
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.
DiagnosticsEngine & getDiagnostics() const
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:921
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 void handleGlobalReplace(cir::GlobalOp oldGV, cir::GlobalOp newGV)
virtual mlir::Operation * getKernelHandle(cir::FuncOp fn, GlobalDecl gd)=0
virtual void finalizeModule()
Perform module finalization: on device side, mark ODR-used device variables as compiler-used.
virtual void internalizeDeviceSideVar(const VarDecl *d, cir::GlobalLinkageKind &linkage)=0
Adjust linkage of shadow variables in host compilation.
virtual void handleVarRegistration(const VarDecl *vd, cir::GlobalOp var)=0
Check whether a variable is a device variable and register it if true.
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.
cir::GlobalOp getAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl *gcd)
void updateResolvedBlockAddress(cir::BlockAddressOp op, cir::LabelOp newLabel)
void setGlobalVisibility(cir::CIRGlobalValueInterface gv, const NamedDecl *d) const
Set the visibility for the given global.
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.
llvm::StringRef getMangledName(clang::GlobalDecl gd)
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *derivedClass, llvm::iterator_range< CastExpr::path_const_iterator > path)
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
void insertGlobalSymbol(mlir::Operation *op)
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)
void setGlobalTlsReferences(const VarDecl &vd, cir::GlobalOp globalOp)
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.
cir::GlobalOp getAddrOfTemplateParamObject(const TemplateParamObjectDecl *tpo)
Get the GlobalOp of a template parameter object.
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.
llvm::DenseMap< const UnnamedGlobalConstantDecl *, cir::GlobalOp > unnamedGlobalConstantDeclMap
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 emitAliasDefinition(GlobalDecl gd)
Emit a definition for an __attribute__((alias)) declaration.
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)
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 setTLSMode(mlir::Operation *op, const VarDecl &d, bool isExtendingDecl=false)
Set TLS mode for the given operation based on the given variable declaration.
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
void eraseGlobalSymbol(mlir::Operation *op)
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.
llvm::StringMap< mlir::Operation * > symbolLookupCache
Cache for O(1) symbol lookups by name, replacing the O(N) linear scan in SymbolTable::lookupSymbolIn ...
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 printPostfixForExternalizedDecl(llvm::raw_ostream &os, const Decl *d)
Print the postfix for externalized static variable or kernels for single source offloading languages ...
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::GlobalOp createGlobalOp(mlir::Location loc, llvm::StringRef name, mlir::Type t, bool isConstant=false, mlir::ptr::MemorySpaceAttrInterface addrSpace={}, mlir::Operation *insertPoint=nullptr)
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)
static cir::VisibilityKind getCIRVisibilityKind(Visibility v)
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 addGlobalAnnotations(const clang::ValueDecl *d, mlir::Operation *gv)
Add global annotations for a global value (GlobalOp or FuncOp).
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)
CIRGenOpenMPRuntime & getOpenMPRuntime()
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,...
void emitDeclareTargetFunction(const FunctionDecl *fd, cir::FuncOp funcOp)
If the function has an OMPDeclareTargetDeclAttr, set the corresponding omp.declare_target attribute o...
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.
mlir::Attribute emitForInitializer(const APValue &value, QualType destType)
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:64
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:118
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a C++ constructor within a class.
Definition DeclCXX.h:2633
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:3061
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:3056
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition DeclCXX.cpp:3047
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition DeclCXX.cpp:2749
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition DeclCXX.cpp:2728
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:2339
bool hasDefinition() const
Definition DeclCXX.h:561
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
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:3824
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
bool isInExportDeclContext() const
Whether this declaration was exported in a lexical context.
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:3182
Cached information about one file (either on disk or in the virtual file system).
Definition FileEntry.h:273
StringRef tryGetRealPathName() const
Definition FileEntry.h:298
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:2018
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:2207
bool hasPrototype() const
Whether this function has a prototype, either because one was explicitly written or because it was "i...
Definition Decl.h:2461
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition Decl.h:2300
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3173
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4567
CallingConv getCallConv() const
Definition TypeBase.h:4922
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
CXXCtorType getCtorType() const
Definition GlobalDecl.h:108
GlobalDecl getCanonicalDecl() const
Definition GlobalDecl.h:97
KernelReferenceKind getKernelReferenceKind() const
Definition GlobalDecl.h:135
GlobalDecl getWithDecl(const Decl *D)
Definition GlobalDecl.h:172
unsigned getMultiVersionIndex() const
Definition GlobalDecl.h:125
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
std::string CUID
The user provided compilation unit ID, if non-empty.
Visibility getVisibility() const
Definition Visibility.h:89
void setLinkage(Linkage L)
Definition Visibility.h:92
Linkage getLinkage() const
Definition Visibility.h:88
bool isVisibilityExplicit() const
Definition Visibility.h:90
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:3717
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:1808
void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex)
Definition Decl.h:1841
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:2931
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:8573
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8487
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8520
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
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1802
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Expr.h:1976
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:3739
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition TargetInfo.h:327
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
bool isReadOnlyFeature(StringRef Feature) const
Determine whether the given target feature is read only.
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
virtual ParsedTargetAttr parseTargetAttr(StringRef Str) const
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
std::vector< std::string > Features
The list of target specific features to enable or disable – this should be a list of strings starting...
std::string TuneCPU
If given, the name of the target CPU to tune code for.
std::string CPU
If given, the name of the target CPU to generate code for.
A template parameter object.
const APValue & getValue() const
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:8783
bool isPointerType() const
Definition TypeBase.h:8684
bool isReferenceType() const
Definition TypeBase.h:8708
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition Type.cpp:5475
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:790
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2864
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition Type.cpp:5484
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition Type.cpp:2528
bool isObjCObjectPointerType() const
Definition TypeBase.h:8863
bool isMemberFunctionPointerType() const
Definition TypeBase.h:8769
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9277
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
Expr * getSubExpr() const
Definition Expr.h:2288
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
Definition DeclCXX.h:4473
const APValue & getValue() const
Definition DeclCXX.h:4499
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:924
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1582
TLSKind getTLSKind() const
Definition Decl.cpp:2147
bool hasInit() const
Definition Decl.cpp:2377
DefinitionKind isThisDeclarationADefinition(ASTContext &) const
Check whether this declaration is a definition.
Definition Decl.cpp:2239
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2169
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
Definition Decl.cpp:2820
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1239
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2627
VarDecl * getDefinition(ASTContext &)
Get the real (not just tentative) definition for this declaration.
Definition Decl.cpp:2345
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1206
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition Decl.cpp:2809
const Expr * getInit() const
Definition Decl.h:1381
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition Decl.h:1230
@ TLS_None
Not a TLS variable.
Definition Decl.h:944
@ DeclarationOnly
This declaration is only a declaration.
Definition Decl.h:1308
@ Definition
This declaration is definitely a definition.
Definition Decl.h:1314
DefinitionKind hasDefinition(ASTContext &) const
Check whether this variable is defined in this translation unit.
Definition Decl.cpp:2354
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:2737
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1371
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)
std::unique_ptr< TargetCIRGenInfo > createSPIRVTargetCIRGenInfo(CIRGenTypes &cgt)
Definition SPIRV.cpp:56
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
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ 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
@ HiddenVisibility
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition Visibility.h:37
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 opFuncMultiVersioning()
static bool sourceLanguageCases()
static bool shouldSkipAliasEmission()
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 checkAliases()
static bool generateDebugInfo()
static bool targetCIRGenInfoOS()
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