@@ -567,6 +567,10 @@ SILValue AvailableValueAggregator::aggregateValues(SILType LoadTy,
567
567
return aggregateStructSubElts (SD, LoadTy, Address, FirstElt);
568
568
569
569
// Otherwise, we have a non-aggregate primitive. Load or extract the value.
570
+ //
571
+ // NOTE: We should never call this when taking since when taking we know that
572
+ // our underlying value is always fully available.
573
+ assert (!isTake ());
570
574
return handlePrimitiveValue (LoadTy, Address, FirstElt);
571
575
}
572
576
@@ -633,8 +637,19 @@ AvailableValueAggregator::aggregateFullyAvailableValue(SILType loadTy,
633
637
634
638
// If we only are tracking a singular value, we do not need to construct
635
639
// SSA. Just return that value.
636
- if (auto val = singularValue.getValueOr (SILValue ()))
640
+ if (auto val = singularValue.getValueOr (SILValue ())) {
641
+ // This assert documents that we are expecting that if we are in ossa, have
642
+ // a non-trivial value, and are not taking, we should never go down this
643
+ // code path. If we did, we would need to insert a copy here. The reason why
644
+ // we know we will never go down this code path is since we have been
645
+ // inserting copy_values implying that our potential singular value would be
646
+ // of the copy_values which are guaranteed to all be different.
647
+ assert ((!B.hasOwnership () || isTake () ||
648
+ val->getType ().isTrivial (*B.getInsertionBB ()->getParent ())) &&
649
+ " Should never reach this code path if we are in ossa and have a "
650
+ " non-trivial value" );
637
651
return val;
652
+ }
638
653
639
654
// Finally, grab the value from the SSA updater.
640
655
SILValue result = updater.GetValueInMiddleOfBlock (B.getInsertionBB ());
@@ -696,17 +711,18 @@ SILValue AvailableValueAggregator::aggregateStructSubElts(StructDecl *sd,
696
711
return B.createStruct (Loc, loadTy, resultElts);
697
712
}
698
713
699
- // We have looked through all of the aggregate values and finally found a
700
- // "primitive value". If the value is available, use it (extracting if we need
701
- // to), otherwise emit a load of the value with the appropriate qualifier.
714
+ // We have looked through all of the aggregate values and finally found a value
715
+ // that is not available without transforming, i.e. a "primitive value". If the
716
+ // value is available, use it (extracting if we need to), otherwise emit a load
717
+ // of the value with the appropriate qualifier.
702
718
SILValue AvailableValueAggregator::handlePrimitiveValue (SILType loadTy,
703
719
SILValue address,
704
720
unsigned firstElt) {
705
- auto &val = AvailableValueList[firstElt] ;
721
+ assert (! isTake () && " Should only take fully available values?! " ) ;
706
722
707
723
// If the value is not available, load the value and update our use list.
724
+ auto &val = AvailableValueList[firstElt];
708
725
if (!val) {
709
- assert (!isTake () && " Should only take fully available values?!" );
710
726
LoadInst *load = ([&]() {
711
727
if (B.hasOwnership ()) {
712
728
return B.createTrivialLoadOr (Loc, address,
@@ -736,7 +752,10 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
736
752
}
737
753
738
754
// If we have an available value, then we want to extract the subelement from
739
- // the borrowed aggregate before each insertion point.
755
+ // the borrowed aggregate before each insertion point. Note that since we have
756
+ // inserted copies at each of these insertion points, we know that we will
757
+ // never have the same value along all paths unless we have a trivial value
758
+ // meaning the SSA updater given a non-trivial value must /always/ be used.
740
759
SILSSAUpdater updater;
741
760
updater.Initialize (loadTy);
742
761
@@ -759,13 +778,25 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
759
778
updater.AddAvailableValue (i->getParent (), eltVal);
760
779
}
761
780
762
- // If we only are tracking a singular value, we do not need to construct
763
- // SSA. Just return that value.
764
- if (auto val = singularValue.getValueOr (SILValue ()))
781
+ SILBasicBlock *insertBlock = B.getInsertionBB ();
782
+
783
+ // If we are not in ossa and have a singular value or if we are in ossa and
784
+ // have a trivial singular value, just return that value.
785
+ //
786
+ // This can never happen for non-trivial values in ossa since we never should
787
+ // visit this code path if we have a take implying that non-trivial values
788
+ // /will/ have a copy and thus are guaranteed (since each copy yields a
789
+ // different value) to not be singular values.
790
+ if (auto val = singularValue.getValueOr (SILValue ())) {
791
+ assert ((!B.hasOwnership () ||
792
+ val->getType ().isTrivial (*insertBlock->getParent ())) &&
793
+ " Should have inserted copies for each insertion point, so shouldn't "
794
+ " have a singular value if non-trivial?!" );
765
795
return val;
796
+ }
766
797
767
798
// Finally, grab the value from the SSA updater.
768
- SILValue eltVal = updater.GetValueInMiddleOfBlock (B. getInsertionBB () );
799
+ SILValue eltVal = updater.GetValueInMiddleOfBlock (insertBlock );
769
800
assert (!B.hasOwnership () ||
770
801
eltVal.getOwnershipKind ().isCompatibleWith (ValueOwnershipKind::Owned));
771
802
assert (eltVal->getType () == loadTy && " Subelement types mismatch" );
0 commit comments