2014-10-14 20:41:50 +02:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-03-21 18:05:05 -07:00
|
|
|
#![allow(non_camel_case_types)]
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2014-11-14 14:20:57 -08:00
|
|
|
use std::cell::{RefCell, Cell};
|
2014-08-27 21:46:52 -04:00
|
|
|
use std::collections::HashMap;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::collections::hash_state::HashState;
|
|
|
|
use std::ffi::CString;
|
2015-01-20 15:45:07 -08:00
|
|
|
use std::fmt::Debug;
|
2015-02-17 20:48:07 -08:00
|
|
|
use std::hash::Hash;
|
2014-12-10 19:46:38 -08:00
|
|
|
use std::iter::repeat;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::path::Path;
|
2014-11-10 12:27:56 -08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
use rustc_front::hir;
|
|
|
|
use rustc_front::visit;
|
|
|
|
use rustc_front::visit::Visitor;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-01-10 11:54:15 -05:00
|
|
|
// The name of the associated type for `Fn` return types
|
|
|
|
pub const FN_OUTPUT_NAME: &'static str = "Output";
|
|
|
|
|
2014-11-07 16:26:26 -05:00
|
|
|
// Useful type to use with `Result<>` indicate that an error has already
|
|
|
|
// been reported to the user, so no need to continue checking.
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2014-11-08 06:59:10 -05:00
|
|
|
pub struct ErrorReported;
|
|
|
|
|
2015-06-25 10:07:01 -07:00
|
|
|
pub fn time<T, F>(do_it: bool, what: &str, f: F) -> T where
|
|
|
|
F: FnOnce() -> T,
|
2014-12-08 20:26:43 -05:00
|
|
|
{
|
2015-03-25 17:06:52 -07:00
|
|
|
thread_local!(static DEPTH: Cell<usize> = Cell::new(0));
|
2015-06-25 10:07:01 -07:00
|
|
|
if !do_it { return f(); }
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2014-11-14 14:20:57 -08:00
|
|
|
let old = DEPTH.with(|slot| {
|
|
|
|
let r = slot.get();
|
|
|
|
slot.set(r + 1);
|
|
|
|
r
|
|
|
|
});
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2014-11-10 12:27:56 -08:00
|
|
|
let mut rv = None;
|
2014-12-08 20:26:43 -05:00
|
|
|
let dur = {
|
|
|
|
let ref mut rvp = rv;
|
|
|
|
|
|
|
|
Duration::span(move || {
|
2015-06-25 10:07:01 -07:00
|
|
|
*rvp = Some(f())
|
2014-12-08 20:26:43 -05:00
|
|
|
})
|
|
|
|
};
|
2014-11-10 12:27:56 -08:00
|
|
|
let rv = rv.unwrap();
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2015-05-15 10:55:23 +12:00
|
|
|
// Hack up our own formatting for the duration to make it easier for scripts
|
|
|
|
// to parse (always use the same number of decimal places and the same unit).
|
|
|
|
const NANOS_PER_SEC: f64 = 1_000_000_000.0;
|
2015-07-05 23:20:00 -07:00
|
|
|
let secs = dur.as_secs() as f64;
|
|
|
|
let secs = secs + dur.subsec_nanos() as f64 / NANOS_PER_SEC;
|
2015-06-27 18:37:33 -07:00
|
|
|
|
|
|
|
let mem_string = match get_resident() {
|
|
|
|
Some(n) => {
|
|
|
|
let mb = n as f64 / 1_000_000.0;
|
|
|
|
format!("; rss: {}MB", mb.round() as usize)
|
|
|
|
}
|
|
|
|
None => "".to_owned(),
|
|
|
|
};
|
|
|
|
println!("{}time: {:.3}{}\t{}", repeat(" ").take(old).collect::<String>(),
|
|
|
|
secs, mem_string, what);
|
2015-05-15 10:55:23 +12:00
|
|
|
|
2014-11-14 14:20:57 -08:00
|
|
|
DEPTH.with(|slot| slot.set(old));
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-02 23:19:29 -08:00
|
|
|
|
2013-03-21 18:50:02 +09:00
|
|
|
rv
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
// Like std::macros::try!, but for Option<>.
|
|
|
|
macro_rules! option_try(
|
|
|
|
($e:expr) => (match $e { Some(e) => e, None => return None })
|
|
|
|
);
|
|
|
|
|
2015-06-27 18:37:33 -07:00
|
|
|
// Memory reporting
|
2015-07-08 08:29:41 -04:00
|
|
|
#[cfg(unix)]
|
2015-06-27 18:37:33 -07:00
|
|
|
fn get_resident() -> Option<usize> {
|
2015-08-11 17:27:05 -07:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2015-07-08 08:29:41 -04:00
|
|
|
|
2015-08-11 17:27:05 -07:00
|
|
|
let field = 1;
|
|
|
|
let mut f = option_try!(File::open("/proc/self/statm").ok());
|
|
|
|
let mut contents = String::new();
|
|
|
|
option_try!(f.read_to_string(&mut contents).ok());
|
|
|
|
let s = option_try!(contents.split_whitespace().nth(field));
|
|
|
|
let npages = option_try!(s.parse::<usize>().ok());
|
|
|
|
Some(npages * 4096)
|
2015-06-27 18:37:33 -07:00
|
|
|
}
|
|
|
|
|
2015-07-08 08:29:41 -04:00
|
|
|
#[cfg(windows)]
|
2015-08-11 17:27:05 -07:00
|
|
|
fn get_resident() -> Option<usize> {
|
2015-07-08 08:29:41 -04:00
|
|
|
use libc::{BOOL, DWORD, HANDLE, SIZE_T, GetCurrentProcess};
|
|
|
|
use std::mem;
|
|
|
|
#[repr(C)] #[allow(non_snake_case)]
|
|
|
|
struct PROCESS_MEMORY_COUNTERS {
|
|
|
|
cb: DWORD,
|
|
|
|
PageFaultCount: DWORD,
|
|
|
|
PeakWorkingSetSize: SIZE_T,
|
|
|
|
WorkingSetSize: SIZE_T,
|
|
|
|
QuotaPeakPagedPoolUsage: SIZE_T,
|
|
|
|
QuotaPagedPoolUsage: SIZE_T,
|
|
|
|
QuotaPeakNonPagedPoolUsage: SIZE_T,
|
|
|
|
QuotaNonPagedPoolUsage: SIZE_T,
|
|
|
|
PagefileUsage: SIZE_T,
|
|
|
|
PeakPagefileUsage: SIZE_T,
|
|
|
|
}
|
|
|
|
type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
|
|
|
|
#[link(name = "psapi")]
|
|
|
|
extern "system" {
|
|
|
|
fn GetProcessMemoryInfo(Process: HANDLE,
|
|
|
|
ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
|
|
|
|
cb: DWORD) -> BOOL;
|
|
|
|
}
|
|
|
|
let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { mem::zeroed() };
|
|
|
|
pmc.cb = mem::size_of_val(&pmc) as DWORD;
|
|
|
|
match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
|
|
|
|
0 => None,
|
|
|
|
_ => Some(pmc.WorkingSetSize as usize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 20:26:43 -05:00
|
|
|
pub fn indent<R, F>(op: F) -> R where
|
2015-01-20 15:45:07 -08:00
|
|
|
R: Debug,
|
2014-12-08 20:26:43 -05:00
|
|
|
F: FnOnce() -> R,
|
|
|
|
{
|
2012-04-05 20:59:07 -07:00
|
|
|
// Use in conjunction with the log post-processor like `src/etc/indenter`
|
|
|
|
// to make debug output more readable.
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2012-10-23 11:28:20 -07:00
|
|
|
let r = op();
|
2014-12-20 00:09:35 -08:00
|
|
|
debug!("<< (Result = {:?})", r);
|
2013-02-15 04:14:34 -05:00
|
|
|
r
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub struct Indenter {
|
|
|
|
_cannot_construct_outside_of_this_module: ()
|
2013-02-27 19:13:53 -05:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
impl Drop for Indenter {
|
2013-10-21 13:08:31 -07:00
|
|
|
fn drop(&mut self) { debug!("<<"); }
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:51:42 +02:00
|
|
|
pub fn indenter() -> Indenter {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>");
|
2014-06-06 15:51:42 +02:00
|
|
|
Indenter { _cannot_construct_outside_of_this_module: () }
|
2012-04-05 20:59:07 -07:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
struct LoopQueryVisitor<P> where P: FnMut(&hir::Expr_) -> bool {
|
2014-12-08 20:26:43 -05:00
|
|
|
p: P,
|
2013-09-25 10:55:04 +02:00
|
|
|
flag: bool,
|
2013-08-14 15:54:35 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
impl<'v, P> Visitor<'v> for LoopQueryVisitor<P> where P: FnMut(&hir::Expr_) -> bool {
|
|
|
|
fn visit_expr(&mut self, e: &hir::Expr) {
|
2013-09-25 10:55:04 +02:00
|
|
|
self.flag |= (self.p)(&e.node);
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2012-06-20 18:50:44 -07:00
|
|
|
// Skip inner loops, since a break in the inner loop isn't a
|
|
|
|
// break inside the outer loop
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ExprLoop(..) | hir::ExprWhile(..) => {}
|
2014-09-12 13:10:30 +03:00
|
|
|
_ => visit::walk_expr(self, e)
|
2012-06-20 18:50:44 -07:00
|
|
|
}
|
2013-08-14 15:54:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a predicate p, returns true iff p is true for any subexpressions
|
|
|
|
// of b -- skipping any inner loops (loop, while, loop_body)
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn loop_query<P>(b: &hir::Block, p: P) -> bool where P: FnMut(&hir::Expr_) -> bool {
|
2013-08-29 18:04:17 -07:00
|
|
|
let mut v = LoopQueryVisitor {
|
|
|
|
p: p,
|
2013-09-25 10:55:04 +02:00
|
|
|
flag: false,
|
2013-08-29 18:04:17 -07:00
|
|
|
};
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_block(&mut v, b);
|
2013-09-25 10:55:04 +02:00
|
|
|
return v.flag;
|
2012-06-14 12:24:56 -07:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
struct BlockQueryVisitor<P> where P: FnMut(&hir::Expr) -> bool {
|
2014-12-08 20:26:43 -05:00
|
|
|
p: P,
|
2013-09-25 10:55:04 +02:00
|
|
|
flag: bool,
|
2013-08-14 15:54:35 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
impl<'v, P> Visitor<'v> for BlockQueryVisitor<P> where P: FnMut(&hir::Expr) -> bool {
|
|
|
|
fn visit_expr(&mut self, e: &hir::Expr) {
|
2013-09-25 10:55:04 +02:00
|
|
|
self.flag |= (self.p)(e);
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_expr(self, e)
|
2013-08-14 15:54:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 12:20:18 -07:00
|
|
|
// Takes a predicate p, returns true iff p is true for any subexpressions
|
|
|
|
// of b -- skipping any inner loops (loop, while, loop_body)
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn block_query<P>(b: &hir::Block, p: P) -> bool where P: FnMut(&hir::Expr) -> bool {
|
2013-08-29 18:04:17 -07:00
|
|
|
let mut v = BlockQueryVisitor {
|
|
|
|
p: p,
|
2013-09-25 10:55:04 +02:00
|
|
|
flag: false,
|
2013-08-29 18:04:17 -07:00
|
|
|
};
|
2014-09-12 13:10:30 +03:00
|
|
|
visit::walk_block(&mut v, &*b);
|
2013-09-25 10:55:04 +02:00
|
|
|
return v.flag;
|
2012-03-10 20:34:57 -08:00
|
|
|
}
|
2014-08-27 21:46:52 -04:00
|
|
|
|
2014-10-14 20:41:50 +02:00
|
|
|
/// Memoizes a one-argument closure using the given RefCell containing
|
|
|
|
/// a type implementing MutableMap to serve as a cache.
|
|
|
|
///
|
|
|
|
/// In the future the signature of this function is expected to be:
|
|
|
|
/// ```
|
|
|
|
/// pub fn memoized<T: Clone, U: Clone, M: MutableMap<T, U>>(
|
|
|
|
/// cache: &RefCell<M>,
|
2015-02-01 12:44:15 -05:00
|
|
|
/// f: &|T| -> U
|
|
|
|
/// ) -> impl |T| -> U {
|
2014-10-14 20:41:50 +02:00
|
|
|
/// ```
|
|
|
|
/// but currently it is not possible.
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2014-10-14 20:41:50 +02:00
|
|
|
/// ```
|
|
|
|
/// struct Context {
|
2015-03-25 17:06:52 -07:00
|
|
|
/// cache: RefCell<HashMap<usize, usize>>
|
2014-10-14 20:41:50 +02:00
|
|
|
/// }
|
|
|
|
///
|
2015-03-25 17:06:52 -07:00
|
|
|
/// fn factorial(ctxt: &Context, n: usize) -> usize {
|
2014-10-14 20:41:50 +02:00
|
|
|
/// memoized(&ctxt.cache, n, |n| match n {
|
|
|
|
/// 0 | 1 => n,
|
|
|
|
/// _ => factorial(ctxt, n - 2) + factorial(ctxt, n - 1)
|
|
|
|
/// })
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
2015-02-17 20:48:07 -08:00
|
|
|
pub fn memoized<T, U, S, F>(cache: &RefCell<HashMap<T, U, S>>, arg: T, f: F) -> U
|
|
|
|
where T: Clone + Hash + Eq,
|
|
|
|
U: Clone,
|
|
|
|
S: HashState,
|
|
|
|
F: FnOnce(T) -> U,
|
|
|
|
{
|
|
|
|
let key = arg.clone();
|
2015-02-22 17:13:41 +01:00
|
|
|
let result = cache.borrow().get(&key).cloned();
|
2015-02-17 20:48:07 -08:00
|
|
|
match result {
|
|
|
|
Some(result) => result,
|
|
|
|
None => {
|
|
|
|
let result = f(arg);
|
|
|
|
cache.borrow_mut().insert(key, result.clone());
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 21:00:43 -08:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn path2cstr(p: &Path) -> CString {
|
|
|
|
use std::os::unix::prelude::*;
|
2015-03-30 11:00:05 -07:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
let p: &OsStr = p.as_ref();
|
|
|
|
CString::new(p.as_bytes()).unwrap()
|
2015-02-26 21:00:43 -08:00
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn path2cstr(p: &Path) -> CString {
|
|
|
|
CString::new(p.to_str().unwrap()).unwrap()
|
|
|
|
}
|