Page MenuHomePhabricator

[LTO] Support for embedding bitcode section during LTO
Needs ReviewPublic

Authored by zapster on Sep 30 2019, 5:55 AM.

Details

Reviewers
rsmith
pcc
alexshap
Group Reviewers
Restricted Project
Restricted Project
Summary

This adds support for embedding bitcode in a binary during LTO. The libLTO gains supports the -lto-embed-bitcode option which accepts off, all, bitcode, and marker. The semantics are the same as for clangs -fembed-bitcode. The option allows users of the LTO library to embed a bitcode section. For example, LLD can pass the option via ld.lld -mllvm=-lto-embed-bitcode=all.

This feature allows doing something comparable to clang -c -fembed-bitcode, but on the (LTO) linker level. Having bitcode alongside native code has many use-cases. To give an example, the MacOS linker can create a -bitcode_bundle section containing bitcode. Also, having this feature built into LLVM is an alternative to 3rd party tools such as wllvm or gllvm. As with these tools, this feature simplifies creating "whole-program" llvm bitcode files, but in contrast to wllvm/gllvm it does not rely on a specific llvm frontend/driver.

I originally proposed this feature as an addition to LLD. It turned out, however, that doing this purely on LLVM/LTO side is more general and might be useful to a broader audience.

The implementation is quite straight forward. The embedding logic moved from clang to llvm/lib/Bitcode and llvm/lib/LTO gained the -lto-embed-bitcode option. Most code just moved.

Diff Detail

Event Timeline

zapster created this revision.Sep 30 2019, 5:55 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
zapster updated this revision to Diff 222401.Sep 30 2019, 5:56 AM

re-ran clang format

zapster marked 5 inline comments as done.Sep 30 2019, 6:05 AM

Added inline remarks.

clang/lib/CodeGen/BackendUtil.cpp
1547

moved to llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

clang/test/Frontend/x86-embed-bitcode.ll
1

This duplicates the embed-bitcode.ll test (which only runs on ARM) for x86.

llvm/include/llvm/Bitcode/BitcodeWriter.h
158

BitcodeWriter.h seems like a natural place for this functionality. However, suggestions for a better location are more than appreciated.

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
4670

moved from clang/lib/CodeGen/BackendUtil.cpp

llvm/lib/LTO/LTOBackend.cpp
327

This options parsing logic is duplicated from clang. We might want move this to a shared place, but I failed to find a good candidate. include/llvm/Support/CodeGen.h came to mind, but it currently only contains types, no code. Any suggestions?