Skip to content

Commit 0d955da

Browse files
authored
Merge pull request #24397 from ravikandhadai/consteval-string-append-changes
2 parents adf224b + a7b7db7 commit 0d955da

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

lib/SILOptimizer/Utils/ConstExpr.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum class WellKnownFunction {
4343
StringInitEmpty,
4444
// String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
4545
StringMakeUTF8,
46-
// static String.+= infix(_: inout String, _: String)
46+
// static String.append (_: String, _: inout String)
4747
StringAppend,
4848
// static String.== infix(_: String)
4949
StringEquals
@@ -233,12 +233,11 @@ SymbolicValue ConstExprFunctionState::computeConstantValue(SILValue value) {
233233
if (auto *sei = dyn_cast<StructExtractInst>(value)) {
234234
auto aggValue = sei->getOperand();
235235
auto val = getConstantValue(aggValue);
236-
if (val.isConstant()) {
237-
assert(val.getKind() == SymbolicValue::Aggregate);
238-
return val.getAggregateValue()[sei->getFieldNo()];
236+
if (!val.isConstant()) {
237+
return val;
239238
}
240-
// Not a const.
241-
return val;
239+
assert(val.getKind() == SymbolicValue::Aggregate);
240+
return val.getAggregateValue()[sei->getFieldNo()];
242241
}
243242

244243
// If this is an unchecked_enum_data from a fragile type, then we can return
@@ -678,29 +677,35 @@ ConstExprFunctionState::computeWellKnownCallResult(ApplyInst *apply,
678677
return None;
679678
}
680679
case WellKnownFunction::StringAppend: {
681-
// static String.+= infix(_: inout String, _: String)
680+
// static String.append (_: String, _: inout String)
682681
assert(conventions.getNumDirectSILResults() == 0 &&
683682
conventions.getNumIndirectSILResults() == 0 &&
684-
conventions.getNumParameters() == 3 &&
685-
"unexpected String.+=() signature");
683+
conventions.getNumParameters() == 2 &&
684+
"unexpected String.append() signature");
686685

687-
auto firstOperand = apply->getOperand(1);
688-
auto firstString = getConstAddrAndLoadResult(firstOperand);
689-
if (firstString.getKind() != SymbolicValue::String) {
686+
auto otherString = getConstantValue(apply->getOperand(1));
687+
if (!otherString.isConstant()) {
688+
return otherString;
689+
}
690+
if (otherString.getKind() != SymbolicValue::String) {
690691
return evaluator.getUnknown((SILInstruction *)apply,
691692
UnknownReason::InvalidOperandValue);
692693
}
693694

694-
auto otherString = getConstantValue(apply->getOperand(2));
695-
if (otherString.getKind() != SymbolicValue::String) {
695+
auto inoutOperand = apply->getOperand(2);
696+
auto firstString = getConstAddrAndLoadResult(inoutOperand);
697+
if (!firstString.isConstant()) {
698+
return firstString;
699+
}
700+
if (firstString.getKind() != SymbolicValue::String) {
696701
return evaluator.getUnknown((SILInstruction *)apply,
697702
UnknownReason::InvalidOperandValue);
698703
}
699704

700705
auto result = SmallString<8>(firstString.getStringValue());
701706
result.append(otherString.getStringValue());
702707
auto resultVal = SymbolicValue::getString(result, evaluator.getAllocator());
703-
computeFSStore(resultVal, firstOperand);
708+
computeFSStore(resultVal, inoutOperand);
704709
return None;
705710
}
706711
case WellKnownFunction::StringEquals: {

stdlib/public/core/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ extension String {
571571

572572
// String append
573573
@inlinable // Forward inlinability to append
574-
@_semantics("string.append")
574+
@_semantics("string.plusequals")
575575
public static func += (lhs: inout String, rhs: String) {
576576
lhs.append(rhs)
577577
}

stdlib/public/core/StringRangeReplaceableCollection.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ extension String: RangeReplaceableCollection {
125125
/// // Prints "Hello, friend"
126126
///
127127
/// - Parameter other: Another string.
128+
@_semantics("string.append")
128129
public mutating func append(_ other: String) {
129130
if self.isEmpty && !_guts.hasNativeStorage {
130131
self = other

test/SILOptimizer/constant_evaluator_test.sil

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,9 @@ bb0:
626626
return %5 : $String
627627
}
628628

629-
// static String.+= infix(_:_:)
629+
// String.append(_:_:)
630630
// The semantics attribute is used by the interpreter.
631-
sil [serialized] [_semantics "string.append"] @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
631+
sil [serialized] [_semantics "string.append"] @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
632632

633633
// CHECK-LABEL: @interpretStringAppend
634634
sil @interpretStringAppend: $@convention(thin) () -> @owned String {
@@ -649,9 +649,9 @@ bb0:
649649
%13 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
650650
%14 = apply %13(%9, %10, %11, %12) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
651651
%15 = begin_access [modify] [static] %0 : $*String
652-
// function_ref static String.+= infix(_:_:)
653-
%16 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
654-
%17 = apply %16(%15, %14, %8) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
652+
// function_ref static String.append (_:_:)
653+
%16 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
654+
%17 = apply %16(%14, %15) : $@convention(method) (@guaranteed String, @inout String) -> ()
655655
end_access %15 : $*String
656656
release_value %14 : $String
657657
%20 = begin_access [read] [static] %0 : $*String
@@ -683,8 +683,8 @@ bb0:
683683
%14 = apply %13(%9, %10, %11, %12) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
684684
%15 = begin_access [modify] [static] %0 : $*String
685685
// function_ref static String.+= infix(_:_:)
686-
%16 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
687-
%17 = apply %16(%15, %14, %8) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
686+
%16 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
687+
%17 = apply %16(%14, %15) : $@convention(method) (@guaranteed String, @inout String) -> ()
688688
end_access %15 : $*String
689689
release_value %14 : $String
690690
%20 = begin_access [read] [static] %0 : $*String
@@ -836,8 +836,8 @@ bb4:
836836
%40 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
837837
%41 = apply %40(%36, %37, %38, %39) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
838838
%42 = begin_access [modify] [static] %10 : $*String
839-
%43 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
840-
%44 = apply %43(%42, %41, %35) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
839+
%43 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
840+
%44 = apply %43(%41, %42) : $@convention(method) (@guaranteed String, @inout String) -> ()
841841
end_access %42 : $*String
842842
release_value %41 : $String
843843
br bb6
@@ -857,8 +857,8 @@ bb7:
857857
%55 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
858858
%56 = apply %55(%51, %52, %53, %54) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
859859
%57 = begin_access [modify] [static] %10 : $*String
860-
%58 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
861-
%59 = apply %58(%57, %56, %50) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
860+
%58 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
861+
%59 = apply %58(%56, %57) : $@convention(method) (@guaranteed String, @inout String) -> ()
862862
end_access %57 : $*String
863863
release_value %56 : $String
864864
br bb13
@@ -872,8 +872,8 @@ bb8:
872872
%68 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
873873
%69 = apply %68(%64, %65, %66, %67) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
874874
%70 = begin_access [modify] [static] %10 : $*String
875-
%71 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
876-
%72 = apply %71(%70, %69, %63) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
875+
%71 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
876+
%72 = apply %71(%69, %70) : $@convention(method) (@guaranteed String, @inout String) -> ()
877877
end_access %70 : $*String
878878
release_value %69 : $String
879879
br bb13
@@ -903,8 +903,8 @@ bb11:
903903

904904
bb12(%93 : $String):
905905
%94 = begin_access [modify] [static] %10 : $*String
906-
%95 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
907-
%96 = apply %95(%94, %93, %76) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
906+
%95 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
907+
%96 = apply %95(%93, %94) : $@convention(method) (@guaranteed String, @inout String) -> ()
908908
end_access %94 : $*String
909909
release_value %93 : $String
910910
br bb13

0 commit comments

Comments
 (0)