Skip to content

[Constant Evaluator] Move "string.append" semantics attribute from String.+= function to String.append function. #24397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions lib/SILOptimizer/Utils/ConstExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum class WellKnownFunction {
StringInitEmpty,
// String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
StringMakeUTF8,
// static String.+= infix(_: inout String, _: String)
// static String.append (_: String, _: inout String)
StringAppend,
// static String.== infix(_: String)
StringEquals
Expand Down Expand Up @@ -233,12 +233,11 @@ SymbolicValue ConstExprFunctionState::computeConstantValue(SILValue value) {
if (auto *sei = dyn_cast<StructExtractInst>(value)) {
auto aggValue = sei->getOperand();
auto val = getConstantValue(aggValue);
if (val.isConstant()) {
assert(val.getKind() == SymbolicValue::Aggregate);
return val.getAggregateValue()[sei->getFieldNo()];
if (!val.isConstant()) {
return val;
}
// Not a const.
return val;
assert(val.getKind() == SymbolicValue::Aggregate);
return val.getAggregateValue()[sei->getFieldNo()];
}

// If this is an unchecked_enum_data from a fragile type, then we can return
Expand Down Expand Up @@ -678,29 +677,35 @@ ConstExprFunctionState::computeWellKnownCallResult(ApplyInst *apply,
return None;
}
case WellKnownFunction::StringAppend: {
// static String.+= infix(_: inout String, _: String)
// static String.append (_: String, _: inout String)
assert(conventions.getNumDirectSILResults() == 0 &&
conventions.getNumIndirectSILResults() == 0 &&
conventions.getNumParameters() == 3 &&
"unexpected String.+=() signature");
conventions.getNumParameters() == 2 &&
"unexpected String.append() signature");

auto firstOperand = apply->getOperand(1);
auto firstString = getConstAddrAndLoadResult(firstOperand);
if (firstString.getKind() != SymbolicValue::String) {
auto otherString = getConstantValue(apply->getOperand(1));
if (!otherString.isConstant()) {
return otherString;
}
if (otherString.getKind() != SymbolicValue::String) {
return evaluator.getUnknown((SILInstruction *)apply,
UnknownReason::InvalidOperandValue);
}

auto otherString = getConstantValue(apply->getOperand(2));
if (otherString.getKind() != SymbolicValue::String) {
auto inoutOperand = apply->getOperand(2);
auto firstString = getConstAddrAndLoadResult(inoutOperand);
if (!firstString.isConstant()) {
return firstString;
}
if (firstString.getKind() != SymbolicValue::String) {
return evaluator.getUnknown((SILInstruction *)apply,
UnknownReason::InvalidOperandValue);
}

auto result = SmallString<8>(firstString.getStringValue());
result.append(otherString.getStringValue());
auto resultVal = SymbolicValue::getString(result, evaluator.getAllocator());
computeFSStore(resultVal, firstOperand);
computeFSStore(resultVal, inoutOperand);
return None;
}
case WellKnownFunction::StringEquals: {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ extension String {

// String append
@inlinable // Forward inlinability to append
@_semantics("string.append")
@_semantics("string.plusequals")
public static func += (lhs: inout String, rhs: String) {
lhs.append(rhs)
}
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/StringRangeReplaceableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extension String: RangeReplaceableCollection {
/// // Prints "Hello, friend"
///
/// - Parameter other: Another string.
@_semantics("string.append")
public mutating func append(_ other: String) {
if self.isEmpty && !_guts.hasNativeStorage {
self = other
Expand Down
30 changes: 15 additions & 15 deletions test/SILOptimizer/constant_evaluator_test.sil
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,9 @@ bb0:
return %5 : $String
}

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

// CHECK-LABEL: @interpretStringAppend
sil @interpretStringAppend: $@convention(thin) () -> @owned String {
Expand All @@ -649,9 +649,9 @@ bb0:
%13 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%14 = apply %13(%9, %10, %11, %12) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%15 = begin_access [modify] [static] %0 : $*String
// function_ref static String.+= infix(_:_:)
%16 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%17 = apply %16(%15, %14, %8) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
// function_ref static String.append (_:_:)
%16 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
%17 = apply %16(%14, %15) : $@convention(method) (@guaranteed String, @inout String) -> ()
end_access %15 : $*String
release_value %14 : $String
%20 = begin_access [read] [static] %0 : $*String
Expand Down Expand Up @@ -683,8 +683,8 @@ bb0:
%14 = apply %13(%9, %10, %11, %12) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%15 = begin_access [modify] [static] %0 : $*String
// function_ref static String.+= infix(_:_:)
%16 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%17 = apply %16(%15, %14, %8) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%16 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
%17 = apply %16(%14, %15) : $@convention(method) (@guaranteed String, @inout String) -> ()
end_access %15 : $*String
release_value %14 : $String
%20 = begin_access [read] [static] %0 : $*String
Expand Down Expand Up @@ -836,8 +836,8 @@ bb4:
%40 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%41 = apply %40(%36, %37, %38, %39) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%42 = begin_access [modify] [static] %10 : $*String
%43 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%44 = apply %43(%42, %41, %35) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%43 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
%44 = apply %43(%41, %42) : $@convention(method) (@guaranteed String, @inout String) -> ()
end_access %42 : $*String
release_value %41 : $String
br bb6
Expand All @@ -857,8 +857,8 @@ bb7:
%55 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%56 = apply %55(%51, %52, %53, %54) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%57 = begin_access [modify] [static] %10 : $*String
%58 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%59 = apply %58(%57, %56, %50) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%58 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
%59 = apply %58(%56, %57) : $@convention(method) (@guaranteed String, @inout String) -> ()
end_access %57 : $*String
release_value %56 : $String
br bb13
Expand All @@ -872,8 +872,8 @@ bb8:
%68 = function_ref @$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%69 = apply %68(%64, %65, %66, %67) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String
%70 = begin_access [modify] [static] %10 : $*String
%71 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%72 = apply %71(%70, %69, %63) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%71 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
%72 = apply %71(%69, %70) : $@convention(method) (@guaranteed String, @inout String) -> ()
end_access %70 : $*String
release_value %69 : $String
br bb13
Expand Down Expand Up @@ -903,8 +903,8 @@ bb11:

bb12(%93 : $String):
%94 = begin_access [modify] [static] %10 : $*String
%95 = function_ref @$sSS2peoiyySSz_SStFZ : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%96 = apply %95(%94, %93, %76) : $@convention(method) (@inout String, @guaranteed String, @thin String.Type) -> ()
%95 = function_ref @$sSS6appendyySSF : $@convention(method) (@guaranteed String, @inout String) -> ()
%96 = apply %95(%93, %94) : $@convention(method) (@guaranteed String, @inout String) -> ()
end_access %94 : $*String
release_value %93 : $String
br bb13
Expand Down