Skip to content

Commit e8c1308

Browse files
committed
Auto merge of rust-lang#122671 - Mark-Simulacrum:const-panic-msg, r=Nilstrieb
Codegen const panic messages as function calls This skips emitting extra arguments at every callsite (of which there can be many). For a librustc_driver build with overflow checks enabled, this cuts 0.7MB from the resulting shared library (see [perf]). A sample improvement from nightly: ``` leaq str.0(%rip), %rdi leaq .Lalloc_d6aeb8e2aa19de39a7f0e861c998af13(%rip), %rdx movl $25, %esi callq *_ZN4core9panicking5panic17h17cabb89c5bcc999E@GOTPCREL(%rip) ``` to this PR: ``` leaq .Lalloc_d6aeb8e2aa19de39a7f0e861c998af13(%rip), %rdi callq *_RNvNtNtCsduqIKoij8JB_4core9panicking11panic_const23panic_const_div_by_zero@GOTPCREL(%rip) ``` [perf]: https://perf.rust-lang.org/compare.html?start=a7e4de13c1785819f4d61da41f6704ed69d5f203&end=64fbb4f0b2d621ff46d559d1e9f5ad89a8d7789b&stat=instructions:u
2 parents c5c3144 + 05783c8 commit e8c1308

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

example/mini_core.rs

+30
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,36 @@ pub fn panic(_msg: &'static str) -> ! {
465465
}
466466
}
467467

468+
macro_rules! panic_const {
469+
($($lang:ident = $message:expr,)+) => {
470+
#[cfg(not(bootstrap))]
471+
pub mod panic_const {
472+
use super::*;
473+
474+
$(
475+
#[track_caller]
476+
#[lang = stringify!($lang)]
477+
pub fn $lang() -> ! {
478+
panic($message);
479+
}
480+
)+
481+
}
482+
}
483+
}
484+
485+
panic_const! {
486+
panic_const_add_overflow = "attempt to add with overflow",
487+
panic_const_sub_overflow = "attempt to subtract with overflow",
488+
panic_const_mul_overflow = "attempt to multiply with overflow",
489+
panic_const_div_overflow = "attempt to divide with overflow",
490+
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
491+
panic_const_neg_overflow = "attempt to negate with overflow",
492+
panic_const_shr_overflow = "attempt to shift right with overflow",
493+
panic_const_shl_overflow = "attempt to shift left with overflow",
494+
panic_const_div_by_zero = "attempt to divide by zero",
495+
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
496+
}
497+
468498
#[lang = "panic_bounds_check"]
469499
#[track_caller]
470500
fn panic_bounds_check(index: usize, len: usize) -> ! {

src/base.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,14 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
372372
);
373373
}
374374
_ => {
375-
let msg_str = msg.description();
376-
codegen_panic(fx, msg_str, source_info);
375+
let location = fx.get_caller_location(source_info).load_scalar(fx);
376+
377+
codegen_panic_inner(
378+
fx,
379+
msg.panic_function(),
380+
&[location],
381+
Some(source_info.span),
382+
);
377383
}
378384
}
379385
}
@@ -957,20 +963,6 @@ pub(crate) fn codegen_operand<'tcx>(
957963
}
958964
}
959965

960-
pub(crate) fn codegen_panic<'tcx>(
961-
fx: &mut FunctionCx<'_, '_, 'tcx>,
962-
msg_str: &str,
963-
source_info: mir::SourceInfo,
964-
) {
965-
let location = fx.get_caller_location(source_info).load_scalar(fx);
966-
967-
let msg_ptr = fx.anonymous_str(msg_str);
968-
let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
969-
let args = [msg_ptr, msg_len, location];
970-
971-
codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, Some(source_info.span));
972-
}
973-
974966
pub(crate) fn codegen_panic_nounwind<'tcx>(
975967
fx: &mut FunctionCx<'_, '_, 'tcx>,
976968
msg_str: &str,

0 commit comments

Comments
 (0)