llvm-project/llvm/lib
Oliver Stannard 3b598b9c86 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 09:58:57 +00:00
..
Analysis Reland: Dead Virtual Function Elimination 2019-10-17 09:58:57 +00:00
AsmParser [Alignment][NFC] Remove dependency on GlobalObject::setAlignment(unsigned) 2019-10-15 11:24:36 +00:00
BinaryFormat [DebugInfo] Add a DW_OP_LLVM_entry_value operation 2019-10-15 11:31:21 +00:00
Bitcode [Alignment][NFC] Remove dependency on GlobalObject::setAlignment(unsigned) 2019-10-15 11:24:36 +00:00
Bitstream [Bitcode] Move Bitstream to a separate library 2019-07-03 22:40:07 +00:00
CodeGen [DFAPacketizer] Use DFAEmitter. NFC. 2019-10-17 08:34:29 +00:00
DebugInfo [Symbolize] Use the local MSVC C++ demangler instead of relying on dbghelp. NFC. 2019-10-16 20:38:44 +00:00
Demangle [Demangle] Add a few more options to the microsoft demangler 2019-10-15 08:29:56 +00:00
ExecutionEngine [Orc] Add a method for ObjectLinkingLayer to return ownership of object buffers. 2019-10-15 21:41:12 +00:00
FuzzMutate [llvm] Migrate llvm::make_unique to std::make_unique 2019-08-15 15:54:37 +00:00
Fuzzer
IR Reland: Dead Virtual Function Elimination 2019-10-17 09:58:57 +00:00
IRReader
LTO Reland: Dead Virtual Function Elimination 2019-10-17 09:58:57 +00:00
LineEditor [CMake] Delete redundant DEPENDS/LINK_LIBS from LineEditor/XRay 2019-06-22 01:50:21 +00:00
Linker [Alignment][NFC] Remove dependency on GlobalObject::setAlignment(unsigned) 2019-10-15 11:24:36 +00:00
MC [NFC][XCOFF][AIX] Rename ControlSections to CsectGroup 2019-10-16 17:36:31 +00:00
MCA [MCA][LSUnit] Track loads and stores until retirement. 2019-10-08 10:46:01 +00:00
Object [XCOFF]implement parsing relocation information for 32-bit xcoff object file 2019-10-15 20:42:11 +00:00
ObjectYAML MinidumpYAML: Add support for the memory info list stream 2019-10-10 13:05:46 +00:00
Option [llvm] Migrate llvm::make_unique to std::make_unique 2019-08-15 15:54:37 +00:00
Passes In the new pass manager use PTO.LoopUnrolling to determine when and how 2019-10-14 22:56:07 +00:00
ProfileData [Alignment][NFC] Move and type functions from MathExtras to Alignment 2019-10-14 13:14:34 +00:00
Remarks [Remarks] Add support for prepending a path to external files 2019-10-16 15:40:59 +00:00
Support Revert [support] GlobPattern: add support for `\` and `[!...]`, and allow `]` in more places 2019-10-16 22:59:02 +00:00
TableGen [gicombiner] Hoist pure C++ combine into the tablegen definition 2019-10-16 23:53:35 +00:00
Target [ARM][MVE] Change VPST to use, not def, VPR 2019-10-17 08:46:31 +00:00
Testing
TextAPI Reland "[TextAPI] Introduce TBDv4" 2019-10-10 04:24:44 +00:00
ToolDrivers [llvm-lib] Detect duplicate input files 2019-10-02 06:41:52 +00:00
Transforms Reland: Dead Virtual Function Elimination 2019-10-17 09:58:57 +00:00
WindowsManifest Fight a bit against global initializers. NFC. 2019-08-22 19:43:27 +00:00
XRay [XRay] Silence static analyzer dyn_cast<BufferExtents> null dereference warning. NFCI. 2019-09-22 18:47:00 +00:00
CMakeLists.txt Fix build errors LLVM tests are disabled. 2019-07-11 22:08:35 +00:00
LLVMBuild.txt [Bitcode] Move Bitstream to a separate library 2019-07-03 22:40:07 +00:00