Skip to content

Commit 25433c9

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 a767a10 commit 25433c9

File tree

14 files changed

+639
-17
lines changed

14 files changed

+639
-17
lines changed

Cargo.lock

Lines changed: 38 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.114.0", optional = true }
1616
cranelift-object = { version = "0.114.0" }
1717
target-lexicon = "0.12.0"
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 local checkout of cranelift
@@ -35,8 +37,9 @@ smallvec = "1.8.1"
3537

3638
[features]
3739
# Enable features not ready to be enabled when compiling as part of rustc
38-
unstable-features = ["jit", "inline_asm_sym"]
40+
unstable-features = ["jit", "inline_asm_sym", "lto"]
3941
jit = ["cranelift-jit", "libloading"]
42+
lto = ["serde", "postcard", "cranelift-codegen/enable-serde", "cranelift-module/enable-serde"]
4043
inline_asm_sym = []
4144

4245
[package.metadata.rust-analyzer]

build_system/build_sysroot.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ fn build_clif_sysroot_for_triple(
243243
prefix.to_str().unwrap()
244244
));
245245
}
246+
rustflags.push("-Clto=thin".to_owned());
247+
rustflags.push("-Zdylib-lto".to_owned());
248+
rustflags.push("-Cembed-bitcode=yes".to_owned());
246249
compiler.rustflags.extend(rustflags);
247250
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
248251
build_cmd.arg("--release");
@@ -253,6 +256,7 @@ fn build_clif_sysroot_for_triple(
253256
if compiler.triple.contains("apple") {
254257
build_cmd.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed");
255258
}
259+
build_cmd.env("CARGO_PROFILE_RELEASE_LTO", "thin");
256260
spawn_and_wait(build_cmd);
257261

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

build_system/tests.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl TestCase {
5757
}
5858

5959
const NO_SYSROOT_SUITE: &[TestCase] = &[
60-
TestCase::build_lib("build.mini_core", "example/mini_core.rs", "lib,dylib"),
60+
TestCase::build_lib("build.mini_core", "example/mini_core.rs", "lib"),
6161
TestCase::build_lib("build.example", "example/example.rs", "lib"),
6262
TestCase::jit_bin("jit.mini_core_hello_world", "example/mini_core_hello_world.rs", "abc bcd"),
6363
TestCase::build_bin_and_run(
@@ -400,6 +400,7 @@ impl<'a> TestRunner<'a> {
400400
}
401401
spawn_and_wait(jit_cmd);
402402

403+
/*
403404
eprintln!("[JIT-lazy] {testname}");
404405
let mut jit_cmd = self.rustc_command([
405406
"-Zunstable-options",
@@ -413,6 +414,7 @@ impl<'a> TestRunner<'a> {
413414
jit_cmd.env("CG_CLIF_JIT_ARGS", args);
414415
}
415416
spawn_and_wait(jit_cmd);
417+
*/
416418
}
417419
}
418420
}
@@ -431,6 +433,7 @@ impl<'a> TestRunner<'a> {
431433
cmd.arg("--out-dir");
432434
cmd.arg(BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs));
433435
cmd.arg("-Cdebuginfo=2");
436+
cmd.arg("-Clto=thin");
434437
cmd.arg("--target");
435438
cmd.arg(&self.target_compiler.triple);
436439
cmd.arg("-Cpanic=abort");

example/mini_core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ struct PanicLocation {
786786
column: u32,
787787
}
788788

789+
/*
789790
#[no_mangle]
790791
#[cfg(not(all(windows, target_env = "gnu")))]
791792
pub fn get_tls() -> u8 {
@@ -794,3 +795,4 @@ pub fn get_tls() -> u8 {
794795
795796
A
796797
}
798+
*/

example/mini_core_hello_world.rs

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

335+
/*
335336
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
336337
unsafe {
337338
global_asm_test();
338339
naked_test();
339340
}
341+
*/
340342

341343
// Both statics have a reference that points to the same anonymous allocation.
342344
static REF1: &u8 = &42;
@@ -361,6 +363,7 @@ fn stack_val_align() {
361363
assert_eq!(&a as *const Foo as usize % 8192, 0);
362364
}
363365

366+
/*
364367
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
365368
extern "C" {
366369
fn global_asm_test();
@@ -385,6 +388,7 @@ global_asm! {
385388
ret
386389
"
387390
}
391+
*/
388392

389393
#[cfg(all(not(jit), target_arch = "x86_64"))]
390394
#[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+
edition = "2021"
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
@@ -254,6 +254,7 @@ pub(crate) fn compile_fn(
254254
}
255255
}
256256

257+
/*
257258
// Define debuginfo for function
258259
let debug_context = &mut cx.debug_context;
259260
profiler.generic_activity("generate debug info").run(|| {
@@ -265,6 +266,7 @@ pub(crate) fn compile_fn(
265266
);
266267
}
267268
});
269+
*/
268270
}
269271

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

src/driver/aot.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl OngoingCodegen {
141141
}
142142

143143
// Adapted from https://github.com/rust-lang/rust/blob/73476d49904751f8d90ce904e16dfbc278083d2c/compiler/rustc_codegen_ssa/src/back/write.rs#L547C1-L706C2
144-
fn produce_final_output_artifacts(
144+
pub(super) fn produce_final_output_artifacts(
145145
sess: &Session,
146146
codegen_results: &CodegenResults,
147147
crate_output: &OutputFilenames,
@@ -328,7 +328,7 @@ fn produce_final_output_artifacts(
328328
// These are used in linking steps and will be cleaned up afterward.
329329
}
330330

331-
fn make_module(sess: &Session, name: String) -> UnwindModule<ObjectModule> {
331+
pub(super) fn make_module(sess: &Session, name: String) -> UnwindModule<ObjectModule> {
332332
let isa = crate::build_isa(sess);
333333

334334
let mut builder =
@@ -379,7 +379,7 @@ fn emit_cgu(
379379
})
380380
}
381381

382-
fn emit_module(
382+
pub(super) fn emit_module(
383383
output_filenames: &OutputFilenames,
384384
prof: &SelfProfilerRef,
385385
mut object: cranelift_object::object::write::Object<'_>,
@@ -488,7 +488,7 @@ fn reuse_workproduct_for_cgu(
488488
})
489489
}
490490

491-
fn codegen_cgu_content(
491+
pub(super) fn codegen_cgu_content(
492492
tcx: TyCtxt<'_>,
493493
module: &mut dyn Module,
494494
cgu_name: rustc_span::Symbol,
@@ -595,7 +595,7 @@ fn module_codegen(
595595
}))
596596
}
597597

598-
fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> CompiledModule {
598+
pub(crate) fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> CompiledModule {
599599
use rustc_middle::mir::mono::CodegenUnitNameBuilder;
600600

601601
let _timer = tcx.sess.timer("write compressed metadata");
@@ -627,7 +627,7 @@ fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> Compiled
627627
}
628628
}
629629

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

src/driver/jit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,15 @@ fn create_jit_module(tcx: TyCtxt<'_>, hotswap: bool) -> (UnwindModule<JITModule>
6666
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
6767
jit_builder.hotswap(hotswap);
6868
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
69-
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
69+
//jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
7070
jit_builder.symbol("__clif_jit_fn", clif_jit_fn as *const u8);
7171
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);
7272

73+
#[cfg(feature = "lto")]
74+
for (_name, module) in super::lto::load_lto_modules(tcx, &crate_info) {
75+
module.apply_to(&mut jit_module);
76+
}
77+
7378
let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, sym::dummy_cgu_name);
7479

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

0 commit comments

Comments
 (0)