Emit LLVM optimization remarks when enabled with -Cremark

The default diagnostic handler considers all remarks to be disabled by
default unless configured otherwise through LLVM internal flags:
`-pass-remarks`, `-pass-remarks-missed`, and `-pass-remarks-analysis`.
This behaviour makes `-Cremark` ineffective on its own.

Fix this by configuring a custom diagnostic handler that enables
optimization remarks based on the value of `-Cremark` option. With
`-Cremark=all` enabling all remarks.
This commit is contained in:
Tomasz Miąsko 2021-11-12 00:00:00 +00:00
parent b16ac4cbba
commit 6846674c75
4 changed files with 158 additions and 12 deletions

View file

@ -259,6 +259,7 @@ pub(crate) fn save_temp_bitcode(
pub struct DiagnosticHandlers<'a> {
data: *mut (&'a CodegenContext<LlvmCodegenBackend>, &'a Handler),
llcx: &'a llvm::Context,
old_handler: Option<&'a llvm::DiagnosticHandler>,
}
impl<'a> DiagnosticHandlers<'a> {
@ -267,12 +268,35 @@ impl<'a> DiagnosticHandlers<'a> {
handler: &'a Handler,
llcx: &'a llvm::Context,
) -> Self {
let remark_passes_all: bool;
let remark_passes: Vec<CString>;
match &cgcx.remark {
Passes::All => {
remark_passes_all = true;
remark_passes = Vec::new();
}
Passes::Some(passes) => {
remark_passes_all = false;
remark_passes =
passes.iter().map(|name| CString::new(name.as_str()).unwrap()).collect();
}
};
let remark_passes: Vec<*const c_char> =
remark_passes.iter().map(|name: &CString| name.as_ptr()).collect();
let data = Box::into_raw(Box::new((cgcx, handler)));
unsafe {
let old_handler = llvm::LLVMRustContextGetDiagnosticHandler(llcx);
llvm::LLVMRustContextConfigureDiagnosticHandler(
llcx,
diagnostic_handler,
data.cast(),
remark_passes_all,
remark_passes.as_ptr(),
remark_passes.len(),
);
llvm::LLVMRustSetInlineAsmDiagnosticHandler(llcx, inline_asm_handler, data.cast());
llvm::LLVMContextSetDiagnosticHandler(llcx, diagnostic_handler, data.cast());
DiagnosticHandlers { data, llcx, old_handler }
}
DiagnosticHandlers { data, llcx }
}
}
@ -281,7 +305,7 @@ impl<'a> Drop for DiagnosticHandlers<'a> {
use std::ptr::null_mut;
unsafe {
llvm::LLVMRustSetInlineAsmDiagnosticHandler(self.llcx, inline_asm_handler, null_mut());
llvm::LLVMContextSetDiagnosticHandler(self.llcx, diagnostic_handler, null_mut());
llvm::LLVMRustContextSetDiagnosticHandler(self.llcx, self.old_handler);
drop(Box::from_raw(self.data));
}
}

View file

@ -672,8 +672,12 @@ pub struct OperandBundleDef<'a>(InvariantOpaque<'a>);
#[repr(C)]
pub struct Linker<'a>(InvariantOpaque<'a>);
pub type DiagnosticHandler = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
pub type InlineAsmDiagHandler = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
extern "C" {
pub type DiagnosticHandler;
}
pub type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
pub type InlineAsmDiagHandlerTy = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
pub mod coverageinfo {
use super::coverage_map;
@ -2286,12 +2290,6 @@ extern "C" {
#[allow(improper_ctypes)]
pub fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);
pub fn LLVMContextSetDiagnosticHandler(
C: &Context,
Handler: DiagnosticHandler,
DiagnosticContext: *mut c_void,
);
#[allow(improper_ctypes)]
pub fn LLVMRustUnpackOptimizationDiagnostic(
DI: &'a DiagnosticInfo,
@ -2321,7 +2319,7 @@ extern "C" {
pub fn LLVMRustSetInlineAsmDiagnosticHandler(
C: &Context,
H: InlineAsmDiagHandler,
H: InlineAsmDiagHandlerTy,
CX: *mut c_void,
);
@ -2436,4 +2434,19 @@ extern "C" {
mod_id: *const c_char,
data: &ThinLTOData,
);
pub fn LLVMRustContextGetDiagnosticHandler(Context: &Context) -> Option<&DiagnosticHandler>;
pub fn LLVMRustContextSetDiagnosticHandler(
context: &Context,
diagnostic_handler: Option<&DiagnosticHandler>,
);
pub fn LLVMRustContextConfigureDiagnosticHandler(
context: &Context,
diagnostic_handler_callback: DiagnosticHandlerTy,
diagnostic_handler_context: *mut c_void,
remark_all_passes: bool,
remark_passes: *const *const c_char,
remark_passes_len: usize,
);
}