Implement conditional stripping of type descriptors, protocols and protocol conformances via !llvm.used.conditional

A new LLVM IR affordance that allows expressing conditions under which globals
can be removed/dropped (even when marked with @llvm.used) is being discussed at:

- <https://reviews.llvm.org/D104496>
- <https://lists.llvm.org/pipermail/llvm-dev/2021-September/152656.html>

This is a preliminary implementation that marks runtime lookup records (namely
protocol records, type descriptors records and protocol conformance records)
with the !llvm.used.conditional descriptors. That allows link-time / LTO-time
removal of these records (by GlobalDCE) based on whether they're actually used
within the linkage unit. Effectively, this allows libraries that have a limited
and known set of clients, to be optimized against the client at LTO time, and
significantly reduce the code size of that library.

Parts of the implementation:
- New -conditional-runtime-records frontend flag to enable using !llvm.used.conditional
- IRGen code that emits these records can now emit these either as a single contiguous
  array (asContiguousArray = true, the old way), which is used for JIT mode, or
  as indivial globals (asContiguousArray = false), which is necessary for the
  !llvm.used.conditional stripping to work.
- When records are emitted as individual globals, they have new names of
  "\01l_protocol_" + mangled name of the protocol descriptor, and similarly for
  other records.
- Fixed existing tests to account for individual records instead of a single array
- Added an IR level test, and an end-to-end execution test to demonstrate that
  the !llvm.used.conditional-based stripping actually works.
This commit is contained in:
Kuba Mracek
2021-09-29 12:54:32 -07:00
parent d0e05003f5
commit 2dfd232d90
13 changed files with 436 additions and 141 deletions

View File

@@ -1934,6 +1934,10 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
Opts.WitnessMethodElimination = true;
}
if (Args.hasArg(OPT_conditional_runtime_records)) {
Opts.ConditionalRuntimeRecords = true;
}
if (Args.hasArg(OPT_internalize_at_link)) {
Opts.InternalizeAtLink = true;
}