Skip to content

Commit e602aa5

Browse files
authored
Merge pull request #34500 from meg-gupta/fixmem2regassert
SILMem2Reg : Delete phi args added by Mem2Reg if there are no uses
2 parents 3b54549 + 39b5ec2 commit e602aa5

File tree

3 files changed

+60
-38
lines changed

3 files changed

+60
-38
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -573,12 +573,8 @@ StackAllocationPromoter::promoteAllocationInBlock(SILBasicBlock *BB) {
573573

574574
// Stop on deallocation.
575575
if (auto *DSI = dyn_cast<DeallocStackInst>(Inst)) {
576-
if (DSI->getOperand() == ASI) {
577-
// Reset LastStore.
578-
// So that we don't pass RunningVal as a phi arg beyond dealloc_stack
579-
LastStore = nullptr;
576+
if (DSI->getOperand() == ASI)
580577
break;
581-
}
582578
}
583579
}
584580
if (LastStore) {
@@ -756,21 +752,6 @@ void StackAllocationPromoter::fixPhiPredBlock(BlockSet &PhiBlocks,
756752
TI->eraseFromParent();
757753
}
758754

759-
static bool hasOnlyUndefIncomingValues(SILPhiArgument *phiArg) {
760-
SmallVector<SILValue, 8> incomingValues;
761-
phiArg->getIncomingPhiValues(incomingValues);
762-
for (auto predArg : incomingValues) {
763-
if (isa<SILUndef>(predArg))
764-
continue;
765-
if (isa<SILPhiArgument>(predArg) &&
766-
hasOnlyUndefIncomingValues(cast<SILPhiArgument>(predArg))) {
767-
continue;
768-
}
769-
return false;
770-
}
771-
return true;
772-
}
773-
774755
void StackAllocationPromoter::fixBranchesAndUses(BlockSet &PhiBlocks) {
775756
// First update uses of the value.
776757
SmallVector<LoadInst *, 4> collectedLoads;
@@ -852,18 +833,13 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSet &PhiBlocks) {
852833
// If the owned phi arg we added did not have any uses, create end_lifetime to
853834
// end its lifetime. In asserts mode, make sure we have only undef incoming
854835
// values for such phi args.
855-
if (ASI->getFunction()->hasOwnership()) {
856836
for (auto Block : PhiBlocks) {
857837
auto *phiArg = cast<SILPhiArgument>(
858838
Block->getArgument(Block->getNumArguments() - 1));
859-
if (phiArg->getOwnershipKind() == ValueOwnershipKind::Owned &&
860-
phiArg->use_empty()) {
861-
assert(hasOnlyUndefIncomingValues(phiArg));
862-
SILBuilderWithScope(&Block->front())
863-
.createEndLifetime(Block->front().getLoc(), phiArg);
839+
if (phiArg->use_empty()) {
840+
erasePhiArgument(Block, Block->getNumArguments() - 1);
864841
}
865842
}
866-
}
867843
}
868844

869845
void StackAllocationPromoter::pruneAllocStackUsage() {

test/SILOptimizer/mem2reg.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,10 @@ bb0(%0 : $Optional<Klass>):
466466
return %4 : $()
467467
}
468468

469+
// check no dead args are passed to bb3
469470
// CHECK-LABEL: sil @multi_basic_block_use_on_one_path :
470471
// CHECK-NOT: alloc_stack
471-
// CHECK:bb2:
472-
// CHECK: br bb3(undef : $Klass)
472+
// CHECK: bb3:
473473
// CHECK-LABEL: } // end sil function 'multi_basic_block_use_on_one_path'
474474
sil @multi_basic_block_use_on_one_path : $@convention(thin) (@owned Klass) -> () {
475475
bb0(%0 : $Klass):

test/SILOptimizer/mem2reg_ossa_nontrivial.sil

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ bb3:
384384
return %res : $()
385385
}
386386

387+
// Test to check no dead args are passed to bb3 as phi arg
387388
// CHECK-LABEL: sil [ossa] @multi_basic_block_stack_deallocated_phiarg :
388389
// CHECK-NOT: alloc_stack
389390
// CHECK-LABEL: bb2:
390-
// CHECK: br bb3(undef : $Klass)
391-
// CHECK: bb3([[PHI:%.*]] : @owned $Klass):
392-
// CHECK-NEXT: end_lifetime [[PHI]] : $Klass
391+
// CHECK: br bb3
392+
// CHECK: bb3:
393393
// CHECK-LABEL: } // end sil function 'multi_basic_block_stack_deallocated_phiarg'
394394
sil [ossa] @multi_basic_block_stack_deallocated_phiarg : $@convention(thin) (@owned Klass) -> () {
395395
bb0(%0 : @owned $Klass):
@@ -410,13 +410,12 @@ bb3:
410410
return %res : $()
411411
}
412412

413+
// Test to check no dead args are passed to bb3 as phi arg
413414
// CHECK-LABEL: sil [ossa] @multi_asi_basic_block_stack_deallocated_phiarg :
414415
// CHECK-NOT: alloc_stack
415416
// CHECK-LABEL: bb2:
416-
// CHECK: br bb3(undef : $Klass, undef : $Klass)
417-
// CHECK: bb3([[PHI1:%.*]] : @owned $Klass, [[PHI2:%.*]] : @owned $Klass):
418-
// CHECK-NEXT: end_lifetime [[PHI2]] : $Klass
419-
// CHECK-NEXT: end_lifetime [[PHI1]] : $Klass
417+
// CHECK: br bb3
418+
// CHECK: bb3:
420419
// CHECK-LABEL: } // end sil function 'multi_asi_basic_block_stack_deallocated_phiarg'
421420
sil [ossa] @multi_asi_basic_block_stack_deallocated_phiarg : $@convention(thin) (@owned Klass, @owned Klass) -> () {
422421
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
@@ -446,8 +445,7 @@ bb3:
446445

447446
// CHECK-LABEL: sil [ossa] @multi_basic_block_destroyed_last_stored_val_phiarg :
448447
// CHECK-NOT: alloc_stack
449-
// CHECK-LABEL: bb2:
450-
// CHECK: br bb3(undef : $Klass)
448+
// CHECK-LABEL: bb3:
451449
// CHECK-LABEL: } // end sil function 'multi_basic_block_destroyed_last_stored_val_phiarg'
452450
sil [ossa] @multi_basic_block_destroyed_last_stored_val_phiarg : $@convention(thin) (@owned Klass) -> () {
453451
bb0(%0 : @owned $Klass):
@@ -630,3 +628,51 @@ bb0(%0 : @owned $Klass):
630628
return %7 : $()
631629
}
632630

631+
// CHECK-LABEL: sil [ossa] @multi_basic_block_bug1 :
632+
// CHECK-NOT: alloc_stack
633+
// CHECK-LABEL: } // end sil function 'multi_basic_block_bug1'
634+
sil [ossa] @multi_basic_block_bug1 : $@convention(thin) (@owned Klass, @owned Klass) -> @owned Klass {
635+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
636+
%stk1 = alloc_stack $Klass
637+
store %0 to [init] %stk1 : $*Klass
638+
cond_br undef, bb1, bb2
639+
640+
bb1:
641+
%new1 = load [take] %stk1 : $*Klass
642+
destroy_value %1 : $Klass
643+
dealloc_stack %stk1 : $*Klass
644+
br bbret(%new1 : $Klass)
645+
646+
bb2:
647+
store %1 to [assign] %stk1 : $*Klass
648+
%new2 = load [take] %stk1 : $*Klass
649+
dealloc_stack %stk1 : $*Klass
650+
br bbret(%new2 : $Klass)
651+
652+
bbret(%new : @owned $Klass):
653+
return %new : $Klass
654+
}
655+
656+
// CHECK-LABEL: sil [ossa] @multi_basic_block_bug2 :
657+
// CHECK-NOT: alloc_stack
658+
// CHECK-LABEL: } // end sil function 'multi_basic_block_bug2'
659+
sil [ossa] @multi_basic_block_bug2 : $@convention(thin) (@owned Klass, @owned Klass) -> @owned Klass {
660+
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
661+
%stk1 = alloc_stack $Klass
662+
store %0 to [init] %stk1 : $*Klass
663+
cond_br undef, bb1, bb2
664+
665+
bb1:
666+
%new1 = load [take] %stk1 : $*Klass
667+
destroy_value %1 : $Klass
668+
br bbret(%new1 : $Klass)
669+
670+
bb2:
671+
store %1 to [assign] %stk1 : $*Klass
672+
%new2 = load [take] %stk1 : $*Klass
673+
br bbret(%new2 : $Klass)
674+
675+
bbret(%new : @owned $Klass):
676+
dealloc_stack %stk1 : $*Klass
677+
return %new : $Klass
678+
}

0 commit comments

Comments
 (0)