| 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 dealing with C++ code generation of virtual tables. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CIRGenVTables.h" |
| 14 | |
| 15 | #include "CIRGenCXXABI.h" |
| 16 | #include "CIRGenModule.h" |
| 17 | #include "mlir/IR/Types.h" |
| 18 | #include "clang/AST/VTTBuilder.h" |
| 19 | #include "clang/AST/VTableBuilder.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace clang; |
| 24 | using namespace clang::CIRGen; |
| 25 | |
| 26 | CIRGenVTables::CIRGenVTables(CIRGenModule &cgm) |
| 27 | : cgm(cgm), vtContext(cgm.getASTContext().getVTableContext()) {} |
| 28 | |
| 29 | cir::FuncOp CIRGenModule::getAddrOfThunk(StringRef name, mlir::Type fnTy, |
| 30 | GlobalDecl gd) { |
| 31 | return getOrCreateCIRFunction(mangledName: name, funcType: fnTy, gd, /*forVTable=*/true, |
| 32 | /*dontDefer=*/true, /*isThunk=*/true); |
| 33 | } |
| 34 | |
| 35 | static void setThunkProperties(CIRGenModule &cgm, const ThunkInfo &thunk, |
| 36 | cir::FuncOp thunkFn, bool forVTable, |
| 37 | GlobalDecl gd) { |
| 38 | cgm.setFunctionLinkage(gd, f: thunkFn); |
| 39 | cgm.getCXXABI().setThunkLinkage(thunk: thunkFn, forVTable, gd, |
| 40 | returnAdjustment: !thunk.Return.isEmpty()); |
| 41 | |
| 42 | // Set the right visibility. |
| 43 | cgm.setGVProperties(op: thunkFn, d: cast<NamedDecl>(Val: gd.getDecl())); |
| 44 | |
| 45 | if (!cgm.getCXXABI().exportThunk()) { |
| 46 | assert(!cir::MissingFeatures::setDLLStorageClass()); |
| 47 | cgm.setDSOLocal(static_cast<mlir::Operation *>(thunkFn)); |
| 48 | } |
| 49 | |
| 50 | if (cgm.supportsCOMDAT() && thunkFn.isWeakForLinker()) |
| 51 | thunkFn.setSelfComdat(); |
| 52 | } |
| 53 | |
| 54 | mlir::Type CIRGenModule::getVTableComponentType() { |
| 55 | mlir::Type ptrTy = builder.getUInt8PtrTy(); |
| 56 | assert(!cir::MissingFeatures::vtableRelativeLayout()); |
| 57 | return ptrTy; |
| 58 | } |
| 59 | |
| 60 | mlir::Type CIRGenVTables::getVTableComponentType() { |
| 61 | return cgm.getVTableComponentType(); |
| 62 | } |
| 63 | |
| 64 | cir::RecordType CIRGenVTables::getVTableType(const VTableLayout &layout) { |
| 65 | SmallVector<mlir::Type, 4> tys; |
| 66 | mlir::Type componentType = getVTableComponentType(); |
| 67 | for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i) |
| 68 | tys.push_back(Elt: cir::ArrayType::get(elementType: componentType, size: layout.getVTableSize(i))); |
| 69 | |
| 70 | // FIXME(cir): should VTableLayout be encoded like we do for some |
| 71 | // AST nodes? |
| 72 | return cgm.getBuilder().getAnonRecordTy( |
| 73 | members: tys, /*packed=*/false, memberKinds: cir::RecordType::getAllDataKinds(members: tys)); |
| 74 | } |
| 75 | |
| 76 | /// At this point in the translation unit, does it appear that can we |
| 77 | /// rely on the vtable being defined elsewhere in the program? |
| 78 | /// |
| 79 | /// The response is really only definitive when called at the end of |
| 80 | /// the translation unit. |
| 81 | /// |
| 82 | /// The only semantic restriction here is that the object file should |
| 83 | /// not contain a vtable definition when that vtable is defined |
| 84 | /// strongly elsewhere. Otherwise, we'd just like to avoid emitting |
| 85 | /// vtables when unnecessary. |
| 86 | /// TODO(cir): this should be merged into common AST helper for codegen. |
| 87 | bool CIRGenVTables::isVTableExternal(const CXXRecordDecl *rd) { |
| 88 | assert(rd->isDynamicClass() && "Non-dynamic classes have no VTable." ); |
| 89 | |
| 90 | // We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't |
| 91 | // emit them even if there is an explicit template instantiation. |
| 92 | if (cgm.getTarget().getCXXABI().isMicrosoft()) |
| 93 | return false; |
| 94 | |
| 95 | // If we have an explicit instantiation declaration (and not a |
| 96 | // definition), the vtable is defined elsewhere. |
| 97 | TemplateSpecializationKind tsk = rd->getTemplateSpecializationKind(); |
| 98 | if (tsk == TSK_ExplicitInstantiationDeclaration) |
| 99 | return true; |
| 100 | |
| 101 | // Otherwise, if the class is an instantiated template, the |
| 102 | // vtable must be defined here. |
| 103 | if (tsk == TSK_ImplicitInstantiation || |
| 104 | tsk == TSK_ExplicitInstantiationDefinition) |
| 105 | return false; |
| 106 | |
| 107 | // Otherwise, if the class doesn't have a key function (possibly |
| 108 | // anymore), the vtable must be defined here. |
| 109 | const CXXMethodDecl *keyFunction = |
| 110 | cgm.getASTContext().getCurrentKeyFunction(RD: rd); |
| 111 | if (!keyFunction) |
| 112 | return false; |
| 113 | |
| 114 | // Otherwise, if we don't have a definition of the key function, the |
| 115 | // vtable must be defined somewhere else. |
| 116 | return !keyFunction->hasBody(); |
| 117 | } |
| 118 | |
| 119 | /// This is a callback from Sema to tell us that a particular vtable is |
| 120 | /// required to be emitted in this translation unit. |
| 121 | /// |
| 122 | /// This is only called for vtables that _must_ be emitted (mainly due to key |
| 123 | /// functions). For weak vtables, CodeGen tracks when they are needed and |
| 124 | /// emits them as-needed. |
| 125 | void CIRGenModule::emitVTable(const CXXRecordDecl *rd) { |
| 126 | vtables.generateClassData(rd); |
| 127 | } |
| 128 | |
| 129 | void CIRGenVTables::generateClassData(const CXXRecordDecl *rd) { |
| 130 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 131 | |
| 132 | if (rd->getNumVBases()) |
| 133 | cgm.getCXXABI().emitVirtualInheritanceTables(rd); |
| 134 | |
| 135 | cgm.getCXXABI().emitVTableDefinitions(cgvt&: *this, rd); |
| 136 | } |
| 137 | |
| 138 | mlir::Attribute CIRGenVTables::getVTableComponent( |
| 139 | const VTableLayout &layout, unsigned componentIndex, mlir::Attribute rtti, |
| 140 | unsigned &nextVTableThunkIndex, unsigned vtableAddressPoint, |
| 141 | bool vtableHasLocalLinkage) { |
| 142 | const VTableComponent &component = layout.vtable_components()[componentIndex]; |
| 143 | |
| 144 | CIRGenBuilderTy &builder = cgm.getBuilder(); |
| 145 | |
| 146 | assert(!cir::MissingFeatures::vtableRelativeLayout()); |
| 147 | |
| 148 | switch (component.getKind()) { |
| 149 | case VTableComponent::CK_UnusedFunctionPointer: |
| 150 | return builder.getConstNullPtrAttr(t: builder.getUInt8PtrTy()); |
| 151 | |
| 152 | case VTableComponent::CK_VCallOffset: |
| 153 | return builder.getConstPtrAttr(type: builder.getUInt8PtrTy(), |
| 154 | value: component.getVCallOffset().getQuantity()); |
| 155 | |
| 156 | case VTableComponent::CK_VBaseOffset: |
| 157 | return builder.getConstPtrAttr(type: builder.getUInt8PtrTy(), |
| 158 | value: component.getVBaseOffset().getQuantity()); |
| 159 | |
| 160 | case VTableComponent::CK_OffsetToTop: |
| 161 | return builder.getConstPtrAttr(type: builder.getUInt8PtrTy(), |
| 162 | value: component.getOffsetToTop().getQuantity()); |
| 163 | |
| 164 | case VTableComponent::CK_RTTI: |
| 165 | assert((mlir::isa<cir::GlobalViewAttr>(rtti) || |
| 166 | mlir::isa<cir::ConstPtrAttr>(rtti)) && |
| 167 | "expected GlobalViewAttr or ConstPtrAttr" ); |
| 168 | return rtti; |
| 169 | |
| 170 | case VTableComponent::CK_FunctionPointer: |
| 171 | case VTableComponent::CK_CompleteDtorPointer: |
| 172 | case VTableComponent::CK_DeletingDtorPointer: { |
| 173 | GlobalDecl gd = component.getGlobalDecl( |
| 174 | HasVectorDeletingDtors: cgm.getASTContext().getTargetInfo().emitVectorDeletingDtors( |
| 175 | cgm.getASTContext().getLangOpts())); |
| 176 | |
| 177 | const bool isThunk = |
| 178 | nextVTableThunkIndex < layout.vtable_thunks().size() && |
| 179 | layout.vtable_thunks()[nextVTableThunkIndex].first == componentIndex; |
| 180 | |
| 181 | if (cgm.getLangOpts().CUDA) { |
| 182 | // Emit NULL for methods we can't codegen on this |
| 183 | // side. Otherwise we'd end up with vtable with unresolved |
| 184 | // references. |
| 185 | const CXXMethodDecl *md = cast<CXXMethodDecl>(Val: gd.getDecl()); |
| 186 | // OK on device side: functions w/ __device__ attribute |
| 187 | // OK on host side: anything except __device__-only functions. |
| 188 | bool canEmitMethod = |
| 189 | cgm.getLangOpts().CUDAIsDevice |
| 190 | ? md->hasAttr<CUDADeviceAttr>() |
| 191 | : (md->hasAttr<CUDAHostAttr>() || !md->hasAttr<CUDADeviceAttr>()); |
| 192 | if (!canEmitMethod) { |
| 193 | if (isThunk) |
| 194 | nextVTableThunkIndex++; |
| 195 | return builder.getConstNullPtrAttr(t: builder.getUInt8PtrTy()); |
| 196 | } |
| 197 | // Method is acceptable, continue processing as usual. |
| 198 | } |
| 199 | |
| 200 | auto getSpecialVirtFn = [&](StringRef name) -> cir::FuncOp { |
| 201 | assert(!cir::MissingFeatures::vtableRelativeLayout()); |
| 202 | |
| 203 | if (cgm.getLangOpts().OpenMP && cgm.getLangOpts().OpenMPIsTargetDevice && |
| 204 | cgm.getTriple().isNVPTX()) |
| 205 | cgm.errorNYI(gd.getDecl()->getSourceRange(), |
| 206 | "getVTableComponent for OMP Device NVPTX" ); |
| 207 | |
| 208 | cir::FuncType fnTy = |
| 209 | cgm.getBuilder().getFuncType(params: {}, retTy: cgm.getBuilder().getVoidTy()); |
| 210 | cir::FuncOp fnPtr = cgm.createRuntimeFunction(ty: fnTy, name); |
| 211 | fnPtr.setUnnamedAddr(true); |
| 212 | |
| 213 | // The Microsoft ABI uses the same function name for pure and deleted |
| 214 | // virtual functions. |
| 215 | if (!fnPtr.isDeclaration()) |
| 216 | return fnPtr; |
| 217 | |
| 218 | // For device compilation, provide a weak definition that traps, |
| 219 | // otherwise linking ends up with unresolved references. |
| 220 | if (cgm.getLangOpts().isTargetDevice()) { |
| 221 | fnPtr.setLinkage(cir::GlobalLinkageKind::WeakAnyLinkage); |
| 222 | mlir::SymbolTable::setSymbolVisibility( |
| 223 | symbol: fnPtr, vis: cgm.getMLIRVisibilityFromCIRLinkage(GLK: fnPtr.getLinkage())); |
| 224 | mlir::OpBuilder::InsertionGuard guard(builder); |
| 225 | builder.setInsertionPointToStart(fnPtr.addEntryBlock()); |
| 226 | cir::TrapOp::create(builder, location: fnPtr.getLoc()); |
| 227 | } |
| 228 | |
| 229 | return fnPtr; |
| 230 | }; |
| 231 | |
| 232 | cir::FuncOp fnPtr; |
| 233 | if (cast<CXXMethodDecl>(Val: gd.getDecl())->isPureVirtual()) { |
| 234 | if (!pureVirtualFn) |
| 235 | pureVirtualFn = |
| 236 | getSpecialVirtFn(cgm.getCXXABI().getPureVirtualCallName()); |
| 237 | fnPtr = pureVirtualFn; |
| 238 | } else if (cast<CXXMethodDecl>(Val: gd.getDecl())->isDeleted()) { |
| 239 | if (!deletedVirtualFn) |
| 240 | deletedVirtualFn = |
| 241 | getSpecialVirtFn(cgm.getCXXABI().getDeletedVirtualCallName()); |
| 242 | fnPtr = deletedVirtualFn; |
| 243 | } else if (isThunk) { |
| 244 | const ThunkInfo &thunkInfo = |
| 245 | layout.vtable_thunks()[nextVTableThunkIndex].second; |
| 246 | nextVTableThunkIndex++; |
| 247 | fnPtr = maybeEmitThunk(gd, thunkAdjustments: thunkInfo, /*forVTable=*/true); |
| 248 | assert(!cir::MissingFeatures::pointerAuthentication()); |
| 249 | } else { |
| 250 | // Otherwise we can use the method definition directly. |
| 251 | cir::FuncType fnTy = cgm.getTypes().getFunctionType(gd); |
| 252 | fnPtr = cgm.getAddrOfFunction(gd, funcType: fnTy, /*ForVTable=*/forVTable: true); |
| 253 | } |
| 254 | |
| 255 | return cir::GlobalViewAttr::get( |
| 256 | type: builder.getUInt8PtrTy(), |
| 257 | symbol: mlir::FlatSymbolRefAttr::get(value: fnPtr.getSymNameAttr())); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | llvm_unreachable("Unexpected vtable component kind" ); |
| 262 | } |
| 263 | |
| 264 | void CIRGenVTables::createVTableInitializer(cir::GlobalOp &vtableOp, |
| 265 | const clang::VTableLayout &layout, |
| 266 | mlir::Attribute rtti, |
| 267 | bool vtableHasLocalLinkage) { |
| 268 | mlir::Type componentType = getVTableComponentType(); |
| 269 | |
| 270 | const llvm::SmallVectorImpl<unsigned> &addressPoints = |
| 271 | layout.getAddressPointIndices(); |
| 272 | unsigned nextVTableThunkIndex = 0; |
| 273 | |
| 274 | mlir::MLIRContext *mlirContext = &cgm.getMLIRContext(); |
| 275 | |
| 276 | SmallVector<mlir::Attribute> vtables; |
| 277 | for (auto [vtableIndex, addressPoint] : llvm::enumerate(First: addressPoints)) { |
| 278 | // Build a ConstArrayAttr of the vtable components. |
| 279 | size_t vtableStart = layout.getVTableOffset(i: vtableIndex); |
| 280 | size_t vtableEnd = vtableStart + layout.getVTableSize(i: vtableIndex); |
| 281 | llvm::SmallVector<mlir::Attribute> components; |
| 282 | components.reserve(N: vtableEnd - vtableStart); |
| 283 | for (size_t componentIndex : llvm::seq(Begin: vtableStart, End: vtableEnd)) |
| 284 | components.push_back( |
| 285 | Elt: getVTableComponent(layout, componentIndex, rtti, nextVTableThunkIndex, |
| 286 | vtableAddressPoint: addressPoint, vtableHasLocalLinkage)); |
| 287 | // Create a ConstArrayAttr to hold the components. |
| 288 | auto arr = cir::ConstArrayAttr::get( |
| 289 | type: cir::ArrayType::get(elementType: componentType, size: components.size()), |
| 290 | elts: mlir::ArrayAttr::get(context: mlirContext, value: components)); |
| 291 | vtables.push_back(Elt: arr); |
| 292 | } |
| 293 | |
| 294 | // Create a ConstRecordAttr to hold the component array. |
| 295 | const auto members = mlir::ArrayAttr::get(context: mlirContext, value: vtables); |
| 296 | cir::ConstRecordAttr record = cgm.getBuilder().getAnonConstRecord(arrayAttr: members); |
| 297 | |
| 298 | // Create a VTableAttr |
| 299 | SmallVector<mlir::Attribute> addressPointAttrs; |
| 300 | for (auto [vtableIndex, addressPoint] : llvm::enumerate(First: addressPoints)) |
| 301 | addressPointAttrs.push_back( |
| 302 | Elt: cir::AddressPointAttr::get(context: mlirContext, index: vtableIndex, offset: addressPoint)); |
| 303 | auto vtableAttr = cir::VTableAttr::get( |
| 304 | type: record.getType(), data: record.getMembers(), |
| 305 | address_points: mlir::ArrayAttr::get(context: mlirContext, value: addressPointAttrs)); |
| 306 | |
| 307 | // Add the vtable initializer to the vtable global op. |
| 308 | cgm.setInitializer(op&: vtableOp, value: vtableAttr); |
| 309 | } |
| 310 | |
| 311 | cir::GlobalOp CIRGenVTables::generateConstructionVTable( |
| 312 | const CXXRecordDecl *rd, const BaseSubobject &base, bool baseIsVirtual, |
| 313 | cir::GlobalLinkageKind linkage, VTableAddressPointsMapTy &addressPoints) { |
| 314 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 315 | |
| 316 | std::unique_ptr<VTableLayout> vtLayout( |
| 317 | getItaniumVTableContext().createConstructionVTableLayout( |
| 318 | MostDerivedClass: base.getBase(), MostDerivedClassOffset: base.getBaseOffset(), MostDerivedClassIsVirtual: baseIsVirtual, LayoutClass: rd)); |
| 319 | |
| 320 | // Add the address points. |
| 321 | addressPoints = vtLayout->getAddressPoints(); |
| 322 | |
| 323 | // Get the mangled construction vtable name. |
| 324 | SmallString<256> outName; |
| 325 | llvm::raw_svector_ostream out(outName); |
| 326 | cast<ItaniumMangleContext>(Val&: cgm.getCXXABI().getMangleContext()) |
| 327 | .mangleCXXCtorVTable(RD: rd, Offset: base.getBaseOffset().getQuantity(), |
| 328 | Type: base.getBase(), out); |
| 329 | SmallString<256> name(outName); |
| 330 | |
| 331 | assert(!cir::MissingFeatures::vtableRelativeLayout()); |
| 332 | |
| 333 | cir::RecordType vtType = getVTableType(layout: *vtLayout); |
| 334 | |
| 335 | // Construction vtable symbols are not part of the Itanium ABI, so we cannot |
| 336 | // guarantee that they actually will be available externally. Instead, when |
| 337 | // emitting an available_externally VTT, we provide references to an internal |
| 338 | // linkage construction vtable. The ABI only requires complete-object vtables |
| 339 | // to be the same for all instances of a type, not construction vtables. |
| 340 | if (linkage == cir::GlobalLinkageKind::AvailableExternallyLinkage) |
| 341 | linkage = cir::GlobalLinkageKind::InternalLinkage; |
| 342 | |
| 343 | llvm::Align align = cgm.getDataLayout().getABITypeAlign(ty: vtType); |
| 344 | mlir::Location loc = cgm.getLoc(cRange: rd->getSourceRange()); |
| 345 | |
| 346 | // Create the variable that will hold the construction vtable. |
| 347 | cir::GlobalOp vtable = cgm.createOrReplaceCXXRuntimeVariable( |
| 348 | loc, name, ty: vtType, linkage, alignment: CharUnits::fromQuantity(Quantity: align)); |
| 349 | |
| 350 | // dynamic_cast relies on a unique vtable address, so the address only |
| 351 | // becomes insignificant without RTTI or when the vtable may be duplicated. |
| 352 | if (!cgm.shouldEmitRTTI() || cgm.mayVTableBeDuplicated(linkage: vtable.getLinkage())) |
| 353 | vtable.setUnnamedAddr(true); |
| 354 | |
| 355 | mlir::Attribute rtti = cgm.getAddrOfRTTIDescriptor( |
| 356 | loc, ty: cgm.getASTContext().getCanonicalTagType(TD: base.getBase())); |
| 357 | |
| 358 | // Create and set the initializer. |
| 359 | createVTableInitializer(vtableOp&: vtable, layout: *vtLayout, rtti, |
| 360 | vtableHasLocalLinkage: cir::isLocalLinkage(linkage: vtable.getLinkage())); |
| 361 | |
| 362 | // Set properties only after the initializer has been set to ensure that the |
| 363 | // GV is treated as definition and not declaration. |
| 364 | assert(!vtable.isDeclaration() && "Shouldn't set properties on declaration" ); |
| 365 | // The global was created as a declaration, which MLIR requires to be private, |
| 366 | // so the visibility has to follow the linkage of the definition. |
| 367 | mlir::SymbolTable::setSymbolVisibility( |
| 368 | symbol: vtable, vis: CIRGenModule::getMLIRVisibility(op: vtable)); |
| 369 | cgm.setGVProperties(op: vtable, d: rd); |
| 370 | |
| 371 | assert(!cir::MissingFeatures::vtableEmitMetadata()); |
| 372 | assert(!cir::MissingFeatures::vtableRelativeLayout()); |
| 373 | |
| 374 | return vtable; |
| 375 | } |
| 376 | |
| 377 | static bool shouldEmitAvailableExternallyVTable(const CIRGenModule &cgm, |
| 378 | const CXXRecordDecl *rd); |
| 379 | |
| 380 | /// Compute the required linkage of the vtable for the given class. |
| 381 | /// |
| 382 | /// Note that we only call this at the end of the translation unit. |
| 383 | cir::GlobalLinkageKind CIRGenModule::getVTableLinkage(const CXXRecordDecl *rd) { |
| 384 | if (!rd->isExternallyVisible()) |
| 385 | return cir::GlobalLinkageKind::InternalLinkage; |
| 386 | |
| 387 | // We're at the end of the translation unit, so the current key |
| 388 | // function is fully correct. |
| 389 | const CXXMethodDecl *keyFunction = astContext.getCurrentKeyFunction(RD: rd); |
| 390 | if (keyFunction && !rd->hasAttr<DLLImportAttr>()) { |
| 391 | // If this class has a key function, use that to determine the |
| 392 | // linkage of the vtable. |
| 393 | const FunctionDecl *def = nullptr; |
| 394 | if (keyFunction->hasBody(Definition&: def)) |
| 395 | keyFunction = cast<CXXMethodDecl>(Val: def); |
| 396 | |
| 397 | // All of the cases below do something different with AppleKext enabled. |
| 398 | assert(!cir::MissingFeatures::appleKext()); |
| 399 | switch (keyFunction->getTemplateSpecializationKind()) { |
| 400 | case TSK_Undeclared: |
| 401 | case TSK_ExplicitSpecialization: |
| 402 | assert( |
| 403 | (def || codeGenOpts.OptimizationLevel > 0 || |
| 404 | codeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo) && |
| 405 | "Shouldn't query vtable linkage without key function, " |
| 406 | "optimizations, or debug info" ); |
| 407 | if (!def && codeGenOpts.OptimizationLevel > 0) |
| 408 | return cir::GlobalLinkageKind::AvailableExternallyLinkage; |
| 409 | |
| 410 | if (keyFunction->isInlined()) |
| 411 | return !astContext.getLangOpts().AppleKext |
| 412 | ? cir::GlobalLinkageKind::LinkOnceODRLinkage |
| 413 | : cir::GlobalLinkageKind::InternalLinkage; |
| 414 | return cir::GlobalLinkageKind::ExternalLinkage; |
| 415 | |
| 416 | case TSK_ImplicitInstantiation: |
| 417 | return cir::GlobalLinkageKind::LinkOnceODRLinkage; |
| 418 | |
| 419 | case TSK_ExplicitInstantiationDefinition: |
| 420 | return cir::GlobalLinkageKind::WeakODRLinkage; |
| 421 | |
| 422 | case TSK_ExplicitInstantiationDeclaration: |
| 423 | return !def ? cir::GlobalLinkageKind::AvailableExternallyLinkage |
| 424 | : cir::GlobalLinkageKind::ExternalLinkage; |
| 425 | } |
| 426 | } |
| 427 | // -fapple-kext mode does not support weak linkage, so we must use |
| 428 | // internal linkage. |
| 429 | if (astContext.getLangOpts().AppleKext) |
| 430 | return cir::GlobalLinkageKind::InternalLinkage; |
| 431 | |
| 432 | auto discardableODRLinkage = cir::GlobalLinkageKind::LinkOnceODRLinkage; |
| 433 | auto nonDiscardableODRLinkage = cir::GlobalLinkageKind::WeakODRLinkage; |
| 434 | if (rd->hasAttr<DLLExportAttr>()) { |
| 435 | // Cannot discard exported vtables. |
| 436 | discardableODRLinkage = nonDiscardableODRLinkage; |
| 437 | } else if (rd->hasAttr<DLLImportAttr>()) { |
| 438 | // Imported vtables are available externally. |
| 439 | discardableODRLinkage = cir::GlobalLinkageKind::AvailableExternallyLinkage; |
| 440 | nonDiscardableODRLinkage = |
| 441 | cir::GlobalLinkageKind::AvailableExternallyLinkage; |
| 442 | } |
| 443 | |
| 444 | switch (rd->getTemplateSpecializationKind()) { |
| 445 | case TSK_Undeclared: |
| 446 | case TSK_ExplicitSpecialization: |
| 447 | case TSK_ImplicitInstantiation: |
| 448 | return discardableODRLinkage; |
| 449 | |
| 450 | case TSK_ExplicitInstantiationDeclaration: |
| 451 | // Explicit instantiations in MSVC do not provide vtables, so we must emit |
| 452 | // our own. |
| 453 | if (getTarget().getCXXABI().isMicrosoft()) |
| 454 | return discardableODRLinkage; |
| 455 | return shouldEmitAvailableExternallyVTable(cgm: *this, rd) |
| 456 | ? cir::GlobalLinkageKind::AvailableExternallyLinkage |
| 457 | : cir::GlobalLinkageKind::ExternalLinkage; |
| 458 | |
| 459 | case TSK_ExplicitInstantiationDefinition: |
| 460 | return nonDiscardableODRLinkage; |
| 461 | } |
| 462 | |
| 463 | llvm_unreachable("Invalid TemplateSpecializationKind!" ); |
| 464 | } |
| 465 | |
| 466 | cir::GlobalOp CIRGenVTables::getAddrOfVTT(const CXXRecordDecl *rd) { |
| 467 | assert(rd->getNumVBases() && "Only classes with virtual bases need a VTT" ); |
| 468 | |
| 469 | SmallString<256> outName; |
| 470 | llvm::raw_svector_ostream out(outName); |
| 471 | cast<ItaniumMangleContext>(Val&: cgm.getCXXABI().getMangleContext()) |
| 472 | .mangleCXXVTT(RD: rd, out); |
| 473 | StringRef name = outName.str(); |
| 474 | |
| 475 | // This will also defer the definition of the VTT. |
| 476 | (void)cgm.getCXXABI().getAddrOfVTable(rd, vptrOffset: CharUnits()); |
| 477 | |
| 478 | VTTBuilder builder(cgm.getASTContext(), rd, /*GenerateDefinition=*/false); |
| 479 | |
| 480 | auto arrayType = cir::ArrayType::get(elementType: cgm.getBuilder().getUInt8PtrTy(), |
| 481 | size: builder.getVTTComponents().size()); |
| 482 | llvm::Align align = |
| 483 | cgm.getDataLayout().getABITypeAlign(ty: cgm.getBuilder().getUInt8PtrTy()); |
| 484 | cir::GlobalOp vtt = cgm.createOrReplaceCXXRuntimeVariable( |
| 485 | loc: cgm.getLoc(cRange: rd->getSourceRange()), name, ty: arrayType, |
| 486 | linkage: cir::GlobalLinkageKind::ExternalLinkage, alignment: CharUnits::fromQuantity(Quantity: align)); |
| 487 | vtt.setUnnamedAddr(true); |
| 488 | cgm.setGVProperties(op: vtt, d: rd); |
| 489 | return vtt; |
| 490 | } |
| 491 | |
| 492 | static cir::GlobalOp |
| 493 | getAddrOfVTTVTable(CIRGenVTables &cgvt, CIRGenModule &cgm, |
| 494 | const CXXRecordDecl *mostDerivedClass, |
| 495 | const VTTVTable &vtable, cir::GlobalLinkageKind linkage, |
| 496 | VTableLayout::AddressPointsMapTy &addressPoints) { |
| 497 | if (vtable.getBase() == mostDerivedClass) { |
| 498 | assert(vtable.getBaseOffset().isZero() && |
| 499 | "Most derived class vtable must have a zero offset!" ); |
| 500 | // This is a regular vtable. |
| 501 | return cgm.getCXXABI().getAddrOfVTable(rd: mostDerivedClass, vptrOffset: CharUnits()); |
| 502 | } |
| 503 | return cgvt.generateConstructionVTable( |
| 504 | rd: mostDerivedClass, base: vtable.getBaseSubobject(), baseIsVirtual: vtable.isVirtual(), linkage, |
| 505 | addressPoints); |
| 506 | } |
| 507 | |
| 508 | /// Emit the definition of the given vtable. |
| 509 | void CIRGenVTables::emitVTTDefinition(cir::GlobalOp vttOp, |
| 510 | cir::GlobalLinkageKind linkage, |
| 511 | const CXXRecordDecl *rd) { |
| 512 | VTTBuilder builder(cgm.getASTContext(), rd, /*GenerateDefinition=*/true); |
| 513 | |
| 514 | mlir::MLIRContext *mlirContext = &cgm.getMLIRContext(); |
| 515 | |
| 516 | auto arrayType = cir::ArrayType::get(elementType: cgm.getBuilder().getUInt8PtrTy(), |
| 517 | size: builder.getVTTComponents().size()); |
| 518 | |
| 519 | SmallVector<cir::GlobalOp> vtables; |
| 520 | SmallVector<VTableAddressPointsMapTy> vtableAddressPoints; |
| 521 | for (const VTTVTable &vtt : builder.getVTTVTables()) { |
| 522 | vtableAddressPoints.push_back(Elt: VTableAddressPointsMapTy()); |
| 523 | vtables.push_back(Elt: getAddrOfVTTVTable(cgvt&: *this, cgm, mostDerivedClass: rd, vtable: vtt, linkage, |
| 524 | addressPoints&: vtableAddressPoints.back())); |
| 525 | } |
| 526 | |
| 527 | SmallVector<mlir::Attribute> vttComponents; |
| 528 | for (const VTTComponent &vttComponent : builder.getVTTComponents()) { |
| 529 | const VTTVTable &vttVT = builder.getVTTVTables()[vttComponent.VTableIndex]; |
| 530 | cir::GlobalOp vtable = vtables[vttComponent.VTableIndex]; |
| 531 | VTableLayout::AddressPointLocation addressPoint; |
| 532 | if (vttVT.getBase() == rd) { |
| 533 | // Just get the address point for the regular vtable. |
| 534 | addressPoint = |
| 535 | getItaniumVTableContext().getVTableLayout(RD: rd).getAddressPoint( |
| 536 | Base: vttComponent.VTableBase); |
| 537 | } else { |
| 538 | addressPoint = vtableAddressPoints[vttComponent.VTableIndex].lookup( |
| 539 | Val: vttComponent.VTableBase); |
| 540 | assert(addressPoint.AddressPointIndex != 0 && |
| 541 | "Did not find ctor vtable address point!" ); |
| 542 | } |
| 543 | |
| 544 | mlir::Attribute indices[2] = { |
| 545 | cgm.getBuilder().getI32IntegerAttr(value: addressPoint.VTableIndex), |
| 546 | cgm.getBuilder().getI32IntegerAttr(value: addressPoint.AddressPointIndex), |
| 547 | }; |
| 548 | |
| 549 | auto indicesAttr = mlir::ArrayAttr::get(context: mlirContext, value: indices); |
| 550 | CIRGenBuilderTy &bld = cgm.getBuilder(); |
| 551 | auto init = bld.getGlobalViewAttr(type: bld.getUInt8PtrTy(), globalOp: vtable, indices: indicesAttr, |
| 552 | /*addressPoint=*/true); |
| 553 | |
| 554 | vttComponents.push_back(Elt: init); |
| 555 | } |
| 556 | |
| 557 | auto init = cir::ConstArrayAttr::get( |
| 558 | type: arrayType, elts: mlir::ArrayAttr::get(context: mlirContext, value: vttComponents)); |
| 559 | |
| 560 | vttOp.setInitialValueAttr(init); |
| 561 | |
| 562 | // Set the correct linkage. |
| 563 | vttOp.setLinkage(linkage); |
| 564 | mlir::SymbolTable::setSymbolVisibility( |
| 565 | symbol: vttOp, vis: CIRGenModule::getMLIRVisibility(op: vttOp)); |
| 566 | |
| 567 | if (cgm.supportsCOMDAT() && vttOp.isWeakForLinker()) |
| 568 | vttOp.setSelfComdat(); |
| 569 | } |
| 570 | |
| 571 | uint64_t CIRGenVTables::getSubVTTIndex(const CXXRecordDecl *rd, |
| 572 | BaseSubobject base) { |
| 573 | BaseSubobjectPairTy classSubobjectPair(rd, base); |
| 574 | |
| 575 | SubVTTIndiciesMapTy::iterator it = subVTTIndicies.find(Val: classSubobjectPair); |
| 576 | if (it != subVTTIndicies.end()) |
| 577 | return it->second; |
| 578 | |
| 579 | VTTBuilder builder(cgm.getASTContext(), rd, /*GenerateDefinition=*/false); |
| 580 | |
| 581 | for (const auto &entry : builder.getSubVTTIndices()) { |
| 582 | // Insert all indices. |
| 583 | BaseSubobjectPairTy subclassSubobjectPair(rd, entry.first); |
| 584 | |
| 585 | subVTTIndicies.insert(KV: std::make_pair(x&: subclassSubobjectPair, y: entry.second)); |
| 586 | } |
| 587 | |
| 588 | it = subVTTIndicies.find(Val: classSubobjectPair); |
| 589 | assert(it != subVTTIndicies.end() && "Did not find index!" ); |
| 590 | |
| 591 | return it->second; |
| 592 | } |
| 593 | |
| 594 | uint64_t CIRGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *rd, |
| 595 | BaseSubobject base) { |
| 596 | auto it = secondaryVirtualPointerIndices.find(Val: std::make_pair(x&: rd, y&: base)); |
| 597 | |
| 598 | if (it != secondaryVirtualPointerIndices.end()) |
| 599 | return it->second; |
| 600 | |
| 601 | VTTBuilder builder(cgm.getASTContext(), rd, /*GenerateDefinition=*/false); |
| 602 | |
| 603 | // Insert all secondary vpointer indices. |
| 604 | for (const auto &entry : builder.getSecondaryVirtualPointerIndices()) { |
| 605 | std::pair<const CXXRecordDecl *, BaseSubobject> pair = |
| 606 | std::make_pair(x&: rd, y: entry.first); |
| 607 | |
| 608 | secondaryVirtualPointerIndices.insert(KV: std::make_pair(x&: pair, y: entry.second)); |
| 609 | } |
| 610 | |
| 611 | it = secondaryVirtualPointerIndices.find(Val: std::make_pair(x&: rd, y&: base)); |
| 612 | assert(it != secondaryVirtualPointerIndices.end() && "Did not find index!" ); |
| 613 | |
| 614 | return it->second; |
| 615 | } |
| 616 | |
| 617 | static RValue performReturnAdjustment(CIRGenFunction &cgf, QualType resultType, |
| 618 | RValue rv, const ThunkInfo &thunk) { |
| 619 | // Emit the return adjustment. For non-reference pointer returns, match |
| 620 | // classic codegen: skip the adjustment when the returned pointer is null. |
| 621 | bool nullCheckValue = !resultType->isReferenceType(); |
| 622 | mlir::Value returnValue = rv.getValue(); |
| 623 | |
| 624 | const CXXRecordDecl *classDecl = |
| 625 | resultType->getPointeeType()->getAsCXXRecordDecl(); |
| 626 | CharUnits classAlign = cgf.cgm.getClassPointerAlignment(rd: classDecl); |
| 627 | mlir::Type pointeeType = cgf.convertTypeForMem(t: resultType->getPointeeType()); |
| 628 | CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 629 | mlir::Location loc = returnValue.getLoc(); |
| 630 | |
| 631 | if (!nullCheckValue) { |
| 632 | returnValue = cgf.cgm.getCXXABI().performReturnAdjustment( |
| 633 | cgf, ret: Address(returnValue, pointeeType, classAlign), unadjustedClass: classDecl, |
| 634 | ra: thunk.Return); |
| 635 | return RValue::get(v: returnValue); |
| 636 | } |
| 637 | |
| 638 | mlir::Value isNotNull = builder.createPtrIsNotNull(ptr: returnValue); |
| 639 | returnValue = |
| 640 | cir::TernaryOp::create( |
| 641 | builder, location: loc, cond: isNotNull, |
| 642 | trueBuilder: [&](mlir::OpBuilder &, mlir::Location) { |
| 643 | mlir::Value adjusted = cgf.cgm.getCXXABI().performReturnAdjustment( |
| 644 | cgf, ret: Address(returnValue, pointeeType, classAlign), unadjustedClass: classDecl, |
| 645 | ra: thunk.Return); |
| 646 | builder.createYield(loc, value: adjusted); |
| 647 | }, |
| 648 | falseBuilder: [&](mlir::OpBuilder &, mlir::Location) { |
| 649 | mlir::Value nullVal = |
| 650 | builder.getNullPtr(ty: returnValue.getType(), loc).getResult(); |
| 651 | builder.createYield(loc, value: nullVal); |
| 652 | }) |
| 653 | .getResult(); |
| 654 | |
| 655 | return RValue::get(v: returnValue); |
| 656 | } |
| 657 | |
| 658 | void CIRGenFunction::startThunk(cir::FuncOp fn, GlobalDecl gd, |
| 659 | const CIRGenFunctionInfo &fnInfo, |
| 660 | bool isUnprototyped) { |
| 661 | assert(!curGD.getDecl() && "curGD was already set!" ); |
| 662 | curGD = gd; |
| 663 | curFuncIsThunk = true; |
| 664 | |
| 665 | // Build FunctionArgs. |
| 666 | const CXXMethodDecl *md = cast<CXXMethodDecl>(Val: gd.getDecl()); |
| 667 | QualType thisType = md->getThisType(); |
| 668 | QualType resultType; |
| 669 | if (isUnprototyped) |
| 670 | resultType = cgm.getASTContext().VoidTy; |
| 671 | else if (cgm.getCXXABI().hasThisReturn(gd)) |
| 672 | resultType = thisType; |
| 673 | else if (cgm.getCXXABI().hasMostDerivedReturn(gd)) |
| 674 | resultType = cgm.getASTContext().VoidPtrTy; |
| 675 | else |
| 676 | resultType = md->getType()->castAs<FunctionProtoType>()->getReturnType(); |
| 677 | FunctionArgList functionArgs; |
| 678 | |
| 679 | // Create the implicit 'this' parameter declaration. |
| 680 | cgm.getCXXABI().buildThisParam(cgf&: *this, params&: functionArgs); |
| 681 | |
| 682 | // Add the rest of the parameters, if we have a prototype to work with. |
| 683 | if (!isUnprototyped) { |
| 684 | functionArgs.append(in_start: md->param_begin(), in_end: md->param_end()); |
| 685 | |
| 686 | if (isa<CXXDestructorDecl>(Val: md)) |
| 687 | cgm.getCXXABI().addImplicitStructorParams(cgf&: *this, resTy&: resultType, |
| 688 | params&: functionArgs); |
| 689 | } |
| 690 | |
| 691 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 692 | |
| 693 | // Start defining the function. |
| 694 | cir::FuncType funcType = cgm.getTypes().getFunctionType(info: fnInfo); |
| 695 | startFunction(gd: GlobalDecl(), returnType: resultType, fn, funcType, args: functionArgs, |
| 696 | loc: md->getLocation(), startLoc: md->getLocation()); |
| 697 | // TODO(cir): Move this into startFunction. |
| 698 | curFnInfo = &fnInfo; |
| 699 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 700 | |
| 701 | // Since we didn't pass a GlobalDecl to startFunction, do this ourselves. |
| 702 | cgm.getCXXABI().emitInstanceFunctionProlog(loc: md->getLocation(), cgf&: *this); |
| 703 | cxxThisValue = cxxabiThisValue; |
| 704 | curCodeDecl = md; |
| 705 | curFuncDecl = md; |
| 706 | } |
| 707 | |
| 708 | void CIRGenFunction::finishThunk() { |
| 709 | // Clear these to restore the invariants expected by |
| 710 | // startFunction/finishFunction. |
| 711 | curCodeDecl = nullptr; |
| 712 | curFuncDecl = nullptr; |
| 713 | |
| 714 | finishFunction(endLoc: SourceLocation()); |
| 715 | } |
| 716 | |
| 717 | void CIRGenFunction::emitCallAndReturnForThunk(cir::FuncOp callee, |
| 718 | SourceRange fnLoc, |
| 719 | const ThunkInfo *thunk, |
| 720 | bool isUnprototyped) { |
| 721 | assert(isa<CXXMethodDecl>(curGD.getDecl()) && |
| 722 | "Please use a new CGF for this thunk" ); |
| 723 | const CXXMethodDecl *md = cast<CXXMethodDecl>(Val: curGD.getDecl()); |
| 724 | |
| 725 | // Determine the this pointer class (may differ from md's class for thunks). |
| 726 | const CXXRecordDecl *thisValueClass = |
| 727 | md->getThisType()->getPointeeCXXRecordDecl(); |
| 728 | if (thunk) |
| 729 | thisValueClass = thunk->ThisType->getPointeeCXXRecordDecl(); |
| 730 | |
| 731 | mlir::Value adjustedThisPtr = |
| 732 | thunk ? cgm.getCXXABI().performThisAdjustment(cgf&: *this, thisAddr: loadCXXThisAddress(), |
| 733 | unadjustedClass: thisValueClass, ti: *thunk) |
| 734 | : loadCXXThis(); |
| 735 | |
| 736 | // If perfect forwarding is required a variadic method, a method using |
| 737 | // inalloca, or an unprototyped thunk, use musttail. Emit an error if this |
| 738 | // thunk requires a return adjustment, since that is impossible with musttail. |
| 739 | assert(!cir::MissingFeatures::opCallInAlloca()); |
| 740 | if ((curFnInfo && curFnInfo->isVariadic()) || isUnprototyped) { |
| 741 | // Error if return adjustment is needed (can't do with musttail). |
| 742 | if (thunk && !thunk->Return.isEmpty()) { |
| 743 | if (isUnprototyped) |
| 744 | cgm.errorUnsupported( |
| 745 | d: md, type: "return-adjusting thunk with incomplete parameter type" ); |
| 746 | else if (curFnInfo && curFnInfo->isVariadic()) |
| 747 | llvm_unreachable("shouldn't try to emit musttail return-adjusting " |
| 748 | "thunks for variadic functions" ); |
| 749 | else |
| 750 | cgm.errorUnsupported( |
| 751 | d: md, type: "non-trivial argument copy for return-adjusting thunk" ); |
| 752 | } |
| 753 | emitMustTailThunk(gd: curGD, adjustedThisPtr, callee); |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | // Build the call argument list. |
| 758 | CallArgList callArgs; |
| 759 | QualType thisType = md->getThisType(); |
| 760 | callArgs.add(rvalue: RValue::get(v: adjustedThisPtr), type: thisType); |
| 761 | |
| 762 | if (isa<CXXDestructorDecl>(Val: md)) |
| 763 | cgm.getCXXABI().adjustCallArgsForDestructorThunk(cgf&: *this, globalDecl: curGD, callArgs); |
| 764 | |
| 765 | #ifndef NDEBUG |
| 766 | unsigned prefixArgs = callArgs.size() - 1; |
| 767 | #endif |
| 768 | |
| 769 | // Add the rest of the method parameters. |
| 770 | for (const ParmVarDecl *pd : md->parameters()) |
| 771 | emitDelegateCallArg(args&: callArgs, param: pd, loc: SourceLocation()); |
| 772 | |
| 773 | const FunctionProtoType *fpt = md->getType()->castAs<FunctionProtoType>(); |
| 774 | |
| 775 | #ifndef NDEBUG |
| 776 | const CIRGenFunctionInfo &callFnInfo = cgm.getTypes().arrangeCXXMethodCall( |
| 777 | callArgs, fpt, RequiredArgs::getFromProtoWithExtraSlots(fpt, 1), |
| 778 | prefixArgs); |
| 779 | assert(callFnInfo.argTypeSize() == curFnInfo->argTypeSize()); |
| 780 | #endif |
| 781 | |
| 782 | // Determine whether we have a return value slot to use. |
| 783 | QualType resultType = cgm.getCXXABI().hasThisReturn(gd: curGD) ? thisType |
| 784 | : cgm.getCXXABI().hasMostDerivedReturn(gd: curGD) |
| 785 | ? cgm.getASTContext().VoidPtrTy |
| 786 | : fpt->getReturnType(); |
| 787 | |
| 788 | ReturnValueSlot slot; |
| 789 | // This should also be tracking volatile, unused, and externally destructed. |
| 790 | assert(!cir::MissingFeatures::returnValueSlotFeatures()); |
| 791 | if (!resultType->isVoidType() && hasAggregateEvaluationKind(type: resultType)) |
| 792 | slot = ReturnValueSlot(returnValue); |
| 793 | |
| 794 | // Now emit our call. |
| 795 | CIRGenCallee cirCallee = CIRGenCallee::forDirect(funcPtr: callee, abstractInfo: curGD); |
| 796 | mlir::Location loc = builder.getUnknownLoc(); |
| 797 | RValue rv = emitCall(funcInfo: *curFnInfo, callee: cirCallee, returnValue: slot, args: callArgs, |
| 798 | /*callOrTryCall=*/callOp: nullptr, /*isMustTail=*/false, clangLoc: fnLoc); |
| 799 | |
| 800 | // Consider return adjustment if we have ThunkInfo. |
| 801 | if (thunk && !thunk->Return.isEmpty()) |
| 802 | rv = performReturnAdjustment(cgf&: *this, resultType, rv, thunk: *thunk); |
| 803 | else |
| 804 | assert(!cir::MissingFeatures::opCallThunkTailHint()); |
| 805 | |
| 806 | // Emit return. For aggregate returns the call has already written the |
| 807 | // result through the slot bound to returnValue above; emit the |
| 808 | // corresponding load+return here rather than leaving the function to |
| 809 | // fall off the end and have LexicalScope::emitImplicitReturn drop a |
| 810 | // `cir.trap` / `cir.unreachable` in its place (which would silently |
| 811 | // discard the result we just stored). |
| 812 | if (!resultType->isVoidType()) { |
| 813 | if (slot.isNull()) |
| 814 | cgm.getCXXABI().emitReturnFromThunk(cgf&: *this, rv, resultType); |
| 815 | else |
| 816 | emitReturnOfRValue(loc, rv, ty: resultType); |
| 817 | } |
| 818 | |
| 819 | // Disable final ARC autorelease. |
| 820 | assert(!cir::MissingFeatures::objCLifetime()); |
| 821 | |
| 822 | finishThunk(); |
| 823 | } |
| 824 | |
| 825 | void CIRGenFunction::emitMustTailThunk(GlobalDecl gd, |
| 826 | mlir::Value adjustedThisPtr, |
| 827 | cir::FuncOp callee) { |
| 828 | // Forward all function arguments, replacing 'this' with the adjusted pointer. |
| 829 | // The call is marked musttail so varargs are forwarded correctly. |
| 830 | mlir::Block *entryBlock = getCurFunctionEntryBlock(); |
| 831 | SmallVector<mlir::Value> args; |
| 832 | for (mlir::BlockArgument arg : entryBlock->getArguments()) |
| 833 | args.push_back(Elt: arg); |
| 834 | |
| 835 | // Replace the 'this' argument (first arg) with the adjusted pointer. |
| 836 | assert(!args.empty() && "thunk must have at least 'this' argument" ); |
| 837 | if (adjustedThisPtr.getType() != args[0].getType()) |
| 838 | adjustedThisPtr = builder.createBitcast(src: adjustedThisPtr, newTy: args[0].getType()); |
| 839 | args[0] = adjustedThisPtr; |
| 840 | |
| 841 | mlir::Location loc = curFn->getLoc(); |
| 842 | cir::FuncType calleeTy = callee.getFunctionType(); |
| 843 | mlir::Type retTy = calleeTy.getReturnType(); |
| 844 | |
| 845 | assert(!cir::MissingFeatures::opCallAttrs()); |
| 846 | cir::CallOp call = |
| 847 | builder.createCallOp(loc, callee, operands: args, /*attrs=*/{}, /*argAttrs=*/{}, |
| 848 | /*resAttrs=*/{}, callingConv: curFnInfo->getCallingConvention()); |
| 849 | call->setAttr(name: cir::CIRDialect::getMustTailAttrName(), |
| 850 | value: mlir::UnitAttr::get(context: builder.getContext())); |
| 851 | |
| 852 | if (isa<cir::VoidType>(Val: retTy)) |
| 853 | cir::ReturnOp::create(builder, location: loc); |
| 854 | else |
| 855 | cir::ReturnOp::create(builder, location: loc, input: call->getResult(idx: 0)); |
| 856 | |
| 857 | finishThunk(); |
| 858 | } |
| 859 | |
| 860 | void CIRGenFunction::generateThunk(cir::FuncOp fn, SourceRange fnLoc, |
| 861 | const CIRGenFunctionInfo &fnInfo, |
| 862 | GlobalDecl gd, const ThunkInfo &thunk, |
| 863 | bool isUnprototyped) { |
| 864 | // Create entry block and set up the builder's insertion point. |
| 865 | // This must be done before calling startThunk() which calls startFunction(). |
| 866 | assert(fn.isDeclaration() && "Function already has body?" ); |
| 867 | mlir::Block *entryBb = fn.addEntryBlock(); |
| 868 | builder.setInsertionPointToStart(entryBb); |
| 869 | |
| 870 | // Create a scope in the symbol table to hold variable declarations. |
| 871 | // This is required before startFunction processes parameters, as it will |
| 872 | // insert them into the symbolTable (ScopedHashTable) which requires an |
| 873 | // active scope. |
| 874 | SymTableScopeTy varScope(symbolTable); |
| 875 | |
| 876 | // Create lexical scope - must stay alive for entire thunk generation. |
| 877 | // startFunction() requires currLexScope to be set. |
| 878 | SourceLocRAIIObject locRAII(*this, fnLoc); |
| 879 | LexicalScope lexScope{*this, fn.getLoc(), entryBb}; |
| 880 | |
| 881 | startThunk(fn, gd, fnInfo, isUnprototyped); |
| 882 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 883 | |
| 884 | // Get our callee. Use a placeholder type if this method is unprototyped so |
| 885 | // that CIRGenModule doesn't try to set attributes. |
| 886 | mlir::Type ty; |
| 887 | if (isUnprototyped) |
| 888 | cgm.errorNYI(feature: "unprototyped thunk placeholder type" ); |
| 889 | else |
| 890 | ty = cgm.getTypes().getFunctionType(info: fnInfo); |
| 891 | |
| 892 | cir::FuncOp calleeOp = cgm.getAddrOfFunction(gd, funcType: ty, /*forVTable=*/true); |
| 893 | |
| 894 | // Make the call and return the result. |
| 895 | emitCallAndReturnForThunk(callee: calleeOp, fnLoc, thunk: &thunk, isUnprototyped); |
| 896 | } |
| 897 | |
| 898 | static bool shouldEmitVTableThunk(CIRGenModule &cgm, const CXXMethodDecl *md, |
| 899 | bool isUnprototyped, bool forVTable) { |
| 900 | // Always emit thunks in the MS C++ ABI. We cannot rely on other TUs to |
| 901 | // provide thunks for us. |
| 902 | if (cgm.getTarget().getCXXABI().isMicrosoft()) |
| 903 | return true; |
| 904 | |
| 905 | // In the Itanium C++ ABI, vtable thunks are provided by TUs that provide |
| 906 | // definitions of the main method. Therefore, emitting thunks with the vtable |
| 907 | // is purely an optimization. Emit the thunk if optimizations are enabled and |
| 908 | // all of the parameter types are complete. |
| 909 | if (forVTable) |
| 910 | return cgm.getCodeGenOpts().OptimizationLevel && !isUnprototyped; |
| 911 | |
| 912 | // Always emit thunks along with the method definition. |
| 913 | return true; |
| 914 | } |
| 915 | |
| 916 | cir::FuncOp CIRGenVTables::maybeEmitThunk(GlobalDecl gd, |
| 917 | const ThunkInfo &thunkAdjustments, |
| 918 | bool forVTable) { |
| 919 | const CXXMethodDecl *md = cast<CXXMethodDecl>(Val: gd.getDecl()); |
| 920 | SmallString<256> name; |
| 921 | MangleContext &mCtx = cgm.getCXXABI().getMangleContext(); |
| 922 | |
| 923 | llvm::raw_svector_ostream out(name); |
| 924 | if (const CXXDestructorDecl *dd = dyn_cast<CXXDestructorDecl>(Val: md)) { |
| 925 | mCtx.mangleCXXDtorThunk(DD: dd, Type: gd.getDtorType(), Thunk: thunkAdjustments, |
| 926 | /*elideOverrideInfo=*/ElideOverrideInfo: false, out); |
| 927 | } else { |
| 928 | mCtx.mangleThunk(MD: md, Thunk: thunkAdjustments, /*elideOverrideInfo=*/ElideOverrideInfo: false, out); |
| 929 | } |
| 930 | |
| 931 | if (cgm.getASTContext().useAbbreviatedThunkName(VirtualMethodDecl: gd, MangledName: name.str())) { |
| 932 | name = "" ; |
| 933 | if (const CXXDestructorDecl *dd = dyn_cast<CXXDestructorDecl>(Val: md)) |
| 934 | mCtx.mangleCXXDtorThunk(DD: dd, Type: gd.getDtorType(), Thunk: thunkAdjustments, |
| 935 | /*elideOverrideInfo=*/ElideOverrideInfo: true, out); |
| 936 | else |
| 937 | mCtx.mangleThunk(MD: md, Thunk: thunkAdjustments, /*elideOverrideInfo=*/ElideOverrideInfo: true, out); |
| 938 | } |
| 939 | |
| 940 | cir::FuncType thunkVTableTy = cgm.getTypes().getFunctionType(gd); |
| 941 | cir::FuncOp thunk = cgm.getAddrOfThunk(name, fnTy: thunkVTableTy, gd); |
| 942 | |
| 943 | // If we don't need to emit a definition, return this declaration as is. |
| 944 | bool isUnprototyped = !cgm.getTypes().isFuncTypeConvertible( |
| 945 | ft: md->getType()->castAs<FunctionType>()); |
| 946 | if (!shouldEmitVTableThunk(cgm, md, isUnprototyped, forVTable)) |
| 947 | return thunk; |
| 948 | |
| 949 | // Arrange a function prototype appropriate for a function definition. In some |
| 950 | // cases in the MS ABI, we may need to build an unprototyped musttail thunk. |
| 951 | const CIRGenFunctionInfo &fnInfo = |
| 952 | isUnprototyped ? (cgm.errorNYI(feature: "unprototyped must-tail thunk" ), |
| 953 | cgm.getTypes().arrangeGlobalDeclaration(gd)) |
| 954 | : cgm.getTypes().arrangeGlobalDeclaration(gd); |
| 955 | cir::FuncType thunkFnTy = cgm.getTypes().getFunctionType(info: fnInfo); |
| 956 | |
| 957 | // This is to replace OG's casting to a function, keeping it here to |
| 958 | // streamline the 1-to-1 mapping from OG starting below. |
| 959 | cir::FuncOp thunkFn = thunk; |
| 960 | if (thunk.getFunctionType() != thunkFnTy) { |
| 961 | cir::FuncOp oldThunkFn = thunkFn; |
| 962 | |
| 963 | assert(oldThunkFn.isDeclaration() && "Shouldn't replace non-declaration" ); |
| 964 | |
| 965 | // Remove the name from the old thunk function and get a new thunk. |
| 966 | cgm.eraseGlobalSymbol(op: oldThunkFn); |
| 967 | oldThunkFn.setName(StringRef()); |
| 968 | thunkFn = |
| 969 | cir::FuncOp::create(builder&: cgm.getBuilder(), location: thunk->getLoc(), sym_name: name.str(), |
| 970 | type: thunkFnTy, linkage: cir::GlobalLinkageKind::ExternalLinkage); |
| 971 | cgm.insertGlobalSymbol(op: thunkFn); |
| 972 | cgm.setCIRFunctionAttributes(gd: md, info: fnInfo, func: thunkFn, /*isThunk=*/false); |
| 973 | |
| 974 | if (!oldThunkFn->use_empty()) |
| 975 | oldThunkFn->replaceAllUsesWith(values&: thunkFn); |
| 976 | |
| 977 | // Remove the old thunk. |
| 978 | oldThunkFn->erase(); |
| 979 | } |
| 980 | |
| 981 | bool abiHasKeyFunctions = cgm.getTarget().getCXXABI().hasKeyFunctions(); |
| 982 | bool useAvailableExternallyLinkage = forVTable && abiHasKeyFunctions; |
| 983 | |
| 984 | // If the type of the underlying GlobalValue is wrong, we'll have to replace |
| 985 | // it. It should be a declaration. |
| 986 | if (!thunkFn.isDeclaration()) { |
| 987 | if (!abiHasKeyFunctions || useAvailableExternallyLinkage) { |
| 988 | // There is already a thunk emitted for this function, do nothing. |
| 989 | return thunkFn; |
| 990 | } |
| 991 | |
| 992 | setThunkProperties(cgm, thunk: thunkAdjustments, thunkFn, forVTable, gd); |
| 993 | return thunkFn; |
| 994 | } |
| 995 | |
| 996 | // TODO(cir): Add "thunk" attribute if unprototyped. |
| 997 | |
| 998 | cgm.setCIRFunctionAttributesForDefinition(decl: cast<FunctionDecl>(Val: gd.getDecl()), |
| 999 | f: thunkFn); |
| 1000 | |
| 1001 | // Thunks for variadic methods are special because in general variadic |
| 1002 | // arguments cannot be perfectly forwarded. In the general case, clang |
| 1003 | // implements such thunks by cloning the original function body. However, for |
| 1004 | // thunks with no return adjustment on targets that support musttail, we can |
| 1005 | // use musttail to perfectly forward the variadic arguments. |
| 1006 | bool shouldCloneVarArgs = false; |
| 1007 | if (!isUnprototyped && thunkFn.getFunctionType().isVarArg()) { |
| 1008 | shouldCloneVarArgs = true; |
| 1009 | if (thunkAdjustments.Return.isEmpty()) { |
| 1010 | switch (cgm.getTriple().getArch()) { |
| 1011 | case llvm::Triple::x86_64: |
| 1012 | case llvm::Triple::x86: |
| 1013 | case llvm::Triple::aarch64: |
| 1014 | shouldCloneVarArgs = false; |
| 1015 | break; |
| 1016 | default: |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | if (shouldCloneVarArgs) { |
| 1023 | if (useAvailableExternallyLinkage) |
| 1024 | return thunkFn; |
| 1025 | cgm.errorNYI(feature: "varargs thunk cloning" ); |
| 1026 | } else { |
| 1027 | // Normal thunk body generation. |
| 1028 | mlir::OpBuilder::InsertionGuard guard(cgm.getBuilder()); |
| 1029 | CIRGenFunction cgf(cgm, cgm.getBuilder()); |
| 1030 | cgf.generateThunk(fn: thunkFn, fnLoc: md->getSourceRange(), fnInfo, gd, |
| 1031 | thunk: thunkAdjustments, isUnprototyped); |
| 1032 | } |
| 1033 | |
| 1034 | setThunkProperties(cgm, thunk: thunkAdjustments, thunkFn, forVTable, gd); |
| 1035 | return thunkFn; |
| 1036 | } |
| 1037 | |
| 1038 | void CIRGenVTables::emitThunks(GlobalDecl gd) { |
| 1039 | const CXXMethodDecl *md = |
| 1040 | cast<CXXMethodDecl>(Val: gd.getDecl())->getCanonicalDecl(); |
| 1041 | |
| 1042 | // We don't need to generate thunks for the base destructor. |
| 1043 | if (isa<CXXDestructorDecl>(Val: md) && gd.getDtorType() == Dtor_Base) |
| 1044 | return; |
| 1045 | |
| 1046 | const VTableContextBase::ThunkInfoVectorTy *thunkInfoVector = |
| 1047 | vtContext->getThunkInfo(GD: gd); |
| 1048 | |
| 1049 | if (!thunkInfoVector) |
| 1050 | return; |
| 1051 | |
| 1052 | for (const ThunkInfo &thunk : *thunkInfoVector) |
| 1053 | maybeEmitThunk(gd, thunkAdjustments: thunk, /*forVTable=*/false); |
| 1054 | } |
| 1055 | |
| 1056 | static bool shouldEmitAvailableExternallyVTable(const CIRGenModule &cgm, |
| 1057 | const CXXRecordDecl *rd) { |
| 1058 | return cgm.getCodeGenOpts().OptimizationLevel > 0 && |
| 1059 | cgm.getCXXABI().canSpeculativelyEmitVTable(RD: rd); |
| 1060 | } |
| 1061 | |
| 1062 | /// Given that we're currently at the end of the translation unit, and |
| 1063 | /// we've emitted a reference to the vtable for this class, should |
| 1064 | /// we define that vtable? |
| 1065 | static bool shouldEmitVTableAtEndOfTranslationUnit(CIRGenModule &cgm, |
| 1066 | const CXXRecordDecl *rd) { |
| 1067 | // If vtable is internal then it has to be done. |
| 1068 | if (!cgm.getVTables().isVTableExternal(rd)) |
| 1069 | return true; |
| 1070 | |
| 1071 | // If it's external then maybe we will need it as available_externally. |
| 1072 | return shouldEmitAvailableExternallyVTable(cgm, rd); |
| 1073 | } |
| 1074 | |
| 1075 | /// Given that at some point we emitted a reference to one or more |
| 1076 | /// vtables, and that we are now at the end of the translation unit, |
| 1077 | /// decide whether we should emit them. |
| 1078 | void CIRGenModule::emitDeferredVTables() { |
| 1079 | #ifndef NDEBUG |
| 1080 | // Remember the size of DeferredVTables, because we're going to assume |
| 1081 | // that this entire operation doesn't modify it. |
| 1082 | size_t savedSize = deferredVTables.size(); |
| 1083 | #endif |
| 1084 | for (const CXXRecordDecl *rd : deferredVTables) { |
| 1085 | if (shouldEmitVTableAtEndOfTranslationUnit(cgm&: *this, rd)) |
| 1086 | vtables.generateClassData(rd); |
| 1087 | else if (shouldOpportunisticallyEmitVTables()) |
| 1088 | opportunisticVTables.push_back(x: rd); |
| 1089 | } |
| 1090 | |
| 1091 | assert(savedSize == deferredVTables.size() && |
| 1092 | "deferred extra vtables during vtable emission?" ); |
| 1093 | deferredVTables.clear(); |
| 1094 | } |
| 1095 | |
| 1096 | void CIRGenModule::emitVTablesOpportunistically() { |
| 1097 | // Try to emit external vtables as available_externally if they have emitted |
| 1098 | // all inlined virtual functions. It runs after EmitDeferred() and therefore |
| 1099 | // is not allowed to create new references to things that need to be emitted |
| 1100 | // lazily. Note that it also uses fact that we eagerly emitting RTTI. |
| 1101 | |
| 1102 | assert( |
| 1103 | (opportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) && |
| 1104 | "Only emit opportunistic vtables with optimizations" ); |
| 1105 | |
| 1106 | for (const CXXRecordDecl *rd : opportunisticVTables) { |
| 1107 | assert(getVTables().isVTableExternal(rd) && |
| 1108 | "This queue should only contain external vtables" ); |
| 1109 | if (getCXXABI().canSpeculativelyEmitVTable(RD: rd)) |
| 1110 | vtables.generateClassData(rd); |
| 1111 | } |
| 1112 | opportunisticVTables.clear(); |
| 1113 | } |
| 1114 | |
| 1115 | bool CIRGenModule::shouldOpportunisticallyEmitVTables() { |
| 1116 | return codeGenOpts.OptimizationLevel > 0; |
| 1117 | } |
| 1118 | |
| 1119 | bool CIRGenModule::mayVTableBeDuplicated(cir::GlobalLinkageKind linkage) const { |
| 1120 | return getTarget().getVTableUniqueness() == |
| 1121 | VTableUniquenessKind::UniqueIfStrongLinkage && |
| 1122 | cir::isWeakForLinker(linkage); |
| 1123 | } |
| 1124 | |