clang 24.0.0git
CIRGenDecl.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 contains code to emit Decl nodes as CIR code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Address.h"
14#include "CIRGenCleanup.h"
16#include "CIRGenFunction.h"
17#include "EHScopeStack.h"
18#include "mlir/IR/Location.h"
19#include "clang/AST/Attr.h"
20#include "clang/AST/Attrs.inc"
21#include "clang/AST/Decl.h"
23#include "clang/AST/Expr.h"
24#include "clang/AST/ExprCXX.h"
26#include "clang/Basic/Cuda.h"
30#include "llvm/Support/SaveAndRestore.h"
31
32using namespace clang;
33using namespace clang::CIRGen;
34
35namespace {
36/// Call the function named by a variable's `cleanup` attribute with the
37/// variable's address when it goes out of scope.
38struct CallCleanupFunction final : EHScopeStack::Cleanup {
39 const CIRGenFunctionInfo &fnInfo;
40 Address addr;
41 QualType varType;
42 CIRGenCallee callee;
43 SourceRange attrRange;
44
45 CallCleanupFunction(cir::FuncOp cleanupFn, const CIRGenFunctionInfo *info,
46 Address addr, QualType varType, const CleanupAttr *attr)
47 : fnInfo(*info), addr(addr), varType(varType),
48 callee(CIRGenCallee::forDirect(
49 cleanupFn, CIRGenCalleeInfo(attr->getFunctionDecl()))),
50 attrRange(attr->getRange()) {}
51
52 void emit(CIRGenFunction &cgf, Flags flags) override {
53 assert(!fnInfo.arguments().empty());
54 QualType argTy = fnInfo.arguments()[0];
55 // Sema only guarantees that these are assignable/convertible pointers (that
56 // is, the address-of the cleanup variable and the function parameter type),
57 // which don't necessarily match in either pointee type or address space.
58 // For example: address-of-g is void**, but the function takes void*.
59 // void f(void* arg);
60 // __attribute__((cleanup(f))) void *g;
61 // Likewise `int *__ptr32` parameters are accepted for an `int` variable.
62 // Classic codegen only emits a bitcast, which asserts for the latter.
63 // Both casts are no-ops when the types already match.
64 CIRGenBuilderTy &builder = cgf.getBuilder();
65 auto argPtrTy = mlir::cast<cir::PointerType>(cgf.convertType(argTy));
66 mlir::Value arg =
67 builder.createPtrBitcast(addr.getPointer(), argPtrTy.getPointee());
68 arg = cgf.performAddrSpaceCast(arg, argPtrTy);
69
70 CallArgList args;
71 args.add(RValue::get(arg), cgf.getContext().getPointerType(varType));
72 cgf.emitCall(fnInfo, callee, ReturnValueSlot(), args,
73 /*callOp=*/nullptr, /*isMustTail=*/false, attrRange);
74 }
75};
76} // namespace
77
78struct CallLifetimeEnd final : EHScopeStack::Cleanup {
79 // The raw alloca pointer (in the alloca address space). Mirrors classic
80 // CodeGen's CallLifetimeEnd, which stores the llvm::Value pointer rather
81 // than an Address.
82 mlir::Value addr;
83 CallLifetimeEnd(mlir::Value addr) : addr(addr) {}
84 bool isRedundantBeforeReturn() override { return true; }
85 void emit(CIRGenFunction &cgf, Flags flags) override {
86 cgf.emitLifetimeEndOp(addr.getLoc(), addr);
87 }
88};
89
92 mlir::OpBuilder::InsertPoint ip) {
93 QualType ty = d.getType();
94 assert(
97
98 mlir::Location loc = getLoc(d.getSourceRange());
99 bool nrvo =
100 getContext().getLangOpts().ElideConstructors && d.isNRVOVariable();
101
103 emission.isEscapingByRef = d.isEscapingByref();
104 if (emission.isEscapingByRef)
105 cgm.errorNYI(d.getSourceRange(),
106 "emitAutoVarAlloca: decl escaping by reference");
107
108 CharUnits alignment = getContext().getDeclAlign(&d);
109
110 // If the type is variably-modified, emit all the VLA sizes for it.
111 if (ty->isVariablyModifiedType())
113
115
116 Address address = Address::invalid();
117 if (ty->isConstantSizeType()) {
118 // If this value is an array, struct, or vector with a statically
119 // determinable constant initializer, there are optimizations we can do.
120 //
121 // TODO: We should constant-evaluate the initializer of any variable,
122 // as long as it is initialized by a constant expression. Currently,
123 // isConstantInitializer produces wrong answers for structs with
124 // reference or bitfield members, and a few other cases, and checking
125 // for POD-ness protects us from some of these.
126 if (d.getInit() &&
127 (ty->isArrayType() || ty->isRecordType() || ty->isVectorType()) &&
128 (d.isConstexpr() ||
129 ((ty.isPODType(getContext()) ||
130 getContext().getBaseElementType(ty)->isObjCObjectPointerType()) &&
132
133 // If the variable's a const type, and it's neither an NRVO
134 // candidate nor a __block variable and has no mutable members,
135 // emit it as a global instead.
136 // Exception is if a variable is located in non-constant address space
137 // in OpenCL.
138 // TODO(cir): perhaps we don't need this at all at CIR since this can
139 // be done as part of lowering down to LLVM.
140 bool needsDtor =
142 if ((!getContext().getLangOpts().OpenCL ||
144 (cgm.getCodeGenOpts().MergeAllConstants && !nrvo &&
145 !d.isEscapingByref() &&
146 ty.isConstantStorage(getContext(), true, !needsDtor))) {
147 cgm.errorNYI(d.getSourceRange(), "emitAutoVarAlloca: type constant");
148 }
149 // Otherwise, tell the initialization code that we're in this case.
150 emission.isConstantAggregate = true;
151 }
152
153 // A normal fixed sized variable becomes an alloca in the entry block,
154 // unless:
155 // - it's an NRVO variable.
156 // - we are compiling OpenMP and it's an OpenMP local variable.
157 if (nrvo) {
158 // The named return value optimization: allocate this variable in the
159 // return slot, so that we can elide the copy when returning this
160 // variable (C++0x [class.copy]p34).
161 address = returnValue;
162
163 if (const RecordDecl *rd = ty->getAsRecordDecl()) {
164 if (const auto *cxxrd = dyn_cast<CXXRecordDecl>(rd);
165 (cxxrd && !cxxrd->hasTrivialDestructor()) ||
166 rd->isNonTrivialToPrimitiveDestroy()) {
167 // In LLVM: Create a flag that is used to indicate when the NRVO was
168 // applied to this variable. Set it to zero to indicate that NRVO was
169 // not applied. For now, use the same approach for CIRGen until we can
170 // be sure it's worth doing something more aggressive.
171 cir::ConstantOp falseNVRO = builder.getFalse(loc);
172 Address nrvoFlag = createTempAlloca(falseNVRO.getType(),
173 CharUnits::One(), loc, "nrvo",
174 /*arraySize=*/nullptr);
175 assert(builder.getInsertionBlock());
176 builder.createStore(loc, falseNVRO, nrvoFlag);
177
178 // Record the NRVO flag for this variable.
179 nrvoFlags[&d] = nrvoFlag.getPointer();
180 emission.nrvoFlag = nrvoFlag.getPointer();
181 }
182 }
183 } else {
184 // A normal fixed sized variable becomes an alloca in the entry block,
185 mlir::Type allocaTy = convertTypeForMem(ty);
186 // Create the temp alloca and declare variable using it.
187 address = createTempAlloca(allocaTy, alignment, loc, d.getName(),
188 /*arraySize=*/nullptr, /*alloca=*/nullptr, ip);
189 declare(address.getPointer(), &d, ty, getLoc(d.getSourceRange()),
190 alignment);
191 // A goto/switch that bypasses the init splits the lifetime across IR
192 // regions and miscompiles under stack coloring (PR28267). Lacking
193 // classic's per-decl bypass analysis, drop markers for the whole
194 // function if any such statement is present.
196 if (shouldEmitLifetimeMarkersForAutoVar() && haveInsertPoint()) {
198 loc, address.getUnderlyingAllocaOp().getResult());
199 }
200 }
201 } else {
202 // Non-constant size type
204 if (!didCallStackSave) {
205 // Save the stack.
206 cir::PointerType defaultTy = allocaInt8PtrTy;
208 cgm.getDataLayout().getAlignment(defaultTy, false));
209 Address stack = createTempAlloca(defaultTy, align, loc, "saved_stack");
210
211 mlir::Value v = builder.createStackSave(loc, defaultTy);
212 assert(v.getType() == allocaInt8PtrTy);
213 builder.createStore(loc, v, stack);
214
215 didCallStackSave = true;
216
217 // Push a cleanup block and restore the stack there.
218 // FIXME: in general circumstances, this should be an EH cleanup.
220 }
221
222 VlaSizePair vlaSize = getVLASize(ty);
223 mlir::Type memTy = convertTypeForMem(vlaSize.type);
224
225 // Allocate memory for the array.
226 address =
227 createTempAlloca(memTy, alignment, loc, d.getName(), vlaSize.numElts,
228 /*alloca=*/nullptr, builder.saveInsertionPoint());
229
230 // If we have debug info enabled, properly describe the VLA dimensions for
231 // this type by registering the vla size expression for each of the
232 // dimensions.
234 }
235
236 emission.addr = address;
237 setAddrOfLocalVar(&d, address);
238
239 if (d.hasAttr<AnnotateAttr>() && haveInsertPoint())
240 emitVarAnnotations(&d, address.getPointer());
241
242 // The lifetime marker must reference the original alloca, so peel any
243 // address-space cast back to it.
244 if (emission.useLifetimeMarkers)
245 ehStack.pushCleanup<CallLifetimeEnd>(
246 NormalEHLifetimeMarker, address.getUnderlyingAllocaOp().getResult());
247
248 return emission;
249}
250
251/// Determine whether the given initializer is trivial in the sense
252/// that it requires no code to be generated.
254 if (!init)
255 return true;
256
257 if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(init))
258 if (CXXConstructorDecl *constructor = construct->getConstructor())
259 if (constructor->isTrivial() && constructor->isDefaultConstructor() &&
260 !construct->requiresZeroInitialization())
261 return true;
262
263 return false;
264}
265
266static void emitStoresForConstant(CIRGenModule &cgm, const VarDecl &d,
267 Address addr, bool isVolatile,
268 CIRGenBuilderTy &builder,
269 mlir::TypedAttr constant) {
270 mlir::Type ty = constant.getType();
271 cir::CIRDataLayout layout{cgm.getModule()};
272 uint64_t constantSize = layout.getTypeAllocSize(ty);
273 if (!constantSize)
274 return;
277
278 if (addr.getElementType() != ty)
279 addr = addr.withElementType(builder, ty);
280
281 // If the address is an alloca, set the init attribute.
282 // The address is usually and alloca, but there is at least one case where
283 // emitAutoVarInit is called from the OpenACC codegen with an address that
284 // is not an alloca.
285 cir::AllocaOp allocaOp = addr.getUnderlyingAllocaOp();
286 if (allocaOp)
287 allocaOp.setInitAttr(mlir::UnitAttr::get(&cgm.getMLIRContext()));
288
289 // There are cases where OpenACC codegen calls emitAutoVarInit with a
290 // temporary decl that doesn't have a source range set.
291 mlir::Location loc = builder.getUnknownLoc();
292 if (d.getSourceRange().isValid())
293 loc = cgm.getLoc(d.getSourceRange());
294
295 // Emit cir.const + cir.store, preserving source-level semantics. For
296 // aggregate types (arrays, records), LoweringPrepare implements the OG
297 // optimization tiers (shouldCreateMemCpyFromGlobal, shouldUseBZeroPlusStores,
298 // shouldUseMemSetToInitialize, shouldSplitConstantStore) by transforming
299 // into cir.global + cir.get_global + cir.copy when appropriate.
300 builder.createStore(loc, builder.getConstant(loc, constant), addr);
301}
302
304 const CIRGenFunction::AutoVarEmission &emission) {
305 assert(emission.variable && "emission was not valid!");
306
307 // If this was emitted as a global constant, we're done.
308 if (emission.wasEmittedAsGlobal())
309 return;
310
311 const VarDecl &d = *emission.variable;
312
313 QualType type = d.getType();
314
315 // If this local has an initializer, emit it now.
316 const Expr *init = d.getInit();
317
318 // Initialize the variable here if it doesn't have a initializer and it is a
319 // C struct that is non-trivial to initialize or an array containing such a
320 // struct.
321 if (!init && type.isNonTrivialToPrimitiveDefaultInitialize() ==
323 cgm.errorNYI(d.getSourceRange(),
324 "emitAutoVarInit: non-trivial to default initialize");
325 return;
326 }
327
328 const Address addr = emission.addr;
329
330 // Check whether this is a byref variable that's potentially
331 // captured and moved by its own initializer. If so, we'll need to
332 // emit the initializer first, then copy into the variable.
334
335 // Note: constexpr already initializes everything correctly.
336 LangOptions::TrivialAutoVarInitKind trivialAutoVarInit =
337 (d.isConstexpr()
339 : (d.getAttr<UninitializedAttr>()
341 : getContext().getLangOpts().getTrivialAutoVarInit()));
342
343 auto initializeWhatIsTechnicallyUninitialized = [&](Address addr) {
344 if (trivialAutoVarInit ==
346 return;
347
348 cgm.errorNYI(d.getSourceRange(), "emitAutoVarInit: trivial initialization");
349 };
350
351 if (isTrivialInitializer(init)) {
352 initializeWhatIsTechnicallyUninitialized(addr);
353 return;
354 }
355
356 mlir::Attribute constant;
357 if (emission.isConstantAggregate ||
359 // FIXME: Differently from LLVM we try not to emit / lower too much
360 // here for CIR since we are interested in seeing the ctor in some
361 // analysis later on. So CIR's implementation of ConstantEmitter will
362 // frequently return an empty Attribute, to signal we want to codegen
363 // some trivial ctor calls and whatnots.
365 if (constant && !mlir::isa<cir::ZeroAttr>(constant) &&
366 (trivialAutoVarInit !=
368 cgm.errorNYI(d.getSourceRange(), "emitAutoVarInit: constant aggregate");
369 return;
370 }
371 }
372
373 // NOTE(cir): In case we have a constant initializer, we can just emit a
374 // store. But, in CIR, we wish to retain any ctor calls, so if it is a
375 // CXX temporary object creation, we ensure the ctor call is used deferring
376 // its removal/optimization to the CIR lowering.
377 if (!constant || isa<CXXTemporaryObjectExpr>(init)) {
378 initializeWhatIsTechnicallyUninitialized(addr);
380 emitExprAsInit(init, &d, lv);
381
382 if (!emission.wasEmittedAsOffloadClause()) {
383 // In case lv has uses it means we indeed initialized something
384 // out of it while trying to build the expression, mark it as such.
385 Address addr = lv.getAddress();
386 assert(addr.isValid() && "Should have an address");
387 cir::AllocaOp allocaOp = addr.getUnderlyingAllocaOp();
388 assert(allocaOp && "Address should come straight out of the alloca");
389
390 if (!allocaOp.use_empty())
391 allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
392 }
393
394 return;
395 }
396
397 // FIXME(cir): migrate most of this file to use mlir::TypedAttr directly.
398 auto typedConstant = mlir::dyn_cast<mlir::TypedAttr>(constant);
399 assert(typedConstant && "expected typed attribute");
400 if (!emission.isConstantAggregate) {
401 // For simple scalar/complex initialization, store the value directly.
402 LValue lv = makeAddrLValue(addr, type);
403 assert(init && "expected initializer");
404 mlir::Location initLoc = getLoc(init->getSourceRange());
405 // lv.setNonGC(true);
407 RValue::get(builder.getConstant(initLoc, typedConstant)), lv);
408 }
409
410 emitStoresForConstant(cgm, d, addr, type.isVolatileQualified(), builder,
411 typedConstant);
412}
413
415 const CIRGenFunction::AutoVarEmission &emission) {
416 const VarDecl &d = *emission.variable;
417
418 // Check the type for a cleanup.
420 emitAutoVarTypeCleanup(emission, dtorKind);
421
423
424 // Handle the cleanup attribute.
425 if (const CleanupAttr *ca = d.getAttr<CleanupAttr>()) {
426 const FunctionDecl *fd = ca->getFunctionDecl();
427
428 cir::FuncOp f = cgm.getAddrOfFunction(fd);
429 assert(f && "Could not find function!");
430
431 const CIRGenFunctionInfo &info =
432 cgm.getTypes().arrangeFunctionDeclaration(fd);
433 ehStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, f, &info,
434 emission.getObjectAddress(*this),
435 d.getType(), ca);
436 }
437}
438
439/// Emit code and set up symbol table for a variable declaration with auto,
440/// register, or no storage class specifier. These turn into simple stack
441/// objects, globals depending on target.
447
448namespace {
449/// Finds the temporaries with a destructor that the initializer of a variable
450/// with automatic storage duration lifetime-extends. Their cleanups are pushed
451/// onto the EH stack when the initializer's full-expression ends.
452class LifetimeExtendedTemporaryFinder
453 : public RecursiveASTVisitor<LifetimeExtendedTemporaryFinder> {
454 const VarDecl &var;
457
458public:
459 explicit LifetimeExtendedTemporaryFinder(const VarDecl &var) : var(var) {}
460
461 /// The types of the temporaries that were found.
462 ArrayRef<QualType> found() const { return types; }
463
464 // Look into the semantic form of initializer lists, which holds the backing
465 // array of a std::initializer_list, and into default member initializers.
466 // The array filler of an initializer list is not visited.
467 bool shouldVisitImplicitCode() const { return true; }
468
469 /// Records the type of each temporary requiring destruction whose lifetime
470 /// is extended by \c var.
471 bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *m) {
472 if (m->getExtendingDecl() != &var ||
473 m->getStorageDuration() != SD_Automatic || !visited.insert(m).second)
474 return true;
475 // Like pushTemporaryCleanup, look at the type of the whole temporary, not
476 // at the subobject that the reference binds to.
477 SmallVector<const Expr *, 2> commaLHSs;
478 SmallVector<SubobjectAdjustment, 2> adjustments;
479 const Expr *e =
480 m->getSubExpr()->skipRValueSubobjectAdjustments(commaLHSs, adjustments);
482 types.push_back(e->getType());
483 return true;
484 }
485};
486} // namespace
487
489 const CXXRecordDecl *rd =
490 type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
491 if (!rd || rd->hasTrivialDestructor())
492 return false;
493 const auto *fpt = rd->getDestructor()->getType()->castAs<FunctionProtoType>();
494 return isUnresolvedExceptionSpec(fpt->getExceptionSpecType()) ||
495 !fpt->isNothrow();
496}
497
500 const Expr *init = d.getInit();
501 if (!init)
502 return {};
503 LifetimeExtendedTemporaryFinder finder(d);
504 finder.TraverseStmt(const_cast<Expr *>(init));
505 return SmallVector<QualType, 2>(finder.found());
506}
507
509 const VarDecl &d, CapturedCleanups &condCleanup,
510 ArrayRef<QualType> extendedTemporaries) {
511 assert(d.hasLocalStorage() && "loop condition variable is not local");
512
513 // Mirror the diagnostic emitted by emitVarDecl on the automatic-storage path.
514 // A condition variable is implicitly in the private address space, so this is
515 // not expected to fire, but keep it to preserve emitVarDecl's behavior.
517 cgm.errorNYI(d.getSourceRange(),
518 "emitLoopConditionVariable: OpenCL local address space");
519
520 // The loop's cleanup region destroys the variable first, then the
521 // temporaries in reverse order of construction. A throw would skip the
522 // remaining destructors because CFG flattening does not yet support EH paths
523 // in loop cleanup regions.
524 if (getLangOpts().Exceptions && !extendedTemporaries.empty() &&
527 (extendedTemporaries.size() > 1 &&
528 llvm::any_of(extendedTemporaries, destructorMayThrow))))
529 cgm.errorNYI(d.getSourceRange(),
530 "loop condition variable cleanup with a potentially-throwing "
531 "destructor before another destructor");
532
533 // The loop's cleanup region covers the initialization of the variable.
535 /*guardDestruction=*/true,
536 extendedTemporaries);
537}
538
540 const VarDecl &d, CapturedCleanups &cleanups, bool guardDestruction,
541 ArrayRef<QualType> extendedTemporaries) {
542 assert(d.hasLocalStorage() && "captured cleanups for a non-local variable");
543 assert((guardDestruction || extendedTemporaries.empty()) &&
544 "lifetime-extended temporaries need a guarded cleanup region");
545 CIRGenFunction::VarDeclContext varDeclCtx{*this, &d};
546 CIRGenFunction::AutoVarEmission emission = [&] {
547 CapturedCleanups::CaptureScope capture(cleanups);
548 return emitAutoVarAlloca(d);
549 }();
550
551 // If the cleanup region covers initialization, guard destruction with an
552 // active flag so that it cannot run when the initializer throws. The flag is
553 // cleared each time control reaches the declaration and set once the
554 // variable is constructed. The temporaries that the initializer
555 // lifetime-extends are destroyed with the variable ([class.temporary]p6), so
556 // capture their cleanups under the same flag.
557 bool needsCleanup =
558 guardDestruction && d.needsDestruction(getContext()) != QualType::DK_none;
559 bool extendsTemporaries = !extendedTemporaries.empty();
560
561 Address activeFlag = Address::invalid();
562 if (needsCleanup || extendsTemporaries) {
563 mlir::Location loc = getLoc(d.getSourceRange());
564 activeFlag = createTempAllocaWithoutCast(
565 builder.getBoolTy(), CharUnits::One(), loc, "cond.cleanup.isactive",
566 /*arraySize=*/nullptr,
567 builder.getBestAllocaInsertPoint(getCurFunctionEntryBlock()));
568 builder.createFlagStore(loc, false, activeFlag.getPointer());
569 }
570
571 // Initializer temporaries keep their normal cleanup handling, except for the
572 // lifetime-extended ones, which are captured when the full-expression ends.
573 // For unguarded regions, initialization must not leave uncaptured cleanups.
574 EHScopeStack::stable_iterator initDepth = ehStack.stable_begin();
575 {
576 LifetimeExtensionCapture capture{&d, &cleanups, activeFlag};
578 lifetimeExtensionCapture, extendsTemporaries ? &capture : nullptr);
579 emitAutoVarInit(emission);
580 }
581 assert((guardDestruction || ehStack.stable_begin() == initDepth) &&
582 "initializer left cleanups that cannot be captured");
583
584 // Only lifetime-extended cleanups remain after initialization. If the AST
585 // walk found no extended temporaries (e.g. for initializer-list array
586 // fillers), these cleanups are uncaptured and would wrap the code that
587 // follows the declaration, such as a loop's cir.condition, in a
588 // cir.cleanup.scope.
589 if (!extendsTemporaries && ehStack.stable_begin() != initDepth) {
590 cgm.errorNYI(d.getSourceRange(),
591 "variable with captured cleanups whose initializer leaves "
592 "uncaptured cleanups");
593 popCleanupBlocks(initDepth);
594 }
595#ifndef NDEBUG
596 for (auto it = ehStack.begin(), ie = ehStack.find(initDepth); it != ie; ++it)
597 assert(!cast<EHCleanupScope>(*it).getCleanupScopeOp() &&
598 "variable initializer left an uncaptured cleanup");
599#endif
600
601 if (activeFlag.isValid()) {
602 // Construction has completed, so activate the destructor cleanups.
603 mlir::Location loc = getLoc(d.getSourceRange());
604 builder.createFlagStore(loc, true, activeFlag.getPointer());
605 }
606
607 {
608 CapturedCleanups::CaptureScope capture(cleanups);
609 emitAutoVarCleanups(emission);
610 }
611
612 if (needsCleanup)
613 initFullExprCleanupWithFlag(activeFlag);
614}
615
617 // If the declaration has external storage, don't emit it now, allow it to be
618 // emitted lazily on its first use.
619 if (d.hasExternalStorage())
620 return;
621
622 if (d.getStorageDuration() != SD_Automatic) {
623 // Static sampler variables translated to function calls.
624 if (d.getType()->isSamplerT()) {
625 // Nothing needs to be done here, but let's flag it as an error until we
626 // have a test. It requires OpenCL support.
627 cgm.errorNYI(d.getSourceRange(), "emitVarDecl: static sampler type");
628 return;
629 }
630
631 cir::GlobalLinkageKind linkage = cgm.getCIRLinkageVarDefinition(&d);
632
633 // FIXME: We need to force the emission/use of a guard variable for
634 // some variables even if we can constant-evaluate them because
635 // we can't guarantee every translation unit will constant-evaluate them.
636
637 return emitStaticVarDecl(d, linkage);
638 }
639
641 cgm.errorNYI(d.getSourceRange(), "emitVarDecl: openCL address space");
642
643 assert(d.hasLocalStorage());
644
645 CIRGenFunction::VarDeclContext varDeclCtx{*this, &d};
646 return emitAutoVarDecl(d);
647}
648
649static std::string getStaticDeclName(CIRGenModule &cgm, const VarDecl &d) {
650 if (cgm.getLangOpts().CPlusPlus)
651 return cgm.getMangledName(&d).str();
652
653 // If this isn't C++, we don't need a mangled name, just a pretty one.
654 assert(!d.isExternallyVisible() && "name shouldn't matter");
655 std::string contextName;
656 const DeclContext *dc = d.getDeclContext();
657 if (auto *cd = dyn_cast<CapturedDecl>(dc))
658 dc = cast<DeclContext>(cd->getNonClosureContext());
659 if (const auto *fd = dyn_cast<FunctionDecl>(dc))
660 contextName = std::string(cgm.getMangledName(fd));
661 else if (isa<BlockDecl>(dc))
662 cgm.errorNYI(d.getSourceRange(),
663 "getStaticDeclName: block decl context for static var");
664 else if (isa<ObjCMethodDecl>(dc))
665 cgm.errorNYI(d.getSourceRange(),
666 "getStaticDeclName: ObjC decl context for static var");
667 else
668 cgm.errorNYI(d.getSourceRange(),
669 "getStaticDeclName: Unknown context for static var decl");
670
671 contextName += "." + d.getNameAsString();
672 return contextName;
673}
674
675// TODO(cir): LLVM uses a Constant base class. Maybe CIR could leverage an
676// interface for all constants?
677cir::GlobalOp
679 cir::GlobalLinkageKind linkage) {
680 // In general, we don't always emit static var decls once before we reference
681 // them. It is possible to reference them before emitting the function that
682 // contains them, and it is possible to emit the containing function multiple
683 // times.
684 if (cir::GlobalOp existingGV = getStaticLocalDeclAddress(&d))
685 return existingGV;
686
687 QualType ty = d.getType();
688 assert(ty->isConstantSizeType() && "VLAs can't be static");
689
690 // Use the label if the variable is renamed with the asm-label extension.
691 std::string name;
692 if (d.hasAttr<AsmLabelAttr>())
693 name = std::string(getMangledName(&d));
694 else
695 name = getStaticDeclName(*this, d);
696
697 mlir::Type lty = getTypes().convertTypeForMem(ty);
698
699 // OpenCL variables in local address space and CUDA shared
700 // variables cannot have an initializer.
701 mlir::Attribute init = nullptr;
703 d.hasAttr<CUDASharedAttr>() || d.hasAttr<LoaderUninitializedAttr>())
704 init = cir::UndefAttr::get(lty);
705 else
706 init = emitNullConstantAttr(ty);
707
708 mlir::ptr::MemorySpaceAttrInterface addrSpace =
710
711 cir::GlobalOp gv =
712 builder.createVersionedGlobal(getModule(), getLoc(d.getLocation()), name,
713 lty, false, linkage, addrSpace);
715 // TODO(cir): infer visibility from linkage in global op builder.
716 gv.setVisibility(getMLIRVisibilityFromCIRLinkage(linkage));
717 gv.setInitialValueAttr(init);
718 gv.setAlignment(getASTContext().getDeclAlign(&d).getAsAlign().value());
719
720 if (supportsCOMDAT() && gv.isWeakForLinker())
721 gv.setSelfComdat();
722
723 if (d.getTLSKind())
724 setTLSMode(gv, d);
725
726 setGVProperties(gv, &d);
727
728 // The global may live in a different address space than the declared type.
729 // Users of the address cast it through castGlobalToDeclAddrSpace.
731
732 // Ensure that the static local gets initialized by making sure the parent
733 // function gets emitted eventually.
734 const Decl *dc = cast<Decl>(d.getDeclContext());
735
736 // We can't name blocks or captured statements directly, so try to emit their
737 // parents.
738 if (isa<BlockDecl>(dc) || isa<CapturedDecl>(dc)) {
739 dc = dc->getNonClosureContext();
740 // FIXME: Ensure that global blocks get emitted.
741 if (!dc)
742 errorNYI(d.getSourceRange(), "non-closure context");
743 }
744
745 GlobalDecl gd;
746 if (const auto *cd = dyn_cast<CXXConstructorDecl>(dc))
747 gd = GlobalDecl(cd, Ctor_Base);
748 else if (const auto *dd = dyn_cast<CXXDestructorDecl>(dc))
749 gd = GlobalDecl(dd, Dtor_Base);
750 else if (const auto *fd = dyn_cast<FunctionDecl>(dc))
751 gd = GlobalDecl(fd);
752 else {
753 // Don't do anything for Obj-C method decls or global closures. We should
754 // never defer them.
755 assert(isa<ObjCMethodDecl>(dc) && "unexpected parent code decl");
756 }
757 if (gd.getDecl()) {
758 if (getLangOpts().OpenMPIsTargetDevice) {
759 // Disable emission of the parent function for the OpenMP device codegen.
760 // TODO(cir): Use CGOpenMPRuntime::DisableAutoDeclareTargetRAII here.
762 "OpenMP: DisableAutoDeclareTargetRAII for static local");
763 }
764 (void)getAddrOfGlobal(gd);
765 }
766
767 return gv;
768}
769
771 mlir::Attribute constAttr,
772 CharUnits align) {
773 auto functionName = [&](const DeclContext *dc) -> std::string {
774 if (const auto *fd = dyn_cast<FunctionDecl>(dc)) {
775 if (const auto *cc = dyn_cast<CXXConstructorDecl>(fd))
776 return cc->getNameAsString();
777 if (const auto *cd = dyn_cast<CXXDestructorDecl>(fd))
778 return cd->getNameAsString();
779 return std::string(getMangledName(fd));
780 } else if (const auto *om = dyn_cast<ObjCMethodDecl>(dc)) {
781 return om->getNameAsString();
782 } else if (isa<BlockDecl>(dc)) {
783 return "<block>";
784 } else if (isa<CapturedDecl>(dc)) {
785 return "<captured>";
786 } else {
787 llvm_unreachable("expected a function or method");
788 }
789 };
790
791 // Form a simple per-variable cache of these values in case we find we
792 // want to reuse them.
793 cir::GlobalOp &cacheEntry = initializerConstants[&d];
794 if (!cacheEntry || cacheEntry.getInitialValue() != constAttr) {
795 auto ty = mlir::cast<mlir::TypedAttr>(constAttr).getType();
796 bool isConstant = true;
797
798 std::string name;
799 if (d.hasGlobalStorage())
800 name = getMangledName(&d).str() + ".const";
801 else if (const DeclContext *dc = d.getParentFunctionOrMethod())
802 name = ("__const." + functionName(dc) + "." + d.getName()).str();
803 else
804 llvm_unreachable("local variable has no parent function or method");
805
807 cir::GlobalOp gv = builder.createVersionedGlobal(
808 getModule(), getLoc(d.getLocation()), name, ty, isConstant,
809 cir::GlobalLinkageKind::PrivateLinkage);
811 // TODO(cir): infer visibility from linkage in global op builder.
812 gv.setVisibility(getMLIRVisibilityFromCIRLinkage(
813 cir::GlobalLinkageKind::PrivateLinkage));
814 gv.setInitialValueAttr(constAttr);
815 gv.setAlignment(align.getAsAlign().value());
816 gv.setUnnamedAddr(true);
817
818 cacheEntry = gv;
819 } else if (cacheEntry.getAlignment() <
820 static_cast<uint64_t>(align.getQuantity())) {
821 cacheEntry.setAlignment(align.getAsAlign().value());
822 }
823
824 // Create a GetGlobalOp to get a pointer to the global
826 mlir::Type eltTy = mlir::cast<mlir::TypedAttr>(constAttr).getType();
827 auto ptrTy = builder.getPointerTo(cacheEntry.getSymType());
828 mlir::Value globalPtr = cir::GetGlobalOp::create(
829 builder, getLoc(d.getLocation()), ptrTy, cacheEntry.getSymName());
830 return Address(globalPtr, eltTy, align);
831}
832
833/// Add the initializer for 'd' to the global variable that has already been
834/// created for it. If the initializer has a different type than gv does, this
835/// may free gv and return a different one. Otherwise it just returns gv.
837 const VarDecl &d, cir::GlobalOp gv, cir::GetGlobalOp gvAddr) {
838 ConstantEmitter emitter(*this);
839 mlir::TypedAttr init = mlir::dyn_cast_if_present<mlir::TypedAttr>(
840 emitter.tryEmitForInitializer(d));
841
842 // If constant emission failed, then this should be a C++ static
843 // initializer.
844 if (!init) {
845 if (!getLangOpts().CPlusPlus) {
846 cgm.errorNYI(d.getInit()->getSourceRange(),
847 "constant l-value expression");
848 } else if (d.hasFlexibleArrayInit(getContext())) {
849 cgm.errorNYI(d.getInit()->getSourceRange(), "flexible array initializer");
850 } else {
851 // Since we have a static initializer, this global variable can't
852 // be constant.
853 gv.setConstant(false);
854 emitCXXGuardedInit(d, gv, /*performInit*/ true);
855 gvAddr.setStaticLocal(true);
856 }
857 return gv;
858 }
859
860 // TODO(cir): There should be debug code here to assert that the decl size
861 // matches the CIR data layout type alloc size, but the code for calculating
862 // the type alloc size is not implemented yet.
864
865 // The initializer may differ in type from the global. Rewrite
866 // the global to match the initializer. (We have to do this
867 // because some types, like unions, can't be completely represented
868 // in the LLVM type system.)
869 if (gv.getSymType() != init.getType()) {
870 gv.setSymType(init.getType());
871
872 // Normally this should be done with a call to cgm.replaceGlobal(oldGV, gv),
873 // but since at this point the current block hasn't been really attached,
874 // there's no visibility into the GetGlobalOp corresponding to this Global.
875 // Given those constraints, thread in the GetGlobalOp and update it
876 // directly.
878 gvAddr.getAddr().setType(builder.getPointerTo(init.getType()));
879 }
880
881 bool needsDtor =
883
884 gv.setConstant(d.getType().isConstantStorage(
885 getContext(), /*ExcludeCtor=*/true, !needsDtor));
886 gv.setInitialValueAttr(init);
887
888 emitter.finalize(gv);
889
890 if (needsDtor) {
891 // We have a constant initializer, but a nontrivial destructor. We still
892 // need to perform a guarded "initialization" in order to register the
893 // destructor.
894 emitCXXGuardedInit(d, gv, /*performInit=*/false);
895 gvAddr.setStaticLocal(true);
896 }
897
898 return gv;
899}
900
902 cir::GlobalLinkageKind linkage) {
903 // Check to see if we already have a global variable for this
904 // declaration. This can happen when double-emitting function
905 // bodies, e.g. with complete and base constructors.
906 cir::GlobalOp globalOp = cgm.getOrCreateStaticVarDecl(d, linkage);
907 // TODO(cir): we should have a way to represent global ops as values without
908 // having to emit a get global op. Sometimes these emissions are not used.
909 mlir::Value addr =
910 builder.createGetGlobal(globalOp, d.getTLSKind() != VarDecl::TLS_None);
911 auto getAddrOp = addr.getDefiningOp<cir::GetGlobalOp>();
912 assert(getAddrOp && "expected cir::GetGlobalOp");
913
914 CharUnits alignment = getContext().getDeclAlign(&d);
915
916 // Store into LocalDeclMap before generating initializer to handle
917 // circular references.
918 mlir::Type elemTy = convertTypeForMem(d.getType());
919 setAddrOfLocalVar(&d, Address(addr, elemTy, alignment));
920
921 // We can't have a VLA here, but we can have a pointer to a VLA,
922 // even though that doesn't really make any sense.
923 // Make sure to evaluate VLA bounds now so that we have them for later.
926
927 // Save the type in case adding the initializer forces a type change.
928 mlir::Type expectedType = addr.getType();
929
930 cir::GlobalOp var = globalOp;
931
932 // Sema ensures any initializer of a CUDA __shared__ variable is a no-op.
933 bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
934 d.hasAttr<CUDASharedAttr>();
935 // If this value has an initializer, emit it.
936 if (d.getInit() && !isCudaSharedVar)
937 var = addInitializerToStaticVarDecl(d, var, getAddrOp);
938
939 var.setAlignment(alignment.getAsAlign().value());
940
941 // There are a lot of attributes that need to be handled here. Until
942 // we start to support them, we just report an error if there are any.
943 if (d.hasAttr<AnnotateAttr>())
944 cgm.addGlobalAnnotations(&d, var);
945 if (d.getAttr<PragmaClangBSSSectionAttr>())
946 cgm.errorNYI(d.getSourceRange(),
947 "emitStaticVarDecl: CIR global BSS section attribute");
948 if (d.getAttr<PragmaClangDataSectionAttr>())
949 cgm.errorNYI(d.getSourceRange(),
950 "emitStaticVarDecl: CIR global Data section attribute");
951 if (d.getAttr<PragmaClangRodataSectionAttr>())
952 cgm.errorNYI(d.getSourceRange(),
953 "emitStaticVarDecl: CIR global Rodata section attribute");
954 if (d.getAttr<PragmaClangRelroSectionAttr>())
955 cgm.errorNYI(d.getSourceRange(),
956 "emitStaticVarDecl: CIR global Relro section attribute");
957
958 if (const SectionAttr *sa = d.getAttr<SectionAttr>())
959 var.setSectionAttr(builder.getStringAttr(sa->getName()));
960
961 if (cgm.getCodeGenOpts().KeepPersistentStorageVariables)
962 cgm.errorNYI(d.getSourceRange(), "static var keep persistent storage");
963
964 // From traditional codegen:
965 // We may have to cast the constant because of the initializer
966 // mismatch above.
967 //
968 // FIXME: It is really dangerous to store this in the map; if anyone
969 // RAUW's the GV uses of this constant will be invalid.
970 mlir::Value castedAddr =
971 builder.createBitcast(getAddrOp.getAddr(), expectedType);
972 castedAddr = cgm.castGlobalToDeclAddrSpace(castedAddr, d);
973 localDeclMap.find(&d)->second = Address(castedAddr, elemTy, alignment);
974 cgm.setStaticLocalDeclAddress(&d, var);
975
978}
979
981 bool capturedByInit) {
983
984 SourceLocRAIIObject locRAII{*this, init->getSourceRange()};
985 mlir::Value value = emitScalarExpr(init);
986 if (capturedByInit) {
987 cgm.errorNYI(init->getSourceRange(), "emitScalarInit: captured by init");
988 return;
989 }
991 emitStoreThroughLValue(RValue::get(value), lvalue, true);
992}
993
995 LValue lvalue, bool capturedByInit) {
996 SourceLocRAIIObject loc{*this, init->getSourceRange()};
997 if (capturedByInit) {
998 cgm.errorNYI(init->getSourceRange(), "emitExprAsInit: captured by init");
999 return;
1000 }
1001
1002 QualType type = d->getType();
1003
1004 if (type->isReferenceType()) {
1005 RValue rvalue = emitReferenceBindingToExpr(init);
1006 if (capturedByInit)
1007 cgm.errorNYI(init->getSourceRange(), "emitExprAsInit: captured by init");
1008 emitStoreThroughLValue(rvalue, lvalue);
1009 return;
1010 }
1012 case cir::TEK_Scalar:
1013 emitScalarInit(init, lvalue);
1014 return;
1015 case cir::TEK_Complex: {
1016 mlir::Value complex = emitComplexExpr(init);
1017 if (capturedByInit)
1018 cgm.errorNYI(init->getSourceRange(),
1019 "emitExprAsInit: complex type captured by init");
1020 mlir::Location loc = getLoc(init->getExprLoc());
1021 emitStoreOfComplex(loc, complex, lvalue,
1022 /*isInit*/ true);
1023 return;
1024 }
1025 case cir::TEK_Aggregate:
1026 // The overlap flag here should be calculated.
1028 emitAggExpr(init,
1032 return;
1033 }
1034 llvm_unreachable("bad evaluation kind");
1035}
1036
1037void CIRGenFunction::emitDecl(const Decl &d, bool evaluateConditionDecl) {
1038 switch (d.getKind()) {
1039 case Decl::BuiltinTemplate:
1040 case Decl::TranslationUnit:
1041 case Decl::ExternCContext:
1042 case Decl::Namespace:
1043 case Decl::UnresolvedUsingTypename:
1044 case Decl::ClassTemplateSpecialization:
1045 case Decl::ClassTemplatePartialSpecialization:
1046 case Decl::VarTemplateSpecialization:
1047 case Decl::VarTemplatePartialSpecialization:
1048 case Decl::TemplateTypeParm:
1049 case Decl::UnresolvedUsingValue:
1050 case Decl::NonTypeTemplateParm:
1051 case Decl::CXXDeductionGuide:
1052 case Decl::CXXMethod:
1053 case Decl::CXXConstructor:
1054 case Decl::CXXDestructor:
1055 case Decl::CXXConversion:
1056 case Decl::Field:
1057 case Decl::MSProperty:
1058 case Decl::IndirectField:
1059 case Decl::ObjCIvar:
1060 case Decl::ObjCAtDefsField:
1061 case Decl::ParmVar:
1062 case Decl::ImplicitParam:
1063 case Decl::ClassTemplate:
1064 case Decl::VarTemplate:
1065 case Decl::FunctionTemplate:
1066 case Decl::TypeAliasTemplate:
1067 case Decl::TemplateTemplateParm:
1068 case Decl::ObjCMethod:
1069 case Decl::ObjCCategory:
1070 case Decl::ObjCProtocol:
1071 case Decl::ObjCInterface:
1072 case Decl::ObjCCategoryImpl:
1073 case Decl::ObjCImplementation:
1074 case Decl::ObjCProperty:
1075 case Decl::ObjCCompatibleAlias:
1076 case Decl::PragmaComment:
1077 case Decl::PragmaDetectMismatch:
1078 case Decl::AccessSpec:
1079 case Decl::LinkageSpec:
1080 case Decl::Export:
1081 case Decl::ObjCPropertyImpl:
1082 case Decl::FileScopeAsm:
1083 case Decl::Friend:
1084 case Decl::FriendTemplate:
1085 case Decl::Block:
1086 case Decl::OutlinedFunction:
1087 case Decl::Captured:
1088 case Decl::UsingShadow:
1089 case Decl::ConstructorUsingShadow:
1090 case Decl::ObjCTypeParam:
1091 case Decl::Binding:
1092 case Decl::UnresolvedUsingIfExists:
1093 case Decl::HLSLBuffer:
1094 case Decl::HLSLRootSignature:
1095 llvm_unreachable("Declaration should not be in declstmts!");
1096
1097 case Decl::Function: // void X();
1098 case Decl::EnumConstant: // enum ? { X = ? }
1099 case Decl::ExplicitInstantiation:
1100 case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
1101 case Decl::Label: // __label__ x;
1102 case Decl::Import:
1103 case Decl::MSGuid: // __declspec(uuid("..."))
1104 case Decl::TemplateParamObject:
1105 case Decl::Empty:
1106 case Decl::Concept:
1107 case Decl::LifetimeExtendedTemporary:
1108 case Decl::RequiresExprBody:
1109 case Decl::UnnamedGlobalConstant:
1110 // None of these decls require codegen support.
1111 return;
1112
1113 case Decl::Enum: // enum X;
1114 case Decl::Record: // struct/union/class X;
1115 case Decl::CXXRecord: // struct/union/class X; [C++]
1116 case Decl::NamespaceAlias:
1117 case Decl::Using: // using X; [C++]
1118 case Decl::UsingEnum: // using enum X; [C++]
1119 case Decl::UsingDirective: // using namespace X; [C++]
1121 return;
1122 case Decl::Var:
1123 case Decl::Decomposition: {
1124 const VarDecl &vd = cast<VarDecl>(d);
1125 assert(vd.isLocalVarDecl() &&
1126 "Should not see file-scope variables inside a function!");
1127 emitVarDecl(vd);
1128 if (evaluateConditionDecl)
1130 return;
1131 }
1132 case Decl::OpenACCDeclare:
1134 return;
1135 case Decl::OpenACCRoutine:
1137 return;
1138 case Decl::OMPThreadPrivate:
1140 return;
1141 case Decl::OMPGroupPrivate:
1143 return;
1144 case Decl::OMPAllocate:
1146 return;
1147 case Decl::OMPCapturedExpr:
1149 return;
1150 case Decl::OMPRequires:
1152 return;
1153 case Decl::OMPDeclareMapper:
1155 return;
1156 case Decl::OMPDeclareReduction:
1158 return;
1159 case Decl::Typedef: // typedef int X;
1160 case Decl::TypeAlias: { // using X = int; [C++0x]
1161 QualType ty = cast<TypedefNameDecl>(d).getUnderlyingType();
1163 if (ty->isVariablyModifiedType())
1165 return;
1166 }
1167 case Decl::ImplicitConceptSpecialization:
1168 case Decl::TopLevelStmt:
1169 case Decl::UsingPack:
1170 case Decl::CXXExpansionStmt:
1171 cgm.errorNYI(d.getSourceRange(),
1172 std::string("emitDecl: unhandled decl type: ") +
1173 d.getDeclKindName());
1174 }
1175}
1176
1178 SourceLocation loc) {
1179 if (!sanOpts.has(SanitizerKind::NullabilityAssign))
1180 return;
1181
1183}
1184
1185namespace {
1186struct DestroyObject final : EHScopeStack::Cleanup {
1187 DestroyObject(Address addr, QualType type,
1188 CIRGenFunction::Destroyer *destroyer)
1189 : addr(addr), type(type), destroyer(destroyer) {
1191 }
1192
1193 Address addr;
1194 QualType type;
1195 CIRGenFunction::Destroyer *destroyer;
1196
1197 void emit(CIRGenFunction &cgf, Flags flags) override {
1199 cgf.emitDestroy(addr, type, destroyer);
1200 }
1201};
1202
1203template <class Derived> struct DestroyNRVOVariable : EHScopeStack::Cleanup {
1204 DestroyNRVOVariable(Address addr, QualType type, mlir::Value nrvoFlag)
1205 : nrvoFlag(nrvoFlag), addr(addr), ty(type) {}
1206
1207 mlir::Value nrvoFlag;
1208 Address addr;
1209 QualType ty;
1210
1211 void emit(CIRGenFunction &cgf, Flags flags) override {
1212 // Along the exceptions path we always execute the dtor.
1213 bool nrvo = flags.isForNormalCleanup() && nrvoFlag;
1214
1215 CIRGenBuilderTy &builder = cgf.getBuilder();
1216 mlir::OpBuilder::InsertionGuard guard(builder);
1217 if (nrvo) {
1218 // If we exited via NRVO, we skip the destructor call.
1219 mlir::Location loc = addr.getPointer().getLoc();
1220 mlir::Value didNRVO = builder.createFlagLoad(loc, nrvoFlag);
1221 mlir::Value notNRVO = builder.createNot(didNRVO);
1222 cir::IfOp::create(builder, loc, notNRVO, /*withElseRegion=*/false,
1223 [&](mlir::OpBuilder &b, mlir::Location) {
1224 static_cast<Derived *>(this)->emitDestructorCall(cgf);
1225 builder.createYield(loc);
1226 });
1227 } else {
1228 static_cast<Derived *>(this)->emitDestructorCall(cgf);
1229 }
1230 }
1231
1232 virtual ~DestroyNRVOVariable() = default;
1233};
1234
1235struct DestroyNRVOVariableCXX final
1236 : DestroyNRVOVariable<DestroyNRVOVariableCXX> {
1237 DestroyNRVOVariableCXX(Address addr, QualType type,
1238 const CXXDestructorDecl *dtor, mlir::Value nrvoFlag)
1239 : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, type, nrvoFlag),
1240 dtor(dtor) {}
1241
1242 const CXXDestructorDecl *dtor;
1243
1244 void emitDestructorCall(CIRGenFunction &cgf) {
1246 /*forVirtualBase=*/false,
1247 /*delegating=*/false, addr, ty);
1248 }
1249};
1250
1251struct CallStackRestore final : EHScopeStack::Cleanup {
1252 Address stack;
1253 CallStackRestore(Address stack) : stack(stack) {}
1254 bool isRedundantBeforeReturn() override { return true; }
1255 void emit(CIRGenFunction &cgf, Flags flags) override {
1256 mlir::Location loc = stack.getPointer().getLoc();
1257 mlir::Value v = cgf.getBuilder().createLoad(loc, stack);
1258 cgf.getBuilder().createStackRestore(loc, v);
1259 }
1260};
1261
1262/// A cleanup which performs a partial array destroy where the end pointer is
1263/// irregularly determined and must be loaded from a local.
1264struct IrregularPartialArrayDestroy final : EHScopeStack::Cleanup {
1265 mlir::Value arrayBegin;
1266 Address arrayEndPointer;
1267 QualType elementType;
1268 CharUnits elementAlign;
1269 CIRGenFunction::Destroyer *destroyer;
1270
1271 IrregularPartialArrayDestroy(mlir::Value arrayBegin, Address arrayEndPointer,
1272 QualType elementType, CharUnits elementAlign,
1273 CIRGenFunction::Destroyer *destroyer)
1274 : arrayBegin(arrayBegin), arrayEndPointer(arrayEndPointer),
1275 elementType(elementType), elementAlign(elementAlign),
1276 destroyer(destroyer) {}
1277
1278 void emit(CIRGenFunction &cgf, Flags flags) override {
1279 CIRGenBuilderTy &builder = cgf.getBuilder();
1280 mlir::Location loc = arrayBegin.getLoc();
1281
1282 mlir::Value arrayEnd = builder.createLoad(loc, arrayEndPointer);
1283
1284 // baseElementType gets us the final 'element' type, which should be the
1285 // RecordType, looking through any multi-dimension arrays.
1286 QualType baseElementType = cgf.getContext().getBaseElementType(elementType);
1287
1288 mlir::Type cirElementType = cgf.convertTypeForMem(baseElementType);
1289 cir::PointerType ptrToElmType = builder.getPointerTo(
1290 cirElementType,
1291 mlir::cast<cir::PointerType>(arrayBegin.getType()).getAddrSpace());
1292
1293 mlir::Value begin = arrayBegin;
1294 if (baseElementType != elementType) {
1295 begin = builder.createPtrBitcast(begin, cirElementType);
1296 arrayEnd = builder.createPtrBitcast(arrayEnd, cirElementType);
1297 }
1298
1299 // The cleanup is destroying elements in reverse from arrayEnd back to
1300 // begin, but only if arrayEnd != begin (i.e. something was
1301 // constructed).
1302 mlir::Value ne =
1303 cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne, arrayEnd, begin);
1304 cir::IfOp::create(
1305 builder, loc, ne, /*withElseRegion=*/false,
1306 [&](mlir::OpBuilder &b, mlir::Location loc) {
1307 Address iterAddr = cgf.createTempAlloca(
1308 ptrToElmType, cgf.getPointerAlign(), loc, "__array_idx");
1309 builder.createStore(loc, arrayEnd, iterAddr);
1310 builder.createDoWhile(
1311 loc,
1312 /*condBuilder=*/
1313 [&](mlir::OpBuilder &b, mlir::Location loc) {
1314 mlir::Value cur = builder.createLoad(loc, iterAddr);
1315 mlir::Value cmp = cir::CmpOp::create(
1316 builder, loc, cir::CmpOpKind::ne, cur, begin);
1317 builder.createCondition(cmp);
1318 },
1319 /*bodyBuilder=*/
1320 [&](mlir::OpBuilder &b, mlir::Location loc) {
1321 mlir::Value cur = builder.createLoad(loc, iterAddr);
1322 cir::ConstantOp negOne = builder.getConstInt(
1323 loc, mlir::cast<cir::IntType>(cgf.ptrDiffTy), -1);
1324 mlir::Value prev = cir::PtrStrideOp::create(
1325 builder, loc, ptrToElmType, cur, negOne);
1326 builder.createStore(loc, prev, iterAddr);
1327 Address elemAddr = Address(prev, cirElementType, elementAlign);
1328 destroyer(cgf, elemAddr, baseElementType);
1329 builder.createYield(loc);
1330 });
1331 builder.createYield(loc);
1332 });
1333 }
1334};
1335} // namespace
1336
1337/// Push an EH cleanup to destroy already-constructed elements of the given
1338/// array. The cleanup is deactivated when the enclosing
1339/// CleanupDeactivationScope exits.
1340///
1341/// \param elementType - the immediate element type of the array;
1342/// possibly still an array type
1344 Address arrayEndPointer,
1345 QualType elementType,
1346 CharUnits elementAlign,
1347 Destroyer *destroyer) {
1349 EHCleanup, arrayBegin, arrayEndPointer, elementType, elementAlign,
1350 destroyer);
1351}
1352
1353/// pushEHDestroyIfNeeded - Push the standard destructor for the given type as
1354/// an EH-only cleanup. If EH cleanup is not needed, just return.
1356 Address addr, QualType type) {
1357 if (!needsEHCleanup(dtorKind))
1358 return;
1359
1361 pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind));
1362}
1363
1364/// Push the standard destructor for the given type as
1365/// at least a normal cleanup.
1367 Address addr, QualType type) {
1368 assert(dtorKind && "cannot push destructor for trivial type");
1369
1370 CleanupKind cleanupKind = getCleanupKind(dtorKind);
1371 pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind));
1372}
1373
1375 QualType type, Destroyer *destroyer) {
1376 pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type, destroyer);
1377}
1378
1381 assert(dtorKind && "cannot push destructor for trivial type");
1382
1383 CleanupKind cleanupKind = getCleanupKind(dtorKind);
1385 cleanupKind, addr, type, getDestroyer(dtorKind), cleanupKind & EHCleanup);
1386}
1387
1389 CleanupKind cleanupKind, Address addr, QualType type, Destroyer *destroyer,
1390 bool useEHCleanupForArray) {
1393 destroyer);
1394}
1395
1397 CleanupKind cleanupKind, Address addr, QualType type, Destroyer *destroyer,
1398 bool useEHCleanupForArray, const ValueDecl *extendingDecl) {
1400 cgm.errorNYI("conditional lifetime-extended destroy");
1401 return;
1402 }
1403
1404 // Add the cleanup to the EHStack. After the full-expr, this would be
1405 // deactivated before being popped from the stack.
1406 pushDestroyAndDeferDeactivation(cleanupKind, addr, type, destroyer,
1407 useEHCleanupForArray);
1408
1410
1411 pushCleanupAfterFullExpr(cleanupKind, addr, type, destroyer);
1412
1413 // A temporary that a loop condition variable extends is destroyed with the
1414 // variable at the end of each iteration, in the loop's cleanup region.
1416 extendingDecl == lifetimeExtensionCapture->extendingDecl) {
1418 entry.activeFlag = lifetimeExtensionCapture->activeFlag;
1419 entry.capturedBy = lifetimeExtensionCapture->cleanups;
1420 }
1421}
1422
1424 const PendingCleanupEntry &entry) {
1425 std::optional<CapturedCleanups::CaptureScope> capture;
1426 if (entry.capturedBy)
1427 capture.emplace(*entry.capturedBy);
1428 ehStack.pushCleanup<DestroyObject>(entry.kind, entry.addr, entry.type,
1429 entry.destroyer);
1430
1431 if (entry.activeFlag.isValid()) {
1432 EHCleanupScope &scope = cast<EHCleanupScope>(*ehStack.begin());
1433 scope.setActiveFlag(entry.activeFlag);
1435 scope.setTestFlagInEHCleanup(scope.isEHCleanup());
1436 }
1437}
1438
1439/// Destroys all the elements of the given array, beginning from last to first.
1440///
1441/// \param begin - a type* denoting the first element of the array
1442/// \param numElements - the number of elements in the array
1443/// \param elementType - the element type of the array
1444/// \param destroyer - the function to call to destroy elements
1446 mlir::Value numElements,
1447 QualType elementType,
1448 CharUnits elementAlign,
1449 Destroyer *destroyer) {
1450 assert(!elementType->isArrayType());
1451
1452 // Differently from LLVM traditional codegen, use a higher level
1453 // representation instead of lowering directly to a loop.
1454 mlir::Type cirElementType = convertTypeForMem(elementType);
1455 cir::PointerType ptrToElmType = builder.getPointerTo(
1456 cirElementType,
1457 mlir::cast<cir::PointerType>(begin.getType()).getAddrSpace());
1458
1459 auto regionBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1460 mlir::BlockArgument arg =
1461 b.getInsertionBlock()->addArgument(ptrToElmType, loc);
1462 Address curAddr = Address(arg, cirElementType, elementAlign);
1464
1465 // Perform the actual destruction there.
1466 destroyer(*this, curAddr, elementType);
1467
1468 cir::YieldOp::create(b, loc);
1469 };
1470
1471 // For a constant array size, use the static form of ArrayDtor.
1472 if (auto constantCount = numElements.getDefiningOp<cir::ConstantOp>()) {
1473 uint64_t size = 0;
1474 if (auto constIntAttr = constantCount.getValueAttr<cir::IntAttr>())
1475 size = constIntAttr.getUInt();
1476 auto arrayTy = cir::ArrayType::get(cirElementType, size);
1477 mlir::Value arrayOp = builder.createPtrBitcast(begin, arrayTy);
1478 cir::ArrayDtor::create(builder, getLoc(*currSrcLoc), arrayOp,
1479 regionBuilder);
1480 return;
1481 }
1482
1483 // For a dynamic array size (VLA), use the dynamic form of ArrayDtor.
1484 mlir::Value elemBegin = builder.createPtrBitcast(begin, cirElementType);
1485 cir::ArrayDtor::create(builder, getLoc(*currSrcLoc), elemBegin, numElements,
1486 regionBuilder);
1487}
1488
1489/// Immediately perform the destruction of the given object.
1490///
1491/// \param addr - the address of the object; a type*
1492/// \param type - the type of the object; if an array type, all
1493/// objects are destroyed in reverse order
1494/// \param destroyer - the function to call to destroy individual
1495/// elements
1497 Destroyer *destroyer) {
1499 if (!arrayType)
1500 return destroyer(*this, addr, type);
1501
1502 mlir::Value length = emitArrayLength(arrayType, type, addr);
1503
1504 CharUnits elementAlign = addr.getAlignment().alignmentOfArrayElement(
1505 getContext().getTypeSizeInChars(type));
1506
1507 // If the array length is constant, we can check for zero at compile time.
1508 auto constantCount = length.getDefiningOp<cir::ConstantOp>();
1509 if (constantCount) {
1510 auto constIntAttr = mlir::dyn_cast<cir::IntAttr>(constantCount.getValue());
1511 if (constIntAttr && constIntAttr.getUInt() == 0)
1512 return;
1513 }
1514
1515 mlir::Value begin = addr.getPointer();
1517 emitArrayDestroy(begin, length, type, elementAlign, destroyer);
1518
1519 // If the array destroy didn't use the length op, we can erase it.
1520 if (constantCount && constantCount.use_empty())
1521 constantCount.erase();
1522}
1523
1526 switch (kind) {
1527 case QualType::DK_none:
1528 llvm_unreachable("no destroyer for trivial dtor");
1530 return destroyCXXObject;
1534 cgm.errorNYI("getDestroyer: other destruction kind");
1535 return nullptr;
1536 }
1537 llvm_unreachable("Unknown DestructionKind");
1538}
1539
1541 ehStack.pushCleanup<CallStackRestore>(kind, spMem);
1542}
1543
1544/// Enter a destroy cleanup for the given local variable.
1546 const CIRGenFunction::AutoVarEmission &emission,
1547 QualType::DestructionKind dtorKind) {
1548 assert(dtorKind != QualType::DK_none);
1549
1550 // Note that for __block variables, we want to destroy the
1551 // original stack object, not the possibly forwarded object.
1552 Address addr = emission.getObjectAddress(*this);
1553
1554 const VarDecl *var = emission.variable;
1555 QualType type = var->getType();
1556
1557 CleanupKind cleanupKind = NormalAndEHCleanup;
1558 CIRGenFunction::Destroyer *destroyer = nullptr;
1559
1560 switch (dtorKind) {
1561 case QualType::DK_none:
1562 llvm_unreachable("no cleanup for trivially-destructible variable");
1563
1565 // If there's an NRVO flag on the emission, we need a different
1566 // cleanup.
1567 if (emission.nrvoFlag) {
1568 assert(!type->isArrayType());
1569 CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
1570 ehStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, type, dtor,
1571 emission.nrvoFlag);
1572 return;
1573 }
1574 // Otherwise, this is handled below.
1575 break;
1576
1580 cgm.errorNYI(var->getSourceRange(),
1581 "emitAutoVarTypeCleanup: other dtor kind");
1582 return;
1583 }
1584
1585 // If we haven't chosen a more specific destroyer, use the default.
1586 if (!destroyer)
1587 destroyer = getDestroyer(dtorKind);
1588
1590 ehStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer);
1591}
1592
1594 if (auto *dd = dyn_cast_if_present<DecompositionDecl>(vd)) {
1595 for (auto *b : dd->flat_bindings())
1596 if (auto *hd = b->getHoldingVar())
1597 emitVarDecl(*hd);
1598 }
1599}
1600
1601bool CIRGenFunction::emitLifetimeStartOp(mlir::Location loc, mlir::Value addr) {
1602 if (!shouldEmitLifetimeMarkers)
1603 return false;
1604
1605 assert(mlir::cast<cir::PointerType>(addr.getType()).getAddrSpace() ==
1607 "Pointer should be in alloca address space");
1608
1609 cir::LifetimeStartOp::create(builder, loc, addr);
1610 return true;
1611}
1612
1613void CIRGenFunction::emitLifetimeEndOp(mlir::Location loc, mlir::Value addr) {
1614 if (!shouldEmitLifetimeMarkers)
1615 return;
1616
1617 assert(mlir::cast<cir::PointerType>(addr.getType()).getAddrSpace() ==
1619 "Pointer should be in alloca address space");
1620
1621 cir::LifetimeEndOp::create(builder, loc, addr);
1622}
static bool emit(llvm::SmallVectorImpl< std::byte > &Code, const T &Val)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
static bool isRedundantBeforeReturn(mlir::Region &cleanupRegion)
static void emitStoresForConstant(CIRGenModule &cgm, const VarDecl &d, Address addr, bool isVolatile, CIRGenBuilderTy &builder, mlir::TypedAttr constant)
static std::string getStaticDeclName(CIRGenModule &cgm, const VarDecl &d)
This file defines OpenACC nodes for declarative directives.
static constexpr bool needsDtor()
Defines the clang::Expr interface and subclasses for C++ expressions.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
static CharSourceRange getRange(const CharSourceRange &EditRange, const SourceManager &SM, const LangOptions &LangOpts, bool IncludeMacroExpansion)
cir::ConditionOp createCondition(mlir::Value condition)
Create a loop condition.
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
cir::PointerType getPointerTo(mlir::Type ty)
cir::DoWhileOp createDoWhile(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder)
Create a do-while operation.
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
mlir::Value createNot(mlir::Location loc, mlir::Value value)
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
cir::LoadOp createFlagLoad(mlir::Location loc, mlir::Value addr)
Emit a load from an boolean flag variable.
llvm::TypeSize getTypeAllocSize(mlir::Type ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
const LangOptions & getLangOpts() const
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3820
mlir::Value getPointer() const
Definition Address.h:98
mlir::Type getElementType() const
Definition Address.h:125
static Address invalid()
Definition Address.h:76
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
clang::CharUnits getAlignment() const
Definition Address.h:138
bool isValid() const
Definition Address.h:77
cir::AllocaOp getUnderlyingAllocaOp() const
Return the underlying alloca for this address, if any.
Definition Address.h:152
mlir::Operation * getDefiningOp() const
Get the operation which defines this address.
Definition Address.h:141
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed)
cir::LoadOp createLoad(mlir::Location loc, Address addr, bool isVolatile=false, bool isNontemporal=false)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, bool isNontemporal=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
cir::StackRestoreOp createStackRestore(mlir::Location loc, mlir::Value v)
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
An RAII class that suppresses cir.cleanup.scope creation for cleanups pushed onto the EH stack while ...
Captures cleanups for variables whose lifetime ends in a region owned by an enclosing op rather than ...
void emitOpenACCRoutine(const OpenACCRoutineDecl &d)
mlir::Type convertType(clang::QualType t)
cir::GlobalOp addInitializerToStaticVarDecl(const VarDecl &d, cir::GlobalOp gv, cir::GetGlobalOp gvAddr)
Add the initializer for 'd' to the global variable that has already been created for it.
static cir::TypeEvaluationKind getEvaluationKind(clang::QualType type)
Return the cir::TypeEvaluationKind of QualType type.
AutoVarEmission emitAutoVarAlloca(const clang::VarDecl &d, mlir::OpBuilder::InsertPoint ip={})
void emitAutoVarTypeCleanup(const AutoVarEmission &emission, clang::QualType::DestructionKind dtorKind)
Enter a destroy cleanup for the given local variable.
llvm::SmallVector< PendingCleanupEntry > lifetimeExtendedCleanupStack
ConditionalEvaluation * hiddenOutermostConditional
void emitVariablyModifiedType(QualType ty)
const clang::LangOptions & getLangOpts() const
cir::AllocaOp createTempAlloca(mlir::Type ty, mlir::Location loc, const Twine &name="tmp", mlir::Value arraySize=nullptr, bool insertIntoFnEntryBlock=false)
This creates an alloca and inserts it into the entry block if ArraySize is nullptr,...
mlir::Block * getCurFunctionEntryBlock()
void emitOMPRequiresDecl(const OMPRequiresDecl &d)
VlaSizePair getVLASize(const VariableArrayType *type)
Returns an MLIR::Value+QualType pair that corresponds to the size, in non-variably-sized elements,...
void emitStaticVarDecl(const VarDecl &d, cir::GlobalLinkageKind linkage)
mlir::Value emitComplexExpr(const Expr *e)
Emit the computation of the specified expression of complex type, returning the result.
bool isTrivialInitializer(const Expr *init)
Determine whether the given initializer is trivial in the sense that it requires no code to be genera...
void emitOpenACCDeclare(const OpenACCDeclareDecl &d)
void pushIrregularPartialArrayCleanup(mlir::Value arrayBegin, Address arrayEndPointer, QualType elementType, CharUnits elementAlign, Destroyer *destroyer)
Push an EH cleanup to destroy already-constructed elements of the given array.
void emitLifetimeEndOp(mlir::Location loc, mlir::Value addr)
void pushCleanupAndDeferDeactivation(CleanupKind kind, As... a)
Push a cleanup and record it for deferred deactivation.
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
void emitVarAnnotations(const clang::VarDecl *d, mlir::Value addr)
Annotate the storage of d at addr.
void emitOMPDeclareReduction(const OMPDeclareReductionDecl &d)
void emitExprAsInit(const clang::Expr *init, const clang::ValueDecl *d, LValue lvalue, bool capturedByInit=false)
Emit an expression as an initializer for an object (variable, field, etc.) at the given location.
void emitCXXGuardedInit(const VarDecl &varDecl, cir::GlobalOp globalOp, bool performInit)
Emit a guarded initializer for a static local variable.
void emitLoopConditionVariable(const clang::VarDecl &d, CapturedCleanups &condCleanup, llvm::ArrayRef< QualType > extendedTemporaries)
Emit a loop's condition-variable declaration.
mlir::Value emitArrayLength(const clang::ArrayType *arrayType, QualType &baseType, Address &addr)
Computes the length of an array in elements, as well as the base element type and a properly-typed fi...
mlir::Value performAddrSpaceCast(mlir::Value v, mlir::Type destTy) const
RValue emitReferenceBindingToExpr(const Expr *e)
Emits a reference binding to the passed in expression.
void pushDestroyAndDeferDeactivation(QualType::DestructionKind dtorKind, Address addr, QualType type)
void emitScalarInit(const clang::Expr *init, LValue lvalue, bool capturedByInit=false)
CleanupKind getCleanupKind(QualType::DestructionKind kind)
void emitOMPAllocateDecl(const OMPAllocateDecl &d)
void emitOMPDeclareMapper(const OMPDeclareMapperDecl &d)
EHScopeStack ehStack
Tracks function scope overall cleanup handling.
clang::SanitizerSet sanOpts
Sanitizers enabled for this function.
mlir::Type convertTypeForMem(QualType t)
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
Push the standard destructor for the given type as at least a normal cleanup.
void emitVarDecl(const clang::VarDecl &d)
This method handles emission of any variable declaration inside a function, including static vars etc...
Address returnValue
The temporary alloca to hold the return value.
void emitArrayDestroy(mlir::Value begin, mlir::Value numElements, QualType elementType, CharUnits elementAlign, Destroyer *destroyer)
Destroys all the elements of the given array, beginning from last to first.
void emitAutoVarDeclWithCapturedCleanups(const clang::VarDecl &d, CapturedCleanups &cleanups, bool guardDestruction=false, llvm::ArrayRef< QualType > extendedTemporaries={})
Emit d with captured cleanups.
void initFullExprCleanupWithFlag(Address activeFlag)
void emitStoreOfComplex(mlir::Location loc, mlir::Value v, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
static bool destructorMayThrow(QualType type)
Whether destroying an object of type type, or its elements if it is an array, may throw an exception.
bool haveInsertPoint() const
True if an insertion point is defined.
void emitAutoVarInit(const AutoVarEmission &emission)
Emit the initializer for an allocated variable.
void maybeEmitDeferredVarDeclInit(const VarDecl *vd)
const LifetimeExtensionCapture * lifetimeExtensionCapture
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
void pushStackRestore(CleanupKind kind, Address spMem)
void emitAutoVarDecl(const clang::VarDecl &d)
Emit code and set up symbol table for a variable declaration with auto, register, or no storage class...
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray, const clang::ValueDecl *extendingDecl)
void popCleanupBlocks(EHScopeStack::stable_iterator oldCleanupStackDepth, ArrayRef< mlir::Value * > valuesToReload={})
Takes the old cleanup stack size and emits the cleanup blocks that have been added.
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind.
CIRGenBuilderTy & getBuilder()
bool didCallStackSave
Whether a cir.stacksave operation has been added.
void emitDecl(const clang::Decl &d, bool evaluateConditionDecl=false)
void emitDestroy(Address addr, QualType type, Destroyer *destroyer)
Immediately perform the destruction of the given object.
void pushPendingCleanupToEHStack(const PendingCleanupEntry &entry)
Promote a single pending cleanup entry onto the EH scope stack.
llvm::DenseMap< const VarDecl *, mlir::Value > nrvoFlags
A mapping from NRVO variables to the flags used to indicate when the NRVO has been applied to this va...
mlir::MLIRContext & getMLIRContext()
Destroyer * getDestroyer(clang::QualType::DestructionKind kind)
void Destroyer(CIRGenFunction &cgf, Address addr, QualType ty)
std::optional< SourceRange > currSrcLoc
Use to track source locations across nested visitor traversals.
DeclMapTy localDeclMap
This keeps track of the CIR allocas or globals for local C declarations.
void pushEHDestroyIfNeeded(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushEHDestroyIfNeeded - Push the standard destructor for the given type as an EH-only cleanup.
void emitOMPThreadPrivateDecl(const OMPThreadPrivateDecl &d)
void emitOMPGroupPrivateDecl(const OMPGroupPrivateDecl &d)
bool emitLifetimeStartOp(mlir::Location loc, mlir::Value addr)
RValue emitCall(const CIRGenFunctionInfo &funcInfo, const CIRGenCallee &callee, ReturnValueSlot returnValue, const CallArgList &args, cir::CIRCallOpInterface *callOp, bool isMustTail, SourceRange clangLoc)
LValue makeAddrLValue(Address addr, QualType ty, AlignmentSource source=AlignmentSource::Type)
void emitCXXDestructorCall(const CXXDestructorDecl *dd, CXXDtorType type, bool forVirtualBase, bool delegating, Address thisAddr, QualType thisTy)
void pushFullExprCleanup(CleanupKind kind, As... a)
Push a cleanup to be run at the end of the current full-expression.
clang::ASTContext & getContext() const
void setAddrOfLocalVar(const clang::VarDecl *vd, Address addr)
Set the address of a local variable.
void pushCleanupAfterFullExpr(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer)
Queue a cleanup to be pushed after finishing the current full-expression.
void emitNullabilityCheck(LValue lhs, mlir::Value rhs, clang::SourceLocation loc)
Given an assignment *lhs = rhs, emit a test that checks if rhs is nonnull, if 1LHS is marked _Nonnull...
void emitOMPCapturedExpr(const OMPCapturedExprDecl &d)
void emitStoreThroughLValue(RValue src, LValue dst, bool isInit=false)
Store the specified rvalue into the specified lvalue, where both are guaranteed to the have the same ...
static llvm::SmallVector< QualType, 2 > getLifetimeExtendedTemporariesWithDestructor(const clang::VarDecl &d)
The types of the temporaries with a destructor that the initializer of the variable d,...
Address createTempAllocaWithoutCast(mlir::Type ty, CharUnits align, mlir::Location loc, const Twine &name="tmp", mlir::Value arraySize=nullptr, mlir::OpBuilder::InsertPoint ip={})
This creates a alloca and inserts it into the entry block of the current region.
void emitAggExpr(const clang::Expr *e, AggValueSlot slot)
void emitAutoVarCleanups(const AutoVarEmission &emission)
This class organizes the cross-function state that is used while generating CIR code.
llvm::StringRef getMangledName(clang::GlobalDecl gd)
cir::GlobalOp getOrCreateStaticVarDecl(const VarDecl &d, cir::GlobalLinkageKind linkage)
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
clang::ASTContext & getASTContext() const
void insertGlobalSymbol(mlir::Operation *op)
llvm::DenseMap< const VarDecl *, cir::GlobalOp > initializerConstants
void setGVProperties(mlir::Operation *op, const NamedDecl *d) const
Set visibility, dllimport/dllexport and dso_local.
static mlir::SymbolTable::Visibility getMLIRVisibilityFromCIRLinkage(cir::GlobalLinkageKind GLK)
mlir::TypedAttr emitNullConstantAttr(QualType t)
LangAS getGlobalVarAddressSpace(const VarDecl *decl)
Return the AST address space of the underlying global variable for D, as determined by its declaratio...
void setTLSMode(mlir::Operation *op, const VarDecl &d, bool isExtendingDecl=false)
Set TLS mode for the given operation based on the given variable declaration.
const clang::LangOptions & getLangOpts() const
void setStaticLocalDeclAddress(const VarDecl *d, cir::GlobalOp c)
mlir::Location getLoc(clang::SourceLocation cLoc)
Helpers to convert the presumed location of Clang's SourceLocation to an MLIR Location.
mlir::ModuleOp getModule() const
mlir::MLIRContext & getMLIRContext()
mlir::Operation * getAddrOfGlobal(clang::GlobalDecl gd, ForDefinition_t isForDefinition=NotForDefinition)
cir::GlobalOp getStaticLocalDeclAddress(const VarDecl *d)
mlir::ptr::MemorySpaceAttrInterface getGlobalCIRAddressSpace(LangAS as)
Return the CIR address space attribute for a global in as, honoring the target address space map wher...
Address createUnnamedGlobalFrom(const VarDecl &d, mlir::Attribute constAttr, CharUnits align)
mlir::Type convertTypeForMem(clang::QualType, bool forBitField=false)
Convert type T into an mlir::Type.
void add(RValue rvalue, clang::QualType type)
Definition CIRGenCall.h:241
mlir::Attribute tryEmitForInitializer(const VarDecl &d)
Try to emit the initializer of the given declaration as an abstract constant.
mlir::Attribute tryEmitAbstractForInitializer(const VarDecl &d)
Try to emit the initializer of the given declaration as an abstract constant.
A cleanup scope which generates the cleanup blocks lazily.
void setTestFlagInEHCleanup(bool value)
void setTestFlagInNormalCleanup(bool value)
void setActiveFlag(Address var)
Information for lazily generating a cleanup.
A saved depth on the scope stack.
Address getAddress() const
This trivial value class is used to represent the result of an expression that is evaluated.
Definition CIRGenValue.h:33
static RValue get(mlir::Value v)
Definition CIRGenValue.h:83
Represents a call to a C++ constructor.
Definition ExprCXX.h:1553
Represents a C++ constructor within a class.
Definition DeclCXX.h:2645
Represents a C++ destructor within a class.
Definition DeclCXX.h:2924
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition DeclCXX.h:1382
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition DeclCXX.cpp:2129
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
llvm::Align getAsAlign() const
Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit bytes.
Definition CharUnits.h:159
static CharUnits One()
Construct a CharUnits quantity of one.
Definition CharUnits.h:55
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition CharUnits.h:184
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:58
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1467
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
Definition DeclBase.cpp:344
T * getAttr() const
Definition DeclBase.h:582
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks,...
SourceLocation getLocation() const
Definition DeclBase.h:448
const char * getDeclKindName() const
Definition DeclBase.cpp:169
DeclContext * getDeclContext()
Definition DeclBase.h:457
bool hasAttr() const
Definition DeclBase.h:586
Kind getKind() const
Definition DeclBase.h:451
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
Definition DeclBase.h:436
This represents one expression.
Definition Expr.h:114
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition Expr.cpp:85
bool isConstantInitializer(ASTContext &Ctx, bool ForRef=false, const Expr **Culprit=nullptr) const
Returns true if this expression can be emitted to IR as a constant, and thus can be used as a constan...
Definition Expr.cpp:3386
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:146
Represents a function declaration or definition.
Definition Decl.h:2082
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5548
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
const Decl * getDecl() const
Definition GlobalDecl.h:115
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4999
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4991
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Definition ExprCXX.h:5024
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:303
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition Decl.h:319
bool isExternallyVisible() const
Definition Decl.h:435
A (possibly-)qualified type.
Definition TypeBase.h:938
@ PDIK_Struct
The type is a struct containing a field whose type is not PCK_Trivial.
Definition TypeBase.h:1494
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8704
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Definition TypeBase.h:1561
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition TypeBase.h:1037
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition Type.cpp:2921
Represents a struct/union/class.
Definition Decl.h:4506
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Encodes a location in the source.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
Definition Type.cpp:2652
bool isArrayType() const
Definition TypeBase.h:8914
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9498
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2881
bool isVectorType() const
Definition TypeBase.h:8954
bool isSamplerT() const
Definition TypeBase.h:9059
bool isRecordType() const
Definition TypeBase.h:8942
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:714
QualType getType() const
Definition Decl.h:725
Represents a variable declaration or definition.
Definition Decl.h:934
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1606
TLSKind getTLSKind() const
Definition Decl.cpp:2151
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2173
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
Definition Decl.cpp:2856
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1249
bool mightBeUsableInConstantExpressions(const ASTContext &C) const
Determine whether this variable's value might be usable in a constant expression, according to the re...
Definition Decl.cpp:2481
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO).
Definition Decl.h:1549
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition Decl.cpp:2845
const Expr * getInit() const
Definition Decl.h:1393
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition Decl.h:1240
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition Decl.h:1192
@ TLS_None
Not a TLS variable.
Definition Decl.h:954
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1276
StorageDuration getStorageDuration() const
Get the storage duration of this variable, per C++ [basic.stc].
Definition Decl.h:1252
bool isEscapingByref() const
Indicates the capture is a __block variable that is captured by a block that can potentially escape (...
Definition Decl.cpp:2705
mlir::ptr::MemorySpaceAttrInterface normalizeDefaultAddressSpace(mlir::ptr::MemorySpaceAttrInterface addrSpace)
Normalize LangAddressSpace::Default to null (empty attribute).
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
const internal::VariadicAllOfMatcher< Attr > attr
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
void info(bool Verbose, unsigned Level, const char *Fmt, Ts &&...Args)
Prints an indented note to stderr when Verbose is set.
Definition Utils.h:57
Top level wrappers for InstallAPI frontend operations.
@ Ctor_Base
Base object ctor.
Definition ABI.h:26
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
bool isUnresolvedExceptionSpec(ExceptionSpecificationType ESpecType)
@ SD_Automatic
Automatic storage duration (most local variables).
Definition Specifiers.h:343
@ Dtor_Base
Base object dtor.
Definition ABI.h:37
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
U cast(CodeGen::Address addr)
Definition Address.h:327
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)
float __ovld __cnfn length(float)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
CallLifetimeEnd(mlir::Value addr)
bool isRedundantBeforeReturn() override
void emit(CIRGenFunction &cgf, Flags flags) override
Emit the cleanup.
mlir::Value addr
static bool objCLifetime()
static bool addAutoInitAnnotation()
static bool addressSpace()
static bool emitNullabilityCheck()
static bool useEHCleanupForArray()
static bool vectorConstants()
static bool aggValueSlotMayOverlap()
static bool dtorCleanups()
static bool dataLayoutTypeAllocSize()
static bool opAllocaCaptureByInit()
static bool lifetimeMarkersBypass()
static bool opAllocaPreciseLifetime()
static bool generateDebugInfo()
bool isEscapingByRef
True if the variable is a __block variable that is captured by an escaping block.
Address addr
The address of the alloca for languages with explicit address space (e.g.
bool useLifetimeMarkers
True if lifetime op should be used.
bool isConstantAggregate
True if the variable is of aggregate type and has a constant initializer.
Address getObjectAddress(CIRGenFunction &cgf) const
Returns the address of the object within this declaration.
Set by emitAutoVarDeclWithCapturedCleanups while it emits the initializer of a variable,...
A cleanup whose destructor call is not emitted where the cleanup is registered.
CapturedCleanups * capturedBy
If set, the cleanup is captured by this owner instead of opening a cir.cleanup.scope when it is pushe...
clang::CharUnits getPointerAlign() const
cir::PointerType allocaInt8PtrTy
void* in alloca address space
mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const