1
- use std:: any:: Any ;
2
1
use std:: assert_matches:: assert_matches;
3
2
use std:: marker:: PhantomData ;
4
3
use std:: path:: { Path , PathBuf } ;
@@ -372,8 +371,6 @@ pub struct CodegenContext<B: WriteBackendMethods> {
372
371
/// The incremental compilation session directory, or None if we are not
373
372
/// compiling incrementally
374
373
pub incr_comp_session_dir : Option < PathBuf > ,
375
- /// Channel back to the main control thread to send messages to
376
- pub coordinator_send : Sender < Box < dyn Any + Send > > ,
377
374
/// `true` if the codegen should be run in parallel.
378
375
///
379
376
/// Depends on [`ExtraBackendMethods::supports_parallel()`] and `-Zno_parallel_backend`.
@@ -1122,10 +1119,10 @@ fn start_executing_work<B: ExtraBackendMethods>(
1122
1119
autodiff_items : & [ AutoDiffItem ] ,
1123
1120
shared_emitter : SharedEmitter ,
1124
1121
codegen_worker_send : Sender < CguMessage > ,
1125
- coordinator_receive : Receiver < Box < dyn Any + Send > > ,
1122
+ coordinator_receive : Receiver < Message < B > > ,
1126
1123
regular_config : Arc < ModuleConfig > ,
1127
1124
allocator_config : Arc < ModuleConfig > ,
1128
- tx_to_llvm_workers : Sender < Box < dyn Any + Send > > ,
1125
+ tx_to_llvm_workers : Sender < Message < B > > ,
1129
1126
) -> thread:: JoinHandle < Result < CompiledModules , ( ) > > {
1130
1127
let coordinator_send = tx_to_llvm_workers;
1131
1128
let sess = tcx. sess ;
@@ -1153,7 +1150,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
1153
1150
let coordinator_send2 = coordinator_send. clone ( ) ;
1154
1151
let helper = jobserver:: client ( )
1155
1152
. into_helper_thread ( move |token| {
1156
- drop ( coordinator_send2. send ( Box :: new ( Message :: Token :: < B > ( token) ) ) ) ;
1153
+ drop ( coordinator_send2. send ( Message :: Token :: < B > ( token) ) ) ;
1157
1154
} )
1158
1155
. expect ( "failed to spawn helper thread" ) ;
1159
1156
@@ -1187,7 +1184,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
1187
1184
remark : sess. opts . cg . remark . clone ( ) ,
1188
1185
remark_dir,
1189
1186
incr_comp_session_dir : sess. incr_comp_session_dir_opt ( ) . map ( |r| r. clone ( ) ) ,
1190
- coordinator_send,
1191
1187
expanded_args : tcx. sess . expanded_args . clone ( ) ,
1192
1188
diag_emitter : shared_emitter. clone ( ) ,
1193
1189
output_filenames : Arc :: clone ( tcx. output_filenames ( ( ) ) ) ,
@@ -1423,7 +1419,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
1423
1419
let ( item, _) =
1424
1420
work_items. pop ( ) . expect ( "queue empty - queue_full_enough() broken?" ) ;
1425
1421
main_thread_state = MainThreadState :: Lending ;
1426
- spawn_work ( & cgcx, & mut llvm_start_time, item) ;
1422
+ spawn_work ( & cgcx, coordinator_send . clone ( ) , & mut llvm_start_time, item) ;
1427
1423
}
1428
1424
}
1429
1425
} else if codegen_state == Completed {
@@ -1502,7 +1498,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
1502
1498
MainThreadState :: Idle => {
1503
1499
if let Some ( ( item, _) ) = work_items. pop ( ) {
1504
1500
main_thread_state = MainThreadState :: Lending ;
1505
- spawn_work ( & cgcx, & mut llvm_start_time, item) ;
1501
+ spawn_work ( & cgcx, coordinator_send . clone ( ) , & mut llvm_start_time, item) ;
1506
1502
} else {
1507
1503
// There is no unstarted work, so let the main thread
1508
1504
// take over for a running worker. Otherwise the
@@ -1538,16 +1534,15 @@ fn start_executing_work<B: ExtraBackendMethods>(
1538
1534
while running_with_own_token < tokens. len ( )
1539
1535
&& let Some ( ( item, _) ) = work_items. pop ( )
1540
1536
{
1541
- spawn_work ( & cgcx, & mut llvm_start_time, item) ;
1537
+ spawn_work ( & cgcx, coordinator_send . clone ( ) , & mut llvm_start_time, item) ;
1542
1538
running_with_own_token += 1 ;
1543
1539
}
1544
1540
}
1545
1541
1546
1542
// Relinquish accidentally acquired extra tokens.
1547
1543
tokens. truncate ( running_with_own_token) ;
1548
1544
1549
- let msg = coordinator_receive. recv ( ) . unwrap ( ) ;
1550
- match * msg. downcast :: < Message < B > > ( ) . ok ( ) . unwrap ( ) {
1545
+ match coordinator_receive. recv ( ) . unwrap ( ) {
1551
1546
// Save the token locally and the next turn of the loop will use
1552
1547
// this to spawn a new unit of work, or it may get dropped
1553
1548
// immediately if we have no more work to spawn.
@@ -1769,6 +1764,7 @@ pub(crate) struct WorkerFatalError;
1769
1764
1770
1765
fn spawn_work < ' a , B : ExtraBackendMethods > (
1771
1766
cgcx : & ' a CodegenContext < B > ,
1767
+ coordinator_send : Sender < Message < B > > ,
1772
1768
llvm_start_time : & mut Option < VerboseTimingGuard < ' a > > ,
1773
1769
work : WorkItem < B > ,
1774
1770
) {
@@ -1782,7 +1778,7 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
1782
1778
// Set up a destructor which will fire off a message that we're done as
1783
1779
// we exit.
1784
1780
struct Bomb < B : ExtraBackendMethods > {
1785
- coordinator_send : Sender < Box < dyn Any + Send > > ,
1781
+ coordinator_send : Sender < Message < B > > ,
1786
1782
result : Option < Result < WorkItemResult < B > , FatalError > > ,
1787
1783
}
1788
1784
impl < B : ExtraBackendMethods > Drop for Bomb < B > {
@@ -1794,11 +1790,11 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
1794
1790
}
1795
1791
None => Message :: WorkItem :: < B > { result : Err ( None ) } ,
1796
1792
} ;
1797
- drop ( self . coordinator_send . send ( Box :: new ( msg) ) ) ;
1793
+ drop ( self . coordinator_send . send ( msg) ) ;
1798
1794
}
1799
1795
}
1800
1796
1801
- let mut bomb = Bomb :: < B > { coordinator_send : cgcx . coordinator_send . clone ( ) , result : None } ;
1797
+ let mut bomb = Bomb :: < B > { coordinator_send, result : None } ;
1802
1798
1803
1799
// Execute the work itself, and if it finishes successfully then flag
1804
1800
// ourselves as a success as well.
@@ -2003,7 +1999,7 @@ impl SharedEmitterMain {
2003
1999
}
2004
2000
2005
2001
pub struct Coordinator < B : ExtraBackendMethods > {
2006
- pub sender : Sender < Box < dyn Any + Send > > ,
2002
+ sender : Sender < Message < B > > ,
2007
2003
future : Option < thread:: JoinHandle < Result < CompiledModules , ( ) > > > ,
2008
2004
// Only used for the Message type.
2009
2005
phantom : PhantomData < B > ,
@@ -2020,7 +2016,7 @@ impl<B: ExtraBackendMethods> Drop for Coordinator<B> {
2020
2016
if let Some ( future) = self . future . take ( ) {
2021
2017
// If we haven't joined yet, signal to the coordinator that it should spawn no more
2022
2018
// work, and wait for worker threads to finish.
2023
- drop ( self . sender . send ( Box :: new ( Message :: CodegenAborted :: < B > ) ) ) ;
2019
+ drop ( self . sender . send ( Message :: CodegenAborted :: < B > ) ) ;
2024
2020
drop ( future. join ( ) ) ;
2025
2021
}
2026
2022
}
@@ -2079,7 +2075,7 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
2079
2075
pub ( crate ) fn codegen_finished ( & self , tcx : TyCtxt < ' _ > ) {
2080
2076
self . wait_for_signal_to_codegen_item ( ) ;
2081
2077
self . check_for_errors ( tcx. sess ) ;
2082
- drop ( self . coordinator . sender . send ( Box :: new ( Message :: CodegenComplete :: < B > ) ) ) ;
2078
+ drop ( self . coordinator . sender . send ( Message :: CodegenComplete :: < B > ) ) ;
2083
2079
}
2084
2080
2085
2081
pub ( crate ) fn check_for_errors ( & self , sess : & Session ) {
@@ -2100,28 +2096,25 @@ impl<B: ExtraBackendMethods> OngoingCodegen<B> {
2100
2096
}
2101
2097
2102
2098
pub ( crate ) fn submit_codegened_module_to_llvm < B : ExtraBackendMethods > (
2103
- _backend : & B ,
2104
- tx_to_llvm_workers : & Sender < Box < dyn Any + Send > > ,
2099
+ coordinator : & Coordinator < B > ,
2105
2100
module : ModuleCodegen < B :: Module > ,
2106
2101
cost : u64 ,
2107
2102
) {
2108
2103
let llvm_work_item = WorkItem :: Optimize ( module) ;
2109
- drop ( tx_to_llvm_workers . send ( Box :: new ( Message :: CodegenDone :: < B > { llvm_work_item, cost } ) ) ) ;
2104
+ drop ( coordinator . sender . send ( Message :: CodegenDone :: < B > { llvm_work_item, cost } ) ) ;
2110
2105
}
2111
2106
2112
2107
pub ( crate ) fn submit_post_lto_module_to_llvm < B : ExtraBackendMethods > (
2113
- _backend : & B ,
2114
- tx_to_llvm_workers : & Sender < Box < dyn Any + Send > > ,
2108
+ coordinator : & Coordinator < B > ,
2115
2109
module : CachedModuleCodegen ,
2116
2110
) {
2117
2111
let llvm_work_item = WorkItem :: CopyPostLtoArtifacts ( module) ;
2118
- drop ( tx_to_llvm_workers . send ( Box :: new ( Message :: CodegenDone :: < B > { llvm_work_item, cost : 0 } ) ) ) ;
2112
+ drop ( coordinator . sender . send ( Message :: CodegenDone :: < B > { llvm_work_item, cost : 0 } ) ) ;
2119
2113
}
2120
2114
2121
2115
pub ( crate ) fn submit_pre_lto_module_to_llvm < B : ExtraBackendMethods > (
2122
- _backend : & B ,
2123
2116
tcx : TyCtxt < ' _ > ,
2124
- tx_to_llvm_workers : & Sender < Box < dyn Any + Send > > ,
2117
+ coordinator : & Coordinator < B > ,
2125
2118
module : CachedModuleCodegen ,
2126
2119
) {
2127
2120
let filename = pre_lto_bitcode_filename ( & module. name ) ;
@@ -2135,10 +2128,10 @@ pub(crate) fn submit_pre_lto_module_to_llvm<B: ExtraBackendMethods>(
2135
2128
} )
2136
2129
} ;
2137
2130
// Schedule the module to be loaded
2138
- drop ( tx_to_llvm_workers . send ( Box :: new ( Message :: AddImportOnlyModule :: < B > {
2131
+ drop ( coordinator . sender . send ( Message :: AddImportOnlyModule :: < B > {
2139
2132
module_data : SerializedModule :: FromUncompressedFile ( mmap) ,
2140
2133
work_product : module. source ,
2141
- } ) ) ) ;
2134
+ } ) ) ;
2142
2135
}
2143
2136
2144
2137
fn pre_lto_bitcode_filename ( module_name : & str ) -> String {
0 commit comments