@@ -33,12 +33,14 @@ ArrayCallKind swift::getArraySemanticsKind(SILFunction *f) {
3333 .StartsWith (" array.init" , ArrayCallKind::kArrayInit )
3434 .Case (" array.uninitialized" , ArrayCallKind::kArrayUninitialized )
3535 .Case (" array.uninitialized_intrinsic" , ArrayCallKind::kArrayUninitializedIntrinsic )
36+ .Case (" array.finalize_intrinsic" , ArrayCallKind::kArrayFinalizeIntrinsic )
3637 .Case (" array.check_subscript" , ArrayCallKind::kCheckSubscript )
3738 .Case (" array.check_index" , ArrayCallKind::kCheckIndex )
3839 .Case (" array.get_count" , ArrayCallKind::kGetCount )
3940 .Case (" array.get_capacity" , ArrayCallKind::kGetCapacity )
4041 .Case (" array.get_element" , ArrayCallKind::kGetElement )
4142 .Case (" array.make_mutable" , ArrayCallKind::kMakeMutable )
43+ .Case (" array.end_mutation" , ArrayCallKind::kEndMutation )
4244 .Case (" array.get_element_address" ,
4345 ArrayCallKind::kGetElementAddress )
4446 .Case (" array.mutate_unknown" , ArrayCallKind::kMutateUnknown )
@@ -101,10 +103,12 @@ bool swift::ArraySemanticsCall::isValidSignature() {
101103 }
102104 case ArrayCallKind::kCheckSubscript : {
103105 // Int, Bool, Self
104- if (SemanticsCall->getNumArguments () != 3 ||
105- !SemanticsCall->getArgument (0 )->getType ().isTrivial (*F))
106+ unsigned numArgs = SemanticsCall->getNumArguments ();
107+ if (numArgs != 2 && numArgs != 3 )
108+ return false ;
109+ if (!SemanticsCall->getArgument (0 )->getType ().isTrivial (*F))
106110 return false ;
107- if (!SemanticsCall->getArgument (1 )->getType ().isTrivial (*F))
111+ if (numArgs == 3 && !SemanticsCall->getArgument (1 )->getType ().isTrivial (*F))
108112 return false ;
109113 auto SelfConvention = FnTy->getSelfParameter ().getConvention ();
110114 return SelfConvention == ParameterConvention::Direct_Guaranteed ||
@@ -324,27 +328,26 @@ bool swift::ArraySemanticsCall::canHoist(SILInstruction *InsertBefore,
324328 // Not implemented yet.
325329 return false ;
326330
327- case ArrayCallKind::kCheckSubscript : {
328- auto IsNativeArg = getArrayPropertyIsNativeTypeChecked ();
329- ArraySemanticsCall IsNative (IsNativeArg,
330- " array.props.isNativeTypeChecked" , true );
331- if (!IsNative) {
332- // Do we have a constant parameter?
333- auto *SI = dyn_cast<StructInst>(IsNativeArg);
334- if (!SI)
335- return false ;
336- if (!isa<IntegerLiteralInst>(SI->getOperand (0 )))
331+ case ArrayCallKind::kCheckSubscript :
332+ if (SILValue IsNativeArg = getArrayPropertyIsNativeTypeChecked ()) {
333+ ArraySemanticsCall IsNative (IsNativeArg,
334+ " array.props.isNativeTypeChecked" , true );
335+ if (!IsNative) {
336+ // Do we have a constant parameter?
337+ auto *SI = dyn_cast<StructInst>(IsNativeArg);
338+ if (!SI)
339+ return false ;
340+ if (!isa<IntegerLiteralInst>(SI->getOperand (0 )))
341+ return false ;
342+ } else if (!IsNative.canHoist (InsertBefore, DT))
343+ // Otherwise, we must be able to hoist the function call.
337344 return false ;
338- } else if (!IsNative.canHoist (InsertBefore, DT))
339- // Otherwise, we must be able to hoist the function call.
340- return false ;
341-
345+ }
342346 return canHoistArrayArgument (SemanticsCall, getSelf (), InsertBefore, DT);
343- }
344347
345- case ArrayCallKind::kMakeMutable : {
348+ case ArrayCallKind::kMakeMutable :
349+ case ArrayCallKind::kEndMutation :
346350 return canHoistArrayArgument (SemanticsCall, getSelf (), InsertBefore, DT);
347- }
348351 } // End switch.
349352
350353 return false ;
@@ -448,9 +451,8 @@ ApplyInst *swift::ArraySemanticsCall::hoistOrCopy(SILInstruction *InsertBefore,
448451 hoistOrCopySelf (SemanticsCall, InsertBefore, DT, LeaveOriginal);
449452
450453 SILValue NewArrayProps;
451- if (Kind == ArrayCallKind:: kCheckSubscript ) {
454+ if (SILValue IsNativeArg = getArrayPropertyIsNativeTypeChecked () ) {
452455 // Copy the array.props argument call.
453- auto IsNativeArg = getArrayPropertyIsNativeTypeChecked ();
454456 ArraySemanticsCall IsNative (IsNativeArg,
455457 " array.props.isNativeTypeChecked" , true );
456458 if (!IsNative) {
@@ -492,8 +494,8 @@ ApplyInst *swift::ArraySemanticsCall::hoistOrCopy(SILInstruction *InsertBefore,
492494 return Call;
493495 }
494496
495- case ArrayCallKind::kMakeMutable : {
496- assert (!LeaveOriginal && " Copying not yet implemented " );
497+ case ArrayCallKind::kMakeMutable :
498+ case ArrayCallKind:: kEndMutation : {
497499 // Hoist the call.
498500 auto Call = hoistOrCopyCall (SemanticsCall, InsertBefore, LeaveOriginal, DT);
499501 return Call;
@@ -515,14 +517,15 @@ void swift::ArraySemanticsCall::removeCall() {
515517
516518 switch (getKind ()) {
517519 default : break ;
518- case ArrayCallKind::kCheckSubscript : {
519- // Remove all uses with the empty tuple ().
520- auto EmptyDep = SILBuilderWithScope (SemanticsCall)
521- .createStruct (SemanticsCall->getLoc (),
522- SemanticsCall->getType (), {});
523- SemanticsCall->replaceAllUsesWith (EmptyDep);
524- }
525- break ;
520+ case ArrayCallKind::kCheckSubscript :
521+ if (!SemanticsCall->getType ().isVoid ()){
522+ // Remove all uses with the empty tuple ().
523+ auto EmptyDep = SILBuilderWithScope (SemanticsCall)
524+ .createStruct (SemanticsCall->getLoc (),
525+ SemanticsCall->getType (), {});
526+ SemanticsCall->replaceAllUsesWith (EmptyDep);
527+ }
528+ break ;
526529 case ArrayCallKind::kGetElement : {
527530 // Remove the matching isNativeTypeChecked and check_subscript call.
528531 ArraySemanticsCall IsNative (getTypeCheckedArgument (),
@@ -552,11 +555,13 @@ SILValue
552555swift::ArraySemanticsCall::getArrayPropertyIsNativeTypeChecked () const {
553556 switch (getKind ()) {
554557 case ArrayCallKind::kCheckSubscript :
555- return SemanticsCall->getArgument (1 );
558+ if (SemanticsCall->getNumArguments () == 3 )
559+ return SemanticsCall->getArgument (1 );
560+ return SILValue ();
556561 case ArrayCallKind::kGetElement :
557562 return getTypeCheckedArgument ();
558563 default :
559- llvm_unreachable ( " Must have an array.props argument " );
564+ return SILValue ( );
560565 }
561566}
562567
@@ -569,6 +574,7 @@ bool swift::ArraySemanticsCall::doesNotChangeArray() const {
569574 case ArrayCallKind::kGetCount :
570575 case ArrayCallKind::kGetCapacity :
571576 case ArrayCallKind::kGetElement :
577+ case ArrayCallKind::kEndMutation :
572578 return true ;
573579 }
574580}
0 commit comments