2010-04-18 04:15:18 +08:00
|
|
|
//===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
|
2009-10-12 06:13:54 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2009-10-12 06:13:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation of virtual tables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenModule.h"
|
2019-12-10 08:11:56 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2009-11-28 04:47:55 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-10-12 06:13:54 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2018-12-11 11:18:39 +08:00
|
|
|
#include "clang/Basic/CodeGenOptions.h"
|
2013-10-31 05:53:58 +08:00
|
|
|
#include "clang/CodeGen/CGFunctionInfo.h"
|
2017-11-01 06:49:48 +08:00
|
|
|
#include "clang/CodeGen/ConstantInitBuilder.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2010-02-11 16:02:13 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2011-05-07 01:27:27 +08:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2010-03-18 04:06:32 +08:00
|
|
|
#include <algorithm>
|
2009-11-13 13:46:16 +08:00
|
|
|
#include <cstdio>
|
2009-10-12 06:13:54 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2014-02-06 01:27:08 +08:00
|
|
|
CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
|
|
|
|
: CGM(CGM), VTContext(CGM.getContext().getVTableContext()) {}
|
2011-09-26 09:56:30 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfThunk(StringRef Name, llvm::Type *FnTy,
|
|
|
|
GlobalDecl GD) {
|
|
|
|
return GetOrCreateLLVMFunction(Name, FnTy, GD, /*ForVTable=*/true,
|
2014-11-01 13:42:23 +08:00
|
|
|
/*DontDefer=*/true, /*IsThunk=*/true);
|
2010-03-24 01:17:29 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 22:48:06 +08:00
|
|
|
static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk,
|
|
|
|
llvm::Function *ThunkFn, bool ForVTable,
|
|
|
|
GlobalDecl GD) {
|
|
|
|
CGM.setFunctionLinkage(GD, ThunkFn);
|
|
|
|
CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable, GD,
|
|
|
|
!Thunk.Return.isEmpty());
|
|
|
|
|
|
|
|
// Set the right visibility.
|
2018-03-01 08:35:47 +08:00
|
|
|
CGM.setGVProperties(ThunkFn, GD);
|
|
|
|
|
|
|
|
if (!CGM.getCXXABI().exportThunk()) {
|
|
|
|
ThunkFn->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
|
|
|
|
ThunkFn->setDSOLocal(true);
|
|
|
|
}
|
2015-07-15 22:48:06 +08:00
|
|
|
|
|
|
|
if (CGM.supportsCOMDAT() && ThunkFn->isWeakForLinker())
|
|
|
|
ThunkFn->setComdat(CGM.getModule().getOrInsertComdat(ThunkFn->getName()));
|
|
|
|
}
|
|
|
|
|
2011-03-09 15:12:35 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
|
|
|
|
const ABIArgInfo &infoR, CanQualType typeR) {
|
|
|
|
return (infoL.getKind() == infoR.getKind() &&
|
|
|
|
(typeL == typeR ||
|
|
|
|
(isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
|
|
|
|
(isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
|
|
|
|
QualType ResultType, RValue RV,
|
|
|
|
const ThunkInfo &Thunk) {
|
|
|
|
// Emit the return adjustment.
|
|
|
|
bool NullCheckValue = !ResultType->isReferenceType();
|
2014-05-21 13:09:00 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *AdjustNull = nullptr;
|
|
|
|
llvm::BasicBlock *AdjustNotNull = nullptr;
|
|
|
|
llvm::BasicBlock *AdjustEnd = nullptr;
|
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
llvm::Value *ReturnValue = RV.getScalarVal();
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
AdjustNull = CGF.createBasicBlock("adjust.null");
|
|
|
|
AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
|
|
|
|
AdjustEnd = CGF.createBasicBlock("adjust.end");
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
|
|
|
|
CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
|
|
|
|
CGF.EmitBlock(AdjustNotNull);
|
|
|
|
}
|
2013-10-30 19:55:43 +08:00
|
|
|
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
auto ClassDecl = ResultType->getPointeeType()->getAsCXXRecordDecl();
|
|
|
|
auto ClassAlign = CGF.CGM.getClassPointerAlignment(ClassDecl);
|
|
|
|
ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF,
|
|
|
|
Address(ReturnValue, ClassAlign),
|
|
|
|
Thunk.Return);
|
2013-10-30 19:55:43 +08:00
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
if (NullCheckValue) {
|
|
|
|
CGF.Builder.CreateBr(AdjustEnd);
|
|
|
|
CGF.EmitBlock(AdjustNull);
|
|
|
|
CGF.Builder.CreateBr(AdjustEnd);
|
|
|
|
CGF.EmitBlock(AdjustEnd);
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
|
|
|
|
PHI->addIncoming(ReturnValue, AdjustNotNull);
|
2016-09-08 17:59:58 +08:00
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
|
2011-05-07 01:27:27 +08:00
|
|
|
AdjustNull);
|
|
|
|
ReturnValue = PHI;
|
|
|
|
}
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
return RValue::get(ReturnValue);
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
/// This function clones a function's DISubprogram node and enters it into
|
2017-11-01 06:49:48 +08:00
|
|
|
/// a value map with the intent that the map can be utilized by the cloner
|
|
|
|
/// to short-circuit Metadata node mapping.
|
|
|
|
/// Furthermore, the function resolves any DILocalVariable nodes referenced
|
|
|
|
/// by dbg.value intrinsics so they can be properly mapped during cloning.
|
|
|
|
static void resolveTopLevelMetadata(llvm::Function *Fn,
|
|
|
|
llvm::ValueToValueMapTy &VMap) {
|
|
|
|
// Clone the DISubprogram node and put it into the Value map.
|
|
|
|
auto *DIS = Fn->getSubprogram();
|
|
|
|
if (!DIS)
|
|
|
|
return;
|
|
|
|
auto *NewDIS = DIS->replaceWithDistinct(DIS->clone());
|
|
|
|
VMap.MD()[DIS].reset(NewDIS);
|
|
|
|
|
|
|
|
// Find all llvm.dbg.declare intrinsics and resolve the DILocalVariable nodes
|
|
|
|
// they are referencing.
|
|
|
|
for (auto &BB : Fn->getBasicBlockList()) {
|
|
|
|
for (auto &I : BB) {
|
2018-08-06 12:00:08 +08:00
|
|
|
if (auto *DII = dyn_cast<llvm::DbgVariableIntrinsic>(&I)) {
|
2017-11-01 06:49:48 +08:00
|
|
|
auto *DILocal = DII->getVariable();
|
|
|
|
if (!DILocal->isResolved())
|
|
|
|
DILocal->resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
// This function does roughly the same thing as GenerateThunk, but in a
|
|
|
|
// very different way, so that va_start and va_end work correctly.
|
|
|
|
// FIXME: This function assumes "this" is the first non-sret LLVM argument of
|
|
|
|
// a function, and that there is an alloca built in the entry block
|
|
|
|
// for all accesses to "this".
|
|
|
|
// FIXME: This function assumes there is only one "ret" statement per function.
|
|
|
|
// FIXME: Cloning isn't correct in the presence of indirect goto!
|
|
|
|
// FIXME: This implementation of thunks bloats codesize by duplicating the
|
|
|
|
// function definition. There are alternatives:
|
|
|
|
// 1. Add some sort of stub support to LLVM for cases where we can
|
|
|
|
// do a this adjustment, then a sibcall.
|
|
|
|
// 2. We could transform the definition to take a va_list instead of an
|
|
|
|
// actual variable argument list, then have the thunks (including a
|
|
|
|
// no-op thunk for the regular definition) call va_start/va_end.
|
|
|
|
// There's a bit of per-call overhead for this solution, but it's
|
|
|
|
// better for codesize if the definition is long.
|
2015-07-01 06:08:44 +08:00
|
|
|
llvm::Function *
|
|
|
|
CodeGenFunction::GenerateVarArgsThunk(llvm::Function *Fn,
|
2011-05-07 01:27:27 +08:00
|
|
|
const CGFunctionInfo &FnInfo,
|
|
|
|
GlobalDecl GD, const ThunkInfo &Thunk) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2019-10-02 06:02:46 +08:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType ResultType = FPT->getReturnType();
|
2011-05-07 01:27:27 +08:00
|
|
|
|
|
|
|
// Get the original function
|
2012-02-17 11:33:10 +08:00
|
|
|
assert(FnInfo.isVariadic());
|
|
|
|
llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
|
2011-05-07 01:27:27 +08:00
|
|
|
llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
|
|
|
llvm::Function *BaseFn = cast<llvm::Function>(Callee);
|
|
|
|
|
2019-09-07 06:55:26 +08:00
|
|
|
// Cloning can't work if we don't have a definition. The Microsoft ABI may
|
|
|
|
// require thunks when a definition is not available. Emit an error in these
|
|
|
|
// cases.
|
|
|
|
if (!MD->isDefined()) {
|
|
|
|
CGM.ErrorUnsupported(MD, "return-adjusting thunk with variadic arguments");
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
assert(!BaseFn->isDeclaration() && "cannot clone undefined variadic method");
|
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
// Clone to thunk.
|
2012-09-19 21:13:52 +08:00
|
|
|
llvm::ValueToValueMapTy VMap;
|
2017-11-01 06:49:48 +08:00
|
|
|
|
|
|
|
// We are cloning a function while some Metadata nodes are still unresolved.
|
|
|
|
// Ensure that the value mapper does not encounter any of them.
|
|
|
|
resolveTopLevelMetadata(BaseFn, VMap);
|
2016-05-11 04:23:29 +08:00
|
|
|
llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap);
|
2011-05-07 01:27:27 +08:00
|
|
|
Fn->replaceAllUsesWith(NewFn);
|
|
|
|
NewFn->takeName(Fn);
|
|
|
|
Fn->eraseFromParent();
|
|
|
|
Fn = NewFn;
|
|
|
|
|
|
|
|
// "Initialize" CGF (minimally).
|
|
|
|
CurFn = Fn;
|
|
|
|
|
|
|
|
// Get the "this" value
|
|
|
|
llvm::Function::arg_iterator AI = Fn->arg_begin();
|
|
|
|
if (CGM.ReturnTypeUsesSRet(FnInfo))
|
|
|
|
++AI;
|
|
|
|
|
|
|
|
// Find the first store of "this", which will be to the alloca associated
|
|
|
|
// with "this".
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
Address ThisPtr(&*AI, CGM.getClassPointerAlignment(MD->getParent()));
|
2015-11-07 07:00:41 +08:00
|
|
|
llvm::BasicBlock *EntryBB = &Fn->front();
|
|
|
|
llvm::BasicBlock::iterator ThisStore =
|
2014-12-30 06:39:45 +08:00
|
|
|
std::find_if(EntryBB->begin(), EntryBB->end(), [&](llvm::Instruction &I) {
|
2015-11-07 07:00:41 +08:00
|
|
|
return isa<llvm::StoreInst>(I) &&
|
|
|
|
I.getOperand(0) == ThisPtr.getPointer();
|
|
|
|
});
|
|
|
|
assert(ThisStore != EntryBB->end() &&
|
|
|
|
"Store of this should be in entry block?");
|
2011-05-07 01:27:27 +08:00
|
|
|
// Adjust "this", if necessary.
|
2015-11-07 07:00:41 +08:00
|
|
|
Builder.SetInsertPoint(&*ThisStore);
|
2013-10-30 19:55:43 +08:00
|
|
|
llvm::Value *AdjustedThisPtr =
|
|
|
|
CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
|
2019-09-07 06:55:26 +08:00
|
|
|
AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr,
|
|
|
|
ThisStore->getOperand(0)->getType());
|
2011-05-07 01:27:27 +08:00
|
|
|
ThisStore->setOperand(0, AdjustedThisPtr);
|
|
|
|
|
|
|
|
if (!Thunk.Return.isEmpty()) {
|
|
|
|
// Fix up the returned value, if necessary.
|
2015-07-29 00:10:58 +08:00
|
|
|
for (llvm::BasicBlock &BB : *Fn) {
|
|
|
|
llvm::Instruction *T = BB.getTerminator();
|
2011-05-07 01:27:27 +08:00
|
|
|
if (isa<llvm::ReturnInst>(T)) {
|
|
|
|
RValue RV = RValue::get(T->getOperand(0));
|
|
|
|
T->eraseFromParent();
|
2015-07-29 00:10:58 +08:00
|
|
|
Builder.SetInsertPoint(&BB);
|
2011-05-07 01:27:27 +08:00
|
|
|
RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
|
|
|
|
Builder.CreateRet(RV.getScalarVal());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 06:08:44 +08:00
|
|
|
|
|
|
|
return Fn;
|
2011-05-07 01:27:27 +08:00
|
|
|
}
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
const CGFunctionInfo &FnInfo,
|
|
|
|
bool IsUnprototyped) {
|
2013-11-16 01:24:45 +08:00
|
|
|
assert(!CurGD.getDecl() && "CurGD was already set!");
|
|
|
|
CurGD = GD;
|
2014-07-26 05:39:46 +08:00
|
|
|
CurFuncIsThunk = true;
|
2013-11-16 01:24:45 +08:00
|
|
|
|
|
|
|
// Build FunctionArgs.
|
2010-03-24 08:39:18 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2019-01-11 09:54:53 +08:00
|
|
|
QualType ThisType = MD->getThisType();
|
2018-04-19 07:21:32 +08:00
|
|
|
QualType ResultType;
|
|
|
|
if (IsUnprototyped)
|
|
|
|
ResultType = CGM.getContext().VoidTy;
|
|
|
|
else if (CGM.getCXXABI().HasThisReturn(GD))
|
|
|
|
ResultType = ThisType;
|
|
|
|
else if (CGM.getCXXABI().hasMostDerivedReturn(GD))
|
|
|
|
ResultType = CGM.getContext().VoidPtrTy;
|
|
|
|
else
|
2019-10-02 06:02:46 +08:00
|
|
|
ResultType = MD->getType()->castAs<FunctionProtoType>()->getReturnType();
|
2010-03-24 08:39:18 +08:00
|
|
|
FunctionArgList FunctionArgs;
|
|
|
|
|
|
|
|
// Create the implicit 'this' parameter declaration.
|
2013-12-18 03:46:40 +08:00
|
|
|
CGM.getCXXABI().buildThisParam(*this, FunctionArgs);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
// Add the rest of the parameters, if we have a prototype to work with.
|
|
|
|
if (!IsUnprototyped) {
|
|
|
|
FunctionArgs.append(MD->param_begin(), MD->param_end());
|
2012-10-25 18:18:50 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
|
|
|
CGM.getCXXABI().addImplicitStructorParams(*this, ResultType,
|
|
|
|
FunctionArgs);
|
|
|
|
}
|
2013-12-18 03:46:40 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Start defining the function.
|
2016-11-10 05:43:51 +08:00
|
|
|
auto NL = ApplyDebugLocation::CreateEmpty(*this);
|
2011-03-09 12:27:21 +08:00
|
|
|
StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
|
2016-11-10 05:43:51 +08:00
|
|
|
MD->getLocation());
|
|
|
|
// Create a scope with an artificial location for the body of this function.
|
|
|
|
auto AL = ApplyDebugLocation::CreateArtificial(*this);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
|
2010-08-31 15:33:07 +08:00
|
|
|
CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
|
2012-02-11 10:57:39 +08:00
|
|
|
CXXThisValue = CXXABIThisValue;
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
CurCodeDecl = MD;
|
|
|
|
CurFuncDecl = MD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::FinishThunk() {
|
|
|
|
// Clear these to restore the invariants expected by
|
|
|
|
// StartFunction/FinishFunction.
|
|
|
|
CurCodeDecl = nullptr;
|
|
|
|
CurFuncDecl = nullptr;
|
|
|
|
|
|
|
|
FinishFunction();
|
2013-11-16 01:24:45 +08:00
|
|
|
}
|
2010-08-31 15:33:07 +08:00
|
|
|
|
2019-02-06 03:17:50 +08:00
|
|
|
void CodeGenFunction::EmitCallAndReturnForThunk(llvm::FunctionCallee Callee,
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
const ThunkInfo *Thunk,
|
|
|
|
bool IsUnprototyped) {
|
2013-11-16 01:24:45 +08:00
|
|
|
assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
|
|
|
|
"Please use a new CGF for this thunk");
|
2014-07-26 09:30:05 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(CurGD.getDecl());
|
2013-11-16 01:24:45 +08:00
|
|
|
|
|
|
|
// Adjust the 'this' pointer if necessary
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
llvm::Value *AdjustedThisPtr =
|
|
|
|
Thunk ? CGM.getCXXABI().performThisAdjustment(
|
|
|
|
*this, LoadCXXThisAddress(), Thunk->This)
|
|
|
|
: LoadCXXThis();
|
2013-10-30 19:55:43 +08:00
|
|
|
|
2019-09-07 06:55:26 +08:00
|
|
|
// If perfect forwarding is required a variadic method, a method using
|
|
|
|
// inalloca, or an unprototyped thunk, use musttail. Emit an error if this
|
|
|
|
// thunk requires a return adjustment, since that is impossible with musttail.
|
|
|
|
if (CurFnInfo->usesInAlloca() || CurFnInfo->isVariadic() || IsUnprototyped) {
|
2014-07-26 09:34:32 +08:00
|
|
|
if (Thunk && !Thunk->Return.isEmpty()) {
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
if (IsUnprototyped)
|
|
|
|
CGM.ErrorUnsupported(
|
|
|
|
MD, "return-adjusting thunk with incomplete parameter type");
|
2019-09-07 06:55:26 +08:00
|
|
|
else if (CurFnInfo->isVariadic())
|
|
|
|
llvm_unreachable("shouldn't try to emit musttail return-adjusting "
|
|
|
|
"thunks for variadic functions");
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
else
|
|
|
|
CGM.ErrorUnsupported(
|
|
|
|
MD, "non-trivial argument copy for return-adjusting thunk");
|
2014-07-26 09:34:32 +08:00
|
|
|
}
|
2019-02-06 03:17:50 +08:00
|
|
|
EmitMustTailThunk(CurGD, AdjustedThisPtr, Callee);
|
2014-07-26 09:34:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Start building CallArgs.
|
2010-03-24 08:39:18 +08:00
|
|
|
CallArgList CallArgs;
|
2019-01-11 09:54:53 +08:00
|
|
|
QualType ThisType = MD->getThisType();
|
2011-05-03 01:57:46 +08:00
|
|
|
CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
if (isa<CXXDestructorDecl>(MD))
|
2014-07-26 09:30:05 +08:00
|
|
|
CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, CurGD, CallArgs);
|
2013-10-09 17:23:58 +08:00
|
|
|
|
2017-02-24 06:47:56 +08:00
|
|
|
#ifndef NDEBUG
|
2017-02-24 06:07:35 +08:00
|
|
|
unsigned PrefixArgs = CallArgs.size() - 1;
|
2017-02-24 06:47:56 +08:00
|
|
|
#endif
|
2013-11-16 01:24:45 +08:00
|
|
|
// Add the rest of the arguments.
|
2016-06-24 12:05:48 +08:00
|
|
|
for (const ParmVarDecl *PD : MD->parameters())
|
2016-11-10 05:43:51 +08:00
|
|
|
EmitDelegateCallArg(CallArgs, PD, SourceLocation());
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2020-01-11 01:40:34 +08:00
|
|
|
const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
#ifndef NDEBUG
|
2016-06-17 07:06:04 +08:00
|
|
|
const CGFunctionInfo &CallFnInfo = CGM.getTypes().arrangeCXXMethodCall(
|
2019-02-02 09:48:23 +08:00
|
|
|
CallArgs, FPT, RequiredArgs::forPrototypePlus(FPT, 1), PrefixArgs);
|
2013-11-16 01:24:45 +08:00
|
|
|
assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
|
|
|
|
CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
|
|
|
|
CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
|
2012-07-07 14:41:13 +08:00
|
|
|
assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
|
|
|
|
similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
|
2013-11-16 01:24:45 +08:00
|
|
|
CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
|
|
|
|
assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
|
|
|
|
for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
|
2011-03-09 15:12:35 +08:00
|
|
|
assert(similar(CallFnInfo.arg_begin()[i].info,
|
|
|
|
CallFnInfo.arg_begin()[i].type,
|
2013-11-16 01:24:45 +08:00
|
|
|
CurFnInfo->arg_begin()[i].info,
|
|
|
|
CurFnInfo->arg_begin()[i].type));
|
2011-03-09 12:27:21 +08:00
|
|
|
#endif
|
2013-11-16 01:24:45 +08:00
|
|
|
|
2010-05-20 13:54:35 +08:00
|
|
|
// Determine whether we have a return value slot to use.
|
2014-11-01 04:09:12 +08:00
|
|
|
QualType ResultType = CGM.getCXXABI().HasThisReturn(CurGD)
|
|
|
|
? ThisType
|
|
|
|
: CGM.getCXXABI().hasMostDerivedReturn(CurGD)
|
|
|
|
? CGM.getContext().VoidPtrTy
|
|
|
|
: FPT->getReturnType();
|
2010-05-20 13:54:35 +08:00
|
|
|
ReturnValueSlot Slot;
|
|
|
|
if (!ResultType->isVoidType() &&
|
2020-07-11 08:24:12 +08:00
|
|
|
(CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect ||
|
|
|
|
hasAggregateEvaluationKind(ResultType)))
|
2019-11-21 10:13:44 +08:00
|
|
|
Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified(),
|
|
|
|
/*IsUnused=*/false, /*IsExternallyDestructed=*/true);
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2010-03-24 08:39:18 +08:00
|
|
|
// Now emit our call.
|
2019-01-30 10:54:28 +08:00
|
|
|
llvm::CallBase *CallOrInvoke;
|
2019-02-06 03:17:50 +08:00
|
|
|
RValue RV = EmitCall(*CurFnInfo, CGCallee::forDirect(Callee, CurGD), Slot,
|
|
|
|
CallArgs, &CallOrInvoke);
|
2014-07-26 09:34:32 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Consider return adjustment if we have ThunkInfo.
|
|
|
|
if (Thunk && !Thunk->Return.isEmpty())
|
|
|
|
RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
|
2015-08-06 19:57:15 +08:00
|
|
|
else if (llvm::CallInst* Call = dyn_cast<llvm::CallInst>(CallOrInvoke))
|
|
|
|
Call->setTailCallKind(llvm::CallInst::TCK_Tail);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Emit return.
|
2010-05-20 13:54:35 +08:00
|
|
|
if (!ResultType->isVoidType() && Slot.isNull())
|
2011-02-08 16:22:06 +08:00
|
|
|
CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2012-07-31 08:33:55 +08:00
|
|
|
// Disable the final ARC autorelease.
|
|
|
|
AutoreleaseResult = false;
|
|
|
|
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
FinishThunk();
|
2013-11-16 01:24:45 +08:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:48:08 +08:00
|
|
|
void CodeGenFunction::EmitMustTailThunk(GlobalDecl GD,
|
2014-07-26 09:34:32 +08:00
|
|
|
llvm::Value *AdjustedThisPtr,
|
2019-02-06 03:17:50 +08:00
|
|
|
llvm::FunctionCallee Callee) {
|
2014-07-26 09:34:32 +08:00
|
|
|
// Emitting a musttail call thunk doesn't use any of the CGCall.cpp machinery
|
|
|
|
// to translate AST arguments into LLVM IR arguments. For thunks, we know
|
|
|
|
// that the caller prototype more or less matches the callee prototype with
|
|
|
|
// the exception of 'this'.
|
|
|
|
SmallVector<llvm::Value *, 8> Args;
|
|
|
|
for (llvm::Argument &A : CurFn->args())
|
|
|
|
Args.push_back(&A);
|
|
|
|
|
|
|
|
// Set the adjusted 'this' pointer.
|
|
|
|
const ABIArgInfo &ThisAI = CurFnInfo->arg_begin()->info;
|
|
|
|
if (ThisAI.isDirect()) {
|
|
|
|
const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
|
|
|
|
int ThisArgNo = RetAI.isIndirect() && !RetAI.isSRetAfterThis() ? 1 : 0;
|
|
|
|
llvm::Type *ThisType = Args[ThisArgNo]->getType();
|
|
|
|
if (ThisType != AdjustedThisPtr->getType())
|
|
|
|
AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
|
|
|
|
Args[ThisArgNo] = AdjustedThisPtr;
|
|
|
|
} else {
|
|
|
|
assert(ThisAI.isInAlloca() && "this is passed directly or inalloca");
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
Address ThisAddr = GetAddrOfLocalVar(CXXABIThisDecl);
|
|
|
|
llvm::Type *ThisType = ThisAddr.getElementType();
|
2014-07-26 09:34:32 +08:00
|
|
|
if (ThisType != AdjustedThisPtr->getType())
|
|
|
|
AdjustedThisPtr = Builder.CreateBitCast(AdjustedThisPtr, ThisType);
|
|
|
|
Builder.CreateStore(AdjustedThisPtr, ThisAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the musttail call manually. Even if the prologue pushed cleanups, we
|
|
|
|
// don't actually want to run them.
|
2019-02-06 03:17:50 +08:00
|
|
|
llvm::CallInst *Call = Builder.CreateCall(Callee, Args);
|
2014-07-26 09:34:32 +08:00
|
|
|
Call->setTailCallKind(llvm::CallInst::TCK_MustTail);
|
|
|
|
|
|
|
|
// Apply the standard set of call attributes.
|
|
|
|
unsigned CallingConv;
|
2017-04-19 07:50:03 +08:00
|
|
|
llvm::AttributeList Attrs;
|
2019-02-06 03:17:50 +08:00
|
|
|
CGM.ConstructAttributeList(Callee.getCallee()->getName(), *CurFnInfo, GD,
|
|
|
|
Attrs, CallingConv, /*AttrOnCallSite=*/true);
|
2014-07-26 09:34:32 +08:00
|
|
|
Call->setAttributes(Attrs);
|
|
|
|
Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
|
|
|
|
|
|
|
|
if (Call->getType()->isVoidTy())
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
else
|
|
|
|
Builder.CreateRet(Call);
|
|
|
|
|
|
|
|
// Finish the function to maintain CodeGenFunction invariants.
|
|
|
|
// FIXME: Don't emit unreachable code.
|
|
|
|
EmitBlock(createBasicBlock());
|
2020-03-20 02:52:22 +08:00
|
|
|
|
|
|
|
FinishThunk();
|
2014-07-26 09:34:32 +08:00
|
|
|
}
|
|
|
|
|
2015-07-13 14:07:58 +08:00
|
|
|
void CodeGenFunction::generateThunk(llvm::Function *Fn,
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
const CGFunctionInfo &FnInfo, GlobalDecl GD,
|
|
|
|
const ThunkInfo &Thunk,
|
|
|
|
bool IsUnprototyped) {
|
|
|
|
StartThunk(Fn, GD, FnInfo, IsUnprototyped);
|
2016-11-10 05:43:51 +08:00
|
|
|
// Create a scope with an artificial location for the body of this function.
|
|
|
|
auto AL = ApplyDebugLocation::CreateArtificial(*this);
|
2013-11-16 01:24:45 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
// Get our callee. Use a placeholder type if this method is unprototyped so
|
|
|
|
// that CodeGenModule doesn't try to set attributes.
|
|
|
|
llvm::Type *Ty;
|
|
|
|
if (IsUnprototyped)
|
|
|
|
Ty = llvm::StructType::get(getLLVMContext());
|
|
|
|
else
|
|
|
|
Ty = CGM.getTypes().GetFunctionType(FnInfo);
|
|
|
|
|
2016-10-27 07:46:34 +08:00
|
|
|
llvm::Constant *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
|
2013-11-16 01:24:45 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
// Fix up the function type for an unprototyped musttail call.
|
|
|
|
if (IsUnprototyped)
|
|
|
|
Callee = llvm::ConstantExpr::getBitCast(Callee, Fn->getType());
|
|
|
|
|
2013-11-16 01:24:45 +08:00
|
|
|
// Make the call and return the result.
|
2019-02-06 03:17:50 +08:00
|
|
|
EmitCallAndReturnForThunk(llvm::FunctionCallee(Fn->getFunctionType(), Callee),
|
|
|
|
&Thunk, IsUnprototyped);
|
2010-03-24 08:39:18 +08:00
|
|
|
}
|
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
static bool shouldEmitVTableThunk(CodeGenModule &CGM, const CXXMethodDecl *MD,
|
|
|
|
bool IsUnprototyped, bool ForVTable) {
|
|
|
|
// Always emit thunks in the MS C++ ABI. We cannot rely on other TUs to
|
|
|
|
// provide thunks for us.
|
|
|
|
if (CGM.getTarget().getCXXABI().isMicrosoft())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// In the Itanium C++ ABI, vtable thunks are provided by TUs that provide
|
|
|
|
// definitions of the main method. Therefore, emitting thunks with the vtable
|
|
|
|
// is purely an optimization. Emit the thunk if optimizations are enabled and
|
|
|
|
// all of the parameter types are complete.
|
|
|
|
if (ForVTable)
|
|
|
|
return CGM.getCodeGenOpts().OptimizationLevel && !IsUnprototyped;
|
|
|
|
|
|
|
|
// Always emit thunks along with the method definition.
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-08 23:44:45 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD,
|
|
|
|
const ThunkInfo &TI,
|
|
|
|
bool ForVTable) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
2014-05-08 23:44:45 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
// First, get a declaration. Compute the mangled name. Don't worry about
|
|
|
|
// getting the function prototype right, since we may only need this
|
|
|
|
// declaration to fill in a vtable slot.
|
|
|
|
SmallString<256> Name;
|
|
|
|
MangleContext &MCtx = CGM.getCXXABI().getMangleContext();
|
|
|
|
llvm::raw_svector_ostream Out(Name);
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD))
|
|
|
|
MCtx.mangleCXXDtorThunk(DD, GD.getDtorType(), TI.This, Out);
|
|
|
|
else
|
|
|
|
MCtx.mangleThunk(MD, TI, Out);
|
|
|
|
llvm::Type *ThunkVTableTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
|
|
|
|
llvm::Constant *Thunk = CGM.GetAddrOfThunk(Name, ThunkVTableTy, GD);
|
|
|
|
|
|
|
|
// If we don't need to emit a definition, return this declaration as is.
|
|
|
|
bool IsUnprototyped = !CGM.getTypes().isFuncTypeConvertible(
|
|
|
|
MD->getType()->castAs<FunctionType>());
|
|
|
|
if (!shouldEmitVTableThunk(CGM, MD, IsUnprototyped, ForVTable))
|
|
|
|
return Thunk;
|
|
|
|
|
|
|
|
// Arrange a function prototype appropriate for a function definition. In some
|
|
|
|
// cases in the MS ABI, we may need to build an unprototyped musttail thunk.
|
|
|
|
const CGFunctionInfo &FnInfo =
|
|
|
|
IsUnprototyped ? CGM.getTypes().arrangeUnprototypedMustTailThunk(MD)
|
|
|
|
: CGM.getTypes().arrangeGlobalDeclaration(GD);
|
|
|
|
llvm::FunctionType *ThunkFnTy = CGM.getTypes().GetFunctionType(FnInfo);
|
|
|
|
|
|
|
|
// If the type of the underlying GlobalValue is wrong, we'll have to replace
|
|
|
|
// it. It should be a declaration.
|
|
|
|
llvm::Function *ThunkFn = cast<llvm::Function>(Thunk->stripPointerCasts());
|
|
|
|
if (ThunkFn->getFunctionType() != ThunkFnTy) {
|
|
|
|
llvm::GlobalValue *OldThunkFn = ThunkFn;
|
|
|
|
|
|
|
|
assert(OldThunkFn->isDeclaration() && "Shouldn't replace non-declaration");
|
2010-03-24 02:18:41 +08:00
|
|
|
|
|
|
|
// Remove the name from the old thunk function and get a new thunk.
|
2011-07-23 18:55:15 +08:00
|
|
|
OldThunkFn->setName(StringRef());
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
ThunkFn = llvm::Function::Create(ThunkFnTy, llvm::Function::ExternalLinkage,
|
|
|
|
Name.str(), &CGM.getModule());
|
|
|
|
CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn);
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2010-03-24 02:18:41 +08:00
|
|
|
// If needed, replace the old thunk with a bitcast.
|
|
|
|
if (!OldThunkFn->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(ThunkFn, OldThunkFn->getType());
|
2010-03-24 02:18:41 +08:00
|
|
|
OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2010-03-24 02:18:41 +08:00
|
|
|
// Remove the old thunk.
|
|
|
|
OldThunkFn->eraseFromParent();
|
|
|
|
}
|
2010-03-24 08:39:18 +08:00
|
|
|
|
2013-10-09 17:23:58 +08:00
|
|
|
bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
|
|
|
|
bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
|
2011-02-07 02:31:40 +08:00
|
|
|
|
|
|
|
if (!ThunkFn->isDeclaration()) {
|
2013-10-09 17:23:58 +08:00
|
|
|
if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
|
2011-02-07 02:31:40 +08:00
|
|
|
// There is already a thunk emitted for this function, do nothing.
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
return ThunkFn;
|
2011-02-07 02:31:40 +08:00
|
|
|
}
|
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
setThunkProperties(CGM, TI, ThunkFn, ForVTable, GD);
|
|
|
|
return ThunkFn;
|
2011-02-07 02:31:40 +08:00
|
|
|
}
|
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
// If this will be unprototyped, add the "thunk" attribute so that LLVM knows
|
|
|
|
// that the return type is meaningless. These thunks can be used to call
|
|
|
|
// functions with differing return types, and the caller is required to cast
|
|
|
|
// the prototype appropriately to extract the correct value.
|
|
|
|
if (IsUnprototyped)
|
|
|
|
ThunkFn->addFnAttr("thunk");
|
|
|
|
|
2012-09-22 04:39:32 +08:00
|
|
|
CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
|
|
|
|
|
2019-09-07 06:55:26 +08:00
|
|
|
// Thunks for variadic methods are special because in general variadic
|
2020-03-20 02:52:22 +08:00
|
|
|
// arguments cannot be perfectly forwarded. In the general case, clang
|
2019-09-07 06:55:26 +08:00
|
|
|
// implements such thunks by cloning the original function body. However, for
|
|
|
|
// thunks with no return adjustment on targets that support musttail, we can
|
|
|
|
// use musttail to perfectly forward the variadic arguments.
|
|
|
|
bool ShouldCloneVarArgs = false;
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
if (!IsUnprototyped && ThunkFn->isVarArg()) {
|
2019-09-07 06:55:26 +08:00
|
|
|
ShouldCloneVarArgs = true;
|
|
|
|
if (TI.Return.isEmpty()) {
|
|
|
|
switch (CGM.getTriple().getArch()) {
|
|
|
|
case llvm::Triple::x86_64:
|
|
|
|
case llvm::Triple::x86:
|
|
|
|
case llvm::Triple::aarch64:
|
|
|
|
ShouldCloneVarArgs = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldCloneVarArgs) {
|
2015-07-01 03:07:26 +08:00
|
|
|
if (UseAvailableExternallyLinkage)
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
return ThunkFn;
|
2019-09-07 06:55:26 +08:00
|
|
|
ThunkFn =
|
|
|
|
CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, TI);
|
2011-05-07 01:27:27 +08:00
|
|
|
} else {
|
|
|
|
// Normal thunk body generation.
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, TI, IsUnprototyped);
|
2011-05-07 01:27:27 +08:00
|
|
|
}
|
2015-07-01 03:07:26 +08:00
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
setThunkProperties(CGM, TI, ThunkFn, ForVTable, GD);
|
|
|
|
return ThunkFn;
|
2010-03-24 00:36:50 +08:00
|
|
|
}
|
|
|
|
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
void CodeGenVTables::EmitThunks(GlobalDecl GD) {
|
2016-09-08 17:59:58 +08:00
|
|
|
const CXXMethodDecl *MD =
|
2010-03-24 00:36:50 +08:00
|
|
|
cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
|
|
|
|
|
|
|
|
// We don't need to generate thunks for the base destructor.
|
|
|
|
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
|
|
|
|
return;
|
|
|
|
|
2013-12-21 07:58:52 +08:00
|
|
|
const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector =
|
|
|
|
VTContext->getThunkInfo(GD);
|
2013-07-30 17:46:19 +08:00
|
|
|
|
2011-09-26 09:56:41 +08:00
|
|
|
if (!ThunkInfoVector)
|
2010-03-25 00:42:11 +08:00
|
|
|
return;
|
|
|
|
|
2015-08-02 03:11:36 +08:00
|
|
|
for (const ThunkInfo& Thunk : *ThunkInfoVector)
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
maybeEmitThunk(GD, Thunk, /*ForVTable=*/false);
|
2010-03-23 12:59:02 +08:00
|
|
|
}
|
|
|
|
|
2020-06-12 02:17:08 +08:00
|
|
|
void CodeGenVTables::addRelativeComponent(ConstantArrayBuilder &builder,
|
|
|
|
llvm::Constant *component,
|
|
|
|
unsigned vtableAddressPoint,
|
|
|
|
bool vtableHasLocalLinkage,
|
|
|
|
bool isCompleteDtor) const {
|
|
|
|
// No need to get the offset of a nullptr.
|
|
|
|
if (component->isNullValue())
|
|
|
|
return builder.add(llvm::ConstantInt::get(CGM.Int32Ty, 0));
|
|
|
|
|
|
|
|
auto *globalVal =
|
|
|
|
cast<llvm::GlobalValue>(component->stripPointerCastsAndAliases());
|
|
|
|
llvm::Module &module = CGM.getModule();
|
|
|
|
|
|
|
|
// We don't want to copy the linkage of the vtable exactly because we still
|
|
|
|
// want the stub/proxy to be emitted for properly calculating the offset.
|
|
|
|
// Examples where there would be no symbol emitted are available_externally
|
|
|
|
// and private linkages.
|
|
|
|
auto stubLinkage = vtableHasLocalLinkage ? llvm::GlobalValue::InternalLinkage
|
|
|
|
: llvm::GlobalValue::ExternalLinkage;
|
|
|
|
|
|
|
|
llvm::Constant *target;
|
|
|
|
if (auto *func = dyn_cast<llvm::Function>(globalVal)) {
|
|
|
|
target = getOrCreateRelativeStub(func, stubLinkage, isCompleteDtor);
|
|
|
|
} else {
|
|
|
|
llvm::SmallString<16> rttiProxyName(globalVal->getName());
|
|
|
|
rttiProxyName.append(".rtti_proxy");
|
|
|
|
|
|
|
|
// The RTTI component may not always be emitted in the same linkage unit as
|
|
|
|
// the vtable. As a general case, we can make a dso_local proxy to the RTTI
|
|
|
|
// that points to the actual RTTI struct somewhere. This will result in a
|
|
|
|
// GOTPCREL relocation when taking the relative offset to the proxy.
|
|
|
|
llvm::GlobalVariable *proxy = module.getNamedGlobal(rttiProxyName);
|
|
|
|
if (!proxy) {
|
|
|
|
proxy = new llvm::GlobalVariable(module, globalVal->getType(),
|
|
|
|
/*isConstant=*/true, stubLinkage,
|
|
|
|
globalVal, rttiProxyName);
|
|
|
|
proxy->setDSOLocal(true);
|
|
|
|
proxy->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
|
|
|
if (!proxy->hasLocalLinkage()) {
|
|
|
|
proxy->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
|
|
|
proxy->setComdat(module.getOrInsertComdat(rttiProxyName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target = proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.addRelativeOffsetToPosition(CGM.Int32Ty, target,
|
|
|
|
/*position=*/vtableAddressPoint);
|
|
|
|
}
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2020-06-12 02:17:08 +08:00
|
|
|
llvm::Function *CodeGenVTables::getOrCreateRelativeStub(
|
|
|
|
llvm::Function *func, llvm::GlobalValue::LinkageTypes stubLinkage,
|
|
|
|
bool isCompleteDtor) const {
|
|
|
|
// A complete object destructor can later be substituted in the vtable for an
|
|
|
|
// appropriate base object destructor when optimizations are enabled. This can
|
|
|
|
// happen for child classes that don't have their own destructor. In the case
|
|
|
|
// where a parent virtual destructor is not guaranteed to be in the same
|
|
|
|
// linkage unit as the child vtable, it's possible for an external reference
|
|
|
|
// for this destructor to be substituted into the child vtable, preventing it
|
|
|
|
// from being in rodata. If this function is a complete virtual destructor, we
|
|
|
|
// can just force a stub to be emitted for it.
|
|
|
|
if (func->isDSOLocal() && !isCompleteDtor)
|
|
|
|
return func;
|
|
|
|
|
|
|
|
llvm::SmallString<16> stubName(func->getName());
|
|
|
|
stubName.append(".stub");
|
|
|
|
|
|
|
|
// Instead of taking the offset between the vtable and virtual function
|
|
|
|
// directly, we emit a dso_local stub that just contains a tail call to the
|
|
|
|
// original virtual function and take the offset between that and the
|
|
|
|
// vtable. We do this because there are some cases where the original
|
|
|
|
// function that would've been inserted into the vtable is not dso_local
|
|
|
|
// which may require some kind of dynamic relocation which prevents the
|
|
|
|
// vtable from being readonly. On x86_64, taking the offset between the
|
|
|
|
// function and the vtable gets lowered to the offset between the PLT entry
|
|
|
|
// for the function and the vtable which gives us a PLT32 reloc. On AArch64,
|
|
|
|
// right now only CALL26 and JUMP26 instructions generate PLT relocations,
|
|
|
|
// so we manifest them with stubs that are just jumps to the original
|
|
|
|
// function.
|
|
|
|
auto &module = CGM.getModule();
|
|
|
|
llvm::Function *stub = module.getFunction(stubName);
|
|
|
|
if (stub) {
|
|
|
|
assert(stub->isDSOLocal() &&
|
|
|
|
"The previous definition of this stub should've been dso_local.");
|
|
|
|
return stub;
|
|
|
|
}
|
|
|
|
|
|
|
|
stub = llvm::Function::Create(func->getFunctionType(), stubLinkage, stubName,
|
|
|
|
module);
|
|
|
|
|
|
|
|
// Propogate function attributes.
|
|
|
|
stub->setAttributes(func->getAttributes());
|
|
|
|
|
|
|
|
stub->setDSOLocal(true);
|
|
|
|
stub->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
|
|
|
if (!stub->hasLocalLinkage()) {
|
|
|
|
stub->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
|
|
|
stub->setComdat(module.getOrInsertComdat(stubName));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the stub with a tail call that will be optimized.
|
|
|
|
llvm::BasicBlock *block =
|
|
|
|
llvm::BasicBlock::Create(module.getContext(), "entry", stub);
|
|
|
|
llvm::IRBuilder<> block_builder(block);
|
|
|
|
llvm::SmallVector<llvm::Value *, 8> args;
|
|
|
|
for (auto &arg : stub->args())
|
|
|
|
args.push_back(&arg);
|
|
|
|
llvm::CallInst *call = block_builder.CreateCall(func, args);
|
|
|
|
call->setAttributes(func->getAttributes());
|
|
|
|
call->setTailCall();
|
|
|
|
if (call->getType()->isVoidTy())
|
|
|
|
block_builder.CreateRetVoid();
|
|
|
|
else
|
|
|
|
block_builder.CreateRet(call);
|
|
|
|
|
|
|
|
return stub;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeGenVTables::useRelativeLayout() const {
|
|
|
|
return CGM.getTarget().getCXXABI().isItaniumFamily() &&
|
|
|
|
CGM.getItaniumVTableContext().isRelativeLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Type *CodeGenVTables::getVTableComponentType() const {
|
|
|
|
if (useRelativeLayout())
|
|
|
|
return CGM.Int32Ty;
|
|
|
|
return CGM.Int8PtrTy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddPointerLayoutOffset(const CodeGenModule &CGM,
|
|
|
|
ConstantArrayBuilder &builder,
|
|
|
|
CharUnits offset) {
|
|
|
|
builder.add(llvm::ConstantExpr::getIntToPtr(
|
|
|
|
llvm::ConstantInt::get(CGM.PtrDiffTy, offset.getQuantity()),
|
|
|
|
CGM.Int8PtrTy));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddRelativeLayoutOffset(const CodeGenModule &CGM,
|
|
|
|
ConstantArrayBuilder &builder,
|
|
|
|
CharUnits offset) {
|
|
|
|
builder.add(llvm::ConstantInt::get(CGM.Int32Ty, offset.getQuantity()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenVTables::addVTableComponent(ConstantArrayBuilder &builder,
|
|
|
|
const VTableLayout &layout,
|
|
|
|
unsigned componentIndex,
|
|
|
|
llvm::Constant *rtti,
|
|
|
|
unsigned &nextVTableThunkIndex,
|
|
|
|
unsigned vtableAddressPoint,
|
|
|
|
bool vtableHasLocalLinkage) {
|
|
|
|
auto &component = layout.vtable_components()[componentIndex];
|
|
|
|
|
|
|
|
auto addOffsetConstant =
|
|
|
|
useRelativeLayout() ? AddRelativeLayoutOffset : AddPointerLayoutOffset;
|
2010-03-26 00:49:53 +08:00
|
|
|
|
2016-11-29 06:18:33 +08:00
|
|
|
switch (component.getKind()) {
|
2016-09-08 09:14:39 +08:00
|
|
|
case VTableComponent::CK_VCallOffset:
|
2020-06-12 02:17:08 +08:00
|
|
|
return addOffsetConstant(CGM, builder, component.getVCallOffset());
|
2014-05-21 13:09:00 +08:00
|
|
|
|
2016-09-08 09:14:39 +08:00
|
|
|
case VTableComponent::CK_VBaseOffset:
|
2020-06-12 02:17:08 +08:00
|
|
|
return addOffsetConstant(CGM, builder, component.getVBaseOffset());
|
2010-03-29 13:40:50 +08:00
|
|
|
|
2016-09-08 09:14:39 +08:00
|
|
|
case VTableComponent::CK_OffsetToTop:
|
2020-06-12 02:17:08 +08:00
|
|
|
return addOffsetConstant(CGM, builder, component.getOffsetToTop());
|
2010-03-26 00:49:53 +08:00
|
|
|
|
2016-09-08 09:14:39 +08:00
|
|
|
case VTableComponent::CK_RTTI:
|
2020-06-12 02:17:08 +08:00
|
|
|
if (useRelativeLayout())
|
|
|
|
return addRelativeComponent(builder, rtti, vtableAddressPoint,
|
|
|
|
vtableHasLocalLinkage,
|
|
|
|
/*isCompleteDtor=*/false);
|
|
|
|
else
|
|
|
|
return builder.add(llvm::ConstantExpr::getBitCast(rtti, CGM.Int8PtrTy));
|
2010-03-26 00:49:53 +08:00
|
|
|
|
2016-09-08 09:14:39 +08:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
|
|
|
case VTableComponent::CK_DeletingDtorPointer: {
|
|
|
|
GlobalDecl GD;
|
|
|
|
|
|
|
|
// Get the right global decl.
|
2016-11-29 06:18:33 +08:00
|
|
|
switch (component.getKind()) {
|
2016-09-08 09:14:39 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected vtable component kind");
|
|
|
|
case VTableComponent::CK_FunctionPointer:
|
2016-11-29 06:18:33 +08:00
|
|
|
GD = component.getFunctionDecl();
|
2010-03-26 00:49:53 +08:00
|
|
|
break;
|
2016-09-08 09:14:39 +08:00
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
2016-11-29 06:18:33 +08:00
|
|
|
GD = GlobalDecl(component.getDestructorDecl(), Dtor_Complete);
|
2010-03-26 00:49:53 +08:00
|
|
|
break;
|
2016-09-08 09:14:39 +08:00
|
|
|
case VTableComponent::CK_DeletingDtorPointer:
|
2016-11-29 06:18:33 +08:00
|
|
|
GD = GlobalDecl(component.getDestructorDecl(), Dtor_Deleting);
|
2010-03-26 00:49:53 +08:00
|
|
|
break;
|
2016-09-08 09:14:39 +08:00
|
|
|
}
|
2010-03-26 00:49:53 +08:00
|
|
|
|
2016-09-08 09:14:39 +08:00
|
|
|
if (CGM.getLangOpts().CUDA) {
|
|
|
|
// Emit NULL for methods we can't codegen on this
|
|
|
|
// side. Otherwise we'd end up with vtable with unresolved
|
|
|
|
// references.
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
// OK on device side: functions w/ __device__ attribute
|
|
|
|
// OK on host side: anything except __device__-only functions.
|
|
|
|
bool CanEmitMethod =
|
|
|
|
CGM.getLangOpts().CUDAIsDevice
|
|
|
|
? MD->hasAttr<CUDADeviceAttr>()
|
|
|
|
: (MD->hasAttr<CUDAHostAttr>() || !MD->hasAttr<CUDADeviceAttr>());
|
|
|
|
if (!CanEmitMethod)
|
2020-06-12 02:17:08 +08:00
|
|
|
return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int8PtrTy));
|
2016-09-08 09:14:39 +08:00
|
|
|
// Method is acceptable, continue processing as usual.
|
|
|
|
}
|
2015-12-18 02:12:36 +08:00
|
|
|
|
2020-01-15 05:42:23 +08:00
|
|
|
auto getSpecialVirtualFn = [&](StringRef name) -> llvm::Constant * {
|
2020-06-12 02:17:08 +08:00
|
|
|
// FIXME(PR43094): When merging comdat groups, lld can select a local
|
|
|
|
// symbol as the signature symbol even though it cannot be accessed
|
|
|
|
// outside that symbol's TU. The relative vtables ABI would make
|
|
|
|
// __cxa_pure_virtual and __cxa_deleted_virtual local symbols, and
|
|
|
|
// depending on link order, the comdat groups could resolve to the one
|
|
|
|
// 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);
|
|
|
|
|
2020-01-15 05:42:23 +08:00
|
|
|
// 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 &&
|
|
|
|
CGM.getTriple().isNVPTX())
|
|
|
|
return llvm::ConstantPointerNull::get(CGM.Int8PtrTy);
|
2016-11-29 06:18:33 +08:00
|
|
|
llvm::FunctionType *fnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
|
2019-02-06 00:42:33 +08:00
|
|
|
llvm::Constant *fn = cast<llvm::Constant>(
|
|
|
|
CGM.CreateRuntimeFunction(fnTy, name).getCallee());
|
2016-11-29 06:18:33 +08:00
|
|
|
if (auto f = dyn_cast<llvm::Function>(fn))
|
|
|
|
f->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
|
|
|
return llvm::ConstantExpr::getBitCast(fn, CGM.Int8PtrTy);
|
2016-09-08 09:14:39 +08:00
|
|
|
};
|
|
|
|
|
2016-11-29 06:18:33 +08:00
|
|
|
llvm::Constant *fnPtr;
|
|
|
|
|
|
|
|
// Pure virtual member functions.
|
|
|
|
if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
|
|
|
|
if (!PureVirtualFn)
|
|
|
|
PureVirtualFn =
|
2020-06-12 02:17:08 +08:00
|
|
|
getSpecialVirtualFn(CGM.getCXXABI().GetPureVirtualCallName());
|
2016-11-29 06:18:33 +08:00
|
|
|
fnPtr = PureVirtualFn;
|
|
|
|
|
|
|
|
// Deleted virtual member functions.
|
|
|
|
} else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
|
|
|
|
if (!DeletedVirtualFn)
|
|
|
|
DeletedVirtualFn =
|
2020-06-12 02:17:08 +08:00
|
|
|
getSpecialVirtualFn(CGM.getCXXABI().GetDeletedVirtualCallName());
|
2016-11-29 06:18:33 +08:00
|
|
|
fnPtr = DeletedVirtualFn;
|
|
|
|
|
|
|
|
// Thunks.
|
|
|
|
} else if (nextVTableThunkIndex < layout.vtable_thunks().size() &&
|
2020-06-12 02:17:08 +08:00
|
|
|
layout.vtable_thunks()[nextVTableThunkIndex].first ==
|
|
|
|
componentIndex) {
|
2016-11-29 06:18:33 +08:00
|
|
|
auto &thunkInfo = layout.vtable_thunks()[nextVTableThunkIndex].second;
|
|
|
|
|
|
|
|
nextVTableThunkIndex++;
|
[MS] Emit vftable thunks for functions with incomplete prototypes
Summary:
The following class hierarchy requires that we be able to emit a
this-adjusting thunk for B::foo in C's vftable:
struct Incomplete;
struct A {
virtual A* foo(Incomplete p) = 0;
};
struct B : virtual A {
void foo(Incomplete p) override;
};
struct C : B { int c; };
This TU is valid, but lacks a definition of 'Incomplete', which makes it
hard to build a thunk for the final overrider, B::foo.
Before this change, Clang gives up attempting to emit the thunk, because
it assumes that if the parameter types are incomplete, it must be
emitting the thunk for optimization purposes. This is untrue for the MS
ABI, where the implementation of B::foo has no idea what thunks C's
vftable may require. Clang needs to emit the thunk without necessarily
having access to the complete prototype of foo.
This change makes Clang emit a musttail variadic call when it needs such
a thunk. I call these "unprototyped" thunks, because they only prototype
the "this" parameter, which must always come first in the MS C++ ABI.
These thunks work, but they create ugly LLVM IR. If the call to the
thunk is devirtualized, it will be a call to a bitcast of a function
pointer. Today, LLVM cannot inline through such a call, but I want to
address that soon, because we also use this pattern for virtual member
pointer thunks.
This change also implements an old FIXME in the code about reusing the
thunk's computed CGFunctionInfo as much as possible. Now we don't end up
computing the thunk's mangled name and arranging it's prototype up to
around three times.
Fixes PR25641
Reviewers: rjmccall, rsmith, hans
Subscribers: Prazek, cfe-commits
Differential Revision: https://reviews.llvm.org/D45112
llvm-svn: 329009
2018-04-03 04:20:33 +08:00
|
|
|
fnPtr = maybeEmitThunk(GD, thunkInfo, /*ForVTable=*/true);
|
2016-11-29 06:18:33 +08:00
|
|
|
|
|
|
|
// Otherwise we can use the method definition directly.
|
|
|
|
} else {
|
|
|
|
llvm::Type *fnTy = CGM.getTypes().GetFunctionTypeForVTable(GD);
|
|
|
|
fnPtr = CGM.GetAddrOfFunction(GD, fnTy, /*ForVTable=*/true);
|
2010-03-26 00:49:53 +08:00
|
|
|
}
|
|
|
|
|
2020-06-12 02:17:08 +08:00
|
|
|
if (useRelativeLayout()) {
|
|
|
|
return addRelativeComponent(
|
|
|
|
builder, fnPtr, vtableAddressPoint, vtableHasLocalLinkage,
|
|
|
|
component.getKind() == VTableComponent::CK_CompleteDtorPointer);
|
|
|
|
} else
|
|
|
|
return builder.add(llvm::ConstantExpr::getBitCast(fnPtr, CGM.Int8PtrTy));
|
2010-03-25 23:26:28 +08:00
|
|
|
}
|
2016-09-08 09:14:39 +08:00
|
|
|
|
|
|
|
case VTableComponent::CK_UnusedFunctionPointer:
|
2020-06-12 02:17:08 +08:00
|
|
|
if (useRelativeLayout())
|
|
|
|
return builder.add(llvm::ConstantExpr::getNullValue(CGM.Int32Ty));
|
|
|
|
else
|
|
|
|
return builder.addNullPointer(CGM.Int8PtrTy);
|
2016-09-08 09:14:39 +08:00
|
|
|
}
|
2016-09-08 19:03:41 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Unexpected vtable component kind");
|
2016-09-08 09:14:39 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 04:40:39 +08:00
|
|
|
llvm::Type *CodeGenVTables::getVTableType(const VTableLayout &layout) {
|
|
|
|
SmallVector<llvm::Type *, 4> tys;
|
2020-06-12 02:17:08 +08:00
|
|
|
llvm::Type *componentType = getVTableComponentType();
|
|
|
|
for (unsigned i = 0, e = layout.getNumVTables(); i != e; ++i)
|
|
|
|
tys.push_back(llvm::ArrayType::get(componentType, layout.getVTableSize(i)));
|
2016-12-14 04:40:39 +08:00
|
|
|
|
|
|
|
return llvm::StructType::get(CGM.getLLVMContext(), tys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenVTables::createVTableInitializer(ConstantStructBuilder &builder,
|
2016-11-29 06:18:33 +08:00
|
|
|
const VTableLayout &layout,
|
2020-06-12 02:17:08 +08:00
|
|
|
llvm::Constant *rtti,
|
|
|
|
bool vtableHasLocalLinkage) {
|
|
|
|
llvm::Type *componentType = getVTableComponentType();
|
|
|
|
|
|
|
|
const auto &addressPoints = layout.getAddressPointIndices();
|
2016-11-29 06:18:33 +08:00
|
|
|
unsigned nextVTableThunkIndex = 0;
|
2020-06-12 02:17:08 +08:00
|
|
|
for (unsigned vtableIndex = 0, endIndex = layout.getNumVTables();
|
|
|
|
vtableIndex != endIndex; ++vtableIndex) {
|
|
|
|
auto vtableElem = builder.beginArray(componentType);
|
|
|
|
|
|
|
|
size_t vtableStart = layout.getVTableOffset(vtableIndex);
|
|
|
|
size_t vtableEnd = vtableStart + layout.getVTableSize(vtableIndex);
|
|
|
|
for (size_t componentIndex = vtableStart; componentIndex < vtableEnd;
|
|
|
|
++componentIndex) {
|
|
|
|
addVTableComponent(vtableElem, layout, componentIndex, rtti,
|
|
|
|
nextVTableThunkIndex, addressPoints[vtableIndex],
|
|
|
|
vtableHasLocalLinkage);
|
2016-12-14 04:40:39 +08:00
|
|
|
}
|
|
|
|
vtableElem.finishAndAddTo(builder);
|
2016-09-08 09:14:39 +08:00
|
|
|
}
|
2010-03-25 23:26:28 +08:00
|
|
|
}
|
|
|
|
|
2020-06-12 02:17:08 +08:00
|
|
|
llvm::GlobalVariable *CodeGenVTables::GenerateConstructionVTable(
|
|
|
|
const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
|
|
|
VTableAddressPointsMapTy &AddressPoints) {
|
2013-08-22 23:23:05 +08:00
|
|
|
if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
|
|
|
|
DI->completeClassData(Base.getBase());
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<VTableLayout> VTLayout(
|
2013-12-21 07:58:52 +08:00
|
|
|
getItaniumVTableContext().createConstructionVTableLayout(
|
2013-11-05 23:54:58 +08:00
|
|
|
Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2010-03-26 00:49:53 +08:00
|
|
|
// Add the address points.
|
2011-09-26 09:57:04 +08:00
|
|
|
AddressPoints = VTLayout->getAddressPoints();
|
2010-03-25 23:26:28 +08:00
|
|
|
|
|
|
|
// Get the mangled construction vtable name.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> OutName;
|
2011-02-11 10:52:17 +08:00
|
|
|
llvm::raw_svector_ostream Out(OutName);
|
2013-10-03 14:26:13 +08:00
|
|
|
cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
|
|
|
|
.mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
|
|
|
|
Base.getBase(), Out);
|
2020-06-12 02:17:08 +08:00
|
|
|
SmallString<256> Name(OutName);
|
|
|
|
|
|
|
|
bool UsingRelativeLayout = getItaniumVTableContext().isRelativeLayout();
|
|
|
|
bool VTableAliasExists =
|
|
|
|
UsingRelativeLayout && CGM.getModule().getNamedAlias(Name);
|
|
|
|
if (VTableAliasExists) {
|
|
|
|
// We previously made the vtable hidden and changed its name.
|
|
|
|
Name.append(".local");
|
|
|
|
}
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2016-12-14 04:40:39 +08:00
|
|
|
llvm::Type *VTType = getVTableType(*VTLayout);
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2013-02-16 08:51:21 +08:00
|
|
|
// Construction vtable symbols are not part of the Itanium ABI, so we cannot
|
|
|
|
// guarantee that they actually will be available externally. Instead, when
|
|
|
|
// emitting an available_externally VTT, we provide references to an internal
|
|
|
|
// linkage construction vtable. The ABI only requires complete-object vtables
|
|
|
|
// to be the same for all instances of a type, not construction vtables.
|
|
|
|
if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
|
|
|
|
Linkage = llvm::GlobalVariable::InternalLinkage;
|
|
|
|
|
2018-09-12 22:09:06 +08:00
|
|
|
unsigned Align = CGM.getDataLayout().getABITypeAlignment(VTType);
|
|
|
|
|
2010-03-25 23:26:28 +08:00
|
|
|
// Create the variable that will hold the construction vtable.
|
2016-09-08 17:59:58 +08:00
|
|
|
llvm::GlobalVariable *VTable =
|
2018-09-12 22:09:06 +08:00
|
|
|
CGM.CreateOrReplaceCXXRuntimeVariable(Name, VTType, Linkage, Align);
|
2011-03-27 17:00:25 +08:00
|
|
|
|
|
|
|
// V-tables are always unnamed_addr.
|
2016-06-15 05:02:05 +08:00
|
|
|
VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
2010-03-25 23:26:28 +08:00
|
|
|
|
2014-07-02 04:30:31 +08:00
|
|
|
llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(
|
|
|
|
CGM.getContext().getTagDeclType(Base.getBase()));
|
|
|
|
|
2010-03-25 23:26:28 +08:00
|
|
|
// Create and set the initializer.
|
2016-11-29 06:18:33 +08:00
|
|
|
ConstantInitBuilder builder(CGM);
|
2016-12-14 04:40:39 +08:00
|
|
|
auto components = builder.beginStruct();
|
2020-06-12 02:17:08 +08:00
|
|
|
createVTableInitializer(components, *VTLayout, RTTI,
|
|
|
|
VTable->hasLocalLinkage());
|
2016-11-29 06:18:33 +08:00
|
|
|
components.finishAndSetAsInitializer(VTable);
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2019-02-12 04:13:42 +08:00
|
|
|
// Set properties only after the initializer has been set to ensure that the
|
|
|
|
// GV is treated as definition and not declaration.
|
|
|
|
assert(!VTable->isDeclaration() && "Shouldn't set properties on declaration");
|
|
|
|
CGM.setGVProperties(VTable, RD);
|
|
|
|
|
Reland: Dead Virtual Function Elimination
Remove dead virtual functions from vtables with
replaceNonMetadataUsesWith, so that CGProfile metadata gets cleaned up
correctly.
Original commit message:
Currently, it is hard for the compiler to remove unused C++ virtual
functions, because they are all referenced from vtables, which are referenced
by constructors. This means that if the constructor is called from any live
code, then we keep every virtual function in the final link, even if there
are no call sites which can use it.
This patch allows unused virtual functions to be removed during LTO (and
regular compilation in limited circumstances) by using type metadata to match
virtual function call sites to the vtable slots they might load from. This
information can then be used in the global dead code elimination pass instead
of the references from vtables to virtual functions, to more accurately
determine which functions are reachable.
To make this transformation safe, I have changed clang's code-generation to
always load virtual function pointers using the llvm.type.checked.load
intrinsic, instead of regular load instructions. I originally tried writing
this using clang's existing code-generation, which uses the llvm.type.test
and llvm.assume intrinsics after doing a normal load. However, it is possible
for optimisations to obscure the relationship between the GEP, load and
llvm.type.test, causing GlobalDCE to fail to find virtual function call
sites.
The existing linkage and visibility types don't accurately describe the scope
in which a virtual call could be made which uses a given vtable. This is
wider than the visibility of the type itself, because a virtual function call
could be made using a more-visible base class. I've added a new
!vcall_visibility metadata type to represent this, described in
TypeMetadata.rst. The internalization pass and libLTO have been updated to
change this metadata when linking is performed.
This doesn't currently work with ThinLTO, because it needs to see every call
to llvm.type.checked.load in the linkage unit. It might be possible to
extend this optimisation to be able to use the ThinLTO summary, as was done
for devirtualization, but until then that combination is rejected in the
clang driver.
To test this, I've written a fuzzer which generates random C++ programs with
complex class inheritance graphs, and virtual functions called through object
and function pointers of different types. The programs are spread across
multiple translation units and DSOs to test the different visibility
restrictions.
I've also tried doing bootstrap builds of LLVM to test this. This isn't
ideal, because only classes in anonymous namespaces can be optimised with
-fvisibility=default, and some parts of LLVM (plugins and bugpoint) do not
work correctly with -fvisibility=hidden. However, there are only 12 test
failures when building with -fvisibility=hidden (and an unmodified compiler),
and this change does not cause any new failures for either value of
-fvisibility.
On the 7 C++ sub-benchmarks of SPEC2006, this gives a geomean code-size
reduction of ~6%, over a baseline compiled with "-O2 -flto
-fvisibility=hidden -fwhole-program-vtables". The best cases are reductions
of ~14% in 450.soplex and 483.xalancbmk, and there are no code size
increases.
I've also run this on a set of 8 mbed-os examples compiled for Armv7M, which
show a geomean size reduction of ~3%, again with no size increases.
I had hoped that this would have no effect on performance, which would allow
it to awlays be enabled (when using -fwhole-program-vtables). However, the
changes in clang to use the llvm.type.checked.load intrinsic are causing ~1%
performance regression in the C++ parts of SPEC2006. It should be possible to
recover some of this perf loss by teaching optimisations about the
llvm.type.checked.load intrinsic, which would make it worth turning this on
by default (though it's still dependent on -fwhole-program-vtables).
Differential revision: https://reviews.llvm.org/D63932
llvm-svn: 375094
2019-10-17 17:58:57 +08:00
|
|
|
CGM.EmitVTableTypeMetadata(RD, VTable, *VTLayout.get());
|
2015-02-21 04:30:56 +08:00
|
|
|
|
2020-06-12 02:17:08 +08:00
|
|
|
if (UsingRelativeLayout && !VTable->isDSOLocal())
|
|
|
|
GenerateRelativeVTableAlias(VTable, OutName);
|
|
|
|
|
2010-03-25 08:35:49 +08:00
|
|
|
return VTable;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:17:08 +08:00
|
|
|
// If the VTable is not dso_local, then we will not be able to indicate that
|
|
|
|
// the VTable does not need a relocation and move into rodata. A frequent
|
|
|
|
// time this can occur is for classes that should be made public from a DSO
|
|
|
|
// (like in libc++). For cases like these, we can make the vtable hidden or
|
|
|
|
// private and create a public alias with the same visibility and linkage as
|
|
|
|
// the original vtable type.
|
|
|
|
void CodeGenVTables::GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable,
|
|
|
|
llvm::StringRef AliasNameRef) {
|
|
|
|
assert(getItaniumVTableContext().isRelativeLayout() &&
|
|
|
|
"Can only use this if the relative vtable ABI is used");
|
|
|
|
assert(!VTable->isDSOLocal() && "This should be called only if the vtable is "
|
|
|
|
"not guaranteed to be dso_local");
|
|
|
|
|
|
|
|
// If the vtable is available_externally, we shouldn't (or need to) generate
|
|
|
|
// an alias for it in the first place since the vtable won't actually by
|
|
|
|
// emitted in this compilation unit.
|
|
|
|
if (VTable->hasAvailableExternallyLinkage())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a new string in the event the alias is already the name of the
|
|
|
|
// vtable. Using the reference directly could lead to use of an inititialized
|
|
|
|
// value in the module's StringMap.
|
|
|
|
llvm::SmallString<256> AliasName(AliasNameRef);
|
|
|
|
VTable->setName(AliasName + ".local");
|
|
|
|
|
|
|
|
auto Linkage = VTable->getLinkage();
|
|
|
|
assert(llvm::GlobalAlias::isValidLinkage(Linkage) &&
|
|
|
|
"Invalid vtable alias linkage");
|
|
|
|
|
|
|
|
llvm::GlobalAlias *VTableAlias = CGM.getModule().getNamedAlias(AliasName);
|
|
|
|
if (!VTableAlias) {
|
|
|
|
VTableAlias = llvm::GlobalAlias::create(VTable->getValueType(),
|
|
|
|
VTable->getAddressSpace(), Linkage,
|
|
|
|
AliasName, &CGM.getModule());
|
|
|
|
} else {
|
|
|
|
assert(VTableAlias->getValueType() == VTable->getValueType());
|
|
|
|
assert(VTableAlias->getLinkage() == Linkage);
|
|
|
|
}
|
|
|
|
VTableAlias->setVisibility(VTable->getVisibility());
|
|
|
|
VTableAlias->setUnnamedAddr(VTable->getUnnamedAddr());
|
|
|
|
|
|
|
|
// Both of these imply dso_local for the vtable.
|
|
|
|
if (!VTable->hasComdat()) {
|
|
|
|
// If this is in a comdat, then we shouldn't make the linkage private due to
|
|
|
|
// an issue in lld where private symbols can be used as the key symbol when
|
|
|
|
// choosing the prevelant group. This leads to "relocation refers to a
|
|
|
|
// symbol in a discarded section".
|
|
|
|
VTable->setLinkage(llvm::GlobalValue::PrivateLinkage);
|
|
|
|
} else {
|
|
|
|
// We should at least make this hidden since we don't want to expose it.
|
|
|
|
VTable->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
VTableAlias->setAliasee(VTable);
|
|
|
|
}
|
|
|
|
|
2015-07-24 12:04:49 +08:00
|
|
|
static bool shouldEmitAvailableExternallyVTable(const CodeGenModule &CGM,
|
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
return CGM.getCodeGenOpts().OptimizationLevel > 0 &&
|
2015-09-15 08:37:06 +08:00
|
|
|
CGM.getCXXABI().canSpeculativelyEmitVTable(RD);
|
2015-07-24 12:04:49 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 09:35:53 +08:00
|
|
|
/// Compute the required linkage of the vtable for the given class.
|
2013-01-26 06:31:03 +08:00
|
|
|
///
|
|
|
|
/// Note that we only call this at the end of the translation unit.
|
2016-09-08 17:59:58 +08:00
|
|
|
llvm::GlobalVariable::LinkageTypes
|
2013-01-26 06:31:03 +08:00
|
|
|
CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
|
2013-05-13 08:12:11 +08:00
|
|
|
if (!RD->isExternallyVisible())
|
2013-01-26 06:31:03 +08:00
|
|
|
return llvm::GlobalVariable::InternalLinkage;
|
|
|
|
|
|
|
|
// We're at the end of the translation unit, so the current key
|
|
|
|
// function is fully correct.
|
2014-10-24 06:40:46 +08:00
|
|
|
const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD);
|
|
|
|
if (keyFunction && !RD->hasAttr<DLLImportAttr>()) {
|
2013-01-26 06:31:03 +08:00
|
|
|
// If this class has a key function, use that to determine the
|
|
|
|
// linkage of the vtable.
|
2014-05-21 13:09:00 +08:00
|
|
|
const FunctionDecl *def = nullptr;
|
2013-01-26 06:31:03 +08:00
|
|
|
if (keyFunction->hasBody(def))
|
|
|
|
keyFunction = cast<CXXMethodDecl>(def);
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
switch (keyFunction->getTemplateSpecializationKind()) {
|
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
2017-01-30 14:36:08 +08:00
|
|
|
assert((def || CodeGenOpts.OptimizationLevel > 0 ||
|
|
|
|
CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo) &&
|
|
|
|
"Shouldn't query vtable linkage without key function, "
|
|
|
|
"optimizations, or debug info");
|
2015-07-24 12:04:49 +08:00
|
|
|
if (!def && CodeGenOpts.OptimizationLevel > 0)
|
|
|
|
return llvm::GlobalVariable::AvailableExternallyLinkage;
|
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
if (keyFunction->isInlined())
|
|
|
|
return !Context.getLangOpts().AppleKext ?
|
|
|
|
llvm::GlobalVariable::LinkOnceODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
return llvm::GlobalVariable::ExternalLinkage;
|
2015-07-02 22:44:35 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
return !Context.getLangOpts().AppleKext ?
|
|
|
|
llvm::GlobalVariable::LinkOnceODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return !Context.getLangOpts().AppleKext ?
|
|
|
|
llvm::GlobalVariable::WeakODRLinkage :
|
|
|
|
llvm::Function::InternalLinkage;
|
2016-09-08 17:59:58 +08:00
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
2013-09-04 05:05:13 +08:00
|
|
|
llvm_unreachable("Should not have been asked to emit this");
|
2013-01-26 06:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -fapple-kext mode does not support weak linkage, so we must use
|
|
|
|
// internal linkage.
|
|
|
|
if (Context.getLangOpts().AppleKext)
|
|
|
|
return llvm::Function::InternalLinkage;
|
2014-05-31 00:59:42 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable::LinkageTypes DiscardableODRLinkage =
|
|
|
|
llvm::GlobalValue::LinkOnceODRLinkage;
|
|
|
|
llvm::GlobalVariable::LinkageTypes NonDiscardableODRLinkage =
|
|
|
|
llvm::GlobalValue::WeakODRLinkage;
|
|
|
|
if (RD->hasAttr<DLLExportAttr>()) {
|
|
|
|
// Cannot discard exported vtables.
|
|
|
|
DiscardableODRLinkage = NonDiscardableODRLinkage;
|
|
|
|
} else if (RD->hasAttr<DLLImportAttr>()) {
|
|
|
|
// Imported vtables are available externally.
|
|
|
|
DiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
|
|
|
|
NonDiscardableODRLinkage = llvm::GlobalVariable::AvailableExternallyLinkage;
|
|
|
|
}
|
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
switch (RD->getTemplateSpecializationKind()) {
|
2015-07-24 12:04:49 +08:00
|
|
|
case TSK_Undeclared:
|
|
|
|
case TSK_ExplicitSpecialization:
|
|
|
|
case TSK_ImplicitInstantiation:
|
|
|
|
return DiscardableODRLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDeclaration:
|
2016-06-30 02:29:21 +08:00
|
|
|
// Explicit instantiations in MSVC do not provide vtables, so we must emit
|
|
|
|
// our own.
|
|
|
|
if (getTarget().getCXXABI().isMicrosoft())
|
|
|
|
return DiscardableODRLinkage;
|
2015-07-24 12:04:49 +08:00
|
|
|
return shouldEmitAvailableExternallyVTable(*this, RD)
|
|
|
|
? llvm::GlobalVariable::AvailableExternallyLinkage
|
|
|
|
: llvm::GlobalVariable::ExternalLinkage;
|
|
|
|
|
|
|
|
case TSK_ExplicitInstantiationDefinition:
|
|
|
|
return NonDiscardableODRLinkage;
|
2013-01-26 06:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateSpecializationKind!");
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:14:32 +08:00
|
|
|
/// This is a callback from Sema to tell us that a particular vtable is
|
2015-01-15 12:07:35 +08:00
|
|
|
/// required to be emitted in this translation unit.
|
2013-01-26 06:31:03 +08:00
|
|
|
///
|
2015-01-15 12:07:35 +08:00
|
|
|
/// This is only called for vtables that _must_ be emitted (mainly due to key
|
|
|
|
/// functions). For weak vtables, CodeGen tracks when they are needed and
|
|
|
|
/// emits them as-needed.
|
|
|
|
void CodeGenModule::EmitVTable(CXXRecordDecl *theClass) {
|
2013-01-26 06:31:03 +08:00
|
|
|
VTables.GenerateClassData(theClass);
|
|
|
|
}
|
|
|
|
|
2016-09-08 17:59:58 +08:00
|
|
|
void
|
2013-01-26 06:31:03 +08:00
|
|
|
CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
|
2013-08-22 23:23:05 +08:00
|
|
|
if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
|
|
|
|
DI->completeClassData(RD);
|
|
|
|
|
2013-06-19 23:20:38 +08:00
|
|
|
if (RD->getNumVBases())
|
2013-09-27 22:48:01 +08:00
|
|
|
CGM.getCXXABI().emitVirtualInheritanceTables(RD);
|
|
|
|
|
|
|
|
CGM.getCXXABI().emitVTableDefinitions(*this, RD);
|
2010-03-29 11:38:52 +08:00
|
|
|
}
|
2013-01-26 06:31:03 +08:00
|
|
|
|
|
|
|
/// At this point in the translation unit, does it appear that can we
|
|
|
|
/// rely on the vtable being defined elsewhere in the program?
|
|
|
|
///
|
|
|
|
/// The response is really only definitive when called at the end of
|
|
|
|
/// the translation unit.
|
|
|
|
///
|
|
|
|
/// The only semantic restriction here is that the object file should
|
2016-01-29 09:35:53 +08:00
|
|
|
/// not contain a vtable definition when that vtable is defined
|
2013-01-26 06:31:03 +08:00
|
|
|
/// strongly elsewhere. Otherwise, we'd just like to avoid emitting
|
2016-01-29 09:35:53 +08:00
|
|
|
/// vtables when unnecessary.
|
2013-01-26 06:31:03 +08:00
|
|
|
bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
|
2013-12-05 12:47:09 +08:00
|
|
|
assert(RD->isDynamicClass() && "Non-dynamic classes have no VTable.");
|
2013-01-26 06:31:03 +08:00
|
|
|
|
2016-06-30 02:29:21 +08:00
|
|
|
// We always synthesize vtables if they are needed in the MS ABI. MSVC doesn't
|
|
|
|
// emit them even if there is an explicit template instantiation.
|
|
|
|
if (CGM.getTarget().getCXXABI().isMicrosoft())
|
2016-02-12 01:49:28 +08:00
|
|
|
return false;
|
|
|
|
|
2013-01-26 06:31:03 +08:00
|
|
|
// If we have an explicit instantiation declaration (and not a
|
2016-01-29 09:35:53 +08:00
|
|
|
// definition), the vtable is defined elsewhere.
|
2013-01-26 06:31:03 +08:00
|
|
|
TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
|
|
|
|
if (TSK == TSK_ExplicitInstantiationDeclaration)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, if the class is an instantiated template, the
|
2016-01-29 09:35:53 +08:00
|
|
|
// vtable must be defined here.
|
2013-01-26 06:31:03 +08:00
|
|
|
if (TSK == TSK_ImplicitInstantiation ||
|
|
|
|
TSK == TSK_ExplicitInstantiationDefinition)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, if the class doesn't have a key function (possibly
|
2016-01-29 09:35:53 +08:00
|
|
|
// anymore), the vtable must be defined here.
|
2013-01-26 06:31:03 +08:00
|
|
|
const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
|
|
|
|
if (!keyFunction)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, if we don't have a definition of the key function, the
|
2016-01-29 09:35:53 +08:00
|
|
|
// vtable must be defined somewhere else.
|
2013-01-26 06:31:03 +08:00
|
|
|
return !keyFunction->hasBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given that we're currently at the end of the translation unit, and
|
2016-01-29 09:35:53 +08:00
|
|
|
/// we've emitted a reference to the vtable for this class, should
|
|
|
|
/// we define that vtable?
|
2013-01-26 06:31:03 +08:00
|
|
|
static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
|
|
|
|
const CXXRecordDecl *RD) {
|
2015-09-15 08:37:06 +08:00
|
|
|
// If vtable is internal then it has to be done.
|
2015-07-24 12:04:49 +08:00
|
|
|
if (!CGM.getVTables().isVTableExternal(RD))
|
|
|
|
return true;
|
|
|
|
|
2015-09-15 08:37:06 +08:00
|
|
|
// If it's external then maybe we will need it as available_externally.
|
2015-07-24 12:04:49 +08:00
|
|
|
return shouldEmitAvailableExternallyVTable(CGM, RD);
|
2013-01-26 06:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given that at some point we emitted a reference to one or more
|
2016-01-29 09:35:53 +08:00
|
|
|
/// vtables, and that we are now at the end of the translation unit,
|
2013-01-26 06:31:03 +08:00
|
|
|
/// decide whether we should emit them.
|
|
|
|
void CodeGenModule::EmitDeferredVTables() {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Remember the size of DeferredVTables, because we're going to assume
|
|
|
|
// that this entire operation doesn't modify it.
|
|
|
|
size_t savedSize = DeferredVTables.size();
|
|
|
|
#endif
|
|
|
|
|
2015-07-29 00:10:58 +08:00
|
|
|
for (const CXXRecordDecl *RD : DeferredVTables)
|
2013-01-26 06:31:03 +08:00
|
|
|
if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
|
|
|
|
VTables.GenerateClassData(RD);
|
2017-06-01 16:04:05 +08:00
|
|
|
else if (shouldOpportunisticallyEmitVTables())
|
|
|
|
OpportunisticVTables.push_back(RD);
|
2013-01-26 06:31:03 +08:00
|
|
|
|
|
|
|
assert(savedSize == DeferredVTables.size() &&
|
2016-01-29 09:35:53 +08:00
|
|
|
"deferred extra vtables during vtable emission?");
|
2013-01-26 06:31:03 +08:00
|
|
|
DeferredVTables.clear();
|
|
|
|
}
|
2015-02-21 04:30:56 +08:00
|
|
|
|
2020-01-25 04:24:18 +08:00
|
|
|
bool CodeGenModule::HasLTOVisibilityPublicStd(const CXXRecordDecl *RD) {
|
|
|
|
if (!getCodeGenOpts().LTOVisibilityPublicStd)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const DeclContext *DC = RD;
|
|
|
|
while (1) {
|
|
|
|
auto *D = cast<Decl>(DC);
|
|
|
|
DC = DC->getParent();
|
|
|
|
if (isa<TranslationUnitDecl>(DC->getRedeclContext())) {
|
|
|
|
if (auto *ND = dyn_cast<NamespaceDecl>(D))
|
|
|
|
if (const IdentifierInfo *II = ND->getIdentifier())
|
|
|
|
if (II->isStr("std") || II->isStr("stdext"))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-29 01:09:37 +08:00
|
|
|
bool CodeGenModule::HasHiddenLTOVisibility(const CXXRecordDecl *RD) {
|
|
|
|
LinkageInfo LV = RD->getLinkageAndVisibility();
|
|
|
|
if (!isExternallyVisible(LV.getLinkage()))
|
|
|
|
return true;
|
2016-02-25 04:46:36 +08:00
|
|
|
|
2016-04-29 01:09:37 +08:00
|
|
|
if (RD->hasAttr<LTOVisibilityPublicAttr>() || RD->hasAttr<UuidAttr>())
|
|
|
|
return false;
|
2016-02-25 04:46:36 +08:00
|
|
|
|
2016-04-29 01:09:37 +08:00
|
|
|
if (getTriple().isOSBinFormatCOFF()) {
|
|
|
|
if (RD->hasAttr<DLLExportAttr>() || RD->hasAttr<DLLImportAttr>())
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (LV.getVisibility() != HiddenVisibility)
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-28 04:39:53 +08:00
|
|
|
|
2020-01-25 04:24:18 +08:00
|
|
|
return !HasLTOVisibilityPublicStd(RD);
|
2015-07-10 03:56:14 +08:00
|
|
|
}
|
|
|
|
|
Reland: Dead Virtual Function Elimination
Remove dead virtual functions from vtables with
replaceNonMetadataUsesWith, so that CGProfile metadata gets cleaned up
correctly.
Original commit message:
Currently, it is hard for the compiler to remove unused C++ virtual
functions, because they are all referenced from vtables, which are referenced
by constructors. This means that if the constructor is called from any live
code, then we keep every virtual function in the final link, even if there
are no call sites which can use it.
This patch allows unused virtual functions to be removed during LTO (and
regular compilation in limited circumstances) by using type metadata to match
virtual function call sites to the vtable slots they might load from. This
information can then be used in the global dead code elimination pass instead
of the references from vtables to virtual functions, to more accurately
determine which functions are reachable.
To make this transformation safe, I have changed clang's code-generation to
always load virtual function pointers using the llvm.type.checked.load
intrinsic, instead of regular load instructions. I originally tried writing
this using clang's existing code-generation, which uses the llvm.type.test
and llvm.assume intrinsics after doing a normal load. However, it is possible
for optimisations to obscure the relationship between the GEP, load and
llvm.type.test, causing GlobalDCE to fail to find virtual function call
sites.
The existing linkage and visibility types don't accurately describe the scope
in which a virtual call could be made which uses a given vtable. This is
wider than the visibility of the type itself, because a virtual function call
could be made using a more-visible base class. I've added a new
!vcall_visibility metadata type to represent this, described in
TypeMetadata.rst. The internalization pass and libLTO have been updated to
change this metadata when linking is performed.
This doesn't currently work with ThinLTO, because it needs to see every call
to llvm.type.checked.load in the linkage unit. It might be possible to
extend this optimisation to be able to use the ThinLTO summary, as was done
for devirtualization, but until then that combination is rejected in the
clang driver.
To test this, I've written a fuzzer which generates random C++ programs with
complex class inheritance graphs, and virtual functions called through object
and function pointers of different types. The programs are spread across
multiple translation units and DSOs to test the different visibility
restrictions.
I've also tried doing bootstrap builds of LLVM to test this. This isn't
ideal, because only classes in anonymous namespaces can be optimised with
-fvisibility=default, and some parts of LLVM (plugins and bugpoint) do not
work correctly with -fvisibility=hidden. However, there are only 12 test
failures when building with -fvisibility=hidden (and an unmodified compiler),
and this change does not cause any new failures for either value of
-fvisibility.
On the 7 C++ sub-benchmarks of SPEC2006, this gives a geomean code-size
reduction of ~6%, over a baseline compiled with "-O2 -flto
-fvisibility=hidden -fwhole-program-vtables". The best cases are reductions
of ~14% in 450.soplex and 483.xalancbmk, and there are no code size
increases.
I've also run this on a set of 8 mbed-os examples compiled for Armv7M, which
show a geomean size reduction of ~3%, again with no size increases.
I had hoped that this would have no effect on performance, which would allow
it to awlays be enabled (when using -fwhole-program-vtables). However, the
changes in clang to use the llvm.type.checked.load intrinsic are causing ~1%
performance regression in the C++ parts of SPEC2006. It should be possible to
recover some of this perf loss by teaching optimisations about the
llvm.type.checked.load intrinsic, which would make it worth turning this on
by default (though it's still dependent on -fwhole-program-vtables).
Differential revision: https://reviews.llvm.org/D63932
llvm-svn: 375094
2019-10-17 17:58:57 +08:00
|
|
|
llvm::GlobalObject::VCallVisibility
|
|
|
|
CodeGenModule::GetVCallVisibilityLevel(const CXXRecordDecl *RD) {
|
|
|
|
LinkageInfo LV = RD->getLinkageAndVisibility();
|
|
|
|
llvm::GlobalObject::VCallVisibility TypeVis;
|
|
|
|
if (!isExternallyVisible(LV.getLinkage()))
|
|
|
|
TypeVis = llvm::GlobalObject::VCallVisibilityTranslationUnit;
|
|
|
|
else if (HasHiddenLTOVisibility(RD))
|
|
|
|
TypeVis = llvm::GlobalObject::VCallVisibilityLinkageUnit;
|
|
|
|
else
|
|
|
|
TypeVis = llvm::GlobalObject::VCallVisibilityPublic;
|
|
|
|
|
|
|
|
for (auto B : RD->bases())
|
|
|
|
if (B.getType()->getAsCXXRecordDecl()->isDynamicClass())
|
|
|
|
TypeVis = std::min(TypeVis,
|
|
|
|
GetVCallVisibilityLevel(B.getType()->getAsCXXRecordDecl()));
|
|
|
|
|
|
|
|
for (auto B : RD->vbases())
|
|
|
|
if (B.getType()->getAsCXXRecordDecl()->isDynamicClass())
|
|
|
|
TypeVis = std::min(TypeVis,
|
|
|
|
GetVCallVisibilityLevel(B.getType()->getAsCXXRecordDecl()));
|
|
|
|
|
|
|
|
return TypeVis;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitVTableTypeMetadata(const CXXRecordDecl *RD,
|
|
|
|
llvm::GlobalVariable *VTable,
|
2016-06-25 05:21:46 +08:00
|
|
|
const VTableLayout &VTLayout) {
|
2017-01-19 07:55:27 +08:00
|
|
|
if (!getCodeGenOpts().LTOUnit)
|
2015-02-21 04:30:56 +08:00
|
|
|
return;
|
|
|
|
|
2015-06-18 03:08:05 +08:00
|
|
|
CharUnits PointerWidth =
|
|
|
|
Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
|
2015-02-21 04:30:56 +08:00
|
|
|
|
2018-06-26 10:15:47 +08:00
|
|
|
typedef std::pair<const CXXRecordDecl *, unsigned> AddressPoint;
|
|
|
|
std::vector<AddressPoint> AddressPoints;
|
2016-04-29 01:09:37 +08:00
|
|
|
for (auto &&AP : VTLayout.getAddressPoints())
|
2018-06-26 10:15:47 +08:00
|
|
|
AddressPoints.push_back(std::make_pair(
|
2018-05-31 06:29:08 +08:00
|
|
|
AP.first.getBase(), VTLayout.getVTableOffset(AP.second.VTableIndex) +
|
|
|
|
AP.second.AddressPointIndex));
|
|
|
|
|
2018-06-26 10:15:47 +08:00
|
|
|
// Sort the address points for determinism.
|
2018-09-27 06:16:28 +08:00
|
|
|
llvm::sort(AddressPoints, [this](const AddressPoint &AP1,
|
|
|
|
const AddressPoint &AP2) {
|
2018-06-26 10:15:47 +08:00
|
|
|
if (&AP1 == &AP2)
|
2015-02-24 09:12:53 +08:00
|
|
|
return false;
|
|
|
|
|
2015-09-10 10:17:40 +08:00
|
|
|
std::string S1;
|
|
|
|
llvm::raw_string_ostream O1(S1);
|
|
|
|
getCXXABI().getMangleContext().mangleTypeName(
|
2018-06-26 10:15:47 +08:00
|
|
|
QualType(AP1.first->getTypeForDecl(), 0), O1);
|
2015-09-10 10:17:40 +08:00
|
|
|
O1.flush();
|
|
|
|
|
|
|
|
std::string S2;
|
|
|
|
llvm::raw_string_ostream O2(S2);
|
|
|
|
getCXXABI().getMangleContext().mangleTypeName(
|
2018-06-26 10:15:47 +08:00
|
|
|
QualType(AP2.first->getTypeForDecl(), 0), O2);
|
2015-09-10 10:17:40 +08:00
|
|
|
O2.flush();
|
|
|
|
|
2015-02-21 04:30:56 +08:00
|
|
|
if (S1 < S2)
|
|
|
|
return true;
|
|
|
|
if (S1 != S2)
|
|
|
|
return false;
|
|
|
|
|
2018-06-26 10:15:47 +08:00
|
|
|
return AP1.second < AP2.second;
|
2015-02-21 04:30:56 +08:00
|
|
|
});
|
|
|
|
|
2018-06-26 10:15:47 +08:00
|
|
|
ArrayRef<VTableComponent> Comps = VTLayout.vtable_components();
|
|
|
|
for (auto AP : AddressPoints) {
|
|
|
|
// Create type metadata for the address point.
|
|
|
|
AddVTableTypeMetadata(VTable, PointerWidth * AP.second, AP.first);
|
|
|
|
|
|
|
|
// The class associated with each address point could also potentially be
|
|
|
|
// used for indirect calls via a member function pointer, so we need to
|
|
|
|
// annotate the address of each function pointer with the appropriate member
|
|
|
|
// function pointer type.
|
|
|
|
for (unsigned I = 0; I != Comps.size(); ++I) {
|
|
|
|
if (Comps[I].getKind() != VTableComponent::CK_FunctionPointer)
|
|
|
|
continue;
|
|
|
|
llvm::Metadata *MD = CreateMetadataIdentifierForVirtualMemPtrType(
|
|
|
|
Context.getMemberPointerType(
|
|
|
|
Comps[I].getFunctionDecl()->getType(),
|
|
|
|
Context.getRecordType(AP.first).getTypePtr()));
|
|
|
|
VTable->addTypeMetadata((PointerWidth * I).getQuantity(), MD);
|
|
|
|
}
|
|
|
|
}
|
Reland: Dead Virtual Function Elimination
Remove dead virtual functions from vtables with
replaceNonMetadataUsesWith, so that CGProfile metadata gets cleaned up
correctly.
Original commit message:
Currently, it is hard for the compiler to remove unused C++ virtual
functions, because they are all referenced from vtables, which are referenced
by constructors. This means that if the constructor is called from any live
code, then we keep every virtual function in the final link, even if there
are no call sites which can use it.
This patch allows unused virtual functions to be removed during LTO (and
regular compilation in limited circumstances) by using type metadata to match
virtual function call sites to the vtable slots they might load from. This
information can then be used in the global dead code elimination pass instead
of the references from vtables to virtual functions, to more accurately
determine which functions are reachable.
To make this transformation safe, I have changed clang's code-generation to
always load virtual function pointers using the llvm.type.checked.load
intrinsic, instead of regular load instructions. I originally tried writing
this using clang's existing code-generation, which uses the llvm.type.test
and llvm.assume intrinsics after doing a normal load. However, it is possible
for optimisations to obscure the relationship between the GEP, load and
llvm.type.test, causing GlobalDCE to fail to find virtual function call
sites.
The existing linkage and visibility types don't accurately describe the scope
in which a virtual call could be made which uses a given vtable. This is
wider than the visibility of the type itself, because a virtual function call
could be made using a more-visible base class. I've added a new
!vcall_visibility metadata type to represent this, described in
TypeMetadata.rst. The internalization pass and libLTO have been updated to
change this metadata when linking is performed.
This doesn't currently work with ThinLTO, because it needs to see every call
to llvm.type.checked.load in the linkage unit. It might be possible to
extend this optimisation to be able to use the ThinLTO summary, as was done
for devirtualization, but until then that combination is rejected in the
clang driver.
To test this, I've written a fuzzer which generates random C++ programs with
complex class inheritance graphs, and virtual functions called through object
and function pointers of different types. The programs are spread across
multiple translation units and DSOs to test the different visibility
restrictions.
I've also tried doing bootstrap builds of LLVM to test this. This isn't
ideal, because only classes in anonymous namespaces can be optimised with
-fvisibility=default, and some parts of LLVM (plugins and bugpoint) do not
work correctly with -fvisibility=hidden. However, there are only 12 test
failures when building with -fvisibility=hidden (and an unmodified compiler),
and this change does not cause any new failures for either value of
-fvisibility.
On the 7 C++ sub-benchmarks of SPEC2006, this gives a geomean code-size
reduction of ~6%, over a baseline compiled with "-O2 -flto
-fvisibility=hidden -fwhole-program-vtables". The best cases are reductions
of ~14% in 450.soplex and 483.xalancbmk, and there are no code size
increases.
I've also run this on a set of 8 mbed-os examples compiled for Armv7M, which
show a geomean size reduction of ~3%, again with no size increases.
I had hoped that this would have no effect on performance, which would allow
it to awlays be enabled (when using -fwhole-program-vtables). However, the
changes in clang to use the llvm.type.checked.load intrinsic are causing ~1%
performance regression in the C++ parts of SPEC2006. It should be possible to
recover some of this perf loss by teaching optimisations about the
llvm.type.checked.load intrinsic, which would make it worth turning this on
by default (though it's still dependent on -fwhole-program-vtables).
Differential revision: https://reviews.llvm.org/D63932
llvm-svn: 375094
2019-10-17 17:58:57 +08:00
|
|
|
|
[WPD/VFE] Always emit vcall_visibility metadata for -fwhole-program-vtables
Summary:
First patch to support Safe Whole Program Devirtualization Enablement,
see RFC here: http://lists.llvm.org/pipermail/llvm-dev/2019-December/137543.html
Always emit !vcall_visibility metadata under -fwhole-program-vtables,
and not just for -fvirtual-function-elimination. The vcall visibility
metadata will (in a subsequent patch) be used to communicate to WPD
which vtables are safe to devirtualize, and we will optionally convert
the metadata to hidden visibility at link time. Subsequent follow on
patches will help enable this by adding vcall_visibility metadata to the
ThinLTO summaries, and always emit type test intrinsics under
-fwhole-program-vtables (and not just for vtables with hidden
visibility).
In order to do this safely with VFE, since for VFE all vtable loads must
be type checked loads which will no longer be the case, this patch adds
a new "Virtual Function Elim" module flag to communicate to GlobalDCE
whether to perform VFE using the vcall_visibility metadata.
One additional advantage of using the vcall_visibility metadata to drive
more WPD at LTO link time is that we can use the same mechanism to
enable more aggressive VFE at LTO link time as well. The link time
option proposed in the RFC will convert vcall_visibility metadata to
hidden (aka linkage unit visibility), which combined with
-fvirtual-function-elimination will allow it to be done more
aggressively at LTO link time under the same conditions.
Reviewers: pcc, ostannard, evgeny777, steven_wu
Subscribers: mehdi_amini, Prazek, hiraditya, dexonsmith, davidxl, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D71907
2019-12-27 00:32:42 +08:00
|
|
|
if (getCodeGenOpts().VirtualFunctionElimination ||
|
|
|
|
getCodeGenOpts().WholeProgramVTables) {
|
Reland: Dead Virtual Function Elimination
Remove dead virtual functions from vtables with
replaceNonMetadataUsesWith, so that CGProfile metadata gets cleaned up
correctly.
Original commit message:
Currently, it is hard for the compiler to remove unused C++ virtual
functions, because they are all referenced from vtables, which are referenced
by constructors. This means that if the constructor is called from any live
code, then we keep every virtual function in the final link, even if there
are no call sites which can use it.
This patch allows unused virtual functions to be removed during LTO (and
regular compilation in limited circumstances) by using type metadata to match
virtual function call sites to the vtable slots they might load from. This
information can then be used in the global dead code elimination pass instead
of the references from vtables to virtual functions, to more accurately
determine which functions are reachable.
To make this transformation safe, I have changed clang's code-generation to
always load virtual function pointers using the llvm.type.checked.load
intrinsic, instead of regular load instructions. I originally tried writing
this using clang's existing code-generation, which uses the llvm.type.test
and llvm.assume intrinsics after doing a normal load. However, it is possible
for optimisations to obscure the relationship between the GEP, load and
llvm.type.test, causing GlobalDCE to fail to find virtual function call
sites.
The existing linkage and visibility types don't accurately describe the scope
in which a virtual call could be made which uses a given vtable. This is
wider than the visibility of the type itself, because a virtual function call
could be made using a more-visible base class. I've added a new
!vcall_visibility metadata type to represent this, described in
TypeMetadata.rst. The internalization pass and libLTO have been updated to
change this metadata when linking is performed.
This doesn't currently work with ThinLTO, because it needs to see every call
to llvm.type.checked.load in the linkage unit. It might be possible to
extend this optimisation to be able to use the ThinLTO summary, as was done
for devirtualization, but until then that combination is rejected in the
clang driver.
To test this, I've written a fuzzer which generates random C++ programs with
complex class inheritance graphs, and virtual functions called through object
and function pointers of different types. The programs are spread across
multiple translation units and DSOs to test the different visibility
restrictions.
I've also tried doing bootstrap builds of LLVM to test this. This isn't
ideal, because only classes in anonymous namespaces can be optimised with
-fvisibility=default, and some parts of LLVM (plugins and bugpoint) do not
work correctly with -fvisibility=hidden. However, there are only 12 test
failures when building with -fvisibility=hidden (and an unmodified compiler),
and this change does not cause any new failures for either value of
-fvisibility.
On the 7 C++ sub-benchmarks of SPEC2006, this gives a geomean code-size
reduction of ~6%, over a baseline compiled with "-O2 -flto
-fvisibility=hidden -fwhole-program-vtables". The best cases are reductions
of ~14% in 450.soplex and 483.xalancbmk, and there are no code size
increases.
I've also run this on a set of 8 mbed-os examples compiled for Armv7M, which
show a geomean size reduction of ~3%, again with no size increases.
I had hoped that this would have no effect on performance, which would allow
it to awlays be enabled (when using -fwhole-program-vtables). However, the
changes in clang to use the llvm.type.checked.load intrinsic are causing ~1%
performance regression in the C++ parts of SPEC2006. It should be possible to
recover some of this perf loss by teaching optimisations about the
llvm.type.checked.load intrinsic, which would make it worth turning this on
by default (though it's still dependent on -fwhole-program-vtables).
Differential revision: https://reviews.llvm.org/D63932
llvm-svn: 375094
2019-10-17 17:58:57 +08:00
|
|
|
llvm::GlobalObject::VCallVisibility TypeVis = GetVCallVisibilityLevel(RD);
|
|
|
|
if (TypeVis != llvm::GlobalObject::VCallVisibilityPublic)
|
[WPD/VFE] Always emit vcall_visibility metadata for -fwhole-program-vtables
Summary:
First patch to support Safe Whole Program Devirtualization Enablement,
see RFC here: http://lists.llvm.org/pipermail/llvm-dev/2019-December/137543.html
Always emit !vcall_visibility metadata under -fwhole-program-vtables,
and not just for -fvirtual-function-elimination. The vcall visibility
metadata will (in a subsequent patch) be used to communicate to WPD
which vtables are safe to devirtualize, and we will optionally convert
the metadata to hidden visibility at link time. Subsequent follow on
patches will help enable this by adding vcall_visibility metadata to the
ThinLTO summaries, and always emit type test intrinsics under
-fwhole-program-vtables (and not just for vtables with hidden
visibility).
In order to do this safely with VFE, since for VFE all vtable loads must
be type checked loads which will no longer be the case, this patch adds
a new "Virtual Function Elim" module flag to communicate to GlobalDCE
whether to perform VFE using the vcall_visibility metadata.
One additional advantage of using the vcall_visibility metadata to drive
more WPD at LTO link time is that we can use the same mechanism to
enable more aggressive VFE at LTO link time as well. The link time
option proposed in the RFC will convert vcall_visibility metadata to
hidden (aka linkage unit visibility), which combined with
-fvirtual-function-elimination will allow it to be done more
aggressively at LTO link time under the same conditions.
Reviewers: pcc, ostannard, evgeny777, steven_wu
Subscribers: mehdi_amini, Prazek, hiraditya, dexonsmith, davidxl, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D71907
2019-12-27 00:32:42 +08:00
|
|
|
VTable->setVCallVisibilityMetadata(TypeVis);
|
Reland: Dead Virtual Function Elimination
Remove dead virtual functions from vtables with
replaceNonMetadataUsesWith, so that CGProfile metadata gets cleaned up
correctly.
Original commit message:
Currently, it is hard for the compiler to remove unused C++ virtual
functions, because they are all referenced from vtables, which are referenced
by constructors. This means that if the constructor is called from any live
code, then we keep every virtual function in the final link, even if there
are no call sites which can use it.
This patch allows unused virtual functions to be removed during LTO (and
regular compilation in limited circumstances) by using type metadata to match
virtual function call sites to the vtable slots they might load from. This
information can then be used in the global dead code elimination pass instead
of the references from vtables to virtual functions, to more accurately
determine which functions are reachable.
To make this transformation safe, I have changed clang's code-generation to
always load virtual function pointers using the llvm.type.checked.load
intrinsic, instead of regular load instructions. I originally tried writing
this using clang's existing code-generation, which uses the llvm.type.test
and llvm.assume intrinsics after doing a normal load. However, it is possible
for optimisations to obscure the relationship between the GEP, load and
llvm.type.test, causing GlobalDCE to fail to find virtual function call
sites.
The existing linkage and visibility types don't accurately describe the scope
in which a virtual call could be made which uses a given vtable. This is
wider than the visibility of the type itself, because a virtual function call
could be made using a more-visible base class. I've added a new
!vcall_visibility metadata type to represent this, described in
TypeMetadata.rst. The internalization pass and libLTO have been updated to
change this metadata when linking is performed.
This doesn't currently work with ThinLTO, because it needs to see every call
to llvm.type.checked.load in the linkage unit. It might be possible to
extend this optimisation to be able to use the ThinLTO summary, as was done
for devirtualization, but until then that combination is rejected in the
clang driver.
To test this, I've written a fuzzer which generates random C++ programs with
complex class inheritance graphs, and virtual functions called through object
and function pointers of different types. The programs are spread across
multiple translation units and DSOs to test the different visibility
restrictions.
I've also tried doing bootstrap builds of LLVM to test this. This isn't
ideal, because only classes in anonymous namespaces can be optimised with
-fvisibility=default, and some parts of LLVM (plugins and bugpoint) do not
work correctly with -fvisibility=hidden. However, there are only 12 test
failures when building with -fvisibility=hidden (and an unmodified compiler),
and this change does not cause any new failures for either value of
-fvisibility.
On the 7 C++ sub-benchmarks of SPEC2006, this gives a geomean code-size
reduction of ~6%, over a baseline compiled with "-O2 -flto
-fvisibility=hidden -fwhole-program-vtables". The best cases are reductions
of ~14% in 450.soplex and 483.xalancbmk, and there are no code size
increases.
I've also run this on a set of 8 mbed-os examples compiled for Armv7M, which
show a geomean size reduction of ~3%, again with no size increases.
I had hoped that this would have no effect on performance, which would allow
it to awlays be enabled (when using -fwhole-program-vtables). However, the
changes in clang to use the llvm.type.checked.load intrinsic are causing ~1%
performance regression in the C++ parts of SPEC2006. It should be possible to
recover some of this perf loss by teaching optimisations about the
llvm.type.checked.load intrinsic, which would make it worth turning this on
by default (though it's still dependent on -fwhole-program-vtables).
Differential revision: https://reviews.llvm.org/D63932
llvm-svn: 375094
2019-10-17 17:58:57 +08:00
|
|
|
}
|
2015-02-21 04:30:56 +08:00
|
|
|
}
|