Skip to content

Commit 20fe948

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 604d40f commit 20fe948

18 files changed

Lines changed: 732 additions & 56 deletions

Cargo.lock

Lines changed: 46 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.133.0", optional = true }
1616
cranelift-object = { version = "0.133.0", default-features = false }
1717
target-lexicon = "0.13"
1818
gimli = { version = "0.33", default-features = false, features = ["write"] }
19-
object = { version = "0.39.1", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }
19+
object = { version = "0.39.1", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe", "unaligned"] }
2020

2121
indexmap = "2.0.0"
2222
libloading = { version = "0.9.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
# Uncomment to use an unreleased version of cranelift
2628
#[patch.crates-io]
@@ -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
@@ -237,6 +237,9 @@ fn build_clif_sysroot_for_triple(
237237
rustflags.push("--remap-path-prefix".to_owned());
238238
rustflags.push(format!("library/={}/library", prefix.to_str().unwrap()));
239239
}
240+
rustflags.push("-Clto=thin".to_owned());
241+
rustflags.push("-Zdylib-lto".to_owned());
242+
rustflags.push("-Cembed-bitcode=yes".to_owned());
240243
compiler.rustflags.extend(rustflags);
241244
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
242245
build_cmd.arg("--release");
@@ -253,6 +256,7 @@ fn build_clif_sysroot_for_triple(
253256
if env::var_os("CARGO_BUILD_INCREMENTAL").is_none() {
254257
build_cmd.env("CARGO_BUILD_INCREMENTAL", "true");
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("build"))

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(
@@ -461,6 +461,7 @@ impl<'a> TestRunner<'a> {
461461
cmd.arg("--out-dir");
462462
cmd.arg(BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs));
463463
cmd.arg("-Cdebuginfo=2");
464+
cmd.arg("-Clto=thin");
464465
cmd.arg("--target");
465466
cmd.arg(&self.target_compiler.triple);
466467
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
@@ -834,6 +834,7 @@ struct PanicLocation {
834834
column: u32,
835835
}
836836

837+
/*
837838
#[unsafe(no_mangle)]
838839
#[cfg(not(all(windows, target_env = "gnu")))]
839840
pub fn get_tls() -> u8 {
@@ -842,3 +843,4 @@ pub fn get_tls() -> u8 {
842843
843844
A
844845
}
846+
*/

example/mini_core_hello_world.rs

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

347+
/*
347348
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
348349
unsafe {
349350
global_asm_test();
350351
naked_test();
351352
}
353+
*/
352354

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

379+
/*
377380
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
378381
unsafe extern "C" {
379382
fn global_asm_test();
@@ -398,6 +401,7 @@ global_asm! {
398401
ret
399402
"
400403
}
404+
*/
401405

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

example/std_example.rs

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

113113
#[cfg(target_arch = "x86_64")]
114114
unsafe {
115-
test_simd();
115+
//test_simd();
116116
}
117117

118118
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 <17426603+bjorn3@users.noreply.github.com>
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
@@ -246,6 +246,7 @@ pub(crate) fn compile_fn(
246246
}
247247
}
248248

249+
/*
249250
// Define debuginfo for function
250251
prof.generic_activity("generate debug info").run(|| {
251252
if let Some(debug_context) = debug_context {
@@ -256,6 +257,7 @@ pub(crate) fn compile_fn(
256257
);
257258
}
258259
});
260+
*/
259261
}
260262

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

0 commit comments

Comments
 (0)