Skip to content

Commit 99f4d85

Browse files
committed
ArraySemantics: support "array.check_subscript" as a two-parameter function.
Support a version of Array._checkSubscript which has no wasNativeTypeChecked parameter.
1 parent 16bd756 commit 99f4d85

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

lib/SILOptimizer/Analysis/ArraySemantic.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ bool swift::ArraySemanticsCall::isValidSignature() {
103103
}
104104
case ArrayCallKind::kCheckSubscript: {
105105
// Int, Bool, Self
106-
if (SemanticsCall->getNumArguments() != 3 ||
107-
!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))
108110
return false;
109-
if (!SemanticsCall->getArgument(1)->getType().isTrivial(*F))
111+
if (numArgs == 3 && !SemanticsCall->getArgument(1)->getType().isTrivial(*F))
110112
return false;
111113
auto SelfConvention = FnTy->getSelfParameter().getConvention();
112114
return SelfConvention == ParameterConvention::Direct_Guaranteed ||
@@ -326,23 +328,22 @@ bool swift::ArraySemanticsCall::canHoist(SILInstruction *InsertBefore,
326328
// Not implemented yet.
327329
return false;
328330

329-
case ArrayCallKind::kCheckSubscript: {
330-
auto IsNativeArg = getArrayPropertyIsNativeTypeChecked();
331-
ArraySemanticsCall IsNative(IsNativeArg,
332-
"array.props.isNativeTypeChecked", true);
333-
if (!IsNative) {
334-
// Do we have a constant parameter?
335-
auto *SI = dyn_cast<StructInst>(IsNativeArg);
336-
if (!SI)
337-
return false;
338-
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.
339344
return false;
340-
} else if (!IsNative.canHoist(InsertBefore, DT))
341-
// Otherwise, we must be able to hoist the function call.
342-
return false;
343-
345+
}
344346
return canHoistArrayArgument(SemanticsCall, getSelf(), InsertBefore, DT);
345-
}
346347

347348
case ArrayCallKind::kMakeMutable:
348349
case ArrayCallKind::kEndMutation:
@@ -450,9 +451,8 @@ ApplyInst *swift::ArraySemanticsCall::hoistOrCopy(SILInstruction *InsertBefore,
450451
hoistOrCopySelf(SemanticsCall, InsertBefore, DT, LeaveOriginal);
451452

452453
SILValue NewArrayProps;
453-
if (Kind == ArrayCallKind::kCheckSubscript) {
454+
if (SILValue IsNativeArg = getArrayPropertyIsNativeTypeChecked()) {
454455
// Copy the array.props argument call.
455-
auto IsNativeArg = getArrayPropertyIsNativeTypeChecked();
456456
ArraySemanticsCall IsNative(IsNativeArg,
457457
"array.props.isNativeTypeChecked", true);
458458
if (!IsNative) {
@@ -517,14 +517,15 @@ void swift::ArraySemanticsCall::removeCall() {
517517

518518
switch (getKind()) {
519519
default: break;
520-
case ArrayCallKind::kCheckSubscript: {
521-
// Remove all uses with the empty tuple ().
522-
auto EmptyDep = SILBuilderWithScope(SemanticsCall)
523-
.createStruct(SemanticsCall->getLoc(),
524-
SemanticsCall->getType(), {});
525-
SemanticsCall->replaceAllUsesWith(EmptyDep);
526-
}
527-
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;
528529
case ArrayCallKind::kGetElement: {
529530
// Remove the matching isNativeTypeChecked and check_subscript call.
530531
ArraySemanticsCall IsNative(getTypeCheckedArgument(),
@@ -554,11 +555,13 @@ SILValue
554555
swift::ArraySemanticsCall::getArrayPropertyIsNativeTypeChecked() const {
555556
switch (getKind()) {
556557
case ArrayCallKind::kCheckSubscript:
557-
return SemanticsCall->getArgument(1);
558+
if (SemanticsCall->getNumArguments() == 3)
559+
return SemanticsCall->getArgument(1);
560+
return SILValue();
558561
case ArrayCallKind::kGetElement:
559562
return getTypeCheckedArgument();
560563
default:
561-
llvm_unreachable("Must have an array.props argument");
564+
return SILValue();
562565
}
563566
}
564567

0 commit comments

Comments
 (0)