Skip to content

[sil] Teach constant folding how to handle destructures and multiple … #22574

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
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
28 changes: 28 additions & 0 deletions include/swift/SIL/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,34 @@ tupleextract_ty<LTy> m_TupleExtractInst(const LTy &Left, unsigned Index) {
return tupleextract_ty<LTy>(Left, Index);
}

/// Match either a tuple_extract that the index field from a tuple or the
/// indexth destructure_tuple result.
template <typename LTy> struct tupleextractoperation_ty {
LTy L;
unsigned index;
tupleextractoperation_ty(const LTy &Left, unsigned i) : L(Left), index(i) {}

template <typename ITy> bool match(ITy *V) {
if (auto *TEI = dyn_cast<TupleExtractInst>(V)) {
return TEI->getFieldNo() == index &&
L.match((ValueBase *)TEI->getOperand());
}

if (auto *DTR = dyn_cast<DestructureTupleResult>(V)) {
return DTR->getIndex() == index &&
L.match((ValueBase *)DTR->getParent()->getOperand());
}

return false;
}
};

template <typename LTy>
tupleextractoperation_ty<LTy> m_TupleExtractOperation(const LTy &Left,
unsigned Index) {
return tupleextractoperation_ty<LTy>(Left, Index);
}

//===----------------------------------------------------------------------===//
// Function/Builtin/Intrinsic Application Matchers
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ class SILInstruction
SILInstructionResultArray getResults() const { return getResultsImpl(); }
unsigned getNumResults() const { return getResults().size(); }

SILValue getResult(unsigned index) const { return getResults()[index]; }

/// Return the types of the results produced by this instruction.
SILInstructionResultArray::type_range getResultTypes() const {
return getResultsImpl().getTypes();
Expand Down
4 changes: 4 additions & 0 deletions include/swift/SILOptimizer/Utils/ConstantFolding.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class ConstantFolder {
/// Initialize the worklist with all instructions of the function \p F.
void initializeWorklist(SILFunction &F);

/// When asserts are enabled, dumps the worklist for diagnostic
/// purposes. Without asserts this is a no-op.
void dumpWorklist() const;

/// Initialize the worklist with a single instruction \p I.
void addToWorklist(SILInstruction *I) {
WorkList.insert(I);
Expand Down
16 changes: 8 additions & 8 deletions lib/SILOptimizer/Utils/CastOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,12 +1429,12 @@ static bool optimizeStaticallyKnownProtocolConformance(
}
case ExistentialRepresentation::Class: {
auto Value =
B.createLoad(Loc, Src, swift::LoadOwnershipQualifier::Unqualified);
B.emitLoadValueOperation(Loc, Src, LoadOwnershipQualifier::Take);
auto Existential =
B.createInitExistentialRef(Loc, Dest->getType().getObjectType(),
SourceType, Value, Conformances);
B.createStore(Loc, Existential, Dest,
swift::StoreOwnershipQualifier::Unqualified);
B.emitStoreValueOperation(Loc, Existential, Dest,
StoreOwnershipQualifier::Init);
break;
}
case ExistentialRepresentation::Boxed: {
Expand All @@ -1445,8 +1445,8 @@ static bool optimizeStaticallyKnownProtocolConformance(
// This needs to be a copy_addr (for now) because we must handle
// address-only types.
B.createCopyAddr(Loc, Src, Projection, IsTake, IsInitialization);
B.createStore(Loc, AllocBox, Dest,
swift::StoreOwnershipQualifier::Unqualified);
B.emitStoreValueOperation(Loc, AllocBox, Dest,
StoreOwnershipQualifier::Init);
break;
}
};
Expand Down Expand Up @@ -1489,12 +1489,12 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
if (!resultTL.isAddressOnly()) {
auto undef = SILValue(
SILUndef::get(DestType.getObjectType(), Builder.getModule()));
Builder.createStore(Loc, undef, Dest,
StoreOwnershipQualifier::Unqualified);
Builder.emitStoreValueOperation(Loc, undef, Dest,
StoreOwnershipQualifier::Init);
}
auto *TrapI = Builder.createBuiltinTrap(Loc);
EraseInstAction(Inst);
Builder.setInsertionPoint(std::next(SILBasicBlock::iterator(TrapI)));
Builder.setInsertionPoint(std::next(TrapI->getIterator()));
auto *UnreachableInst =
Builder.createUnreachable(ArtificialUnreachableLocation());

Expand Down
Loading