@@ -906,6 +906,7 @@ fn load_query_result_cache<'tcx>(
906906}
907907
908908pub fn default_provide ( providers : & mut ty:: query:: Providers < ' _ > ) {
909+ providers. ongoing_codegen = ongoing_codegen;
909910 providers. analysis = analysis;
910911 providers. hir_map = hir_map;
911912 providers. lower_ast_to_hir = lower_ast_to_hir;
@@ -963,7 +964,6 @@ impl BoxedGlobalCtxt {
963964pub fn create_global_ctxt (
964965 compiler : & Compiler ,
965966 io : InputsAndOutputs ,
966- tx : mpsc:: Sender < Box < dyn Any + Send > > ,
967967) -> BoxedGlobalCtxt {
968968 let sess = compiler. session ( ) . clone ( ) ;
969969 let cstore = compiler. cstore . clone ( ) ;
@@ -985,6 +985,13 @@ pub fn create_global_ctxt(
985985 default_provide_extern ( & mut extern_providers) ;
986986 codegen_backend. provide_extern ( & mut extern_providers) ;
987987
988+ // Move the dyn Any coercion outside the generator to avoid lifetime issues
989+ fn codegen_backend_any (
990+ i : Arc < dyn CodegenBackend + Send + Sync >
991+ ) -> Box < dyn Any + Send + Sync > {
992+ Box :: new ( i)
993+ }
994+
988995 let gcx = TyCtxt :: create_global_ctxt (
989996 sess,
990997 & * * cstore,
@@ -993,7 +1000,7 @@ pub fn create_global_ctxt(
9931000 extern_providers,
9941001 & arenas,
9951002 crate_name,
996- tx ,
1003+ codegen_backend_any ( codegen_backend . clone ( ) ) ,
9971004 io,
9981005 ) ;
9991006
@@ -1211,12 +1218,23 @@ fn encode_and_write_metadata(
12111218
12121219/// Runs the codegen backend, after which the AST and analysis can
12131220/// be discarded.
1214- pub fn start_codegen < ' tcx > (
1215- codegen_backend : & dyn CodegenBackend ,
1221+ fn ongoing_codegen < ' tcx > (
12161222 tcx : TyCtxt < ' tcx > ,
1217- rx : mpsc:: Receiver < Box < dyn Any + Send > > ,
1218- outputs : & OutputFilenames ,
1219- ) -> Box < dyn Any > {
1223+ cnum : CrateNum ,
1224+ ) -> Result < Lrc < ty:: OngoingCodegen > > {
1225+ tcx. analysis ( cnum) ?;
1226+
1227+ assert_eq ! ( cnum, LOCAL_CRATE ) ;
1228+ // Don't do code generation if there were any errors
1229+ tcx. sess . compile_status ( ) ?;
1230+
1231+ let outputs = tcx. prepare_outputs ( ( ) ) ?;
1232+
1233+ let rx = OneThread :: into_inner ( tcx. rx_to_llvm_workers . steal ( ) ) ;
1234+ let codegen_backend: & dyn Any = & * tcx. codegen_backend ;
1235+ let codegen_backend = codegen_backend. downcast_ref :: < Arc < dyn CodegenBackend + Send + Sync > > ( )
1236+ . unwrap ( ) ;
1237+
12201238 if log_enabled ! ( :: log:: Level :: Info ) {
12211239 println ! ( "Pre-codegen" ) ;
12221240 tcx. print_debug_stats ( ) ;
@@ -1227,7 +1245,7 @@ pub fn start_codegen<'tcx>(
12271245 } ) ;
12281246
12291247 let ( metadata, need_metadata_module) = time ( tcx. sess , "metadata encoding and writing" , || {
1230- encode_and_write_metadata ( tcx, outputs)
1248+ encode_and_write_metadata ( tcx, & outputs)
12311249 } ) ;
12321250
12331251 tcx. sess . profiler ( |p| p. start_activity ( "codegen crate" ) ) ;
@@ -1242,11 +1260,15 @@ pub fn start_codegen<'tcx>(
12421260 }
12431261
12441262 if tcx. sess . opts . output_types . contains_key ( & OutputType :: Mir ) {
1245- if let Err ( e) = mir:: transform:: dump_mir:: emit_mir ( tcx, outputs) {
1263+ if let Err ( e) = mir:: transform:: dump_mir:: emit_mir ( tcx, & outputs) {
12461264 tcx. sess . err ( & format ! ( "could not emit MIR: {}" , e) ) ;
12471265 tcx. sess . abort_if_errors ( ) ;
12481266 }
12491267 }
12501268
1251- codegen
1269+ Ok ( Lrc :: new ( ty:: OngoingCodegen {
1270+ outputs,
1271+ dep_graph : tcx. dep_graph ( ) . clone ( ) ,
1272+ codegen_object : Steal :: new ( OneThread :: new ( codegen) ) ,
1273+ } ) )
12521274}
0 commit comments