Skip to content

Commit

Permalink
[WIP] Implement LTO support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjorn3 committed Dec 4, 2024
1 parent 88975a1 commit 75aea04
Show file tree
Hide file tree
Showing 14 changed files with 640 additions and 17 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ cranelift-jit = { version = "0.114.0", optional = true }
cranelift-object = { version = "0.114.0" }
target-lexicon = "0.12.0"
gimli = { version = "0.31", default-features = false, features = ["write"] }
object = { version = "0.36", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }
object = { version = "0.36", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe", "unaligned"] }

indexmap = "2.0.0"
libloading = { version = "0.8.0", optional = true }
smallvec = "1.8.1"
serde = { version = "1.0.203", features = ["derive"], optional = true }
postcard = { version = "1.0.8", default-features = false, features = ["use-std"], optional = true }

[patch.crates-io]
# Uncomment to use local checkout of cranelift
Expand All @@ -35,8 +37,9 @@ smallvec = "1.8.1"

[features]
# Enable features not ready to be enabled when compiling as part of rustc
unstable-features = ["jit", "inline_asm_sym"]
unstable-features = ["jit", "inline_asm_sym", "lto"]
jit = ["cranelift-jit", "libloading"]
lto = ["serde", "postcard", "cranelift-codegen/enable-serde", "cranelift-module/enable-serde"]
inline_asm_sym = []

[package.metadata.rust-analyzer]
Expand Down
4 changes: 4 additions & 0 deletions build_system/build_sysroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ fn build_clif_sysroot_for_triple(
prefix.to_str().unwrap()
));
}
rustflags.push("-Clto=thin".to_owned());
rustflags.push("-Zdylib-lto".to_owned());
rustflags.push("-Cembed-bitcode=yes".to_owned());
compiler.rustflags.extend(rustflags);
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
build_cmd.arg("--release");
Expand All @@ -253,6 +256,7 @@ fn build_clif_sysroot_for_triple(
if compiler.triple.contains("apple") {
build_cmd.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed");
}
build_cmd.env("CARGO_PROFILE_RELEASE_LTO", "thin");
spawn_and_wait(build_cmd);

for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
Expand Down
5 changes: 4 additions & 1 deletion build_system/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl TestCase {
}

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

/*
eprintln!("[JIT-lazy] {testname}");
let mut jit_cmd = self.rustc_command([
"-Zunstable-options",
Expand All @@ -413,6 +414,7 @@ impl<'a> TestRunner<'a> {
jit_cmd.env("CG_CLIF_JIT_ARGS", args);
}
spawn_and_wait(jit_cmd);
*/
}
}
}
Expand All @@ -431,6 +433,7 @@ impl<'a> TestRunner<'a> {
cmd.arg("--out-dir");
cmd.arg(BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs));
cmd.arg("-Cdebuginfo=2");
cmd.arg("-Clto=thin");
cmd.arg("--target");
cmd.arg(&self.target_compiler.triple);
cmd.arg("-Cpanic=abort");
Expand Down
2 changes: 2 additions & 0 deletions example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ struct PanicLocation {
column: u32,
}

/*
#[no_mangle]
#[cfg(not(all(windows, target_env = "gnu")))]
pub fn get_tls() -> u8 {
Expand All @@ -794,3 +795,4 @@ pub fn get_tls() -> u8 {
A
}
*/
4 changes: 4 additions & 0 deletions example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,13 @@ fn main() {
#[cfg(all(not(jit), not(all(windows, target_env = "gnu"))))]
test_tls();

/*
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
unsafe {
global_asm_test();
naked_test();
}
*/

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

/*
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
extern "C" {
fn global_asm_test();
Expand All @@ -385,6 +388,7 @@ global_asm! {
ret
"
}
*/

#[cfg(all(not(jit), target_arch = "x86_64"))]
#[naked]
Expand Down
25 changes: 25 additions & 0 deletions patches/0029-stdlib-dylib-crate-type.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
From 0910cbe862990b0c3a17c67bca199ebb4452b0ec Mon Sep 17 00:00:00 2001
From: bjorn3 <[email protected]>
Date: Tue, 28 Mar 2023 17:09:01 +0000
Subject: [PATCH] Disable dylib crate type

---
library/std/Cargo.toml | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml
index 598a4bf..3e68680 100644
--- a/library/std/Cargo.toml
+++ b/library/std/Cargo.toml
@@ -7,7 +7,7 @@ description = "The Rust Standard Library"
edition = "2021"

[lib]
-crate-type = ["dylib", "rlib"]
+crate-type = ["rlib"]

[dependencies]
alloc = { path = "../alloc", public = true }
--
2.34.1

2 changes: 2 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ pub(crate) fn compile_fn(
}
}

/*
// Define debuginfo for function
let debug_context = &mut cx.debug_context;
cx.profiler.generic_activity("generate debug info").run(|| {
Expand All @@ -263,6 +264,7 @@ pub(crate) fn compile_fn(
);
}
});
*/
}

fn verify_func(tcx: TyCtxt<'_>, writer: &crate::pretty_clif::CommentWriter, func: &Function) {
Expand Down
12 changes: 6 additions & 6 deletions src/driver/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl OngoingCodegen {
}

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

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

let mut builder =
Expand Down Expand Up @@ -379,7 +379,7 @@ fn emit_cgu(
})
}

fn emit_module(
pub(super) fn emit_module(
output_filenames: &OutputFilenames,
prof: &SelfProfilerRef,
mut object: cranelift_object::object::write::Object<'_>,
Expand Down Expand Up @@ -488,7 +488,7 @@ fn reuse_workproduct_for_cgu(
})
}

fn codegen_cgu_content(
pub(super) fn codegen_cgu_content(
tcx: TyCtxt<'_>,
module: &mut dyn Module,
cgu_name: rustc_span::Symbol,
Expand Down Expand Up @@ -680,7 +680,7 @@ pub(crate) fn run_aot(
})
}

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

Expand All @@ -703,7 +703,7 @@ fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
}
}

fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> CompiledModule {
pub(super) fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> CompiledModule {
tcx.sess.time("write compressed metadata", || {
use rustc_middle::mir::mono::CodegenUnitNameBuilder;

Expand Down
7 changes: 6 additions & 1 deletion src/driver/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ fn create_jit_module(tcx: TyCtxt<'_>, hotswap: bool) -> (UnwindModule<JITModule>
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
jit_builder.hotswap(hotswap);
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
//jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
jit_builder.symbol("__clif_jit_fn", clif_jit_fn as *const u8);
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);

#[cfg(feature = "lto")]
for (_name, module) in super::lto::load_lto_modules(tcx, &crate_info) {
module.apply_to(&mut jit_module);
}

let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, sym::dummy_cgu_name);

crate::allocator::codegen(tcx, &mut jit_module);
Expand Down
Loading

0 comments on commit 75aea04

Please sign in to comment.