Skip to content

Commit 724c912

Browse files
committed
rustc: add ability to output regular LTO bitcode modules
Adding the option to control from rustc CLI if the resulted ".o" bitcode module files are with thinLTO info or regular LTO info. Allows using "-lto-embed-bitcode=optimized" during linkage correctly. Signed-off-by: Ziv Dunkelman <[email protected]>
1 parent 0ed9c64 commit 724c912

File tree

10 files changed

+32
-7
lines changed

10 files changed

+32
-7
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub(crate) fn run_thin(
199199

200200
pub(crate) fn prepare_thin(module: ModuleCodegen<ModuleLlvm>) -> (String, ThinBuffer) {
201201
let name = module.name.clone();
202-
let buffer = ThinBuffer::new(module.module_llvm.llmod());
202+
let buffer = ThinBuffer::new(module.module_llvm.llmod(), true);
203203
(name, buffer)
204204
}
205205

@@ -709,9 +709,9 @@ unsafe impl Send for ThinBuffer {}
709709
unsafe impl Sync for ThinBuffer {}
710710

711711
impl ThinBuffer {
712-
pub fn new(m: &llvm::Module) -> ThinBuffer {
712+
pub fn new(m: &llvm::Module, is_thin: bool) -> ThinBuffer {
713713
unsafe {
714-
let buffer = llvm::LLVMRustThinLTOBufferCreate(m);
714+
let buffer = llvm::LLVMRustThinLTOBufferCreate(m, is_thin);
715715
ThinBuffer(buffer)
716716
}
717717
}

compiler/rustc_codegen_llvm/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ pub(crate) unsafe fn codegen(
790790
let _timer = cgcx
791791
.prof
792792
.generic_activity_with_arg("LLVM_module_codegen_make_bitcode", &*module.name);
793-
let thin = ThinBuffer::new(llmod);
793+
let thin = ThinBuffer::new(llmod, config.emit_thin_lto);
794794
let data = thin.data();
795795

796796
if let Some(bitcode_filename) = bc_out.file_name() {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,7 @@ extern "C" {
24692469
pub fn LLVMRustModuleBufferFree(p: &'static mut ModuleBuffer);
24702470
pub fn LLVMRustModuleCost(M: &Module) -> u64;
24712471

2472-
pub fn LLVMRustThinLTOBufferCreate(M: &Module) -> &'static mut ThinLTOBuffer;
2472+
pub fn LLVMRustThinLTOBufferCreate(M: &Module, is_thin: bool) -> &'static mut ThinLTOBuffer;
24732473
pub fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer);
24742474
pub fn LLVMRustThinLTOBufferPtr(M: &ThinLTOBuffer) -> *const c_char;
24752475
pub fn LLVMRustThinLTOBufferLen(M: &ThinLTOBuffer) -> size_t;

compiler/rustc_codegen_ssa/src/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub struct ModuleConfig {
9999
pub emit_ir: bool,
100100
pub emit_asm: bool,
101101
pub emit_obj: EmitObj,
102+
pub emit_thin_lto: bool,
102103
pub bc_cmdline: String,
103104

104105
// Miscellaneous flags. These are mostly copied from command-line
@@ -218,6 +219,7 @@ impl ModuleConfig {
218219
false
219220
),
220221
emit_obj,
222+
emit_thin_lto: sess.opts.unstable_opts.emit_thin_lto,
221223
bc_cmdline: sess.target.bitcode_llvm_cmdline.to_string(),
222224

223225
verify_llvm_ir: sess.verify_llvm_ir(),

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ fn test_unstable_options_tracking_hash() {
734734
tracked!(drop_tracking, true);
735735
tracked!(dual_proc_macros, true);
736736
tracked!(dwarf_version, Some(5));
737+
tracked!(emit_thin_lto, false);
737738
tracked!(fewer_names, Some(true));
738739
tracked!(force_unstable_if_unmarked, true);
739740
tracked!(fuel, Some(("abc".to_string(), 99)));

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "llvm/Transforms/Utils/AddDiscriminators.h"
3535
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
3636
#include "llvm/LTO/LTO.h"
37+
#include "llvm/Bitcode/BitcodeWriterPass.h"
3738
#include "llvm-c/Transforms/PassManagerBuilder.h"
3839

3940
#include "llvm/Transforms/Instrumentation.h"
@@ -1638,13 +1639,17 @@ struct LLVMRustThinLTOBuffer {
16381639
};
16391640

16401641
extern "C" LLVMRustThinLTOBuffer*
1641-
LLVMRustThinLTOBufferCreate(LLVMModuleRef M) {
1642+
LLVMRustThinLTOBufferCreate(LLVMModuleRef M, bool is_thin) {
16421643
auto Ret = std::make_unique<LLVMRustThinLTOBuffer>();
16431644
{
16441645
raw_string_ostream OS(Ret->data);
16451646
{
16461647
legacy::PassManager PM;
1647-
PM.add(createWriteThinLTOBitcodePass(OS));
1648+
if (is_thin) {
1649+
PM.add(createWriteThinLTOBitcodePass(OS));
1650+
} else {
1651+
PM.add(createBitcodeWriterPass(OS));
1652+
}
16481653
PM.run(*unwrap(M));
16491654
}
16501655
}

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,8 @@ options! {
12771277
"version of DWARF debug information to emit (default: 2 or 4, depending on platform)"),
12781278
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
12791279
"emit a section containing stack size metadata (default: no)"),
1280+
emit_thin_lto: bool = (true, parse_bool, [TRACKED],
1281+
"emit the bc module with thin LTO info (default: yes)"),
12801282
fewer_names: Option<bool> = (None, parse_opt_bool, [TRACKED],
12811283
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \
12821284
(default: no)"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# needs-matching-clang
2+
3+
# This test makes sure the embed bitcode in elf created with
4+
# lto-embed-bitcode=optimized is valid llvm BC module.
5+
6+
-include ../../run-make-fulldeps/tools.mk
7+
8+
all:
9+
$(RUSTC) test.rs --target $(TARGET) -Clink-arg=-fuse-ld=lld -Clinker-plugin-lto -Clinker=$(CLANG) -Clink-arg=-Wl,--plugin-opt=-lto-embed-bitcode=optimized -Zemit-thin-lto=no
10+
$(LLVM_BIN_DIR)/objcopy --dump-section .llvmbc=$(TMPDIR)/test.bc $(TMPDIR)/test
11+
$(LLVM_BIN_DIR)/llvm-dis $(TMPDIR)/test.bc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello World!");
3+
}

src/test/rustdoc-ui/z-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
-Z dump-mir-spanview=val -- in addition to `.mir` files, create `.html` files to view spans for all `statement`s (including terminators), only `terminator` spans, or computed `block` spans (one span encompassing a block's terminator and all statements). If `-Z instrument-coverage` is also enabled, create an additional `.html` file showing the computed coverage spans.
3636
-Z dwarf-version=val -- version of DWARF debug information to emit (default: 2 or 4, depending on platform)
3737
-Z emit-stack-sizes=val -- emit a section containing stack size metadata (default: no)
38+
-Z emit-thin-lto=val -- emit the bc module with thin LTO info (default: yes)
3839
-Z fewer-names=val -- reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) (default: no)
3940
-Z force-unstable-if-unmarked=val -- force all crates to be `rustc_private` unstable (default: no)
4041
-Z fuel=val -- set the optimization fuel quota for a crate

0 commit comments

Comments
 (0)