Skip to content

Commit 4dabf9f

Browse files
authored
Merge pull request #22355 from gottesmm/pr-da7a380ffb527032db33e17037611065c0430078
2 parents b0f5815 + 21e51ed commit 4dabf9f

File tree

6 files changed

+42
-27
lines changed

6 files changed

+42
-27
lines changed

include/swift/SILOptimizer/Utils/CastOptimizer.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
2222
#include "swift/SILOptimizer/Analysis/EpilogueARCAnalysis.h"
2323
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
24+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
2425
#include "llvm/ADT/SmallPtrSet.h"
2526
#include "llvm/Support/Allocator.h"
2627
#include <functional>
@@ -34,6 +35,12 @@ class SILOptFunctionBuilder;
3435
class CastOptimizer {
3536
SILOptFunctionBuilder &FunctionBuilder;
3637

38+
/// Temporary context for clients that do not provide their own.
39+
SILBuilderContext TempBuilderContext;
40+
41+
/// Reference to the provided SILBuilderContext.
42+
SILBuilderContext &BuilderContext;
43+
3744
/// Callback that replaces the first SILValue's uses with a use of the second
3845
/// value.
3946
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction;
@@ -75,13 +82,16 @@ class CastOptimizer {
7582

7683
public:
7784
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
85+
SILBuilderContext *BuilderContext,
7886
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction,
7987
std::function<void(SingleValueInstruction *, ValueBase *)>
8088
ReplaceInstUsesAction,
8189
std::function<void(SILInstruction *)> EraseAction,
8290
std::function<void()> WillSucceedAction,
8391
std::function<void()> WillFailAction = []() {})
8492
: FunctionBuilder(FunctionBuilder),
93+
TempBuilderContext(FunctionBuilder.getModule()),
94+
BuilderContext(BuilderContext ? *BuilderContext : TempBuilderContext),
8595
ReplaceValueUsesAction(ReplaceValueUsesAction),
8696
ReplaceInstUsesAction(ReplaceInstUsesAction),
8797
EraseInstAction(EraseAction), WillSucceedAction(WillSucceedAction),
@@ -93,12 +103,13 @@ class CastOptimizer {
93103
// arguments. It seems the number of the default argument with lambda is
94104
// limited.
95105
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
106+
SILBuilderContext *BuilderContext,
96107
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction,
97108
std::function<void(SingleValueInstruction *I, ValueBase *V)>
98109
ReplaceInstUsesAction,
99110
std::function<void(SILInstruction *)> EraseAction =
100111
[](SILInstruction *) {})
101-
: CastOptimizer(FunctionBuilder, ReplaceValueUsesAction,
112+
: CastOptimizer(FunctionBuilder, BuilderContext, ReplaceValueUsesAction,
102113
ReplaceInstUsesAction, EraseAction, []() {}, []() {}) {}
103114

104115
/// Simplify checked_cast_br. It may change the control flow.

include/swift/SILOptimizer/Utils/SILOptFunctionBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class SILOptFunctionBuilder {
5555
pm.getModule()->eraseFunction(f);
5656
}
5757

58+
SILModule &getModule() const { return *getPassManager().getModule(); }
59+
5860
private:
5961
SILPassManager &getPassManager() const {
6062
return *transform.getPassManager();

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class SILCombiner :
158158
bool removeCondFails)
159159
: AA(AA), DA(DA), PCA(PCA), CHA(CHA), Worklist(), MadeChange(false),
160160
RemoveCondFails(removeCondFails), Iteration(0), Builder(B),
161-
CastOpt(FuncBuilder,
161+
CastOpt(FuncBuilder, nullptr /*SILBuilderContext*/,
162162
/* ReplaceValueUsesAction */
163163
[&](SILValue Original, SILValue Replacement) {
164164
replaceValueUsesWith(Original, Replacement);

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ bool SimplifyCFG::simplifyCheckedCastBranchBlock(CheckedCastBranchInst *CCBI) {
19351935

19361936
bool MadeChange = false;
19371937
CastOptimizer CastOpt(
1938-
FuncBuilder,
1938+
FuncBuilder, nullptr /*SILBuilderContext*/,
19391939
/* ReplaceValueUsesAction */
19401940
[&MadeChange](SILValue oldValue, SILValue newValue) {
19411941
MadeChange = true;
@@ -1970,7 +1970,7 @@ bool SimplifyCFG::simplifyCheckedCastValueBranchBlock(
19701970

19711971
bool MadeChange = false;
19721972
CastOptimizer CastOpt(
1973-
FuncBuilder,
1973+
FuncBuilder, nullptr /*SILBuilderContext*/,
19741974
/* ReplaceValueUsesAction */
19751975
[&MadeChange](SILValue oldValue, SILValue newValue) {
19761976
MadeChange = true;
@@ -2006,7 +2006,7 @@ simplifyCheckedCastAddrBranchBlock(CheckedCastAddrBranchInst *CCABI) {
20062006

20072007
bool MadeChange = false;
20082008
CastOptimizer CastOpt(
2009-
FuncBuilder,
2009+
FuncBuilder, nullptr /*SILBuilderContext*/,
20102010
/* ReplaceValueUsesAction */
20112011
[&MadeChange](SILValue, SILValue) { MadeChange = true; },
20122012
/* ReplaceInstUsesAction */

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ SILInstruction *CastOptimizer::optimizeBridgedObjCToSwiftCast(
9898
CanType CanBridgedTy = BridgedTargetTy->getCanonicalType();
9999
SILType SILBridgedTy = SILType::getPrimitiveObjectType(CanBridgedTy);
100100

101-
SILBuilderWithScope Builder(Inst);
101+
SILBuilderWithScope Builder(Inst, BuilderContext);
102102
SILValue SrcOp;
103103
SILInstruction *NewI = nullptr;
104104

@@ -349,7 +349,7 @@ SILInstruction *CastOptimizer::optimizeBridgedSwiftToObjCCast(
349349
(void)Conf;
350350

351351
// Generate code to invoke _bridgeToObjectiveC
352-
SILBuilderWithScope Builder(Inst);
352+
SILBuilderWithScope Builder(Inst, BuilderContext);
353353

354354
auto *NTD = Source.getNominalOrBoundGenericNominal();
355355
assert(NTD);
@@ -702,7 +702,7 @@ SILInstruction *CastOptimizer::simplifyCheckedCastAddrBranchInst(
702702
auto *FailureBB = Inst->getFailureBB();
703703
auto &Mod = Inst->getModule();
704704

705-
SILBuilderWithScope Builder(Inst);
705+
SILBuilderWithScope Builder(Inst, BuilderContext);
706706

707707
// Try to determine the outcome of the cast from a known type
708708
// to a protocol type at compile-time.
@@ -816,7 +816,7 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
816816
return nullptr;
817817

818818
// We know the dynamic type of the operand.
819-
SILBuilderWithScope Builder(Inst);
819+
SILBuilderWithScope Builder(Inst, BuilderContext);
820820
auto Loc = Inst->getLoc();
821821
auto *SuccessBB = Inst->getSuccessBB();
822822
auto *FailureBB = Inst->getFailureBB();
@@ -858,7 +858,7 @@ CastOptimizer::simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
858858
auto Feasibility = classifyDynamicCast(Mod.getSwiftModule(), SourceType,
859859
TargetType, isSourceTypeExact);
860860

861-
SILBuilderWithScope Builder(Inst);
861+
SILBuilderWithScope Builder(Inst, BuilderContext);
862862

863863
if (Feasibility == DynamicCastFeasibility::WillFail) {
864864
auto *NewI = Builder.createBranch(Loc, FailureBB);
@@ -943,7 +943,7 @@ SILInstruction *CastOptimizer::simplifyCheckedCastValueBranchInst(
943943
auto Feasibility = classifyDynamicCast(Mod.getSwiftModule(), SourceType,
944944
TargetType, isSourceTypeExact);
945945

946-
SILBuilderWithScope Builder(Inst);
946+
SILBuilderWithScope Builder(Inst, BuilderContext);
947947

948948
if (Feasibility == DynamicCastFeasibility::WillFail) {
949949
auto *NewI = Builder.createBranch(Loc, FailureBB);
@@ -1065,7 +1065,7 @@ SILInstruction *CastOptimizer::optimizeCheckedCastAddrBranchInst(
10651065
canUseScalarCheckedCastInstructions(
10661066
Inst->getModule(), MI->getType().getASTType(),
10671067
Inst->getTargetType())) {
1068-
SILBuilderWithScope B(Inst);
1068+
SILBuilderWithScope B(Inst, BuilderContext);
10691069
auto NewI = B.createCheckedCastBranch(
10701070
Loc, false /*isExact*/, MI, Dest->getType().getObjectType(),
10711071
SuccessBB, FailureBB, Inst->getTrueBBCount(),
@@ -1114,7 +1114,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
11141114
// checked_cast_br %0 to ...
11151115
if (auto *IEMI = dyn_cast<InitExistentialMetatypeInst>(Op)) {
11161116
if (auto *MI = dyn_cast<MetatypeInst>(IEMI->getOperand())) {
1117-
SILBuilderWithScope B(Inst);
1117+
SILBuilderWithScope B(Inst, BuilderContext);
11181118
auto *NewI = B.createCheckedCastBranch(
11191119
Loc, /* isExact */ false, MI, LoweredTargetType, SuccessBB, FailureBB,
11201120
Inst->getTrueBBCount(), Inst->getFalseBBCount());
@@ -1177,7 +1177,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
11771177
EMT->getRepresentation());
11781178
auto CanMetaTy = CanTypeWrapper<MetatypeType>(MetaTy);
11791179
auto SILMetaTy = SILType::getPrimitiveObjectType(CanMetaTy);
1180-
SILBuilderWithScope B(Inst);
1180+
SILBuilderWithScope B(Inst, BuilderContext);
11811181
B.getOpenedArchetypes().addOpenedArchetypeOperands(
11821182
FoundIEI->getTypeDependentOperands());
11831183
auto *MI = B.createMetatype(FoundIEI->getLoc(), SILMetaTy);
@@ -1236,7 +1236,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
12361236
auto *MetaTy = MetatypeType::get(ConcreteTy, EMT->getRepresentation());
12371237
auto CanMetaTy = CanTypeWrapper<MetatypeType>(MetaTy);
12381238
auto SILMetaTy = SILType::getPrimitiveObjectType(CanMetaTy);
1239-
SILBuilderWithScope B(Inst);
1239+
SILBuilderWithScope B(Inst, BuilderContext);
12401240
B.getOpenedArchetypes().addOpenedArchetypeOperands(
12411241
FoundIERI->getTypeDependentOperands());
12421242
auto *MI = B.createMetatype(FoundIERI->getLoc(), SILMetaTy);
@@ -1271,7 +1271,7 @@ ValueBase *CastOptimizer::optimizeUnconditionalCheckedCastInst(
12711271
if (Feasibility == DynamicCastFeasibility::WillFail) {
12721272
// Remove the cast and insert a trap, followed by an
12731273
// unreachable instruction.
1274-
SILBuilderWithScope Builder(Inst);
1274+
SILBuilderWithScope Builder(Inst, BuilderContext);
12751275
auto *Trap = Builder.createBuiltinTrap(Loc);
12761276
Inst->replaceAllUsesWithUndef();
12771277
EraseInstAction(Inst);
@@ -1296,7 +1296,7 @@ ValueBase *CastOptimizer::optimizeUnconditionalCheckedCastInst(
12961296
}
12971297
}
12981298

1299-
SILBuilderWithScope Builder(Inst);
1299+
SILBuilderWithScope Builder(Inst, BuilderContext);
13001300

13011301
// Try to apply the bridged casts optimizations
13021302
auto SourceType = LoweredSourceType.getASTType();
@@ -1481,7 +1481,7 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
14811481
if (Feasibility == DynamicCastFeasibility::WillFail) {
14821482
// Remove the cast and insert a trap, followed by an
14831483
// unreachable instruction.
1484-
SILBuilderWithScope Builder(Inst);
1484+
SILBuilderWithScope Builder(Inst, BuilderContext);
14851485
// mem2reg's invariants get unhappy if we don't try to
14861486
// initialize a loadable result.
14871487
auto DestType = Dest->getType();
@@ -1529,7 +1529,7 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
15291529
}
15301530

15311531
if (ResultNotUsed) {
1532-
SILBuilderWithScope B(Inst);
1532+
SILBuilderWithScope B(Inst, BuilderContext);
15331533
B.createDestroyAddr(Inst->getLoc(), Inst->getSrc());
15341534
if (DestroyDestInst)
15351535
EraseInstAction(DestroyDestInst);
@@ -1561,7 +1561,7 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
15611561
if (isBridgingCast(SourceType, TargetType))
15621562
return nullptr;
15631563

1564-
SILBuilderWithScope Builder(Inst);
1564+
SILBuilderWithScope Builder(Inst, BuilderContext);
15651565
if (!emitSuccessfulIndirectUnconditionalCast(Builder, Mod.getSwiftModule(),
15661566
Loc, Src, SourceType, Dest,
15671567
TargetType, Inst)) {
@@ -1596,19 +1596,21 @@ SILInstruction *CastOptimizer::optimizeMetatypeConversion(
15961596
return NewCast;
15971597
};
15981598
if (auto *MI = dyn_cast<MetatypeInst>(Op)) {
1599-
return replaceCast(
1600-
SILBuilderWithScope(MCI).createMetatype(MCI->getLoc(), Ty));
1599+
return replaceCast(SILBuilderWithScope(MCI, BuilderContext)
1600+
.createMetatype(MCI->getLoc(), Ty));
16011601
}
16021602
// For metatype instructions that require an operand, generate the new
16031603
// metatype at the same position as the original to avoid extending the
16041604
// lifetime of `Op` past its destroy.
16051605
if (auto *VMI = dyn_cast<ValueMetatypeInst>(Op)) {
1606-
return replaceCast(SILBuilderWithScope(VMI).createValueMetatype(
1607-
MCI->getLoc(), Ty, VMI->getOperand()));
1606+
return replaceCast(
1607+
SILBuilderWithScope(VMI, BuilderContext)
1608+
.createValueMetatype(MCI->getLoc(), Ty, VMI->getOperand()));
16081609
}
16091610
if (auto *EMI = dyn_cast<ExistentialMetatypeInst>(Op)) {
1610-
return replaceCast(SILBuilderWithScope(EMI).createExistentialMetatype(
1611-
MCI->getLoc(), Ty, EMI->getOperand()));
1611+
return replaceCast(
1612+
SILBuilderWithScope(EMI, BuilderContext)
1613+
.createExistentialMetatype(MCI->getLoc(), Ty, EMI->getOperand()));
16121614
}
16131615
return nullptr;
16141616
}

lib/SILOptimizer/Utils/ConstantFolding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ ConstantFolder::processWorkList() {
14381438
// instruction from different entry points in the WorkList.
14391439
llvm::DenseSet<SILInstruction *> ErrorSet;
14401440
llvm::SetVector<SILInstruction *> FoldedUsers;
1441-
CastOptimizer CastOpt(FuncBuilder,
1441+
CastOptimizer CastOpt(FuncBuilder, nullptr /*SILBuilderContext*/,
14421442
/* ReplaceValueUsesAction */
14431443
[&](SILValue oldValue, SILValue newValue) {
14441444
InvalidateInstructions = true;

0 commit comments

Comments
 (0)