Skip to content

Commit 78fe180

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 989bb25 commit 78fe180

18 files changed

+644
-16
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(
@@ -409,6 +409,7 @@ impl<'a> TestRunner<'a> {
409409
cmd.arg("--out-dir");
410410
cmd.arg(BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs));
411411
cmd.arg("-Cdebuginfo=2");
412+
cmd.arg("-Clto=thin");
412413
cmd.arg("--target");
413414
cmd.arg(&self.target_compiler.triple);
414415
if !self.panic_unwind_support {

config.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ aot.mini_core_hello_world
2020

2121
testsuite.base_sysroot
2222
aot.arbitrary_self_types_pointers_and_wrappers
23-
jit.std_example
23+
#jit.std_example
2424
aot.std_example
2525
aot.dst_field_align
2626
aot.subslice-patterns-const-eval

example/mini_core.rs

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

779+
/*
779780
#[unsafe(no_mangle)]
780781
#[cfg(not(all(windows, target_env = "gnu")))]
781782
pub fn get_tls() -> u8 {
@@ -784,3 +785,4 @@ pub fn get_tls() -> u8 {
784785
785786
A
786787
}
788+
*/

example/mini_core_hello_world.rs

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

329+
/*
329330
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
330331
unsafe {
331332
global_asm_test();
332333
naked_test();
333334
}
335+
*/
334336

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

360+
/*
358361
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
359362
unsafe extern "C" {
360363
fn global_asm_test();
@@ -379,6 +382,7 @@ global_asm! {
379382
ret
380383
"
381384
}
385+
*/
382386

383387
#[cfg(all(not(jit), target_arch = "x86_64"))]
384388
#[unsafe(naked)]

example/std_example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn main() {
120120

121121
#[cfg(target_arch = "x86_64")]
122122
unsafe {
123-
test_simd();
123+
//test_simd();
124124
}
125125

126126
Box::pin(
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) {

0 commit comments

Comments
 (0)