Skip to content

Commit b25c2bf

Browse files
authored
Merge pull request #678 from rust-lang/remove-embed-lto-bitcode
Remove EMBED_LTO_BITCODE
2 parents 3a8b6a1 + 1e4b273 commit b25c2bf

File tree

9 files changed

+90
-104
lines changed

9 files changed

+90
-104
lines changed

.github/workflows/release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
- name: Build
6363
run: |
6464
./y.sh prepare --only-libcore
65-
EMBED_LTO_BITCODE=1 ./y.sh build --sysroot --release --release-sysroot
65+
./y.sh build --sysroot --release --release-sysroot
6666
./y.sh test --cargo-tests
6767
./y.sh clean all
6868
@@ -80,11 +80,11 @@ jobs:
8080
# FIXME(antoyo): we cannot enable LTO for stdarch tests currently because of some failing LTO tests using proc-macros.
8181
# FIXME(antoyo): this should probably not be needed since we embed the LTO bitcode.
8282
printf '[profile.release]\nlto = "fat"\n' >> build/build_sysroot/sysroot_src/library/Cargo.toml
83-
EMBED_LTO_BITCODE=1 ./y.sh test --release --clean --release-sysroot --build-sysroot --keep-lto-tests ${{ matrix.commands }}
83+
./y.sh test --release --clean --release-sysroot --build-sysroot --keep-lto-tests ${{ matrix.commands }}
8484
8585
- name: LTO test
8686
run: |
87-
EMBED_LTO_BITCODE=1 CHANNEL="release" ./y.sh cargo build --release --manifest-path tests/hello-world/Cargo.toml
87+
CHANNEL="release" ./y.sh cargo build --release --manifest-path tests/hello-world/Cargo.toml
8888
call_found=$(objdump -dj .text tests/hello-world/target/release/hello_world | grep -c "call .*mylib.*my_func" ) ||:
8989
if [ $call_found -gt 0 ]; then
9090
echo "ERROR: call my_func found in asm"

Cargo.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ dependencies = [
5656

5757
[[package]]
5858
name = "gccjit"
59-
version = "2.9.0"
59+
version = "2.10.0"
6060
source = "registry+https://github.com/rust-lang/crates.io-index"
61-
checksum = "4a0e310ef75f396cd11b2443b353d55376656ca92c13cba36f92b7aff346ac1a"
61+
checksum = "60362e038e71e4bdc1a5b23fb45e1aba587b5947fe0db58f4871d95608f89eca"
6262
dependencies = [
6363
"gccjit_sys",
6464
]
6565

6666
[[package]]
6767
name = "gccjit_sys"
68-
version = "0.8.2"
68+
version = "0.9.0"
6969
source = "registry+https://github.com/rust-lang/crates.io-index"
70-
checksum = "95ed7572b30cd32430294dde6fb70822d58e67c6846a548647e8739776a0125b"
70+
checksum = "ddd542c8414e122217551c6af6b7d33acf51a227aee85276f218c087525e01bb"
7171
dependencies = [
7272
"libc",
7373
]

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ default = ["master"]
2424
[dependencies]
2525
object = { version = "0.37.0", default-features = false, features = ["std", "read"] }
2626
tempfile = "3.20"
27-
gccjit = "2.8"
27+
gccjit = "2.10"
2828
#gccjit = { git = "https://github.com/rust-lang/gccjit.rs" }
2929

3030
# Local copy.

Readme.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,6 @@ $ CHANNEL="release" $CG_GCCJIT_DIR/y.sh cargo run
141141

142142
If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./y.sh test`) you should use `CHANNEL="debug"` instead or omit `CHANNEL="release"` completely.
143143

144-
### LTO
145-
146-
To use LTO, you need to set the variable `EMBED_LTO_BITCODE=1` in addition to setting `lto = "fat"` in the `Cargo.toml`.
147-
148-
Failing to set `EMBED_LTO_BITCODE` will give you the following error:
149-
150-
```
151-
error: failed to copy bitcode to object file: No such file or directory (os error 2)
152-
```
153-
154144
### Rustc
155145

156146
If you want to run `rustc` directly, you can do so with:

libgccjit.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0256a7539a83aa26aade8cca138d9c69634a51b1
1+
28b84db392ac0a572f1a2a2a1317aa5f2bc742cb

src/back/lto.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::ffi::{CStr, CString};
2121
use std::fs::{self, File};
2222
use std::path::{Path, PathBuf};
2323
use std::sync::Arc;
24+
use std::sync::atomic::Ordering;
2425

2526
use gccjit::{Context, OutputKind};
2627
use object::read::archive::ArchiveFile;
@@ -38,7 +39,7 @@ use tempfile::{TempDir, tempdir};
3839

3940
use crate::back::write::save_temp_bitcode;
4041
use crate::errors::LtoBitcodeFromRlib;
41-
use crate::{GccCodegenBackend, GccContext, SyncContext, to_gcc_opt_level};
42+
use crate::{GccCodegenBackend, GccContext, LTO_SUPPORTED, LtoMode, SyncContext, to_gcc_opt_level};
4243

4344
struct LtoData {
4445
// TODO(antoyo): use symbols_below_threshold.
@@ -228,7 +229,7 @@ fn fat_lto(
228229
info!("linking {:?}", name);
229230
match bc_decoded {
230231
SerializedModule::Local(ref module_buffer) => {
231-
module.module_llvm.should_combine_object_files = true;
232+
module.module_llvm.lto_mode = LtoMode::Fat;
232233
module
233234
.module_llvm
234235
.context
@@ -533,7 +534,7 @@ pub fn optimize_thin_module(
533534
// that LLVM Context and Module.
534535
//let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
535536
//let llmod_raw = parse_module(llcx, module_name, thin_module.data(), &dcx)? as *const _;
536-
let mut should_combine_object_files = false;
537+
let mut lto_mode = LtoMode::None;
537538
let context = match thin_module.shared.thin_buffers.get(thin_module.idx) {
538539
Some(thin_buffer) => Arc::clone(&thin_buffer.context),
539540
None => {
@@ -544,7 +545,7 @@ pub fn optimize_thin_module(
544545
SerializedModule::Local(ref module_buffer) => {
545546
let path = module_buffer.0.to_str().expect("path");
546547
context.add_driver_option(path);
547-
should_combine_object_files = true;
548+
lto_mode = LtoMode::Thin;
548549
/*module.module_llvm.should_combine_object_files = true;
549550
module
550551
.module_llvm
@@ -559,11 +560,13 @@ pub fn optimize_thin_module(
559560
Arc::new(SyncContext::new(context))
560561
}
561562
};
563+
let lto_supported = LTO_SUPPORTED.load(Ordering::SeqCst);
562564
let module = ModuleCodegen::new_regular(
563565
thin_module.name().to_string(),
564566
GccContext {
565567
context,
566-
should_combine_object_files,
568+
lto_mode,
569+
lto_supported,
567570
// TODO(antoyo): use the correct relocation model here.
568571
relocation_model: RelocModel::Pic,
569572
temp_dir: None,

src/back/write.rs

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_target::spec::SplitDebuginfo;
1010

1111
use crate::base::add_pic_option;
1212
use crate::errors::CopyBitcode;
13-
use crate::{GccCodegenBackend, GccContext};
13+
use crate::{GccCodegenBackend, GccContext, LtoMode};
1414

1515
pub(crate) fn codegen(
1616
cgcx: &CodegenContext<GccCodegenBackend>,
@@ -24,12 +24,8 @@ pub(crate) fn codegen(
2424
{
2525
let context = &module.module_llvm.context;
2626

27-
let should_combine_object_files = module.module_llvm.should_combine_object_files;
28-
29-
// NOTE: Only generate object files with GIMPLE when this environment variable is set for
30-
// now because this requires a particular setup (same gcc/lto1/lto-wrapper commit as libgccjit).
31-
// TODO(antoyo): remove this environment variable.
32-
let fat_lto = env::var("EMBED_LTO_BITCODE").as_deref() == Ok("1");
27+
let lto_mode = module.module_llvm.lto_mode;
28+
let lto_supported = module.module_llvm.lto_supported;
3329

3430
let bc_out = cgcx.output_filenames.temp_path_for_cgu(
3531
OutputType::Bitcode,
@@ -43,80 +39,48 @@ pub(crate) fn codegen(
4339
);
4440

4541
if config.bitcode_needed() {
46-
if fat_lto {
42+
let _timer = cgcx
43+
.prof
44+
.generic_activity_with_arg("GCC_module_codegen_make_bitcode", &*module.name);
45+
46+
// TODO(antoyo)
47+
/*if let Some(bitcode_filename) = bc_out.file_name() {
48+
cgcx.prof.artifact_size(
49+
"llvm_bitcode",
50+
bitcode_filename.to_string_lossy(),
51+
data.len() as u64,
52+
);
53+
}*/
54+
55+
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
4756
let _timer = cgcx
4857
.prof
49-
.generic_activity_with_arg("GCC_module_codegen_make_bitcode", &*module.name);
50-
51-
// TODO(antoyo)
52-
/*if let Some(bitcode_filename) = bc_out.file_name() {
53-
cgcx.prof.artifact_size(
54-
"llvm_bitcode",
55-
bitcode_filename.to_string_lossy(),
56-
data.len() as u64,
57-
);
58-
}*/
59-
60-
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
61-
let _timer = cgcx.prof.generic_activity_with_arg(
62-
"GCC_module_codegen_emit_bitcode",
63-
&*module.name,
64-
);
58+
.generic_activity_with_arg("GCC_module_codegen_emit_bitcode", &*module.name);
59+
if lto_supported {
6560
context.add_command_line_option("-flto=auto");
6661
context.add_command_line_option("-flto-partition=one");
6762
// TODO(antoyo): remove since we don't want fat objects when it is for Bitcode only.
6863
context.add_command_line_option("-ffat-lto-objects");
69-
context.compile_to_file(
70-
OutputKind::ObjectFile,
71-
bc_out.to_str().expect("path to str"),
72-
);
7364
}
65+
context
66+
.compile_to_file(OutputKind::ObjectFile, bc_out.to_str().expect("path to str"));
67+
}
7468

75-
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full) {
76-
let _timer = cgcx.prof.generic_activity_with_arg(
77-
"GCC_module_codegen_embed_bitcode",
78-
&*module.name,
79-
);
69+
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full) {
70+
let _timer = cgcx
71+
.prof
72+
.generic_activity_with_arg("GCC_module_codegen_embed_bitcode", &*module.name);
73+
if lto_supported {
8074
// TODO(antoyo): maybe we should call embed_bitcode to have the proper iOS fixes?
8175
//embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, data);
8276

8377
context.add_command_line_option("-flto=auto");
8478
context.add_command_line_option("-flto-partition=one");
8579
context.add_command_line_option("-ffat-lto-objects");
86-
// TODO(antoyo): Send -plugin/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/liblto_plugin.so to linker (this should be done when specifying the appropriate rustc cli argument).
87-
context.compile_to_file(
88-
OutputKind::ObjectFile,
89-
bc_out.to_str().expect("path to str"),
90-
);
91-
}
92-
} else {
93-
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
94-
let _timer = cgcx.prof.generic_activity_with_arg(
95-
"GCC_module_codegen_emit_bitcode",
96-
&*module.name,
97-
);
98-
context.compile_to_file(
99-
OutputKind::ObjectFile,
100-
bc_out.to_str().expect("path to str"),
101-
);
102-
}
103-
104-
if config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full) {
105-
// TODO(antoyo): we might want to emit to emit an error here, saying to set the
106-
// environment variable EMBED_LTO_BITCODE.
107-
let _timer = cgcx.prof.generic_activity_with_arg(
108-
"GCC_module_codegen_embed_bitcode",
109-
&*module.name,
110-
);
111-
// TODO(antoyo): maybe we should call embed_bitcode to have the proper iOS fixes?
112-
//embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, data);
113-
114-
// TODO(antoyo): Send -plugin/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/liblto_plugin.so to linker (this should be done when specifying the appropriate rustc cli argument).
115-
context.compile_to_file(
116-
OutputKind::ObjectFile,
117-
bc_out.to_str().expect("path to str"),
118-
);
11980
}
81+
// TODO(antoyo): Send -plugin/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/liblto_plugin.so to linker (this should be done when specifying the appropriate rustc cli argument).
82+
context
83+
.compile_to_file(OutputKind::ObjectFile, bc_out.to_str().expect("path to str"));
12084
}
12185
}
12286

@@ -165,7 +129,10 @@ pub(crate) fn codegen(
165129
context.set_debug_info(true);
166130
context.dump_to_file(path, true);
167131
}
168-
if should_combine_object_files {
132+
if lto_mode != LtoMode::None {
133+
let fat_lto = lto_mode == LtoMode::Fat;
134+
// We need to check if we're doing LTO since this code is also used for the
135+
// dummy ThinLTO implementation to combine the object files.
169136
if fat_lto {
170137
context.add_command_line_option("-flto=auto");
171138
context.add_command_line_option("-flto-partition=one");

src/base.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_target::spec::SymbolVisibility;
2121

2222
use crate::builder::Builder;
2323
use crate::context::CodegenCx;
24-
use crate::{GccContext, LockedTargetInfo, SyncContext, gcc_util, new_context};
24+
use crate::{GccContext, LockedTargetInfo, LtoMode, SyncContext, gcc_util, new_context};
2525

2626
#[cfg(feature = "master")]
2727
pub fn visibility_to_gcc(visibility: Visibility) -> gccjit::Visibility {
@@ -74,6 +74,7 @@ pub fn compile_codegen_unit(
7474
tcx: TyCtxt<'_>,
7575
cgu_name: Symbol,
7676
target_info: LockedTargetInfo,
77+
lto_supported: bool,
7778
) -> (ModuleCodegen<GccContext>, u64) {
7879
let prof_timer = tcx.prof.generic_activity("codegen_module");
7980
let start_time = Instant::now();
@@ -82,7 +83,7 @@ pub fn compile_codegen_unit(
8283
let (module, _) = tcx.dep_graph.with_task(
8384
dep_node,
8485
tcx,
85-
(cgu_name, target_info),
86+
(cgu_name, target_info, lto_supported),
8687
module_codegen,
8788
Some(dep_graph::hash_result),
8889
);
@@ -95,7 +96,7 @@ pub fn compile_codegen_unit(
9596

9697
fn module_codegen(
9798
tcx: TyCtxt<'_>,
98-
(cgu_name, target_info): (Symbol, LockedTargetInfo),
99+
(cgu_name, target_info, lto_supported): (Symbol, LockedTargetInfo, bool),
99100
) -> ModuleCodegen<GccContext> {
100101
let cgu = tcx.codegen_unit(cgu_name);
101102
// Instantiate monomorphizations without filling out definitions yet...
@@ -247,7 +248,8 @@ pub fn compile_codegen_unit(
247248
GccContext {
248249
context: Arc::new(SyncContext::new(context)),
249250
relocation_model: tcx.sess.relocation_model(),
250-
should_combine_object_files: false,
251+
lto_supported,
252+
lto_mode: LtoMode::None,
251253
temp_dir: None,
252254
},
253255
)

0 commit comments

Comments
 (0)