clang 22.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 "CIRGenCXXABI.h"
16#include "CIRGenFunction.h"
17
19#include "clang/AST/DeclBase.h"
29
30#include "CIRGenFunctionInfo.h"
31#include "mlir/IR/BuiltinOps.h"
32#include "mlir/IR/Location.h"
33#include "mlir/IR/MLIRContext.h"
34#include "mlir/IR/Verifier.h"
35
36#include <algorithm>
37
38using namespace clang;
39using namespace clang::CIRGen;
40
42 switch (cgm.getASTContext().getCXXABIKind()) {
43 case TargetCXXABI::GenericItanium:
44 case TargetCXXABI::GenericAArch64:
45 case TargetCXXABI::AppleARM64:
46 return CreateCIRGenItaniumCXXABI(cgm);
47
48 case TargetCXXABI::Fuchsia:
49 case TargetCXXABI::GenericARM:
50 case TargetCXXABI::iOS:
51 case TargetCXXABI::WatchOS:
52 case TargetCXXABI::GenericMIPS:
53 case TargetCXXABI::WebAssembly:
54 case TargetCXXABI::XL:
55 case TargetCXXABI::Microsoft:
56 cgm.errorNYI("C++ ABI kind not yet implemented");
57 return nullptr;
58 }
59
60 llvm_unreachable("invalid C++ ABI kind");
61}
62
63CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
64 clang::ASTContext &astContext,
65 const clang::CodeGenOptions &cgo,
66 DiagnosticsEngine &diags)
67 : builder(mlirContext, *this), astContext(astContext),
68 langOpts(astContext.getLangOpts()), codeGenOpts(cgo),
69 theModule{mlir::ModuleOp::create(mlir::UnknownLoc::get(&mlirContext))},
70 diags(diags), target(astContext.getTargetInfo()),
71 abi(createCXXABI(*this)), genTypes(*this), vtables(*this) {
72
73 // Initialize cached types
74 voidTy = cir::VoidType::get(&getMLIRContext());
75 voidPtrTy = cir::PointerType::get(voidTy);
76 sInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/true);
77 sInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/true);
78 sInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/true);
79 sInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/true);
80 sInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/true);
81 uInt8Ty = cir::IntType::get(&getMLIRContext(), 8, /*isSigned=*/false);
82 uInt8PtrTy = cir::PointerType::get(uInt8Ty);
84 uInt16Ty = cir::IntType::get(&getMLIRContext(), 16, /*isSigned=*/false);
85 uInt32Ty = cir::IntType::get(&getMLIRContext(), 32, /*isSigned=*/false);
86 uInt64Ty = cir::IntType::get(&getMLIRContext(), 64, /*isSigned=*/false);
87 uInt128Ty = cir::IntType::get(&getMLIRContext(), 128, /*isSigned=*/false);
88 fP16Ty = cir::FP16Type::get(&getMLIRContext());
89 bFloat16Ty = cir::BF16Type::get(&getMLIRContext());
90 floatTy = cir::SingleType::get(&getMLIRContext());
91 doubleTy = cir::DoubleType::get(&getMLIRContext());
92 fP80Ty = cir::FP80Type::get(&getMLIRContext());
93 fP128Ty = cir::FP128Type::get(&getMLIRContext());
94
95 allocaInt8PtrTy = cir::PointerType::get(uInt8Ty, cirAllocaAddressSpace);
96
98 astContext
99 .toCharUnitsFromBits(
100 astContext.getTargetInfo().getPointerAlign(LangAS::Default))
101 .getQuantity();
102
103 const unsigned charSize = astContext.getTargetInfo().getCharWidth();
104 uCharTy = cir::IntType::get(&getMLIRContext(), charSize, /*isSigned=*/false);
105
106 // TODO(CIR): Should be updated once TypeSizeInfoAttr is upstreamed
107 const unsigned sizeTypeSize =
108 astContext.getTypeSize(astContext.getSignedSizeType());
109 SizeSizeInBytes = astContext.toCharUnitsFromBits(sizeTypeSize).getQuantity();
110 // In CIRGenTypeCache, UIntPtrTy and SizeType are fields of the same union
111 uIntPtrTy =
112 cir::IntType::get(&getMLIRContext(), sizeTypeSize, /*isSigned=*/false);
113 ptrDiffTy =
114 cir::IntType::get(&getMLIRContext(), sizeTypeSize, /*isSigned=*/true);
115
116 std::optional<cir::SourceLanguage> sourceLanguage = getCIRSourceLanguage();
117 if (sourceLanguage)
118 theModule->setAttr(
119 cir::CIRDialect::getSourceLanguageAttrName(),
120 cir::SourceLanguageAttr::get(&mlirContext, *sourceLanguage));
121 theModule->setAttr(cir::CIRDialect::getTripleAttrName(),
122 builder.getStringAttr(getTriple().str()));
123
124 if (cgo.OptimizationLevel > 0 || cgo.OptimizeSize > 0)
125 theModule->setAttr(cir::CIRDialect::getOptInfoAttrName(),
126 cir::OptInfoAttr::get(&mlirContext,
127 cgo.OptimizationLevel,
128 cgo.OptimizeSize));
129 // Set the module name to be the name of the main file. TranslationUnitDecl
130 // often contains invalid source locations and isn't a reliable source for the
131 // module location.
132 FileID mainFileId = astContext.getSourceManager().getMainFileID();
133 const FileEntry &mainFile =
134 *astContext.getSourceManager().getFileEntryForID(mainFileId);
135 StringRef path = mainFile.tryGetRealPathName();
136 if (!path.empty()) {
137 theModule.setSymName(path);
138 theModule->setLoc(mlir::FileLineColLoc::get(&mlirContext, path,
139 /*line=*/0,
140 /*column=*/0));
141 }
142}
143
145
146/// FIXME: this could likely be a common helper and not necessarily related
147/// with codegen.
148/// Return the best known alignment for an unknown pointer to a
149/// particular class.
151 if (!rd->hasDefinition())
152 return CharUnits::One(); // Hopefully won't be used anywhere.
153
154 auto &layout = astContext.getASTRecordLayout(rd);
155
156 // If the class is final, then we know that the pointer points to an
157 // object of that type and can use the full alignment.
158 if (rd->isEffectivelyFinal())
159 return layout.getAlignment();
160
161 // Otherwise, we have to assume it could be a subclass.
162 return layout.getNonVirtualAlignment();
163}
164
166 LValueBaseInfo *baseInfo) {
168
169 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown, but
170 // that doesn't return the information we need to compute baseInfo.
171
172 // Honor alignment typedef attributes even on incomplete types.
173 // We also honor them straight for C++ class types, even as pointees;
174 // there's an expressivity gap here.
175 if (const auto *tt = t->getAs<TypedefType>()) {
176 if (unsigned align = tt->getDecl()->getMaxAlignment()) {
177 if (baseInfo)
179 return astContext.toCharUnitsFromBits(align);
180 }
181 }
182
183 // Analyze the base element type, so we don't get confused by incomplete
184 // array types.
185 t = astContext.getBaseElementType(t);
186
187 if (t->isIncompleteType()) {
188 // We could try to replicate the logic from
189 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
190 // type is incomplete, so it's impossible to test. We could try to reuse
191 // getTypeAlignIfKnown, but that doesn't return the information we need
192 // to set baseInfo. So just ignore the possibility that the alignment is
193 // greater than one.
194 if (baseInfo)
196 return CharUnits::One();
197 }
198
199 if (baseInfo)
201
202 CharUnits alignment;
203 if (t.getQualifiers().hasUnaligned()) {
204 alignment = CharUnits::One();
205 } else {
207 alignment = astContext.getTypeAlignInChars(t);
208 }
209
210 // Cap to the global maximum type alignment unless the alignment
211 // was somehow explicit on the type.
212 if (unsigned maxAlign = astContext.getLangOpts().MaxTypeAlign) {
213 if (alignment.getQuantity() > maxAlign &&
214 !astContext.isAlignmentRequired(t))
215 alignment = CharUnits::fromQuantity(maxAlign);
216 }
217 return alignment;
218}
219
221 if (theTargetCIRGenInfo)
222 return *theTargetCIRGenInfo;
223
224 const llvm::Triple &triple = getTarget().getTriple();
225 switch (triple.getArch()) {
226 default:
228
229 // Currently we just fall through to x86_64.
230 [[fallthrough]];
231
232 case llvm::Triple::x86_64: {
233 switch (triple.getOS()) {
234 default:
236
237 // Currently we just fall through to x86_64.
238 [[fallthrough]];
239
240 case llvm::Triple::Linux:
241 theTargetCIRGenInfo = createX8664TargetCIRGenInfo(genTypes);
242 return *theTargetCIRGenInfo;
243 }
244 }
245 }
246}
247
249 assert(cLoc.isValid() && "expected valid source location");
250 const SourceManager &sm = astContext.getSourceManager();
251 PresumedLoc pLoc = sm.getPresumedLoc(cLoc);
252 StringRef filename = pLoc.getFilename();
253 return mlir::FileLineColLoc::get(builder.getStringAttr(filename),
254 pLoc.getLine(), pLoc.getColumn());
255}
256
257mlir::Location CIRGenModule::getLoc(SourceRange cRange) {
258 assert(cRange.isValid() && "expected a valid source range");
259 mlir::Location begin = getLoc(cRange.getBegin());
260 mlir::Location end = getLoc(cRange.getEnd());
261 mlir::Attribute metadata;
262 return mlir::FusedLoc::get({begin, end}, metadata, builder.getContext());
263}
264
265mlir::Operation *
267 const Decl *d = gd.getDecl();
268
270 return getAddrOfCXXStructor(gd, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
271 /*DontDefer=*/false, isForDefinition);
272
273 if (isa<CXXMethodDecl>(d)) {
274 const CIRGenFunctionInfo &fi =
276 cir::FuncType ty = getTypes().getFunctionType(fi);
277 return getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false,
278 isForDefinition);
279 }
280
281 if (isa<FunctionDecl>(d)) {
283 cir::FuncType ty = getTypes().getFunctionType(fi);
284 return getAddrOfFunction(gd, ty, /*ForVTable=*/false, /*DontDefer=*/false,
285 isForDefinition);
286 }
287
288 return getAddrOfGlobalVar(cast<VarDecl>(d), /*ty=*/nullptr, isForDefinition)
289 .getDefiningOp();
290}
291
293 // We call getAddrOfGlobal with isForDefinition set to ForDefinition in
294 // order to get a Value with exactly the type we need, not something that
295 // might have been created for another decl with the same mangled name but
296 // different type.
297 mlir::Operation *op = getAddrOfGlobal(d, ForDefinition);
298
299 // In case of different address spaces, we may still get a cast, even with
300 // IsForDefinition equal to ForDefinition. Query mangled names table to get
301 // GlobalValue.
302 if (!op)
304
305 assert(op && "expected a valid global op");
306
307 // Check to see if we've already emitted this. This is necessary for a
308 // couple of reasons: first, decls can end up in deferred-decls queue
309 // multiple times, and second, decls can end up with definitions in unusual
310 // ways (e.g. by an extern inline function acquiring a strong function
311 // redefinition). Just ignore those cases.
312 // TODO: Not sure what to map this to for MLIR
313 mlir::Operation *globalValueOp = op;
314 if (auto gv = dyn_cast<cir::GetGlobalOp>(op))
315 globalValueOp =
316 mlir::SymbolTable::lookupSymbolIn(getModule(), gv.getNameAttr());
317
318 if (auto cirGlobalValue =
319 dyn_cast<cir::CIRGlobalValueInterface>(globalValueOp))
320 if (!cirGlobalValue.isDeclaration())
321 return;
322
323 // If this is OpenMP, check if it is legal to emit this global normally.
325
326 // Otherwise, emit the definition and move on to the next one.
328}
329
331 // Emit code for any potentially referenced deferred decls. Since a previously
332 // unused static decl may become used during the generation of code for a
333 // static function, iterate until no changes are made.
334
338
339 // Stop if we're out of both deferred vtables and deferred declarations.
340 if (deferredDeclsToEmit.empty())
341 return;
342
343 // Grab the list of decls to emit. If emitGlobalDefinition schedules more
344 // work, it will not interfere with this.
345 std::vector<GlobalDecl> curDeclsToEmit;
346 curDeclsToEmit.swap(deferredDeclsToEmit);
347
348 for (const GlobalDecl &d : curDeclsToEmit) {
350
351 // If we found out that we need to emit more decls, do that recursively.
352 // This has the advantage that the decls are emitted in a DFS and related
353 // ones are close together, which is convenient for testing.
354 if (!deferredDeclsToEmit.empty()) {
355 emitDeferred();
356 assert(deferredDeclsToEmit.empty());
357 }
358 }
359}
360
362 if (const auto *cd = dyn_cast<clang::OpenACCConstructDecl>(gd.getDecl())) {
364 return;
365 }
366
367 // TODO(OMP): The logic in this function for the 'rest' of the OpenMP
368 // declarative declarations is complicated and needs to be done on a per-kind
369 // basis, so all of that needs to be added when we implement the individual
370 // global-allowed declarations. See uses of `cir::MissingFeatures::openMP
371 // throughout this function.
372
373 const auto *global = cast<ValueDecl>(gd.getDecl());
374
375 if (const auto *fd = dyn_cast<FunctionDecl>(global)) {
376 // Update deferred annotations with the latest declaration if the function
377 // was already used or defined.
378 if (fd->hasAttr<AnnotateAttr>())
379 errorNYI(fd->getSourceRange(), "deferredAnnotations");
380 if (!fd->doesThisDeclarationHaveABody()) {
381 if (!fd->doesDeclarationForceExternallyVisibleDefinition())
382 return;
383
384 errorNYI(fd->getSourceRange(),
385 "function declaration that forces code gen");
386 return;
387 }
388 } else {
389 const auto *vd = cast<VarDecl>(global);
390 assert(vd->isFileVarDecl() && "Cannot emit local var decl as global.");
391 if (vd->isThisDeclarationADefinition() != VarDecl::Definition &&
392 !astContext.isMSStaticDataMemberInlineDefinition(vd)) {
394 // If this declaration may have caused an inline variable definition to
395 // change linkage, make sure that it's emitted.
396 if (astContext.getInlineVariableDefinitionKind(vd) ==
399 // Otherwise, we can ignore this declaration. The variable will be emitted
400 // on its first use.
401 return;
402 }
403 }
404
405 // Defer code generation to first use when possible, e.g. if this is an inline
406 // function. If the global must always be emitted, do it eagerly if possible
407 // to benefit from cache locality. Deferring code generation is necessary to
408 // avoid adding initializers to external declarations.
409 if (mustBeEmitted(global) && mayBeEmittedEagerly(global)) {
410 // Emit the definition if it can't be deferred.
412 return;
413 }
414
415 // If we're deferring emission of a C++ variable with an initializer, remember
416 // the order in which it appeared on the file.
418
419 llvm::StringRef mangledName = getMangledName(gd);
420 if (getGlobalValue(mangledName) != nullptr) {
421 // The value has already been used and should therefore be emitted.
423 } else if (mustBeEmitted(global)) {
424 // The value must be emitted, but cannot be emitted eagerly.
425 assert(!mayBeEmittedEagerly(global));
427 } else {
428 // Otherwise, remember that we saw a deferred decl with this name. The first
429 // use of the mangled name will cause it to move into deferredDeclsToEmit.
430 deferredDecls[mangledName] = gd;
431 }
432}
433
435 mlir::Operation *op) {
436 auto const *funcDecl = cast<FunctionDecl>(gd.getDecl());
438 cir::FuncType funcType = getTypes().getFunctionType(fi);
439 cir::FuncOp funcOp = dyn_cast_if_present<cir::FuncOp>(op);
440 if (!funcOp || funcOp.getFunctionType() != funcType) {
441 funcOp = getAddrOfFunction(gd, funcType, /*ForVTable=*/false,
442 /*DontDefer=*/true, ForDefinition);
443 }
444
445 // Already emitted.
446 if (!funcOp.isDeclaration())
447 return;
448
449 setFunctionLinkage(gd, funcOp);
450 setGVProperties(funcOp, funcDecl);
452 maybeSetTrivialComdat(*funcDecl, funcOp);
454
455 CIRGenFunction cgf(*this, builder);
456 curCGF = &cgf;
457 {
458 mlir::OpBuilder::InsertionGuard guard(builder);
459 cgf.generateCode(gd, funcOp, funcType);
460 }
461 curCGF = nullptr;
462
463 setNonAliasAttributes(gd, funcOp);
465
466 auto getPriority = [this](const auto *attr) -> int {
467 Expr *e = attr->getPriority();
468 if (e)
469 return e->EvaluateKnownConstInt(this->getASTContext()).getExtValue();
470 return attr->DefaultPriority;
471 };
472
473 if (const ConstructorAttr *ca = funcDecl->getAttr<ConstructorAttr>())
474 addGlobalCtor(funcOp, getPriority(ca));
475 if (const DestructorAttr *da = funcDecl->getAttr<DestructorAttr>())
476 addGlobalDtor(funcOp, getPriority(da));
477
478 if (funcDecl->getAttr<AnnotateAttr>())
479 errorNYI(funcDecl->getSourceRange(), "deferredAnnotations");
480}
481
482/// Track functions to be called before main() runs.
483void CIRGenModule::addGlobalCtor(cir::FuncOp ctor,
484 std::optional<int> priority) {
487
488 // Traditional LLVM codegen directly adds the function to the list of global
489 // ctors. In CIR we just add a global_ctor attribute to the function. The
490 // global list is created in LoweringPrepare.
491 //
492 // FIXME(from traditional LLVM): Type coercion of void()* types.
493 ctor.setGlobalCtorPriority(priority);
494}
495
496/// Add a function to the list that will be called when the module is unloaded.
497void CIRGenModule::addGlobalDtor(cir::FuncOp dtor,
498 std::optional<int> priority) {
499 if (codeGenOpts.RegisterGlobalDtorsWithAtExit &&
500 (!getASTContext().getTargetInfo().getTriple().isOSAIX()))
501 errorNYI(dtor.getLoc(), "registerGlobalDtorsWithAtExit");
502
503 // FIXME(from traditional LLVM): Type coercion of void()* types.
504 dtor.setGlobalDtorPriority(priority);
505}
506
509 if (dk == VarDecl::Definition && vd->hasAttr<DLLImportAttr>())
510 return;
511
513 // If we have a definition, this might be a deferred decl. If the
514 // instantiation is explicit, make sure we emit it at the end.
517
519}
520
521mlir::Operation *CIRGenModule::getGlobalValue(StringRef name) {
522 return mlir::SymbolTable::lookupSymbolIn(theModule, name);
523}
524
525cir::GlobalOp CIRGenModule::createGlobalOp(CIRGenModule &cgm,
526 mlir::Location loc, StringRef name,
527 mlir::Type t, bool isConstant,
528 mlir::Operation *insertPoint) {
529 cir::GlobalOp g;
530 CIRGenBuilderTy &builder = cgm.getBuilder();
531
532 {
533 mlir::OpBuilder::InsertionGuard guard(builder);
534
535 // If an insertion point is provided, we're replacing an existing global,
536 // otherwise, create the new global immediately after the last gloabl we
537 // emitted.
538 if (insertPoint) {
539 builder.setInsertionPoint(insertPoint);
540 } else {
541 // Group global operations together at the top of the module.
542 if (cgm.lastGlobalOp)
543 builder.setInsertionPointAfter(cgm.lastGlobalOp);
544 else
545 builder.setInsertionPointToStart(cgm.getModule().getBody());
546 }
547
548 g = cir::GlobalOp::create(builder, loc, name, t, isConstant);
549 if (!insertPoint)
550 cgm.lastGlobalOp = g;
551
552 // Default to private until we can judge based on the initializer,
553 // since MLIR doesn't allow public declarations.
554 mlir::SymbolTable::setSymbolVisibility(
555 g, mlir::SymbolTable::Visibility::Private);
556 }
557 return g;
558}
559
560void CIRGenModule::setCommonAttributes(GlobalDecl gd, mlir::Operation *gv) {
561 const Decl *d = gd.getDecl();
562 if (isa_and_nonnull<NamedDecl>(d))
563 setGVProperties(gv, dyn_cast<NamedDecl>(d));
566}
567
568void CIRGenModule::setNonAliasAttributes(GlobalDecl gd, mlir::Operation *op) {
569 setCommonAttributes(gd, op);
570
575
577}
578
579std::optional<cir::SourceLanguage> CIRGenModule::getCIRSourceLanguage() const {
580 using ClangStd = clang::LangStandard;
581 using CIRLang = cir::SourceLanguage;
582 auto opts = getLangOpts();
583
584 if (opts.CPlusPlus)
585 return CIRLang::CXX;
586 if (opts.C99 || opts.C11 || opts.C17 || opts.C23 || opts.C2y ||
587 opts.LangStd == ClangStd::lang_c89 ||
588 opts.LangStd == ClangStd::lang_gnu89)
589 return CIRLang::C;
590
591 // TODO(cir): support remaining source languages.
593 errorNYI("CIR does not yet support the given source language");
594 return std::nullopt;
595}
596
597static void setLinkageForGV(cir::GlobalOp &gv, const NamedDecl *nd) {
598 // Set linkage and visibility in case we never see a definition.
600 // Don't set internal linkage on declarations.
601 // "extern_weak" is overloaded in LLVM; we probably should have
602 // separate linkage types for this.
604 (nd->hasAttr<WeakAttr>() || nd->isWeakImported()))
605 gv.setLinkage(cir::GlobalLinkageKind::ExternalWeakLinkage);
606}
607
608/// If the specified mangled name is not in the module,
609/// create and return an mlir GlobalOp with the specified type (TODO(cir):
610/// address space).
611///
612/// TODO(cir):
613/// 1. If there is something in the module with the specified name, return
614/// it potentially bitcasted to the right type.
615///
616/// 2. If \p d is non-null, it specifies a decl that correspond to this. This
617/// is used to set the attributes on the global when it is first created.
618///
619/// 3. If \p isForDefinition is true, it is guaranteed that an actual global
620/// with type \p ty will be returned, not conversion of a variable with the same
621/// mangled name but some other type.
622cir::GlobalOp
623CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
624 LangAS langAS, const VarDecl *d,
625 ForDefinition_t isForDefinition) {
626 // Lookup the entry, lazily creating it if necessary.
627 cir::GlobalOp entry;
628 if (mlir::Operation *v = getGlobalValue(mangledName)) {
629 if (!isa<cir::GlobalOp>(v))
630 errorNYI(d->getSourceRange(), "global with non-GlobalOp type");
631 entry = cast<cir::GlobalOp>(v);
632 }
633
634 if (entry) {
637
640
641 if (entry.getSymType() == ty)
642 return entry;
643
644 // If there are two attempts to define the same mangled name, issue an
645 // error.
646 //
647 // TODO(cir): look at mlir::GlobalValue::isDeclaration for all aspects of
648 // recognizing the global as a declaration, for now only check if
649 // initializer is present.
650 if (isForDefinition && !entry.isDeclaration()) {
651 errorNYI(d->getSourceRange(), "global with conflicting type");
652 }
653
654 // Address space check removed because it is unnecessary because CIR records
655 // address space info in types.
656
657 // (If global is requested for a definition, we always need to create a new
658 // global, not just return a bitcast.)
659 if (!isForDefinition)
660 return entry;
661 }
662
663 mlir::Location loc = getLoc(d->getSourceRange());
664
665 // Calculate constant storage flag before creating the global. This was moved
666 // from after the global creation to ensure the constant flag is set correctly
667 // at creation time, matching the logic used in emitCXXGlobalVarDeclInit.
668 bool isConstant = false;
669 if (d) {
670 bool needsDtor =
672 isConstant = d->getType().isConstantStorage(
673 astContext, /*ExcludeCtor=*/true, /*ExcludeDtor=*/!needsDtor);
674 }
675
676 // mlir::SymbolTable::Visibility::Public is the default, no need to explicitly
677 // mark it as such.
678 cir::GlobalOp gv =
679 CIRGenModule::createGlobalOp(*this, loc, mangledName, ty, isConstant,
680 /*insertPoint=*/entry.getOperation());
681
682 // This is the first use or definition of a mangled name. If there is a
683 // deferred decl with this name, remember that we need to emit it at the end
684 // of the file.
685 auto ddi = deferredDecls.find(mangledName);
686 if (ddi != deferredDecls.end()) {
687 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
688 // list, and remove it from DeferredDecls (since we don't need it anymore).
689 addDeferredDeclToEmit(ddi->second);
690 deferredDecls.erase(ddi);
691 }
692
693 // Handle things which are present even on external declarations.
694 if (d) {
695 if (langOpts.OpenMP && !langOpts.OpenMPSimd)
696 errorNYI(d->getSourceRange(), "OpenMP target global variable");
697
698 gv.setAlignmentAttr(getSize(astContext.getDeclAlign(d)));
699
700 setLinkageForGV(gv, d);
701
702 if (d->getTLSKind()) {
704 errorNYI(d->getSourceRange(), "TLS dynamic");
705 setTLSMode(gv, *d);
706 }
707
708 setGVProperties(gv, d);
709
710 // If required by the ABI, treat declarations of static data members with
711 // inline initializers as definitions.
712 if (astContext.isMSStaticDataMemberInlineDefinition(d))
713 errorNYI(d->getSourceRange(), "MS static data member inline definition");
714
716 gv.setGlobalVisibilityAttr(getGlobalVisibilityAttrFromDecl(d));
717
718 // Handle XCore specific ABI requirements.
719 if (getTriple().getArch() == llvm::Triple::xcore)
720 errorNYI(d->getSourceRange(), "XCore specific ABI requirements");
721
722 // Check if we a have a const declaration with an initializer, we may be
723 // able to emit it as available_externally to expose it's value to the
724 // optimizer.
725 if (getLangOpts().CPlusPlus && gv.isPublic() &&
726 d->getType().isConstQualified() && gv.isDeclaration() &&
727 !d->hasDefinition() && d->hasInit() && !d->hasAttr<DLLImportAttr>())
729 "external const declaration with initializer");
730 }
731
732 return gv;
733}
734
735cir::GlobalOp
737 ForDefinition_t isForDefinition) {
738 assert(d->hasGlobalStorage() && "Not a global variable");
739 QualType astTy = d->getType();
740 if (!ty)
741 ty = getTypes().convertTypeForMem(astTy);
742
743 StringRef mangledName = getMangledName(d);
744 return getOrCreateCIRGlobal(mangledName, ty, astTy.getAddressSpace(), d,
745 isForDefinition);
746}
747
748/// Return the mlir::Value for the address of the given global variable. If
749/// \p ty is non-null and if the global doesn't exist, then it will be created
750/// with the specified type instead of whatever the normal requested type would
751/// be. If \p isForDefinition is true, it is guaranteed that an actual global
752/// with type \p ty will be returned, not conversion of a variable with the same
753/// mangled name but some other type.
754mlir::Value CIRGenModule::getAddrOfGlobalVar(const VarDecl *d, mlir::Type ty,
755 ForDefinition_t isForDefinition) {
756 assert(d->hasGlobalStorage() && "Not a global variable");
757 QualType astTy = d->getType();
758 if (!ty)
759 ty = getTypes().convertTypeForMem(astTy);
760
761 bool tlsAccess = d->getTLSKind() != VarDecl::TLS_None;
762 cir::GlobalOp g = getOrCreateCIRGlobal(d, ty, isForDefinition);
763 mlir::Type ptrTy = builder.getPointerTo(g.getSymType());
764 return cir::GetGlobalOp::create(builder, getLoc(d->getSourceRange()), ptrTy,
765 g.getSymNameAttr(), tlsAccess);
766}
767
768cir::GlobalViewAttr CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *d) {
769 assert(d->hasGlobalStorage() && "Not a global variable");
770 mlir::Type ty = getTypes().convertTypeForMem(d->getType());
771
772 cir::GlobalOp globalOp = getOrCreateCIRGlobal(d, ty, NotForDefinition);
774 cir::PointerType ptrTy = builder.getPointerTo(globalOp.getSymType());
775 return builder.getGlobalViewAttr(ptrTy, globalOp);
776}
777
779 bool isTentative) {
780 if (getLangOpts().OpenCL || getLangOpts().OpenMPIsTargetDevice) {
781 errorNYI(vd->getSourceRange(), "emit OpenCL/OpenMP global variable");
782 return;
783 }
784
785 // Whether the definition of the variable is available externally.
786 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
787 // since this is the job for its original source.
788 bool isDefinitionAvailableExternally =
789 astContext.GetGVALinkageForVariable(vd) == GVA_AvailableExternally;
790
791 // It is useless to emit the definition for an available_externally variable
792 // which can't be marked as const.
793 if (isDefinitionAvailableExternally &&
795 // TODO: Update this when we have interface to check constexpr
796 // destructor.
797 vd->needsDestruction(astContext) ||
798 !vd->getType().isConstantStorage(astContext, true, true)))
799 return;
800
801 mlir::Attribute init;
802 bool needsGlobalCtor = false;
803 bool needsGlobalDtor =
804 !isDefinitionAvailableExternally &&
806 const VarDecl *initDecl;
807 const Expr *initExpr = vd->getAnyInitializer(initDecl);
808
809 std::optional<ConstantEmitter> emitter;
810
812
813 if (vd->hasAttr<LoaderUninitializedAttr>()) {
814 errorNYI(vd->getSourceRange(), "loader uninitialized attribute");
815 return;
816 } else if (!initExpr) {
817 // This is a tentative definition; tentative definitions are
818 // implicitly initialized with { 0 }.
819 //
820 // Note that tentative definitions are only emitted at the end of
821 // a translation unit, so they should never have incomplete
822 // type. In addition, EmitTentativeDefinition makes sure that we
823 // never attempt to emit a tentative definition if a real one
824 // exists. A use may still exists, however, so we still may need
825 // to do a RAUW.
826 assert(!vd->getType()->isIncompleteType() && "Unexpected incomplete type");
827 init = builder.getZeroInitAttr(convertType(vd->getType()));
828 } else {
829 emitter.emplace(*this);
830 mlir::Attribute initializer = emitter->tryEmitForInitializer(*initDecl);
831 if (!initializer) {
832 QualType qt = initExpr->getType();
833 if (vd->getType()->isReferenceType())
834 qt = vd->getType();
835
836 if (getLangOpts().CPlusPlus) {
837 if (initDecl->hasFlexibleArrayInit(astContext))
838 errorNYI(vd->getSourceRange(), "flexible array initializer");
839 init = builder.getZeroInitAttr(convertType(qt));
840 if (!isDefinitionAvailableExternally)
841 needsGlobalCtor = true;
842 } else {
843 errorNYI(vd->getSourceRange(), "static initializer");
844 }
845 } else {
846 init = initializer;
847 // We don't need an initializer, so remove the entry for the delayed
848 // initializer position (just in case this entry was delayed) if we
849 // also don't need to register a destructor.
851 }
852 }
853
854 mlir::Type initType;
855 if (mlir::isa<mlir::SymbolRefAttr>(init)) {
856 errorNYI(vd->getSourceRange(), "global initializer is a symbol reference");
857 return;
858 } else {
859 assert(mlir::isa<mlir::TypedAttr>(init) && "This should have a type");
860 auto typedInitAttr = mlir::cast<mlir::TypedAttr>(init);
861 initType = typedInitAttr.getType();
862 }
863 assert(!mlir::isa<mlir::NoneType>(initType) && "Should have a type by now");
864
865 cir::GlobalOp gv =
866 getOrCreateCIRGlobal(vd, initType, ForDefinition_t(!isTentative));
867 // TODO(cir): Strip off pointer casts from Entry if we get them?
868
869 if (!gv || gv.getSymType() != initType) {
870 errorNYI(vd->getSourceRange(), "global initializer with type mismatch");
871 return;
872 }
873
875
876 if (vd->hasAttr<AnnotateAttr>()) {
877 errorNYI(vd->getSourceRange(), "annotate global variable");
878 }
879
880 if (langOpts.CUDA) {
881 errorNYI(vd->getSourceRange(), "CUDA global variable");
882 }
883
884 // Set initializer and finalize emission
886 if (emitter)
887 emitter->finalize(gv);
888
889 // If it is safe to mark the global 'constant', do so now.
890 // Use the same logic as classic codegen EmitGlobalVarDefinition.
891 gv.setConstant((vd->hasAttr<CUDAConstantAttr>() && langOpts.CUDAIsDevice) ||
892 (!needsGlobalCtor && !needsGlobalDtor &&
893 vd->getType().isConstantStorage(astContext,
894 /*ExcludeCtor=*/true,
895 /*ExcludeDtor=*/true)));
897
898 // Set CIR's linkage type as appropriate.
899 cir::GlobalLinkageKind linkage =
900 getCIRLinkageVarDefinition(vd, /*IsConstant=*/false);
901
902 // Set CIR linkage and DLL storage class.
903 gv.setLinkage(linkage);
904 // FIXME(cir): setLinkage should likely set MLIR's visibility automatically.
905 gv.setVisibility(getMLIRVisibilityFromCIRLinkage(linkage));
907 if (linkage == cir::GlobalLinkageKind::CommonLinkage) {
908 // common vars aren't constant even if declared const.
909 gv.setConstant(false);
910 // Tentative definition of global variables may be initialized with
911 // non-zero null pointers. In this case they should have weak linkage
912 // since common linkage must have zero initializer and must not have
913 // explicit section therefore cannot have non-zero initial value.
914 std::optional<mlir::Attribute> initializer = gv.getInitialValue();
915 if (initializer && !getBuilder().isNullValue(*initializer))
916 gv.setLinkage(cir::GlobalLinkageKind::WeakAnyLinkage);
917 }
918
919 setNonAliasAttributes(vd, gv);
920
922
923 maybeSetTrivialComdat(*vd, gv);
924
925 // Emit the initializer function if necessary.
926 if (needsGlobalCtor || needsGlobalDtor)
927 emitCXXGlobalVarDeclInitFunc(vd, gv, needsGlobalCtor);
928}
929
931 mlir::Operation *op) {
932 const auto *decl = cast<ValueDecl>(gd.getDecl());
933 if (const auto *fd = dyn_cast<FunctionDecl>(decl)) {
934 // TODO(CIR): Skip generation of CIR for functions with available_externally
935 // linkage at -O0.
936
937 if (const auto *method = dyn_cast<CXXMethodDecl>(decl)) {
938 // Make sure to emit the definition(s) before we emit the thunks. This is
939 // necessary for the generation of certain thunks.
940 if (isa<CXXConstructorDecl>(method) || isa<CXXDestructorDecl>(method))
941 abi->emitCXXStructor(gd);
942 else if (fd->isMultiVersion())
943 errorNYI(method->getSourceRange(), "multiversion functions");
944 else
946
947 if (method->isVirtual())
949
950 return;
951 }
952
953 if (fd->isMultiVersion())
954 errorNYI(fd->getSourceRange(), "multiversion functions");
956 return;
957 }
958
959 if (const auto *vd = dyn_cast<VarDecl>(decl))
960 return emitGlobalVarDefinition(vd, !vd->hasDefinition());
961
962 llvm_unreachable("Invalid argument to CIRGenModule::emitGlobalDefinition");
963}
964
965mlir::Attribute
967 assert(!e->getType()->isPointerType() && "Strings are always arrays");
968
969 // Don't emit it as the address of the string, emit the string data itself
970 // as an inline array.
971 if (e->getCharByteWidth() == 1) {
972 SmallString<64> str(e->getString());
973
974 // Resize the string to the right size, which is indicated by its type.
975 const ConstantArrayType *cat =
976 astContext.getAsConstantArrayType(e->getType());
977 uint64_t finalSize = cat->getZExtSize();
978 str.resize(finalSize);
979
980 mlir::Type eltTy = convertType(cat->getElementType());
981 return builder.getString(str, eltTy, finalSize);
982 }
983
984 auto arrayTy = mlir::cast<cir::ArrayType>(convertType(e->getType()));
985
986 auto arrayEltTy = mlir::cast<cir::IntType>(arrayTy.getElementType());
987
988 uint64_t arraySize = arrayTy.getSize();
989 unsigned literalSize = e->getLength();
990 assert(arraySize == literalSize + 1 &&
991 "wide string literal array size must be literal length plus null "
992 "terminator");
993
994 // Check if the string is all null bytes before building the vector.
995 // In most non-zero cases, this will break out on the first element.
996 bool isAllZero = true;
997 for (unsigned i = 0; i < literalSize; ++i) {
998 if (e->getCodeUnit(i) != 0) {
999 isAllZero = false;
1000 break;
1001 }
1002 }
1003
1004 if (isAllZero)
1005 return cir::ZeroAttr::get(arrayTy);
1006
1007 // Otherwise emit a constant array holding the characters.
1009 elements.reserve(arraySize);
1010 for (unsigned i = 0; i < literalSize; ++i)
1011 elements.push_back(cir::IntAttr::get(arrayEltTy, e->getCodeUnit(i)));
1012 // Add null terminator
1013 elements.push_back(cir::IntAttr::get(arrayEltTy, 0));
1014
1015 auto elementsAttr = mlir::ArrayAttr::get(&getMLIRContext(), elements);
1016 return builder.getConstArray(elementsAttr, arrayTy);
1017}
1018
1020 return getTriple().supportsCOMDAT();
1021}
1022
1023static bool shouldBeInCOMDAT(CIRGenModule &cgm, const Decl &d) {
1024 if (!cgm.supportsCOMDAT())
1025 return false;
1026
1027 if (d.hasAttr<SelectAnyAttr>())
1028 return true;
1029
1030 GVALinkage linkage;
1031 if (auto *vd = dyn_cast<VarDecl>(&d))
1032 linkage = cgm.getASTContext().GetGVALinkageForVariable(vd);
1033 else
1034 linkage =
1036
1037 switch (linkage) {
1041 return false;
1044 return true;
1045 }
1046 llvm_unreachable("No such linkage");
1047}
1048
1049void CIRGenModule::maybeSetTrivialComdat(const Decl &d, mlir::Operation *op) {
1050 if (!shouldBeInCOMDAT(*this, d))
1051 return;
1052 if (auto globalOp = dyn_cast_or_null<cir::GlobalOp>(op)) {
1053 globalOp.setComdat(true);
1054 } else {
1055 auto funcOp = cast<cir::FuncOp>(op);
1056 funcOp.setComdat(true);
1057 }
1058}
1059
1061 // Make sure that this type is translated.
1062 genTypes.updateCompletedType(td);
1063}
1064
1065void CIRGenModule::addReplacement(StringRef name, mlir::Operation *op) {
1066 replacements[name] = op;
1067}
1068
1069void CIRGenModule::replacePointerTypeArgs(cir::FuncOp oldF, cir::FuncOp newF) {
1070 std::optional<mlir::SymbolTable::UseRange> optionalUseRange =
1071 oldF.getSymbolUses(theModule);
1072 if (!optionalUseRange)
1073 return;
1074
1075 for (const mlir::SymbolTable::SymbolUse &u : *optionalUseRange) {
1076 // CallTryOp only shows up after FlattenCFG.
1077 auto call = mlir::dyn_cast<cir::CallOp>(u.getUser());
1078 if (!call)
1079 continue;
1080
1081 for (const auto [argOp, fnArgType] :
1082 llvm::zip(call.getArgs(), newF.getFunctionType().getInputs())) {
1083 if (argOp.getType() == fnArgType)
1084 continue;
1085
1086 // The purpose of this entire function is to insert bitcasts in the case
1087 // where these types don't match, but I haven't seen a case where that
1088 // happens.
1089 errorNYI(call.getLoc(), "replace call with mismatched types");
1090 }
1091 }
1092}
1093
1094void CIRGenModule::applyReplacements() {
1095 for (auto &i : replacements) {
1096 StringRef mangledName = i.first();
1097 mlir::Operation *replacement = i.second;
1098 mlir::Operation *entry = getGlobalValue(mangledName);
1099 if (!entry)
1100 continue;
1101 assert(isa<cir::FuncOp>(entry) && "expected function");
1102 auto oldF = cast<cir::FuncOp>(entry);
1103 auto newF = dyn_cast<cir::FuncOp>(replacement);
1104 if (!newF) {
1105 // In classic codegen, this can be a global alias, a bitcast, or a GEP.
1106 errorNYI(replacement->getLoc(), "replacement is not a function");
1107 continue;
1108 }
1109
1110 // LLVM has opaque pointer but CIR not. So we may have to handle these
1111 // different pointer types when performing replacement.
1112 replacePointerTypeArgs(oldF, newF);
1113
1114 // Replace old with new, but keep the old order.
1115 if (oldF.replaceAllSymbolUses(newF.getSymNameAttr(), theModule).failed())
1116 llvm_unreachable("internal error, cannot RAUW symbol");
1117 if (newF) {
1118 newF->moveBefore(oldF);
1119 oldF->erase();
1120 }
1121 }
1122}
1123
1125 mlir::Location loc, StringRef name, mlir::Type ty,
1126 cir::GlobalLinkageKind linkage, clang::CharUnits alignment) {
1127 auto gv = mlir::dyn_cast_or_null<cir::GlobalOp>(
1128 mlir::SymbolTable::lookupSymbolIn(theModule, name));
1129
1130 if (gv) {
1131 // Check if the variable has the right type.
1132 if (gv.getSymType() == ty)
1133 return gv;
1134
1135 // Because of C++ name mangling, the only way we can end up with an already
1136 // existing global with the same name is if it has been declared extern
1137 // "C".
1138 assert(gv.isDeclaration() && "Declaration has wrong type!");
1139
1140 errorNYI(loc, "createOrReplaceCXXRuntimeVariable: declaration exists with "
1141 "wrong type");
1142 return gv;
1143 }
1144
1145 // Create a new variable.
1146 gv = createGlobalOp(*this, loc, name, ty);
1147
1148 // Set up extra information and add to the module
1149 gv.setLinkageAttr(
1150 cir::GlobalLinkageKindAttr::get(&getMLIRContext(), linkage));
1151 mlir::SymbolTable::setSymbolVisibility(gv,
1153
1154 if (supportsCOMDAT() && cir::isWeakForLinker(linkage) &&
1155 !gv.hasAvailableExternallyLinkage()) {
1156 gv.setComdat(true);
1157 }
1158
1159 gv.setAlignmentAttr(getSize(alignment));
1160 setDSOLocal(static_cast<mlir::Operation *>(gv));
1161 return gv;
1162}
1163
1164// TODO(CIR): this could be a common method between LLVM codegen.
1165static bool isVarDeclStrongDefinition(const ASTContext &astContext,
1166 CIRGenModule &cgm, const VarDecl *vd,
1167 bool noCommon) {
1168 // Don't give variables common linkage if -fno-common was specified unless it
1169 // was overridden by a NoCommon attribute.
1170 if ((noCommon || vd->hasAttr<NoCommonAttr>()) && !vd->hasAttr<CommonAttr>())
1171 return true;
1172
1173 // C11 6.9.2/2:
1174 // A declaration of an identifier for an object that has file scope without
1175 // an initializer, and without a storage-class specifier or with the
1176 // storage-class specifier static, constitutes a tentative definition.
1177 if (vd->getInit() || vd->hasExternalStorage())
1178 return true;
1179
1180 // A variable cannot be both common and exist in a section.
1181 if (vd->hasAttr<SectionAttr>())
1182 return true;
1183
1184 // A variable cannot be both common and exist in a section.
1185 // We don't try to determine which is the right section in the front-end.
1186 // If no specialized section name is applicable, it will resort to default.
1187 if (vd->hasAttr<PragmaClangBSSSectionAttr>() ||
1188 vd->hasAttr<PragmaClangDataSectionAttr>() ||
1189 vd->hasAttr<PragmaClangRelroSectionAttr>() ||
1190 vd->hasAttr<PragmaClangRodataSectionAttr>())
1191 return true;
1192
1193 // Thread local vars aren't considered common linkage.
1194 if (vd->getTLSKind())
1195 return true;
1196
1197 // Tentative definitions marked with WeakImportAttr are true definitions.
1198 if (vd->hasAttr<WeakImportAttr>())
1199 return true;
1200
1201 // A variable cannot be both common and exist in a comdat.
1202 if (shouldBeInCOMDAT(cgm, *vd))
1203 return true;
1204
1205 // Declarations with a required alignment do not have common linkage in MSVC
1206 // mode.
1207 if (astContext.getTargetInfo().getCXXABI().isMicrosoft()) {
1208 if (vd->hasAttr<AlignedAttr>())
1209 return true;
1210 QualType varType = vd->getType();
1211 if (astContext.isAlignmentRequired(varType))
1212 return true;
1213
1214 if (const auto *rd = varType->getAsRecordDecl()) {
1215 for (const FieldDecl *fd : rd->fields()) {
1216 if (fd->isBitField())
1217 continue;
1218 if (fd->hasAttr<AlignedAttr>())
1219 return true;
1220 if (astContext.isAlignmentRequired(fd->getType()))
1221 return true;
1222 }
1223 }
1224 }
1225
1226 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
1227 // common symbols, so symbols with greater alignment requirements cannot be
1228 // common.
1229 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
1230 // alignments for common symbols via the aligncomm directive, so this
1231 // restriction only applies to MSVC environments.
1232 if (astContext.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
1233 astContext.getTypeAlignIfKnown(vd->getType()) >
1234 astContext.toBits(CharUnits::fromQuantity(32)))
1235 return true;
1236
1237 return false;
1238}
1239
1241 const DeclaratorDecl *dd, GVALinkage linkage, bool isConstantVariable) {
1242 if (linkage == GVA_Internal)
1243 return cir::GlobalLinkageKind::InternalLinkage;
1244
1245 if (dd->hasAttr<WeakAttr>()) {
1246 if (isConstantVariable)
1247 return cir::GlobalLinkageKind::WeakODRLinkage;
1248 return cir::GlobalLinkageKind::WeakAnyLinkage;
1249 }
1250
1251 if (const auto *fd = dd->getAsFunction())
1252 if (fd->isMultiVersion() && linkage == GVA_AvailableExternally)
1253 return cir::GlobalLinkageKind::LinkOnceAnyLinkage;
1254
1255 // We are guaranteed to have a strong definition somewhere else,
1256 // so we can use available_externally linkage.
1257 if (linkage == GVA_AvailableExternally)
1258 return cir::GlobalLinkageKind::AvailableExternallyLinkage;
1259
1260 // Note that Apple's kernel linker doesn't support symbol
1261 // coalescing, so we need to avoid linkonce and weak linkages there.
1262 // Normally, this means we just map to internal, but for explicit
1263 // instantiations we'll map to external.
1264
1265 // In C++, the compiler has to emit a definition in every translation unit
1266 // that references the function. We should use linkonce_odr because
1267 // a) if all references in this translation unit are optimized away, we
1268 // don't need to codegen it. b) if the function persists, it needs to be
1269 // merged with other definitions. c) C++ has the ODR, so we know the
1270 // definition is dependable.
1271 if (linkage == GVA_DiscardableODR)
1272 return !astContext.getLangOpts().AppleKext
1273 ? cir::GlobalLinkageKind::LinkOnceODRLinkage
1274 : cir::GlobalLinkageKind::InternalLinkage;
1275
1276 // An explicit instantiation of a template has weak linkage, since
1277 // explicit instantiations can occur in multiple translation units
1278 // and must all be equivalent. However, we are not allowed to
1279 // throw away these explicit instantiations.
1280 //
1281 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
1282 // so say that CUDA templates are either external (for kernels) or internal.
1283 // This lets llvm perform aggressive inter-procedural optimizations. For
1284 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
1285 // therefore we need to follow the normal linkage paradigm.
1286 if (linkage == GVA_StrongODR) {
1287 if (getLangOpts().AppleKext)
1288 return cir::GlobalLinkageKind::ExternalLinkage;
1289 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
1290 !getLangOpts().GPURelocatableDeviceCode)
1291 return dd->hasAttr<CUDAGlobalAttr>()
1292 ? cir::GlobalLinkageKind::ExternalLinkage
1293 : cir::GlobalLinkageKind::InternalLinkage;
1294 return cir::GlobalLinkageKind::WeakODRLinkage;
1295 }
1296
1297 // C++ doesn't have tentative definitions and thus cannot have common
1298 // linkage.
1299 if (!getLangOpts().CPlusPlus && isa<VarDecl>(dd) &&
1300 !isVarDeclStrongDefinition(astContext, *this, cast<VarDecl>(dd),
1301 getCodeGenOpts().NoCommon))
1302 return cir::GlobalLinkageKind::CommonLinkage;
1303
1304 // selectany symbols are externally visible, so use weak instead of
1305 // linkonce. MSVC optimizes away references to const selectany globals, so
1306 // all definitions should be the same and ODR linkage should be used.
1307 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
1308 if (dd->hasAttr<SelectAnyAttr>())
1309 return cir::GlobalLinkageKind::WeakODRLinkage;
1310
1311 // Otherwise, we have strong external linkage.
1312 assert(linkage == GVA_StrongExternal);
1313 return cir::GlobalLinkageKind::ExternalLinkage;
1314}
1315
1316/// This function is called when we implement a function with no prototype, e.g.
1317/// "int foo() {}". If there are existing call uses of the old function in the
1318/// module, this adjusts them to call the new function directly.
1319///
1320/// This is not just a cleanup: the always_inline pass requires direct calls to
1321/// functions to be able to inline them. If there is a bitcast in the way, it
1322/// won't inline them. Instcombine normally deletes these calls, but it isn't
1323/// run at -O0.
1325 mlir::Operation *old, cir::FuncOp newFn) {
1326 // If we're redefining a global as a function, don't transform it.
1327 auto oldFn = mlir::dyn_cast<cir::FuncOp>(old);
1328 if (!oldFn)
1329 return;
1330
1331 // TODO(cir): this RAUW ignores the features below.
1335 if (oldFn->getAttrs().size() <= 1)
1336 errorNYI(old->getLoc(),
1337 "replaceUsesOfNonProtoTypeWithRealFunction: Attribute forwarding");
1338
1339 // Mark new function as originated from a no-proto declaration.
1340 newFn.setNoProto(oldFn.getNoProto());
1341
1342 // Iterate through all calls of the no-proto function.
1343 std::optional<mlir::SymbolTable::UseRange> symUses =
1344 oldFn.getSymbolUses(oldFn->getParentOp());
1345 for (const mlir::SymbolTable::SymbolUse &use : symUses.value()) {
1346 mlir::OpBuilder::InsertionGuard guard(builder);
1347
1348 if (auto noProtoCallOp = mlir::dyn_cast<cir::CallOp>(use.getUser())) {
1349 builder.setInsertionPoint(noProtoCallOp);
1350
1351 // Patch call type with the real function type.
1352 cir::CallOp realCallOp = builder.createCallOp(
1353 noProtoCallOp.getLoc(), newFn, noProtoCallOp.getOperands());
1354
1355 // Replace old no proto call with fixed call.
1356 noProtoCallOp.replaceAllUsesWith(realCallOp);
1357 noProtoCallOp.erase();
1358 } else if (auto getGlobalOp =
1359 mlir::dyn_cast<cir::GetGlobalOp>(use.getUser())) {
1360 // Replace type
1361 getGlobalOp.getAddr().setType(
1362 cir::PointerType::get(newFn.getFunctionType()));
1363 } else {
1364 errorNYI(use.getUser()->getLoc(),
1365 "replaceUsesOfNonProtoTypeWithRealFunction: unexpected use");
1366 }
1367 }
1368}
1369
1370cir::GlobalLinkageKind
1372 assert(!isConstant && "constant variables NYI");
1373 GVALinkage linkage = astContext.GetGVALinkageForVariable(vd);
1374 return getCIRLinkageForDeclarator(vd, linkage, isConstant);
1375}
1376
1378 const auto *d = cast<FunctionDecl>(gd.getDecl());
1379
1380 GVALinkage linkage = astContext.GetGVALinkageForFunction(d);
1381
1382 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(d))
1383 return getCXXABI().getCXXDestructorLinkage(linkage, dtor, gd.getDtorType());
1384
1385 return getCIRLinkageForDeclarator(d, linkage, /*isConstantVariable=*/false);
1386}
1387
1388static cir::GlobalOp
1389generateStringLiteral(mlir::Location loc, mlir::TypedAttr c,
1390 cir::GlobalLinkageKind lt, CIRGenModule &cgm,
1391 StringRef globalName, CharUnits alignment) {
1393
1394 // Create a global variable for this string
1395 // FIXME(cir): check for insertion point in module level.
1396 cir::GlobalOp gv = CIRGenModule::createGlobalOp(
1397 cgm, loc, globalName, c.getType(), !cgm.getLangOpts().WritableStrings);
1398
1399 // Set up extra information and add to the module
1400 gv.setAlignmentAttr(cgm.getSize(alignment));
1401 gv.setLinkageAttr(
1402 cir::GlobalLinkageKindAttr::get(cgm.getBuilder().getContext(), lt));
1406 if (gv.isWeakForLinker()) {
1407 assert(cgm.supportsCOMDAT() && "Only COFF uses weak string literals");
1408 gv.setComdat(true);
1409 }
1410 cgm.setDSOLocal(static_cast<mlir::Operation *>(gv));
1411 return gv;
1412}
1413
1414// LLVM IR automatically uniques names when new llvm::GlobalVariables are
1415// created. This is handy, for example, when creating globals for string
1416// literals. Since we don't do that when creating cir::GlobalOp's, we need
1417// a mechanism to generate a unique name in advance.
1418//
1419// For now, this mechanism is only used in cases where we know that the
1420// name is compiler-generated, so we don't use the MLIR symbol table for
1421// the lookup.
1422std::string CIRGenModule::getUniqueGlobalName(const std::string &baseName) {
1423 // If this is the first time we've generated a name for this basename, use
1424 // it as is and start a counter for this base name.
1425 auto it = cgGlobalNames.find(baseName);
1426 if (it == cgGlobalNames.end()) {
1427 cgGlobalNames[baseName] = 1;
1428 return baseName;
1429 }
1430
1431 std::string result =
1432 baseName + "." + std::to_string(cgGlobalNames[baseName]++);
1433 // There should not be any symbol with this name in the module.
1434 assert(!mlir::SymbolTable::lookupSymbolIn(theModule, result));
1435 return result;
1436}
1437
1438/// Return a pointer to a constant array for the given string literal.
1440 StringRef name) {
1441 CharUnits alignment =
1442 astContext.getAlignOfGlobalVarInChars(s->getType(), /*VD=*/nullptr);
1443
1444 mlir::Attribute c = getConstantArrayFromStringLiteral(s);
1445
1446 cir::GlobalOp gv;
1447 if (!getLangOpts().WritableStrings && constantStringMap.count(c)) {
1448 gv = constantStringMap[c];
1449 // The bigger alignment always wins.
1450 if (!gv.getAlignment() ||
1451 uint64_t(alignment.getQuantity()) > *gv.getAlignment())
1452 gv.setAlignmentAttr(getSize(alignment));
1453 } else {
1454 // Mangle the string literal if that's how the ABI merges duplicate strings.
1455 // Don't do it if they are writable, since we don't want writes in one TU to
1456 // affect strings in another.
1457 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(s) &&
1458 !getLangOpts().WritableStrings) {
1459 errorNYI(s->getSourceRange(),
1460 "getGlobalForStringLiteral: mangle string literals");
1461 }
1462
1463 // Unlike LLVM IR, CIR doesn't automatically unique names for globals, so
1464 // we need to do that explicitly.
1465 std::string uniqueName = getUniqueGlobalName(name.str());
1466 // Synthetic string literals (e.g., from SourceLocExpr) may not have valid
1467 // source locations. Use unknown location in those cases.
1468 mlir::Location loc = s->getBeginLoc().isValid()
1469 ? getLoc(s->getSourceRange())
1470 : builder.getUnknownLoc();
1471 auto typedC = llvm::cast<mlir::TypedAttr>(c);
1472 gv = generateStringLiteral(loc, typedC,
1473 cir::GlobalLinkageKind::PrivateLinkage, *this,
1474 uniqueName, alignment);
1475 setDSOLocal(static_cast<mlir::Operation *>(gv));
1476 constantStringMap[c] = gv;
1477
1479 }
1480 return gv;
1481}
1482
1483/// Return a pointer to a constant array for the given string literal.
1484cir::GlobalViewAttr
1486 StringRef name) {
1487 cir::GlobalOp gv = getGlobalForStringLiteral(s, name);
1488 auto arrayTy = mlir::dyn_cast<cir::ArrayType>(gv.getSymType());
1489 assert(arrayTy && "String literal must be array");
1491 cir::PointerType ptrTy = getBuilder().getPointerTo(arrayTy.getElementType());
1492
1493 return builder.getGlobalViewAttr(ptrTy, gv);
1494}
1495
1496// TODO(cir): this could be a common AST helper for both CIR and LLVM codegen.
1498 if (getLangOpts().OpenCL)
1500
1501 // For temporaries inside functions, CUDA treats them as normal variables.
1502 // LangAS::cuda_device, on the other hand, is reserved for those variables
1503 // explicitly marked with __device__.
1504 if (getLangOpts().CUDAIsDevice)
1505 return LangAS::Default;
1506
1507 if (getLangOpts().SYCLIsDevice ||
1508 (getLangOpts().OpenMP && getLangOpts().OpenMPIsTargetDevice))
1509 errorNYI("SYCL or OpenMP temp address space");
1510 return LangAS::Default;
1511}
1512
1514 CIRGenFunction *cgf) {
1515 if (cgf && e->getType()->isVariablyModifiedType())
1517
1519 "emitExplicitCastExprType");
1520}
1521
1524
1525 mlir::Location loc = getLoc(e->getSourceRange());
1526
1527 const auto *decl = cast<DeclRefExpr>(e->getSubExpr())->getDecl();
1528
1529 // A member function pointer.
1530 if (isa<CXXMethodDecl>(decl)) {
1531 errorNYI(e->getSourceRange(), "emitMemberPointerConstant: method pointer");
1532 return {};
1533 }
1534
1535 // Otherwise, a member data pointer.
1536 auto ty = mlir::cast<cir::DataMemberType>(convertType(e->getType()));
1537 const auto *fieldDecl = cast<FieldDecl>(decl);
1538 return cir::ConstantOp::create(
1539 builder, loc, builder.getDataMemberAttr(ty, fieldDecl->getFieldIndex()));
1540}
1541
1543 for (Decl *decl : dc->decls()) {
1544 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
1545 // are themselves considered "top-level", so EmitTopLevelDecl on an
1546 // ObjCImplDecl does not recursively visit them. We need to do that in
1547 // case they're nested inside another construct (LinkageSpecDecl /
1548 // ExportDecl) that does stop them from being considered "top-level".
1549 if (auto *oid = dyn_cast<ObjCImplDecl>(decl))
1550 errorNYI(oid->getSourceRange(), "emitDeclConext: ObjCImplDecl");
1551
1553 }
1554}
1555
1556// Emit code for a single top level declaration.
1558
1559 // Ignore dependent declarations.
1560 if (decl->isTemplated())
1561 return;
1562
1563 switch (decl->getKind()) {
1564 default:
1565 errorNYI(decl->getBeginLoc(), "declaration of kind",
1566 decl->getDeclKindName());
1567 break;
1568
1569 case Decl::CXXConversion:
1570 case Decl::CXXMethod:
1571 case Decl::Function: {
1572 auto *fd = cast<FunctionDecl>(decl);
1573 // Consteval functions shouldn't be emitted.
1574 if (!fd->isConsteval())
1575 emitGlobal(fd);
1576 break;
1577 }
1578
1579 case Decl::Var:
1580 case Decl::Decomposition:
1581 case Decl::VarTemplateSpecialization: {
1582 auto *vd = cast<VarDecl>(decl);
1584 errorNYI(decl->getSourceRange(), "global variable decompositions");
1585 break;
1586 }
1587 emitGlobal(vd);
1588 break;
1589 }
1590 case Decl::OpenACCRoutine:
1592 break;
1593 case Decl::OpenACCDeclare:
1595 break;
1596 case Decl::OMPThreadPrivate:
1598 break;
1599 case Decl::OMPGroupPrivate:
1601 break;
1602 case Decl::OMPAllocate:
1604 break;
1605 case Decl::OMPCapturedExpr:
1607 break;
1608 case Decl::OMPDeclareReduction:
1610 break;
1611 case Decl::OMPDeclareMapper:
1613 break;
1614 case Decl::OMPRequires:
1616 break;
1617 case Decl::Enum:
1618 case Decl::Using: // using X; [C++]
1619 case Decl::UsingDirective: // using namespace X; [C++]
1620 case Decl::UsingEnum: // using enum X; [C++]
1621 case Decl::NamespaceAlias:
1622 case Decl::Typedef:
1623 case Decl::TypeAlias: // using foo = bar; [C++11]
1624 case Decl::Record:
1626 break;
1627
1628 // No code generation needed.
1629 case Decl::ClassTemplate:
1630 case Decl::Concept:
1631 case Decl::CXXDeductionGuide:
1632 case Decl::Empty:
1633 case Decl::FunctionTemplate:
1634 case Decl::StaticAssert:
1635 case Decl::TypeAliasTemplate:
1636 case Decl::UsingShadow:
1637 case Decl::VarTemplate:
1638 case Decl::VarTemplatePartialSpecialization:
1639 break;
1640
1641 case Decl::CXXConstructor:
1643 break;
1644 case Decl::CXXDestructor:
1646 break;
1647
1648 // C++ Decls
1649 case Decl::LinkageSpec:
1650 case Decl::Namespace:
1652 break;
1653
1654 case Decl::ClassTemplateSpecialization:
1655 case Decl::CXXRecord: {
1658 for (auto *childDecl : crd->decls())
1660 emitTopLevelDecl(childDecl);
1661 break;
1662 }
1663
1664 case Decl::FileScopeAsm:
1665 // File-scope asm is ignored during device-side CUDA compilation.
1666 if (langOpts.CUDA && langOpts.CUDAIsDevice)
1667 break;
1668 // File-scope asm is ignored during device-side OpenMP compilation.
1669 if (langOpts.OpenMPIsTargetDevice)
1670 break;
1671 // File-scope asm is ignored during device-side SYCL compilation.
1672 if (langOpts.SYCLIsDevice)
1673 break;
1674 auto *file_asm = cast<FileScopeAsmDecl>(decl);
1675 std::string line = file_asm->getAsmString();
1676 globalScopeAsm.push_back(builder.getStringAttr(line));
1677 break;
1678 }
1679}
1680
1681void CIRGenModule::setInitializer(cir::GlobalOp &op, mlir::Attribute value) {
1682 // Recompute visibility when updating initializer.
1683 op.setInitialValueAttr(value);
1685}
1686
1687std::pair<cir::FuncType, cir::FuncOp> CIRGenModule::getAddrAndTypeOfCXXStructor(
1688 GlobalDecl gd, const CIRGenFunctionInfo *fnInfo, cir::FuncType fnType,
1689 bool dontDefer, ForDefinition_t isForDefinition) {
1690 auto *md = cast<CXXMethodDecl>(gd.getDecl());
1691
1692 if (isa<CXXDestructorDecl>(md)) {
1693 // Always alias equivalent complete destructors to base destructors in the
1694 // MS ABI.
1695 if (getTarget().getCXXABI().isMicrosoft() &&
1696 gd.getDtorType() == Dtor_Complete &&
1697 md->getParent()->getNumVBases() == 0)
1698 errorNYI(md->getSourceRange(),
1699 "getAddrAndTypeOfCXXStructor: MS ABI complete destructor");
1700 }
1701
1702 if (!fnType) {
1703 if (!fnInfo)
1705 fnType = getTypes().getFunctionType(*fnInfo);
1706 }
1707
1708 auto fn = getOrCreateCIRFunction(getMangledName(gd), fnType, gd,
1709 /*ForVtable=*/false, dontDefer,
1710 /*IsThunk=*/false, isForDefinition);
1711
1712 return {fnType, fn};
1713}
1714
1716 mlir::Type funcType, bool forVTable,
1717 bool dontDefer,
1718 ForDefinition_t isForDefinition) {
1719 assert(!cast<FunctionDecl>(gd.getDecl())->isConsteval() &&
1720 "consteval function should never be emitted");
1721
1722 if (!funcType) {
1723 const auto *fd = cast<FunctionDecl>(gd.getDecl());
1724 funcType = convertType(fd->getType());
1725 }
1726
1727 // Devirtualized destructor calls may come through here instead of via
1728 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
1729 // of the complete destructor when necessary.
1730 if (const auto *dd = dyn_cast<CXXDestructorDecl>(gd.getDecl())) {
1731 if (getTarget().getCXXABI().isMicrosoft() &&
1732 gd.getDtorType() == Dtor_Complete &&
1733 dd->getParent()->getNumVBases() == 0)
1734 errorNYI(dd->getSourceRange(),
1735 "getAddrOfFunction: MS ABI complete destructor");
1736 }
1737
1738 StringRef mangledName = getMangledName(gd);
1739 cir::FuncOp func =
1740 getOrCreateCIRFunction(mangledName, funcType, gd, forVTable, dontDefer,
1741 /*isThunk=*/false, isForDefinition);
1742 return func;
1743}
1744
1745static std::string getMangledNameImpl(CIRGenModule &cgm, GlobalDecl gd,
1746 const NamedDecl *nd) {
1747 SmallString<256> buffer;
1748
1749 llvm::raw_svector_ostream out(buffer);
1751
1753
1754 if (mc.shouldMangleDeclName(nd)) {
1755 mc.mangleName(gd.getWithDecl(nd), out);
1756 } else {
1757 IdentifierInfo *ii = nd->getIdentifier();
1758 assert(ii && "Attempt to mangle unnamed decl.");
1759
1760 const auto *fd = dyn_cast<FunctionDecl>(nd);
1761 if (fd &&
1762 fd->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
1763 cgm.errorNYI(nd->getSourceRange(), "getMangledName: X86RegCall");
1764 } else if (fd && fd->hasAttr<CUDAGlobalAttr>() &&
1766 cgm.errorNYI(nd->getSourceRange(), "getMangledName: CUDA device stub");
1767 }
1768 out << ii->getName();
1769 }
1770
1771 // Check if the module name hash should be appended for internal linkage
1772 // symbols. This should come before multi-version target suffixes are
1773 // appendded. This is to keep the name and module hash suffix of the internal
1774 // linkage function together. The unique suffix should only be added when name
1775 // mangling is done to make sure that the final name can be properly
1776 // demangled. For example, for C functions without prototypes, name mangling
1777 // is not done and the unique suffix should not be appended then.
1779
1780 if (const auto *fd = dyn_cast<FunctionDecl>(nd)) {
1781 if (fd->isMultiVersion()) {
1782 cgm.errorNYI(nd->getSourceRange(),
1783 "getMangledName: multi-version functions");
1784 }
1785 }
1786 if (cgm.getLangOpts().GPURelocatableDeviceCode) {
1787 cgm.errorNYI(nd->getSourceRange(),
1788 "getMangledName: GPU relocatable device code");
1789 }
1790
1791 return std::string(out.str());
1792}
1793
1794static FunctionDecl *
1796 const FunctionDecl *protoFunc) {
1797 // If this is a C no-prototype function, we can take the 'easy' way out and
1798 // just create a function with no arguments/functions, etc.
1799 if (!protoFunc->hasPrototype())
1800 return FunctionDecl::Create(
1801 ctx, /*DC=*/ctx.getTranslationUnitDecl(),
1802 /*StartLoc=*/SourceLocation{}, /*NLoc=*/SourceLocation{}, bindName,
1803 protoFunc->getType(), /*TInfo=*/nullptr, StorageClass::SC_None);
1804
1805 QualType funcTy = protoFunc->getType();
1806 auto *fpt = cast<FunctionProtoType>(protoFunc->getType());
1807
1808 // If this is a member function, add an explicit 'this' to the function type.
1809 if (auto *methodDecl = dyn_cast<CXXMethodDecl>(protoFunc);
1810 methodDecl && methodDecl->isImplicitObjectMemberFunction()) {
1811 llvm::SmallVector<QualType> paramTypes{fpt->getParamTypes()};
1812 paramTypes.insert(paramTypes.begin(), methodDecl->getThisType());
1813
1814 funcTy = ctx.getFunctionType(fpt->getReturnType(), paramTypes,
1815 fpt->getExtProtoInfo());
1816 fpt = cast<FunctionProtoType>(funcTy);
1817 }
1818
1819 auto *tempFunc =
1821 /*StartLoc=*/SourceLocation{},
1822 /*NLoc=*/SourceLocation{}, bindName, funcTy,
1823 /*TInfo=*/nullptr, StorageClass::SC_None);
1824
1826 params.reserve(fpt->getNumParams());
1827
1828 // Add all of the parameters.
1829 for (unsigned i = 0, e = fpt->getNumParams(); i != e; ++i) {
1831 ctx, tempFunc, /*StartLoc=*/SourceLocation{},
1832 /*IdLoc=*/SourceLocation{},
1833 /*Id=*/nullptr, fpt->getParamType(i), /*TInfo=*/nullptr,
1834 StorageClass::SC_None, /*DefArg=*/nullptr);
1835 parm->setScopeInfo(0, i);
1836 params.push_back(parm);
1837 }
1838
1839 tempFunc->setParams(params);
1840
1841 return tempFunc;
1842}
1843
1844std::string
1846 const FunctionDecl *attachedFunction) {
1848 getASTContext(), bindName, attachedFunction);
1849
1850 std::string ret = getMangledNameImpl(*this, GlobalDecl(tempFunc), tempFunc);
1851
1852 // This does nothing (it is a do-nothing function), since this is a
1853 // slab-allocator, but leave a call in to immediately destroy this in case we
1854 // ever come up with a way of getting allocations back.
1855 getASTContext().Deallocate(tempFunc);
1856 return ret;
1857}
1858
1860 GlobalDecl canonicalGd = gd.getCanonicalDecl();
1861
1862 // Some ABIs don't have constructor variants. Make sure that base and complete
1863 // constructors get mangled the same.
1864 if (const auto *cd = dyn_cast<CXXConstructorDecl>(canonicalGd.getDecl())) {
1865 if (!getTarget().getCXXABI().hasConstructorVariants()) {
1866 errorNYI(cd->getSourceRange(),
1867 "getMangledName: C++ constructor without variants");
1868 return cast<NamedDecl>(gd.getDecl())->getIdentifier()->getName();
1869 }
1870 }
1871
1872 // Keep the first result in the case of a mangling collision.
1873 const auto *nd = cast<NamedDecl>(gd.getDecl());
1874 std::string mangledName = getMangledNameImpl(*this, gd, nd);
1875
1876 auto result = manglings.insert(std::make_pair(mangledName, gd));
1877 return mangledDeclNames[canonicalGd] = result.first->first();
1878}
1879
1881 assert(!d->getInit() && "Cannot emit definite definitions here!");
1882
1883 StringRef mangledName = getMangledName(d);
1884 mlir::Operation *gv = getGlobalValue(mangledName);
1885
1886 // If we already have a definition, not declaration, with the same mangled
1887 // name, emitting of declaration is not required (and would actually overwrite
1888 // the emitted definition).
1889 if (gv && !mlir::cast<cir::GlobalOp>(gv).isDeclaration())
1890 return;
1891
1892 // If we have not seen a reference to this variable yet, place it into the
1893 // deferred declarations table to be emitted if needed later.
1894 if (!mustBeEmitted(d) && !gv) {
1895 deferredDecls[mangledName] = d;
1896 return;
1897 }
1898
1899 // The tentative definition is the only definition.
1901}
1902
1904 // Never defer when EmitAllDecls is specified.
1905 if (langOpts.EmitAllDecls)
1906 return true;
1907
1908 const auto *vd = dyn_cast<VarDecl>(global);
1909 if (vd &&
1910 ((codeGenOpts.KeepPersistentStorageVariables &&
1911 (vd->getStorageDuration() == SD_Static ||
1912 vd->getStorageDuration() == SD_Thread)) ||
1913 (codeGenOpts.KeepStaticConsts && vd->getStorageDuration() == SD_Static &&
1914 vd->getType().isConstQualified())))
1915 return true;
1916
1917 return getASTContext().DeclMustBeEmitted(global);
1918}
1919
1921 // In OpenMP 5.0 variables and function may be marked as
1922 // device_type(host/nohost) and we should not emit them eagerly unless we sure
1923 // that they must be emitted on the host/device. To be sure we need to have
1924 // seen a declare target with an explicit mentioning of the function, we know
1925 // we have if the level of the declare target attribute is -1. Note that we
1926 // check somewhere else if we should emit this at all.
1927 if (langOpts.OpenMP >= 50 && !langOpts.OpenMPSimd) {
1928 std::optional<OMPDeclareTargetDeclAttr *> activeAttr =
1929 OMPDeclareTargetDeclAttr::getActiveAttr(global);
1930 if (!activeAttr || (*activeAttr)->getLevel() != (unsigned)-1)
1931 return false;
1932 }
1933
1934 const auto *fd = dyn_cast<FunctionDecl>(global);
1935 if (fd) {
1936 // Implicit template instantiations may change linkage if they are later
1937 // explicitly instantiated, so they should not be emitted eagerly.
1938 if (fd->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
1939 return false;
1940 // Defer until all versions have been semantically checked.
1941 if (fd->hasAttr<TargetVersionAttr>() && !fd->isMultiVersion())
1942 return false;
1943 if (langOpts.SYCLIsDevice) {
1944 errorNYI(fd->getSourceRange(), "mayBeEmittedEagerly: SYCL");
1945 return false;
1946 }
1947 }
1948 const auto *vd = dyn_cast<VarDecl>(global);
1949 if (vd)
1950 if (astContext.getInlineVariableDefinitionKind(vd) ==
1952 // A definition of an inline constexpr static data member may change
1953 // linkage later if it's redeclared outside the class.
1954 return false;
1955
1956 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
1957 // codegen for global variables, because they may be marked as threadprivate.
1958 if (langOpts.OpenMP && langOpts.OpenMPUseTLS &&
1959 astContext.getTargetInfo().isTLSSupported() && isa<VarDecl>(global) &&
1960 !global->getType().isConstantStorage(astContext, false, false) &&
1961 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(global))
1962 return false;
1963
1964 assert((fd || vd) &&
1965 "Only FunctionDecl and VarDecl should hit this path so far.");
1966 return true;
1967}
1968
1969static bool shouldAssumeDSOLocal(const CIRGenModule &cgm,
1970 cir::CIRGlobalValueInterface gv) {
1971 if (gv.hasLocalLinkage())
1972 return true;
1973
1974 if (!gv.hasDefaultVisibility() && !gv.hasExternalWeakLinkage())
1975 return true;
1976
1977 // DLLImport explicitly marks the GV as external.
1978 // so it shouldn't be dso_local
1979 // But we don't have the info set now
1981
1982 const llvm::Triple &tt = cgm.getTriple();
1983 const CodeGenOptions &cgOpts = cgm.getCodeGenOpts();
1984 if (tt.isOSCygMing()) {
1985 // In MinGW and Cygwin, variables without DLLImport can still be
1986 // automatically imported from a DLL by the linker; don't mark variables
1987 // that potentially could come from another DLL as DSO local.
1988
1989 // With EmulatedTLS, TLS variables can be autoimported from other DLLs
1990 // (and this actually happens in the public interface of libstdc++), so
1991 // such variables can't be marked as DSO local. (Native TLS variables
1992 // can't be dllimported at all, though.)
1993 cgm.errorNYI("shouldAssumeDSOLocal: MinGW");
1994 }
1995
1996 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
1997 // remain unresolved in the link, they can be resolved to zero, which is
1998 // outside the current DSO.
1999 if (tt.isOSBinFormatCOFF() && gv.hasExternalWeakLinkage())
2000 return false;
2001
2002 // Every other GV is local on COFF.
2003 // Make an exception for windows OS in the triple: Some firmware builds use
2004 // *-win32-macho triples. This (accidentally?) produced windows relocations
2005 // without GOT tables in older clang versions; Keep this behaviour.
2006 // FIXME: even thread local variables?
2007 if (tt.isOSBinFormatCOFF() || (tt.isOSWindows() && tt.isOSBinFormatMachO()))
2008 return true;
2009
2010 // Only handle COFF and ELF for now.
2011 if (!tt.isOSBinFormatELF())
2012 return false;
2013
2014 llvm::Reloc::Model rm = cgOpts.RelocationModel;
2015 const LangOptions &lOpts = cgm.getLangOpts();
2016 if (rm != llvm::Reloc::Static && !lOpts.PIE) {
2017 // On ELF, if -fno-semantic-interposition is specified and the target
2018 // supports local aliases, there will be neither CC1
2019 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
2020 // dso_local on the function if using a local alias is preferable (can avoid
2021 // PLT indirection).
2022 if (!(isa<cir::FuncOp>(gv) && gv.canBenefitFromLocalAlias()))
2023 return false;
2024 return !(lOpts.SemanticInterposition || lOpts.HalfNoSemanticInterposition);
2025 }
2026
2027 // A definition cannot be preempted from an executable.
2028 if (!gv.isDeclarationForLinker())
2029 return true;
2030
2031 // Most PIC code sequences that assume that a symbol is local cannot produce a
2032 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
2033 // depended, it seems worth it to handle it here.
2034 if (rm == llvm::Reloc::PIC_ && gv.hasExternalWeakLinkage())
2035 return false;
2036
2037 // PowerPC64 prefers TOC indirection to avoid copy relocations.
2038 if (tt.isPPC64())
2039 return false;
2040
2041 if (cgOpts.DirectAccessExternalData) {
2042 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
2043 // for non-thread-local variables. If the symbol is not defined in the
2044 // executable, a copy relocation will be needed at link time. dso_local is
2045 // excluded for thread-local variables because they generally don't support
2046 // copy relocations.
2047 if (auto globalOp = dyn_cast<cir::GlobalOp>(gv.getOperation())) {
2048 // Assume variables are not thread-local until that support is added.
2050 return true;
2051 }
2052
2053 // -fno-pic sets dso_local on a function declaration to allow direct
2054 // accesses when taking its address (similar to a data symbol). If the
2055 // function is not defined in the executable, a canonical PLT entry will be
2056 // needed at link time. -fno-direct-access-external-data can avoid the
2057 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
2058 // it could just cause trouble without providing perceptible benefits.
2059 if (isa<cir::FuncOp>(gv) && !cgOpts.NoPLT && rm == llvm::Reloc::Static)
2060 return true;
2061 }
2062
2063 // If we can use copy relocations we can assume it is local.
2064
2065 // Otherwise don't assume it is local.
2066
2067 return false;
2068}
2069
2070void CIRGenModule::setGlobalVisibility(mlir::Operation *gv,
2071 const NamedDecl *d) const {
2073}
2074
2075void CIRGenModule::setDSOLocal(cir::CIRGlobalValueInterface gv) const {
2076 gv.setDSOLocal(shouldAssumeDSOLocal(*this, gv));
2077}
2078
2079void CIRGenModule::setDSOLocal(mlir::Operation *op) const {
2080 if (auto globalValue = dyn_cast<cir::CIRGlobalValueInterface>(op))
2081 setDSOLocal(globalValue);
2082}
2083
2084void CIRGenModule::setGVProperties(mlir::Operation *op,
2085 const NamedDecl *d) const {
2087 setGVPropertiesAux(op, d);
2088}
2089
2090void CIRGenModule::setGVPropertiesAux(mlir::Operation *op,
2091 const NamedDecl *d) const {
2092 setGlobalVisibility(op, d);
2093 setDSOLocal(op);
2095}
2096
2098 switch (getCodeGenOpts().getDefaultTLSModel()) {
2100 return cir::TLS_Model::GeneralDynamic;
2102 return cir::TLS_Model::LocalDynamic;
2104 return cir::TLS_Model::InitialExec;
2106 return cir::TLS_Model::LocalExec;
2107 }
2108 llvm_unreachable("Invalid TLS model!");
2109}
2110
2111void CIRGenModule::setTLSMode(mlir::Operation *op, const VarDecl &d) {
2112 assert(d.getTLSKind() && "setting TLS mode on non-TLS var!");
2113
2114 cir::TLS_Model tlm = getDefaultCIRTLSModel();
2115
2116 // Override the TLS model if it is explicitly specified.
2117 if (d.getAttr<TLSModelAttr>())
2118 errorNYI(d.getSourceRange(), "TLS model attribute");
2119
2120 auto global = cast<cir::GlobalOp>(op);
2121 global.setTlsModel(tlm);
2122}
2123
2125 cir::FuncOp func,
2126 bool isIncompleteFunction,
2127 bool isThunk) {
2128 // NOTE(cir): Original CodeGen checks if this is an intrinsic. In CIR we
2129 // represent them in dedicated ops. The correct attributes are ensured during
2130 // translation to LLVM. Thus, we don't need to check for them here.
2131
2134
2135 // TODO(cir): This needs a lot of work to better match CodeGen. That
2136 // ultimately ends up in setGlobalVisibility, which already has the linkage of
2137 // the LLVM GV (corresponding to our FuncOp) computed, so it doesn't have to
2138 // recompute it here. This is a minimal fix for now.
2139 if (!isLocalLinkage(getFunctionLinkage(globalDecl))) {
2140 const Decl *decl = globalDecl.getDecl();
2141 func.setGlobalVisibilityAttr(getGlobalVisibilityAttrFromDecl(decl));
2142 }
2143
2144 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2145 const auto *fd = cast<FunctionDecl>(globalDecl.getDecl());
2146 if (fd->isInlineBuiltinDeclaration()) {
2147 const FunctionDecl *fdBody;
2148 bool hasBody = fd->hasBody(fdBody);
2149 (void)hasBody;
2150 assert(hasBody && "Inline builtin declarations should always have an "
2151 "available body!");
2153 }
2154}
2155
2157 const clang::FunctionDecl *decl, cir::FuncOp f) {
2160
2161 std::optional<cir::InlineKind> existingInlineKind = f.getInlineKind();
2162 bool isNoInline =
2163 existingInlineKind && *existingInlineKind == cir::InlineKind::NoInline;
2164 bool isAlwaysInline = existingInlineKind &&
2165 *existingInlineKind == cir::InlineKind::AlwaysInline;
2166 if (!decl) {
2167 assert(!cir::MissingFeatures::hlsl());
2168
2169 if (!isAlwaysInline &&
2170 codeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2171 // If inlining is disabled and we don't have a declaration to control
2172 // inlining, mark the function as 'noinline' unless it is explicitly
2173 // marked as 'alwaysinline'.
2174 f.setInlineKind(cir::InlineKind::NoInline);
2175 }
2176
2177 return;
2178 }
2179
2186 assert(!cir::MissingFeatures::hlsl());
2187
2188 // Handle inline attributes
2189 if (decl->hasAttr<NoInlineAttr>() && !isAlwaysInline) {
2190 // Add noinline if the function isn't always_inline.
2191 f.setInlineKind(cir::InlineKind::NoInline);
2192 } else if (decl->hasAttr<AlwaysInlineAttr>() && !isNoInline) {
2193 // Don't override AlwaysInline with NoInline, or vice versa, since we can't
2194 // specify both in IR.
2195 f.setInlineKind(cir::InlineKind::AlwaysInline);
2196 } else if (codeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2197 // If inlining is disabled, force everything that isn't always_inline
2198 // to carry an explicit noinline attribute.
2199 if (!isAlwaysInline) {
2200 f.setInlineKind(cir::InlineKind::NoInline);
2201 }
2202 } else {
2203 // Otherwise, propagate the inline hint attribute and potentially use its
2204 // absence to mark things as noinline.
2205 // Search function and template pattern redeclarations for inline.
2206 if (auto *fd = dyn_cast<FunctionDecl>(decl)) {
2207 // TODO: Share this checkForInline implementation with classic codegen.
2208 // This logic is likely to change over time, so sharing would help ensure
2209 // consistency.
2210 auto checkForInline = [](const FunctionDecl *decl) {
2211 auto checkRedeclForInline = [](const FunctionDecl *redecl) {
2212 return redecl->isInlineSpecified();
2213 };
2214 if (any_of(decl->redecls(), checkRedeclForInline))
2215 return true;
2216 const FunctionDecl *pattern = decl->getTemplateInstantiationPattern();
2217 if (!pattern)
2218 return false;
2219 return any_of(pattern->redecls(), checkRedeclForInline);
2220 };
2221 if (checkForInline(fd)) {
2222 f.setInlineKind(cir::InlineKind::InlineHint);
2223 } else if (codeGenOpts.getInlining() ==
2225 !fd->isInlined() && !isAlwaysInline) {
2226 f.setInlineKind(cir::InlineKind::NoInline);
2227 }
2228 }
2229 }
2230
2232}
2233
2235 StringRef mangledName, mlir::Type funcType, GlobalDecl gd, bool forVTable,
2236 bool dontDefer, bool isThunk, ForDefinition_t isForDefinition,
2237 mlir::ArrayAttr extraAttrs) {
2238 const Decl *d = gd.getDecl();
2239
2240 if (isThunk)
2241 errorNYI(d->getSourceRange(), "getOrCreateCIRFunction: thunk");
2242
2243 // In what follows, we continue past 'errorNYI' as if nothing happened because
2244 // the rest of the implementation is better than doing nothing.
2245
2246 if (const auto *fd = cast_or_null<FunctionDecl>(d)) {
2247 // For the device mark the function as one that should be emitted.
2248 if (getLangOpts().OpenMPIsTargetDevice && fd->isDefined() && !dontDefer &&
2249 !isForDefinition)
2250 errorNYI(fd->getSourceRange(),
2251 "getOrCreateCIRFunction: OpenMP target function");
2252
2253 // Any attempts to use a MultiVersion function should result in retrieving
2254 // the iFunc instead. Name mangling will handle the rest of the changes.
2255 if (fd->isMultiVersion())
2256 errorNYI(fd->getSourceRange(), "getOrCreateCIRFunction: multi-version");
2257 }
2258
2259 // Lookup the entry, lazily creating it if necessary.
2260 mlir::Operation *entry = getGlobalValue(mangledName);
2261 if (entry) {
2262 assert(mlir::isa<cir::FuncOp>(entry));
2263
2265
2266 // Handle dropped DLL attributes.
2267 if (d && !d->hasAttr<DLLImportAttr>() && !d->hasAttr<DLLExportAttr>()) {
2269 setDSOLocal(entry);
2270 }
2271
2272 // If there are two attempts to define the same mangled name, issue an
2273 // error.
2274 auto fn = cast<cir::FuncOp>(entry);
2275 if (isForDefinition && fn && !fn.isDeclaration()) {
2276 errorNYI(d->getSourceRange(), "Duplicate function definition");
2277 }
2278 if (fn && fn.getFunctionType() == funcType) {
2279 return fn;
2280 }
2281
2282 if (!isForDefinition) {
2283 return fn;
2284 }
2285
2286 // TODO(cir): classic codegen checks here if this is a llvm::GlobalAlias.
2287 // How will we support this?
2288 }
2289
2290 auto *funcDecl = llvm::cast_or_null<FunctionDecl>(gd.getDecl());
2291 bool invalidLoc = !funcDecl ||
2292 funcDecl->getSourceRange().getBegin().isInvalid() ||
2293 funcDecl->getSourceRange().getEnd().isInvalid();
2294 cir::FuncOp funcOp = createCIRFunction(
2295 invalidLoc ? theModule->getLoc() : getLoc(funcDecl->getSourceRange()),
2296 mangledName, mlir::cast<cir::FuncType>(funcType), funcDecl);
2297
2298 // If we already created a function with the same mangled name (but different
2299 // type) before, take its name and add it to the list of functions to be
2300 // replaced with F at the end of CodeGen.
2301 //
2302 // This happens if there is a prototype for a function (e.g. "int f()") and
2303 // then a definition of a different type (e.g. "int f(int x)").
2304 if (entry) {
2305
2306 // Fetch a generic symbol-defining operation and its uses.
2307 auto symbolOp = mlir::cast<mlir::SymbolOpInterface>(entry);
2308
2309 // This might be an implementation of a function without a prototype, in
2310 // which case, try to do special replacement of calls which match the new
2311 // prototype. The really key thing here is that we also potentially drop
2312 // arguments from the call site so as to make a direct call, which makes the
2313 // inliner happier and suppresses a number of optimizer warnings (!) about
2314 // dropping arguments.
2315 if (symbolOp.getSymbolUses(symbolOp->getParentOp()))
2317
2318 // Obliterate no-proto declaration.
2319 entry->erase();
2320 }
2321
2322 if (d)
2323 setFunctionAttributes(gd, funcOp, /*isIncompleteFunction=*/false, isThunk);
2324
2325 // 'dontDefer' actually means don't move this to the deferredDeclsToEmit list.
2326 if (dontDefer) {
2327 // TODO(cir): This assertion will need an additional condition when we
2328 // support incomplete functions.
2329 assert(funcOp.getFunctionType() == funcType);
2330 return funcOp;
2331 }
2332
2333 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
2334 // each other bottoming out wiht the base dtor. Therefore we emit non-base
2335 // dtors on usage, even if there is no dtor definition in the TU.
2336 if (isa_and_nonnull<CXXDestructorDecl>(d) &&
2337 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(d),
2338 gd.getDtorType()))
2339 errorNYI(d->getSourceRange(), "getOrCreateCIRFunction: dtor");
2340
2341 // This is the first use or definition of a mangled name. If there is a
2342 // deferred decl with this name, remember that we need to emit it at the end
2343 // of the file.
2344 auto ddi = deferredDecls.find(mangledName);
2345 if (ddi != deferredDecls.end()) {
2346 // Move the potentially referenced deferred decl to the
2347 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
2348 // don't need it anymore).
2349 addDeferredDeclToEmit(ddi->second);
2350 deferredDecls.erase(ddi);
2351
2352 // Otherwise, there are cases we have to worry about where we're using a
2353 // declaration for which we must emit a definition but where we might not
2354 // find a top-level definition.
2355 // - member functions defined inline in their classes
2356 // - friend functions defined inline in some class
2357 // - special member functions with implicit definitions
2358 // If we ever change our AST traversal to walk into class methods, this
2359 // will be unnecessary.
2360 //
2361 // We also don't emit a definition for a function if it's going to be an
2362 // entry in a vtable, unless it's already marked as used.
2363 } else if (getLangOpts().CPlusPlus && d) {
2364 // Look for a declaration that's lexically in a record.
2365 for (const auto *fd = cast<FunctionDecl>(d)->getMostRecentDecl(); fd;
2366 fd = fd->getPreviousDecl()) {
2367 if (isa<CXXRecordDecl>(fd->getLexicalDeclContext())) {
2368 if (fd->doesThisDeclarationHaveABody()) {
2370 break;
2371 }
2372 }
2373 }
2374 }
2375
2376 return funcOp;
2377}
2378
2379cir::FuncOp
2380CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
2381 cir::FuncType funcType,
2382 const clang::FunctionDecl *funcDecl) {
2383 cir::FuncOp func;
2384 {
2385 mlir::OpBuilder::InsertionGuard guard(builder);
2386
2387 // Some global emissions are triggered while emitting a function, e.g.
2388 // void s() { x.method() }
2389 //
2390 // Be sure to insert a new function before a current one.
2391 CIRGenFunction *cgf = this->curCGF;
2392 if (cgf)
2393 builder.setInsertionPoint(cgf->curFn);
2394
2395 func = cir::FuncOp::create(builder, loc, name, funcType);
2396
2398
2399 if (funcDecl && !funcDecl->hasPrototype())
2400 func.setNoProto(true);
2401
2402 assert(func.isDeclaration() && "expected empty body");
2403
2404 // A declaration gets private visibility by default, but external linkage
2405 // as the default linkage.
2406 func.setLinkageAttr(cir::GlobalLinkageKindAttr::get(
2407 &getMLIRContext(), cir::GlobalLinkageKind::ExternalLinkage));
2408 mlir::SymbolTable::setSymbolVisibility(
2409 func, mlir::SymbolTable::Visibility::Private);
2410
2412
2413 // Mark C++ special member functions (Constructor, Destructor etc.)
2414 setCXXSpecialMemberAttr(func, funcDecl);
2415
2416 if (!cgf)
2417 theModule.push_back(func);
2418
2419 if (this->getLangOpts().OpenACC) {
2420 // We only have to handle this attribute, since OpenACCAnnotAttrs are
2421 // handled via the end-of-TU work.
2422 for (const auto *attr :
2423 funcDecl->specific_attrs<OpenACCRoutineDeclAttr>())
2424 emitOpenACCRoutineDecl(funcDecl, func, attr->getLocation(),
2425 attr->Clauses);
2426 }
2427 }
2428 return func;
2429}
2430
2431cir::FuncOp
2432CIRGenModule::createCIRBuiltinFunction(mlir::Location loc, StringRef name,
2433 cir::FuncType ty,
2434 const clang::FunctionDecl *fd) {
2435 cir::FuncOp fnOp = createCIRFunction(loc, name, ty, fd);
2436 fnOp.setBuiltin(true);
2437 return fnOp;
2438}
2439
2440static cir::CtorKind getCtorKindFromDecl(const CXXConstructorDecl *ctor) {
2441 if (ctor->isDefaultConstructor())
2442 return cir::CtorKind::Default;
2443 if (ctor->isCopyConstructor())
2444 return cir::CtorKind::Copy;
2445 if (ctor->isMoveConstructor())
2446 return cir::CtorKind::Move;
2447 return cir::CtorKind::Custom;
2448}
2449
2450static cir::AssignKind getAssignKindFromDecl(const CXXMethodDecl *method) {
2451 if (method->isCopyAssignmentOperator())
2452 return cir::AssignKind::Copy;
2453 if (method->isMoveAssignmentOperator())
2454 return cir::AssignKind::Move;
2455 llvm_unreachable("not a copy or move assignment operator");
2456}
2457
2459 cir::FuncOp funcOp, const clang::FunctionDecl *funcDecl) {
2460 if (!funcDecl)
2461 return;
2462
2463 if (const auto *dtor = dyn_cast<CXXDestructorDecl>(funcDecl)) {
2464 auto cxxDtor = cir::CXXDtorAttr::get(
2465 convertType(getASTContext().getCanonicalTagType(dtor->getParent())),
2466 dtor->isTrivial());
2467 funcOp.setCxxSpecialMemberAttr(cxxDtor);
2468 return;
2469 }
2470
2471 if (const auto *ctor = dyn_cast<CXXConstructorDecl>(funcDecl)) {
2472 cir::CtorKind kind = getCtorKindFromDecl(ctor);
2473 auto cxxCtor = cir::CXXCtorAttr::get(
2474 convertType(getASTContext().getCanonicalTagType(ctor->getParent())),
2475 kind, ctor->isTrivial());
2476 funcOp.setCxxSpecialMemberAttr(cxxCtor);
2477 return;
2478 }
2479
2480 const auto *method = dyn_cast<CXXMethodDecl>(funcDecl);
2481 if (method && (method->isCopyAssignmentOperator() ||
2482 method->isMoveAssignmentOperator())) {
2483 cir::AssignKind assignKind = getAssignKindFromDecl(method);
2484 auto cxxAssign = cir::CXXAssignAttr::get(
2485 convertType(getASTContext().getCanonicalTagType(method->getParent())),
2486 assignKind, method->isTrivial());
2487 funcOp.setCxxSpecialMemberAttr(cxxAssign);
2488 return;
2489 }
2490}
2491
2492static void setWindowsItaniumDLLImport(CIRGenModule &cgm, bool isLocal,
2493 cir::FuncOp funcOp, StringRef name) {
2494 // In Windows Itanium environments, try to mark runtime functions
2495 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
2496 // will link their standard library statically or dynamically. Marking
2497 // functions imported when they are not imported can cause linker errors
2498 // and warnings.
2499 if (!isLocal && cgm.getTarget().getTriple().isWindowsItaniumEnvironment() &&
2500 !cgm.getCodeGenOpts().LTOVisibilityPublicStd) {
2504 }
2505}
2506
2507cir::FuncOp CIRGenModule::createRuntimeFunction(cir::FuncType ty,
2508 StringRef name, mlir::ArrayAttr,
2509 bool isLocal,
2510 bool assumeConvergent) {
2511 if (assumeConvergent)
2512 errorNYI("createRuntimeFunction: assumeConvergent");
2513
2514 cir::FuncOp entry = getOrCreateCIRFunction(name, ty, GlobalDecl(),
2515 /*forVtable=*/false);
2516
2517 if (entry) {
2518 // TODO(cir): set the attributes of the function.
2521 setWindowsItaniumDLLImport(*this, isLocal, entry, name);
2522 entry.setDSOLocal(true);
2523 }
2524
2525 return entry;
2526}
2527
2528mlir::SymbolTable::Visibility
2530 // MLIR doesn't accept public symbols declarations (only
2531 // definitions).
2532 if (op.isDeclaration())
2533 return mlir::SymbolTable::Visibility::Private;
2534 return getMLIRVisibilityFromCIRLinkage(op.getLinkage());
2535}
2536
2537mlir::SymbolTable::Visibility
2539 switch (glk) {
2540 case cir::GlobalLinkageKind::InternalLinkage:
2541 case cir::GlobalLinkageKind::PrivateLinkage:
2542 return mlir::SymbolTable::Visibility::Private;
2543 case cir::GlobalLinkageKind::ExternalLinkage:
2544 case cir::GlobalLinkageKind::ExternalWeakLinkage:
2545 case cir::GlobalLinkageKind::LinkOnceODRLinkage:
2546 case cir::GlobalLinkageKind::AvailableExternallyLinkage:
2547 case cir::GlobalLinkageKind::CommonLinkage:
2548 case cir::GlobalLinkageKind::WeakAnyLinkage:
2549 case cir::GlobalLinkageKind::WeakODRLinkage:
2550 return mlir::SymbolTable::Visibility::Public;
2551 default: {
2552 llvm::errs() << "visibility not implemented for '"
2553 << stringifyGlobalLinkageKind(glk) << "'\n";
2554 assert(0 && "not implemented");
2555 }
2556 }
2557 llvm_unreachable("linkage should be handled above!");
2558}
2559
2561 clang::VisibilityAttr::VisibilityType visibility) {
2562 switch (visibility) {
2563 case clang::VisibilityAttr::VisibilityType::Default:
2564 return cir::VisibilityKind::Default;
2565 case clang::VisibilityAttr::VisibilityType::Hidden:
2566 return cir::VisibilityKind::Hidden;
2567 case clang::VisibilityAttr::VisibilityType::Protected:
2568 return cir::VisibilityKind::Protected;
2569 }
2570 llvm_unreachable("unexpected visibility value");
2571}
2572
2573cir::VisibilityAttr
2575 const clang::VisibilityAttr *va = decl->getAttr<clang::VisibilityAttr>();
2576 cir::VisibilityAttr cirVisibility =
2577 cir::VisibilityAttr::get(&getMLIRContext());
2578 if (va) {
2579 cirVisibility = cir::VisibilityAttr::get(
2580 &getMLIRContext(),
2581 getGlobalVisibilityKindFromClangVisibility(va->getVisibility()));
2582 }
2583 return cirVisibility;
2584}
2585
2587 emitDeferred();
2588 applyReplacements();
2589
2590 theModule->setAttr(cir::CIRDialect::getModuleLevelAsmAttrName(),
2591 builder.getArrayAttr(globalScopeAsm));
2592
2593 // There's a lot of code that is not implemented yet.
2595}
2596
2597void CIRGenModule::emitAliasForGlobal(StringRef mangledName,
2598 mlir::Operation *op, GlobalDecl aliasGD,
2599 cir::FuncOp aliasee,
2600 cir::GlobalLinkageKind linkage) {
2601
2602 auto *aliasFD = dyn_cast<FunctionDecl>(aliasGD.getDecl());
2603 assert(aliasFD && "expected FunctionDecl");
2604
2605 // The aliasee function type is different from the alias one, this difference
2606 // is specific to CIR because in LLVM the ptr types are already erased at this
2607 // point.
2608 const CIRGenFunctionInfo &fnInfo =
2610 cir::FuncType fnType = getTypes().getFunctionType(fnInfo);
2611
2612 cir::FuncOp alias =
2614 mangledName, fnType, aliasFD);
2615 alias.setAliasee(aliasee.getName());
2616 alias.setLinkage(linkage);
2617 // Declarations cannot have public MLIR visibility, just mark them private
2618 // but this really should have no meaning since CIR should not be using
2619 // this information to derive linkage information.
2620 mlir::SymbolTable::setSymbolVisibility(
2621 alias, mlir::SymbolTable::Visibility::Private);
2622
2623 // Alias constructors and destructors are always unnamed_addr.
2625
2626 // Switch any previous uses to the alias.
2627 if (op) {
2628 errorNYI(aliasFD->getSourceRange(), "emitAliasForGlobal: previous uses");
2629 } else {
2630 // Name already set by createCIRFunction
2631 }
2632
2633 // Finally, set up the alias with its proper name and attributes.
2634 setCommonAttributes(aliasGD, alias);
2635}
2636
2638 return genTypes.convertType(type);
2639}
2640
2642 // Verify the module after we have finished constructing it, this will
2643 // check the structural properties of the IR and invoke any specific
2644 // verifiers we have on the CIR operations.
2645 return mlir::verify(theModule).succeeded();
2646}
2647
2648mlir::Attribute CIRGenModule::getAddrOfRTTIDescriptor(mlir::Location loc,
2649 QualType ty, bool forEh) {
2650 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
2651 // FIXME: should we even be calling this method if RTTI is disabled
2652 // and it's not for EH?
2653 if (!shouldEmitRTTI(forEh))
2654 return builder.getConstNullPtrAttr(builder.getUInt8PtrTy());
2655
2656 if (forEh && ty->isObjCObjectPointerType() &&
2657 langOpts.ObjCRuntime.isGNUFamily()) {
2658 errorNYI(loc, "getAddrOfRTTIDescriptor: Objc PtrType & Objc RT GUN");
2659 return {};
2660 }
2661
2662 return getCXXABI().getAddrOfRTTIDescriptor(loc, ty);
2663}
2664
2665// TODO(cir): this can be shared with LLVM codegen.
2667 const CXXRecordDecl *derivedClass,
2668 llvm::iterator_range<CastExpr::path_const_iterator> path) {
2669 CharUnits offset = CharUnits::Zero();
2670
2671 const ASTContext &astContext = getASTContext();
2672 const CXXRecordDecl *rd = derivedClass;
2673
2674 for (const CXXBaseSpecifier *base : path) {
2675 assert(!base->isVirtual() && "Should not see virtual bases here!");
2676
2677 // Get the layout.
2678 const ASTRecordLayout &layout = astContext.getASTRecordLayout(rd);
2679
2680 const auto *baseDecl = base->getType()->castAsCXXRecordDecl();
2681
2682 // Add the offset.
2683 offset += layout.getBaseClassOffset(baseDecl);
2684
2685 rd = baseDecl;
2686 }
2687
2688 return offset;
2689}
2690
2692 llvm::StringRef feature) {
2693 unsigned diagID = diags.getCustomDiagID(
2694 DiagnosticsEngine::Error, "ClangIR code gen Not Yet Implemented: %0");
2695 return diags.Report(loc, diagID) << feature;
2696}
2697
2699 llvm::StringRef feature) {
2700 return errorNYI(loc.getBegin(), feature) << loc;
2701}
2702
2703void CIRGenModule::mapBlockAddress(cir::BlockAddrInfoAttr blockInfo,
2704 cir::LabelOp label) {
2705 [[maybe_unused]] auto result =
2706 blockAddressInfoToLabel.try_emplace(blockInfo, label);
2707 assert(result.second &&
2708 "attempting to map a blockaddress info that is already mapped");
2709}
2710
2711void CIRGenModule::mapUnresolvedBlockAddress(cir::BlockAddressOp op) {
2712 [[maybe_unused]] auto result = unresolvedBlockAddressToLabel.insert(op);
2713 assert(result.second &&
2714 "attempting to map a blockaddress operation that is already mapped");
2715}
2716
2717void CIRGenModule::mapResolvedBlockAddress(cir::BlockAddressOp op,
2718 cir::LabelOp label) {
2719 [[maybe_unused]] auto result = blockAddressToLabel.try_emplace(op, label);
2720 assert(result.second &&
2721 "attempting to map a blockaddress operation that is already mapped");
2722}
2723
2725 cir::LabelOp newLabel) {
2726 auto *it = blockAddressToLabel.find(op);
2727 assert(it != blockAddressToLabel.end() &&
2728 "trying to update a blockaddress not previously mapped");
2729 assert(!it->second && "blockaddress already has a resolved label");
2730
2731 it->second = newLabel;
2732}
2733
2734cir::LabelOp
2735CIRGenModule::lookupBlockAddressInfo(cir::BlockAddrInfoAttr blockInfo) {
2736 return blockAddressInfoToLabel.lookup(blockInfo);
2737}
Defines the clang::ASTContext interface.
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 void setWindowsItaniumDLLImport(CIRGenModule &cgm, bool isLocal, cir::FuncOp funcOp, StringRef name)
static std::string getMangledNameImpl(CIRGenModule &cgm, GlobalDecl gd, const NamedDecl *nd)
static cir::GlobalOp generateStringLiteral(mlir::Location loc, mlir::TypedAttr c, cir::GlobalLinkageKind lt, CIRGenModule &cgm, StringRef globalName, CharUnits alignment)
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)
This file defines OpenACC nodes for declarative directives.
Defines the SourceManager interface.
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
cir::PointerType getPointerTo(mlir::Type ty)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
TranslationUnitDecl * getTranslationUnitDecl() const
@ 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:870
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:909
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.
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
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.
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)
cir::FuncOp createRuntimeFunction(cir::FuncType ty, llvm::StringRef name, mlir::ArrayAttr={}, bool isLocal=false, bool assumeConvergent=false)
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)
mlir::Attribute getAddrOfRTTIDescriptor(mlir::Location loc, QualType ty, bool forEH=false)
Get the address of the RTTI descriptor for the given type.
clang::CharUnits getNaturalTypeAlignment(clang::QualType t, LValueBaseInfo *baseInfo)
FIXME: this could likely be a common helper and not necessarily related with codegen.
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
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 emitGlobalDecl(const clang::GlobalDecl &d)
Helper for emitDeferred to apply actual codegen.
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::ArrayAttr extraAttrs={})
void emitGlobalVarDefinition(const clang::VarDecl *vd, bool isTentative=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.
std::map< llvm::StringRef, clang::GlobalDecl > deferredDecls
This contains all the decls which have definitions but which are deferred for emission and therefore ...
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
const clang::LangOptions & getLangOpts() const
void emitOpenACCRoutineDecl(const clang::FunctionDecl *funcDecl, cir::FuncOp func, SourceLocation pragmaLoc, ArrayRef< const OpenACCClause * > clauses)
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)
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
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::Operation * getGlobalValue(llvm::StringRef ref)
void emitOMPDeclareReduction(const OMPDeclareReductionDecl *d)
mlir::ModuleOp getModule() const
cir::GlobalLinkageKind getCIRLinkageForDeclarator(const DeclaratorDecl *dd, GVALinkage linkage, bool isConstantVariable)
mlir::MLIRContext & getMLIRContext()
mlir::Operation * getAddrOfGlobal(clang::GlobalDecl gd, ForDefinition_t isForDefinition=NotForDefinition)
static cir::GlobalOp createGlobalOp(CIRGenModule &cgm, mlir::Location loc, llvm::StringRef name, mlir::Type t, bool isConstant=false, mlir::Operation *insertPoint=nullptr)
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.
void emitDeclContext(const DeclContext *dc)
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 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 cir::TargetAddressSpaceAttr getCIRAllocaAddressSpace() const
Get the address space for alloca.
Definition TargetInfo.h:51
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a C++ constructor within a class.
Definition DeclCXX.h:2604
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:3013
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:3008
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition DeclCXX.cpp:2999
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2129
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition DeclCXX.cpp:2735
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition DeclCXX.cpp:2714
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:2325
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:3761
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2373
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:573
bool isWeakImported() const
Determine whether this is a weak-imported symbol.
Definition DeclBase.cpp:870
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:559
bool hasAttr() const
Definition DeclBase.h:577
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition DeclBase.h:427
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:232
ExplicitCastExpr - An explicit cast written in the source code.
Definition Expr.h:3928
This represents one expression.
Definition Expr.h:112
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
QualType getType() const
Definition Expr.h:144
Represents a member of a struct/union/class.
Definition Decl.h:3160
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:2000
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:2189
bool hasPrototype() const
Whether this function has a prototype, either because one was explicitly written or because it was "i...
Definition Decl.h:2443
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:3195
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4465
CallingConv getCallConv() const
Definition TypeBase.h:4820
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...
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:52
bool shouldMangleDeclName(const NamedDecl *D)
Definition Mangle.cpp:121
void mangleName(GlobalDecl GD, raw_ostream &)
Definition Mangle.cpp:186
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:1226
Represents a parameter to a function.
Definition Decl.h:1790
void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex)
Definition Decl.h:1823
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:2953
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:8419
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition TypeBase.h:8333
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8366
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
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:338
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1799
unsigned getLength() const
Definition Expr.h:1909
uint32_t getCodeUnit(size_t i) const
Definition Expr.h:1882
StringRef getString() const
Definition Expr.h:1867
unsigned getCharByteWidth() const
Definition Expr.h:1910
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3717
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.
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isPointerType() const
Definition TypeBase.h:8530
bool isReferenceType() const
Definition TypeBase.h:8554
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2801
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition Type.cpp:2436
bool isObjCObjectPointerType() const
Definition TypeBase.h:8705
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9112
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2244
Expr * getSubExpr() const
Definition Expr.h:2285
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
TLSKind getTLSKind() const
Definition Decl.cpp:2175
bool hasInit() const
Definition Decl.cpp:2405
DefinitionKind isThisDeclarationADefinition(ASTContext &) const
Check whether this declaration is a definition.
Definition Decl.cpp:2267
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2197
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
Definition Decl.cpp:2869
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1226
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2655
VarDecl * getDefinition(ASTContext &)
Get the real (not just tentative) definition for this declaration.
Definition Decl.cpp:2373
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition Decl.cpp:2858
const Expr * getInit() const
Definition Decl.h:1368
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition Decl.h:1217
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition Decl.h:952
@ TLS_None
Not a TLS variable.
Definition Decl.h:946
@ Definition
This declaration is definitely a definition.
Definition Decl.h:1301
DefinitionKind hasDefinition(ASTContext &) const
Check whether this variable is defined in this translation unit.
Definition Decl.cpp:2382
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:2786
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1358
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 ...
CIRGenCXXABI * CreateCIRGenItaniumCXXABI(CIRGenModule &cgm)
Creates and Itanium-family ABI.
std::unique_ptr< TargetCIRGenInfo > createX8664TargetCIRGenInfo(CIRGenTypes &cgt)
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Decl, FieldDecl > fieldDecl
Matches field 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
@ 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 alignCXXRecordDecl()
static bool opFuncArmNewAttr()
static bool getRuntimeFunctionDecl()
static bool weakRefReference()
static bool opFuncOptNoneAttr()
static bool opGlobalSection()
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 setFunctionAttributes()
static bool setDLLStorageClass()
static bool opFuncUnwindTablesAttr()
static bool opFuncParameterAttributes()
static bool targetCIRGenInfoArch()
static bool opFuncExtraAttrs()
static bool opFuncNakedAttr()
static bool opFuncSection()
static bool attributeNoBuiltin()
static bool opGlobalDLLImportExport()
static bool opGlobalPartition()
static bool opGlobalWeakRef()
static bool setTargetAttributes()
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 deferredVtables()
static bool cudaSupport()
static bool opFuncMaybeHandleStaticInExternC()
static bool generateDebugInfo()
static bool targetCIRGenInfoOS()
static bool opFuncCPUAndFeaturesAttributes()
static bool maybeHandleStaticInExternC()
static bool setLLVMFunctionFEnvAttributes()
cir::TargetAddressSpaceAttr cirAllocaAddressSpace
mlir::Type uCharTy
ClangIR char.
cir::PointerType allocaInt8PtrTy
void* in alloca address space
cir::PointerType voidPtrTy
void* in address space 0
LangStandard - Information about the properties of a particular language standard.