Skip to main content

rustc_data_structures/sync/
parallel.rs

1//! This module defines parallel operations that are implemented in
2//! one way for the serial compiler, and another way the parallel compiler.
3
4use std::any::Any;
5use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
6
7use parking_lot::Mutex;
8
9use crate::FatalErrorMarker;
10use crate::sync::{DynSend, DynSync, FromDyn, IntoDynSyncSend, mode};
11
12/// A guard used to hold panics that occur during a parallel section to later by unwound.
13/// This is used for the parallel compiler to prevent fatal errors from non-deterministically
14/// hiding errors by ensuring that everything in the section has completed executing before
15/// continuing with unwinding. It's also used for the non-parallel code to ensure error message
16/// output match the parallel compiler for testing purposes.
17pub struct ParallelGuard {
18    panic: Mutex<Option<IntoDynSyncSend<Box<dyn Any + Send + 'static>>>>,
19}
20
21impl ParallelGuard {
22    pub fn run<R>(&self, f: impl FnOnce() -> R) -> Option<R> {
23        catch_unwind(AssertUnwindSafe(f))
24            .map_err(|err| {
25                let mut panic = self.panic.lock();
26                if panic.is_none() || !(*err).is::<FatalErrorMarker>() {
27                    *panic = Some(IntoDynSyncSend(err));
28                }
29            })
30            .ok()
31    }
32}
33
34/// This gives access to a fresh parallel guard in the closure and will unwind any panics
35/// caught in it after the closure returns.
36#[inline]
37pub fn parallel_guard<R>(f: impl FnOnce(&ParallelGuard) -> R) -> R {
38    let guard = ParallelGuard { panic: Mutex::new(None) };
39    let ret = f(&guard);
40    if let Some(IntoDynSyncSend(panic)) = guard.panic.into_inner() {
41        resume_unwind(panic);
42    }
43    ret
44}
45
46fn serial_join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
47where
48    A: FnOnce() -> RA,
49    B: FnOnce() -> RB,
50{
51    let (a, b) = parallel_guard(|guard| {
52        let a = guard.run(oper_a);
53        let b = guard.run(oper_b);
54        (a, b)
55    });
56    (a.unwrap(), b.unwrap())
57}
58
59pub fn spawn(func: impl FnOnce() + DynSend + 'static) {
60    if mode::is_dyn_thread_safe() {
61        let func = FromDyn::from(func);
62        rustc_thread_pool::spawn(|| {
63            (func.into_inner())();
64        });
65    } else {
66        func()
67    }
68}
69
70/// Runs the functions in parallel.
71///
72/// The first function is executed immediately on the current thread.
73/// Use that for the longest running function for better scheduling.
74pub fn par_fns(funcs: &mut [&mut (dyn FnMut() + DynSend)]) {
75    parallel_guard(|guard: &ParallelGuard| {
76        if mode::is_dyn_thread_safe() {
77            let funcs = FromDyn::from(funcs);
78            rustc_thread_pool::scope(|s| {
79                let Some((first, rest)) = funcs.into_inner().split_at_mut_checked(1) else {
80                    return;
81                };
82
83                // Reverse the order of the later functions since Rayon executes them in reverse
84                // order when using a single thread. This ensures the execution order matches
85                // that of a single threaded rustc.
86                for f in rest.iter_mut().rev() {
87                    let f = FromDyn::from(f);
88                    s.spawn(|_| {
89                        guard.run(|| (f.into_inner())());
90                    });
91                }
92
93                // Run the first function without spawning to
94                // ensure it executes immediately on this thread.
95                guard.run(|| first[0]());
96            });
97        } else {
98            for f in funcs {
99                guard.run(|| f());
100            }
101        }
102    });
103}
104
105#[inline]
106pub fn par_join<A, B, RA: DynSend, RB: DynSend>(oper_a: A, oper_b: B) -> (RA, RB)
107where
108    A: FnOnce() -> RA + DynSend,
109    B: FnOnce() -> RB + DynSend,
110{
111    if mode::is_dyn_thread_safe() {
112        let oper_a = FromDyn::from(oper_a);
113        let oper_b = FromDyn::from(oper_b);
114        let (a, b) = parallel_guard(|guard| {
115            rustc_thread_pool::join(
116                move || guard.run(move || FromDyn::from(oper_a.into_inner()())),
117                move || guard.run(move || FromDyn::from(oper_b.into_inner()())),
118            )
119        });
120        (a.unwrap().into_inner(), b.unwrap().into_inner())
121    } else {
122        serial_join(oper_a, oper_b)
123    }
124}
125
126fn par_slice<I: DynSend>(
127    items: &mut [I],
128    guard: &ParallelGuard,
129    for_each: impl Fn(&mut I) + DynSync + DynSend,
130) {
131    let for_each = FromDyn::from(for_each);
132    let mut items = for_each.derive(items);
133    rustc_thread_pool::scope(|s| {
134        let proof = items.derive(());
135        let group_size = std::cmp::max(items.len() / 128, 1);
136        for group in items.chunks_mut(group_size) {
137            let group = proof.derive(group);
138            s.spawn(|_| {
139                let mut group = group;
140                for i in group.iter_mut() {
141                    guard.run(|| for_each(i));
142                }
143            });
144        }
145    });
146}
147
148pub fn par_for_each_in<I: DynSend, T: IntoIterator<Item = I>>(
149    t: T,
150    for_each: impl Fn(&I) + DynSync + DynSend,
151) {
152    parallel_guard(|guard| {
153        if mode::is_dyn_thread_safe() {
154            let mut items: Vec<_> = t.into_iter().collect();
155            par_slice(&mut items, guard, |i| for_each(&*i))
156        } else {
157            t.into_iter().for_each(|i| {
158                guard.run(|| for_each(&i));
159            });
160        }
161    });
162}
163
164/// This runs `for_each` in parallel for each iterator item. If one or more of the
165/// `for_each` calls returns `Err`, the function will also return `Err`. The error returned
166/// will be non-deterministic, but this is expected to be used with `ErrorGuaranteed` which
167/// are all equivalent.
168pub fn try_par_for_each_in<T: IntoIterator, E: DynSend>(
169    t: T,
170    for_each: impl Fn(&<T as IntoIterator>::Item) -> Result<(), E> + DynSync + DynSend,
171) -> Result<(), E>
172where
173    <T as IntoIterator>::Item: DynSend,
174{
175    parallel_guard(|guard| {
176        if mode::is_dyn_thread_safe() {
177            let mut items: Vec<_> = t.into_iter().collect();
178
179            let error = Mutex::new(None);
180
181            par_slice(&mut items, guard, |i| {
182                if let Err(err) = for_each(&*i) {
183                    *error.lock() = Some(err);
184                }
185            });
186
187            if let Some(err) = error.into_inner() { Err(err) } else { Ok(()) }
188        } else {
189            t.into_iter().filter_map(|i| guard.run(|| for_each(&i))).fold(Ok(()), Result::and)
190        }
191    })
192}
193
194pub fn par_map<I: DynSend, T: IntoIterator<Item = I>, R: DynSend, C: FromIterator<R>>(
195    t: T,
196    map: impl Fn(I) -> R + DynSync + DynSend,
197) -> C {
198    parallel_guard(|guard| {
199        if mode::is_dyn_thread_safe() {
200            let map = FromDyn::from(map);
201
202            let mut items: Vec<(Option<I>, Option<R>)> =
203                t.into_iter().map(|i| (Some(i), None)).collect();
204
205            par_slice(&mut items, guard, |i| {
206                i.1 = Some(map(i.0.take().unwrap()));
207            });
208
209            items.into_iter().filter_map(|i| i.1).collect()
210        } else {
211            t.into_iter().filter_map(|i| guard.run(|| map(i))).collect()
212        }
213    })
214}
215
216pub fn broadcast<R: DynSend>(op: impl Fn(usize) -> R + DynSync) -> Vec<R> {
217    if mode::is_dyn_thread_safe() {
218        let op = FromDyn::from(op);
219        let results = rustc_thread_pool::broadcast(|context| op.derive(op(context.index())));
220        results.into_iter().map(|r| r.into_inner()).collect()
221    } else {
222        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [op(0)]))vec![op(0)]
223    }
224}