Skip to content

Commit 07a328b

Browse files
committed
[generics] Change replaceWithSpecializedCallee to use an exhaustive switch.
This will ensure that if we ever add additional apply sites, we will get a warning to update this code. I also updated the coding style in this old piece of code. Just noticed this as I was preparing to update some code here. Should be NFC.
1 parent f7fd83d commit 07a328b

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,89 +1939,94 @@ static void prepareCallArguments(ApplySite AI, SILBuilder &Builder,
19391939

19401940
/// Create a new apply based on an old one, but with a different
19411941
/// function being applied.
1942-
static ApplySite replaceWithSpecializedCallee(ApplySite AI,
1943-
SILValue Callee,
1944-
const ReabstractionInfo &ReInfo) {
1945-
SILBuilderWithScope Builder(AI.getInstruction());
1946-
SILLocation Loc = AI.getLoc();
1947-
SmallVector<SILValue, 4> Arguments;
1948-
SILValue StoreResultTo;
1942+
static ApplySite replaceWithSpecializedCallee(ApplySite applySite,
1943+
SILValue callee,
1944+
const ReabstractionInfo &reInfo) {
1945+
SILBuilderWithScope builder(applySite.getInstruction());
1946+
SILLocation loc = applySite.getLoc();
1947+
SmallVector<SILValue, 4> arguments;
1948+
SILValue resultOut;
19491949

1950-
prepareCallArguments(AI, Builder, ReInfo, Arguments, StoreResultTo);
1950+
prepareCallArguments(applySite, builder, reInfo, arguments, resultOut);
19511951

19521952
// Create a substituted callee type.
1953-
auto CanFnTy = Callee->getType().castTo<SILFunctionType>();
1954-
SubstitutionMap Subs;
1955-
if (ReInfo.getSpecializedType()->isPolymorphic()) {
1956-
Subs = ReInfo.getCallerParamSubstitutionMap();
1957-
Subs = SubstitutionMap::get(CanFnTy->getSubstGenericSignature(), Subs);
1958-
}
1959-
1960-
auto CalleeSubstFnTy =
1961-
CanFnTy->substGenericArgs(*Callee->getModule(), Subs,
1962-
ReInfo.getResilienceExpansion());
1963-
auto CalleeSILSubstFnTy = SILType::getPrimitiveObjectType(CalleeSubstFnTy);
1964-
SILFunctionConventions substConv(CalleeSubstFnTy, Builder.getModule());
1965-
1966-
if (auto *TAI = dyn_cast<TryApplyInst>(AI)) {
1967-
SILBasicBlock *ResultBB = TAI->getNormalBB();
1968-
assert(ResultBB->getSinglePredecessorBlock() == TAI->getParent());
1969-
auto *NewTAI = Builder.createTryApply(Loc, Callee, Subs, Arguments,
1970-
ResultBB, TAI->getErrorBB());
1971-
if (StoreResultTo) {
1953+
auto canFnTy = callee->getType().castTo<SILFunctionType>();
1954+
SubstitutionMap subs;
1955+
if (reInfo.getSpecializedType()->isPolymorphic()) {
1956+
subs = reInfo.getCallerParamSubstitutionMap();
1957+
subs = SubstitutionMap::get(canFnTy->getSubstGenericSignature(), subs);
1958+
}
1959+
1960+
auto calleeSubstFnTy = canFnTy->substGenericArgs(
1961+
*callee->getModule(), subs, reInfo.getResilienceExpansion());
1962+
auto calleeSILSubstFnTy = SILType::getPrimitiveObjectType(calleeSubstFnTy);
1963+
SILFunctionConventions substConv(calleeSubstFnTy, builder.getModule());
1964+
1965+
switch (applySite.getKind()) {
1966+
case ApplySiteKind::TryApplyInst: {
1967+
auto *tai = cast<TryApplyInst>(applySite);
1968+
SILBasicBlock *resultBlock = tai->getNormalBB();
1969+
assert(resultBlock->getSinglePredecessorBlock() == tai->getParent());
1970+
auto *newTAI = builder.createTryApply(loc, callee, subs, arguments,
1971+
resultBlock, tai->getErrorBB());
1972+
if (resultOut) {
19721973
assert(substConv.useLoweredAddresses());
19731974
// The original normal result of the try_apply is an empty tuple.
1974-
assert(ResultBB->getNumArguments() == 1);
1975-
Builder.setInsertionPoint(ResultBB->begin());
1976-
fixUsedVoidType(ResultBB->getArgument(0), Loc, Builder);
1975+
assert(resultBlock->getNumArguments() == 1);
1976+
builder.setInsertionPoint(resultBlock->begin());
1977+
fixUsedVoidType(resultBlock->getArgument(0), loc, builder);
19771978

1978-
SILArgument *Arg = ResultBB->replacePhiArgument(
1979-
0, StoreResultTo->getType().getObjectType(),
1980-
ValueOwnershipKind::Owned);
1979+
SILArgument *arg = resultBlock->replacePhiArgument(
1980+
0, resultOut->getType().getObjectType(), ValueOwnershipKind::Owned);
19811981
// Store the direct result to the original result address.
1982-
Builder.createStore(Loc, Arg, StoreResultTo,
1982+
builder.createStore(loc, arg, resultOut,
19831983
StoreOwnershipQualifier::Unqualified);
19841984
}
1985-
return NewTAI;
1985+
return newTAI;
19861986
}
1987-
if (auto *A = dyn_cast<ApplyInst>(AI)) {
1988-
auto *NewAI = Builder.createApply(Loc, Callee, Subs, Arguments,
1989-
A->isNonThrowing());
1990-
if (StoreResultTo) {
1987+
case ApplySiteKind::ApplyInst: {
1988+
auto *ai = cast<ApplyInst>(applySite);
1989+
auto *newAI =
1990+
builder.createApply(loc, callee, subs, arguments, ai->isNonThrowing());
1991+
if (resultOut) {
19911992
assert(substConv.useLoweredAddresses());
1992-
if (!CalleeSILSubstFnTy.isNoReturnFunction(
1993-
Builder.getModule(), Builder.getTypeExpansionContext())) {
1993+
if (!calleeSILSubstFnTy.isNoReturnFunction(
1994+
builder.getModule(), builder.getTypeExpansionContext())) {
19941995
// Store the direct result to the original result address.
1995-
fixUsedVoidType(A, Loc, Builder);
1996-
Builder.createStore(Loc, NewAI, StoreResultTo,
1996+
fixUsedVoidType(ai, loc, builder);
1997+
builder.createStore(loc, newAI, resultOut,
19971998
StoreOwnershipQualifier::Unqualified);
19981999
} else {
1999-
Builder.createUnreachable(Loc);
2000+
builder.createUnreachable(loc);
20002001
// unreachable should be the terminator instruction.
20012002
// So, split the current basic block right after the
20022003
// inserted unreachable instruction.
2003-
Builder.getInsertionPoint()->getParent()->split(
2004-
Builder.getInsertionPoint());
2004+
builder.getInsertionPoint()->getParent()->split(
2005+
builder.getInsertionPoint());
20052006
}
20062007
}
2007-
A->replaceAllUsesWith(NewAI);
2008-
return NewAI;
2008+
ai->replaceAllUsesWith(newAI);
2009+
return newAI;
20092010
}
2010-
if (auto *A = dyn_cast<BeginApplyInst>(AI)) {
2011-
assert(!StoreResultTo);
2012-
auto *NewAI = Builder.createBeginApply(Loc, Callee, Subs, Arguments,
2013-
A->isNonThrowing());
2014-
A->replaceAllUsesPairwiseWith(NewAI);
2015-
return NewAI;
2011+
case ApplySiteKind::BeginApplyInst: {
2012+
auto *bai = cast<BeginApplyInst>(applySite);
2013+
assert(!resultOut);
2014+
auto *newBAI = builder.createBeginApply(loc, callee, subs, arguments,
2015+
bai->isNonThrowing());
2016+
bai->replaceAllUsesPairwiseWith(newBAI);
2017+
return newBAI;
20162018
}
2017-
if (auto *PAI = dyn_cast<PartialApplyInst>(AI)) {
2018-
auto *NewPAI = Builder.createPartialApply(
2019-
Loc, Callee, Subs, Arguments,
2020-
PAI->getType().getAs<SILFunctionType>()->getCalleeConvention(),
2021-
PAI->isOnStack());
2022-
PAI->replaceAllUsesWith(NewPAI);
2023-
return NewPAI;
2019+
case ApplySiteKind::PartialApplyInst: {
2020+
auto *pai = cast<PartialApplyInst>(applySite);
2021+
auto *newPAI = builder.createPartialApply(
2022+
loc, callee, subs, arguments,
2023+
pai->getType().getAs<SILFunctionType>()->getCalleeConvention(),
2024+
pai->isOnStack());
2025+
pai->replaceAllUsesWith(newPAI);
2026+
return newPAI;
20242027
}
2028+
}
2029+
20252030
llvm_unreachable("unhandled kind of apply");
20262031
}
20272032

0 commit comments

Comments
 (0)