Skip to content

Commit fad94c6

Browse files
committed
- fixes
1 parent 4b9a7c0 commit fad94c6

29 files changed

+117
-175
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OwnershipLiveness.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ extension InteriorUseWalker {
803803
}
804804
var enclosingValues = Stack<Value>(context)
805805
defer { enclosingValues.deinitialize() }
806-
gatherEnclosingValues(for: guaranteedPhi.value, in: &enclosingValues,
806+
gatherEnclosingValues(for: guaranteedPhi.value.lookThroughBorrowedFromUser, in: &enclosingValues,
807807
context)
808808
guard enclosingValues.contains(definingValue) else {
809809
// Since definingValue is not an enclosing value, it must be

SwiftCompilerSources/Sources/Optimizer/Utilities/Verifier.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private extension Argument {
5858
var forwardingBorrowedFromFound = false
5959
for use in phi.value.uses {
6060
precondition(use.instruction is BorrowedFromInst,
61-
"guaranteed phi \(self) has non borrowed-from use \(use)")
61+
"guaranteed phi: \(self)\n has non borrowed-from use: \(use)")
6262
if use.index == 0 {
6363
precondition(!forwardingBorrowedFromFound, "phi \(self) has multiple forwarding borrowed-from uses")
6464
forwardingBorrowedFromFound = true

include/swift/SILOptimizer/Utils/Devirtualize.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ SubstitutionMap getWitnessMethodSubstitutions(SILModule &Module, ApplySite AI,
6868
///
6969
/// Return the new apply and true if the CFG was also modified.
7070
std::pair<ApplySite, bool>
71-
tryDevirtualizeApply(ApplySite AI, ClassHierarchyAnalysis *CHA,
71+
tryDevirtualizeApply(SILPassManager *pm, ApplySite AI, ClassHierarchyAnalysis *CHA,
7272
OptRemark::Emitter *ORE = nullptr,
7373
bool isMandatory = false);
7474
bool canDevirtualizeApply(FullApplySite AI, ClassHierarchyAnalysis *CHA);
@@ -86,7 +86,8 @@ CanType getSelfInstanceType(CanType ClassOrMetatypeType);
8686
///
8787
/// Return the new apply and true if the CFG was also modified.
8888
std::pair<FullApplySite, bool>
89-
devirtualizeClassMethod(FullApplySite AI, SILValue ClassInstance, ClassDecl *CD,
89+
devirtualizeClassMethod(SILPassManager *pm,
90+
FullApplySite AI, SILValue ClassInstance, ClassDecl *CD,
9091
CanType classType, OptRemark::Emitter *ORE);
9192

9293
/// Attempt to devirtualize the given apply site, which is known to be
@@ -97,7 +98,8 @@ devirtualizeClassMethod(FullApplySite AI, SILValue ClassInstance, ClassDecl *CD,
9798
///
9899
/// Return the new apply and true if the CFG was also modified.
99100
std::pair<FullApplySite, bool>
100-
tryDevirtualizeClassMethod(FullApplySite AI, SILValue ClassInstance,
101+
tryDevirtualizeClassMethod(SILPassManager *pm,
102+
FullApplySite AI, SILValue ClassInstance,
101103
ClassDecl *CD, CanType ClassType,
102104
OptRemark::Emitter *ORE,
103105
bool isEffectivelyFinalMethod = false);
@@ -110,7 +112,8 @@ tryDevirtualizeClassMethod(FullApplySite AI, SILValue ClassInstance,
110112
/// the original apply site.
111113
///
112114
/// Return the new apply and true if the CFG was also modified.
113-
std::pair<ApplySite, bool> tryDevirtualizeWitnessMethod(ApplySite AI,
115+
std::pair<ApplySite, bool> tryDevirtualizeWitnessMethod(SILPassManager *pm,
116+
ApplySite AI,
114117
OptRemark::Emitter *ORE,
115118
bool isMandatory);
116119

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ SILValue getConcreteValueOfExistentialBoxAddr(SILValue addr,
192192
/// BranchInst (a phi is never the last guaranteed user). \p builder's current
193193
/// insertion point must dominate all \p usePoints.
194194
std::pair<SILValue, bool /* changedCFG */>
195-
castValueToABICompatibleType(SILBuilder *builder, SILLocation Loc,
195+
castValueToABICompatibleType(SILBuilder *builder, SILPassManager *pm,
196+
SILLocation Loc,
196197
SILValue value, SILType srcTy, SILType destTy,
197198
ArrayRef<SILInstruction *> usePoints);
198199
/// Peek through trivial Enum initialization, typically for pointless

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,9 @@ getCalleeFunction(SILFunction *F, FullApplySite AI, bool &IsThick,
772772
return CalleeFunction;
773773
}
774774

775-
static SILInstruction *tryDevirtualizeApplyHelper(FullApplySite InnerAI,
775+
static SILInstruction *tryDevirtualizeApplyHelper(SILPassManager *pm, FullApplySite InnerAI,
776776
ClassHierarchyAnalysis *CHA) {
777-
auto NewInst = tryDevirtualizeApply(InnerAI, CHA).first;
777+
auto NewInst = tryDevirtualizeApply(pm, InnerAI, CHA).first;
778778
if (!NewInst)
779779
return InnerAI.getInstruction();
780780

@@ -804,7 +804,8 @@ static SILInstruction *tryDevirtualizeApplyHelper(FullApplySite InnerAI,
804804
///
805805
/// \returns true if successful, false if failed due to circular inlining.
806806
static bool
807-
runOnFunctionRecursively(SILOptFunctionBuilder &FuncBuilder, SILFunction *F,
807+
runOnFunctionRecursively(SILOptFunctionBuilder &FuncBuilder, SILPassManager *pm,
808+
SILFunction *F,
808809
FullApplySite AI, DenseFunctionSet &FullyInlinedSet,
809810
ImmutableFunctionSet::Factory &SetFactory,
810811
ImmutableFunctionSet CurrentInliningSet,
@@ -855,7 +856,7 @@ runOnFunctionRecursively(SILOptFunctionBuilder &FuncBuilder, SILFunction *F,
855856
// *NOTE* If devirtualization succeeds, devirtInst may not be InnerAI,
856857
// but a casted result of InnerAI or even a block argument due to
857858
// abstraction changes when calling the witness or class method.
858-
auto *devirtInst = tryDevirtualizeApplyHelper(InnerAI, CHA);
859+
auto *devirtInst = tryDevirtualizeApplyHelper(pm, InnerAI, CHA);
859860
// If devirtualization succeeds, make sure we record that this function
860861
// changed.
861862
if (devirtInst != InnerAI.getInstruction())
@@ -879,7 +880,7 @@ runOnFunctionRecursively(SILOptFunctionBuilder &FuncBuilder, SILFunction *F,
879880

880881
// Then recursively process it first before trying to inline it.
881882
if (!runOnFunctionRecursively(
882-
FuncBuilder, CalleeFunction, InnerAI, FullyInlinedSet, SetFactory,
883+
FuncBuilder, pm, CalleeFunction, InnerAI, FullyInlinedSet, SetFactory,
883884
CurrentInliningSet, CHA, changedFunctions)) {
884885
// If we failed due to circular inlining, then emit some notes to
885886
// trace back the failure if we have more information.
@@ -1040,7 +1041,7 @@ class MandatoryInlining : public SILModuleTransform {
10401041
if (F.wasDeserializedCanonical())
10411042
continue;
10421043

1043-
runOnFunctionRecursively(FuncBuilder, &F, FullApplySite(),
1044+
runOnFunctionRecursively(FuncBuilder, getPassManager(), &F, FullApplySite(),
10441045
FullyInlinedSet, SetFactory,
10451046
SetFactory.getEmptySet(), CHA, changedFunctions);
10461047

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,8 +1566,9 @@ bool BridgedPassContext::tryDeleteDeadClosure(BridgedInstruction closure, bool n
15661566

15671567
BridgedPassContext::DevirtResult BridgedPassContext::tryDevirtualizeApply(BridgedInstruction apply,
15681568
bool isMandatory) const {
1569-
auto cha = invocation->getPassManager()->getAnalysis<ClassHierarchyAnalysis>();
1570-
auto result = ::tryDevirtualizeApply(ApplySite(apply.unbridged()), cha,
1569+
SILPassManager *pm = invocation->getPassManager();
1570+
auto cha = pm->getAnalysis<ClassHierarchyAnalysis>();
1571+
auto result = ::tryDevirtualizeApply(pm, ApplySite(apply.unbridged()), cha,
15711572
nullptr, isMandatory);
15721573
if (result.first) {
15731574
OptionalBridgedInstruction newApply(result.first.getInstruction()->asSILNode());

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,8 @@ SILCombiner::visitDifferentiableFunctionExtractInst(DifferentiableFunctionExtrac
19951995
return nullptr;
19961996

19971997
std::tie(newValue, std::ignore) =
1998-
castValueToABICompatibleType(&Builder, DFEI->getLoc(),
1998+
castValueToABICompatibleType(&Builder, parentTransform->getPassManager(),
1999+
DFEI->getLoc(),
19992000
newValue,
20002001
newValue->getType(), DFEI->getType(), {});
20012002
}

lib/SILOptimizer/Transforms/Devirtualizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void Devirtualizer::devirtualizeAppliesInFunction(SILFunction &F,
7878
for (auto Apply : Applies) {
7979
ApplySite NewInst;
8080
bool modifiedCFG;
81-
std::tie(NewInst, modifiedCFG) = tryDevirtualizeApply(Apply, CHA, &ORE);
81+
std::tie(NewInst, modifiedCFG) = tryDevirtualizeApply(getPassManager(), Apply, CHA, &ORE);
8282
if (!NewInst)
8383
continue;
8484

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,11 +2186,13 @@ bool SimplifyCFG::simplifySwitchEnumBlock(SwitchEnumInst *SEI) {
21862186
EnumCase.get());
21872187
}
21882188
Builder.createBranch(loc, LiveBlock, PayLoad);
2189+
SEI->eraseFromParent();
2190+
updateBorrowedFromPhis(PM, { cast<SILPhiArgument>(LiveBlock->getArgument(0)) });
21892191
} else {
21902192
Builder.createBranch(loc, LiveBlock);
2193+
SEI->eraseFromParent();
21912194
}
21922195

2193-
SEI->eraseFromParent();
21942196
if (EI && isInstructionTriviallyDead(EI)) {
21952197
EI->replaceAllUsesOfAllResultsWithUndef();
21962198
EI->eraseFromParent();
@@ -2571,7 +2573,7 @@ bool SimplifyCFG::simplifyTryApplyBlock(TryApplyInst *TAI) {
25712573
}
25722574
// Cast argument if required.
25732575
std::tie(Arg, std::ignore) = castValueToABICompatibleType(
2574-
&Builder, TAI->getLoc(), Arg, origConv.getSILArgumentType(i, context),
2576+
&Builder, PM, TAI->getLoc(), Arg, origConv.getSILArgumentType(i, context),
25752577
targetConv.getSILArgumentType(calleeArgIdx, context), {TAI});
25762578
Args.push_back(Arg);
25772579
calleeArgIdx += 1;
@@ -2606,7 +2608,7 @@ bool SimplifyCFG::simplifyTryApplyBlock(TryApplyInst *TAI) {
26062608
// Non-guaranteed values don't need use points when casting.
26072609
SILValue CastedResult;
26082610
std::tie(CastedResult, std::ignore) = castValueToABICompatibleType(
2609-
&Builder, Loc, NewAI, ResultTy, OrigResultTy, /*usePoints*/ {});
2611+
&Builder, PM, Loc, NewAI, ResultTy, OrigResultTy, /*usePoints*/ {});
26102612

26112613
BranchInst *branch = Builder.createBranch(Loc, NormalBB, { CastedResult });
26122614

@@ -3704,7 +3706,7 @@ bool SimplifyCFG::simplifyArgument(SILBasicBlock *BB, unsigned i) {
37043706
if (!A->hasOneUse())
37053707
return false;
37063708

3707-
auto *Use = *A->use_begin();
3709+
auto *Use = *lookThroughBorrowedFromUser(A)->use_begin();
37083710
auto *User = Use->getUser();
37093711

37103712
auto disableInOSSA = [](SingleValueInstruction *inst) {
@@ -3750,6 +3752,10 @@ bool SimplifyCFG::simplifyArgument(SILBasicBlock *BB, unsigned i) {
37503752
// Okay, we'll replace the BB arg with one with the right type, replace
37513753
// the uses in this block, and then rewrite the branch operands.
37523754
LLVM_DEBUG(llvm::dbgs() << "unwrap argument:" << *A);
3755+
if (auto *bfi = getBorrowedFromUser(A)) {
3756+
bfi->replaceAllUsesWith(A);
3757+
bfi->eraseFromParent();
3758+
}
37533759
A->replaceAllUsesWith(SILUndef::get(A->getType(), *BB->getParent()));
37543760
auto *NewArg = BB->replacePhiArgument(i, proj->getType(),
37553761
BB->getArgument(i)->getOwnershipKind());
@@ -3772,6 +3778,8 @@ bool SimplifyCFG::simplifyArgument(SILBasicBlock *BB, unsigned i) {
37723778

37733779
proj->eraseFromParent();
37743780

3781+
updateBorrowedFromPhis(PM, { NewArg });
3782+
37753783
return true;
37763784
}
37773785

lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static FullApplySite CloneApply(FullApplySite AI, SILValue SelfArg,
122122

123123
/// Insert monomorphic inline caches for a specific class or metatype
124124
/// type \p SubClassTy.
125-
static FullApplySite speculateMonomorphicTarget(FullApplySite AI,
125+
static FullApplySite speculateMonomorphicTarget(SILPassManager *pm, FullApplySite AI,
126126
CanType SubType, ClassDecl *CD,
127127
CanType ClassType,
128128
CheckedCastBranchInst *&CCBI) {
@@ -228,7 +228,7 @@ static FullApplySite speculateMonomorphicTarget(FullApplySite AI,
228228
++NumTargetsPredicted;
229229

230230
// Devirtualize the apply instruction on the identical path.
231-
auto NewInst = devirtualizeClassMethod(IdenAI, DownCastedClassInstance, CD,
231+
auto NewInst = devirtualizeClassMethod(pm, IdenAI, DownCastedClassInstance, CD,
232232
ClassType, nullptr)
233233
.first;
234234
assert(NewInst && "Expected to be able to devirtualize apply!");
@@ -378,7 +378,7 @@ static bool isDefaultCaseKnown(ClassHierarchyAnalysis *CHA,
378378

379379
/// Try to speculate the call target for the call \p AI. This function
380380
/// returns true if a change was made.
381-
static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
381+
static bool tryToSpeculateTarget(SILPassManager *pm, FullApplySite AI, ClassHierarchyAnalysis *CHA,
382382
OptRemark::Emitter &ORE) {
383383
ClassMethodInst *CMI = cast<ClassMethodInst>(AI.getCallee());
384384

@@ -409,7 +409,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
409409
ClassHierarchyAnalysis::ClassList Subs;
410410
if (isDefaultCaseKnown(CHA, AI, CD, Subs)) {
411411
auto NewInst =
412-
tryDevirtualizeClassMethod(AI, SubTypeValue, CD, ClassType, &ORE)
412+
tryDevirtualizeClassMethod(pm, AI, SubTypeValue, CD, ClassType, &ORE)
413413
.first;
414414
if (NewInst)
415415
deleteDevirtualizedApply(AI);
@@ -418,7 +418,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
418418

419419
LLVM_DEBUG(llvm::dbgs() << "Inserting monomorphic speculative call for "
420420
"class " << CD->getName() << "\n");
421-
return !!speculateMonomorphicTarget(AI, SubType, CD, ClassType, LastCCBI);
421+
return !!speculateMonomorphicTarget(pm, AI, SubType, CD, ClassType, LastCCBI);
422422
}
423423

424424
// True if any instructions were changed or generated.
@@ -454,7 +454,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
454454
}
455455

456456
auto FirstAI =
457-
speculateMonomorphicTarget(AI, SubType, CD, ClassType, LastCCBI);
457+
speculateMonomorphicTarget(pm, AI, SubType, CD, ClassType, LastCCBI);
458458
if (FirstAI) {
459459
Changed = true;
460460
AI = FirstAI;
@@ -516,7 +516,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
516516
}
517517

518518
// Pass the metatype of the subclass.
519-
auto NewAI = speculateMonomorphicTarget(AI, ClassOrMetatypeType, S,
519+
auto NewAI = speculateMonomorphicTarget(pm, AI, ClassOrMetatypeType, S,
520520
CanClassType, LastCCBI);
521521
if (!NewAI) {
522522
++NotHandledSubsNum;
@@ -573,7 +573,7 @@ static bool tryToSpeculateTarget(FullApplySite AI, ClassHierarchyAnalysis *CHA,
573573
return true;
574574
}
575575
auto NewInst =
576-
tryDevirtualizeClassMethod(AI, SubTypeValue, CD, ClassType, nullptr)
576+
tryDevirtualizeClassMethod(pm, AI, SubTypeValue, CD, ClassType, nullptr)
577577
.first;
578578
if (NewInst) {
579579
ORE.emit(RB);
@@ -622,7 +622,7 @@ namespace {
622622
OptRemark::Emitter ORE(DEBUG_TYPE, CurFn);
623623
// Go over the collected calls and try to insert speculative calls.
624624
for (auto AI : ToSpecialize)
625-
Changed |= tryToSpeculateTarget(AI, CHA, ORE);
625+
Changed |= tryToSpeculateTarget(getPassManager(), AI, CHA, ORE);
626626

627627
if (Changed) {
628628
CurFn.getModule().linkFunction(&CurFn, SILModule::LinkingMode::LinkAll);

0 commit comments

Comments
 (0)