Skip to content

Commit ee81e0d

Browse files
committed
rustc_codegen_llvm: rework internal API per review
1 parent fb4ee7d commit ee81e0d

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::back::write::{
2-
self, bitcode_section_name, save_temp_bitcode, target_is_aix, target_is_apple,
3-
CodegenDiagnosticsStage, DiagnosticHandlers,
2+
self, bitcode_section_name, save_temp_bitcode, CodegenDiagnosticsStage, DiagnosticHandlers,
43
};
54
use crate::errors::{
65
DynamicLinkingWithLTO, LlvmError, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib,
@@ -124,12 +123,9 @@ fn prepare_lto(
124123
.filter(|&(name, _)| looks_like_rust_object_file(name));
125124
for (name, child) in obj_files {
126125
info!("adding bitcode from {}", name);
127-
let is_apple = target_is_apple(cgcx);
128-
let is_aix = target_is_aix(cgcx);
129126
match get_bitcode_slice_from_object_data(
130127
child.data(&*archive_data).expect("corrupt rlib"),
131-
is_apple,
132-
is_aix,
128+
cgcx,
133129
) {
134130
Ok(data) => {
135131
let module = SerializedModule::FromRlib(data.to_vec());
@@ -151,17 +147,16 @@ fn prepare_lto(
151147
Ok((symbols_below_threshold, upstream_modules))
152148
}
153149

154-
fn get_bitcode_slice_from_object_data(
155-
obj: &[u8],
156-
is_apple: bool,
157-
is_aix: bool,
158-
) -> Result<&[u8], LtoBitcodeFromRlib> {
150+
fn get_bitcode_slice_from_object_data<'a>(
151+
obj: &'a [u8],
152+
cgcx: &CodegenContext<LlvmCodegenBackend>,
153+
) -> Result<&'a [u8], LtoBitcodeFromRlib> {
159154
// The object crate doesn't understand bitcode files, but we can just sniff for the possible
160155
// magic strings here and return the whole slice directly.
161156
if obj.starts_with(b"\xDE\xC0\x17\x0B") || obj.starts_with(b"BC\xC0\xDE") {
162157
return Ok(obj);
163158
}
164-
let section = bitcode_section_name(is_apple, is_aix).trim_end_matches('\0');
159+
let section = bitcode_section_name(cgcx).trim_end_matches('\0');
165160
match object::read::File::parse(obj) {
166161
Ok(f) => match f.section_by_name(section) {
167162
Some(d) => Ok(d.data().unwrap()),

compiler/rustc_codegen_llvm/src/back/write.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -853,27 +853,27 @@ fn create_section_with_flags_asm(section_name: &str, section_flags: &str, data:
853853
asm
854854
}
855855

856-
pub(crate) fn bitcode_section_name(is_apple: bool, is_aix: bool) -> &'static str {
857-
if is_apple {
858-
"__LLVM,__bitcode\0"
859-
} else if is_aix {
860-
".ipa\0"
861-
} else {
862-
".llvmbc\0"
863-
}
864-
}
865-
866-
pub(crate) fn target_is_apple(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
856+
fn target_is_apple(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
867857
cgcx.opts.target_triple.triple().contains("-ios")
868858
|| cgcx.opts.target_triple.triple().contains("-darwin")
869859
|| cgcx.opts.target_triple.triple().contains("-tvos")
870860
|| cgcx.opts.target_triple.triple().contains("-watchos")
871861
}
872862

873-
pub(crate) fn target_is_aix(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
863+
fn target_is_aix(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
874864
cgcx.opts.target_triple.triple().contains("-aix")
875865
}
876866

867+
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static str {
868+
if target_is_apple(cgcx) {
869+
"__LLVM,__bitcode\0"
870+
} else if target_is_aix(cgcx) {
871+
".ipa\0"
872+
} else {
873+
".llvmbc\0"
874+
}
875+
}
876+
877877
/// Embed the bitcode of an LLVM module in the LLVM module itself.
878878
///
879879
/// This is done primarily for iOS where it appears to be standard to compile C
@@ -950,7 +950,7 @@ unsafe fn embed_bitcode(
950950
);
951951
llvm::LLVMSetInitializer(llglobal, llconst);
952952

953-
let section = bitcode_section_name(is_apple, is_aix);
953+
let section = bitcode_section_name(cgcx);
954954
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
955955
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
956956
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);

0 commit comments

Comments
 (0)