Skip to content

Commit 211b585

Browse files
committed
Auto merge of #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 1ee4ae9 + c8cb091 commit 211b585

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

example/mini_core.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,36 @@ pub fn panic(_msg: &'static str) -> ! {
418418
}
419419
}
420420

421+
macro_rules! panic_const {
422+
($($lang:ident = $message:expr,)+) => {
423+
#[cfg(not(bootstrap))]
424+
pub mod panic_const {
425+
use super::*;
426+
427+
$(
428+
#[track_caller]
429+
#[lang = stringify!($lang)]
430+
pub fn $lang() -> ! {
431+
panic($message);
432+
}
433+
)+
434+
}
435+
}
436+
}
437+
438+
panic_const! {
439+
panic_const_add_overflow = "attempt to add with overflow",
440+
panic_const_sub_overflow = "attempt to subtract with overflow",
441+
panic_const_mul_overflow = "attempt to multiply with overflow",
442+
panic_const_div_overflow = "attempt to divide with overflow",
443+
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
444+
panic_const_neg_overflow = "attempt to negate with overflow",
445+
panic_const_shr_overflow = "attempt to shift right with overflow",
446+
panic_const_shl_overflow = "attempt to shift left with overflow",
447+
panic_const_div_by_zero = "attempt to divide by zero",
448+
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
449+
}
450+
421451
#[lang = "panic_cannot_unwind"]
422452
fn panic_cannot_unwind() -> ! {
423453
unsafe {

0 commit comments

Comments
 (0)