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