Skip to content

Commit c131bdc

Browse files
committed
Auto merge of #50699 - Zoxc:blocking-queries, r=mw
Blocking Rayon queries r? @michaelwoerister
2 parents 19d0b53 + f273f28 commit c131bdc

File tree

13 files changed

+617
-49
lines changed

13 files changed

+617
-49
lines changed

src/Cargo.lock

+13-9
Original file line numberDiff line numberDiff line change
@@ -1786,12 +1786,16 @@ dependencies = [
17861786
"jobserver 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
17871787
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
17881788
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
1789+
"parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
17891790
"polonius-engine 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
17901791
"proc_macro 0.0.0",
1792+
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
1793+
"rustc-rayon-core 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
17911794
"rustc_apfloat 0.0.0",
17921795
"rustc_data_structures 0.0.0",
17931796
"rustc_errors 0.0.0",
17941797
"rustc_target 0.0.0",
1798+
"scoped-tls 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
17951799
"serialize 0.0.0",
17961800
"syntax 0.0.0",
17971801
"syntax_pos 0.0.0",
@@ -1828,7 +1832,7 @@ dependencies = [
18281832
"rustc-ap-rustc_cratesio_shim 147.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
18291833
"rustc-ap-serialize 147.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
18301834
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
1831-
"rustc-rayon 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
1835+
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
18321836
"stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
18331837
]
18341838

@@ -1911,16 +1915,16 @@ dependencies = [
19111915

19121916
[[package]]
19131917
name = "rustc-rayon"
1914-
version = "0.1.0"
1918+
version = "0.1.1"
19151919
source = "registry+https://github.com/rust-lang/crates.io-index"
19161920
dependencies = [
19171921
"either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
1918-
"rustc-rayon-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
1922+
"rustc-rayon-core 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
19191923
]
19201924

19211925
[[package]]
19221926
name = "rustc-rayon-core"
1923-
version = "0.1.0"
1927+
version = "0.1.1"
19241928
source = "registry+https://github.com/rust-lang/crates.io-index"
19251929
dependencies = [
19261930
"crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2044,8 +2048,8 @@ dependencies = [
20442048
"parking_lot 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
20452049
"parking_lot_core 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
20462050
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
2047-
"rustc-rayon 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
2048-
"rustc-rayon-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
2051+
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
2052+
"rustc-rayon-core 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
20492053
"rustc_cratesio_shim 0.0.0",
20502054
"serialize 0.0.0",
20512055
"stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2061,7 +2065,7 @@ dependencies = [
20612065
"graphviz 0.0.0",
20622066
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
20632067
"rustc 0.0.0",
2064-
"rustc-rayon 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
2068+
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
20652069
"rustc_allocator 0.0.0",
20662070
"rustc_borrowck 0.0.0",
20672071
"rustc_codegen_utils 0.0.0",
@@ -3215,8 +3219,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
32153219
"checksum rustc-ap-syntax_pos 147.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fb707a229093791dc3fc35aca61d9bf0e3708f23da4536683527857bc624b061"
32163220
"checksum rustc-demangle 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11fb43a206a04116ffd7cfcf9bcb941f8eb6cc7ff667272246b0a1c74259a3cb"
32173221
"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8"
3218-
"checksum rustc-rayon 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1aa5cd8c3a706edb19b6ec6aa7b056bdc635b6e99c5cf7014f9af9d92f15e99"
3219-
"checksum rustc-rayon-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d69983f8613a9c3ba1a3bbf5e8bdf2fd5c42317b1d8dd8623ca8030173bf8a6b"
3222+
"checksum rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c6d5a683c6ba4ed37959097e88d71c9e8e26659a3cb5be8b389078e7ad45306"
3223+
"checksum rustc-rayon-core 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40f06724db71e18d68b3b946fdf890ca8c921d9edccc1404fdfdb537b0d12649"
32203224
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
32213225
"checksum rustc_version 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a54aa04a10c68c1c4eacb4337fd883b435997ede17a9385784b990777686b09a"
32223226
"checksum rustfix 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9da3cf9b79dc889a2c9879643f26d7a53e37e9361c7566b7d2787d5ace0d8396"

src/librustc/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ fmt_macros = { path = "../libfmt_macros" }
1515
graphviz = { path = "../libgraphviz" }
1616
jobserver = "0.1"
1717
lazy_static = "1.0.0"
18+
scoped-tls = { version = "0.1.1", features = ["nightly"] }
1819
log = { version = "0.4", features = ["release_max_level_info", "std"] }
1920
polonius-engine = "0.5.0"
2021
proc_macro = { path = "../libproc_macro" }
22+
rustc-rayon = "0.1.1"
23+
rustc-rayon-core = "0.1.1"
2124
rustc_apfloat = { path = "../librustc_apfloat" }
2225
rustc_target = { path = "../librustc_target" }
2326
rustc_data_structures = { path = "../librustc_data_structures" }
@@ -26,6 +29,7 @@ serialize = { path = "../libserialize" }
2629
syntax = { path = "../libsyntax" }
2730
syntax_pos = { path = "../libsyntax_pos" }
2831
backtrace = "0.3.3"
32+
parking_lot = "0.5.5"
2933
byteorder = { version = "1.1", features = ["i128"]}
3034
chalk-engine = { version = "0.6.0", default-features=false }
3135

src/librustc/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#![feature(unboxed_closures)]
6868
#![feature(trace_macros)]
6969
#![feature(trusted_len)]
70+
#![feature(vec_remove_item)]
7071
#![feature(catch_expr)]
7172
#![feature(integer_atomics)]
7273
#![feature(test)]
@@ -83,13 +84,17 @@ extern crate fmt_macros;
8384
extern crate getopts;
8485
extern crate graphviz;
8586
#[macro_use] extern crate lazy_static;
87+
#[macro_use] extern crate scoped_tls;
8688
#[cfg(windows)]
8789
extern crate libc;
8890
extern crate polonius_engine;
8991
extern crate rustc_target;
9092
#[macro_use] extern crate rustc_data_structures;
9193
extern crate serialize;
94+
extern crate parking_lot;
9295
extern crate rustc_errors as errors;
96+
extern crate rustc_rayon as rayon;
97+
extern crate rustc_rayon_core as rayon_core;
9398
#[macro_use] extern crate log;
9499
#[macro_use] extern crate syntax;
95100
extern crate syntax_pos;

src/librustc/ty/context.rs

+65-3
Original file line numberDiff line numberDiff line change
@@ -1699,16 +1699,21 @@ impl<'a, 'tcx> Lift<'tcx> for &'a Slice<CanonicalVarInfo> {
16991699
pub mod tls {
17001700
use super::{GlobalCtxt, TyCtxt};
17011701

1702-
use std::cell::Cell;
17031702
use std::fmt;
17041703
use std::mem;
17051704
use syntax_pos;
17061705
use ty::maps;
17071706
use errors::{Diagnostic, TRACK_DIAGNOSTICS};
17081707
use rustc_data_structures::OnDrop;
1709-
use rustc_data_structures::sync::{self, Lrc};
1708+
use rustc_data_structures::sync::{self, Lrc, Lock};
17101709
use dep_graph::OpenTask;
17111710

1711+
#[cfg(not(parallel_queries))]
1712+
use std::cell::Cell;
1713+
1714+
#[cfg(parallel_queries)]
1715+
use rayon_core;
1716+
17121717
/// This is the implicit state of rustc. It contains the current
17131718
/// TyCtxt and query. It is updated when creating a local interner or
17141719
/// executing a new query. Whenever there's a TyCtxt value available
@@ -1732,16 +1737,38 @@ pub mod tls {
17321737
pub task: &'a OpenTask,
17331738
}
17341739

1735-
// A thread local value which stores a pointer to the current ImplicitCtxt
1740+
/// Sets Rayon's thread local variable which is preserved for Rayon jobs
1741+
/// to `value` during the call to `f`. It is restored to its previous value after.
1742+
/// This is used to set the pointer to the new ImplicitCtxt.
1743+
#[cfg(parallel_queries)]
1744+
fn set_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
1745+
rayon_core::tlv::with(value, f)
1746+
}
1747+
1748+
/// Gets Rayon's thread local variable which is preserved for Rayon jobs.
1749+
/// This is used to get the pointer to the current ImplicitCtxt.
1750+
#[cfg(parallel_queries)]
1751+
fn get_tlv() -> usize {
1752+
rayon_core::tlv::get()
1753+
}
1754+
1755+
/// A thread local variable which stores a pointer to the current ImplicitCtxt
1756+
#[cfg(not(parallel_queries))]
17361757
thread_local!(static TLV: Cell<usize> = Cell::new(0));
17371758

1759+
/// Sets TLV to `value` during the call to `f`.
1760+
/// It is restored to its previous value after.
1761+
/// This is used to set the pointer to the new ImplicitCtxt.
1762+
#[cfg(not(parallel_queries))]
17381763
fn set_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
17391764
let old = get_tlv();
17401765
let _reset = OnDrop(move || TLV.with(|tlv| tlv.set(old)));
17411766
TLV.with(|tlv| tlv.set(value));
17421767
f()
17431768
}
17441769

1770+
/// This is used to get the pointer to the current ImplicitCtxt.
1771+
#[cfg(not(parallel_queries))]
17451772
fn get_tlv() -> usize {
17461773
TLV.with(|tlv| tlv.get())
17471774
}
@@ -1810,6 +1837,15 @@ pub mod tls {
18101837
where F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'gcx>) -> R
18111838
{
18121839
with_thread_locals(|| {
1840+
// Update GCX_PTR to indicate there's a GlobalCtxt available
1841+
GCX_PTR.with(|lock| {
1842+
*lock.lock() = gcx as *const _ as usize;
1843+
});
1844+
// Set GCX_PTR back to 0 when we exit
1845+
let _on_drop = OnDrop(move || {
1846+
GCX_PTR.with(|lock| *lock.lock() = 0);
1847+
});
1848+
18131849
let tcx = TyCtxt {
18141850
gcx,
18151851
interners: &gcx.global_interners,
@@ -1826,6 +1862,32 @@ pub mod tls {
18261862
})
18271863
}
18281864

1865+
/// Stores a pointer to the GlobalCtxt if one is available.
1866+
/// This is used to access the GlobalCtxt in the deadlock handler
1867+
/// given to Rayon.
1868+
scoped_thread_local!(pub static GCX_PTR: Lock<usize>);
1869+
1870+
/// Creates a TyCtxt and ImplicitCtxt based on the GCX_PTR thread local.
1871+
/// This is used in the deadlock handler.
1872+
pub unsafe fn with_global<F, R>(f: F) -> R
1873+
where F: for<'a, 'gcx, 'tcx> FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
1874+
{
1875+
let gcx = GCX_PTR.with(|lock| *lock.lock());
1876+
assert!(gcx != 0);
1877+
let gcx = &*(gcx as *const GlobalCtxt<'_>);
1878+
let tcx = TyCtxt {
1879+
gcx,
1880+
interners: &gcx.global_interners,
1881+
};
1882+
let icx = ImplicitCtxt {
1883+
query: None,
1884+
tcx,
1885+
layout_depth: 0,
1886+
task: &OpenTask::Ignore,
1887+
};
1888+
enter_context(&icx, |_| f(tcx))
1889+
}
1890+
18291891
/// Allows access to the current ImplicitCtxt in a closure if one is available
18301892
pub fn with_context_opt<F, R>(f: F) -> R
18311893
where F: for<'a, 'gcx, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'gcx, 'tcx>>) -> R

0 commit comments

Comments
 (0)