Skip to content

Commit 230a8ee

Browse files
committed
Auto merge of #98100 - bjorn3:use_object_for_bitcode_reading, r=wesleywiser
Use object instead of LLVM for reading bitcode from rlibs Together with changes I plan to make as part of #97485 this will allow entirely removing usage of LLVM's archive reader and thus allow removing `archive_ro.rs` and `ArchiveWrapper.cpp`.
2 parents 0631ea5 + 395d564 commit 230a8ee

File tree

6 files changed

+18
-30
lines changed

6 files changed

+18
-30
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3334,6 +3334,7 @@ dependencies = [
33343334
"libc",
33353335
"libloading",
33363336
"measureme",
3337+
"object 0.29.0",
33373338
"rustc-demangle",
33383339
"rustc_ast",
33393340
"rustc_attr",

compiler/rustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cstr = "0.2"
1313
libc = "0.2"
1414
libloading = "0.7.1"
1515
measureme = "10.0.0"
16+
object = { version = "0.29.0", default-features = false, features = ["std", "read_core", "archive", "coff", "elf", "macho", "pe"] }
1617
tracing = "0.1"
1718
rustc_middle = { path = "../rustc_middle" }
1819
rustc-demangle = "0.1.21"

compiler/rustc_codegen_llvm/src/back/lto.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use crate::back::write::{
22
self, save_temp_bitcode, to_llvm_opt_settings, with_llvm_pmb, DiagnosticHandlers,
33
};
4-
use crate::llvm::archive_ro::ArchiveRO;
54
use crate::llvm::{self, build_string, False, True};
65
use crate::{llvm_util, LlvmCodegenBackend, ModuleLlvm};
6+
use object::read::archive::ArchiveFile;
77
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
88
use rustc_codegen_ssa::back::symbol_export;
99
use rustc_codegen_ssa::back::write::{CodegenContext, FatLTOInput, TargetMachineFactoryConfig};
1010
use rustc_codegen_ssa::traits::*;
1111
use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind};
1212
use rustc_data_structures::fx::FxHashMap;
13+
use rustc_data_structures::memmap::Mmap;
1314
use rustc_errors::{FatalError, Handler};
1415
use rustc_hir::def_id::LOCAL_CRATE;
1516
use rustc_middle::bug;
@@ -107,14 +108,24 @@ fn prepare_lto(
107108
.extend(exported_symbols[&cnum].iter().filter_map(symbol_filter));
108109
}
109110

110-
let archive = ArchiveRO::open(path).expect("wanted an rlib");
111+
let archive_data = unsafe {
112+
Mmap::map(std::fs::File::open(&path).expect("couldn't open rlib"))
113+
.expect("couldn't map rlib")
114+
};
115+
let archive = ArchiveFile::parse(&*archive_data).expect("wanted an rlib");
111116
let obj_files = archive
112-
.iter()
113-
.filter_map(|child| child.ok().and_then(|c| c.name().map(|name| (name, c))))
117+
.members()
118+
.filter_map(|child| {
119+
child.ok().and_then(|c| {
120+
std::str::from_utf8(c.name()).ok().map(|name| (name.trim(), c))
121+
})
122+
})
114123
.filter(|&(name, _)| looks_like_rust_object_file(name));
115124
for (name, child) in obj_files {
116125
info!("adding bitcode from {}", name);
117-
match get_bitcode_slice_from_object_data(child.data()) {
126+
match get_bitcode_slice_from_object_data(
127+
child.data(&*archive_data).expect("corrupt rlib"),
128+
) {
118129
Ok(data) => {
119130
let module = SerializedModule::FromRlib(data.to_vec());
120131
upstream_modules.push((module, CString::new(name).unwrap()));

compiler/rustc_codegen_llvm/src/llvm/archive_ro.rs

-11
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ impl<'a> Child<'a> {
8383
}
8484
}
8585
}
86-
87-
pub fn data(&self) -> &'a [u8] {
88-
unsafe {
89-
let mut data_len = 0;
90-
let data_ptr = super::LLVMRustArchiveChildData(self.raw, &mut data_len);
91-
if data_ptr.is_null() {
92-
panic!("failed to read data from archive child");
93-
}
94-
slice::from_raw_parts(data_ptr as *const u8, data_len as usize)
95-
}
96-
}
9786
}
9887

9988
impl<'a> Drop for Child<'a> {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,6 @@ extern "C" {
23892389
AIR: &ArchiveIterator<'a>,
23902390
) -> Option<&'a mut ArchiveChild<'a>>;
23912391
pub fn LLVMRustArchiveChildName(ACR: &ArchiveChild<'_>, size: &mut size_t) -> *const c_char;
2392-
pub fn LLVMRustArchiveChildData(ACR: &ArchiveChild<'_>, size: &mut size_t) -> *const c_char;
23932392
pub fn LLVMRustArchiveChildFree<'a>(ACR: &'a mut ArchiveChild<'a>);
23942393
pub fn LLVMRustArchiveIteratorFree<'a>(AIR: &'a mut ArchiveIterator<'a>);
23952394
pub fn LLVMRustDestroyArchive(AR: &'static mut Archive);

compiler/rustc_llvm/llvm-wrapper/ArchiveWrapper.cpp

-13
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,6 @@ LLVMRustArchiveChildName(LLVMRustArchiveChildConstRef Child, size_t *Size) {
154154
return Name.data();
155155
}
156156

157-
extern "C" const char *LLVMRustArchiveChildData(LLVMRustArchiveChildRef Child,
158-
size_t *Size) {
159-
StringRef Buf;
160-
Expected<StringRef> BufOrErr = Child->getBuffer();
161-
if (!BufOrErr) {
162-
LLVMRustSetLastError(toString(BufOrErr.takeError()).c_str());
163-
return nullptr;
164-
}
165-
Buf = BufOrErr.get();
166-
*Size = Buf.size();
167-
return Buf.data();
168-
}
169-
170157
extern "C" LLVMRustArchiveMemberRef
171158
LLVMRustArchiveMemberNew(char *Filename, char *Name,
172159
LLVMRustArchiveChildRef Child) {

0 commit comments

Comments
 (0)