From a9cb094879fabd23774a0d9dcfed7f3eeb9a4aff Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Sat, 13 May 2017 21:42:51 +0200 Subject: [PATCH 01/10] Explain why `thread::yield_now` could be used. Part of #29378. --- src/libstd/thread/mod.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 200368be275c3..8f1a88ed30515 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -468,6 +468,23 @@ pub fn current() -> Thread { /// Cooperatively gives up a timeslice to the OS scheduler. /// +/// This is used when the programmer knows that the thread will have nothing +/// to do for some time, and thus avoid wasting computing time. +/// +/// For example when polling on a resource, it is common to check that it is +/// available, and if not to yield in order to avoid busy waiting. +/// +/// Thus the pattern of `yield`ing after a failed poll is rather common when +/// implementing low-level shared resources or synchronization primitives. +/// +/// However programmers will usualy prefer to use, [`channel`]s, [`Condvar`]s, +/// [`Mutex`]es or [`join`] for their synchronisation routines, as they avoid +/// thinking about thread schedulling. +/// +/// Note that [`channel`]s for example are implemented using this primitive. +/// Indeed when you call `send` or `recv`, which are blocking, they will yield +/// if the channel is not available. +/// /// # Examples /// /// ``` @@ -475,6 +492,12 @@ pub fn current() -> Thread { /// /// thread::yield_now(); /// ``` +/// +/// [`channel`]: ../../std/sync/mpsc/index.html +/// [`spawn`]: ../../std/thread/fn.spawn.html +/// [`join`]: ../../std/thread/struct.JoinHandle.html#method.join +/// [`Mutex`]: ../../std/sync/struct.Mutex.html +/// [`Condvar`]: ../../std/sync/struct.Condvar.html #[stable(feature = "rust1", since = "1.0.0")] pub fn yield_now() { imp::Thread::yield_now() From a51777ec874e31b29d85efaa186fe4841257b168 Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Sun, 14 May 2017 19:35:36 +0200 Subject: [PATCH 02/10] Improve `thread::Builder` documentation. Part of #29378 . - In particular explains *why* we would use the builder instead of the free function. - Changes the parent-child explanation for a spawned-caller. - Add link to `io::Result` in `thread::Builder` - Corrects the `thread::Builder::spawn` documentation. --- src/libstd/thread/mod.rs | 49 +++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/libstd/thread/mod.rs b/src/libstd/thread/mod.rs index 200368be275c3..1f079c21f5776 100644 --- a/src/libstd/thread/mod.rs +++ b/src/libstd/thread/mod.rs @@ -180,8 +180,33 @@ pub use self::local::{LocalKey, LocalKeyState}; // Builder //////////////////////////////////////////////////////////////////////////////// -/// Thread configuration. Provides detailed control over the properties -/// and behavior of new threads. +/// Thread factory, which can be used in order to configure the properties of +/// a new thread. +/// +/// Methods can be chained on it in order to configure it. +/// +/// The two configurations available are: +/// +/// - [`name`]: allows to give a name to the thread which is currently +/// only used in `panic` messages. +/// - [`stack_size`]: specifies the desired stack size. Note that this can +/// be overriden by the OS. +/// +/// If the [`stack_size`] field is not specified, the stack size +/// will be the `RUST_MIN_STACK` environment variable. If it is +/// not specified either, a sensible default will be set. +/// +/// If the [`name`] field is not specified, the thread will not be named. +/// +/// The [`spawn`] method will take ownership of the builder and create an +/// [`io::Result`] to the thread handle with the given configuration. +/// +/// The [`thread::spawn`] free function uses a `Builder` with default +/// configuration and [`unwrap`]s its return value. +/// +/// You may want to use [`spawn`] instead of [`thread::spawn`], when you want +/// to recover from a failure to launch a thread, indeed the free function will +/// panick where the `Builder` method will return a [`io::Result`]. /// /// # Examples /// @@ -196,6 +221,13 @@ pub use self::local::{LocalKey, LocalKeyState}; /// /// handler.join().unwrap(); /// ``` +/// +/// [`thread::spawn`]: ../../std/thread/fn.spawn.html +/// [`stack_size`]: ../../std/thread/struct.Builder.html#method.stack_size +/// [`name`]: ../../std/thread/struct.Builder.html#method.name +/// [`spawn`]: ../../std/thread/struct.Builder.html#method.spawn +/// [`io::Result`]: ../../std/io/type.Result.html +/// [`unwrap`]: ../../std/result/enum.Result.html#method.unwrap #[stable(feature = "rust1", since = "1.0.0")] #[derive(Debug)] pub struct Builder { @@ -209,11 +241,6 @@ impl Builder { /// Generates the base configuration for spawning a thread, from which /// configuration methods can be chained. /// - /// If the [`stack_size`] field is not specified, the stack size - /// will be the `RUST_MIN_STACK` environment variable. If it is - /// not specified either, a sensible default will be set (2MB as - /// of the writting of this doc). - /// /// # Examples /// /// ``` @@ -229,8 +256,6 @@ impl Builder { /// /// handler.join().unwrap(); /// ``` - /// - /// [`stack_size`]: ../../std/thread/struct.Builder.html#method.stack_size #[stable(feature = "rust1", since = "1.0.0")] pub fn new() -> Builder { Builder { @@ -280,9 +305,10 @@ impl Builder { self } - /// Spawns a new thread, and returns a join handle for it. + /// Spawns a new thread by taking ownership of the `Builder`, and returns an + /// [`io::Result`] to its [`JoinHandle`]. /// - /// The child thread may outlive the parent (unless the parent thread + /// The spawned thread may outlive the caller (unless the caller thread /// is the main thread; the whole process is terminated when the main /// thread finishes). The join handle can be used to block on /// termination of the child thread, including recovering its panics. @@ -297,6 +323,7 @@ impl Builder { /// /// [`spawn`]: ../../std/thread/fn.spawn.html /// [`io::Result`]: ../../std/io/type.Result.html + /// [`JoinHandle`]: ../../std/thread/struct.JoinHandle.html /// /// # Examples /// From 1a24a591dd43c53de7e3f16eff9f67469cf7a52b Mon Sep 17 00:00:00 2001 From: Robin Kruppe Date: Wed, 26 Apr 2017 23:22:45 +0200 Subject: [PATCH 03/10] Remove rustc_llvm dependency from rustc_metadata Move the code for loading metadata from rlibs and dylibs from rustc_metadata into rustc_trans, and introduce a trait to avoid introducing a direct dependency on rustc_trans. This means rustc_metadata is no longer rebuilt when LLVM changes. --- src/Cargo.lock | 19 ++- src/librustc/Cargo.toml | 1 + src/librustc/lib.rs | 1 + src/librustc/middle/cstore.rs | 32 +++- src/librustc_driver/lib.rs | 10 +- src/librustc_driver/test.rs | 3 +- src/librustc_metadata/Cargo.toml | 2 +- src/librustc_metadata/creader.rs | 2 + src/librustc_metadata/cstore.rs | 15 +- src/librustc_metadata/cstore_impl.rs | 21 +-- src/librustc_metadata/decoder.rs | 6 +- src/librustc_metadata/lib.rs | 2 +- src/librustc_metadata/locator.rs | 225 ++++++++------------------- src/librustc_trans/Cargo.toml | 1 + src/librustc_trans/back/archive.rs | 5 +- src/librustc_trans/back/link.rs | 9 +- src/librustc_trans/base.rs | 4 +- src/librustc_trans/lib.rs | 4 + src/librustc_trans/metadata.rs | 122 +++++++++++++++ src/librustdoc/core.rs | 3 +- src/librustdoc/test.rs | 5 +- src/test/run-make/issue-19371/foo.rs | 3 +- 22 files changed, 276 insertions(+), 219 deletions(-) create mode 100644 src/librustc_trans/metadata.rs diff --git a/src/Cargo.lock b/src/Cargo.lock index 776a268aa8de1..bd9fda42570fd 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -316,6 +316,14 @@ name = "open" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "owning_ref" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "panic_abort" version = "0.0.0" @@ -442,6 +450,7 @@ dependencies = [ "fmt_macros 0.0.0", "graphviz 0.0.0", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_back 0.0.0", "rustc_bitflags 0.0.0", "rustc_const_math 0.0.0", @@ -626,13 +635,13 @@ version = "0.0.0" dependencies = [ "flate 0.0.0", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "proc_macro 0.0.0", "rustc 0.0.0", "rustc_back 0.0.0", "rustc_const_math 0.0.0", "rustc_data_structures 0.0.0", "rustc_errors 0.0.0", - "rustc_llvm 0.0.0", "serialize 0.0.0", "syntax 0.0.0", "syntax_ext 0.0.0", @@ -734,6 +743,7 @@ version = "0.0.0" dependencies = [ "flate 0.0.0", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "rustc 0.0.0", "rustc_back 0.0.0", "rustc_bitflags 0.0.0", @@ -820,6 +830,11 @@ dependencies = [ name = "serialize" version = "0.0.0" +[[package]] +name = "stable_deref_trait" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "std" version = "0.0.0" @@ -1015,6 +1030,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "e1cbfa3781f3fe73dc05321bed52a06d2d491eaa764c52335cf4399f046ece99" "checksum num_cpus 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca313f1862c7ec3e0dfe8ace9fa91b1d9cb5c84ace3d00f5ec4216238e93c167" "checksum open 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3478ed1686bd1300c8a981a940abc92b06fac9cbef747f4c668d4e032ff7b842" +"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" "checksum pest 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0a6dda33d67c26f0aac90d324ab2eb7239c819fc7b2552fe9faa4fe88441edc8" "checksum pulldown-cmark 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ab1e588ef8efd702c7ed9d2bd774db5e6f4d878bb5a1a9f371828fbdff6973" "checksum pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1058d7bb927ca067656537eec4e02c2b4b70eaaa129664c5b90c111e20326f41" @@ -1026,6 +1042,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-serialize 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "684ce48436d6465300c9ea783b6b14c4361d6b8dcbb1375b486a69cc19e2dfb0" "checksum serde 0.9.11 (registry+https://github.com/rust-lang/crates.io-index)" = "a702319c807c016e51f672e5c77d6f0b46afddd744b5e437d6b8436b888b458f" "checksum serde_json 0.9.9 (registry+https://github.com/rust-lang/crates.io-index)" = "dbc45439552eb8fb86907a2c41c1fd0ef97458efb87ff7f878db466eb581824e" +"checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b" "checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694" "checksum term_size 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "07b6c1ac5b3fffd75073276bca1ceed01f67a28537097a2a9539e116e50fb21a" "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" diff --git a/src/librustc/Cargo.toml b/src/librustc/Cargo.toml index fa217acd9f9bf..3c762e43e9aa6 100644 --- a/src/librustc/Cargo.toml +++ b/src/librustc/Cargo.toml @@ -13,6 +13,7 @@ arena = { path = "../libarena" } fmt_macros = { path = "../libfmt_macros" } graphviz = { path = "../libgraphviz" } log = "0.3" +owning_ref = "0.3.3" rustc_back = { path = "../librustc_back" } rustc_bitflags = { path = "../librustc_bitflags" } rustc_const_math = { path = "../librustc_const_math" } diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 5cf26ea8bfca5..c979677ff717c 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -54,6 +54,7 @@ extern crate fmt_macros; extern crate getopts; extern crate graphviz; extern crate libc; +extern crate owning_ref; extern crate rustc_llvm as llvm; extern crate rustc_back; extern crate rustc_data_structures; diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 569b1aeeb09d7..a68aca4600054 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -36,8 +36,9 @@ use session::search_paths::PathKind; use util::nodemap::{NodeSet, DefIdMap}; use std::any::Any; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::rc::Rc; +use owning_ref::ErasedBoxRef; use syntax::ast; use syntax::ext::base::SyntaxExtension; use syntax::symbol::Symbol; @@ -201,11 +202,33 @@ impl EncodedMetadataHashes { } } +/// The backend's way to give the crate store access to the metadata in a library. +/// Note that it returns the raw metadata bytes stored in the library file, whether +/// it is compressed, uncompressed, some weird mix, etc. +/// rmeta files are backend independent and not handled here. +/// +/// At the time of this writing, there is only one backend and one way to store +/// metadata in library -- this trait just serves to decouple rustc_metadata from +/// the archive reader, which depends on LLVM. +pub trait MetadataLoader { + fn get_rlib_metadata(&self, + target: &Target, + filename: &Path) + -> Result, String>; + fn get_dylib_metadata(&self, + target: &Target, + filename: &Path) + -> Result, String>; +} + /// A store of Rust crates, through with their metadata /// can be accessed. pub trait CrateStore { fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc; + // access to the metadata loader + fn metadata_loader(&self) -> &MetadataLoader; + // item info fn visibility(&self, def: DefId) -> ty::Visibility; fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap>; @@ -275,8 +298,6 @@ pub trait CrateStore { fn used_link_args(&self) -> Vec; // utility functions - fn metadata_filename(&self) -> &str; - fn metadata_section_name(&self, target: &Target) -> &str; fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>; fn used_crate_source(&self, cnum: CrateNum) -> CrateSource; fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option; @@ -413,8 +434,6 @@ impl CrateStore for DummyCrateStore { fn used_link_args(&self) -> Vec { vec![] } // utility functions - fn metadata_filename(&self) -> &str { bug!("metadata_filename") } - fn metadata_section_name(&self, target: &Target) -> &str { bug!("metadata_section_name") } fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> { vec![] } fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") } @@ -427,6 +446,9 @@ impl CrateStore for DummyCrateStore { bug!("encode_metadata") } fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") } + + // access to the metadata loader + fn metadata_loader(&self) -> &MetadataLoader { bug!("metadata_loader") } } pub trait CrateLoader { diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 024fc546a158e..2dab680ad45aa 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -204,7 +204,7 @@ pub fn run_compiler<'a>(args: &[String], }; let dep_graph = DepGraph::new(sopts.build_dep_graph()); - let cstore = Rc::new(CStore::new(&dep_graph)); + let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader)); let loader = file_loader.unwrap_or(box RealFileLoader); let codemap = Rc::new(CodeMap::with_file_loader(loader, sopts.file_path_mapping())); @@ -409,7 +409,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { return None; } let dep_graph = DepGraph::new(sopts.build_dep_graph()); - let cstore = Rc::new(CStore::new(&dep_graph)); + let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader)); let mut sess = build_session(sopts.clone(), &dep_graph, None, @@ -558,7 +558,11 @@ impl RustcDefaultCalls { &Input::File(ref ifile) => { let path = &(*ifile); let mut v = Vec::new(); - locator::list_file_metadata(&sess.target.target, path, &mut v).unwrap(); + locator::list_file_metadata(&sess.target.target, + path, + sess.cstore.metadata_loader(), + &mut v) + .unwrap(); println!("{}", String::from_utf8(v).unwrap()); } &Input::Str { .. } => { diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index e2cbc480715fb..be4f64b7109c5 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -14,6 +14,7 @@ use driver; use rustc::dep_graph::DepGraph; use rustc_lint; use rustc_resolve::MakeGlobMap; +use rustc_trans; use rustc::middle::lang_items; use rustc::middle::free_region::FreeRegionMap; use rustc::middle::region::{CodeExtent, RegionMaps}; @@ -104,7 +105,7 @@ fn test_env(source_string: &str, let dep_graph = DepGraph::new(false); let _ignore = dep_graph.in_ignore(); - let cstore = Rc::new(CStore::new(&dep_graph)); + let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader)); let sess = session::build_session_(options, &dep_graph, None, diff --git a/src/librustc_metadata/Cargo.toml b/src/librustc_metadata/Cargo.toml index e8b906092730e..f47788ee036dc 100644 --- a/src/librustc_metadata/Cargo.toml +++ b/src/librustc_metadata/Cargo.toml @@ -11,13 +11,13 @@ crate-type = ["dylib"] [dependencies] flate = { path = "../libflate" } log = "0.3" +owning_ref = "0.3.3" proc_macro = { path = "../libproc_macro" } rustc = { path = "../librustc" } rustc_back = { path = "../librustc_back" } rustc_const_math = { path = "../librustc_const_math" } rustc_data_structures = { path = "../librustc_data_structures" } rustc_errors = { path = "../librustc_errors" } -rustc_llvm = { path = "../librustc_llvm" } serialize = { path = "../libserialize" } syntax = { path = "../libsyntax" } syntax_ext = { path = "../libsyntax_ext" } diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index d2874f1628901..dc7be42e452cb 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -393,6 +393,7 @@ impl<'a> CrateLoader<'a> { rejected_via_filename: vec![], should_match_name: true, is_proc_macro: Some(false), + metadata_loader: &*self.cstore.metadata_loader, }; self.load(&mut locate_ctxt).or_else(|| { @@ -554,6 +555,7 @@ impl<'a> CrateLoader<'a> { rejected_via_filename: vec![], should_match_name: true, is_proc_macro: None, + metadata_loader: &*self.cstore.metadata_loader, }; let library = self.load(&mut locate_ctxt).or_else(|| { if !is_cross { diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index c12b4209675de..d2ad6d0ab3449 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -11,21 +11,20 @@ // The crate store - a central repo for information collected about external // crates and libraries -use locator; use schema::{self, Tracked}; use rustc::dep_graph::{DepGraph, DepNode, GlobalMetaDataKind}; use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefIndex, DefId}; use rustc::hir::map::definitions::DefPathTable; use rustc::hir::svh::Svh; -use rustc::middle::cstore::{DepKind, ExternCrate}; +use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader}; use rustc_back::PanicStrategy; use rustc_data_structures::indexed_vec::IndexVec; use rustc::util::nodemap::{FxHashMap, FxHashSet, NodeMap, DefIdMap}; use std::cell::{RefCell, Cell}; use std::rc::Rc; -use flate::Bytes; +use owning_ref::ErasedBoxRef; use syntax::{ast, attr}; use syntax::ext::base::SyntaxExtension; use syntax::symbol::Symbol; @@ -43,11 +42,7 @@ pub use cstore_impl::provide; // own crate numbers. pub type CrateNumMap = IndexVec; -pub enum MetadataBlob { - Inflated(Bytes), - Archive(locator::ArchiveMetadata), - Raw(Vec), -} +pub struct MetadataBlob(pub ErasedBoxRef<[u8]>); /// Holds information about a syntax_pos::FileMap imported from another crate. /// See `imported_filemaps()` for more information. @@ -103,10 +98,11 @@ pub struct CStore { statically_included_foreign_items: RefCell>, pub dllimport_foreign_items: RefCell>, pub visible_parent_map: RefCell>, + pub metadata_loader: Box, } impl CStore { - pub fn new(dep_graph: &DepGraph) -> CStore { + pub fn new(dep_graph: &DepGraph, metadata_loader: Box) -> CStore { CStore { dep_graph: dep_graph.clone(), metas: RefCell::new(FxHashMap()), @@ -116,6 +112,7 @@ impl CStore { statically_included_foreign_items: RefCell::new(FxHashSet()), dllimport_foreign_items: RefCell::new(FxHashSet()), visible_parent_map: RefCell::new(FxHashMap()), + metadata_loader: metadata_loader, } } diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index dbf3e94832fc4..1a2298d3fb1ba 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -10,12 +10,11 @@ use cstore; use encoder; -use locator; use schema; use rustc::dep_graph::DepTrackingMapConfig; use rustc::middle::cstore::{CrateStore, CrateSource, LibSource, DepKind, - ExternCrate, NativeLibrary, LinkMeta, + ExternCrate, NativeLibrary, MetadataLoader, LinkMeta, LinkagePreference, LoadedMacro, EncodedMetadata}; use rustc::hir::def; use rustc::middle::lang_items; @@ -38,7 +37,6 @@ use syntax::parse::filemap_to_stream; use syntax::symbol::Symbol; use syntax_pos::{Span, NO_EXPANSION}; use rustc::hir::svh::Svh; -use rustc_back::target::Target; use rustc::hir; macro_rules! provide { @@ -135,6 +133,10 @@ impl CrateStore for cstore::CStore { self.get_crate_data(krate) } + fn metadata_loader(&self) -> &MetadataLoader { + &*self.metadata_loader + } + fn visibility(&self, def: DefId) -> ty::Visibility { self.dep_graph.read(DepNode::MetaData(def)); self.get_crate_data(def.krate).get_visibility(def.index) @@ -420,17 +422,6 @@ impl CrateStore for cstore::CStore { { self.get_used_link_args().borrow().clone() } - - fn metadata_filename(&self) -> &str - { - locator::METADATA_FILENAME - } - - fn metadata_section_name(&self, target: &Target) -> &str - { - locator::meta_section_name(target) - } - fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)> { self.do_get_used_crates(prefer) @@ -522,4 +513,4 @@ impl CrateStore for cstore::CStore { drop(visible_parent_map); self.visible_parent_map.borrow() } -} +} \ No newline at end of file diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 819095e262832..c734b9f411c2f 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -77,11 +77,7 @@ pub trait Metadata<'a, 'tcx>: Copy { impl<'a, 'tcx> Metadata<'a, 'tcx> for &'a MetadataBlob { fn raw_bytes(self) -> &'a [u8] { - match *self { - MetadataBlob::Inflated(ref vec) => vec, - MetadataBlob::Archive(ref ar) => ar.as_slice(), - MetadataBlob::Raw(ref vec) => vec, - } + &self.0 } } diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index 27555f49e57fc..e3d9e5ac74a06 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -37,6 +37,7 @@ extern crate syntax; extern crate syntax_pos; extern crate flate; extern crate serialize as rustc_serialize; // used by deriving +extern crate owning_ref; extern crate rustc_errors as errors; extern crate syntax_ext; extern crate proc_macro; @@ -46,7 +47,6 @@ extern crate rustc; extern crate rustc_back; extern crate rustc_const_math; extern crate rustc_data_structures; -extern crate rustc_llvm; mod diagnostics; diff --git a/src/librustc_metadata/locator.rs b/src/librustc_metadata/locator.rs index 84bb82de370e4..34b07af9f01f4 100644 --- a/src/librustc_metadata/locator.rs +++ b/src/librustc_metadata/locator.rs @@ -224,15 +224,12 @@ use creader::Library; use schema::{METADATA_HEADER, rustc_version}; use rustc::hir::svh::Svh; +use rustc::middle::cstore::MetadataLoader; use rustc::session::{config, Session}; use rustc::session::filesearch::{FileSearch, FileMatches, FileDoesntMatch}; use rustc::session::search_paths::PathKind; -use rustc::util::common; use rustc::util::nodemap::FxHashMap; -use rustc_llvm as llvm; -use rustc_llvm::{False, ObjectFile, mk_section_iter}; -use rustc_llvm::archive_ro::ArchiveRO; use errors::DiagnosticBuilder; use syntax::symbol::Symbol; use syntax_pos::Span; @@ -243,11 +240,10 @@ use std::fmt; use std::fs::{self, File}; use std::io::{self, Read}; use std::path::{Path, PathBuf}; -use std::ptr; -use std::slice; use std::time::Instant; use flate; +use owning_ref::{ErasedBoxRef, OwningRef}; pub struct CrateMismatch { path: PathBuf, @@ -272,12 +268,7 @@ pub struct Context<'a> { pub rejected_via_filename: Vec, pub should_match_name: bool, pub is_proc_macro: Option, -} - -pub struct ArchiveMetadata { - _archive: ArchiveRO, - // points into self._archive - data: *const [u8], + pub metadata_loader: &'a MetadataLoader, } pub struct CratePaths { @@ -287,8 +278,6 @@ pub struct CratePaths { pub rmeta: Option, } -pub const METADATA_FILENAME: &'static str = "rust.metadata.bin"; - #[derive(Copy, Clone, PartialEq)] enum CrateFlavor { Rlib, @@ -596,20 +585,21 @@ impl<'a> Context<'a> { let mut err: Option = None; for (lib, kind) in m { info!("{} reading metadata from: {}", flavor, lib.display()); - let (hash, metadata) = match get_metadata_section(self.target, flavor, &lib) { - Ok(blob) => { - if let Some(h) = self.crate_matches(&blob, &lib) { - (h, blob) - } else { - info!("metadata mismatch"); + let (hash, metadata) = + match get_metadata_section(self.target, flavor, &lib, self.metadata_loader) { + Ok(blob) => { + if let Some(h) = self.crate_matches(&blob, &lib) { + (h, blob) + } else { + info!("metadata mismatch"); + continue; + } + } + Err(err) => { + info!("no metadata found: {}", err); continue; } - } - Err(err) => { - info!("no metadata found: {}", err); - continue; - } - }; + }; // If we see multiple hashes, emit an error about duplicate candidates. if slot.as_ref().map_or(false, |s| s.0 != hash) { let mut e = struct_span_err!(self.sess, @@ -833,50 +823,14 @@ pub fn note_crate_name(err: &mut DiagnosticBuilder, name: &str) { err.note(&format!("crate name: {}", name)); } -impl ArchiveMetadata { - fn new(ar: ArchiveRO) -> Option { - let data = { - let section = ar.iter() - .filter_map(|s| s.ok()) - .find(|sect| sect.name() == Some(METADATA_FILENAME)); - match section { - Some(s) => s.data() as *const [u8], - None => { - debug!("didn't find '{}' in the archive", METADATA_FILENAME); - return None; - } - } - }; - - Some(ArchiveMetadata { - _archive: ar, - data: data, - }) - } - - pub fn as_slice<'a>(&'a self) -> &'a [u8] { - unsafe { &*self.data } - } -} - -fn verify_decompressed_encoding_version(blob: &MetadataBlob, - filename: &Path) - -> Result<(), String> { - if !blob.is_compatible() { - Err((format!("incompatible metadata version found: '{}'", - filename.display()))) - } else { - Ok(()) - } -} - // Just a small wrapper to time how long reading metadata takes. fn get_metadata_section(target: &Target, flavor: CrateFlavor, - filename: &Path) + filename: &Path, + loader: &MetadataLoader) -> Result { let start = Instant::now(); - let ret = get_metadata_section_imp(target, flavor, filename); + let ret = get_metadata_section_imp(target, flavor, filename, loader); info!("reading {:?} => {:?}", filename.file_name().unwrap(), start.elapsed()); @@ -885,118 +839,61 @@ fn get_metadata_section(target: &Target, fn get_metadata_section_imp(target: &Target, flavor: CrateFlavor, - filename: &Path) + filename: &Path, + loader: &MetadataLoader) -> Result { if !filename.exists() { return Err(format!("no such file: '{}'", filename.display())); } - if flavor == CrateFlavor::Rlib { - // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap - // internally to read the file. We also avoid even using a memcpy by - // just keeping the archive along while the metadata is in use. - let archive = match ArchiveRO::open(filename) { - Some(ar) => ar, - None => { - debug!("llvm didn't like `{}`", filename.display()); - return Err(format!("failed to read rlib metadata: '{}'", filename.display())); + let raw_bytes: ErasedBoxRef<[u8]> = match flavor { + CrateFlavor::Rlib => loader.get_rlib_metadata(target, filename)?, + CrateFlavor::Dylib => { + let buf = loader.get_dylib_metadata(target, filename)?; + // The header is uncompressed + let header_len = METADATA_HEADER.len(); + debug!("checking {} bytes of metadata-version stamp", header_len); + let header = &buf[..cmp::min(header_len, buf.len())]; + if header != METADATA_HEADER { + return Err(format!("incompatible metadata version found: '{}'", + filename.display())); } - }; - return match ArchiveMetadata::new(archive).map(|ar| MetadataBlob::Archive(ar)) { - None => Err(format!("failed to read rlib metadata: '{}'", filename.display())), - Some(blob) => { - verify_decompressed_encoding_version(&blob, filename)?; - Ok(blob) - } - }; - } else if flavor == CrateFlavor::Rmeta { - let mut file = File::open(filename).map_err(|_| - format!("could not open file: '{}'", filename.display()))?; - let mut buf = vec![]; - file.read_to_end(&mut buf).map_err(|_| - format!("failed to read rlib metadata: '{}'", filename.display()))?; - let blob = MetadataBlob::Raw(buf); - verify_decompressed_encoding_version(&blob, filename)?; - return Ok(blob); - } - unsafe { - let buf = common::path2cstr(filename); - let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr()); - if mb as isize == 0 { - return Err(format!("error reading library: '{}'", filename.display())); - } - let of = match ObjectFile::new(mb) { - Some(of) => of, - _ => { - return Err((format!("provided path not an object file: '{}'", filename.display()))) - } - }; - let si = mk_section_iter(of.llof); - while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False { - let mut name_buf = ptr::null(); - let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf); - let name = slice::from_raw_parts(name_buf as *const u8, name_len as usize).to_vec(); - let name = String::from_utf8(name).unwrap(); - debug!("get_metadata_section: name {}", name); - if read_meta_section_name(target) == name { - let cbuf = llvm::LLVMGetSectionContents(si.llsi); - let csz = llvm::LLVMGetSectionSize(si.llsi) as usize; - let cvbuf: *const u8 = cbuf as *const u8; - let vlen = METADATA_HEADER.len(); - debug!("checking {} bytes of metadata-version stamp", vlen); - let minsz = cmp::min(vlen, csz); - let buf0 = slice::from_raw_parts(cvbuf, minsz); - let version_ok = buf0 == METADATA_HEADER; - if !version_ok { - return Err((format!("incompatible metadata version found: '{}'", - filename.display()))); - } - let cvbuf1 = cvbuf.offset(vlen as isize); - debug!("inflating {} bytes of compressed metadata", csz - vlen); - let bytes = slice::from_raw_parts(cvbuf1, csz - vlen); - match flate::inflate_bytes(bytes) { - Ok(inflated) => { - let blob = MetadataBlob::Inflated(inflated); - verify_decompressed_encoding_version(&blob, filename)?; - return Ok(blob); - } - Err(_) => {} + // Header is okay -> inflate the actual metadata + let compressed_bytes = &buf[header_len..]; + debug!("inflating {} bytes of compressed metadata", compressed_bytes.len()); + match flate::inflate_bytes(compressed_bytes) { + Ok(inflated) => { + let buf = unsafe { OwningRef::new_assert_stable_address(inflated) }; + buf.map_owner_box().erase_owner() + } + Err(_) => { + return Err(format!("failed to decompress metadata: {}", filename.display())); } } - llvm::LLVMMoveToNextSection(si.llsi); } - Err(format!("metadata not found: '{}'", filename.display())) - } -} - -pub fn meta_section_name(target: &Target) -> &'static str { - // Historical note: - // - // When using link.exe it was seen that the section name `.note.rustc` - // was getting shortened to `.note.ru`, and according to the PE and COFF - // specification: - // - // > Executable images do not use a string table and do not support - // > section names longer than 8 characters - // - // https://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx - // - // As a result, we choose a slightly shorter name! As to why - // `.note.rustc` works on MinGW, that's another good question... - - if target.options.is_like_osx { - "__DATA,.rustc" + CrateFlavor::Rmeta => { + let mut file = File::open(filename).map_err(|_| + format!("could not open file: '{}'", filename.display()))?; + let mut buf = vec![]; + file.read_to_end(&mut buf).map_err(|_| + format!("failed to read rmeta metadata: '{}'", filename.display()))?; + OwningRef::new(buf).map_owner_box().erase_owner() + } + }; + let blob = MetadataBlob(raw_bytes); + if blob.is_compatible() { + Ok(blob) } else { - ".rustc" + Err(format!("incompatible metadata version found: '{}'", filename.display())) } } -pub fn read_meta_section_name(_target: &Target) -> &'static str { - ".rustc" -} - // A diagnostic function for dumping crate metadata to an output stream -pub fn list_file_metadata(target: &Target, path: &Path, out: &mut io::Write) -> io::Result<()> { +pub fn list_file_metadata(target: &Target, + path: &Path, + loader: &MetadataLoader, + out: &mut io::Write) + -> io::Result<()> { let filename = path.file_name().unwrap().to_str().unwrap(); let flavor = if filename.ends_with(".rlib") { CrateFlavor::Rlib @@ -1005,7 +902,7 @@ pub fn list_file_metadata(target: &Target, path: &Path, out: &mut io::Write) -> } else { CrateFlavor::Dylib }; - match get_metadata_section(target, flavor, path) { + match get_metadata_section(target, flavor, path, loader) { Ok(metadata) => metadata.list_crate_metadata(out), Err(msg) => write!(out, "{}\n", msg), } diff --git a/src/librustc_trans/Cargo.toml b/src/librustc_trans/Cargo.toml index af477f5a15217..4ccc85257f3c9 100644 --- a/src/librustc_trans/Cargo.toml +++ b/src/librustc_trans/Cargo.toml @@ -12,6 +12,7 @@ test = false [dependencies] flate = { path = "../libflate" } log = "0.3" +owning_ref = "0.3.3" rustc = { path = "../librustc" } rustc_back = { path = "../librustc_back" } rustc_bitflags = { path = "../librustc_bitflags" } diff --git a/src/librustc_trans/back/archive.rs b/src/librustc_trans/back/archive.rs index 0f908b7d0698b..902065c8688d0 100644 --- a/src/librustc_trans/back/archive.rs +++ b/src/librustc_trans/back/archive.rs @@ -20,6 +20,7 @@ use std::str; use libc; use llvm::archive_ro::{ArchiveRO, Child}; use llvm::{self, ArchiveKind}; +use metadata::METADATA_FILENAME; use rustc::session::Session; pub struct ArchiveConfig<'a> { @@ -158,11 +159,9 @@ impl<'a> ArchiveBuilder<'a> { // Ignoring all bytecode files, no matter of // name let bc_ext = ".bytecode.deflate"; - let metadata_filename = - self.config.sess.cstore.metadata_filename().to_owned(); self.add_archive(rlib, move |fname: &str| { - if fname.ends_with(bc_ext) || fname == metadata_filename { + if fname.ends_with(bc_ext) || fname == METADATA_FILENAME { return true } diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index e42e69d2a76e2..b8aabef65a984 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -13,6 +13,7 @@ use super::linker::Linker; use super::rpath::RPathConfig; use super::rpath; use super::msvc; +use metadata::METADATA_FILENAME; use session::config; use session::config::NoDebugInfo; use session::config::{OutputFilenames, Input, OutputType}; @@ -521,7 +522,7 @@ fn link_rlib<'a>(sess: &'a Session, // contain the metadata in a separate file. We use a temp directory // here so concurrent builds in the same directory don't try to use // the same filename for metadata (stomping over one another) - let metadata = tmpdir.join(sess.cstore.metadata_filename()); + let metadata = tmpdir.join(METADATA_FILENAME); emit_metadata(sess, trans, &metadata); ab.add_file(&metadata); @@ -1141,8 +1142,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, archive.update_symbols(); for f in archive.src_files() { - if f.ends_with("bytecode.deflate") || - f == sess.cstore.metadata_filename() { + if f.ends_with("bytecode.deflate") || f == METADATA_FILENAME { archive.remove_file(&f); continue } @@ -1217,8 +1217,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, let mut any_objects = false; for f in archive.src_files() { - if f.ends_with("bytecode.deflate") || - f == sess.cstore.metadata_filename() { + if f.ends_with("bytecode.deflate") || f == METADATA_FILENAME { archive.remove_file(&f); continue } diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index 8689e176f7a7b..437ced85b2e4a 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -34,6 +34,7 @@ use back::linker::LinkerInfo; use back::symbol_export::{self, ExportedSymbols}; use llvm::{ContextRef, Linkage, ModuleRef, ValueRef, Vector, get_param}; use llvm; +use metadata; use rustc::hir::def_id::LOCAL_CRATE; use middle::lang_items::StartFnLangItem; use middle::cstore::EncodedMetadata; @@ -778,8 +779,7 @@ fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>, }; unsafe { llvm::LLVMSetInitializer(llglobal, llconst); - let section_name = - tcx.sess.cstore.metadata_section_name(&tcx.sess.target.target); + let section_name = metadata::metadata_section_name(&tcx.sess.target.target); let name = CString::new(section_name).unwrap(); llvm::LLVMSetSection(llglobal, name.as_ptr()); diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs index 8e633ee59b67d..15a1b32a5fd58 100644 --- a/src/librustc_trans/lib.rs +++ b/src/librustc_trans/lib.rs @@ -45,6 +45,7 @@ use syntax_pos::symbol::Symbol; extern crate flate; extern crate libc; +extern crate owning_ref; #[macro_use] extern crate rustc; extern crate rustc_back; extern crate rustc_data_structures; @@ -70,6 +71,8 @@ pub use rustc::util; pub use base::trans_crate; pub use back::symbol_names::provide; +pub use metadata::LlvmMetadataLoader; + pub mod back { pub use rustc::hir::svh; @@ -120,6 +123,7 @@ mod declare; mod glue; mod intrinsic; mod machine; +mod metadata; mod meth; mod mir; mod monomorphize; diff --git a/src/librustc_trans/metadata.rs b/src/librustc_trans/metadata.rs new file mode 100644 index 0000000000000..2c0148dfbb371 --- /dev/null +++ b/src/librustc_trans/metadata.rs @@ -0,0 +1,122 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use rustc::util::common; +use rustc::middle::cstore::MetadataLoader; +use rustc_back::target::Target; +use llvm; +use llvm::{False, ObjectFile, mk_section_iter}; +use llvm::archive_ro::ArchiveRO; + +use owning_ref::{ErasedBoxRef, OwningRef}; +use std::path::Path; +use std::ptr; +use std::slice; + +pub const METADATA_FILENAME: &str = "rust.metadata.bin"; + +pub struct LlvmMetadataLoader; + +impl MetadataLoader for LlvmMetadataLoader { + fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result, String> { + // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap + // internally to read the file. We also avoid even using a memcpy by + // just keeping the archive along while the metadata is in use. + let archive = ArchiveRO::open(filename) + .map(|ar| OwningRef::new(box ar)) + .ok_or_else(|| { + debug!("llvm didn't like `{}`", filename.display()); + format!("failed to read rlib metadata: '{}'", filename.display()) + })?; + let buf: OwningRef<_, [u8]> = archive + .try_map(|ar| { + ar.iter() + .filter_map(|s| s.ok()) + .find(|sect| sect.name() == Some(METADATA_FILENAME)) + .map(|s| s.data()) + .ok_or_else(|| { + debug!("didn't find '{}' in the archive", METADATA_FILENAME); + format!("failed to read rlib metadata: '{}'", + filename.display()) + }) + })?; + Ok(buf.erase_owner()) + } + + fn get_dylib_metadata(&self, + target: &Target, + filename: &Path) + -> Result, String> { + unsafe { + let buf = common::path2cstr(filename); + let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr()); + if mb as isize == 0 { + return Err(format!("error reading library: '{}'", filename.display())); + } + let of = ObjectFile::new(mb) + .map(|of| OwningRef::new(box of)) + .ok_or_else(|| format!("provided path not an object file: '{}'", + filename.display()))?; + let buf = of.try_map(|of| search_meta_section(of, target, filename))?; + Ok(buf.erase_owner()) + } + } +} + +fn search_meta_section<'a>(of: &'a ObjectFile, + target: &Target, + filename: &Path) + -> Result<&'a [u8], String> { + unsafe { + let si = mk_section_iter(of.llof); + while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False { + let mut name_buf = ptr::null(); + let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf); + let name = slice::from_raw_parts(name_buf as *const u8, name_len as usize).to_vec(); + let name = String::from_utf8(name).unwrap(); + debug!("get_metadata_section: name {}", name); + if read_metadata_section_name(target) == name { + let cbuf = llvm::LLVMGetSectionContents(si.llsi); + let csz = llvm::LLVMGetSectionSize(si.llsi) as usize; + // The buffer is valid while the object file is around + let buf: &'a [u8] = slice::from_raw_parts(cbuf as *const u8, csz); + return Ok(buf); + } + llvm::LLVMMoveToNextSection(si.llsi); + } + } + Err(format!("metadata not found: '{}'", filename.display())) +} + +pub fn metadata_section_name(target: &Target) -> &'static str { + // Historical note: + // + // When using link.exe it was seen that the section name `.note.rustc` + // was getting shortened to `.note.ru`, and according to the PE and COFF + // specification: + // + // > Executable images do not use a string table and do not support + // > section names longer than 8 characters + // + // https://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx + // + // As a result, we choose a slightly shorter name! As to why + // `.note.rustc` works on MinGW, that's another good question... + + if target.options.is_like_osx { + "__DATA,.rustc" + } else { + ".rustc" + } +} + +fn read_metadata_section_name(_target: &Target) -> &'static str { + ".rustc" +} diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 9e2d85163335c..d41e993140969 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -19,6 +19,7 @@ use rustc::ty::{self, TyCtxt, GlobalArenas}; use rustc::hir::map as hir_map; use rustc::lint; use rustc::util::nodemap::FxHashMap; +use rustc_trans; use rustc_trans::back::link; use rustc_resolve as resolve; use rustc_metadata::cstore::CStore; @@ -138,7 +139,7 @@ pub fn run_core(search_paths: SearchPaths, let dep_graph = DepGraph::new(false); let _ignore = dep_graph.in_ignore(); - let cstore = Rc::new(CStore::new(&dep_graph)); + let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader)); let mut sess = session::build_session_( sessopts, &dep_graph, cpath, diagnostic_handler, codemap, cstore.clone() ); diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index d5237d629cfc1..0600ae5b66e7e 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -34,6 +34,7 @@ use rustc_driver::{self, driver, Compilation}; use rustc_driver::driver::phase_2_configure_and_expand; use rustc_metadata::cstore::CStore; use rustc_resolve::MakeGlobMap; +use rustc_trans; use rustc_trans::back::link; use syntax::ast; use syntax::codemap::CodeMap; @@ -81,7 +82,7 @@ pub fn run(input: &str, let dep_graph = DepGraph::new(false); let _ignore = dep_graph.in_ignore(); - let cstore = Rc::new(CStore::new(&dep_graph)); + let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader)); let mut sess = session::build_session_( sessopts, &dep_graph, Some(input_path.clone()), handler, codemap.clone(), cstore.clone(), ); @@ -229,7 +230,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec, libs: SearchPaths, let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter); let dep_graph = DepGraph::new(false); - let cstore = Rc::new(CStore::new(&dep_graph)); + let cstore = Rc::new(CStore::new(&dep_graph, box rustc_trans::LlvmMetadataLoader)); let mut sess = session::build_session_( sessopts, &dep_graph, None, diagnostic_handler, codemap, cstore.clone(), ); diff --git a/src/test/run-make/issue-19371/foo.rs b/src/test/run-make/issue-19371/foo.rs index 0336fe277c51f..74ecdab33725b 100644 --- a/src/test/run-make/issue-19371/foo.rs +++ b/src/test/run-make/issue-19371/foo.rs @@ -15,6 +15,7 @@ extern crate rustc_driver; extern crate rustc_lint; extern crate rustc_metadata; extern crate rustc_errors; +extern crate rustc_trans; extern crate syntax; use rustc::dep_graph::DepGraph; @@ -58,7 +59,7 @@ fn basic_sess(sysroot: PathBuf) -> (Session, Rc) { let descriptions = Registry::new(&rustc::DIAGNOSTICS); let dep_graph = DepGraph::new(opts.build_dep_graph()); - let cstore = Rc::new(CStore::new(&dep_graph)); + let cstore = Rc::new(CStore::new(&dep_graph, Box::new(rustc_trans::LlvmMetadataLoader))); let sess = build_session(opts, &dep_graph, None, descriptions, cstore.clone()); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); (sess, cstore) From e5ec07388cc4d8da07ba0401ba55614ff518de6f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 14 May 2017 12:41:42 -0700 Subject: [PATCH 04/10] appveyor: Wrap handle.exe download in a retry Should help deal with spurious download failures. --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 42c6256a95a8c..01b176b8487b3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -141,7 +141,7 @@ install: - set PATH="C:\Program Files (x86)\Inno Setup 5";%PATH% # Help debug some handle issues on AppVeyor - - ps: Invoke-WebRequest -Uri https://download.sysinternals.com/files/Handle.zip -OutFile handle.zip + - appveyor-retry appveyor DownloadFile https://download.sysinternals.com/files/Handle.zip - mkdir handle - ps: Expand-Archive handle.zip -dest handle - set PATH=%PATH%;%CD%\handle From c0d5aa8aaf478e52d0e96bd0fbd19cde6e30ebb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 23 Apr 2017 17:57:07 -0700 Subject: [PATCH 05/10] Make unsatisfied trait bounds note multiline Make diagnostic note for existing method with unsatisfied trait bounds multiline for cleaner output. ``` = note: the method `count` exists but the following trait bounds were not satisfied: `[closure@../../src/test/compile-fail/issue-36053-2.rs:17:39: 17:53] : std::ops::FnMut<(&_,)>` `std::iter::Filter> [closure@../../src/test/compile-fail/issue-36053-2.rs:17:39: 17:53]> : std::iter::Iterator` Before: ``` = note: the method `count` exists but the following trait bounds were not satisfied: `[closure@../../src/test/compile-fail/issue-36053-2.rs:17:39: 17:53] : std::ops::FnMut<(&_,)>`, `std::iter::Filter>, [closure@../../src/test/compile-fail/issue-36053-2.rs:17:39: 17:53]> : std::iter::Iterator` ``` --- src/librustc_typeck/check/method/suggest.rs | 4 ++-- src/test/ui/mismatched_types/issue-36053-2.stderr | 4 +++- .../method-help-unsatisfied-bound.rs | 2 +- .../method-help-unsatisfied-bound.stderr | 11 +++++++++++ 4 files changed, 17 insertions(+), 4 deletions(-) rename src/test/{compile-fail => ui/mismatched_types}/method-help-unsatisfied-bound.rs (87%) create mode 100644 src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index c7ec379b0de25..7e70bb92cd6e0 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -251,9 +251,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let bound_list = unsatisfied_predicates.iter() .map(|p| format!("`{} : {}`", p.self_ty(), p)) .collect::>() - .join(", "); + .join("\n"); err.note(&format!("the method `{}` exists but the following trait bounds \ - were not satisfied: {}", + were not satisfied:\n{}", item_name, bound_list)); } diff --git a/src/test/ui/mismatched_types/issue-36053-2.stderr b/src/test/ui/mismatched_types/issue-36053-2.stderr index adc229aaacc54..78e0f7e619b12 100644 --- a/src/test/ui/mismatched_types/issue-36053-2.stderr +++ b/src/test/ui/mismatched_types/issue-36053-2.stderr @@ -4,7 +4,9 @@ error: no method named `count` found for type `std::iter::Filter("str").fuse().filter(|a: &str| true).count(); | ^^^^^ | - = note: the method `count` exists but the following trait bounds were not satisfied: `[closure@$DIR/issue-36053-2.rs:17:39: 17:53] : std::ops::FnMut<(&_,)>`, `std::iter::Filter>, [closure@$DIR/issue-36053-2.rs:17:39: 17:53]> : std::iter::Iterator` + = note: the method `count` exists but the following trait bounds were not satisfied: + `[closure@$DIR/issue-36053-2.rs:17:39: 17:53] : std::ops::FnMut<(&_,)>` + `std::iter::Filter>, [closure@$DIR/issue-36053-2.rs:17:39: 17:53]> : std::iter::Iterator` error[E0281]: type mismatch: `[closure@$DIR/issue-36053-2.rs:17:39: 17:53]` implements the trait `for<'r> std::ops::FnMut<(&'r str,)>`, but the trait `for<'r> std::ops::FnMut<(&'r &str,)>` is required --> $DIR/issue-36053-2.rs:17:32 diff --git a/src/test/compile-fail/method-help-unsatisfied-bound.rs b/src/test/ui/mismatched_types/method-help-unsatisfied-bound.rs similarity index 87% rename from src/test/compile-fail/method-help-unsatisfied-bound.rs rename to src/test/ui/mismatched_types/method-help-unsatisfied-bound.rs index 6416d5467c890..a4eb445555112 100644 --- a/src/test/compile-fail/method-help-unsatisfied-bound.rs +++ b/src/test/ui/mismatched_types/method-help-unsatisfied-bound.rs @@ -14,5 +14,5 @@ fn main() { let a: Result<(), Foo> = Ok(()); a.unwrap(); //~^ ERROR no method named `unwrap` found for type `std::result::Result<(), Foo>` - //~| NOTE the following trait bounds were not satisfied: `Foo : std::fmt::Debug` + //~| NOTE the method `unwrap` exists but the following trait bounds were not satisfied } diff --git a/src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr b/src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr new file mode 100644 index 0000000000000..2bd786c20fef0 --- /dev/null +++ b/src/test/ui/mismatched_types/method-help-unsatisfied-bound.stderr @@ -0,0 +1,11 @@ +error: no method named `unwrap` found for type `std::result::Result<(), Foo>` in the current scope + --> $DIR/method-help-unsatisfied-bound.rs:15:7 + | +15 | a.unwrap(); + | ^^^^^^ + | + = note: the method `unwrap` exists but the following trait bounds were not satisfied: + `Foo : std::fmt::Debug` + +error: aborting due to previous error + From f92bd3dbd005bb0bbcdeb64eea263634428d660a Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Sun, 14 May 2017 20:06:54 +0200 Subject: [PATCH 06/10] Add links to the `thread::LocalKey` doc. Part of #29378 . --- src/libstd/thread/local.rs | 45 +++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/libstd/thread/local.rs b/src/libstd/thread/local.rs index e2b22b1d89f04..c2c6e6cf87dff 100644 --- a/src/libstd/thread/local.rs +++ b/src/libstd/thread/local.rs @@ -19,16 +19,16 @@ use mem; /// A thread local storage key which owns its contents. /// /// This key uses the fastest possible implementation available to it for the -/// target platform. It is instantiated with the `thread_local!` macro and the -/// primary method is the `with` method. +/// target platform. It is instantiated with the [`thread_local!`] macro and the +/// primary method is the [`with`] method. /// -/// The `with` method yields a reference to the contained value which cannot be +/// The [`with`] method yields a reference to the contained value which cannot be /// sent across threads or escape the given closure. /// /// # Initialization and Destruction /// -/// Initialization is dynamically performed on the first call to `with()` -/// within a thread, and values that implement `Drop` get destructed when a +/// Initialization is dynamically performed on the first call to [`with`] +/// within a thread, and values that implement [`Drop`] get destructed when a /// thread exits. Some caveats apply, which are explained below. /// /// # Examples @@ -77,6 +77,10 @@ use mem; /// 3. On macOS, initializing TLS during destruction of other TLS slots can /// sometimes cancel *all* destructors for the current thread, whether or not /// the slots have already had their destructors run or not. +/// +/// [`with`]: ../../std/thread/struct.LocalKey.html#method.with +/// [`thread_local!`]: ../../std/macro.thread_local.html +/// [`Drop`]: ../../std/ops/trait.Drop.html #[stable(feature = "rust1", since = "1.0.0")] pub struct LocalKey { // This outer `LocalKey` type is what's going to be stored in statics, @@ -106,7 +110,7 @@ impl fmt::Debug for LocalKey { } } -/// Declare a new thread local storage key of type `std::thread::LocalKey`. +/// Declare a new thread local storage key of type [`std::thread::LocalKey`]. /// /// # Syntax /// @@ -124,8 +128,10 @@ impl fmt::Debug for LocalKey { /// # fn main() {} /// ``` /// -/// See [LocalKey documentation](thread/struct.LocalKey.html) for more +/// See [LocalKey documentation][`std::thread::LocalKey`] for more /// information. +/// +/// [`std::thread::LocalKey`]: ../std/thread/struct.LocalKey.html #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] #[allow_internal_unstable] @@ -195,11 +201,13 @@ macro_rules! __thread_local_inner { #[derive(Debug, Eq, PartialEq, Copy, Clone)] pub enum LocalKeyState { /// All keys are in this state whenever a thread starts. Keys will - /// transition to the `Valid` state once the first call to `with` happens + /// transition to the `Valid` state once the first call to [`with`] happens /// and the initialization expression succeeds. /// /// Keys in the `Uninitialized` state will yield a reference to the closure - /// passed to `with` so long as the initialization routine does not panic. + /// passed to [`with`] so long as the initialization routine does not panic. + /// + /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with Uninitialized, /// Once a key has been accessed successfully, it will enter the `Valid` @@ -208,7 +216,9 @@ pub enum LocalKeyState { /// `Destroyed` state. /// /// Keys in the `Valid` state will be guaranteed to yield a reference to the - /// closure passed to `with`. + /// closure passed to [`with`]. + /// + /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with Valid, /// When a thread exits, the destructors for keys will be run (if @@ -216,7 +226,9 @@ pub enum LocalKeyState { /// destructor has run, a key is in the `Destroyed` state. /// /// Keys in the `Destroyed` states will trigger a panic when accessed via - /// `with`. + /// [`with`]. + /// + /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with Destroyed, } @@ -283,23 +295,26 @@ impl LocalKey { /// Query the current state of this key. /// /// A key is initially in the `Uninitialized` state whenever a thread - /// starts. It will remain in this state up until the first call to `with` + /// starts. It will remain in this state up until the first call to [`with`] /// within a thread has run the initialization expression successfully. /// /// Once the initialization expression succeeds, the key transitions to the - /// `Valid` state which will guarantee that future calls to `with` will + /// `Valid` state which will guarantee that future calls to [`with`] will /// succeed within the thread. /// /// When a thread exits, each key will be destroyed in turn, and as keys are /// destroyed they will enter the `Destroyed` state just before the /// destructor starts to run. Keys may remain in the `Destroyed` state after /// destruction has completed. Keys without destructors (e.g. with types - /// that are `Copy`), may never enter the `Destroyed` state. + /// that are [`Copy`]), may never enter the `Destroyed` state. /// /// Keys in the `Uninitialized` state can be accessed so long as the /// initialization does not panic. Keys in the `Valid` state are guaranteed /// to be able to be accessed. Keys in the `Destroyed` state will panic on - /// any call to `with`. + /// any call to [`with`]. + /// + /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with + /// [`Copy`]: ../../std/marker/trait.Copy.html #[unstable(feature = "thread_local_state", reason = "state querying was recently added", issue = "27716")] From 8e4f3151166d8ddea2d365c589bf4ac7ab6f20c9 Mon Sep 17 00:00:00 2001 From: Robin Kruppe Date: Sun, 30 Apr 2017 20:04:35 +0200 Subject: [PATCH 07/10] Remove rustc_llvm dependency from librustc Consequently, session creation can no longer initialize LLVM. The few places that use the compiler without going through rustc_driver/CompilerCalls thus need to be careful to manually initialize LLVM (via rustc_trans!) immediately after session creation. This means librustc is not rebuilt when LLVM changes. --- src/Cargo.lock | 1 - src/librustc/Cargo.toml | 1 - src/librustc/lib.rs | 1 - src/librustc/session/mod.rs | 54 ---------------------------- src/librustc_driver/lib.rs | 2 ++ src/librustc_driver/test.rs | 1 + src/librustc_trans/lib.rs | 52 +++++++++++++++++++++++++++ src/librustdoc/core.rs | 1 + src/librustdoc/test.rs | 2 ++ src/test/run-make/issue-19371/foo.rs | 1 + 10 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index bd9fda42570fd..8bf360751c578 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -456,7 +456,6 @@ dependencies = [ "rustc_const_math 0.0.0", "rustc_data_structures 0.0.0", "rustc_errors 0.0.0", - "rustc_llvm 0.0.0", "serialize 0.0.0", "syntax 0.0.0", "syntax_pos 0.0.0", diff --git a/src/librustc/Cargo.toml b/src/librustc/Cargo.toml index 3c762e43e9aa6..9d64f511914d6 100644 --- a/src/librustc/Cargo.toml +++ b/src/librustc/Cargo.toml @@ -19,7 +19,6 @@ rustc_bitflags = { path = "../librustc_bitflags" } rustc_const_math = { path = "../librustc_const_math" } rustc_data_structures = { path = "../librustc_data_structures" } rustc_errors = { path = "../librustc_errors" } -rustc_llvm = { path = "../librustc_llvm" } serialize = { path = "../libserialize" } syntax = { path = "../libsyntax" } syntax_pos = { path = "../libsyntax_pos" } diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index c979677ff717c..f32ee7900646b 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -55,7 +55,6 @@ extern crate getopts; extern crate graphviz; extern crate libc; extern crate owning_ref; -extern crate rustc_llvm as llvm; extern crate rustc_back; extern crate rustc_data_structures; extern crate serialize; diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 2e2d5a6bd4d38..814246330a4c2 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -37,19 +37,16 @@ use syntax_pos::{Span, MultiSpan, FileMap}; use rustc_back::{LinkerFlavor, PanicStrategy}; use rustc_back::target::Target; use rustc_data_structures::flock; -use llvm; use std::path::{Path, PathBuf}; use std::cell::{self, Cell, RefCell}; use std::collections::HashMap; use std::env; -use std::ffi::CString; use std::io::Write; use std::rc::Rc; use std::fmt; use std::time::Duration; use std::sync::Arc; -use libc::c_int; mod code_stats; pub mod config; @@ -713,8 +710,6 @@ pub fn build_session_(sopts: config::Options, out_of_fuel: Cell::new(false), }; - init_llvm(&sess); - sess } @@ -743,55 +738,6 @@ pub enum IncrCompSession { } } -fn init_llvm(sess: &Session) { - unsafe { - // Before we touch LLVM, make sure that multithreading is enabled. - use std::sync::Once; - static INIT: Once = Once::new(); - static mut POISONED: bool = false; - INIT.call_once(|| { - if llvm::LLVMStartMultithreaded() != 1 { - // use an extra bool to make sure that all future usage of LLVM - // cannot proceed despite the Once not running more than once. - POISONED = true; - } - - configure_llvm(sess); - }); - - if POISONED { - bug!("couldn't enable multi-threaded LLVM"); - } - } -} - -unsafe fn configure_llvm(sess: &Session) { - let mut llvm_c_strs = Vec::new(); - let mut llvm_args = Vec::new(); - - { - let mut add = |arg: &str| { - let s = CString::new(arg).unwrap(); - llvm_args.push(s.as_ptr()); - llvm_c_strs.push(s); - }; - add("rustc"); // fake program name - if sess.time_llvm_passes() { add("-time-passes"); } - if sess.print_llvm_passes() { add("-debug-pass=Structure"); } - - for arg in &sess.opts.cg.llvm_args { - add(&(*arg)); - } - } - - llvm::LLVMInitializePasses(); - - llvm::initialize_available_targets(); - - llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, - llvm_args.as_ptr()); -} - pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! { let emitter: Box = match output { config::ErrorOutputType::HumanReadable(color_config) => { diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 2dab680ad45aa..20eba802e85a0 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -211,6 +211,7 @@ pub fn run_compiler<'a>(args: &[String], let mut sess = session::build_session_with_codemap( sopts, &dep_graph, input_file_path, descriptions, cstore.clone(), codemap, emitter_dest, ); + rustc_trans::init(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); let mut cfg = config::build_configuration(&sess, cfg); @@ -415,6 +416,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { None, descriptions.clone(), cstore.clone()); + rustc_trans::init(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); let mut cfg = config::build_configuration(&sess, cfg.clone()); target_features::add_configuration(&mut cfg, &sess); diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index be4f64b7109c5..1d236a96bf62e 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -112,6 +112,7 @@ fn test_env(source_string: &str, diagnostic_handler, Rc::new(CodeMap::new(FilePathMapping::empty())), cstore.clone()); + rustc_trans::init(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); let input = config::Input::Str { name: driver::anon_src(), diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs index 15a1b32a5fd58..d9552eb08b4b6 100644 --- a/src/librustc_trans/lib.rs +++ b/src/librustc_trans/lib.rs @@ -178,3 +178,55 @@ pub struct CrateTranslation { } __build_diagnostic_array! { librustc_trans, DIAGNOSTICS } + +use rustc::session::Session; +pub fn init(sess: &Session) { + unsafe { + // Before we touch LLVM, make sure that multithreading is enabled. + use std::sync::Once; + static INIT: Once = Once::new(); + static mut POISONED: bool = false; + INIT.call_once(|| { + if llvm::LLVMStartMultithreaded() != 1 { + // use an extra bool to make sure that all future usage of LLVM + // cannot proceed despite the Once not running more than once. + POISONED = true; + } + + configure_llvm(sess); + }); + + if POISONED { + bug!("couldn't enable multi-threaded LLVM"); + } + } +} + +use std::ffi::CString; +use libc::c_int; +unsafe fn configure_llvm(sess: &Session) { + let mut llvm_c_strs = Vec::new(); + let mut llvm_args = Vec::new(); + + { + let mut add = |arg: &str| { + let s = CString::new(arg).unwrap(); + llvm_args.push(s.as_ptr()); + llvm_c_strs.push(s); + }; + add("rustc"); // fake program name + if sess.time_llvm_passes() { add("-time-passes"); } + if sess.print_llvm_passes() { add("-debug-pass=Structure"); } + + for arg in &sess.opts.cg.llvm_args { + add(&(*arg)); + } + } + + llvm::LLVMInitializePasses(); + + llvm::initialize_available_targets(); + + llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, + llvm_args.as_ptr()); +} diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index d41e993140969..9a689ed079ee2 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -143,6 +143,7 @@ pub fn run_core(search_paths: SearchPaths, let mut sess = session::build_session_( sessopts, &dep_graph, cpath, diagnostic_handler, codemap, cstore.clone() ); + rustc_trans::init(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs)); diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 0600ae5b66e7e..cfe2fad0fa469 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -86,6 +86,7 @@ pub fn run(input: &str, let mut sess = session::build_session_( sessopts, &dep_graph, Some(input_path.clone()), handler, codemap.clone(), cstore.clone(), ); + rustc_trans::init(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); sess.parse_sess.config = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone())); @@ -234,6 +235,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec, libs: SearchPaths, let mut sess = session::build_session_( sessopts, &dep_graph, None, diagnostic_handler, codemap, cstore.clone(), ); + rustc_trans::init(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); let outdir = Mutex::new(TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir")); diff --git a/src/test/run-make/issue-19371/foo.rs b/src/test/run-make/issue-19371/foo.rs index 74ecdab33725b..e96588c6e5aea 100644 --- a/src/test/run-make/issue-19371/foo.rs +++ b/src/test/run-make/issue-19371/foo.rs @@ -61,6 +61,7 @@ fn basic_sess(sysroot: PathBuf) -> (Session, Rc) { let dep_graph = DepGraph::new(opts.build_dep_graph()); let cstore = Rc::new(CStore::new(&dep_graph, Box::new(rustc_trans::LlvmMetadataLoader))); let sess = build_session(opts, &dep_graph, None, descriptions, cstore.clone()); + rustc_trans::init(&sess); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); (sess, cstore) } From e3f6e68d633040be0b88a0e11e6645bc506f8655 Mon Sep 17 00:00:00 2001 From: Robin Kruppe Date: Sun, 30 Apr 2017 20:33:25 +0200 Subject: [PATCH 08/10] Remove (direct) rustc_llvm dependency from rustc_driver This does not actually improve build times, since it still depends on rustc_trans, but is better layering and fits the multi-backend future slightly better. --- src/Cargo.lock | 1 - src/librustc/session/config.rs | 2 +- src/librustc_driver/Cargo.toml | 1 - src/librustc_driver/lib.rs | 25 ++--- src/librustc_driver/target_features.rs | 34 +------ src/librustc_trans/lib.rs | 54 +---------- src/librustc_trans/llvm_util.rs | 125 +++++++++++++++++++++++++ 7 files changed, 139 insertions(+), 103 deletions(-) create mode 100644 src/librustc_trans/llvm_util.rs diff --git a/src/Cargo.lock b/src/Cargo.lock index 8bf360751c578..05b99b0700797 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -560,7 +560,6 @@ dependencies = [ "rustc_errors 0.0.0", "rustc_incremental 0.0.0", "rustc_lint 0.0.0", - "rustc_llvm 0.0.0", "rustc_metadata 0.0.0", "rustc_mir 0.0.0", "rustc_passes 0.0.0", diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 884a71f0d32d4..4212fa1f8b12e 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -328,7 +328,7 @@ top_level_options!( } ); -#[derive(Clone, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum PrintRequest { FileNames, Sysroot, diff --git a/src/librustc_driver/Cargo.toml b/src/librustc_driver/Cargo.toml index 5b5113caa8e8c..2e949f48c175e 100644 --- a/src/librustc_driver/Cargo.toml +++ b/src/librustc_driver/Cargo.toml @@ -22,7 +22,6 @@ rustc_data_structures = { path = "../librustc_data_structures" } rustc_errors = { path = "../librustc_errors" } rustc_incremental = { path = "../librustc_incremental" } rustc_lint = { path = "../librustc_lint" } -rustc_llvm = { path = "../librustc_llvm" } rustc_metadata = { path = "../librustc_metadata" } rustc_mir = { path = "../librustc_mir" } rustc_passes = { path = "../librustc_passes" } diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 20eba802e85a0..34f636d0b9a12 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -56,7 +56,6 @@ extern crate rustc_save_analysis; extern crate rustc_trans; extern crate rustc_typeck; extern crate serialize; -extern crate rustc_llvm as llvm; #[macro_use] extern crate log; extern crate syntax; @@ -70,7 +69,7 @@ use rustc_resolve as resolve; use rustc_save_analysis as save; use rustc_save_analysis::DumpHandler; use rustc_trans::back::link; -use rustc_trans::back::write::{create_target_machine, RELOC_MODEL_ARGS, CODE_GEN_MODEL_ARGS}; +use rustc_trans::back::write::{RELOC_MODEL_ARGS, CODE_GEN_MODEL_ARGS}; use rustc::dep_graph::DepGraph; use rustc::session::{self, config, Session, build_session, CompileResult}; use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType}; @@ -182,7 +181,7 @@ pub fn run_compiler<'a>(args: &[String], let (sopts, cfg) = config::build_session_options_and_crate_config(&matches); if sopts.debugging_opts.debug_llvm { - unsafe { llvm::LLVMRustSetDebug(1); } + rustc_trans::enable_llvm_debug(); } let descriptions = diagnostics_registry(); @@ -671,14 +670,6 @@ impl RustcDefaultCalls { println!("{}", cfg); } } - PrintRequest::TargetCPUs => { - let tm = create_target_machine(sess); - unsafe { llvm::LLVMRustPrintTargetCPUs(tm); } - } - PrintRequest::TargetFeatures => { - let tm = create_target_machine(sess); - unsafe { llvm::LLVMRustPrintTargetFeatures(tm); } - } PrintRequest::RelocationModels => { println!("Available relocation models:"); for &(name, _) in RELOC_MODEL_ARGS.iter() { @@ -693,6 +684,9 @@ impl RustcDefaultCalls { } println!(""); } + PrintRequest::TargetCPUs | PrintRequest::TargetFeatures => { + rustc_trans::print(*req, sess); + } } } return Compilation::Stop; @@ -730,10 +724,7 @@ pub fn version(binary: &str, matches: &getopts::Matches) { println!("commit-date: {}", unw(commit_date_str())); println!("host: {}", config::host_triple()); println!("release: {}", unw(release_str())); - unsafe { - println!("LLVM version: {}.{}", - llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor()); - } + rustc_trans::print_version(); } } @@ -1026,9 +1017,7 @@ pub fn handle_options(args: &[String]) -> Option { } if cg_flags.contains(&"passes=list".to_string()) { - unsafe { - ::llvm::LLVMRustPrintPasses(); - } + rustc_trans::print_passes(); return None; } diff --git a/src/librustc_driver/target_features.rs b/src/librustc_driver/target_features.rs index 61bc7c6eb4c71..bee61bb398029 100644 --- a/src/librustc_driver/target_features.rs +++ b/src/librustc_driver/target_features.rs @@ -9,24 +9,9 @@ // except according to those terms. use syntax::ast; -use llvm::LLVMRustHasFeature; use rustc::session::Session; -use rustc_trans::back::write::create_target_machine; use syntax::symbol::Symbol; -use libc::c_char; - -// WARNING: the features must be known to LLVM or the feature -// detection code will walk past the end of the feature array, -// leading to crashes. - -const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"]; - -const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0", - "sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0", - "ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0", - "sse4a\0", "rdrnd\0", "rdseed\0", "fma\0"]; - -const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"]; +use rustc_trans; /// Add `target_feature = "..."` cfgs for a variety of platform /// specific features (SSE, NEON etc.). @@ -34,21 +19,10 @@ const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"]; /// This is performed by checking whether a whitelisted set of /// features is available on the target machine, by querying LLVM. pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) { - let target_machine = create_target_machine(sess); - - let whitelist = match &*sess.target.target.arch { - "arm" => ARM_WHITELIST, - "x86" | "x86_64" => X86_WHITELIST, - "hexagon" => HEXAGON_WHITELIST, - _ => &[], - }; - let tf = Symbol::intern("target_feature"); - for feat in whitelist { - assert_eq!(feat.chars().last(), Some('\0')); - if unsafe { LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } { - cfg.insert((tf, Some(Symbol::intern(&feat[..feat.len() - 1])))); - } + + for feat in rustc_trans::target_features(sess) { + cfg.insert((tf, Some(feat))); } let requested_features = sess.opts.cg.target_feature.split(','); diff --git a/src/librustc_trans/lib.rs b/src/librustc_trans/lib.rs index d9552eb08b4b6..3ac0d88b90d7b 100644 --- a/src/librustc_trans/lib.rs +++ b/src/librustc_trans/lib.rs @@ -72,6 +72,7 @@ pub use base::trans_crate; pub use back::symbol_names::provide; pub use metadata::LlvmMetadataLoader; +pub use llvm_util::{init, target_features, print_version, print_passes, print, enable_llvm_debug}; pub mod back { pub use rustc::hir::svh; @@ -122,6 +123,7 @@ mod debuginfo; mod declare; mod glue; mod intrinsic; +mod llvm_util; mod machine; mod metadata; mod meth; @@ -178,55 +180,3 @@ pub struct CrateTranslation { } __build_diagnostic_array! { librustc_trans, DIAGNOSTICS } - -use rustc::session::Session; -pub fn init(sess: &Session) { - unsafe { - // Before we touch LLVM, make sure that multithreading is enabled. - use std::sync::Once; - static INIT: Once = Once::new(); - static mut POISONED: bool = false; - INIT.call_once(|| { - if llvm::LLVMStartMultithreaded() != 1 { - // use an extra bool to make sure that all future usage of LLVM - // cannot proceed despite the Once not running more than once. - POISONED = true; - } - - configure_llvm(sess); - }); - - if POISONED { - bug!("couldn't enable multi-threaded LLVM"); - } - } -} - -use std::ffi::CString; -use libc::c_int; -unsafe fn configure_llvm(sess: &Session) { - let mut llvm_c_strs = Vec::new(); - let mut llvm_args = Vec::new(); - - { - let mut add = |arg: &str| { - let s = CString::new(arg).unwrap(); - llvm_args.push(s.as_ptr()); - llvm_c_strs.push(s); - }; - add("rustc"); // fake program name - if sess.time_llvm_passes() { add("-time-passes"); } - if sess.print_llvm_passes() { add("-debug-pass=Structure"); } - - for arg in &sess.opts.cg.llvm_args { - add(&(*arg)); - } - } - - llvm::LLVMInitializePasses(); - - llvm::initialize_available_targets(); - - llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, - llvm_args.as_ptr()); -} diff --git a/src/librustc_trans/llvm_util.rs b/src/librustc_trans/llvm_util.rs new file mode 100644 index 0000000000000..0f4c6b6408015 --- /dev/null +++ b/src/librustc_trans/llvm_util.rs @@ -0,0 +1,125 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use syntax_pos::symbol::Symbol; +use back::write::create_target_machine; +use llvm; +use rustc::session::Session; +use rustc::session::config::PrintRequest; +use libc::{c_int, c_char}; +use std::ffi::CString; + +pub fn init(sess: &Session) { + unsafe { + // Before we touch LLVM, make sure that multithreading is enabled. + use std::sync::Once; + static INIT: Once = Once::new(); + static mut POISONED: bool = false; + INIT.call_once(|| { + if llvm::LLVMStartMultithreaded() != 1 { + // use an extra bool to make sure that all future usage of LLVM + // cannot proceed despite the Once not running more than once. + POISONED = true; + } + + configure_llvm(sess); + }); + + if POISONED { + bug!("couldn't enable multi-threaded LLVM"); + } + } +} + +unsafe fn configure_llvm(sess: &Session) { + let mut llvm_c_strs = Vec::new(); + let mut llvm_args = Vec::new(); + + { + let mut add = |arg: &str| { + let s = CString::new(arg).unwrap(); + llvm_args.push(s.as_ptr()); + llvm_c_strs.push(s); + }; + add("rustc"); // fake program name + if sess.time_llvm_passes() { add("-time-passes"); } + if sess.print_llvm_passes() { add("-debug-pass=Structure"); } + + for arg in &sess.opts.cg.llvm_args { + add(&(*arg)); + } + } + + llvm::LLVMInitializePasses(); + + llvm::initialize_available_targets(); + + llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, + llvm_args.as_ptr()); +} + +// WARNING: the features must be known to LLVM or the feature +// detection code will walk past the end of the feature array, +// leading to crashes. + +const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"]; + +const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0", + "sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0", + "ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0", + "sse4a\0", "rdrnd\0", "rdseed\0", "fma\0"]; + +const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"]; + +pub fn target_features(sess: &Session) -> Vec { + let target_machine = create_target_machine(sess); + + let whitelist = match &*sess.target.target.arch { + "arm" => ARM_WHITELIST, + "x86" | "x86_64" => X86_WHITELIST, + "hexagon" => HEXAGON_WHITELIST, + _ => &[], + }; + + let mut features = Vec::new(); + for feat in whitelist { + assert_eq!(feat.chars().last(), Some('\0')); + if unsafe { llvm::LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } { + features.push(Symbol::intern(&feat[..feat.len() - 1])); + } + } + features +} + +pub fn print_version() { + unsafe { + println!("LLVM version: {}.{}", + llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor()); + } +} + +pub fn print_passes() { + unsafe { llvm::LLVMRustPrintPasses(); } +} + +pub fn print(req: PrintRequest, sess: &Session) { + let tm = create_target_machine(sess); + unsafe { + match req { + PrintRequest::TargetCPUs => llvm::LLVMRustPrintTargetCPUs(tm), + PrintRequest::TargetFeatures => llvm::LLVMRustPrintTargetFeatures(tm), + _ => bug!("rustc_trans can't handle print request: {:?}", req), + } + } +} + +pub fn enable_llvm_debug() { + unsafe { llvm::LLVMRustSetDebug(1); } +} From acdeedc19253130798916a8bb20651657d9f01e5 Mon Sep 17 00:00:00 2001 From: Robin Kruppe Date: Sun, 14 May 2017 20:33:37 +0200 Subject: [PATCH 09/10] Use AtomicBool instead of a 'static mut' for LLVM init posioning --- src/librustc_trans/llvm_util.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/librustc_trans/llvm_util.rs b/src/librustc_trans/llvm_util.rs index 0f4c6b6408015..15f56036b0c1b 100644 --- a/src/librustc_trans/llvm_util.rs +++ b/src/librustc_trans/llvm_util.rs @@ -16,23 +16,25 @@ use rustc::session::config::PrintRequest; use libc::{c_int, c_char}; use std::ffi::CString; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Once; + pub fn init(sess: &Session) { unsafe { // Before we touch LLVM, make sure that multithreading is enabled. - use std::sync::Once; + static POISONED: AtomicBool = AtomicBool::new(false); static INIT: Once = Once::new(); - static mut POISONED: bool = false; INIT.call_once(|| { if llvm::LLVMStartMultithreaded() != 1 { // use an extra bool to make sure that all future usage of LLVM // cannot proceed despite the Once not running more than once. - POISONED = true; + POISONED.store(true, Ordering::SeqCst); } configure_llvm(sess); }); - if POISONED { + if POISONED.load(Ordering::SeqCst) { bug!("couldn't enable multi-threaded LLVM"); } } From 04a16ff5acaaccf6332f706f090442f0ab9b0f95 Mon Sep 17 00:00:00 2001 From: Robin Kruppe Date: Mon, 15 May 2017 15:09:05 +0200 Subject: [PATCH 10/10] Fix run-make/llvm-pass --- src/test/run-make/llvm-pass/plugin.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/run-make/llvm-pass/plugin.rs b/src/test/run-make/llvm-pass/plugin.rs index f77b2fca857a6..37aab2bbd059a 100644 --- a/src/test/run-make/llvm-pass/plugin.rs +++ b/src/test/run-make/llvm-pass/plugin.rs @@ -14,6 +14,7 @@ extern crate rustc; extern crate rustc_plugin; +extern crate rustc_trans; #[link(name = "llvm-function-pass", kind = "static")] #[link(name = "llvm-module-pass", kind = "static")]