Skip to content

Commit 7e359d1

Browse files
authored
Merge pull request #30738 from apple/revert-30689-curry-thunk-peephole
Revert "SILOptimizer: Peephole to eliminate closures which just apply a witness_method"
2 parents e839cd3 + cf474db commit 7e359d1

File tree

5 files changed

+3
-353
lines changed

5 files changed

+3
-353
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -78,104 +78,6 @@ static bool foldInverseReabstractionThunks(PartialApplyInst *PAI,
7878
return true;
7979
}
8080

81-
ApplyInst *getSingleApplyInBlock(SILBasicBlock &BB) {
82-
ApplyInst *Result = nullptr;
83-
84-
for (auto &I : BB) {
85-
if (isa<DebugValueAddrInst>(I) ||
86-
isa<DebugValueInst>(I))
87-
continue;
88-
89-
// FIXME: Should also support ClassMethodInst
90-
if (isa<WitnessMethodInst>(I) ||
91-
isa<ReturnInst>(I))
92-
continue;
93-
94-
if (auto *TI = dyn_cast<TupleInst>(&I))
95-
if (TI->getElements().empty())
96-
continue;
97-
98-
if (auto *AI = dyn_cast<ApplyInst>(&I)) {
99-
if (Result == nullptr) {
100-
Result = AI;
101-
continue;
102-
}
103-
}
104-
105-
return nullptr;
106-
}
107-
108-
return Result;
109-
}
110-
111-
static bool foldPartialApplyOfTrivialClosure(PartialApplyInst *PAI,
112-
SILCombiner *Combiner) {
113-
auto *FRI = dyn_cast<FunctionRefInst>(PAI->getCallee());
114-
if (FRI == nullptr)
115-
return false;
116-
117-
auto *F = FRI->getReferencedFunctionOrNull();
118-
if (F == nullptr)
119-
return false;
120-
121-
// The referenced function must consist of a single basic block.
122-
if (F->getBlocks().size() != 1)
123-
return false;
124-
125-
// The referenced function must not throw.
126-
if (F->getLoweredFunctionType()->getOptionalErrorResult())
127-
return false;
128-
129-
// The only non-trivial instructions in the function must be an apply
130-
// of a witness_method callee.
131-
auto &BB = *F->getBlocks().begin();
132-
auto *AI = getSingleApplyInBlock(BB);
133-
if (AI == nullptr)
134-
return false;
135-
136-
// The apply instruction must have the same number of arguments as the
137-
// function that contains it, and they must be passed in order.
138-
auto InnerArgs = AI->getArguments();
139-
if (InnerArgs.size() != BB.args_size())
140-
return false;
141-
142-
auto ArgsIter = BB.args_begin();
143-
for (auto Arg : InnerArgs) {
144-
if (Arg != *(ArgsIter++))
145-
return false;
146-
}
147-
148-
SILBuilderWithScope B(PAI);
149-
150-
// FIXME: Should also support ClassMethodInst
151-
auto *WMI = cast<WitnessMethodInst>(AI->getCallee());
152-
auto InnerSubs = PAI->getSubstitutionMap();
153-
auto Subs = AI->getSubstitutionMap().subst(InnerSubs);
154-
auto *NewCallee = B.createWitnessMethod(
155-
WMI->getLoc(),
156-
WMI->getLookupType().subst(InnerSubs)->getCanonicalType(),
157-
WMI->getConformance().subst(WMI->getLookupType(), InnerSubs),
158-
WMI->getMember(),
159-
WMI->getType().subst(F->getModule().Types, InnerSubs));
160-
161-
SmallVector<SILValue, 8> NewArgs;
162-
auto Args = PAI->getArguments();
163-
NewArgs.append(Args.begin(), Args.end());
164-
165-
auto *NewPAI = B.createPartialApply(
166-
PAI->getLoc(),
167-
NewCallee,
168-
Subs,
169-
NewArgs,
170-
PAI->getType().getAs<SILFunctionType>()->getCalleeConvention(),
171-
PAI->isOnStack());
172-
173-
assert(PAI->getType() == NewPAI->getType());
174-
PAI->replaceAllUsesWith(NewPAI);
175-
Combiner->eraseInstFromFunction(*PAI);
176-
return true;
177-
}
178-
17981
SILInstruction *SILCombiner::visitPartialApplyInst(PartialApplyInst *PAI) {
18082
if (PAI->getFunction()->hasOwnership())
18183
return nullptr;
@@ -207,11 +109,6 @@ SILInstruction *SILCombiner::visitPartialApplyInst(PartialApplyInst *PAI) {
207109
if (foldInverseReabstractionThunks(PAI, this))
208110
return nullptr;
209111

210-
// partial_apply %function_ref => partial_apply %witness_method if
211-
// the body of the function_ref is just an apply %witness_method
212-
if (foldPartialApplyOfTrivialClosure(PAI, this))
213-
return nullptr;
214-
215112
bool argsAreKeptAlive = tryOptimizeApplyOfPartialApply(
216113
PAI, Builder.getBuilderContext(), getInstModCallbacks());
217114
if (argsAreKeptAlive)

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ replacePartialApplyInst(SILBuilder &builder, SILLocation loc,
589589
auto convention =
590590
oldPAI->getType().getAs<SILFunctionType>()->getCalleeConvention();
591591
auto *newPAI =
592-
builder.createPartialApply(loc, newFn, newSubs, newArgs, convention,
593-
oldPAI->isOnStack());
592+
builder.createPartialApply(loc, newFn, newSubs, newArgs, convention);
594593

595594
// Check if any casting is required for the partially-applied function.
596595
SILValue resultValue = castValueToABICompatibleType(

test/SILOptimizer/sil_combine_curry_thunk.sil

Lines changed: 0 additions & 166 deletions
This file was deleted.

0 commit comments

Comments
 (0)