18#include "mlir/IR/Location.h"
20#include "clang/AST/Attrs.inc"
30#include "llvm/Support/SaveAndRestore.h"
38struct CallCleanupFunction final : EHScopeStack::Cleanup {
39 const CIRGenFunctionInfo &fnInfo;
43 SourceRange attrRange;
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()))),
52 void emit(CIRGenFunction &cgf, Flags flags)
override {
53 assert(!fnInfo.arguments().empty());
54 QualType argTy = fnInfo.arguments()[0];
65 auto argPtrTy = mlir::cast<cir::PointerType>(cgf.
convertType(argTy));
72 cgf.
emitCall(fnInfo, callee, ReturnValueSlot(), args,
73 nullptr,
false, attrRange);
92 mlir::OpBuilder::InsertPoint ip) {
106 "emitAutoVarAlloca: decl escaping by reference");
130 getContext().getBaseElementType(ty)->isObjCObjectPointerType()) &&
144 (
cgm.getCodeGenOpts().MergeAllConstants && !nrvo &&
164 if (
const auto *cxxrd = dyn_cast<CXXRecordDecl>(rd);
165 (cxxrd && !cxxrd->hasTrivialDestructor()) ||
166 rd->isNonTrivialToPrimitiveDestroy()) {
171 cir::ConstantOp falseNVRO = builder.getFalse(loc);
175 assert(builder.getInsertionBlock());
176 builder.createStore(loc, falseNVRO, nrvoFlag);
188 nullptr,
nullptr, ip);
208 cgm.getDataLayout().getAlignment(defaultTy,
false));
211 mlir::Value v = builder.createStackSave(loc, defaultTy);
213 builder.createStore(loc, v, stack);
228 nullptr, builder.saveInsertionPoint());
236 emission.
addr = address;
259 if (constructor->isTrivial() && constructor->isDefaultConstructor() &&
260 !construct->requiresZeroInitialization())
269 mlir::TypedAttr constant) {
270 mlir::Type ty = constant.getType();
291 mlir::Location loc = builder.getUnknownLoc();
305 assert(emission.
variable &&
"emission was not valid!");
321 if (!init &&
type.isNonTrivialToPrimitiveDefaultInitialize() ==
324 "emitAutoVarInit: non-trivial to default initialize");
339 : (d.
getAttr<UninitializedAttr>()
343 auto initializeWhatIsTechnicallyUninitialized = [&](
Address addr) {
344 if (trivialAutoVarInit ==
352 initializeWhatIsTechnicallyUninitialized(addr);
356 mlir::Attribute constant;
365 if (constant && !mlir::isa<cir::ZeroAttr>(constant) &&
366 (trivialAutoVarInit !=
378 initializeWhatIsTechnicallyUninitialized(addr);
386 assert(addr.
isValid() &&
"Should have an address");
388 assert(allocaOp &&
"Address should come straight out of the alloca");
390 if (!allocaOp.use_empty())
398 auto typedConstant = mlir::dyn_cast<mlir::TypedAttr>(constant);
399 assert(typedConstant &&
"expected typed attribute");
403 assert(init &&
"expected initializer");
407 RValue::get(builder.getConstant(initLoc, typedConstant)), lv);
425 if (
const CleanupAttr *ca = d.
getAttr<CleanupAttr>()) {
428 cir::FuncOp f =
cgm.getAddrOfFunction(fd);
429 assert(f &&
"Could not find function!");
432 cgm.getTypes().arrangeFunctionDeclaration(fd);
452class LifetimeExtendedTemporaryFinder
459 explicit LifetimeExtendedTemporaryFinder(
const VarDecl &var) : var(var) {}
467 bool shouldVisitImplicitCode()
const {
return true; }
471 bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *m) {
477 SmallVector<const Expr *, 2> commaLHSs;
478 SmallVector<SubobjectAdjustment, 2> adjustments;
490 type->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
503 LifetimeExtendedTemporaryFinder finder(d);
504 finder.TraverseStmt(
const_cast<Expr *
>(init));
518 "emitLoopConditionVariable: OpenCL local address space");
524 if (
getLangOpts().Exceptions && !extendedTemporaries.empty() &&
527 (extendedTemporaries.size() > 1 &&
530 "loop condition variable cleanup with a potentially-throwing "
531 "destructor before another destructor");
536 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};
559 bool extendsTemporaries = !extendedTemporaries.empty();
562 if (needsCleanup || extendsTemporaries) {
565 builder.getBoolTy(),
CharUnits::One(), loc,
"cond.cleanup.isactive",
568 builder.createFlagStore(loc,
false, activeFlag.
getPointer());
581 assert((guardDestruction ||
ehStack.stable_begin() == initDepth) &&
582 "initializer left cleanups that cannot be captured");
589 if (!extendsTemporaries &&
ehStack.stable_begin() != initDepth) {
591 "variable with captured cleanups whose initializer leaves "
592 "uncaptured cleanups");
596 for (
auto it =
ehStack.begin(), ie =
ehStack.find(initDepth); it != ie; ++it)
598 "variable initializer left an uncaptured cleanup");
604 builder.createFlagStore(loc,
true, activeFlag.
getPointer());
631 cir::GlobalLinkageKind linkage =
cgm.getCIRLinkageVarDefinition(&d);
645 CIRGenFunction::VarDeclContext varDeclCtx{*
this, &d};
655 std::string contextName;
657 if (
auto *cd = dyn_cast<CapturedDecl>(dc))
659 if (
const auto *fd = dyn_cast<FunctionDecl>(dc))
663 "getStaticDeclName: block decl context for static var");
666 "getStaticDeclName: ObjC decl context for static var");
669 "getStaticDeclName: Unknown context for static var decl");
679 cir::GlobalLinkageKind linkage) {
701 mlir::Attribute init =
nullptr;
703 d.
hasAttr<CUDASharedAttr>() || d.
hasAttr<LoaderUninitializedAttr>())
704 init = cir::UndefAttr::get(lty);
708 mlir::ptr::MemorySpaceAttrInterface addrSpace =
713 lty,
false, linkage, addrSpace);
717 gv.setInitialValueAttr(init);
718 gv.setAlignment(
getASTContext().getDeclAlign(&d).getAsAlign().value());
746 if (
const auto *cd = dyn_cast<CXXConstructorDecl>(dc))
748 else if (
const auto *dd = dyn_cast<CXXDestructorDecl>(dc))
750 else if (
const auto *fd = dyn_cast<FunctionDecl>(dc))
762 "OpenMP: DisableAutoDeclareTargetRAII for static local");
771 mlir::Attribute constAttr,
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();
780 }
else if (
const auto *om = dyn_cast<ObjCMethodDecl>(dc)) {
781 return om->getNameAsString();
787 llvm_unreachable(
"expected a function or method");
794 if (!cacheEntry || cacheEntry.getInitialValue() != constAttr) {
795 auto ty = mlir::cast<mlir::TypedAttr>(constAttr).getType();
796 bool isConstant =
true;
802 name = (
"__const." + functionName(dc) +
"." + d.
getName()).str();
804 llvm_unreachable(
"local variable has no parent function or method");
807 cir::GlobalOp gv = builder.createVersionedGlobal(
809 cir::GlobalLinkageKind::PrivateLinkage);
813 cir::GlobalLinkageKind::PrivateLinkage));
814 gv.setInitialValueAttr(constAttr);
815 gv.setAlignment(align.getAsAlign().value());
816 gv.setUnnamedAddr(
true);
819 }
else if (cacheEntry.getAlignment() <
820 static_cast<uint64_t
>(align.getQuantity())) {
821 cacheEntry.setAlignment(align.getAsAlign().value());
826 mlir::Type eltTy = mlir::cast<mlir::TypedAttr>(constAttr).getType();
827 auto ptrTy = builder.getPointerTo(cacheEntry.getSymType());
828 mlir::Value globalPtr = cir::GetGlobalOp::create(
830 return Address(globalPtr, eltTy, align);
837 const VarDecl &d, cir::GlobalOp gv, cir::GetGlobalOp gvAddr) {
839 mlir::TypedAttr init = mlir::dyn_cast_if_present<mlir::TypedAttr>(
847 "constant l-value expression");
853 gv.setConstant(
false);
855 gvAddr.setStaticLocal(
true);
869 if (gv.getSymType() != init.getType()) {
870 gv.setSymType(init.getType());
878 gvAddr.getAddr().setType(builder.getPointerTo(init.getType()));
886 gv.setInitialValueAttr(init);
895 gvAddr.setStaticLocal(
true);
902 cir::GlobalLinkageKind linkage) {
906 cir::GlobalOp globalOp =
cgm.getOrCreateStaticVarDecl(d, linkage);
912 assert(getAddrOp &&
"expected cir::GetGlobalOp");
928 mlir::Type expectedType = addr.getType();
930 cir::GlobalOp var = globalOp;
936 if (d.
getInit() && !isCudaSharedVar)
939 var.setAlignment(alignment.
getAsAlign().value());
944 cgm.addGlobalAnnotations(&d, var);
945 if (d.
getAttr<PragmaClangBSSSectionAttr>())
947 "emitStaticVarDecl: CIR global BSS section attribute");
948 if (d.
getAttr<PragmaClangDataSectionAttr>())
950 "emitStaticVarDecl: CIR global Data section attribute");
951 if (d.
getAttr<PragmaClangRodataSectionAttr>())
953 "emitStaticVarDecl: CIR global Rodata section attribute");
954 if (d.
getAttr<PragmaClangRelroSectionAttr>())
956 "emitStaticVarDecl: CIR global Relro section attribute");
958 if (
const SectionAttr *sa = d.
getAttr<SectionAttr>())
959 var.setSectionAttr(builder.getStringAttr(sa->getName()));
961 if (
cgm.getCodeGenOpts().KeepPersistentStorageVariables)
970 mlir::Value castedAddr =
971 builder.createBitcast(getAddrOp.getAddr(), expectedType);
972 castedAddr =
cgm.castGlobalToDeclAddrSpace(castedAddr, d);
974 cgm.setStaticLocalDeclAddress(&d, var);
981 bool capturedByInit) {
986 if (capturedByInit) {
995 LValue lvalue,
bool capturedByInit) {
997 if (capturedByInit) {
1004 if (
type->isReferenceType()) {
1019 "emitExprAsInit: complex type captured by init");
1034 llvm_unreachable(
"bad evaluation kind");
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:
1057 case Decl::MSProperty:
1058 case Decl::IndirectField:
1059 case Decl::ObjCIvar:
1060 case Decl::ObjCAtDefsField:
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:
1081 case Decl::ObjCPropertyImpl:
1082 case Decl::FileScopeAsm:
1084 case Decl::FriendTemplate:
1086 case Decl::OutlinedFunction:
1087 case Decl::Captured:
1088 case Decl::UsingShadow:
1089 case Decl::ConstructorUsingShadow:
1090 case Decl::ObjCTypeParam:
1092 case Decl::UnresolvedUsingIfExists:
1093 case Decl::HLSLBuffer:
1094 case Decl::HLSLRootSignature:
1095 llvm_unreachable(
"Declaration should not be in declstmts!");
1097 case Decl::Function:
1098 case Decl::EnumConstant:
1099 case Decl::ExplicitInstantiation:
1100 case Decl::StaticAssert:
1104 case Decl::TemplateParamObject:
1107 case Decl::LifetimeExtendedTemporary:
1108 case Decl::RequiresExprBody:
1109 case Decl::UnnamedGlobalConstant:
1115 case Decl::CXXRecord:
1116 case Decl::NamespaceAlias:
1118 case Decl::UsingEnum:
1119 case Decl::UsingDirective:
1123 case Decl::Decomposition: {
1126 "Should not see file-scope variables inside a function!");
1128 if (evaluateConditionDecl)
1132 case Decl::OpenACCDeclare:
1135 case Decl::OpenACCRoutine:
1138 case Decl::OMPThreadPrivate:
1141 case Decl::OMPGroupPrivate:
1144 case Decl::OMPAllocate:
1147 case Decl::OMPCapturedExpr:
1150 case Decl::OMPRequires:
1153 case Decl::OMPDeclareMapper:
1156 case Decl::OMPDeclareReduction:
1160 case Decl::TypeAlias: {
1167 case Decl::ImplicitConceptSpecialization:
1168 case Decl::TopLevelStmt:
1169 case Decl::UsingPack:
1170 case Decl::CXXExpansionStmt:
1172 std::string(
"emitDecl: unhandled decl type: ") +
1179 if (!
sanOpts.has(SanitizerKind::NullabilityAssign))
1189 : addr(addr),
type(
type), destroyer(destroyer) {
1204 DestroyNRVOVariable(Address addr, QualType
type, mlir::Value nrvoFlag)
1205 : nrvoFlag(nrvoFlag), addr(addr), ty(
type) {}
1207 mlir::Value nrvoFlag;
1211 void emit(CIRGenFunction &cgf, Flags flags)
override {
1213 bool nrvo = flags.isForNormalCleanup() && nrvoFlag;
1216 mlir::OpBuilder::InsertionGuard guard(builder);
1219 mlir::Location loc = addr.
getPointer().getLoc();
1221 mlir::Value notNRVO = builder.
createNot(didNRVO);
1222 cir::IfOp::create(builder, loc, notNRVO,
false,
1223 [&](mlir::OpBuilder &b, mlir::Location) {
1224 static_cast<Derived *
>(
this)->emitDestructorCall(cgf);
1228 static_cast<Derived *
>(
this)->emitDestructorCall(cgf);
1232 virtual ~DestroyNRVOVariable() =
default;
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),
1242 const CXXDestructorDecl *dtor;
1244 void emitDestructorCall(CIRGenFunction &cgf) {
1253 CallStackRestore(Address stack) : stack(stack) {}
1255 void emit(CIRGenFunction &cgf, Flags flags)
override {
1256 mlir::Location loc = stack.getPointer().getLoc();
1265 mlir::Value arrayBegin;
1267 QualType elementType;
1268 CharUnits elementAlign;
1271 IrregularPartialArrayDestroy(mlir::Value arrayBegin, Address arrayEndPointer,
1272 QualType elementType, CharUnits elementAlign,
1274 : arrayBegin(arrayBegin), arrayEndPointer(arrayEndPointer),
1275 elementType(elementType), elementAlign(elementAlign),
1276 destroyer(destroyer) {}
1278 void emit(CIRGenFunction &cgf, Flags flags)
override {
1280 mlir::Location loc = arrayBegin.getLoc();
1282 mlir::Value arrayEnd = builder.
createLoad(loc, arrayEndPointer);
1291 mlir::cast<cir::PointerType>(arrayBegin.getType()).getAddrSpace());
1293 mlir::Value begin = arrayBegin;
1294 if (baseElementType != elementType) {
1303 cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne, arrayEnd, begin);
1305 builder, loc, ne,
false,
1306 [&](mlir::OpBuilder &b, mlir::Location loc) {
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);
1320 [&](mlir::OpBuilder &b, mlir::Location loc) {
1321 mlir::Value cur = builder.
createLoad(loc, iterAddr);
1323 loc, mlir::cast<cir::IntType>(cgf.
ptrDiffTy), -1);
1324 mlir::Value prev = cir::PtrStrideOp::create(
1325 builder, loc, ptrToElmType, cur, negOne);
1328 destroyer(cgf, elemAddr, baseElementType);
1349 EHCleanup, arrayBegin, arrayEndPointer, elementType, elementAlign,
1368 assert(dtorKind &&
"cannot push destructor for trivial type");
1381 assert(dtorKind &&
"cannot push destructor for trivial type");
1390 bool useEHCleanupForArray) {
1398 bool useEHCleanupForArray,
const ValueDecl *extendingDecl) {
1400 cgm.errorNYI(
"conditional lifetime-extended destroy");
1407 useEHCleanupForArray);
1425 std::optional<CapturedCleanups::CaptureScope> capture;
1446 mlir::Value numElements,
1455 cir::PointerType ptrToElmType = builder.getPointerTo(
1457 mlir::cast<cir::PointerType>(begin.getType()).getAddrSpace());
1459 auto regionBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1460 mlir::BlockArgument arg =
1461 b.getInsertionBlock()->addArgument(ptrToElmType, loc);
1466 destroyer(*
this, curAddr, elementType);
1468 cir::YieldOp::create(b, loc);
1472 if (
auto constantCount = numElements.getDefiningOp<cir::ConstantOp>()) {
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);
1484 mlir::Value elemBegin = builder.createPtrBitcast(begin, cirElementType);
1485 cir::ArrayDtor::create(builder,
getLoc(*
currSrcLoc), elemBegin, numElements,
1500 return destroyer(*
this, addr,
type);
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)
1520 if (constantCount && constantCount.use_empty())
1521 constantCount.erase();
1528 llvm_unreachable(
"no destroyer for trivial dtor");
1534 cgm.errorNYI(
"getDestroyer: other destruction kind");
1537 llvm_unreachable(
"Unknown DestructionKind");
1541 ehStack.pushCleanup<CallStackRestore>(
kind, spMem);
1562 llvm_unreachable(
"no cleanup for trivially-destructible variable");
1568 assert(!
type->isArrayType());
1570 ehStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr,
type, dtor,
1580 cgm.errorNYI(var->getSourceRange(),
1581 "emitAutoVarTypeCleanup: other dtor kind");
1590 ehStack.pushCleanup<DestroyObject>(cleanupKind, addr,
type, destroyer);
1594 if (
auto *dd = dyn_cast_if_present<DecompositionDecl>(vd)) {
1595 for (
auto *b : dd->flat_bindings())
1596 if (
auto *hd = b->getHoldingVar())
1602 if (!shouldEmitLifetimeMarkers)
1605 assert(mlir::cast<cir::PointerType>(addr.getType()).getAddrSpace() ==
1607 "Pointer should be in alloca address space");
1609 cir::LifetimeStartOp::create(builder, loc, addr);
1614 if (!shouldEmitLifetimeMarkers)
1617 assert(mlir::cast<cir::PointerType>(addr.getType()).getAddrSpace() ==
1619 "Pointer should be in alloca address space");
1621 cir::LifetimeEndOp::create(builder, loc, addr);
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.
mlir::Value getPointer() const
mlir::Type getElementType() const
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
cir::AllocaOp getUnderlyingAllocaOp() const
Return the underlying alloca for this address, if any.
mlir::Operation * getDefiningOp() const
Get the operation which defines this address.
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()
bool isInConditionalBranch() const
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...
static Destroyer destroyCXXObject
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
bool supportsCOMDAT() 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)
void finalize(cir::GlobalOp gv)
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)
bool isNormalCleanup() const
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.
static RValue get(mlir::Value v)
Represents a call to a C++ constructor.
Represents a C++ constructor within a class.
Represents a C++ destructor within a class.
Represents a C++ struct/union/class.
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
This is an opaque type for sizes expressed in character units.
llvm::Align getAsAlign() const
Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit bytes.
static CharUnits One()
Construct a CharUnits quantity of one.
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks,...
SourceLocation getLocation() const
const char * getDeclKindName() const
DeclContext * getDeclContext()
virtual SourceRange getSourceRange() const LLVM_READONLY
Source range that this declaration covers.
This represents one expression.
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...
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...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Represents a function declaration or definition.
Represents a prototype with parameter type info, e.g.
GlobalDecl - represents a global declaration.
const Decl * getDecl() const
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
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...
bool isExternallyVisible() const
A (possibly-)qualified type.
@ DK_objc_strong_lifetime
@ PDIK_Struct
The type is a struct containing a field whose type is not PCK_Trivial.
LangAS getAddressSpace() const
Return the address space of this type.
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Represents a struct/union/class.
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...
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
const T * castAs() const
Member-template castAs<specific type>.
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
bool isVectorType() const
bool isRecordType() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
TLSKind getTLSKind() const
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
bool mightBeUsableInConstantExpressions(const ASTContext &C) const
Determine whether this variable's value might be usable in a constant expression, according to the re...
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO).
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
const Expr * getInit() const
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
@ TLS_None
Not a TLS variable.
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
StorageDuration getStorageDuration() const
Get the storage duration of this variable, per C++ [basic.stc].
bool isEscapingByref() const
Indicates the capture is a __block variable that is captured by a block that can potentially escape (...
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.
void info(bool Verbose, unsigned Level, const char *Fmt, Ts &&...Args)
Prints an indented note to stderr when Verbose is set.
Top level wrappers for InstallAPI frontend operations.
@ Ctor_Base
Base object ctor.
bool isa(CodeGen::Address addr)
bool isUnresolvedExceptionSpec(ExceptionSpecificationType ESpecType)
@ SD_Automatic
Automatic storage duration (most local variables).
@ Dtor_Base
Base object dtor.
@ Dtor_Complete
Complete object dtor.
U cast(CodeGen::Address addr)
__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.
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 wasEmittedAsGlobal() const
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.
const clang::VarDecl * variable
bool isConstantAggregate
True if the variable is of aggregate type and has a constant initializer.
bool wasEmittedAsOffloadClause() const
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