Skip to content

Commit 3cd700b

Browse files
committed
[WIP] Implement LTO support
Serializing and deserializing Cranelift IR is supported, but due to the lack of interprocedural optimizations in Cranelift there is no runtime perf benefit. Only a compile time hit. It may still be useful for things like the JIT mode where invoking a regular linker is not possible.
1 parent 5782b21 commit 3cd700b

File tree

14 files changed

+627
-14
lines changed

14 files changed

+627
-14
lines changed

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ cranelift-jit = { version = "0.121.0", optional = true }
1616
cranelift-object = { version = "0.121.0" }
1717
target-lexicon = "0.13"
1818
gimli = { version = "0.31", default-features = false, features = ["write"] }
19-
object = { version = "0.36", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }
19+
object = { version = "0.36", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe", "unaligned"] }
2020

2121
indexmap = "2.0.0"
2222
libloading = { version = "0.8.0", optional = true }
2323
smallvec = "1.8.1"
24+
serde = { version = "1.0.203", features = ["derive"], optional = true }
25+
postcard = { version = "1.0.8", default-features = false, features = ["use-std"], optional = true }
2426

2527
[patch.crates-io]
2628
# Uncomment to use an unreleased version of cranelift
@@ -43,8 +45,9 @@ smallvec = "1.8.1"
4345

4446
[features]
4547
# Enable features not ready to be enabled when compiling as part of rustc
46-
unstable-features = ["jit", "inline_asm_sym"]
48+
unstable-features = ["jit", "inline_asm_sym", "lto"]
4749
jit = ["cranelift-jit", "libloading"]
50+
lto = ["serde", "postcard", "cranelift-codegen/enable-serde", "cranelift-module/enable-serde"]
4851
inline_asm_sym = []
4952
unwinding = [] # Not yet included in unstable-features for performance reasons
5053

build_system/build_sysroot.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ fn build_clif_sysroot_for_triple(
245245
prefix.to_str().unwrap()
246246
));
247247
}
248+
rustflags.push("-Clto=thin".to_owned());
249+
rustflags.push("-Zdylib-lto".to_owned());
250+
rustflags.push("-Cembed-bitcode=yes".to_owned());
248251
compiler.rustflags.extend(rustflags);
249252
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
250253
build_cmd.arg("--release");
@@ -255,6 +258,7 @@ fn build_clif_sysroot_for_triple(
255258
if compiler.triple.contains("apple") {
256259
build_cmd.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed");
257260
}
261+
build_cmd.env("CARGO_PROFILE_RELEASE_LTO", "thin");
258262
spawn_and_wait(build_cmd);
259263

260264
for entry in fs::read_dir(build_dir.join("deps")).unwrap() {

build_system/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl TestCase {
5656
}
5757

5858
const NO_SYSROOT_SUITE: &[TestCase] = &[
59-
TestCase::build_lib("build.mini_core", "example/mini_core.rs", "lib,dylib"),
59+
TestCase::build_lib("build.mini_core", "example/mini_core.rs", "lib"),
6060
TestCase::build_lib("build.example", "example/example.rs", "lib"),
6161
TestCase::jit_bin("jit.mini_core_hello_world", "example/mini_core_hello_world.rs", "abc bcd"),
6262
TestCase::build_bin_and_run(
@@ -417,6 +417,7 @@ impl<'a> TestRunner<'a> {
417417
cmd.arg("--out-dir");
418418
cmd.arg(BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs));
419419
cmd.arg("-Cdebuginfo=2");
420+
cmd.arg("-Clto=thin");
420421
cmd.arg("--target");
421422
cmd.arg(&self.target_compiler.triple);
422423
if !self.panic_unwind_support {

example/mini_core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ struct PanicLocation {
774774
column: u32,
775775
}
776776

777+
/*
777778
#[no_mangle]
778779
#[cfg(not(all(windows, target_env = "gnu")))]
779780
pub fn get_tls() -> u8 {
@@ -782,3 +783,4 @@ pub fn get_tls() -> u8 {
782783
783784
A
784785
}
786+
*/

example/mini_core_hello_world.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,13 @@ fn main() {
324324
#[cfg(all(not(jit), not(all(windows, target_env = "gnu"))))]
325325
test_tls();
326326

327+
/*
327328
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
328329
unsafe {
329330
global_asm_test();
330331
naked_test();
331332
}
333+
*/
332334

333335
// Both statics have a reference that points to the same anonymous allocation.
334336
static REF1: &u8 = &42;
@@ -353,6 +355,7 @@ fn stack_val_align() {
353355
assert_eq!(&a as *const Foo as usize % 8192, 0);
354356
}
355357

358+
/*
356359
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
357360
extern "C" {
358361
fn global_asm_test();
@@ -377,6 +380,7 @@ global_asm! {
377380
ret
378381
"
379382
}
383+
*/
380384

381385
#[cfg(all(not(jit), target_arch = "x86_64"))]
382386
#[unsafe(naked)]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From 0910cbe862990b0c3a17c67bca199ebb4452b0ec Mon Sep 17 00:00:00 2001
2+
From: bjorn3 <[email protected]>
3+
Date: Tue, 28 Mar 2023 17:09:01 +0000
4+
Subject: [PATCH] Disable dylib crate type
5+
6+
---
7+
library/std/Cargo.toml | 2 +-
8+
1 files changed, 1 insertions(+), 1 deletions(-)
9+
10+
diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml
11+
index 598a4bf..3e68680 100644
12+
--- a/library/std/Cargo.toml
13+
+++ b/library/std/Cargo.toml
14+
@@ -7,7 +7,7 @@ description = "The Rust Standard Library"
15+
autobenches = false
16+
17+
[lib]
18+
-crate-type = ["dylib", "rlib"]
19+
+crate-type = ["rlib"]
20+
21+
[dependencies]
22+
alloc = { path = "../alloc", public = true }
23+
--
24+
2.34.1
25+

src/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ pub(crate) fn compile_fn(
243243
}
244244
}
245245

246+
/*
246247
// Define debuginfo for function
247248
let debug_context = &mut cx.debug_context;
248249
profiler.generic_activity("generate debug info").run(|| {
@@ -254,6 +255,7 @@ pub(crate) fn compile_fn(
254255
);
255256
}
256257
});
258+
*/
257259
}
258260

259261
fn verify_func(tcx: TyCtxt<'_>, writer: &crate::pretty_clif::CommentWriter, func: &Function) {

src/driver/aot.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl OngoingCodegen {
140140
}
141141

142142
// Adapted from https://github.com/rust-lang/rust/blob/73476d49904751f8d90ce904e16dfbc278083d2c/compiler/rustc_codegen_ssa/src/back/write.rs#L547C1-L706C2
143-
fn produce_final_output_artifacts(
143+
pub(super) fn produce_final_output_artifacts(
144144
sess: &Session,
145145
codegen_results: &CodegenResults,
146146
crate_output: &OutputFilenames,
@@ -324,7 +324,7 @@ fn produce_final_output_artifacts(
324324
// These are used in linking steps and will be cleaned up afterward.
325325
}
326326

327-
fn make_module(sess: &Session, name: String) -> UnwindModule<ObjectModule> {
327+
pub(super) fn make_module(sess: &Session, name: String) -> UnwindModule<ObjectModule> {
328328
let isa = crate::build_isa(sess, false);
329329

330330
let mut builder =
@@ -385,7 +385,7 @@ fn emit_cgu(
385385
})
386386
}
387387

388-
fn emit_module(
388+
pub(super) fn emit_module(
389389
output_filenames: &OutputFilenames,
390390
invocation_temp: Option<&str>,
391391
prof: &SelfProfilerRef,
@@ -508,7 +508,7 @@ fn reuse_workproduct_for_cgu(
508508
})
509509
}
510510

511-
fn codegen_cgu_content(
511+
pub(super) fn codegen_cgu_content(
512512
tcx: TyCtxt<'_>,
513513
module: &mut dyn Module,
514514
cgu_name: rustc_span::Symbol,
@@ -646,7 +646,7 @@ fn module_codegen(
646646
}))
647647
}
648648

649-
fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
649+
pub(crate) fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
650650
let mut allocator_module = make_module(tcx.sess, "allocator_shim".to_string());
651651
let created_alloc_shim = crate::allocator::codegen(tcx, &mut allocator_module);
652652

src/driver/jit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ fn create_jit_module(tcx: TyCtxt<'_>) -> (UnwindModule<JITModule>, CodegenCx) {
2323
let isa = crate::build_isa(tcx.sess, true);
2424
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
2525
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
26-
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
26+
//jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
2727
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);
2828

29+
#[cfg(feature = "lto")]
30+
for (_name, module) in super::lto::load_lto_modules(tcx, &crate_info) {
31+
module.apply_to(&mut jit_module);
32+
}
33+
2934
let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, sym::dummy_cgu_name);
3035

3136
crate::allocator::codegen(tcx, &mut jit_module);

0 commit comments

Comments
 (0)