Skip to content

ArraySemantics: remove some unused code #77918

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 1 commit into from
Dec 3, 2024
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
16 changes: 0 additions & 16 deletions include/swift/SILOptimizer/Analysis/ArraySemantic.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,6 @@ class ArraySemanticsCall {
/// parameter.
void removeCall();

/// Replace a call to get_element by a value.
///
/// Preconditions:
/// The value \p V must dominate this get_element call.
/// This must be a get_element call.
///
/// Returns true on success, false otherwise.
bool replaceByValue(SILValue V);

/// Replace a call to append(contentsOf: ) with a series of
/// append(element: ) calls.
bool replaceByAppendingValues(SILFunction *AppendFn,
SILFunction *ReserveFn,
const llvm::SmallVectorImpl<SILValue> &Vals,
SubstitutionMap Subs);

/// Hoist the call to the insert point.
void hoist(SILInstruction *InsertBefore, DominanceInfo *DT) {
hoistOrCopy(InsertBefore, DT, false);
Expand Down
116 changes: 0 additions & 116 deletions lib/SILOptimizer/Analysis/ArraySemantic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,122 +718,6 @@ SILValue swift::ArraySemanticsCall::getArrayElementStoragePointer() const {
return getArrayUninitializedInitResult(*this, 1);
}

bool swift::ArraySemanticsCall::replaceByValue(SILValue V) {
assert(getKind() == ArrayCallKind::kGetElement &&
"Must be a get_element call");
// We only handle loadable types.
if (!V->getType().isLoadable(*SemanticsCall->getFunction()))
return false;

if (!hasGetElementDirectResult())
return false;

// Expect a check_subscript call or the empty dependence.
auto SubscriptCheck = getSubscriptCheckArgument();
ArraySemanticsCall Check(SubscriptCheck, "array.check_subscript");
auto *EmptyDep = dyn_cast<StructInst>(SubscriptCheck);
if (!Check && (!EmptyDep || !EmptyDep->getElements().empty()))
return false;

// In OSSA, the InsertPt is after V's definition and not before SemanticsCall
// Because we are creating copy_value in ossa, and the source may have been
// taken previously. So our insert point for copy_value is immediately after
// V, where we can be sure it is live.
auto InsertPt = V->getFunction()->hasOwnership()
? getInsertAfterPoint(V)
: SemanticsCall->getIterator();
assert(InsertPt.has_value());

SILValue CopiedVal = SILBuilderWithScope(InsertPt.value())
.emitCopyValueOperation(SemanticsCall->getLoc(), V);
SemanticsCall->replaceAllUsesWith(CopiedVal);

removeCall();
return true;
}

bool swift::ArraySemanticsCall::replaceByAppendingValues(
SILFunction *AppendFn, SILFunction *ReserveFn,
const SmallVectorImpl<SILValue> &Vals, SubstitutionMap Subs) {
assert(getKind() == ArrayCallKind::kAppendContentsOf &&
"Must be an append_contentsOf call");
assert(AppendFn && "Must provide an append SILFunction");

auto *F = SemanticsCall->getFunction();

// We only handle loadable types.
if (any_of(Vals, [F](SILValue V) -> bool {
return !V->getType().isLoadable(*F);
}))
return false;

CanSILFunctionType AppendFnTy = AppendFn->getLoweredFunctionType();
SILValue ArrRef = SemanticsCall->getArgument(1);
SILBuilderWithScope Builder(SemanticsCall);
auto Loc = SemanticsCall->getLoc();
auto *FnRef = Builder.createFunctionRefFor(Loc, AppendFn);

if (Vals.size() > 1) {
// Create a call to reserveCapacityForAppend() to reserve space for multiple
// elements.
FunctionRefBaseInst *ReserveFnRef =
Builder.createFunctionRefFor(Loc, ReserveFn);
SILFunctionType *ReserveFnTy =
ReserveFnRef->getType().castTo<SILFunctionType>();
assert(ReserveFnTy->getNumParameters() == 2);
StructType *IntType =
ReserveFnTy->getParameters()[0]
.getArgumentType(F->getModule(), ReserveFnTy,
Builder.getTypeExpansionContext())
->castTo<StructType>();
StructDecl *IntDecl = IntType->getDecl();
VarDecl *field = IntDecl->getStoredProperties()[0];
SILType BuiltinIntTy =SILType::getPrimitiveObjectType(
field->getInterfaceType()->getCanonicalType());
IntegerLiteralInst *CapacityLiteral =
Builder.createIntegerLiteral(Loc, BuiltinIntTy, Vals.size());
StructInst *Capacity = Builder.createStruct(Loc,
SILType::getPrimitiveObjectType(CanType(IntType)), {CapacityLiteral});
Builder.createApply(Loc, ReserveFnRef, Subs, {Capacity, ArrRef});
}

for (SILValue V : Vals) {
auto SubTy = V->getType();
auto &ValLowering = Builder.getTypeLowering(SubTy);
// In OSSA, the InsertPt is after V's definition and not before
// SemanticsCall. Because we are creating copy_value in ossa, and the source
// may have been taken previously. So our insert point for copy_value is
// immediately after V, where we can be sure it is live.
auto InsertPt = F->hasOwnership() ? getInsertAfterPoint(V)
: SemanticsCall->getIterator();
assert(InsertPt.has_value());
SILValue CopiedVal = SILBuilderWithScope(InsertPt.value())
.emitCopyValueOperation(V.getLoc(), V);
auto *AllocStackInst = Builder.createAllocStack(Loc, SubTy);
ValLowering.emitStoreOfCopy(Builder, Loc, CopiedVal, AllocStackInst,
IsInitialization_t::IsInitialization);

SILValue Args[] = {AllocStackInst, ArrRef};
Builder.createApply(Loc, FnRef, Subs, Args);
Builder.createDeallocStack(Loc, AllocStackInst);
if (!isConsumedParameterInCaller(
AppendFnTy->getParameters()[0].getConvention())) {
ValLowering.emitDestroyValue(Builder, Loc, CopiedVal);
}
}
CanSILFunctionType AppendContentsOfFnTy =
SemanticsCall->getReferencedFunctionOrNull()->getLoweredFunctionType();
if (AppendContentsOfFnTy->getParameters()[0].getConvention() ==
ParameterConvention::Direct_Owned) {
SILValue SrcArray = SemanticsCall->getArgument(0);
Builder.emitDestroyValueOperation(SemanticsCall->getLoc(), SrcArray);
}

removeCall();

return true;
}

bool swift::ArraySemanticsCall::mapInitializationStores(
llvm::DenseMap<uint64_t, StoreInst *> &ElementValueMap) {
if (getKind() != ArrayCallKind::kArrayUninitialized &&
Expand Down