Skip to content

SIL Optimizer improvements for variadic generics [5.9] #65241

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
19 changes: 18 additions & 1 deletion lib/AST/SubstitutionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "swift/AST/InFlightSubstitution.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/PackConformance.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/Types.h"
Expand Down Expand Up @@ -792,13 +793,29 @@ bool SubstitutionMap::isIdentity() const {
if (empty())
return true;

for (auto conf : getConformances()) {
if (conf.isAbstract())
continue;

if (conf.isPack()) {
auto patternConfs = conf.getPack()->getPatternConformances();
if (patternConfs.size() == 1 && patternConfs[0].isAbstract())
continue;
}

return false;
}

GenericSignature sig = getGenericSignature();
bool hasNonIdentityReplacement = false;
auto replacements = getReplacementTypesBuffer();

sig->forEachParam([&](GenericTypeParamType *paramTy, bool isCanonical) {
if (isCanonical) {
if (!paramTy->isEqual(replacements[0]))
Type wrappedParamTy = paramTy;
if (paramTy->isParameterPack())
wrappedParamTy = PackType::getSingletonPackExpansion(paramTy);
if (!wrappedParamTy->isEqual(replacements[0]))
hasNonIdentityReplacement = true;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/Fulfillment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ static CanType getSingletonPackExpansionParameter(CanPackType packType,
return expansion.getPatternType();
}
}

return CanType();
}

bool FulfillmentMap::searchTypeMetadataPack(IRGenModule &IGM,
Expand Down
16 changes: 16 additions & 0 deletions lib/SIL/IR/SILInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,22 @@ namespace {
return true;
}

bool visitScalarPackIndexInst(const ScalarPackIndexInst *RHS) {
auto *X = cast<ScalarPackIndexInst>(LHS);
return (X->getIndexedPackType() == RHS->getIndexedPackType() &&
X->getComponentIndex() == RHS->getComponentIndex());
}

bool visitDynamicPackIndexInst(const DynamicPackIndexInst *RHS) {
auto *X = cast<DynamicPackIndexInst>(LHS);
return X->getIndexedPackType() == RHS->getIndexedPackType();
}

bool visitTuplePackElementAddrInst(const TuplePackElementAddrInst *RHS) {
auto *X = cast<TuplePackElementAddrInst>(LHS);
return X->getElementType() == RHS->getElementType();
}

private:
const SILInstruction *LHS;
};
Expand Down
12 changes: 11 additions & 1 deletion lib/SILOptimizer/LoopTransforms/LoopUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,21 @@ static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,

SILValue RecNext = Cmp->getArguments()[0];
SILPhiArgument *RecArg;

// Match signed add with overflow, unsigned add with overflow and
// add without overflow.
if (!match(RecNext, m_TupleExtractOperation(
m_ApplyInst(BuiltinValueKind::SAddOver,
m_SILPhiArgument(RecArg), m_One()),
0)))
0)) &&
!match(RecNext, m_TupleExtractOperation(
m_ApplyInst(BuiltinValueKind::UAddOver,
m_SILPhiArgument(RecArg), m_One()),
0)) &&
!match(RecNext, m_ApplyInst(BuiltinValueKind::Add,
m_SILPhiArgument(RecArg), m_One()))) {
return None;
}

if (RecArg->getParent() != Header)
return None;
Expand Down
5 changes: 5 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ class SILCombiner :
SILInstruction *
visitDifferentiableFunctionExtractInst(DifferentiableFunctionExtractInst *DFEI);

SILInstruction *visitPackLengthInst(PackLengthInst *PLI);
SILInstruction *visitPackElementGetInst(PackElementGetInst *PEGI);
SILInstruction *visitTuplePackElementAddrInst(TuplePackElementAddrInst *TPEAI);
SILInstruction *visitCopyAddrInst(CopyAddrInst *CAI);

SILInstruction *legacyVisitGlobalValueInst(GlobalValueInst *globalValue);

#define PASS(ID, TAG, DESCRIPTION)
Expand Down
133 changes: 133 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2391,3 +2391,136 @@ SILCombiner::visitDifferentiableFunctionExtractInst(DifferentiableFunctionExtrac
replaceInstUsesWith(*DFEI, newValue);
return eraseInstFromFunction(*DFEI);
}

// Simplify `pack_length` with constant-length pack.
//
// Before:
// %len = pack_length $Pack{Int, String, Float}
//
// After:
// %len = integer_literal Builtin.Word, 3
SILInstruction *SILCombiner::visitPackLengthInst(PackLengthInst *PLI) {
auto PackTy = PLI->getPackType();
if (!PackTy->containsPackExpansionType()) {
return Builder.createIntegerLiteral(PLI->getLoc(), PLI->getType(),
PackTy->getNumElements());
}

return nullptr;
}

// Simplify `pack_element_get` where the index is a `dynamic_pack_index` with
// a constant operand.
//
// Before:
// %idx = integer_literal Builtin.Word, N
// %pack_idx = dynamic_pack_index %Pack{Int, String, Float}, %idx
// %pack_elt = pack_element_get %pack_value, %pack_idx, @element("...")
//
// After:
// %pack_idx = scalar_pack_index %Pack{Int, String, Float}, N
// %concrete_elt = pack_element_get %pack_value, %pack_idx, <<concrete type>>
// %pack_elt = unchecked_addr_cast %concrete_elt, @element("...")
SILInstruction *SILCombiner::visitPackElementGetInst(PackElementGetInst *PEGI) {
auto *DPII = dyn_cast<DynamicPackIndexInst>(PEGI->getIndex());
if (DPII == nullptr)
return nullptr;

auto PackTy = PEGI->getPackType();
if (PackTy->containsPackExpansionType())
return nullptr;

auto *Op = dyn_cast<IntegerLiteralInst>(DPII->getOperand());
if (Op == nullptr)
return nullptr;

if (Op->getValue().uge(PackTy->getNumElements()))
return nullptr;

unsigned Index = Op->getValue().getZExtValue();
auto *SPII = Builder.createScalarPackIndex(
DPII->getLoc(), Index, DPII->getIndexedPackType());

auto ElementTy = SILType::getPrimitiveAddressType(
PEGI->getPackType().getElementType(Index));
auto *NewPEGI = Builder.createPackElementGet(
PEGI->getLoc(), SPII, PEGI->getPack(),
ElementTy);

return Builder.createUncheckedAddrCast(
PEGI->getLoc(), NewPEGI, PEGI->getElementType());
}

// Simplify `tuple_pack_element_addr` where the index is a `dynamic_pack_index`
//with a constant operand.
//
// Before:
// %idx = integer_literal Builtin.Word, N
// %pack_idx = dynamic_pack_index %Pack{Int, String, Float}, %idx
// %tuple_elt = tuple_pack_element_addr %tuple_value, %pack_idx, @element("...")
//
// After:
// %concrete_elt = tuple_element_addr %tuple_value, N
// %tuple_elt = unchecked_addr_cast %concrete_elt, @element("...")
SILInstruction *
SILCombiner::visitTuplePackElementAddrInst(TuplePackElementAddrInst *TPEAI) {
auto *DPII = dyn_cast<DynamicPackIndexInst>(TPEAI->getIndex());
if (DPII == nullptr)
return nullptr;

auto PackTy = DPII->getIndexedPackType();
if (PackTy->containsPackExpansionType())
return nullptr;

auto *Op = dyn_cast<IntegerLiteralInst>(DPII->getOperand());
if (Op == nullptr)
return nullptr;

if (Op->getValue().uge(PackTy->getNumElements()))
return nullptr;

unsigned Index = Op->getValue().getZExtValue();

auto *TEAI = Builder.createTupleElementAddr(
TPEAI->getLoc(), TPEAI->getTuple(), Index);
return Builder.createUncheckedAddrCast(
TPEAI->getLoc(), TEAI, TPEAI->getElementType());
}

// This is a hack. When optimizing a simple pack expansion expression which
// forms a tuple from a pack, like `(repeat each t)`, after the above
// peepholes we end up with:
//
// %src = unchecked_addr_cast %real_src, @element("...")
// %dst = unchecked_addr_cast %real_dst, @element("...")
// copy_addr %src, %dst
//
// Simplify this to
//
// copy_addr %real_src, %real_dst
//
// Assuming that %real_src and %real_dst have the same type.
//
// In this simple case, this eliminates the opened element archetype entirely.
// However, a more principled peephole would be to transform an
// open_pack_element with a scalar index by replacing all usages of the
// element archetype with a concrete type.
SILInstruction *
SILCombiner::visitCopyAddrInst(CopyAddrInst *CAI) {
auto *Src = dyn_cast<UncheckedAddrCastInst>(CAI->getSrc());
auto *Dst = dyn_cast<UncheckedAddrCastInst>(CAI->getDest());

if (Src == nullptr || Dst == nullptr)
return nullptr;

if (Src->getType() != Dst->getType() ||
!Src->getType().is<ElementArchetypeType>())
return nullptr;

if (Src->getOperand()->getType() != Dst->getOperand()->getType())
return nullptr;

return Builder.createCopyAddr(
CAI->getLoc(), Src->getOperand(), Dst->getOperand(),
CAI->isTakeOfSrc(), CAI->isInitializationOfDest());
}
24 changes: 23 additions & 1 deletion lib/SILOptimizer/Transforms/CSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,25 @@ class HashVisitor : public SILInstructionVisitor<HashVisitor, llvm::hash_code> {
X->getKind(), tryLookThroughOwnershipInsts(&X->getOperandRef()),
llvm::hash_combine_range(ConformsTo.begin(), ConformsTo.end()));
}

hash_code visitScalarPackIndexInst(ScalarPackIndexInst *X) {
return llvm::hash_combine(
X->getKind(), X->getIndexedPackType(), X->getComponentIndex());
}

hash_code visitDynamicPackIndexInst(DynamicPackIndexInst *X) {
return llvm::hash_combine(
X->getKind(), X->getIndexedPackType(),
tryLookThroughOwnershipInsts(&X->getOperandRef()));
}

hash_code visitTuplePackElementAddrInst(TuplePackElementAddrInst *X) {
OperandValueArrayRef Operands(X->getAllOperands());
return llvm::hash_combine(
X->getKind(),
llvm::hash_combine_range(Operands.begin(), Operands.end()),
X->getElementType());
}
};
} // end anonymous namespace

Expand Down Expand Up @@ -567,7 +586,7 @@ bool llvm::DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS,
};
bool isEqual =
LHSI->getKind() == RHSI->getKind() && LHSI->isIdenticalTo(RHSI, opCmp);
#ifdef NDEBUG
#ifndef NDEBUG
if (isEqual && getHashValue(LHS) != getHashValue(RHS)) {
llvm::dbgs() << "LHS: ";
LHSI->dump();
Expand Down Expand Up @@ -1225,6 +1244,9 @@ bool CSE::canHandle(SILInstruction *Inst) {
case SILInstructionKind::MarkDependenceInst:
case SILInstructionKind::InitExistentialMetatypeInst:
case SILInstructionKind::WitnessMethodInst:
case SILInstructionKind::ScalarPackIndexInst:
case SILInstructionKind::DynamicPackIndexInst:
case SILInstructionKind::TuplePackElementAddrInst:
// Intentionally we don't handle (prev_)dynamic_function_ref.
// They change at runtime.
#define LOADABLE_REF_STORAGE(Name, ...) \
Expand Down
27 changes: 25 additions & 2 deletions lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,31 @@ static SILValue constantFoldBinary(BuiltinInst *BI,
// Are there valid uses for these in stdlib?
case BuiltinValueKind::Add:
case BuiltinValueKind::Mul:
case BuiltinValueKind::Sub:
return nullptr;
case BuiltinValueKind::Sub: {
OperandValueArrayRef Args = BI->getArguments();
auto *LHS = dyn_cast<IntegerLiteralInst>(Args[0]);
auto *RHS = dyn_cast<IntegerLiteralInst>(Args[1]);
if (!RHS || !LHS)
return nullptr;
APInt LHSI = LHS->getValue();
APInt RHSI = RHS->getValue();

switch (ID) {
default: llvm_unreachable("Not all cases are covered!");
case BuiltinValueKind::Add:
LHSI += RHSI;
break;
case BuiltinValueKind::Mul:
LHSI *= RHSI;
break;
case BuiltinValueKind::Sub:
LHSI -= RHSI;
break;
}

SILBuilderWithScope B(BI);
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), LHSI);
}

case BuiltinValueKind::And:
case BuiltinValueKind::AShr:
Expand Down
1 change: 0 additions & 1 deletion lib/Serialization/DeclTypeRecordNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ TYPE(NAME_ALIAS)

TYPE(PACK_EXPANSION)
TYPE(PACK)
TRAILING_INFO(PACK_TYPE_ELT)
TYPE(SIL_PACK)

TYPE(ERROR)
Expand Down
31 changes: 9 additions & 22 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6866,31 +6866,18 @@ Expected<Type> DESERIALIZE_TYPE(PACK_EXPANSION_TYPE)(

Expected<Type> DESERIALIZE_TYPE(PACK_TYPE)(
ModuleFile &MF, SmallVectorImpl<uint64_t> &scratch, StringRef blobData) {
// The pack record itself is empty. Read all trailing elements.
SmallVector<Type, 8> elements;
while (true) {
llvm::BitstreamEntry entry =
MF.fatalIfUnexpected(MF.DeclTypeCursor.advance(AF_DontPopBlockAtEnd));
if (entry.Kind != llvm::BitstreamEntry::Record)
break;

scratch.clear();
unsigned recordID = MF.fatalIfUnexpected(
MF.DeclTypeCursor.readRecord(entry.ID, scratch, &blobData));
if (recordID != decls_block::PACK_TYPE_ELT)
break;

TypeID typeID;
decls_block::PackTypeEltLayout::readRecord(scratch, typeID);

auto elementTy = MF.getTypeChecked(typeID);
if (!elementTy)
return elementTy.takeError();
ArrayRef<uint64_t> elementTypeIDs;
decls_block::PackTypeLayout::readRecord(scratch, elementTypeIDs);

elements.push_back(elementTy.get());
SmallVector<Type, 8> elementTypes;
for (auto elementTypeID : elementTypeIDs) {
auto elementType = MF.getTypeChecked(elementTypeID);
if (!elementType)
return elementType.takeError();
elementTypes.push_back(elementType.get());
}

return PackType::get(MF.getContext(), elements);
return PackType::get(MF.getContext(), elementTypes);
}

Expected<Type> DESERIALIZE_TYPE(SIL_PACK_TYPE)(
Expand Down
5 changes: 2 additions & 3 deletions lib/Serialization/DeserializeSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,11 +1285,10 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
RawOpCode = (unsigned)SILInstructionKind::HasSymbolInst;
break;
case SIL_PACK_ELEMENT_GET:
SILPackElementGetLayout::readRecord(scratch,
SILPackElementGetLayout::readRecord(scratch, RawOpCode,
TyID, TyCategory,
TyID2, TyCategory2, ValID2,
ValID3);
RawOpCode = (unsigned)SILInstructionKind::PackElementGetInst;
break;
case SIL_PACK_ELEMENT_SET:
SILPackElementSetLayout::readRecord(scratch,
Expand Down Expand Up @@ -1332,7 +1331,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
}
case SILInstructionKind::AllocPackInst: {
assert(RecordKind == SIL_ONE_TYPE && "Layout should be OneType.");
ResultInst = Builder.createAllocStack(
ResultInst = Builder.createAllocPack(
Loc, getSILType(MF->getType(TyID), (SILValueCategory)TyCategory, Fn));
break;
}
Expand Down
Loading