diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2023-09-02 21:17:18 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2023-12-08 17:34:50 +0000 |
commit | 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e (patch) | |
tree | 62f873df87c7c675557a179e0c4c83fe9f3087bc /contrib/llvm-project/clang/lib/CodeGen/CGVTables.cpp | |
parent | cf037972ea8863e2bab7461d77345367d2c1e054 (diff) | |
parent | 7fa27ce4a07f19b07799a767fc29416f3b625afb (diff) |
Diffstat (limited to 'contrib/llvm-project/clang/lib/CodeGen/CGVTables.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/CodeGen/CGVTables.cpp | 83 |
1 files changed, 50 insertions, 33 deletions
diff --git a/contrib/llvm-project/clang/lib/CodeGen/CGVTables.cpp b/contrib/llvm-project/clang/lib/CodeGen/CGVTables.cpp index a0b5d9e4b096..91dd7a8e046b 100644 --- a/contrib/llvm-project/clang/lib/CodeGen/CGVTables.cpp +++ b/contrib/llvm-project/clang/lib/CodeGen/CGVTables.cpp @@ -690,7 +690,7 @@ bool CodeGenVTables::useRelativeLayout() const { llvm::Type *CodeGenModule::getVTableComponentType() const { if (UseRelativeLayout(*this)) return Int32Ty; - return Int8PtrTy; + return GlobalsInt8PtrTy; } llvm::Type *CodeGenVTables::getVTableComponentType() const { @@ -702,7 +702,7 @@ static void AddPointerLayoutOffset(const CodeGenModule &CGM, CharUnits offset) { builder.add(llvm::ConstantExpr::getIntToPtr( llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()), - CGM.Int8PtrTy)); + CGM.GlobalsInt8PtrTy)); } static void AddRelativeLayoutOffset(const CodeGenModule &CGM, @@ -739,7 +739,7 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder, vtableHasLocalLinkage, /*isCompleteDtor=*/false); else - return builder.add(llvm::ConstantExpr::getBitCast(rtti, CGM.Int8PtrTy)); + return builder.add(rtti); case VTableComponent::CK_FunctionPointer: case VTableComponent::CK_CompleteDtorPointer: @@ -758,7 +758,8 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder, ? MD->hasAttr<CUDADeviceAttr>() : (MD->hasAttr<CUDAHostAttr>() || !MD->hasAttr<CUDADeviceAttr>()); if (!CanEmitMethod) - return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int8PtrTy)); + return builder.add( + llvm::ConstantExpr::getNullValue(CGM.GlobalsInt8PtrTy)); // Method is acceptable, continue processing as usual. } @@ -771,20 +772,20 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder, // with the local symbol. As a temporary solution, fill these components // with zero. We shouldn't be calling these in the first place anyway. if (useRelativeLayout()) - return llvm::ConstantPointerNull::get(CGM.Int8PtrTy); + return llvm::ConstantPointerNull::get(CGM.GlobalsInt8PtrTy); // For NVPTX devices in OpenMP emit special functon as null pointers, // otherwise linking ends up with unresolved references. - if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPIsDevice && + if (CGM.getLangOpts().OpenMP && CGM.getLangOpts().OpenMPIsTargetDevice && CGM.getTriple().isNVPTX()) - return llvm::ConstantPointerNull::get(CGM.Int8PtrTy); + return llvm::ConstantPointerNull::get(CGM.GlobalsInt8PtrTy); llvm::FunctionType *fnTy = llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false); llvm::Constant *fn = cast<llvm::Constant>( CGM.CreateRuntimeFunction(fnTy, name).getCallee()); if (auto f = dyn_cast<llvm::Function>(fn)) f->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); - return llvm::ConstantExpr::getBitCast(fn, CGM.Int8PtrTy); + return fn; }; llvm::Constant *fnPtr; @@ -822,15 +823,26 @@ void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder, return addRelativeComponent( builder, fnPtr, vtableAddressPoint, vtableHasLocalLinkage, component.getKind() == VTableComponent::CK_CompleteDtorPointer); - } else - return builder.add(llvm::ConstantExpr::getBitCast(fnPtr, CGM.Int8PtrTy)); + } else { + // TODO: this icky and only exists due to functions being in the generic + // address space, rather than the global one, even though they are + // globals; fixing said issue might be intrusive, and will be done + // later. + unsigned FnAS = fnPtr->getType()->getPointerAddressSpace(); + unsigned GVAS = CGM.GlobalsInt8PtrTy->getPointerAddressSpace(); + + if (FnAS != GVAS) + fnPtr = + llvm::ConstantExpr::getAddrSpaceCast(fnPtr, CGM.GlobalsInt8PtrTy); + return builder.add(fnPtr); + } } case VTableComponent::CK_UnusedFunctionPointer: if (useRelativeLayout()) return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int32Ty)); else - return builder.addNullPointer(CGM.Int8PtrTy); + return builder.addNullPointer(CGM.GlobalsInt8PtrTy); } llvm_unreachable("Unexpected vtable component kind"); @@ -1045,19 +1057,20 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { switch (keyFunction->getTemplateSpecializationKind()) { case TSK_Undeclared: case TSK_ExplicitSpecialization: - assert((def || CodeGenOpts.OptimizationLevel > 0 || - CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo) && - "Shouldn't query vtable linkage without key function, " - "optimizations, or debug info"); - if (!def && CodeGenOpts.OptimizationLevel > 0) - return llvm::GlobalVariable::AvailableExternallyLinkage; + assert( + (def || CodeGenOpts.OptimizationLevel > 0 || + CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo) && + "Shouldn't query vtable linkage without key function, " + "optimizations, or debug info"); + if (!def && CodeGenOpts.OptimizationLevel > 0) + return llvm::GlobalVariable::AvailableExternallyLinkage; - if (keyFunction->isInlined()) - return !Context.getLangOpts().AppleKext ? - llvm::GlobalVariable::LinkOnceODRLinkage : - llvm::Function::InternalLinkage; + if (keyFunction->isInlined()) + return !Context.getLangOpts().AppleKext + ? llvm::GlobalVariable::LinkOnceODRLinkage + : llvm::Function::InternalLinkage; - return llvm::GlobalVariable::ExternalLinkage; + return llvm::GlobalVariable::ExternalLinkage; case TSK_ImplicitInstantiation: return !Context.getLangOpts().AppleKext ? @@ -1171,9 +1184,16 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) { if (!keyFunction) return false; + const FunctionDecl *Def; // Otherwise, if we don't have a definition of the key function, the // vtable must be defined somewhere else. - return !keyFunction->hasBody(); + if (!keyFunction->hasBody(Def)) + return true; + + assert(Def && "The body of the key function is not assigned to Def?"); + // If the non-inline key function comes from another module unit, the vtable + // must be defined there. + return Def->isInAnotherModuleUnit() && !Def->isInlineSpecified(); } /// Given that we're currently at the end of the translation unit, and @@ -1211,7 +1231,8 @@ void CodeGenModule::EmitDeferredVTables() { } bool CodeGenModule::AlwaysHasLTOVisibilityPublic(const CXXRecordDecl *RD) { - if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>()) + if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>() || + RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>()) return true; if (!getCodeGenOpts().LTOVisibilityPublicStd) @@ -1238,13 +1259,9 @@ bool CodeGenModule::HasHiddenLTOVisibility(const CXXRecordDecl *RD) { if (!isExternallyVisible(LV.getLinkage())) return true; - if (getTriple().isOSBinFormatCOFF()) { - if (RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>()) - return false; - } else { - if (LV.getVisibility() != HiddenVisibility) - return false; - } + if (!getTriple().isOSBinFormatCOFF() && + LV.getVisibility() != HiddenVisibility) + return false; return !AlwaysHasLTOVisibilityPublic(RD); } @@ -1268,13 +1285,13 @@ llvm::GlobalObject::VCallVisibility CodeGenModule::GetVCallVisibilityLevel( else TypeVis = llvm::GlobalObject::VCallVisibilityPublic; - for (auto B : RD->bases()) + for (const auto &B : RD->bases()) if (B.getType()->getAsCXXRecordDecl()->isDynamicClass()) TypeVis = std::min( TypeVis, GetVCallVisibilityLevel(B.getType()->getAsCXXRecordDecl(), Visited)); - for (auto B : RD->vbases()) + for (const auto &B : RD->vbases()) if (B.getType()->getAsCXXRecordDecl()->isDynamicClass()) TypeVis = std::min( TypeVis, |