| //===----------------------------------------------------------------------===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| // | |
| // This contains code dealing with C++ code generation. | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #include "CIRGenCXXABI.h" | |
| #include "CIRGenFunction.h" | |
| #include "CIRGenModule.h" | |
| #include "clang/AST/GlobalDecl.h" | |
| #include "clang/CIR/Dialect/IR/CIRAttrs.h" | |
| #include "clang/CIR/MissingFeatures.h" | |
| #include "llvm/Support/SaveAndRestore.h" | |
| using namespace clang; | |
| using namespace clang::CIRGen; | |
| /// Emit code to cause the variable at the given address to be considered as | |
| /// constant from this point onwards. | |
| static void emitDeclInvariant(CIRGenFunction &cgf, const VarDecl *d) { | |
| mlir::Value addr = cgf.cgm.getAddrOfGlobalVar(d); | |
| cgf.emitInvariantStart(cgf.getContext().getTypeSizeInChars(d->getType()), | |
| addr, cgf.getLoc(d->getSourceRange())); | |
| } | |
| void CIRGenFunction::emitInvariantStart(CharUnits size, mlir::Value addr, | |
| mlir::Location loc) { | |
| // Do not emit the intrinsic if we're not optimizing. | |
| if (!cgm.getCodeGenOpts().OptimizationLevel) | |
| return; | |
| CIRGenBuilderTy &builder = getBuilder(); | |
| // Create the size constant as i64 | |
| uint64_t width = size.getQuantity(); | |
| mlir::Value sizeValue = builder.getConstInt(loc, builder.getSInt64Ty(), | |
| static_cast<int64_t>(width)); | |
| // Create the intrinsic call. The llvm.invariant.start intrinsic returns a | |
| // token, but we don't need to capture it. The address space will be | |
| // automatically handled when the intrinsic is lowered to LLVM IR. | |
| cir::LLVMIntrinsicCallOp::create( | |
| builder, loc, builder.getStringAttr("invariant.start"), addr.getType(), | |
| mlir::ValueRange{sizeValue, addr}); | |
| } | |
| /// The ctor and dtor regions of a global (or static local) hold a single block, | |
| /// but an initializer can have control flow: a `throw` or a trap ends the block | |
| /// it is in, and the rest of the initializer goes in a new, unreachable one. | |
| /// If emitting into \p region produced more than one block, move them all into | |
| /// a cir.scope, leaving the region with one block that holds the scope. | |
| static void wrapMultiBlockRegionInScope(CIRGenBuilderTy &builder, | |
| mlir::Region ®ion, | |
| mlir::Location loc) { | |
| if (region.empty() || region.hasOneBlock()) | |
| return; | |
| mlir::OpBuilder::InsertionGuard guard(builder); | |
| mlir::Block *wrapper = builder.createBlock(®ion, region.begin()); | |
| builder.setInsertionPointToStart(wrapper); | |
| auto scope = cir::ScopeOp::create(builder, loc, | |
| [](mlir::OpBuilder &, mlir::Location) {}); | |
| // Swap the scope's empty block for the original blocks, which already end in | |
| // the cir.yield that terminates the scope. | |
| mlir::Region &scopeRegion = scope.getRegion(); | |
| scopeRegion.getBlocks().clear(); | |
| scopeRegion.getBlocks().splice(scopeRegion.end(), region.getBlocks(), | |
| std::next(region.begin()), region.end()); | |
| builder.setInsertionPointToEnd(wrapper); | |
| cir::YieldOp::create(builder, loc); | |
| } | |
| static void emitDeclInit(CIRGenFunction &cgf, const VarDecl *varDecl, | |
| cir::GlobalOp globalOp, mlir::Region &ctorRegion) { | |
| assert((varDecl->hasGlobalStorage() || | |
| (varDecl->hasLocalStorage() && | |
| cgf.getContext().getLangOpts().OpenCLCPlusPlus)) && | |
| "VarDecl must have global or local (in the case of OpenCL) storage!"); | |
| assert(!varDecl->getType()->isReferenceType() && | |
| "Should not call emitDeclInit on a reference!"); | |
| CIRGenBuilderTy &builder = cgf.getBuilder(); | |
| // Set up the ctor region. | |
| mlir::OpBuilder::InsertionGuard guard(builder); | |
| mlir::Block *block = builder.createBlock(&ctorRegion); | |
| CIRGenFunction::LexicalScope lexScope{cgf, globalOp.getLoc(), | |
| builder.getInsertionBlock()}; | |
| lexScope.setAsGlobalInit(); | |
| builder.setInsertionPointToStart(block); | |
| Address declAddr(cgf.cgm.getAddrOfGlobalVar(varDecl), | |
| cgf.cgm.getASTContext().getDeclAlign(varDecl)); | |
| QualType type = varDecl->getType(); | |
| LValue lv = cgf.makeAddrLValue(declAddr, type); | |
| const Expr *init = varDecl->getInit(); | |
| switch (CIRGenFunction::getEvaluationKind(type)) { | |
| case cir::TEK_Scalar: | |
| assert(!cir::MissingFeatures::objCGC()); | |
| cgf.emitScalarInit(init, lv, false); | |
| break; | |
| case cir::TEK_Complex: | |
| cgf.emitComplexExprIntoLValue(init, lv, /*isInit=*/true); | |
| break; | |
| case cir::TEK_Aggregate: | |
| assert(!cir::MissingFeatures::aggValueSlotGC()); | |
| cgf.emitAggExpr(init, | |
| AggValueSlot::forLValue(lv, AggValueSlot::IsDestructed, | |
| AggValueSlot::IsNotAliased, | |
| AggValueSlot::DoesNotOverlap)); | |
| break; | |
| } | |
| // Finish the ctor region. | |
| builder.setInsertionPointToEnd(builder.getInsertionBlock()); | |
| cir::YieldOp::create(builder, globalOp.getLoc()); | |
| } | |
| static void emitDeclDestroy(CIRGenFunction &cgf, const VarDecl *vd, | |
| cir::GlobalOp addr, mlir::Region &dtorRegion) { | |
| // Honor __attribute__((no_destroy)) and bail instead of attempting | |
| // to emit a reference to a possibly nonexistent destructor, which | |
| // in turn can cause a crash. This will result in a global constructor | |
| // that isn't balanced out by a destructor call as intended by the | |
| // attribute. This also checks for -fno-c++-static-destructors and | |
| // bails even if the attribute is not present. | |
| QualType::DestructionKind dtorKind = vd->needsDestruction(cgf.getContext()); | |
| // FIXME: __attribute__((cleanup)) ? | |
| switch (dtorKind) { | |
| case QualType::DK_none: | |
| return; | |
| case QualType::DK_cxx_destructor: | |
| break; | |
| case QualType::DK_objc_strong_lifetime: | |
| case QualType::DK_objc_weak_lifetime: | |
| case QualType::DK_nontrivial_c_struct: | |
| // We don't care about releasing objects during process teardown. | |
| assert(!vd->getTLSKind() && "should have rejected this"); | |
| return; | |
| } | |
| // If not constant storage we'll emit this regardless of NeedsDtor value. | |
| CIRGenBuilderTy &builder = cgf.getBuilder(); | |
| // Prepare the dtor region. | |
| mlir::OpBuilder::InsertionGuard guard(builder); | |
| // Lifetime extended temporaries might have already created a block, so use | |
| // that if it exists. | |
| mlir::Block *block = dtorRegion.empty() ? builder.createBlock(&dtorRegion) | |
| : &dtorRegion.front(); | |
| CIRGenFunction::LexicalScope lexScope{cgf, addr.getLoc(), block}; | |
| lexScope.setAsGlobalInit(); | |
| builder.setInsertionPointToStart(block); | |
| CIRGenModule &cgm = cgf.cgm; | |
| QualType type = vd->getType(); | |
| // Special-case non-array C++ destructors, if they have the right signature. | |
| // Under some ABIs, destructors return this instead of void, and cannot be | |
| // passed directly to __cxa_atexit if the target does not allow this | |
| // mismatch. | |
| const CXXRecordDecl *record = type->getAsCXXRecordDecl(); | |
| bool canRegisterDestructor = | |
| record && (!cgm.getCXXABI().hasThisReturn( | |
| GlobalDecl(record->getDestructor(), Dtor_Complete)) || | |
| cgm.getCXXABI().canCallMismatchedFunctionType()); | |
| // If __cxa_atexit is disabled via a flag, a different helper function is | |
| // generated elsewhere which uses atexit instead, and it takes the destructor | |
| // directly. | |
| cir::FuncOp fnOp; | |
| if (record && (canRegisterDestructor || cgm.getCodeGenOpts().CXAAtExit)) { | |
| assert(!record->hasTrivialDestructor()); | |
| assert(!cir::MissingFeatures::openCL()); | |
| CXXDestructorDecl *dtor = record->getDestructor(); | |
| // In LLVM OG codegen this is done in registerGlobalDtor, but CIRGen | |
| // relies on LoweringPrepare for further decoupling, so build the | |
| // call right here. | |
| auto gd = GlobalDecl(dtor, Dtor_Complete); | |
| fnOp = cgm.getAddrAndTypeOfCXXStructor(gd).second; | |
| // When a global has a constant initializer that fixes the active member | |
| // of a union (e.g. an SSO short variant), CIR creates the global with | |
| // the initializer's narrowed record type, so `getAddrOfGlobalVar` returns | |
| // a pointer to the narrowed type rather than the variable's declared | |
| // type. Mirror the cast pattern from `emitGlobalVarDeclLValue` so the | |
| // destructor receives a `this` pointer typed as the declared class. | |
| mlir::Value thisAddr = cgm.getAddrOfGlobalVar(vd); | |
| mlir::Type realVarTy = cgm.getTypes().convertTypeForMem(type); | |
| cir::PointerType realPtrTy = cir::PointerType::get( | |
| realVarTy, | |
| mlir::cast<cir::PointerType>(thisAddr.getType()).getAddrSpace()); | |
| if (realPtrTy != thisAddr.getType()) | |
| thisAddr = builder.createBitcast(thisAddr.getLoc(), thisAddr, realPtrTy); | |
| // Make sure the call and the callee agree on calling convention. | |
| builder.createCallOp(cgf.getLoc(vd->getSourceRange()), fnOp, | |
| mlir::ValueRange{thisAddr}, /*attrs=*/{}, | |
| /*argAttrs=*/{}, /*resAttrs=*/{}, | |
| fnOp.getCallingConv()); | |
| assert(fnOp && "expected cir.func"); | |
| // TODO(cir): This doesn't do anything but check for unhandled conditions. | |
| // What it is meant to do should really be happening in LoweringPrepare. | |
| cgm.getCXXABI().registerGlobalDtor(vd, fnOp, nullptr); | |
| } else { | |
| // Otherwise, a custom destroyed is needed. Classic codegen creates a helper | |
| // function here and emits the destroy into the helper function, which is | |
| // called from __cxa_atexit. | |
| // In CIR, we just emit the destroy into the dtor region. It will be moved | |
| // into a separate function during the LoweringPrepare pass. | |
| // FIXME(cir): We should create a new operation here to explicitly get the | |
| // address of the global into whose dtor region we are emiiting the destroy. | |
| // The same applies to code above where it is calling getAddrOfGlobalVar. | |
| mlir::Value globalVal = builder.createGetGlobal(addr); | |
| globalVal.getDefiningOp<cir::GetGlobalOp>().setStaticLocal( | |
| addr.getDynamicInitGuard().has_value() && vd->isLocalVarDecl()); | |
| CharUnits alignment = cgf.getContext().getDeclAlign(vd); | |
| Address globalAddr{globalVal, cgf.convertTypeForMem(type), alignment}; | |
| cgf.emitDestroy(globalAddr, type, cgf.getDestroyer(dtorKind)); | |
| } | |
| builder.setInsertionPointToEnd(block); | |
| if (block->empty()) { | |
| block->erase(); | |
| // Don't confuse lexical cleanup. | |
| builder.clearInsertionPoint(); | |
| } else if (!block->mightHaveTerminator()) { | |
| // Temporary cleanup might have created a yield already: if not, create one. | |
| cir::YieldOp::create(builder, addr.getLoc()); | |
| } | |
| } | |
| cir::FuncOp CIRGenModule::codegenCXXStructor(GlobalDecl gd) { | |
| const CIRGenFunctionInfo &fnInfo = | |
| getTypes().arrangeCXXStructorDeclaration(gd); | |
| cir::FuncType funcType = getTypes().getFunctionType(fnInfo); | |
| cir::FuncOp fn = getAddrOfCXXStructor(gd, &fnInfo, /*FnType=*/nullptr, | |
| /*DontDefer=*/true, ForDefinition); | |
| setFunctionLinkage(gd, fn); | |
| CIRGenFunction cgf{*this, builder}; | |
| curCGF = &cgf; | |
| { | |
| mlir::OpBuilder::InsertionGuard guard(builder); | |
| cgf.generateCode(gd, fn, funcType); | |
| } | |
| curCGF = nullptr; | |
| setNonAliasAttributes(gd, fn); | |
| setCIRFunctionAttributesForDefinition(mlir::cast<FunctionDecl>(gd.getDecl()), | |
| fn); | |
| return fn; | |
| } | |
| // Global variables requiring non-trivial initialization are handled | |
| // differently in CIR than in classic codegen. Classic codegen emits | |
| // a global init function (__cxx_global_var_init) and inserts | |
| // initialization for each global there. In CIR, we attach a ctor | |
| // region to the global variable and insert the initialization code | |
| // into the ctor region. This will be moved into the | |
| // Map clang's VarDecl::TLSKind onto the CIR-native enum. | |
| static cir::TLSKind getCIRTLSKind(clang::VarDecl::TLSKind kind) { | |
| switch (kind) { | |
| case clang::VarDecl::TLS_None: | |
| return cir::TLSKind::None; | |
| case clang::VarDecl::TLS_Static: | |
| return cir::TLSKind::Static; | |
| case clang::VarDecl::TLS_Dynamic: | |
| return cir::TLSKind::Dynamic; | |
| } | |
| llvm_unreachable("unknown TLSKind"); | |
| } | |
| // Map clang's TemplateSpecializationKind onto the CIR-native enum. | |
| static cir::TemplateSpecializationKind | |
| getCIRTemplateSpecializationKind(clang::TemplateSpecializationKind kind) { | |
| switch (kind) { | |
| case clang::TSK_Undeclared: | |
| return cir::TemplateSpecializationKind::Undeclared; | |
| case clang::TSK_ImplicitInstantiation: | |
| return cir::TemplateSpecializationKind::ImplicitInstantiation; | |
| case clang::TSK_ExplicitSpecialization: | |
| return cir::TemplateSpecializationKind::ExplicitSpecialization; | |
| case clang::TSK_ExplicitInstantiationDeclaration: | |
| return cir::TemplateSpecializationKind::ExplicitInstantiationDeclaration; | |
| case clang::TSK_ExplicitInstantiationDefinition: | |
| return cir::TemplateSpecializationKind::ExplicitInstantiationDefinition; | |
| } | |
| llvm_unreachable("unknown clang::TemplateSpecializationKind"); | |
| } | |
| // __cxx_global_var_init function during the LoweringPrepare pass. | |
| void CIRGenModule::emitCXXSpecialVarDeclInit(const VarDecl *varDecl, | |
| cir::GlobalOp addr, | |
| bool performInit, | |
| mlir::Region &ctorRegion, | |
| mlir::Region &dtorRegion) { | |
| QualType ty = varDecl->getType(); | |
| assert(curCGF && "Special var init only available inside of a function"); | |
| CIRGenFunction &cgf = *curCGF; | |
| // TODO: handle address space | |
| // The address space of a static local variable (addr) may be different | |
| // from the address space of the "this" argument of the constructor. In that | |
| // case, we need an addrspacecast before calling the constructor. | |
| // | |
| // struct StructWithCtor { | |
| // __device__ StructWithCtor() {...} | |
| // }; | |
| // __device__ void foo() { | |
| // __shared__ StructWithCtor s; | |
| // ... | |
| // } | |
| // | |
| // For example, in the above CUDA code, the static local variable s has a | |
| // "shared" address space qualifier, but the constructor of StructWithCtor | |
| // expects "this" in the "generic" address space. | |
| assert(!cir::MissingFeatures::addressSpace()); | |
| // Attach the AST handle for consumers that need arbitrary AST properties. | |
| addr.setAstAttr(cir::ASTVarDeclAttr::get(&getMLIRContext(), varDecl)); | |
| // For dynamic-init-guarded globals, also materialize the specific facts | |
| // LoweringPrepare needs into a serializable attribute, so that lowering can | |
| // run without a live ASTContext (e.g. on serialized CIR in split-compilation | |
| // flows). This is orthogonal to the AST handle above. | |
| if (addr.getDynamicInitGuard().has_value()) | |
| addr.setDynamicInitInfoAttr(cir::DynamicInitInfoAttr::get( | |
| &getMLIRContext(), varDecl->isLocalVarDecl(), | |
| getCIRTLSKind(varDecl->getTLSKind()), varDecl->isInline(), | |
| getCIRTemplateSpecializationKind( | |
| varDecl->getTemplateSpecializationKind()))); | |
| if (!ty->isReferenceType()) { | |
| assert(!cir::MissingFeatures::openMP()); | |
| bool needsDtor = varDecl->needsDestruction(getASTContext()) == | |
| QualType::DK_cxx_destructor; | |
| bool isConstantStorage = | |
| varDecl->getType().isConstantStorage(getASTContext(), true, !needsDtor); | |
| // PerformInit, constant store invariant / destroy handled below. | |
| if (performInit) { | |
| emitDeclInit(cgf, varDecl, addr, ctorRegion); | |
| // Do this before anything else is added to the end of the region. | |
| wrapMultiBlockRegionInScope(cgf.getBuilder(), ctorRegion, addr.getLoc()); | |
| // For constant storage, emit invariant.start in the ctor region after | |
| // initialization but before the yield. | |
| if (isConstantStorage) { | |
| CIRGenBuilderTy &builder = cgf.getBuilder(); | |
| mlir::OpBuilder::InsertionGuard guard(builder); | |
| // Set insertion point to end of ctor region (before yield) | |
| if (!ctorRegion.empty()) { | |
| mlir::Block *block = &ctorRegion.back(); | |
| // Find the yield op and insert before it | |
| mlir::Operation *yieldOp = block->getTerminator(); | |
| if (yieldOp) { | |
| builder.setInsertionPoint(yieldOp); | |
| emitDeclInvariant(cgf, varDecl); | |
| } | |
| } | |
| } | |
| } else if (isConstantStorage) { | |
| emitDeclInvariant(cgf, varDecl); | |
| } | |
| if (!isConstantStorage) { | |
| emitDeclDestroy(cgf, varDecl, addr, dtorRegion); | |
| wrapMultiBlockRegionInScope(cgf.getBuilder(), dtorRegion, addr.getLoc()); | |
| } | |
| return; | |
| } | |
| mlir::OpBuilder::InsertionGuard guard(builder); | |
| auto *block = builder.createBlock(&ctorRegion); | |
| CIRGenFunction::LexicalScope scope{*curCGF, addr.getLoc(), | |
| builder.getInsertionBlock()}; | |
| scope.setAsGlobalInit(); | |
| builder.setInsertionPointToStart(block); | |
| mlir::Value getGlobal = builder.createGetGlobal(addr, varDecl->getTLSKind()); | |
| // If we're initializing a guarded local static, set the flag that | |
| // indicates that. Namespace-scope vague-linkage globals also get a | |
| // dynamic-init guard, but aren't "static locals". | |
| getGlobal.getDefiningOp<cir::GetGlobalOp>().setStaticLocal( | |
| addr.getDynamicInitGuard().has_value() && varDecl->isLocalVarDecl()); | |
| Address declAddr(getGlobal, getASTContext().getDeclAlign(varDecl)); | |
| assert(performInit && "cannot have a constant initializer which needs " | |
| "destruction for reference"); | |
| RValue rv = cgf.emitReferenceBindingToExpr(varDecl->getInit()); | |
| { | |
| mlir::OpBuilder::InsertionGuard guard(builder); | |
| mlir::Operation *rvalDefOp = rv.getValue().getDefiningOp(); | |
| if (rvalDefOp && rvalDefOp->getBlock()) { | |
| mlir::Block *rvalSrcBlock = rvalDefOp->getBlock(); | |
| if (!rvalSrcBlock->empty() && isa<cir::YieldOp>(rvalSrcBlock->back())) { | |
| mlir::Operation &front = rvalSrcBlock->front(); | |
| getGlobal.getDefiningOp()->moveBefore(&front); | |
| builder.setInsertionPoint(cast<cir::YieldOp>(rvalSrcBlock->back())); | |
| } | |
| } | |
| cgf.emitStoreOfScalar(rv.getValue(), declAddr, /*isVolatile=*/false, ty, | |
| LValueBaseInfo{}); | |
| } | |
| builder.setInsertionPointToEnd(builder.getInsertionBlock()); | |
| cir::YieldOp::create(builder, addr->getLoc()); | |
| wrapMultiBlockRegionInScope(builder, ctorRegion, addr.getLoc()); | |
| } | |
| void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl, | |
| cir::GlobalOp addr, | |
| bool performInit) { | |
| assert(!varDecl->isStaticLocal()); | |
| // Create a CIRGenFunction to emit the initializer. While this isn't a true | |
| // function, the handling works the same way. | |
| CIRGenFunction cgf{*this, builder, true}; | |
| llvm::SaveAndRestore<CIRGenFunction *> savedCGF(curCGF, &cgf); | |
| curCGF->curFn = addr; | |
| CIRGenFunction::SourceLocRAIIObject fnLoc{cgf, varDecl->getSourceRange()}; | |
| // Set up the constrained FP environment for the dynamic initializer. | |
| llvm::RoundingMode rm = getLangOpts().getDefaultRoundingMode(); | |
| LangOptions::FPExceptionModeKind eb = getLangOpts().getDefaultExceptionMode(); | |
| builder.setDefaultConstrainedRounding(rm); | |
| builder.setDefaultConstrainedExcept(eb); | |
| builder.setIsFPConstrained(false); | |
| if (eb != LangOptions::FPE_Ignore || | |
| rm != llvm::RoundingMode::NearestTiesToEven) { | |
| builder.setIsFPConstrained(true); | |
| addr.setStrictfp(true); | |
| } | |
| if (const auto *ipa = varDecl->getAttr<InitPriorityAttr>()) | |
| addr.setInitPriority(ipa->getPriority()); | |
| emitCXXSpecialVarDeclInit(varDecl, addr, performInit, addr.getCtorRegion(), | |
| addr.getDtorRegion()); | |
| } | |
| void CIRGenModule::emitCXXStaticLocalVarDeclInit(const VarDecl *varDecl, | |
| cir::GlobalOp addr, | |
| bool performInit) { | |
| assert(varDecl->isStaticLocal()); | |
| auto initOp = | |
| cir::LocalInitOp::create(builder, addr->getLoc(), addr.getSymNameAttr(), | |
| varDecl->getTLSKind() != VarDecl::TLS_None); | |
| emitCXXSpecialVarDeclInit(varDecl, addr, performInit, initOp.getCtorRegion(), | |
| initOp.getDtorRegion()); | |
| } |