| 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" |
| 15 | #include "CIRGenConstantEmitter.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" |
| 22 | #include "clang/AST/DeclOpenACC.h" |
| 23 | #include "clang/AST/Expr.h" |
| 24 | #include "clang/AST/ExprCXX.h" |
| 25 | #include "clang/AST/RecursiveASTVisitor.h" |
| 26 | #include "clang/Basic/Cuda.h" |
| 27 | #include "clang/CIR/Dialect/IR/CIRAttrs.h" |
| 28 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 29 | #include "clang/CIR/MissingFeatures.h" |
| 30 | #include "llvm/Support/SaveAndRestore.h" |
| 31 | |
| 32 | using namespace clang; |
| 33 | using namespace clang::CIRGen; |
| 34 | |
| 35 | namespace { |
| 36 | /// Call the function named by a variable's `cleanup` attribute with the |
| 37 | /// variable's address when it goes out of scope. |
| 38 | struct 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 | funcPtr: cleanupFn, abstractInfo: 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>(Val: cgf.convertType(t: argTy)); |
| 66 | mlir::Value arg = |
| 67 | builder.createPtrBitcast(src: addr.getPointer(), newPointeeTy: argPtrTy.getPointee()); |
| 68 | arg = cgf.performAddrSpaceCast(v: arg, destTy: argPtrTy); |
| 69 | |
| 70 | CallArgList args; |
| 71 | args.add(rvalue: RValue::get(v: arg), type: cgf.getContext().getPointerType(T: varType)); |
| 72 | cgf.emitCall(funcInfo: fnInfo, callee, returnValue: ReturnValueSlot(), args, |
| 73 | /*callOp=*/nullptr, /*isMustTail=*/false, clangLoc: attrRange); |
| 74 | } |
| 75 | }; |
| 76 | } // namespace |
| 77 | |
| 78 | struct 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(loc: addr.getLoc(), addr); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | CIRGenFunction::AutoVarEmission |
| 91 | CIRGenFunction::emitAutoVarAlloca(const VarDecl &d, |
| 92 | mlir::OpBuilder::InsertPoint ip) { |
| 93 | QualType ty = d.getType(); |
| 94 | assert( |
| 95 | ty.getAddressSpace() == LangAS::Default || |
| 96 | (ty.getAddressSpace() == LangAS::opencl_private && getLangOpts().OpenCL)); |
| 97 | |
| 98 | mlir::Location loc = getLoc(srcLoc: d.getSourceRange()); |
| 99 | bool nrvo = |
| 100 | getContext().getLangOpts().ElideConstructors && d.isNRVOVariable(); |
| 101 | |
| 102 | CIRGenFunction::AutoVarEmission emission(d); |
| 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: &d); |
| 109 | |
| 110 | // If the type is variably-modified, emit all the VLA sizes for it. |
| 111 | if (ty->isVariablyModifiedType()) |
| 112 | emitVariablyModifiedType(ty); |
| 113 | |
| 114 | assert(!cir::MissingFeatures::openMP()); |
| 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(Context: getContext()) || |
| 130 | getContext().getBaseElementType(QT: ty)->isObjCObjectPointerType()) && |
| 131 | d.getInit()->isConstantInitializer(Ctx&: getContext())))) { |
| 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 = |
| 141 | d.needsDestruction(Ctx: getContext()) == QualType::DK_cxx_destructor; |
| 142 | if ((!getContext().getLangOpts().OpenCL || |
| 143 | ty.getAddressSpace() == LangAS::opencl_constant) && |
| 144 | (cgm.getCodeGenOpts().MergeAllConstants && !nrvo && |
| 145 | !d.isEscapingByref() && |
| 146 | ty.isConstantStorage(Ctx: getContext(), ExcludeCtor: true, ExcludeDtor: !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>(Val: 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(ty: falseNVRO.getType(), |
| 173 | align: CharUnits::One(), loc, name: "nrvo" , |
| 174 | /*arraySize=*/nullptr); |
| 175 | assert(builder.getInsertionBlock()); |
| 176 | builder.createStore(loc, val: falseNVRO, dst: 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(t: ty); |
| 186 | // Create the temp alloca and declare variable using it. |
| 187 | address = createTempAlloca(ty: allocaTy, align: alignment, loc, name: d.getName(), |
| 188 | /*arraySize=*/nullptr, /*alloca=*/nullptr, ip); |
| 189 | declare(addrVal: address.getPointer(), var: &d, ty, loc: getLoc(srcLoc: 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. |
| 195 | assert(!cir::MissingFeatures::lifetimeMarkersBypass()); |
| 196 | if (shouldEmitLifetimeMarkersForAutoVar() && haveInsertPoint()) { |
| 197 | emission.useLifetimeMarkers = emitLifetimeStartOp( |
| 198 | loc, addr: address.getUnderlyingAllocaOp().getResult()); |
| 199 | } |
| 200 | } |
| 201 | } else { |
| 202 | // Non-constant size type |
| 203 | assert(!cir::MissingFeatures::openMP()); |
| 204 | if (!didCallStackSave) { |
| 205 | // Save the stack. |
| 206 | cir::PointerType defaultTy = allocaInt8PtrTy; |
| 207 | CharUnits align = CharUnits::fromQuantity( |
| 208 | Quantity: cgm.getDataLayout().getAlignment(ty: defaultTy, useABIAlign: false)); |
| 209 | Address stack = createTempAlloca(ty: defaultTy, align, loc, name: "saved_stack" ); |
| 210 | |
| 211 | mlir::Value v = builder.createStackSave(loc, ty: defaultTy); |
| 212 | assert(v.getType() == allocaInt8PtrTy); |
| 213 | builder.createStore(loc, val: v, dst: 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. |
| 219 | pushStackRestore(kind: NormalCleanup, spMem: stack); |
| 220 | } |
| 221 | |
| 222 | VlaSizePair vlaSize = getVLASize(type: ty); |
| 223 | mlir::Type memTy = convertTypeForMem(t: vlaSize.type); |
| 224 | |
| 225 | // Allocate memory for the array. |
| 226 | address = |
| 227 | createTempAlloca(ty: memTy, align: alignment, loc, name: d.getName(), arraySize: vlaSize.numElts, |
| 228 | /*alloca=*/nullptr, ip: 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. |
| 233 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 234 | } |
| 235 | |
| 236 | emission.addr = address; |
| 237 | setAddrOfLocalVar(vd: &d, addr: address); |
| 238 | |
| 239 | if (d.hasAttr<AnnotateAttr>() && haveInsertPoint()) |
| 240 | emitVarAnnotations(d: &d, addr: 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 | kind: NormalEHLifetimeMarker, a: 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. |
| 253 | bool CIRGenFunction::isTrivialInitializer(const Expr *init) { |
| 254 | if (!init) |
| 255 | return true; |
| 256 | |
| 257 | if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(Val: 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 | |
| 266 | static 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; |
| 275 | assert(!cir::MissingFeatures::addAutoInitAnnotation()); |
| 276 | assert(!cir::MissingFeatures::vectorConstants()); |
| 277 | |
| 278 | if (addr.getElementType() != ty) |
| 279 | addr = addr.withElementType(builder, ElemTy: 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(context: &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(cRange: 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, val: builder.getConstant(loc, attr: constant), dst: addr); |
| 301 | } |
| 302 | |
| 303 | void CIRGenFunction::emitAutoVarInit( |
| 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() == |
| 322 | QualType::PDIK_Struct) { |
| 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. |
| 333 | assert(!cir::MissingFeatures::opAllocaCaptureByInit()); |
| 334 | |
| 335 | // Note: constexpr already initializes everything correctly. |
| 336 | LangOptions::TrivialAutoVarInitKind trivialAutoVarInit = |
| 337 | (d.isConstexpr() |
| 338 | ? LangOptions::TrivialAutoVarInitKind::Uninitialized |
| 339 | : (d.getAttr<UninitializedAttr>() |
| 340 | ? LangOptions::TrivialAutoVarInitKind::Uninitialized |
| 341 | : getContext().getLangOpts().getTrivialAutoVarInit())); |
| 342 | |
| 343 | auto initializeWhatIsTechnicallyUninitialized = [&](Address addr) { |
| 344 | if (trivialAutoVarInit == |
| 345 | LangOptions::TrivialAutoVarInitKind::Uninitialized) |
| 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 || |
| 358 | d.mightBeUsableInConstantExpressions(C: getContext())) { |
| 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. |
| 364 | constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(d); |
| 365 | if (constant && !mlir::isa<cir::ZeroAttr>(Val: constant) && |
| 366 | (trivialAutoVarInit != |
| 367 | LangOptions::TrivialAutoVarInitKind::Uninitialized)) { |
| 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>(Val: init)) { |
| 378 | initializeWhatIsTechnicallyUninitialized(addr); |
| 379 | LValue lv = makeAddrLValue(addr, ty: type, source: AlignmentSource::Decl); |
| 380 | emitExprAsInit(init, d: &d, lvalue: 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(context: &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>(Val&: 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, ty: type); |
| 403 | assert(init && "expected initializer" ); |
| 404 | mlir::Location initLoc = getLoc(srcLoc: init->getSourceRange()); |
| 405 | // lv.setNonGC(true); |
| 406 | return emitStoreThroughLValue( |
| 407 | src: RValue::get(v: builder.getConstant(loc: initLoc, attr: typedConstant)), dst: lv); |
| 408 | } |
| 409 | |
| 410 | emitStoresForConstant(cgm, d, addr, isVolatile: type.isVolatileQualified(), builder, |
| 411 | constant: typedConstant); |
| 412 | } |
| 413 | |
| 414 | void CIRGenFunction::emitAutoVarCleanups( |
| 415 | const CIRGenFunction::AutoVarEmission &emission) { |
| 416 | const VarDecl &d = *emission.variable; |
| 417 | |
| 418 | // Check the type for a cleanup. |
| 419 | if (QualType::DestructionKind dtorKind = d.needsDestruction(Ctx: getContext())) |
| 420 | emitAutoVarTypeCleanup(emission, dtorKind); |
| 421 | |
| 422 | assert(!cir::MissingFeatures::opAllocaPreciseLifetime()); |
| 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(gd: fd); |
| 429 | assert(f && "Could not find function!" ); |
| 430 | |
| 431 | const CIRGenFunctionInfo &info = |
| 432 | cgm.getTypes().arrangeFunctionDeclaration(fd); |
| 433 | ehStack.pushCleanup<CallCleanupFunction>(kind: NormalAndEHCleanup, a: f, a: &info, |
| 434 | a: emission.getObjectAddress(cgf&: *this), |
| 435 | a: d.getType(), a: 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. |
| 442 | void CIRGenFunction::emitAutoVarDecl(const VarDecl &d) { |
| 443 | CIRGenFunction::AutoVarEmission emission = emitAutoVarAlloca(d); |
| 444 | emitAutoVarInit(emission); |
| 445 | emitAutoVarCleanups(emission); |
| 446 | } |
| 447 | |
| 448 | namespace { |
| 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. |
| 452 | class LifetimeExtendedTemporaryFinder |
| 453 | : public RecursiveASTVisitor<LifetimeExtendedTemporaryFinder> { |
| 454 | const VarDecl &var; |
| 455 | llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 2> visited; |
| 456 | SmallVector<QualType, 2> types; |
| 457 | |
| 458 | public: |
| 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(Ptr: 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(CommaLHS&: commaLHSs, Adjustments&: adjustments); |
| 481 | if (e->getType().isDestructedType() != QualType::DK_none) |
| 482 | types.push_back(Elt: e->getType()); |
| 483 | return true; |
| 484 | } |
| 485 | }; |
| 486 | } // namespace |
| 487 | |
| 488 | bool CIRGenFunction::destructorMayThrow(QualType type) { |
| 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(ESpecType: fpt->getExceptionSpecType()) || |
| 495 | !fpt->isNothrow(); |
| 496 | } |
| 497 | |
| 498 | SmallVector<QualType, 2> |
| 499 | CIRGenFunction::getLifetimeExtendedTemporariesWithDestructor(const VarDecl &d) { |
| 500 | const Expr *init = d.getInit(); |
| 501 | if (!init) |
| 502 | return {}; |
| 503 | LifetimeExtendedTemporaryFinder finder(d); |
| 504 | finder.TraverseStmt(S: const_cast<Expr *>(init)); |
| 505 | return SmallVector<QualType, 2>(finder.found()); |
| 506 | } |
| 507 | |
| 508 | void CIRGenFunction::emitLoopConditionVariable( |
| 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. |
| 516 | if (d.getType().getAddressSpace() == LangAS::opencl_local) |
| 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() && |
| 525 | ((d.needsDestruction(Ctx: getContext()) != QualType::DK_none && |
| 526 | destructorMayThrow(type: d.getType())) || |
| 527 | (extendedTemporaries.size() > 1 && |
| 528 | llvm::any_of(Range&: extendedTemporaries, P: 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. |
| 534 | emitAutoVarDeclWithCapturedCleanups(d, cleanups&: condCleanup, |
| 535 | /*guardDestruction=*/true, |
| 536 | extendedTemporaries); |
| 537 | } |
| 538 | |
| 539 | void CIRGenFunction::emitAutoVarDeclWithCapturedCleanups( |
| 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(Ctx: getContext()) != QualType::DK_none; |
| 559 | bool extendsTemporaries = !extendedTemporaries.empty(); |
| 560 | |
| 561 | Address activeFlag = Address::invalid(); |
| 562 | if (needsCleanup || extendsTemporaries) { |
| 563 | mlir::Location loc = getLoc(srcLoc: d.getSourceRange()); |
| 564 | activeFlag = createTempAllocaWithoutCast( |
| 565 | ty: builder.getBoolTy(), align: CharUnits::One(), loc, name: "cond.cleanup.isactive" , |
| 566 | /*arraySize=*/nullptr, |
| 567 | ip: builder.getBestAllocaInsertPoint(block: getCurFunctionEntryBlock())); |
| 568 | builder.createFlagStore(loc, val: false, dst: 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{.extendingDecl: &d, .cleanups: &cleanups, .activeFlag: activeFlag}; |
| 577 | llvm::SaveAndRestore<const LifetimeExtensionCapture *> captureExtended( |
| 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(oldCleanupStackDepth: 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(srcLoc: d.getSourceRange()); |
| 604 | builder.createFlagStore(loc, val: true, dst: activeFlag.getPointer()); |
| 605 | } |
| 606 | |
| 607 | { |
| 608 | CapturedCleanups::CaptureScope capture(cleanups); |
| 609 | emitAutoVarCleanups(emission); |
| 610 | } |
| 611 | |
| 612 | if (needsCleanup) |
| 613 | initFullExprCleanupWithFlag(activeFlag); |
| 614 | } |
| 615 | |
| 616 | void CIRGenFunction::emitVarDecl(const VarDecl &d) { |
| 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(vd: &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 | |
| 640 | if (d.getType().getAddressSpace() == LangAS::opencl_local) |
| 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 | |
| 649 | static std::string getStaticDeclName(CIRGenModule &cgm, const VarDecl &d) { |
| 650 | if (cgm.getLangOpts().CPlusPlus) |
| 651 | return cgm.getMangledName(gd: &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>(Val: dc)) |
| 658 | dc = cast<DeclContext>(Val: cd->getNonClosureContext()); |
| 659 | if (const auto *fd = dyn_cast<FunctionDecl>(Val: dc)) |
| 660 | contextName = std::string(cgm.getMangledName(gd: fd)); |
| 661 | else if (isa<BlockDecl>(Val: dc)) |
| 662 | cgm.errorNYI(d.getSourceRange(), |
| 663 | "getStaticDeclName: block decl context for static var" ); |
| 664 | else if (isa<ObjCMethodDecl>(Val: 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? |
| 677 | cir::GlobalOp |
| 678 | CIRGenModule::getOrCreateStaticVarDecl(const VarDecl &d, |
| 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: &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(gd: &d)); |
| 694 | else |
| 695 | name = getStaticDeclName(cgm&: *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; |
| 702 | if (ty.getAddressSpace() == LangAS::opencl_local || |
| 703 | d.hasAttr<CUDASharedAttr>() || d.hasAttr<LoaderUninitializedAttr>()) |
| 704 | init = cir::UndefAttr::get(type: lty); |
| 705 | else |
| 706 | init = emitNullConstantAttr(t: ty); |
| 707 | |
| 708 | mlir::ptr::MemorySpaceAttrInterface addrSpace = |
| 709 | getGlobalCIRAddressSpace(as: getGlobalVarAddressSpace(decl: &d)); |
| 710 | |
| 711 | cir::GlobalOp gv = |
| 712 | builder.createVersionedGlobal(module: getModule(), loc: getLoc(cLoc: d.getLocation()), name, |
| 713 | type: lty, isConstant: false, linkage, addrSpace); |
| 714 | insertGlobalSymbol(op: gv); |
| 715 | // TODO(cir): infer visibility from linkage in global op builder. |
| 716 | gv.setVisibility(getMLIRVisibilityFromCIRLinkage(GLK: linkage)); |
| 717 | gv.setInitialValueAttr(init); |
| 718 | gv.setAlignment(getASTContext().getDeclAlign(D: &d).getAsAlign().value()); |
| 719 | |
| 720 | if (supportsCOMDAT() && gv.isWeakForLinker()) |
| 721 | gv.setSelfComdat(); |
| 722 | |
| 723 | if (d.getTLSKind()) |
| 724 | setTLSMode(op: gv, d); |
| 725 | |
| 726 | setGVProperties(op: gv, d: &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. |
| 730 | setStaticLocalDeclAddress(d: &d, c: gv); |
| 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>(Val: d.getDeclContext()); |
| 735 | |
| 736 | // We can't name blocks or captured statements directly, so try to emit their |
| 737 | // parents. |
| 738 | if (isa<BlockDecl>(Val: dc) || isa<CapturedDecl>(Val: 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>(Val: dc)) |
| 747 | gd = GlobalDecl(cd, Ctor_Base); |
| 748 | else if (const auto *dd = dyn_cast<CXXDestructorDecl>(Val: dc)) |
| 749 | gd = GlobalDecl(dd, Dtor_Base); |
| 750 | else if (const auto *fd = dyn_cast<FunctionDecl>(Val: 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. |
| 761 | errorNYI(d.getSourceRange(), |
| 762 | "OpenMP: DisableAutoDeclareTargetRAII for static local" ); |
| 763 | } |
| 764 | (void)getAddrOfGlobal(gd); |
| 765 | } |
| 766 | |
| 767 | return gv; |
| 768 | } |
| 769 | |
| 770 | Address CIRGenModule::createUnnamedGlobalFrom(const VarDecl &d, |
| 771 | mlir::Attribute constAttr, |
| 772 | CharUnits align) { |
| 773 | auto functionName = [&](const DeclContext *dc) -> std::string { |
| 774 | if (const auto *fd = dyn_cast<FunctionDecl>(Val: dc)) { |
| 775 | if (const auto *cc = dyn_cast<CXXConstructorDecl>(Val: fd)) |
| 776 | return cc->getNameAsString(); |
| 777 | if (const auto *cd = dyn_cast<CXXDestructorDecl>(Val: fd)) |
| 778 | return cd->getNameAsString(); |
| 779 | return std::string(getMangledName(gd: fd)); |
| 780 | } else if (const auto *om = dyn_cast<ObjCMethodDecl>(Val: dc)) { |
| 781 | return om->getNameAsString(); |
| 782 | } else if (isa<BlockDecl>(Val: dc)) { |
| 783 | return "<block>" ; |
| 784 | } else if (isa<CapturedDecl>(Val: 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>(Val&: constAttr).getType(); |
| 796 | bool isConstant = true; |
| 797 | |
| 798 | std::string name; |
| 799 | if (d.hasGlobalStorage()) |
| 800 | name = getMangledName(gd: &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 | |
| 806 | assert(!cir::MissingFeatures::addressSpace()); |
| 807 | cir::GlobalOp gv = builder.createVersionedGlobal( |
| 808 | module: getModule(), loc: getLoc(cLoc: d.getLocation()), name, type: ty, isConstant, |
| 809 | linkage: cir::GlobalLinkageKind::PrivateLinkage); |
| 810 | insertGlobalSymbol(op: gv); |
| 811 | // TODO(cir): infer visibility from linkage in global op builder. |
| 812 | gv.setVisibility(getMLIRVisibilityFromCIRLinkage( |
| 813 | GLK: 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 |
| 825 | assert(!cir::MissingFeatures::addressSpace()); |
| 826 | mlir::Type eltTy = mlir::cast<mlir::TypedAttr>(Val&: constAttr).getType(); |
| 827 | auto ptrTy = builder.getPointerTo(ty: cacheEntry.getSymType()); |
| 828 | mlir::Value globalPtr = cir::GetGlobalOp::create( |
| 829 | builder, location: getLoc(cLoc: d.getLocation()), addr: ptrTy, name: 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. |
| 836 | cir::GlobalOp CIRGenFunction::addInitializerToStaticVarDecl( |
| 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 | Val: 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(Ctx: 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(varDecl: d, globalOp: 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. |
| 863 | assert(!cir::MissingFeatures::dataLayoutTypeAllocSize()); |
| 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. |
| 877 | assert(!cir::MissingFeatures::addressSpace()); |
| 878 | gvAddr.getAddr().setType(builder.getPointerTo(ty: init.getType())); |
| 879 | } |
| 880 | |
| 881 | bool needsDtor = |
| 882 | d.needsDestruction(Ctx: getContext()) == QualType::DK_cxx_destructor; |
| 883 | |
| 884 | gv.setConstant(d.getType().isConstantStorage( |
| 885 | Ctx: getContext(), /*ExcludeCtor=*/true, ExcludeDtor: !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(varDecl: d, globalOp: gv, /*performInit=*/false); |
| 895 | gvAddr.setStaticLocal(true); |
| 896 | } |
| 897 | |
| 898 | return gv; |
| 899 | } |
| 900 | |
| 901 | void CIRGenFunction::emitStaticVarDecl(const VarDecl &d, |
| 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(global: globalOp, threadLocal: 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: &d); |
| 915 | |
| 916 | // Store into LocalDeclMap before generating initializer to handle |
| 917 | // circular references. |
| 918 | mlir::Type elemTy = convertTypeForMem(t: d.getType()); |
| 919 | setAddrOfLocalVar(vd: &d, addr: 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. |
| 924 | if (d.getType()->isVariablyModifiedType()) |
| 925 | emitVariablyModifiedType(ty: d.getType()); |
| 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, gv: var, gvAddr: 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: &d, gv: 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(bytes: 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(src: getAddrOp.getAddr(), newTy: expectedType); |
| 972 | castedAddr = cgm.castGlobalToDeclAddrSpace(addr: castedAddr, vd: d); |
| 973 | localDeclMap.find(Val: &d)->second = Address(castedAddr, elemTy, alignment); |
| 974 | cgm.setStaticLocalDeclAddress(d: &d, c: var); |
| 975 | |
| 976 | assert(!cir::MissingFeatures::sanitizers()); |
| 977 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 978 | } |
| 979 | |
| 980 | void CIRGenFunction::emitScalarInit(const Expr *init, LValue lvalue, |
| 981 | bool capturedByInit) { |
| 982 | assert(!cir::MissingFeatures::objCLifetime()); |
| 983 | |
| 984 | SourceLocRAIIObject locRAII{*this, init->getSourceRange()}; |
| 985 | mlir::Value value = emitScalarExpr(e: init); |
| 986 | if (capturedByInit) { |
| 987 | cgm.errorNYI(init->getSourceRange(), "emitScalarInit: captured by init" ); |
| 988 | return; |
| 989 | } |
| 990 | assert(!cir::MissingFeatures::emitNullabilityCheck()); |
| 991 | emitStoreThroughLValue(src: RValue::get(v: value), dst: lvalue, isInit: true); |
| 992 | } |
| 993 | |
| 994 | void CIRGenFunction::emitExprAsInit(const Expr *init, const ValueDecl *d, |
| 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(e: init); |
| 1006 | if (capturedByInit) |
| 1007 | cgm.errorNYI(init->getSourceRange(), "emitExprAsInit: captured by init" ); |
| 1008 | emitStoreThroughLValue(src: rvalue, dst: lvalue); |
| 1009 | return; |
| 1010 | } |
| 1011 | switch (CIRGenFunction::getEvaluationKind(type)) { |
| 1012 | case cir::TEK_Scalar: |
| 1013 | emitScalarInit(init, lvalue); |
| 1014 | return; |
| 1015 | case cir::TEK_Complex: { |
| 1016 | mlir::Value complex = emitComplexExpr(e: init); |
| 1017 | if (capturedByInit) |
| 1018 | cgm.errorNYI(init->getSourceRange(), |
| 1019 | "emitExprAsInit: complex type captured by init" ); |
| 1020 | mlir::Location loc = getLoc(srcLoc: init->getExprLoc()); |
| 1021 | emitStoreOfComplex(loc, v: complex, dest: lvalue, |
| 1022 | /*isInit*/ true); |
| 1023 | return; |
| 1024 | } |
| 1025 | case cir::TEK_Aggregate: |
| 1026 | // The overlap flag here should be calculated. |
| 1027 | assert(!cir::MissingFeatures::aggValueSlotMayOverlap()); |
| 1028 | emitAggExpr(e: init, |
| 1029 | slot: AggValueSlot::forLValue(LV: lvalue, isDestructed: AggValueSlot::IsDestructed, |
| 1030 | isAliased: AggValueSlot::IsNotAliased, |
| 1031 | mayOverlap: AggValueSlot::MayOverlap)); |
| 1032 | return; |
| 1033 | } |
| 1034 | llvm_unreachable("bad evaluation kind" ); |
| 1035 | } |
| 1036 | |
| 1037 | void 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++] |
| 1120 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 1121 | return; |
| 1122 | case Decl::Var: |
| 1123 | case Decl::Decomposition: { |
| 1124 | const VarDecl &vd = cast<VarDecl>(Val: d); |
| 1125 | assert(vd.isLocalVarDecl() && |
| 1126 | "Should not see file-scope variables inside a function!" ); |
| 1127 | emitVarDecl(d: vd); |
| 1128 | if (evaluateConditionDecl) |
| 1129 | maybeEmitDeferredVarDeclInit(vd: &vd); |
| 1130 | return; |
| 1131 | } |
| 1132 | case Decl::OpenACCDeclare: |
| 1133 | emitOpenACCDeclare(d: cast<OpenACCDeclareDecl>(Val: d)); |
| 1134 | return; |
| 1135 | case Decl::OpenACCRoutine: |
| 1136 | emitOpenACCRoutine(d: cast<OpenACCRoutineDecl>(Val: d)); |
| 1137 | return; |
| 1138 | case Decl::OMPThreadPrivate: |
| 1139 | emitOMPThreadPrivateDecl(d: cast<OMPThreadPrivateDecl>(Val: d)); |
| 1140 | return; |
| 1141 | case Decl::OMPGroupPrivate: |
| 1142 | emitOMPGroupPrivateDecl(d: cast<OMPGroupPrivateDecl>(Val: d)); |
| 1143 | return; |
| 1144 | case Decl::OMPAllocate: |
| 1145 | emitOMPAllocateDecl(d: cast<OMPAllocateDecl>(Val: d)); |
| 1146 | return; |
| 1147 | case Decl::OMPCapturedExpr: |
| 1148 | emitOMPCapturedExpr(d: cast<OMPCapturedExprDecl>(Val: d)); |
| 1149 | return; |
| 1150 | case Decl::OMPRequires: |
| 1151 | emitOMPRequiresDecl(d: cast<OMPRequiresDecl>(Val: d)); |
| 1152 | return; |
| 1153 | case Decl::OMPDeclareMapper: |
| 1154 | emitOMPDeclareMapper(d: cast<OMPDeclareMapperDecl>(Val: d)); |
| 1155 | return; |
| 1156 | case Decl::OMPDeclareReduction: |
| 1157 | emitOMPDeclareReduction(d: cast<OMPDeclareReductionDecl>(Val: d)); |
| 1158 | return; |
| 1159 | case Decl::Typedef: // typedef int X; |
| 1160 | case Decl::TypeAlias: { // using X = int; [C++0x] |
| 1161 | QualType ty = cast<TypedefNameDecl>(Val: d).getUnderlyingType(); |
| 1162 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 1163 | if (ty->isVariablyModifiedType()) |
| 1164 | emitVariablyModifiedType(ty); |
| 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 | |
| 1177 | void CIRGenFunction::emitNullabilityCheck(LValue lhs, mlir::Value rhs, |
| 1178 | SourceLocation loc) { |
| 1179 | if (!sanOpts.has(K: SanitizerKind::NullabilityAssign)) |
| 1180 | return; |
| 1181 | |
| 1182 | assert(!cir::MissingFeatures::sanitizers()); |
| 1183 | } |
| 1184 | |
| 1185 | namespace { |
| 1186 | struct DestroyObject final : EHScopeStack::Cleanup { |
| 1187 | DestroyObject(Address addr, QualType type, |
| 1188 | CIRGenFunction::Destroyer *destroyer) |
| 1189 | : addr(addr), type(type), destroyer(destroyer) { |
| 1190 | assert(!cir::MissingFeatures::useEHCleanupForArray()); |
| 1191 | } |
| 1192 | |
| 1193 | Address addr; |
| 1194 | QualType type; |
| 1195 | CIRGenFunction::Destroyer *destroyer; |
| 1196 | |
| 1197 | void emit(CIRGenFunction &cgf, Flags flags) override { |
| 1198 | assert(!cir::MissingFeatures::useEHCleanupForArray()); |
| 1199 | cgf.emitDestroy(addr, type, destroyer); |
| 1200 | } |
| 1201 | }; |
| 1202 | |
| 1203 | template <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, addr: nrvoFlag); |
| 1221 | mlir::Value notNRVO = builder.createNot(value: 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 | |
| 1235 | struct 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) { |
| 1245 | cgf.emitCXXDestructorCall(dd: dtor, type: Dtor_Complete, |
| 1246 | /*forVirtualBase=*/false, |
| 1247 | /*delegating=*/false, thisAddr: addr, thisTy: ty); |
| 1248 | } |
| 1249 | }; |
| 1250 | |
| 1251 | struct 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, addr: 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. |
| 1264 | struct 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, addr: 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(QT: elementType); |
| 1287 | |
| 1288 | mlir::Type cirElementType = cgf.convertTypeForMem(t: baseElementType); |
| 1289 | cir::PointerType ptrToElmType = builder.getPointerTo( |
| 1290 | ty: cirElementType, |
| 1291 | as: mlir::cast<cir::PointerType>(Val: arrayBegin.getType()).getAddrSpace()); |
| 1292 | |
| 1293 | mlir::Value begin = arrayBegin; |
| 1294 | if (baseElementType != elementType) { |
| 1295 | begin = builder.createPtrBitcast(src: begin, newPointeeTy: cirElementType); |
| 1296 | arrayEnd = builder.createPtrBitcast(src: arrayEnd, newPointeeTy: 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, location: loc, kind: cir::CmpOpKind::ne, lhs: arrayEnd, rhs: begin); |
| 1304 | cir::IfOp::create( |
| 1305 | builder, location: loc, cond: ne, /*withElseRegion=*/false, |
| 1306 | thenBuilder: [&](mlir::OpBuilder &b, mlir::Location loc) { |
| 1307 | Address iterAddr = cgf.createTempAlloca( |
| 1308 | ty: ptrToElmType, align: cgf.getPointerAlign(), loc, name: "__array_idx" ); |
| 1309 | builder.createStore(loc, val: arrayEnd, dst: iterAddr); |
| 1310 | builder.createDoWhile( |
| 1311 | loc, |
| 1312 | /*condBuilder=*/ |
| 1313 | [&](mlir::OpBuilder &b, mlir::Location loc) { |
| 1314 | mlir::Value cur = builder.createLoad(loc, addr: iterAddr); |
| 1315 | mlir::Value cmp = cir::CmpOp::create( |
| 1316 | builder, location: loc, kind: cir::CmpOpKind::ne, lhs: cur, rhs: begin); |
| 1317 | builder.createCondition(condition: cmp); |
| 1318 | }, |
| 1319 | /*bodyBuilder=*/ |
| 1320 | [&](mlir::OpBuilder &b, mlir::Location loc) { |
| 1321 | mlir::Value cur = builder.createLoad(loc, addr: iterAddr); |
| 1322 | cir::ConstantOp negOne = builder.getConstInt( |
| 1323 | loc, t: mlir::cast<cir::IntType>(Val&: cgf.ptrDiffTy), c: -1); |
| 1324 | mlir::Value prev = cir::PtrStrideOp::create( |
| 1325 | builder, location: loc, result: ptrToElmType, base: cur, stride: negOne); |
| 1326 | builder.createStore(loc, val: prev, dst: 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 |
| 1343 | void CIRGenFunction::pushIrregularPartialArrayCleanup(mlir::Value arrayBegin, |
| 1344 | Address arrayEndPointer, |
| 1345 | QualType elementType, |
| 1346 | CharUnits elementAlign, |
| 1347 | Destroyer *destroyer) { |
| 1348 | pushCleanupAndDeferDeactivation<IrregularPartialArrayDestroy>( |
| 1349 | kind: EHCleanup, a: arrayBegin, a: arrayEndPointer, a: elementType, a: elementAlign, |
| 1350 | a: 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. |
| 1355 | void CIRGenFunction::pushEHDestroyIfNeeded(QualType::DestructionKind dtorKind, |
| 1356 | Address addr, QualType type) { |
| 1357 | if (!needsEHCleanup(kind: dtorKind)) |
| 1358 | return; |
| 1359 | |
| 1360 | assert(!cir::MissingFeatures::useEHCleanupForArray()); |
| 1361 | pushDestroy(kind: EHCleanup, addr, type, destroyer: getDestroyer(kind: dtorKind)); |
| 1362 | } |
| 1363 | |
| 1364 | /// Push the standard destructor for the given type as |
| 1365 | /// at least a normal cleanup. |
| 1366 | void CIRGenFunction::pushDestroy(QualType::DestructionKind dtorKind, |
| 1367 | Address addr, QualType type) { |
| 1368 | assert(dtorKind && "cannot push destructor for trivial type" ); |
| 1369 | |
| 1370 | CleanupKind cleanupKind = getCleanupKind(kind: dtorKind); |
| 1371 | pushDestroy(kind: cleanupKind, addr, type, destroyer: getDestroyer(kind: dtorKind)); |
| 1372 | } |
| 1373 | |
| 1374 | void CIRGenFunction::pushDestroy(CleanupKind cleanupKind, Address addr, |
| 1375 | QualType type, Destroyer *destroyer) { |
| 1376 | pushFullExprCleanup<DestroyObject>(kind: cleanupKind, a: addr, a: type, a: destroyer); |
| 1377 | } |
| 1378 | |
| 1379 | void CIRGenFunction::pushDestroyAndDeferDeactivation( |
| 1380 | QualType::DestructionKind dtorKind, Address addr, QualType type) { |
| 1381 | assert(dtorKind && "cannot push destructor for trivial type" ); |
| 1382 | |
| 1383 | CleanupKind cleanupKind = getCleanupKind(kind: dtorKind); |
| 1384 | pushDestroyAndDeferDeactivation( |
| 1385 | cleanupKind, addr, type, destroyer: getDestroyer(kind: dtorKind), useEHCleanupForArray: cleanupKind & EHCleanup); |
| 1386 | } |
| 1387 | |
| 1388 | void CIRGenFunction::pushDestroyAndDeferDeactivation( |
| 1389 | CleanupKind cleanupKind, Address addr, QualType type, Destroyer *destroyer, |
| 1390 | bool useEHCleanupForArray) { |
| 1391 | assert(!cir::MissingFeatures::useEHCleanupForArray()); |
| 1392 | pushCleanupAndDeferDeactivation<DestroyObject>(kind: cleanupKind, a: addr, a: type, |
| 1393 | a: destroyer); |
| 1394 | } |
| 1395 | |
| 1396 | void CIRGenFunction::pushLifetimeExtendedDestroy( |
| 1397 | CleanupKind cleanupKind, Address addr, QualType type, Destroyer *destroyer, |
| 1398 | bool useEHCleanupForArray, const ValueDecl *extendingDecl) { |
| 1399 | if (isInConditionalBranch() || hiddenOutermostConditional) { |
| 1400 | cgm.errorNYI(feature: "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 | |
| 1409 | assert(!cir::MissingFeatures::useEHCleanupForArray()); |
| 1410 | |
| 1411 | pushCleanupAfterFullExpr(kind: 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. |
| 1415 | if (lifetimeExtensionCapture && |
| 1416 | extendingDecl == lifetimeExtensionCapture->extendingDecl) { |
| 1417 | PendingCleanupEntry &entry = lifetimeExtendedCleanupStack.back(); |
| 1418 | entry.activeFlag = lifetimeExtensionCapture->activeFlag; |
| 1419 | entry.capturedBy = lifetimeExtensionCapture->cleanups; |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | void CIRGenFunction::pushPendingCleanupToEHStack( |
| 1424 | const PendingCleanupEntry &entry) { |
| 1425 | std::optional<CapturedCleanups::CaptureScope> capture; |
| 1426 | if (entry.capturedBy) |
| 1427 | capture.emplace(args&: *entry.capturedBy); |
| 1428 | ehStack.pushCleanup<DestroyObject>(kind: entry.kind, a: entry.addr, a: entry.type, |
| 1429 | a: entry.destroyer); |
| 1430 | |
| 1431 | if (entry.activeFlag.isValid()) { |
| 1432 | EHCleanupScope &scope = cast<EHCleanupScope>(Val&: *ehStack.begin()); |
| 1433 | scope.setActiveFlag(entry.activeFlag); |
| 1434 | scope.setTestFlagInNormalCleanup(scope.isNormalCleanup()); |
| 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 |
| 1445 | void CIRGenFunction::emitArrayDestroy(mlir::Value begin, |
| 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(t: elementType); |
| 1455 | cir::PointerType ptrToElmType = builder.getPointerTo( |
| 1456 | ty: cirElementType, |
| 1457 | as: mlir::cast<cir::PointerType>(Val: begin.getType()).getAddrSpace()); |
| 1458 | |
| 1459 | auto regionBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) { |
| 1460 | mlir::BlockArgument arg = |
| 1461 | b.getInsertionBlock()->addArgument(type: ptrToElmType, loc); |
| 1462 | Address curAddr = Address(arg, cirElementType, elementAlign); |
| 1463 | assert(!cir::MissingFeatures::dtorCleanups()); |
| 1464 | |
| 1465 | // Perform the actual destruction there. |
| 1466 | destroyer(*this, curAddr, elementType); |
| 1467 | |
| 1468 | cir::YieldOp::create(builder&: b, location: 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(elementType: cirElementType, size); |
| 1477 | mlir::Value arrayOp = builder.createPtrBitcast(src: begin, newPointeeTy: arrayTy); |
| 1478 | cir::ArrayDtor::create(builder, location: getLoc(srcLoc: *currSrcLoc), addr: 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(src: begin, newPointeeTy: cirElementType); |
| 1485 | cir::ArrayDtor::create(builder, location: getLoc(srcLoc: *currSrcLoc), addr: elemBegin, num_elements: 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 |
| 1496 | void CIRGenFunction::emitDestroy(Address addr, QualType type, |
| 1497 | Destroyer *destroyer) { |
| 1498 | const ArrayType *arrayType = getContext().getAsArrayType(T: type); |
| 1499 | if (!arrayType) |
| 1500 | return destroyer(*this, addr, type); |
| 1501 | |
| 1502 | mlir::Value length = emitArrayLength(arrayType, baseType&: type, addr); |
| 1503 | |
| 1504 | CharUnits elementAlign = addr.getAlignment().alignmentOfArrayElement( |
| 1505 | elementSize: getContext().getTypeSizeInChars(T: 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>(Val: constantCount.getValue()); |
| 1511 | if (constIntAttr && constIntAttr.getUInt() == 0) |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | mlir::Value begin = addr.getPointer(); |
| 1516 | assert(!cir::MissingFeatures::useEHCleanupForArray()); |
| 1517 | emitArrayDestroy(begin, numElements: length, elementType: 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 | |
| 1524 | CIRGenFunction::Destroyer * |
| 1525 | CIRGenFunction::getDestroyer(QualType::DestructionKind kind) { |
| 1526 | switch (kind) { |
| 1527 | case QualType::DK_none: |
| 1528 | llvm_unreachable("no destroyer for trivial dtor" ); |
| 1529 | case QualType::DK_cxx_destructor: |
| 1530 | return destroyCXXObject; |
| 1531 | case QualType::DK_objc_strong_lifetime: |
| 1532 | case QualType::DK_objc_weak_lifetime: |
| 1533 | case QualType::DK_nontrivial_c_struct: |
| 1534 | cgm.errorNYI(feature: "getDestroyer: other destruction kind" ); |
| 1535 | return nullptr; |
| 1536 | } |
| 1537 | llvm_unreachable("Unknown DestructionKind" ); |
| 1538 | } |
| 1539 | |
| 1540 | void CIRGenFunction::pushStackRestore(CleanupKind kind, Address spMem) { |
| 1541 | ehStack.pushCleanup<CallStackRestore>(kind, a: spMem); |
| 1542 | } |
| 1543 | |
| 1544 | /// Enter a destroy cleanup for the given local variable. |
| 1545 | void CIRGenFunction::emitAutoVarTypeCleanup( |
| 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(cgf&: *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 | |
| 1564 | case QualType::DK_cxx_destructor: |
| 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>(kind: cleanupKind, a: addr, a: type, a: dtor, |
| 1571 | a: emission.nrvoFlag); |
| 1572 | return; |
| 1573 | } |
| 1574 | // Otherwise, this is handled below. |
| 1575 | break; |
| 1576 | |
| 1577 | case QualType::DK_objc_strong_lifetime: |
| 1578 | case QualType::DK_objc_weak_lifetime: |
| 1579 | case QualType::DK_nontrivial_c_struct: |
| 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(kind: dtorKind); |
| 1588 | |
| 1589 | assert(!cir::MissingFeatures::useEHCleanupForArray()); |
| 1590 | ehStack.pushCleanup<DestroyObject>(kind: cleanupKind, a: addr, a: type, a: destroyer); |
| 1591 | } |
| 1592 | |
| 1593 | void CIRGenFunction::maybeEmitDeferredVarDeclInit(const VarDecl *vd) { |
| 1594 | if (auto *dd = dyn_cast_if_present<DecompositionDecl>(Val: vd)) { |
| 1595 | for (auto *b : dd->flat_bindings()) |
| 1596 | if (auto *hd = b->getHoldingVar()) |
| 1597 | emitVarDecl(d: *hd); |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | bool CIRGenFunction::emitLifetimeStartOp(mlir::Location loc, mlir::Value addr) { |
| 1602 | if (!shouldEmitLifetimeMarkers) |
| 1603 | return false; |
| 1604 | |
| 1605 | assert(mlir::cast<cir::PointerType>(addr.getType()).getAddrSpace() == |
| 1606 | cir::normalizeDefaultAddressSpace(getCIRAllocaAddressSpace()) && |
| 1607 | "Pointer should be in alloca address space" ); |
| 1608 | |
| 1609 | cir::LifetimeStartOp::create(builder, location: loc, ptr: addr); |
| 1610 | return true; |
| 1611 | } |
| 1612 | |
| 1613 | void CIRGenFunction::emitLifetimeEndOp(mlir::Location loc, mlir::Value addr) { |
| 1614 | if (!shouldEmitLifetimeMarkers) |
| 1615 | return; |
| 1616 | |
| 1617 | assert(mlir::cast<cir::PointerType>(addr.getType()).getAddrSpace() == |
| 1618 | cir::normalizeDefaultAddressSpace(getCIRAllocaAddressSpace()) && |
| 1619 | "Pointer should be in alloca address space" ); |
| 1620 | |
| 1621 | cir::LifetimeEndOp::create(builder, location: loc, ptr: addr); |
| 1622 | } |
| 1623 | |