Skip to content

Commit b01eb90

Browse files
authored
Merge pull request #6103 from apple/revert-5978-redundant-array-init-removal
Revert "SILOptimizer: Replace Array.append(contentsOf: with Array.append(elem…"
2 parents bfae247 + 5eece88 commit b01eb90

File tree

9 files changed

+34
-303
lines changed

9 files changed

+34
-303
lines changed

benchmark/single-source/ArrayAppend.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,4 @@ public func run_ArrayAppendRepeatCol(_ N: Int) {
157157
}
158158
}
159159

160-
// Concatenate a single element array
161-
@inline(never)
162-
public func run_ArrayConcat(_ N: Int) {
163-
for _ in 0..<N {
164-
for _ in 0..<10 {
165-
var nums = [Int]()
166-
for _ in 0..<40000 {
167-
nums += [1]
168-
}
169-
}
170-
}
171-
}
160+

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,7 @@ class ASTContext {
429429

430430
/// Retrieve the declaration of Swift.==(Int, Int) -> Bool.
431431
FuncDecl *getEqualIntDecl() const;
432-
433-
/// Retrieve the declaration of Array.append(element:)
434-
FuncDecl *getArrayAppendElementDecl() const;
435-
432+
436433
/// Retrieve the declaration of Swift._unimplementedInitializer.
437434
FuncDecl *getUnimplementedInitializerDecl(LazyResolver *resolver) const;
438435

include/swift/SILOptimizer/Analysis/ArraySemantic.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ enum class ArrayCallKind {
3737
// The following two semantic function kinds return the result @owned
3838
// instead of operating on self passed as parameter.
3939
kArrayInit,
40-
kArrayUninitialized,
41-
// The following semantic function kinds have a self parameter,
42-
// and are mutating
43-
kAppendContentsOf,
44-
kAppendElement
40+
kArrayUninitialized
4541
};
4642

4743
/// Wrapper around array semantic calls.
@@ -135,12 +131,6 @@ class ArraySemanticsCall {
135131
/// Returns true on success, false otherwise.
136132
bool replaceByValue(SILValue V);
137133

138-
/// Replace a call to append(contentsOf: ) with a series of
139-
/// append(element: ) calls.
140-
bool replaceByAppendingValues(SILModule &M, SILFunction *AppendFn,
141-
llvm::SmallVectorImpl<SILValue> &Vals,
142-
ArrayRef<Substitution> Subs);
143-
144134
/// Hoist the call to the insert point.
145135
void hoist(SILInstruction *InsertBefore, DominanceInfo *DT) {
146136
hoistOrCopy(InsertBefore, DT, false);

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ struct ASTContext::Implementation {
160160
/// func ==(Int, Int) -> Bool
161161
FuncDecl *EqualIntDecl = nullptr;
162162

163-
/// func append(Element) -> void
164-
FuncDecl *ArrayAppendElementDecl = nullptr;
165-
166163
/// func _unimplementedInitializer(className: StaticString).
167164
FuncDecl *UnimplementedInitializerDecl = nullptr;
168165

@@ -891,55 +888,6 @@ FuncDecl *ASTContext::getEqualIntDecl() const {
891888
return nullptr;
892889
}
893890

894-
FuncDecl *ASTContext::getArrayAppendElementDecl() const {
895-
if (Impl.ArrayAppendElementDecl)
896-
return Impl.ArrayAppendElementDecl;
897-
898-
auto AppendFunctions = getArrayDecl()->lookupDirect(getIdentifier("append"));
899-
900-
for (auto CandidateFn : AppendFunctions) {
901-
auto FnDecl = dyn_cast<FuncDecl>(CandidateFn);
902-
auto Attrs = FnDecl->getAttrs();
903-
for (auto *A : Attrs.getAttributes<SemanticsAttr, false>()) {
904-
if (A->Value != "array.append_element")
905-
continue;
906-
907-
#ifndef NDEBUG
908-
auto ParamLists = FnDecl->getParameterLists();
909-
assert(ParamLists.size() == 2 && "Should have two parameter lists");
910-
assert(ParamLists[0]->size() == 1 &&
911-
"First parameter list should have one parameter");
912-
auto SelfInOutTy = ParamLists[0]->get(0)->getType()->getAs<InOutType>();
913-
assert(SelfInOutTy && "Self parameter should be an inout Type");
914-
auto SelfGenericStructTy =
915-
SelfInOutTy->getObjectType()->getAs<BoundGenericStructType>();
916-
assert(SelfGenericStructTy &&
917-
"Self parameter should be a BoundGenericStructType Type");
918-
assert(SelfGenericStructTy->getDecl() == getArrayDecl() &&
919-
"Self parameter should be an Array");
920-
921-
assert(ParamLists[1]->size() == 1 &&
922-
"Second parameter list should have one parameter");
923-
auto ElementType = ParamLists[1]
924-
->get(0)
925-
->getType()
926-
->getAs<ArchetypeType>();
927-
assert(ElementType &&
928-
"First parameter replacement should be a Archetype");
929-
assert(ElementType->getName() == getIdentifier("Element") &&
930-
"The Archetype's name should be \"Element\"");
931-
932-
assert(FnDecl->getResultInterfaceType()->isVoid() &&
933-
"The return type should be void");
934-
#endif
935-
Impl.ArrayAppendElementDecl = FnDecl;
936-
return FnDecl;
937-
}
938-
}
939-
940-
return NULL;
941-
}
942-
943891
FuncDecl *
944892
ASTContext::getUnimplementedInitializerDecl(LazyResolver *resolver) const {
945893
if (Impl.UnimplementedInitializerDecl)

lib/SILOptimizer/Analysis/ArraySemantic.cpp

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ ArrayCallKind swift::ArraySemanticsCall::getKind() const {
161161
.Case("array.get_element_address",
162162
ArrayCallKind::kGetElementAddress)
163163
.Case("array.mutate_unknown", ArrayCallKind::kMutateUnknown)
164-
.Case("array.withUnsafeMutableBufferPointer",
165-
ArrayCallKind::kWithUnsafeMutableBufferPointer)
166-
.Case("array.append_contentsOf", ArrayCallKind::kAppendContentsOf)
167-
.Case("array.append_element", ArrayCallKind::kAppendElement)
164+
.Case("array.withUnsafeMutableBufferPointer", ArrayCallKind::kWithUnsafeMutableBufferPointer)
168165
.Default(ArrayCallKind::kNone);
169166
if (Tmp != ArrayCallKind::kNone) {
170167
assert(Kind == ArrayCallKind::kNone && "Multiple array semantic "
@@ -685,41 +682,3 @@ bool swift::ArraySemanticsCall::replaceByValue(SILValue V) {
685682
removeCall();
686683
return true;
687684
}
688-
689-
bool swift::ArraySemanticsCall::replaceByAppendingValues(
690-
SILModule &M, SILFunction *AppendFn, SmallVectorImpl<SILValue> &Vals,
691-
ArrayRef<Substitution> Subs) {
692-
assert(getKind() == ArrayCallKind::kAppendContentsOf &&
693-
"Must be an append_contentsOf call");
694-
assert(AppendFn && "Must provide an append SILFunction");
695-
696-
// We only handle loadable types.
697-
if (any_of(Vals, [&M](SILValue V) -> bool {
698-
return !V->getType().isLoadable(M);
699-
}))
700-
return false;
701-
702-
auto ArrRef = SemanticsCall->getArgument(1);
703-
SILBuilderWithScope Builder(SemanticsCall);
704-
auto Loc = SemanticsCall->getLoc();
705-
auto *FnRef = Builder.createFunctionRef(Loc, AppendFn);
706-
auto FnTy = FnRef->getType();
707-
708-
for (auto &V : Vals) {
709-
auto SubTy = V->getType();
710-
auto &ValLowering = Builder.getModule().getTypeLowering(SubTy);
711-
auto CopiedVal = ValLowering.emitCopyValue(Builder, Loc, V);
712-
auto *AllocStackInst = Builder.createAllocStack(Loc, SubTy);
713-
Builder.createStore(Loc, CopiedVal, AllocStackInst,
714-
StoreOwnershipQualifier::Unqualified);
715-
SILValue Args[] = {AllocStackInst, ArrRef};
716-
Builder.createApply(Loc, FnRef, FnTy.substGenericArgs(M, Subs),
717-
FnTy.castTo<SILFunctionType>()->getSILResult(), Subs,
718-
Args, false);
719-
Builder.createDeallocStack(Loc, AllocStackInst);
720-
}
721-
722-
removeCall();
723-
724-
return true;
725-
}

lib/SILOptimizer/LoopTransforms/COWArrayOpt.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,6 @@ static bool isNonMutatingArraySemanticCall(SILInstruction *Inst) {
489489
case ArrayCallKind::kWithUnsafeMutableBufferPointer:
490490
case ArrayCallKind::kArrayInit:
491491
case ArrayCallKind::kArrayUninitialized:
492-
case ArrayCallKind::kAppendContentsOf:
493-
case ArrayCallKind::kAppendElement:
494492
return false;
495493
}
496494

@@ -827,8 +825,6 @@ static bool mayChangeArrayValueToNonUniqueState(ArraySemanticsCall &Call) {
827825
case ArrayCallKind::kWithUnsafeMutableBufferPointer:
828826
case ArrayCallKind::kArrayInit:
829827
case ArrayCallKind::kArrayUninitialized:
830-
case ArrayCallKind::kAppendContentsOf:
831-
case ArrayCallKind::kAppendElement:
832828
return true;
833829
}
834830

0 commit comments

Comments
 (0)