Skip to content

[cast-opt] Allow users to pass in a SILBuilderContext to the CastOpti… #22355

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
13 changes: 12 additions & 1 deletion include/swift/SILOptimizer/Utils/CastOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
#include "swift/SILOptimizer/Analysis/EpilogueARCAnalysis.h"
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/Allocator.h"
#include <functional>
Expand All @@ -34,6 +35,12 @@ class SILOptFunctionBuilder;
class CastOptimizer {
SILOptFunctionBuilder &FunctionBuilder;

/// Temporary context for clients that do not provide their own.
SILBuilderContext TempBuilderContext;

/// Reference to the provided SILBuilderContext.
SILBuilderContext &BuilderContext;

/// Callback that replaces the first SILValue's uses with a use of the second
/// value.
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction;
Expand Down Expand Up @@ -75,13 +82,16 @@ class CastOptimizer {

public:
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
SILBuilderContext *BuilderContext,
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction,
std::function<void(SingleValueInstruction *, ValueBase *)>
ReplaceInstUsesAction,
std::function<void(SILInstruction *)> EraseAction,
std::function<void()> WillSucceedAction,
std::function<void()> WillFailAction = []() {})
: FunctionBuilder(FunctionBuilder),
TempBuilderContext(FunctionBuilder.getModule()),
BuilderContext(BuilderContext ? *BuilderContext : TempBuilderContext),
ReplaceValueUsesAction(ReplaceValueUsesAction),
ReplaceInstUsesAction(ReplaceInstUsesAction),
EraseInstAction(EraseAction), WillSucceedAction(WillSucceedAction),
Expand All @@ -93,12 +103,13 @@ class CastOptimizer {
// arguments. It seems the number of the default argument with lambda is
// limited.
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
SILBuilderContext *BuilderContext,
std::function<void(SILValue, SILValue)> ReplaceValueUsesAction,
std::function<void(SingleValueInstruction *I, ValueBase *V)>
ReplaceInstUsesAction,
std::function<void(SILInstruction *)> EraseAction =
[](SILInstruction *) {})
: CastOptimizer(FunctionBuilder, ReplaceValueUsesAction,
: CastOptimizer(FunctionBuilder, BuilderContext, ReplaceValueUsesAction,
ReplaceInstUsesAction, EraseAction, []() {}, []() {}) {}

/// Simplify checked_cast_br. It may change the control flow.
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SILOptimizer/Utils/SILOptFunctionBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class SILOptFunctionBuilder {
pm.getModule()->eraseFunction(f);
}

SILModule &getModule() const { return *getPassManager().getModule(); }

private:
SILPassManager &getPassManager() const {
return *transform.getPassManager();
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/SILCombiner/SILCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class SILCombiner :
bool removeCondFails)
: AA(AA), DA(DA), PCA(PCA), CHA(CHA), Worklist(), MadeChange(false),
RemoveCondFails(removeCondFails), Iteration(0), Builder(B),
CastOpt(FuncBuilder,
CastOpt(FuncBuilder, nullptr /*SILBuilderContext*/,
/* ReplaceValueUsesAction */
[&](SILValue Original, SILValue Replacement) {
replaceValueUsesWith(Original, Replacement);
Expand Down
6 changes: 3 additions & 3 deletions lib/SILOptimizer/Transforms/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ bool SimplifyCFG::simplifyCheckedCastBranchBlock(CheckedCastBranchInst *CCBI) {

bool MadeChange = false;
CastOptimizer CastOpt(
FuncBuilder,
FuncBuilder, nullptr /*SILBuilderContext*/,
/* ReplaceValueUsesAction */
[&MadeChange](SILValue oldValue, SILValue newValue) {
MadeChange = true;
Expand Down Expand Up @@ -1970,7 +1970,7 @@ bool SimplifyCFG::simplifyCheckedCastValueBranchBlock(

bool MadeChange = false;
CastOptimizer CastOpt(
FuncBuilder,
FuncBuilder, nullptr /*SILBuilderContext*/,
/* ReplaceValueUsesAction */
[&MadeChange](SILValue oldValue, SILValue newValue) {
MadeChange = true;
Expand Down Expand Up @@ -2006,7 +2006,7 @@ simplifyCheckedCastAddrBranchBlock(CheckedCastAddrBranchInst *CCABI) {

bool MadeChange = false;
CastOptimizer CastOpt(
FuncBuilder,
FuncBuilder, nullptr /*SILBuilderContext*/,
/* ReplaceValueUsesAction */
[&MadeChange](SILValue, SILValue) { MadeChange = true; },
/* ReplaceInstUsesAction */
Expand Down
44 changes: 23 additions & 21 deletions lib/SILOptimizer/Utils/CastOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ SILInstruction *CastOptimizer::optimizeBridgedObjCToSwiftCast(
CanType CanBridgedTy = BridgedTargetTy->getCanonicalType();
SILType SILBridgedTy = SILType::getPrimitiveObjectType(CanBridgedTy);

SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);
SILValue SrcOp;
SILInstruction *NewI = nullptr;

Expand Down Expand Up @@ -349,7 +349,7 @@ SILInstruction *CastOptimizer::optimizeBridgedSwiftToObjCCast(
(void)Conf;

// Generate code to invoke _bridgeToObjectiveC
SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);

auto *NTD = Source.getNominalOrBoundGenericNominal();
assert(NTD);
Expand Down Expand Up @@ -702,7 +702,7 @@ SILInstruction *CastOptimizer::simplifyCheckedCastAddrBranchInst(
auto *FailureBB = Inst->getFailureBB();
auto &Mod = Inst->getModule();

SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);

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

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

SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);

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

SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);

if (Feasibility == DynamicCastFeasibility::WillFail) {
auto *NewI = Builder.createBranch(Loc, FailureBB);
Expand Down Expand Up @@ -1065,7 +1065,7 @@ SILInstruction *CastOptimizer::optimizeCheckedCastAddrBranchInst(
canUseScalarCheckedCastInstructions(
Inst->getModule(), MI->getType().getASTType(),
Inst->getTargetType())) {
SILBuilderWithScope B(Inst);
SILBuilderWithScope B(Inst, BuilderContext);
auto NewI = B.createCheckedCastBranch(
Loc, false /*isExact*/, MI, Dest->getType().getObjectType(),
SuccessBB, FailureBB, Inst->getTrueBBCount(),
Expand Down Expand Up @@ -1114,7 +1114,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
// checked_cast_br %0 to ...
if (auto *IEMI = dyn_cast<InitExistentialMetatypeInst>(Op)) {
if (auto *MI = dyn_cast<MetatypeInst>(IEMI->getOperand())) {
SILBuilderWithScope B(Inst);
SILBuilderWithScope B(Inst, BuilderContext);
auto *NewI = B.createCheckedCastBranch(
Loc, /* isExact */ false, MI, LoweredTargetType, SuccessBB, FailureBB,
Inst->getTrueBBCount(), Inst->getFalseBBCount());
Expand Down Expand Up @@ -1177,7 +1177,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
EMT->getRepresentation());
auto CanMetaTy = CanTypeWrapper<MetatypeType>(MetaTy);
auto SILMetaTy = SILType::getPrimitiveObjectType(CanMetaTy);
SILBuilderWithScope B(Inst);
SILBuilderWithScope B(Inst, BuilderContext);
B.getOpenedArchetypes().addOpenedArchetypeOperands(
FoundIEI->getTypeDependentOperands());
auto *MI = B.createMetatype(FoundIEI->getLoc(), SILMetaTy);
Expand Down Expand Up @@ -1236,7 +1236,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
auto *MetaTy = MetatypeType::get(ConcreteTy, EMT->getRepresentation());
auto CanMetaTy = CanTypeWrapper<MetatypeType>(MetaTy);
auto SILMetaTy = SILType::getPrimitiveObjectType(CanMetaTy);
SILBuilderWithScope B(Inst);
SILBuilderWithScope B(Inst, BuilderContext);
B.getOpenedArchetypes().addOpenedArchetypeOperands(
FoundIERI->getTypeDependentOperands());
auto *MI = B.createMetatype(FoundIERI->getLoc(), SILMetaTy);
Expand Down Expand Up @@ -1271,7 +1271,7 @@ ValueBase *CastOptimizer::optimizeUnconditionalCheckedCastInst(
if (Feasibility == DynamicCastFeasibility::WillFail) {
// Remove the cast and insert a trap, followed by an
// unreachable instruction.
SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);
auto *Trap = Builder.createBuiltinTrap(Loc);
Inst->replaceAllUsesWithUndef();
EraseInstAction(Inst);
Expand All @@ -1296,7 +1296,7 @@ ValueBase *CastOptimizer::optimizeUnconditionalCheckedCastInst(
}
}

SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);

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

if (ResultNotUsed) {
SILBuilderWithScope B(Inst);
SILBuilderWithScope B(Inst, BuilderContext);
B.createDestroyAddr(Inst->getLoc(), Inst->getSrc());
if (DestroyDestInst)
EraseInstAction(DestroyDestInst);
Expand Down Expand Up @@ -1561,7 +1561,7 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
if (isBridgingCast(SourceType, TargetType))
return nullptr;

SILBuilderWithScope Builder(Inst);
SILBuilderWithScope Builder(Inst, BuilderContext);
if (!emitSuccessfulIndirectUnconditionalCast(Builder, Mod.getSwiftModule(),
Loc, Src, SourceType, Dest,
TargetType, Inst)) {
Expand Down Expand Up @@ -1596,19 +1596,21 @@ SILInstruction *CastOptimizer::optimizeMetatypeConversion(
return NewCast;
};
if (auto *MI = dyn_cast<MetatypeInst>(Op)) {
return replaceCast(
SILBuilderWithScope(MCI).createMetatype(MCI->getLoc(), Ty));
return replaceCast(SILBuilderWithScope(MCI, BuilderContext)
.createMetatype(MCI->getLoc(), Ty));
}
// For metatype instructions that require an operand, generate the new
// metatype at the same position as the original to avoid extending the
// lifetime of `Op` past its destroy.
if (auto *VMI = dyn_cast<ValueMetatypeInst>(Op)) {
return replaceCast(SILBuilderWithScope(VMI).createValueMetatype(
MCI->getLoc(), Ty, VMI->getOperand()));
return replaceCast(
SILBuilderWithScope(VMI, BuilderContext)
.createValueMetatype(MCI->getLoc(), Ty, VMI->getOperand()));
}
if (auto *EMI = dyn_cast<ExistentialMetatypeInst>(Op)) {
return replaceCast(SILBuilderWithScope(EMI).createExistentialMetatype(
MCI->getLoc(), Ty, EMI->getOperand()));
return replaceCast(
SILBuilderWithScope(EMI, BuilderContext)
.createExistentialMetatype(MCI->getLoc(), Ty, EMI->getOperand()));
}
return nullptr;
}
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Utils/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ ConstantFolder::processWorkList() {
// instruction from different entry points in the WorkList.
llvm::DenseSet<SILInstruction *> ErrorSet;
llvm::SetVector<SILInstruction *> FoldedUsers;
CastOptimizer CastOpt(FuncBuilder,
CastOptimizer CastOpt(FuncBuilder, nullptr /*SILBuilderContext*/,
/* ReplaceValueUsesAction */
[&](SILValue oldValue, SILValue newValue) {
InvalidateInstructions = true;
Expand Down