@@ -68,7 +68,7 @@ use rustc_hir::lang_items::LangItem;
68
68
use rustc_hir:: { CoroutineDesugaring , CoroutineKind } ;
69
69
use rustc_index:: bit_set:: { BitMatrix , DenseBitSet , GrowableBitSet } ;
70
70
use rustc_index:: { Idx , IndexVec } ;
71
- use rustc_middle:: mir:: visit:: { MutVisitor , PlaceContext , Visitor } ;
71
+ use rustc_middle:: mir:: visit:: { MutVisitor , MutatingUseContext , PlaceContext , Visitor } ;
72
72
use rustc_middle:: mir:: * ;
73
73
use rustc_middle:: ty:: util:: Discr ;
74
74
use rustc_middle:: ty:: {
@@ -111,6 +111,8 @@ impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor<'tcx> {
111
111
fn visit_local ( & mut self , local : & mut Local , _: PlaceContext , _: Location ) {
112
112
if * local == self . from {
113
113
* local = self . to ;
114
+ } else if * local == self . to {
115
+ * local = self . from ;
114
116
}
115
117
}
116
118
@@ -160,13 +162,15 @@ impl<'tcx> MutVisitor<'tcx> for SelfArgVisitor<'tcx> {
160
162
}
161
163
}
162
164
165
+ #[ tracing:: instrument( level = "trace" , skip( tcx) ) ]
163
166
fn replace_base < ' tcx > ( place : & mut Place < ' tcx > , new_base : Place < ' tcx > , tcx : TyCtxt < ' tcx > ) {
164
167
place. local = new_base. local ;
165
168
166
169
let mut new_projection = new_base. projection . to_vec ( ) ;
167
170
new_projection. append ( & mut place. projection . to_vec ( ) ) ;
168
171
169
172
place. projection = tcx. mk_place_elems ( & new_projection) ;
173
+ tracing:: trace!( ?place) ;
170
174
}
171
175
172
176
const SELF_ARG : Local = Local :: from_u32 ( 1 ) ;
@@ -205,8 +209,8 @@ struct TransformVisitor<'tcx> {
205
209
// The set of locals that have no `StorageLive`/`StorageDead` annotations.
206
210
always_live_locals : DenseBitSet < Local > ,
207
211
208
- // The original RETURN_PLACE local
209
- old_ret_local : Local ,
212
+ // New local we just create to hold the `CoroutineState` value.
213
+ new_ret_local : Local ,
210
214
211
215
old_yield_ty : Ty < ' tcx > ,
212
216
@@ -271,6 +275,7 @@ impl<'tcx> TransformVisitor<'tcx> {
271
275
// `core::ops::CoroutineState` only has single element tuple variants,
272
276
// so we can just write to the downcasted first field and then set the
273
277
// discriminant to the appropriate variant.
278
+ #[ tracing:: instrument( level = "trace" , skip( self , statements) ) ]
274
279
fn make_state (
275
280
& self ,
276
281
val : Operand < ' tcx > ,
@@ -344,11 +349,12 @@ impl<'tcx> TransformVisitor<'tcx> {
344
349
345
350
statements. push ( Statement :: new (
346
351
source_info,
347
- StatementKind :: Assign ( Box :: new ( ( Place :: return_place ( ) , rvalue) ) ) ,
352
+ StatementKind :: Assign ( Box :: new ( ( self . new_ret_local . into ( ) , rvalue) ) ) ,
348
353
) ) ;
349
354
}
350
355
351
356
// Create a Place referencing a coroutine struct field
357
+ #[ tracing:: instrument( level = "trace" , skip( self ) , ret) ]
352
358
fn make_field ( & self , variant_index : VariantIdx , idx : FieldIdx , ty : Ty < ' tcx > ) -> Place < ' tcx > {
353
359
let self_place = Place :: from ( SELF_ARG ) ;
354
360
let base = self . tcx . mk_place_downcast_unnamed ( self_place, variant_index) ;
@@ -359,6 +365,7 @@ impl<'tcx> TransformVisitor<'tcx> {
359
365
}
360
366
361
367
// Create a statement which changes the discriminant
368
+ #[ tracing:: instrument( level = "trace" , skip( self ) ) ]
362
369
fn set_discr ( & self , state_disc : VariantIdx , source_info : SourceInfo ) -> Statement < ' tcx > {
363
370
let self_place = Place :: from ( SELF_ARG ) ;
364
371
Statement :: new (
@@ -371,6 +378,7 @@ impl<'tcx> TransformVisitor<'tcx> {
371
378
}
372
379
373
380
// Create a statement which reads the discriminant into a temporary
381
+ #[ tracing:: instrument( level = "trace" , skip( self , body) ) ]
374
382
fn get_discr ( & self , body : & mut Body < ' tcx > ) -> ( Statement < ' tcx > , Place < ' tcx > ) {
375
383
let temp_decl = LocalDecl :: new ( self . discr_ty , body. span ) ;
376
384
let local_decls_len = body. local_decls . push ( temp_decl) ;
@@ -383,29 +391,48 @@ impl<'tcx> TransformVisitor<'tcx> {
383
391
) ;
384
392
( assign, temp)
385
393
}
394
+
395
+ /// Allocates a new local and replaces all references of `local` with it. Returns the new local.
396
+ ///
397
+ /// `local` will be changed to a new local decl with type `ty`.
398
+ ///
399
+ /// Note that the new local will be uninitialized. It is the caller's responsibility to assign some
400
+ /// valid value to it before its first use.
401
+ #[ tracing:: instrument( level = "trace" , skip( self , body) ) ]
402
+ fn replace_local ( & mut self , local : Local , new_local : Local , body : & mut Body < ' tcx > ) -> Local {
403
+ body. local_decls . swap ( local, new_local) ;
404
+
405
+ let mut visitor = RenameLocalVisitor { from : local, to : new_local, tcx : self . tcx } ;
406
+ visitor. visit_body ( body) ;
407
+ for suspension in & mut self . suspension_points {
408
+ let ctxt = PlaceContext :: MutatingUse ( MutatingUseContext :: Yield ) ;
409
+ let location = Location { block : START_BLOCK , statement_index : 0 } ;
410
+ visitor. visit_place ( & mut suspension. resume_arg , ctxt, location) ;
411
+ }
412
+
413
+ new_local
414
+ }
386
415
}
387
416
388
417
impl < ' tcx > MutVisitor < ' tcx > for TransformVisitor < ' tcx > {
389
418
fn tcx ( & self ) -> TyCtxt < ' tcx > {
390
419
self . tcx
391
420
}
392
421
393
- fn visit_local ( & mut self , local : & mut Local , _: PlaceContext , _: Location ) {
422
+ #[ tracing:: instrument( level = "trace" , skip( self ) , ret) ]
423
+ fn visit_local ( & mut self , local : & mut Local , _: PlaceContext , _location : Location ) {
394
424
assert ! ( !self . remap. contains( * local) ) ;
395
425
}
396
426
397
- fn visit_place (
398
- & mut self ,
399
- place : & mut Place < ' tcx > ,
400
- _context : PlaceContext ,
401
- _location : Location ,
402
- ) {
427
+ #[ tracing:: instrument( level = "trace" , skip( self ) , ret) ]
428
+ fn visit_place ( & mut self , place : & mut Place < ' tcx > , _: PlaceContext , _location : Location ) {
403
429
// Replace an Local in the remap with a coroutine struct access
404
430
if let Some ( & Some ( ( ty, variant_index, idx) ) ) = self . remap . get ( place. local ) {
405
431
replace_base ( place, self . make_field ( variant_index, idx, ty) , self . tcx ) ;
406
432
}
407
433
}
408
434
435
+ #[ tracing:: instrument( level = "trace" , skip( self , data) , ret) ]
409
436
fn visit_basic_block_data ( & mut self , block : BasicBlock , data : & mut BasicBlockData < ' tcx > ) {
410
437
// Remove StorageLive and StorageDead statements for remapped locals
411
438
for s in & mut data. statements {
@@ -416,29 +443,35 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
416
443
}
417
444
}
418
445
419
- let ret_val = match data. terminator ( ) . kind {
446
+ for ( statement_index, statement) in data. statements . iter_mut ( ) . enumerate ( ) {
447
+ let location = Location { block, statement_index } ;
448
+ self . visit_statement ( statement, location) ;
449
+ }
450
+
451
+ let location = Location { block, statement_index : data. statements . len ( ) } ;
452
+ let mut terminator = data. terminator . take ( ) . unwrap ( ) ;
453
+ let source_info = terminator. source_info ;
454
+ match terminator. kind {
420
455
TerminatorKind :: Return => {
421
- Some ( ( true , None , Operand :: Move ( Place :: from ( self . old_ret_local ) ) , None ) )
422
- }
423
- TerminatorKind :: Yield { ref value, resume, resume_arg, drop } => {
424
- Some ( ( false , Some ( ( resume, resume_arg) ) , value. clone ( ) , drop) )
456
+ let mut v = Operand :: Move ( Place :: return_place ( ) ) ;
457
+ self . visit_operand ( & mut v, location) ;
458
+ // We must assign the value first in case it gets declared dead below
459
+ self . make_state ( v, source_info, true , & mut data. statements ) ;
460
+ // State for returned
461
+ let state = VariantIdx :: new ( CoroutineArgs :: RETURNED ) ;
462
+ data. statements . push ( self . set_discr ( state, source_info) ) ;
463
+ terminator. kind = TerminatorKind :: Return ;
425
464
}
426
- _ => None ,
427
- } ;
428
-
429
- if let Some ( ( is_return, resume, v, drop) ) = ret_val {
430
- let source_info = data. terminator ( ) . source_info ;
431
- // We must assign the value first in case it gets declared dead below
432
- self . make_state ( v, source_info, is_return, & mut data. statements ) ;
433
- let state = if let Some ( ( resume, mut resume_arg) ) = resume {
434
- // Yield
435
- let state = CoroutineArgs :: RESERVED_VARIANTS + self . suspension_points . len ( ) ;
436
-
465
+ TerminatorKind :: Yield { mut value, resume, mut resume_arg, drop } => {
437
466
// The resume arg target location might itself be remapped if its base local is
438
467
// live across a yield.
439
- if let Some ( & Some ( ( ty, variant, idx) ) ) = self . remap . get ( resume_arg. local ) {
440
- replace_base ( & mut resume_arg, self . make_field ( variant, idx, ty) , self . tcx ) ;
441
- }
468
+ self . visit_operand ( & mut value, location) ;
469
+ let ctxt = PlaceContext :: MutatingUse ( MutatingUseContext :: Yield ) ;
470
+ self . visit_place ( & mut resume_arg, ctxt, location) ;
471
+ // We must assign the value first in case it gets declared dead below
472
+ self . make_state ( value. clone ( ) , source_info, false , & mut data. statements ) ;
473
+ // Yield
474
+ let state = CoroutineArgs :: RESERVED_VARIANTS + self . suspension_points . len ( ) ;
442
475
443
476
let storage_liveness: GrowableBitSet < Local > =
444
477
self . storage_liveness [ block] . clone ( ) . unwrap ( ) . into ( ) ;
@@ -453,7 +486,6 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
453
486
. push ( Statement :: new ( source_info, StatementKind :: StorageDead ( l) ) ) ;
454
487
}
455
488
}
456
-
457
489
self . suspension_points . push ( SuspensionPoint {
458
490
state,
459
491
resume,
@@ -462,16 +494,13 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
462
494
storage_liveness,
463
495
} ) ;
464
496
465
- VariantIdx :: new ( state)
466
- } else {
467
- // Return
468
- VariantIdx :: new ( CoroutineArgs :: RETURNED ) // state for returned
469
- } ;
470
- data. statements . push ( self . set_discr ( state, source_info) ) ;
471
- data. terminator_mut ( ) . kind = TerminatorKind :: Return ;
472
- }
473
-
474
- self . super_basic_block_data ( block, data) ;
497
+ let state = VariantIdx :: new ( state) ;
498
+ data. statements . push ( self . set_discr ( state, source_info) ) ;
499
+ terminator. kind = TerminatorKind :: Return ;
500
+ }
501
+ _ => self . visit_terminator ( & mut terminator, location) ,
502
+ } ;
503
+ data. terminator = Some ( terminator) ;
475
504
}
476
505
}
477
506
@@ -484,6 +513,7 @@ fn make_aggregate_adt<'tcx>(
484
513
Rvalue :: Aggregate ( Box :: new ( AggregateKind :: Adt ( def_id, variant_idx, args, None , None ) ) , operands)
485
514
}
486
515
516
+ #[ tracing:: instrument( level = "trace" , skip( tcx, body) ) ]
487
517
fn make_coroutine_state_argument_indirect < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
488
518
let coroutine_ty = body. local_decls . raw [ 1 ] . ty ;
489
519
@@ -496,6 +526,7 @@ fn make_coroutine_state_argument_indirect<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Bo
496
526
SelfArgVisitor :: new ( tcx, ProjectionElem :: Deref ) . visit_body ( body) ;
497
527
}
498
528
529
+ #[ tracing:: instrument( level = "trace" , skip( tcx, body) ) ]
499
530
fn make_coroutine_state_argument_pinned < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
500
531
let ref_coroutine_ty = body. local_decls . raw [ 1 ] . ty ;
501
532
@@ -512,27 +543,6 @@ fn make_coroutine_state_argument_pinned<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body
512
543
. visit_body ( body) ;
513
544
}
514
545
515
- /// Allocates a new local and replaces all references of `local` with it. Returns the new local.
516
- ///
517
- /// `local` will be changed to a new local decl with type `ty`.
518
- ///
519
- /// Note that the new local will be uninitialized. It is the caller's responsibility to assign some
520
- /// valid value to it before its first use.
521
- fn replace_local < ' tcx > (
522
- local : Local ,
523
- ty : Ty < ' tcx > ,
524
- body : & mut Body < ' tcx > ,
525
- tcx : TyCtxt < ' tcx > ,
526
- ) -> Local {
527
- let new_decl = LocalDecl :: new ( ty, body. span ) ;
528
- let new_local = body. local_decls . push ( new_decl) ;
529
- body. local_decls . swap ( local, new_local) ;
530
-
531
- RenameLocalVisitor { from : local, to : new_local, tcx } . visit_body ( body) ;
532
-
533
- new_local
534
- }
535
-
536
546
/// Transforms the `body` of the coroutine applying the following transforms:
537
547
///
538
548
/// - Eliminates all the `get_context` calls that async lowering created.
@@ -554,6 +564,7 @@ fn replace_local<'tcx>(
554
564
/// The async lowering step and the type / lifetime inference / checking are
555
565
/// still using the `ResumeTy` indirection for the time being, and that indirection
556
566
/// is removed here. After this transform, the coroutine body only knows about `&mut Context<'_>`.
567
+ #[ tracing:: instrument( level = "trace" , skip( tcx, body) , ret) ]
557
568
fn transform_async_context < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) -> Ty < ' tcx > {
558
569
let context_mut_ref = Ty :: new_task_context ( tcx) ;
559
570
@@ -607,6 +618,7 @@ fn eliminate_get_context_call<'tcx>(bb_data: &mut BasicBlockData<'tcx>) -> Local
607
618
}
608
619
609
620
#[ cfg_attr( not( debug_assertions) , allow( unused) ) ]
621
+ #[ tracing:: instrument( level = "trace" , skip( tcx, body) , ret) ]
610
622
fn replace_resume_ty_local < ' tcx > (
611
623
tcx : TyCtxt < ' tcx > ,
612
624
body : & mut Body < ' tcx > ,
@@ -617,7 +629,7 @@ fn replace_resume_ty_local<'tcx>(
617
629
// We have to replace the `ResumeTy` that is used for type and borrow checking
618
630
// with `&mut Context<'_>` in MIR.
619
631
#[ cfg( debug_assertions) ]
620
- {
632
+ if local_ty != context_mut_ref {
621
633
if let ty:: Adt ( resume_ty_adt, _) = local_ty. kind ( ) {
622
634
let expected_adt = tcx. adt_def ( tcx. require_lang_item ( LangItem :: ResumeTy , body. span ) ) ;
623
635
assert_eq ! ( * resume_ty_adt, expected_adt) ;
@@ -671,6 +683,7 @@ struct LivenessInfo {
671
683
/// case none exist, the local is considered to be always live.
672
684
/// - a local has to be stored if it is either directly used after the
673
685
/// the suspend point, or if it is live and has been previously borrowed.
686
+ #[ tracing:: instrument( level = "trace" , skip( tcx, body) ) ]
674
687
fn locals_live_across_suspend_points < ' tcx > (
675
688
tcx : TyCtxt < ' tcx > ,
676
689
body : & Body < ' tcx > ,
@@ -946,6 +959,7 @@ impl StorageConflictVisitor<'_, '_> {
946
959
}
947
960
}
948
961
962
+ #[ tracing:: instrument( level = "trace" , skip( liveness, body) ) ]
949
963
fn compute_layout < ' tcx > (
950
964
liveness : LivenessInfo ,
951
965
body : & Body < ' tcx > ,
@@ -1050,7 +1064,9 @@ fn compute_layout<'tcx>(
1050
1064
variant_source_info,
1051
1065
storage_conflicts,
1052
1066
} ;
1067
+ debug ! ( ?remap) ;
1053
1068
debug ! ( ?layout) ;
1069
+ debug ! ( ?storage_liveness) ;
1054
1070
1055
1071
( remap, layout, storage_liveness)
1056
1072
}
@@ -1222,6 +1238,7 @@ fn generate_poison_block_and_redirect_unwinds_there<'tcx>(
1222
1238
}
1223
1239
}
1224
1240
1241
+ #[ tracing:: instrument( level = "trace" , skip( tcx, transform, body) ) ]
1225
1242
fn create_coroutine_resume_function < ' tcx > (
1226
1243
tcx : TyCtxt < ' tcx > ,
1227
1244
transform : TransformVisitor < ' tcx > ,
@@ -1300,7 +1317,7 @@ fn create_coroutine_resume_function<'tcx>(
1300
1317
}
1301
1318
1302
1319
/// An operation that can be performed on a coroutine.
1303
- #[ derive( PartialEq , Copy , Clone ) ]
1320
+ #[ derive( PartialEq , Copy , Clone , Debug ) ]
1304
1321
enum Operation {
1305
1322
Resume ,
1306
1323
Drop ,
@@ -1315,6 +1332,7 @@ impl Operation {
1315
1332
}
1316
1333
}
1317
1334
1335
+ #[ tracing:: instrument( level = "trace" , skip( transform, body) ) ]
1318
1336
fn create_cases < ' tcx > (
1319
1337
body : & mut Body < ' tcx > ,
1320
1338
transform : & TransformVisitor < ' tcx > ,
@@ -1446,6 +1464,8 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
1446
1464
// This only applies to coroutines
1447
1465
return ;
1448
1466
} ;
1467
+ tracing:: trace!( def_id = ?body. source. def_id( ) ) ;
1468
+
1449
1469
let old_ret_ty = body. return_ty ( ) ;
1450
1470
1451
1471
assert ! ( body. coroutine_drop( ) . is_none( ) && body. coroutine_drop_async( ) . is_none( ) ) ;
@@ -1492,10 +1512,6 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
1492
1512
}
1493
1513
} ;
1494
1514
1495
- // We rename RETURN_PLACE which has type mir.return_ty to old_ret_local
1496
- // RETURN_PLACE then is a fresh unused local with type ret_ty.
1497
- let old_ret_local = replace_local ( RETURN_PLACE , new_ret_ty, body, tcx) ;
1498
-
1499
1515
// We need to insert clean drop for unresumed state and perform drop elaboration
1500
1516
// (finally in open_drop_for_tuple) before async drop expansion.
1501
1517
// Async drops, produced by this drop elaboration, will be expanded,
@@ -1520,6 +1536,11 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
1520
1536
cleanup_async_drops ( body) ;
1521
1537
}
1522
1538
1539
+ // We rename RETURN_PLACE which has type mir.return_ty to new_ret_local
1540
+ // RETURN_PLACE then is a fresh unused local with type ret_ty.
1541
+ let new_ret_local = body. local_decls . push ( LocalDecl :: new ( new_ret_ty, body. span ) ) ;
1542
+ tracing:: trace!( ?new_ret_local) ;
1543
+
1523
1544
let always_live_locals = always_storage_live_locals ( body) ;
1524
1545
let movable = coroutine_kind. movability ( ) == hir:: Movability :: Movable ;
1525
1546
let liveness_info =
@@ -1554,13 +1575,16 @@ impl<'tcx> crate::MirPass<'tcx> for StateTransform {
1554
1575
storage_liveness,
1555
1576
always_live_locals,
1556
1577
suspension_points : Vec :: new ( ) ,
1557
- old_ret_local,
1558
1578
discr_ty,
1579
+ new_ret_local,
1559
1580
old_ret_ty,
1560
1581
old_yield_ty,
1561
1582
} ;
1562
1583
transform. visit_body ( body) ;
1563
1584
1585
+ // Swap the actual `RETURN_PLACE` and the provisional `new_ret_local`.
1586
+ transform. replace_local ( RETURN_PLACE , new_ret_local, body) ;
1587
+
1564
1588
// MIR parameters are not explicitly assigned-to when entering the MIR body.
1565
1589
// If we want to save their values inside the coroutine state, we need to do so explicitly.
1566
1590
let source_info = SourceInfo :: outermost ( body. span ) ;
0 commit comments