Skip to content

Commit 815a114

Browse files
committed
Implement printing to file in PassWrapper
1 parent 6e734fc commit 815a114

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,12 @@ extern "C" {
22802280

22812281
pub fn LLVMRustHasFeature(T: &TargetMachine, s: *const c_char) -> bool;
22822282

2283-
pub fn LLVMRustPrintTargetCPUs(T: &TargetMachine, cpu: *const c_char);
2283+
pub fn LLVMRustPrintTargetCPUs(
2284+
T: &TargetMachine,
2285+
cpu: *const c_char,
2286+
print: unsafe extern "C" fn(out: *mut c_void, string: *const c_char, len: usize),
2287+
out: *mut c_void,
2288+
);
22842289
pub fn LLVMRustGetTargetFeaturesCount(T: &TargetMachine) -> size_t;
22852290
pub fn LLVMRustGetTargetFeature(
22862291
T: &TargetMachine,

compiler/rustc_codegen_llvm/src/llvm_util.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use rustc_session::config::{PrintKind, PrintRequest};
1717
use rustc_session::Session;
1818
use rustc_span::symbol::Symbol;
1919
use rustc_target::spec::{MergeFunctions, PanicStrategy};
20-
use std::ffi::{CStr, CString};
2120

21+
use std::ffi::{c_char, c_void, CStr, CString};
2222
use std::path::Path;
2323
use std::ptr;
2424
use std::slice;
@@ -401,7 +401,7 @@ fn print_target_features(out: &mut dyn PrintBackendInfo, sess: &Session, tm: &ll
401401
writeln!(out, "and may be renamed or removed in a future version of LLVM or rustc.\n");
402402
}
403403

404-
pub(crate) fn print(req: &PrintRequest, out: &mut dyn PrintBackendInfo, sess: &Session) {
404+
pub(crate) fn print(req: &PrintRequest, mut out: &mut dyn PrintBackendInfo, sess: &Session) {
405405
require_inited();
406406
let tm = create_informational_target_machine(sess);
407407
match req.kind {
@@ -411,7 +411,19 @@ pub(crate) fn print(req: &PrintRequest, out: &mut dyn PrintBackendInfo, sess: &S
411411
// at least as long as the C function
412412
let cpu_cstring = CString::new(handle_native(sess.target.cpu.as_ref()))
413413
.unwrap_or_else(|e| bug!("failed to convert to cstring: {}", e));
414-
unsafe { llvm::LLVMRustPrintTargetCPUs(tm, cpu_cstring.as_ptr()) };
414+
unsafe extern "C" fn callback(out: *mut c_void, string: *const c_char, len: usize) {
415+
let out = &mut *(out as *mut &mut dyn PrintBackendInfo);
416+
let bytes = slice::from_raw_parts(string as *const u8, len);
417+
write!(out, "{}", String::from_utf8_lossy(bytes));
418+
}
419+
unsafe {
420+
llvm::LLVMRustPrintTargetCPUs(
421+
tm,
422+
cpu_cstring.as_ptr(),
423+
callback,
424+
&mut out as *mut &mut dyn PrintBackendInfo as *mut c_void,
425+
);
426+
}
415427
}
416428
PrintKind::TargetFeatures => print_target_features(out, sess, tm),
417429
_ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22

3+
#include <iomanip>
34
#include <vector>
45
#include <set>
56

@@ -306,44 +307,55 @@ static size_t getLongestEntryLength(ArrayRef<KV> Table) {
306307
return MaxLen;
307308
}
308309

309-
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM, const char* TargetCPU) {
310+
using PrintBackendInfo = void(void*, const char* Data, size_t Len);
311+
312+
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM,
313+
const char* TargetCPU,
314+
PrintBackendInfo Print,
315+
void* Out) {
310316
const TargetMachine *Target = unwrap(TM);
311317
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
312318
const Triple::ArchType HostArch = Triple(sys::getDefaultTargetTriple()).getArch();
313319
const Triple::ArchType TargetArch = Target->getTargetTriple().getArch();
314320

321+
std::ostringstream Buf;
322+
315323
#if LLVM_VERSION_GE(17, 0)
316324
const ArrayRef<SubtargetSubTypeKV> CPUTable = MCInfo->getAllProcessorDescriptions();
317325
#elif defined(LLVM_RUSTLLVM)
318326
const ArrayRef<SubtargetSubTypeKV> CPUTable = MCInfo->getCPUTable();
319327
#else
320-
printf("Full target CPU help is not supported by this LLVM version.\n\n");
328+
Buf << "Full target CPU help is not supported by this LLVM version.\n\n";
321329
SubtargetSubTypeKV TargetCPUKV = { TargetCPU, {{}}, {{}} };
322330
const ArrayRef<SubtargetSubTypeKV> CPUTable = TargetCPUKV;
323331
#endif
324332
unsigned MaxCPULen = getLongestEntryLength(CPUTable);
325333

326-
printf("Available CPUs for this target:\n");
334+
Buf << "Available CPUs for this target:\n";
327335
// Don't print the "native" entry when the user specifies --target with a
328336
// different arch since that could be wrong or misleading.
329337
if (HostArch == TargetArch) {
330338
MaxCPULen = std::max(MaxCPULen, (unsigned) std::strlen("native"));
331339
const StringRef HostCPU = sys::getHostCPUName();
332-
printf(" %-*s - Select the CPU of the current host (currently %.*s).\n",
333-
MaxCPULen, "native", (int)HostCPU.size(), HostCPU.data());
340+
Buf << " " << std::left << std::setw(MaxCPULen) << "native"
341+
<< " - Select the CPU of the current host "
342+
"(currently " << HostCPU.str() << ").\n";
334343
}
335344
for (auto &CPU : CPUTable) {
336345
// Compare cpu against current target to label the default
337346
if (strcmp(CPU.Key, TargetCPU) == 0) {
338-
printf(" %-*s - This is the default target CPU"
339-
" for the current build target (currently %s).",
340-
MaxCPULen, CPU.Key, Target->getTargetTriple().str().c_str());
347+
Buf << " " << std::left << std::setw(MaxCPULen) << CPU.Key
348+
<< " - This is the default target CPU for the current build target "
349+
"(currently " << Target->getTargetTriple().str() << ").";
341350
}
342351
else {
343-
printf(" %-*s", MaxCPULen, CPU.Key);
352+
Buf << " " << CPU.Key;
344353
}
345-
printf("\n");
354+
Buf << "\n";
346355
}
356+
357+
const auto &BufString = Buf.str();
358+
Print(Out, BufString.data(), BufString.size());
347359
}
348360

349361
extern "C" size_t LLVMRustGetTargetFeaturesCount(LLVMTargetMachineRef TM) {

0 commit comments

Comments
 (0)