From 4636b32a42ea3111f53048197092e7ac08e6f792 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 24 Jul 2014 21:33:46 -0700 Subject: [PATCH 1/6] Make most of the failure functions take &(&'static str, uint) Passing one pointer takes less code than one pointer and an integer. --- src/libcore/failure.rs | 45 ++++++++++++++++++++++++++++++++++++++--- src/libcore/macros.rs | 2 +- src/librustrt/unwind.rs | 19 +++++++++-------- src/libstd/macros.rs | 4 ++-- 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/libcore/failure.rs b/src/libcore/failure.rs index 4bc39db8ecf09..022258911f1e0 100644 --- a/src/libcore/failure.rs +++ b/src/libcore/failure.rs @@ -33,33 +33,72 @@ use fmt; use intrinsics; +#[cfg(stage0)] #[cold] #[inline(never)] // this is the slow path, always #[lang="fail_"] fn fail_(expr: &'static str, file: &'static str, line: uint) -> ! { format_args!(|args| -> () { - begin_unwind(args, file, line); + begin_unwind(args, &(file, line)); }, "{}", expr); unsafe { intrinsics::abort() } } +#[cfg(stage0)] #[cold] #[lang="fail_bounds_check"] fn fail_bounds_check(file: &'static str, line: uint, index: uint, len: uint) -> ! { format_args!(|args| -> () { - begin_unwind(args, file, line); + begin_unwind(args, &(file, line)); }, "index out of bounds: the len is {} but the index is {}", len, index); unsafe { intrinsics::abort() } } +#[cfg(stage0)] #[cold] -pub fn begin_unwind(fmt: &fmt::Arguments, file: &'static str, line: uint) -> ! { +pub fn begin_unwind(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! { #[allow(ctypes)] extern { #[lang = "begin_unwind"] fn begin_unwind(fmt: &fmt::Arguments, file: &'static str, line: uint) -> !; } + let (file, line) = *file_line; + unsafe { begin_unwind(fmt, file, line) } +} + +#[cfg(not(stage0))] +#[cold] #[inline(never)] // this is the slow path, always +#[lang="fail_"] +fn fail_(expr: &'static str, file: &'static str, line: uint) -> ! { + format_args!(|args| -> () { + begin_unwind(args, &(file, line)); + }, "{}", expr); + + unsafe { intrinsics::abort() } +} + +#[cfg(not(stage0))] +#[cold] +#[lang="fail_bounds_check"] +fn fail_bounds_check(file: &'static str, line: uint, + index: uint, len: uint) -> ! { + format_args!(|args| -> () { + begin_unwind(args, &(file, line)); + }, "index out of bounds: the len is {} but the index is {}", len, index); + unsafe { intrinsics::abort() } +} + +#[cfg(not(stage0))] +#[cold] +pub fn begin_unwind(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! { + #[allow(ctypes)] + extern { + #[lang = "begin_unwind"] + fn begin_unwind(fmt: &fmt::Arguments, file_line: &'static str, + line: uint) -> !; + } + let (file, line) = *file_line; unsafe { begin_unwind(fmt, file, line) } } diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 93c838198c559..e2709c4edde47 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -33,7 +33,7 @@ macro_rules! fail( // up with the number of calls to fail!() #[inline(always)] fn run_fmt(fmt: &::std::fmt::Arguments) -> ! { - ::core::failure::begin_unwind(fmt, file!(), line!()) + ::core::failure::begin_unwind(fmt, &(file!(), line!())) } format_args!(run_fmt, $fmt, $($arg)*) }); diff --git a/src/librustrt/unwind.rs b/src/librustrt/unwind.rs index cb1b6f46afe36..db2cae2371821 100644 --- a/src/librustrt/unwind.rs +++ b/src/librustrt/unwind.rs @@ -384,7 +384,7 @@ pub mod eabi { #[lang = "begin_unwind"] pub extern fn rust_begin_unwind(msg: &fmt::Arguments, file: &'static str, line: uint) -> ! { - begin_unwind_fmt(msg, file, line) + begin_unwind_fmt(msg, &(file, line)) } /// The entry point for unwinding with a formatted message. @@ -394,8 +394,7 @@ pub extern fn rust_begin_unwind(msg: &fmt::Arguments, /// on (e.g.) the inlining of other functions as possible), by moving /// the actual formatting into this shared place. #[inline(never)] #[cold] -pub fn begin_unwind_fmt(msg: &fmt::Arguments, file: &'static str, - line: uint) -> ! { +pub fn begin_unwind_fmt(msg: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! { use core::fmt::FormatWriter; // We do two allocations here, unfortunately. But (a) they're @@ -415,9 +414,10 @@ pub fn begin_unwind_fmt(msg: &fmt::Arguments, file: &'static str, let mut v = Vec::new(); let _ = write!(&mut VecWriter { v: &mut v }, "{}", msg); - begin_unwind_inner(box String::from_utf8(v).unwrap(), file, line) + begin_unwind_inner(box String::from_utf8(v).unwrap(), file_line) } +// FIXME: Need to change expr_fail in AstBuilder to change this to &(str, uint) /// This is the entry point of unwinding for fail!() and assert!(). #[inline(never)] #[cold] // avoid code bloat at the call sites as much as possible pub fn begin_unwind(msg: M, file: &'static str, line: uint) -> ! { @@ -429,13 +429,13 @@ pub fn begin_unwind(msg: M, file: &'static str, line: uint) -> ! // failing. // see below for why we do the `Any` coercion here. - begin_unwind_inner(box msg, file, line) + begin_unwind_inner(box msg, &(file, line)) } /// Unwinding for `fail!()`. Saves passing a string. #[inline(never)] #[cold] #[experimental] -pub fn begin_unwind_no_time_to_explain(file: &'static str, line: uint) -> ! { - begin_unwind_inner(box () ("explicit failure"), file, line) +pub fn begin_unwind_no_time_to_explain(file_line: &(&'static str, uint)) -> ! { + begin_unwind_inner(box () ("explicit failure"), file_line) } /// The core of the unwinding. @@ -448,9 +448,7 @@ pub fn begin_unwind_no_time_to_explain(file: &'static str, line: uint) -> ! { /// Do this split took the LLVM IR line counts of `fn main() { fail!() /// }` from ~1900/3700 (-O/no opts) to 180/590. #[inline(never)] #[cold] // this is the slow path, please never inline this -fn begin_unwind_inner(msg: Box, - file: &'static str, - line: uint) -> ! { +fn begin_unwind_inner(msg: Box, file_line: &(&'static str, uint)) -> ! { // First, invoke call the user-defined callbacks triggered on task failure. // // By the time that we see a callback has been registered (by reading @@ -467,6 +465,7 @@ fn begin_unwind_inner(msg: Box, 0 => {} n => { let f: Callback = unsafe { mem::transmute(n) }; + let (file, line) = *file_line; f(msg, file, line); } } diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 3c6c860f516bb..fd0c72ce313ee 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -39,7 +39,7 @@ #[macro_export] macro_rules! fail( () => ( - ::std::rt::begin_unwind_no_time_to_explain(file!(), line!()) + ::std::rt::begin_unwind_no_time_to_explain(&(file!(), line!())) ); ($msg:expr) => ( ::std::rt::begin_unwind($msg, file!(), line!()) @@ -58,7 +58,7 @@ macro_rules! fail( // up with the number of calls to fail!() #[inline(always)] fn run_fmt(fmt: &::std::fmt::Arguments) -> ! { - ::std::rt::begin_unwind_fmt(fmt, file!(), line!()) + ::std::rt::begin_unwind_fmt(fmt, &(file!(), line!())) } format_args!(run_fmt, $fmt, $($arg)*) }); From f7ab07c7806bb4a7418f228c1cf266cdf011a878 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 25 Jul 2014 00:38:37 -0700 Subject: [PATCH 2/6] Put the struct passed to unwinding functions into a static Produces very clean asm, but makes bigger binaries. --- src/libcore/macros.rs | 3 ++- src/libstd/macros.rs | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index e2709c4edde47..26bb69943f8a5 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -33,7 +33,8 @@ macro_rules! fail( // up with the number of calls to fail!() #[inline(always)] fn run_fmt(fmt: &::std::fmt::Arguments) -> ! { - ::core::failure::begin_unwind(fmt, &(file!(), line!())) + static file_line: (&'static str, uint) = (file!(), line!()); + ::core::failure::begin_unwind(fmt, &file_line) } format_args!(run_fmt, $fmt, $($arg)*) }); diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index fd0c72ce313ee..9ce142836cf60 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -38,12 +38,16 @@ /// ``` #[macro_export] macro_rules! fail( - () => ( - ::std::rt::begin_unwind_no_time_to_explain(&(file!(), line!())) - ); - ($msg:expr) => ( - ::std::rt::begin_unwind($msg, file!(), line!()) - ); + () => ({ + // static requires less code at runtime, more constant data + static file_line: (&'static str, uint) = (file!(), line!()); + ::std::rt::begin_unwind_no_time_to_explain(&file_line) + }); + ($msg:expr) => ({ + static file_line: (&'static str, uint) = (file!(), line!()); + let (file, line) = file_line; + ::std::rt::begin_unwind($msg, file, line) + }); ($fmt:expr, $($arg:tt)*) => ({ // a closure can't have return type !, so we need a full // function to pass to format_args!, *and* we need the @@ -58,7 +62,8 @@ macro_rules! fail( // up with the number of calls to fail!() #[inline(always)] fn run_fmt(fmt: &::std::fmt::Arguments) -> ! { - ::std::rt::begin_unwind_fmt(fmt, &(file!(), line!())) + static file_line: (&'static str, uint) = (file!(), line!()); + ::std::rt::begin_unwind_fmt(fmt, &file_line) } format_args!(run_fmt, $fmt, $($arg)*) }); From a43e7d5cb9b5725dd32376eeea41b4d23ab93160 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 25 Jul 2014 15:54:10 -0700 Subject: [PATCH 3/6] core: Remove unneeded cfgs --- src/libcore/failure.rs | 37 ------------------------------------- 1 file changed, 37 deletions(-) diff --git a/src/libcore/failure.rs b/src/libcore/failure.rs index 022258911f1e0..5965603770ba7 100644 --- a/src/libcore/failure.rs +++ b/src/libcore/failure.rs @@ -33,7 +33,6 @@ use fmt; use intrinsics; -#[cfg(stage0)] #[cold] #[inline(never)] // this is the slow path, always #[lang="fail_"] fn fail_(expr: &'static str, file: &'static str, line: uint) -> ! { @@ -44,7 +43,6 @@ fn fail_(expr: &'static str, file: &'static str, line: uint) -> ! { unsafe { intrinsics::abort() } } -#[cfg(stage0)] #[cold] #[lang="fail_bounds_check"] fn fail_bounds_check(file: &'static str, line: uint, @@ -55,7 +53,6 @@ fn fail_bounds_check(file: &'static str, line: uint, unsafe { intrinsics::abort() } } -#[cfg(stage0)] #[cold] pub fn begin_unwind(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! { #[allow(ctypes)] @@ -68,37 +65,3 @@ pub fn begin_unwind(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! unsafe { begin_unwind(fmt, file, line) } } -#[cfg(not(stage0))] -#[cold] #[inline(never)] // this is the slow path, always -#[lang="fail_"] -fn fail_(expr: &'static str, file: &'static str, line: uint) -> ! { - format_args!(|args| -> () { - begin_unwind(args, &(file, line)); - }, "{}", expr); - - unsafe { intrinsics::abort() } -} - -#[cfg(not(stage0))] -#[cold] -#[lang="fail_bounds_check"] -fn fail_bounds_check(file: &'static str, line: uint, - index: uint, len: uint) -> ! { - format_args!(|args| -> () { - begin_unwind(args, &(file, line)); - }, "index out of bounds: the len is {} but the index is {}", len, index); - unsafe { intrinsics::abort() } -} - -#[cfg(not(stage0))] -#[cold] -pub fn begin_unwind(fmt: &fmt::Arguments, file_line: &(&'static str, uint)) -> ! { - #[allow(ctypes)] - extern { - #[lang = "begin_unwind"] - fn begin_unwind(fmt: &fmt::Arguments, file_line: &'static str, - line: uint) -> !; - } - let (file, line) = *file_line; - unsafe { begin_unwind(fmt, file, line) } -} From cf7a89f0c0935412f24d64c0ae2e202970124ff9 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 25 Jul 2014 15:54:56 -0700 Subject: [PATCH 4/6] std: Use correct conventions for statics in macros --- src/libstd/macros.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 9ce142836cf60..f3f47e3923d95 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -40,12 +40,12 @@ macro_rules! fail( () => ({ // static requires less code at runtime, more constant data - static file_line: (&'static str, uint) = (file!(), line!()); - ::std::rt::begin_unwind_no_time_to_explain(&file_line) + static FILE_LINE: (&'static str, uint) = (file!(), line!()); + ::std::rt::begin_unwind_no_time_to_explain(&FILE_LINE) }); ($msg:expr) => ({ - static file_line: (&'static str, uint) = (file!(), line!()); - let (file, line) = file_line; + static FILE_LINE: (&'static str, uint) = (file!(), line!()); + let (file, line) = FILE_LINE; ::std::rt::begin_unwind($msg, file, line) }); ($fmt:expr, $($arg:tt)*) => ({ @@ -62,8 +62,8 @@ macro_rules! fail( // up with the number of calls to fail!() #[inline(always)] fn run_fmt(fmt: &::std::fmt::Arguments) -> ! { - static file_line: (&'static str, uint) = (file!(), line!()); - ::std::rt::begin_unwind_fmt(fmt, &file_line) + static FILE_LINE: (&'static str, uint) = (file!(), line!()); + ::std::rt::begin_unwind_fmt(fmt, &FILE_LINE) } format_args!(run_fmt, $fmt, $($arg)*) }); From 53f0eae3866f5b4708e9f34b009df843f91136d7 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 25 Jul 2014 15:57:15 -0700 Subject: [PATCH 5/6] Revert "Use fewer instructions for `fail!`" This reverts commit c61f9763e2e03afbe62445877ceb3ed15e22e123. Conflicts: src/librustrt/unwind.rs src/libstd/macros.rs --- src/librustrt/lib.rs | 2 +- src/librustrt/unwind.rs | 6 ------ src/libstd/macros.rs | 5 ++--- src/libstd/rt/mod.rs | 3 +-- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/librustrt/lib.rs b/src/librustrt/lib.rs index e2d7f91d1a6f9..b7b5e09a5562a 100644 --- a/src/librustrt/lib.rs +++ b/src/librustrt/lib.rs @@ -33,7 +33,7 @@ extern crate collections; #[cfg(test)] #[phase(plugin, link)] extern crate std; pub use self::util::{Stdio, Stdout, Stderr}; -pub use self::unwind::{begin_unwind, begin_unwind_fmt, begin_unwind_no_time_to_explain}; +pub use self::unwind::{begin_unwind, begin_unwind_fmt}; use core::prelude::*; diff --git a/src/librustrt/unwind.rs b/src/librustrt/unwind.rs index db2cae2371821..5dfeb15afb84a 100644 --- a/src/librustrt/unwind.rs +++ b/src/librustrt/unwind.rs @@ -432,12 +432,6 @@ pub fn begin_unwind(msg: M, file: &'static str, line: uint) -> ! begin_unwind_inner(box msg, &(file, line)) } -/// Unwinding for `fail!()`. Saves passing a string. -#[inline(never)] #[cold] #[experimental] -pub fn begin_unwind_no_time_to_explain(file_line: &(&'static str, uint)) -> ! { - begin_unwind_inner(box () ("explicit failure"), file_line) -} - /// The core of the unwinding. /// /// This is non-generic to avoid instantiation bloat in other crates diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index f3f47e3923d95..f0732c7d508e8 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -39,11 +39,10 @@ #[macro_export] macro_rules! fail( () => ({ - // static requires less code at runtime, more constant data - static FILE_LINE: (&'static str, uint) = (file!(), line!()); - ::std::rt::begin_unwind_no_time_to_explain(&FILE_LINE) + fail!("explicit failure") }); ($msg:expr) => ({ + // static requires less code at runtime, more constant data static FILE_LINE: (&'static str, uint) = (file!(), line!()); let (file, line) = FILE_LINE; ::std::rt::begin_unwind($msg, file, line) diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs index 023a30de027a9..4490977bde619 100644 --- a/src/libstd/rt/mod.rs +++ b/src/libstd/rt/mod.rs @@ -66,8 +66,7 @@ pub use self::util::{default_sched_threads, min_stack, running_on_valgrind}; // standard library which work together to create the entire runtime. pub use alloc::{heap, libc_heap}; pub use rustrt::{task, local, mutex, exclusive, stack, args, rtio, thread}; -pub use rustrt::{Stdio, Stdout, Stderr}; -pub use rustrt::{begin_unwind, begin_unwind_fmt, begin_unwind_no_time_to_explain}; +pub use rustrt::{Stdio, Stdout, Stderr, begin_unwind, begin_unwind_fmt}; pub use rustrt::{bookkeeping, at_exit, unwind, DEFAULT_ERROR_CODE, Runtime}; // Simple backtrace functionality (to print on failure) From f49f1575aa83a20e6cf9cb79b36d0bf02f2b28d5 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 28 Jul 2014 13:40:55 -0700 Subject: [PATCH 6/6] Use correct conventions for static --- src/libcore/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 26bb69943f8a5..5373008612bb3 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -33,8 +33,8 @@ macro_rules! fail( // up with the number of calls to fail!() #[inline(always)] fn run_fmt(fmt: &::std::fmt::Arguments) -> ! { - static file_line: (&'static str, uint) = (file!(), line!()); - ::core::failure::begin_unwind(fmt, &file_line) + static FILE_LINE: (&'static str, uint) = (file!(), line!()); + ::core::failure::begin_unwind(fmt, &FILE_LINE) } format_args!(run_fmt, $fmt, $($arg)*) });