forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: 374539
- Loading branch information
Showing
34 changed files
with
1,428 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// RUN: %clang_cc1 -flto -flto-unit -triple x86_64-unknown-linux -emit-llvm -fvirtual-function-elimination -fwhole-program-vtables -o - %s | FileCheck %s | ||
|
||
|
||
// Anonymous namespace. | ||
namespace { | ||
// CHECK: @_ZTVN12_GLOBAL__N_11AE = {{.*}} !vcall_visibility [[VIS_TU:![0-9]+]] | ||
struct A { | ||
A() {} | ||
virtual int f() { return 1; } | ||
}; | ||
} | ||
void *construct_A() { | ||
return new A(); | ||
} | ||
|
||
|
||
// Hidden visibility. | ||
// CHECK: @_ZTV1B = {{.*}} !vcall_visibility [[VIS_DSO:![0-9]+]] | ||
struct __attribute__((visibility("hidden"))) B { | ||
B() {} | ||
virtual int f() { return 1; } | ||
}; | ||
B *construct_B() { | ||
return new B(); | ||
} | ||
|
||
|
||
// Default visibility. | ||
// CHECK-NOT: @_ZTV1C = {{.*}} !vcall_visibility | ||
struct __attribute__((visibility("default"))) C { | ||
C() {} | ||
virtual int f() { return 1; } | ||
}; | ||
C *construct_C() { | ||
return new C(); | ||
} | ||
|
||
|
||
// Hidden visibility, public LTO visibility. | ||
// CHECK-NOT: @_ZTV1D = {{.*}} !vcall_visibility | ||
struct __attribute__((visibility("hidden"))) [[clang::lto_visibility_public]] D { | ||
D() {} | ||
virtual int f() { return 1; } | ||
}; | ||
D *construct_D() { | ||
return new D(); | ||
} | ||
|
||
|
||
// Hidden visibility, but inherits from class with default visibility. | ||
// CHECK-NOT: @_ZTV1E = {{.*}} !vcall_visibility | ||
struct __attribute__((visibility("hidden"))) E : C { | ||
E() {} | ||
virtual int f() { return 1; } | ||
}; | ||
E *construct_E() { | ||
return new E(); | ||
} | ||
|
||
|
||
// Anonymous namespace, but inherits from class with default visibility. | ||
// CHECK-NOT: @_ZTVN12_GLOBAL__N_11FE = {{.*}} !vcall_visibility | ||
namespace { | ||
struct __attribute__((visibility("hidden"))) F : C { | ||
F() {} | ||
virtual int f() { return 1; } | ||
}; | ||
} | ||
void *construct_F() { | ||
return new F(); | ||
} | ||
|
||
|
||
// Anonymous namespace, but inherits from class with hidden visibility. | ||
// CHECK: @_ZTVN12_GLOBAL__N_11GE = {{.*}} !vcall_visibility [[VIS_DSO:![0-9]+]] | ||
namespace { | ||
struct __attribute__((visibility("hidden"))) G : B { | ||
G() {} | ||
virtual int f() { return 1; } | ||
}; | ||
} | ||
void *construct_G() { | ||
return new G(); | ||
} | ||
|
||
|
||
// CHECK-DAG: [[VIS_DSO]] = !{i64 1} | ||
// CHECK-DAG: [[VIS_TU]] = !{i64 2} |
Oops, something went wrong.