Skip to content

Commit 2e587df

Browse files
Provide a way of accessing the ThinLTO module import map in rustc.
1 parent 1114ab6 commit 2e587df

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ extern { pub type ThinLTOData; }
363363
/// LLVMRustThinLTOBuffer
364364
extern { pub type ThinLTOBuffer; }
365365

366+
// LLVMRustModuleNameCallback
367+
pub type ThinLTOModuleNameCallback =
368+
unsafe extern "C" fn(*mut c_void, *const c_char, *const c_char);
369+
366370
/// LLVMRustThinLTOModule
367371
#[repr(C)]
368372
pub struct ThinLTOModule {
@@ -1622,6 +1626,11 @@ extern "C" {
16221626
Data: &ThinLTOData,
16231627
Module: &Module,
16241628
) -> bool;
1629+
pub fn LLVMRustGetThinLTOModuleImports(
1630+
Data: *const ThinLTOData,
1631+
ModuleNameCallback: ThinLTOModuleNameCallback,
1632+
CallbackPayload: *mut c_void,
1633+
);
16251634
pub fn LLVMRustFreeThinLTOData(Data: &'static mut ThinLTOData);
16261635
pub fn LLVMRustParseBitcodeForThinLTO(
16271636
Context: &Context,

src/rustllvm/PassWrapper.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,28 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
11231123
return true;
11241124
}
11251125

1126+
extern "C" typedef void (*LLVMRustModuleNameCallback)(void*, // payload
1127+
const char*, // importing module name
1128+
const char*); // imported module name
1129+
1130+
// Calls `module_name_callback` for each module import done by ThinLTO.
1131+
// The callback is provided with regular null-terminated C strings.
1132+
extern "C" void
1133+
LLVMRustGetThinLTOModuleImports(const LLVMRustThinLTOData *data,
1134+
LLVMRustModuleNameCallback module_name_callback,
1135+
void* callback_payload) {
1136+
for (const auto& importing_module : data->ImportLists) {
1137+
const std::string importing_module_id = importing_module.getKey().str();
1138+
const auto& imports = importing_module.getValue();
1139+
for (const auto& imported_module : imports) {
1140+
const std::string imported_module_id = imported_module.getKey().str();
1141+
module_name_callback(callback_payload,
1142+
importing_module_id.c_str(),
1143+
imported_module_id.c_str());
1144+
}
1145+
}
1146+
}
1147+
11261148
// This struct and various functions are sort of a hack right now, but the
11271149
// problem is that we've got in-memory LLVM modules after we generate and
11281150
// optimize all codegen-units for one compilation in rustc. To be compatible
@@ -1288,6 +1310,11 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M) {
12881310
report_fatal_error("ThinLTO not available");
12891311
}
12901312

1313+
extern "C" LLVMRustThinLTOModuleImports
1314+
LLVMRustGetLLVMRustThinLTOModuleImports(const LLVMRustThinLTOData *Data) {
1315+
report_fatal_error("ThinLTO not available");
1316+
}
1317+
12911318
extern "C" void
12921319
LLVMRustFreeThinLTOData(LLVMRustThinLTOData *Data) {
12931320
report_fatal_error("ThinLTO not available");

0 commit comments

Comments
 (0)