Skip to content

Commit a821f4a

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 c28a379 commit a821f4a

File tree

15 files changed

+681
-20
lines changed

15 files changed

+681
-20
lines changed

Cargo.lock

Lines changed: 59 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.110.1", optional = true }
1616
cranelift-object = { version = "0.110.1" }
1717
target-lexicon = "0.12.0"
1818
gimli = { version = "0.28", 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ fn build_clif_sysroot_for_triple(
266266
prefix.to_str().unwrap()
267267
));
268268
}
269+
rustflags.push("-Clto=thin".to_owned());
270+
rustflags.push("-Zdylib-lto".to_owned());
271+
rustflags.push("-Cembed-bitcode=yes".to_owned());
269272
compiler.rustflags.extend(rustflags);
270273
let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
271274
if channel == "release" {

build_system/prepare.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ codegen-units = 10000
4545
debug-assertions = false
4646
overflow-checks = false
4747
codegen-units = 10000
48+
49+
[profile.dev]
50+
lto = "thin"
51+
52+
[profile.release]
53+
lto = "thin"
4854
"#,
4955
)
5056
.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(
@@ -408,6 +408,7 @@ impl<'a> TestRunner<'a> {
408408
}
409409
spawn_and_wait(jit_cmd);
410410

411+
/*
411412
eprintln!("[JIT-lazy] {testname}");
412413
let mut jit_cmd = self.rustc_command([
413414
"-Zunstable-options",
@@ -421,6 +422,7 @@ impl<'a> TestRunner<'a> {
421422
jit_cmd.env("CG_CLIF_JIT_ARGS", args);
422423
}
423424
spawn_and_wait(jit_cmd);
425+
*/
424426
}
425427
}
426428
}
@@ -439,6 +441,7 @@ impl<'a> TestRunner<'a> {
439441
cmd.arg("--out-dir");
440442
cmd.arg(format!("{}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display()));
441443
cmd.arg("-Cdebuginfo=2");
444+
cmd.arg("-Clto=thin");
442445
cmd.arg("--target");
443446
cmd.arg(&self.target_compiler.triple);
444447
cmd.arg("-Cpanic=abort");

example/mini_core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ struct PanicLocation {
735735
column: u32,
736736
}
737737

738+
/*
738739
#[no_mangle]
739740
#[cfg(not(all(windows, target_env = "gnu")))]
740741
pub fn get_tls() -> u8 {
@@ -743,3 +744,4 @@ pub fn get_tls() -> u8 {
743744
744745
A
745746
}
747+
*/

example/mini_core_hello_world.rs

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

336+
/*
336337
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
337338
unsafe {
338339
global_asm_test();
339340
naked_test();
340341
}
342+
*/
341343

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

367+
/*
365368
#[cfg(all(not(jit), target_arch = "x86_64", any(target_os = "linux", target_os = "macos")))]
366369
extern "C" {
367370
fn global_asm_test();
@@ -386,6 +389,7 @@ global_asm! {
386389
ret
387390
"
388391
}
392+
*/
389393

390394
#[cfg(all(not(jit), target_arch = "x86_64"))]
391395
#[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
@@ -248,6 +248,7 @@ pub(crate) fn compile_fn(
248248
}
249249
}
250250

251+
/*
251252
// Define debuginfo for function
252253
let debug_context = &mut cx.debug_context;
253254
cx.profiler.generic_activity("generate debug info").run(|| {
@@ -259,6 +260,7 @@ pub(crate) fn compile_fn(
259260
);
260261
}
261262
});
263+
*/
262264
}
263265

264266
pub(crate) fn verify_func(

src/driver/aot.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl OngoingCodegen {
135135
}
136136

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

325-
fn make_module(
325+
pub(super) fn make_module(
326326
sess: &Session,
327327
backend_config: &BackendConfig,
328328
name: String,
@@ -377,7 +377,7 @@ fn emit_cgu(
377377
})
378378
}
379379

380-
fn emit_module(
380+
pub(super) fn emit_module(
381381
output_filenames: &OutputFilenames,
382382
prof: &SelfProfilerRef,
383383
mut object: cranelift_object::object::write::Object<'_>,
@@ -481,7 +481,7 @@ fn reuse_workproduct_for_cgu(
481481
})
482482
}
483483

484-
fn codegen_cgu_content(
484+
pub(super) fn codegen_cgu_content(
485485
tcx: TyCtxt<'_>,
486486
module: &mut dyn Module,
487487
cgu_name: rustc_span::Symbol,
@@ -679,7 +679,7 @@ pub(crate) fn run_aot(
679679
})
680680
}
681681

682-
fn emit_allocator_module(
682+
pub(super) fn emit_allocator_module(
683683
tcx: TyCtxt<'_>,
684684
backend_config: &BackendConfig,
685685
) -> Option<CompiledModule> {
@@ -705,7 +705,7 @@ fn emit_allocator_module(
705705
}
706706
}
707707

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

0 commit comments

Comments
 (0)