Skip to content

Commit b8cb40b

Browse files
authored
Merge pull request #18517 from gottesmm/pr-69d1270fb634f67dfbb02afed012bb5cada0dcc1
[passmanager] Change the optimizer to use SILOptFunctionBuilder.
2 parents 1623f42 + b723044 commit b8cb40b

26 files changed

+264
-135
lines changed

include/swift/SILOptimizer/PassManager/Transforms.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ namespace swift {
6666
/// Inject the pass manager running this pass.
6767
void injectPassManager(SILPassManager *PMM) { PM = PMM; }
6868

69+
SILPassManager *getPassManager() const { return PM; }
70+
6971
irgen::IRGenModule *getIRGenModule() {
7072
auto *Mod = PM->getIRGenModule();
7173
assert(Mod && "Expecting a valid module");

include/swift/SILOptimizer/Utils/CastOptimizer.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828

2929
namespace swift {
3030

31+
class SILOptFunctionBuilder;
32+
3133
/// \brief This is a helper class used to optimize casts.
3234
class CastOptimizer {
35+
SILOptFunctionBuilder &FunctionBuilder;
36+
3337
// Callback to be called when uses of an instruction should be replaced.
3438
std::function<void(SingleValueInstruction *I, ValueBase *V)>
3539
ReplaceInstUsesAction;
@@ -64,12 +68,13 @@ class CastOptimizer {
6468
SILInstruction *TrapInst);
6569

6670
public:
67-
CastOptimizer(std::function<void(SingleValueInstruction *I, ValueBase *V)>
71+
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
72+
std::function<void(SingleValueInstruction *I, ValueBase *V)>
6873
ReplaceInstUsesAction,
6974
std::function<void(SILInstruction *)> EraseAction,
7075
std::function<void()> WillSucceedAction,
7176
std::function<void()> WillFailAction = []() {})
72-
: ReplaceInstUsesAction(ReplaceInstUsesAction),
77+
: FunctionBuilder(FunctionBuilder), ReplaceInstUsesAction(ReplaceInstUsesAction),
7378
EraseInstAction(EraseAction), WillSucceedAction(WillSucceedAction),
7479
WillFailAction(WillFailAction) {}
7580

@@ -78,11 +83,12 @@ class CastOptimizer {
7883
// couldn't use the single constructor version which has three default
7984
// arguments. It seems the number of the default argument with lambda is
8085
// limited.
81-
CastOptimizer(std::function<void(SingleValueInstruction *I, ValueBase *V)>
86+
CastOptimizer(SILOptFunctionBuilder &FunctionBuilder,
87+
std::function<void(SingleValueInstruction *I, ValueBase *V)>
8288
ReplaceInstUsesAction,
8389
std::function<void(SILInstruction *)> EraseAction =
8490
[](SILInstruction *) {})
85-
: CastOptimizer(ReplaceInstUsesAction, EraseAction, []() {}, []() {}) {}
91+
: CastOptimizer(FunctionBuilder, ReplaceInstUsesAction, EraseAction, []() {}, []() {}) {}
8692

8793
/// Simplify checked_cast_br. It may change the control flow.
8894
SILInstruction *simplifyCheckedCastBranchInst(CheckedCastBranchInst *Inst);

include/swift/SILOptimizer/Utils/ConstantFolding.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace swift {
2626

27+
class SILOptFunctionBuilder;
28+
2729
/// Evaluates the constant result of a binary bit-operation.
2830
///
2931
/// The \p ID must be the ID of a binary bit-operation builtin.
@@ -54,6 +56,8 @@ APInt constantFoldCast(APInt val, const BuiltinInfo &BI);
5456
/// A utility class to do constant folding.
5557
class ConstantFolder {
5658
private:
59+
SILOptFunctionBuilder &FuncBuilder;
60+
5761
/// The worklist of the constants that could be folded into their users.
5862
llvm::SetVector<SILInstruction *> WorkList;
5963

@@ -75,10 +79,12 @@ class ConstantFolder {
7579
/// \param EnableDiagnostics Print diagnostics as part of mandatory constant
7680
/// propagation.
7781
/// \param Callback Called for each constant folded instruction.
78-
ConstantFolder(unsigned AssertConfiguration,
82+
ConstantFolder(SILOptFunctionBuilder &FuncBuilder,
83+
unsigned AssertConfiguration,
7984
bool EnableDiagnostics = false,
8085
std::function<void (SILInstruction *)> Callback =
81-
[](SILInstruction *){}) :
86+
[](SILInstruction *){}) :
87+
FuncBuilder(FuncBuilder),
8288
AssertConfiguration(AssertConfiguration),
8389
EnableDiagnostics(EnableDiagnostics),
8490
Callback(Callback) { }

include/swift/SILOptimizer/Utils/GenericCloner.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,30 @@ class GenericCloner : public TypeSubstCloner<GenericCloner> {
4040
public:
4141
friend class SILCloner<GenericCloner>;
4242

43-
GenericCloner(SILFunction *F,
43+
GenericCloner(SILOptFunctionBuilder &FuncBuilder,
44+
SILFunction *F,
4445
IsSerialized_t Serialized,
4546
const ReabstractionInfo &ReInfo,
4647
SubstitutionMap ParamSubs,
4748
StringRef NewName,
4849
CloneCollector::CallbackType Callback)
49-
: TypeSubstCloner(*initCloned(F, Serialized, ReInfo, NewName), *F,
50+
: TypeSubstCloner(*initCloned(FuncBuilder, F, Serialized, ReInfo, NewName), *F,
5051
ParamSubs), ReInfo(ReInfo), Callback(Callback) {
5152
assert(F->getDebugScope()->Parent != getCloned()->getDebugScope()->Parent);
5253
}
5354
/// Clone and remap the types in \p F according to the substitution
5455
/// list in \p Subs. Parameters are re-abstracted (changed from indirect to
5556
/// direct) according to \p ReInfo.
5657
static SILFunction *
57-
cloneFunction(SILFunction *F,
58+
cloneFunction(SILOptFunctionBuilder &FuncBuilder,
59+
SILFunction *F,
5860
IsSerialized_t Serialized,
5961
const ReabstractionInfo &ReInfo,
6062
SubstitutionMap ParamSubs,
6163
StringRef NewName,
6264
CloneCollector::CallbackType Callback =nullptr) {
6365
// Clone and specialize the function.
64-
GenericCloner SC(F, Serialized, ReInfo, ParamSubs,
66+
GenericCloner SC(FuncBuilder, F, Serialized, ReInfo, ParamSubs,
6567
NewName, Callback);
6668
SC.populateCloned();
6769
SC.cleanUp(SC.getCloned());
@@ -84,7 +86,8 @@ class GenericCloner : public TypeSubstCloner<GenericCloner> {
8486
}
8587

8688
private:
87-
static SILFunction *initCloned(SILFunction *Orig,
89+
static SILFunction *initCloned(SILOptFunctionBuilder &FuncBuilder,
90+
SILFunction *Orig,
8891
IsSerialized_t Serialized,
8992
const ReabstractionInfo &ReInfo,
9093
StringRef NewName);

include/swift/SILOptimizer/Utils/Generics.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace swift {
2929

3030
class FunctionSignaturePartialSpecializer;
31+
class SILOptFunctionBuilder;
3132

3233
namespace OptRemark {
3334
class Emitter;
@@ -41,6 +42,7 @@ class Emitter;
4142
///
4243
/// This is the top-level entry point for specializing an existing call site.
4344
void trySpecializeApplyOfGeneric(
45+
SILOptFunctionBuilder &FunctionBuilder,
4446
ApplySite Apply, DeadInstructionSet &DeadApplies,
4547
llvm::SmallVectorImpl<SILFunction *> &NewFunctions,
4648
OptRemark::Emitter &ORE);
@@ -257,6 +259,7 @@ class ReabstractionInfo {
257259
/// Helper class for specializing a generic function given a list of
258260
/// substitutions.
259261
class GenericFuncSpecializer {
262+
SILOptFunctionBuilder &FuncBuilder;
260263
SILModule &M;
261264
SILFunction *GenericFunc;
262265
SubstitutionMap ParamSubs;
@@ -267,7 +270,8 @@ class GenericFuncSpecializer {
267270
std::string ClonedName;
268271

269272
public:
270-
GenericFuncSpecializer(SILFunction *GenericFunc,
273+
GenericFuncSpecializer(SILOptFunctionBuilder &FuncBuilder,
274+
SILFunction *GenericFunc,
271275
SubstitutionMap ParamSubs,
272276
IsSerialized_t Serialized,
273277
const ReabstractionInfo &ReInfo);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===--- SILOptFunctionBuilder.h --------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SILOPTIMIZER_UTILS_SILOPTFUNCTIONBUILDER_H
14+
#define SWIFT_SILOPTIMIZER_UTILS_SILOPTFUNCTIONBUILDER_H
15+
16+
#include "swift/SIL/SILFunctionBuilder.h"
17+
#include "swift/SILOptimizer/PassManager/PassManager.h"
18+
19+
namespace swift {
20+
21+
class SILOptFunctionBuilder {
22+
SILFunctionBuilder builder;
23+
24+
public:
25+
SILOptFunctionBuilder(SILPassManager &passManager)
26+
: builder(*passManager.getModule()) {}
27+
28+
template <class... ArgTys>
29+
SILFunction *getOrCreateSharedFunction(ArgTys &&... args) {
30+
return builder.getOrCreateSharedFunction(std::forward<ArgTys>(args)...);
31+
}
32+
33+
template <class... ArgTys>
34+
SILFunction *getOrCreateFunction(ArgTys &&... args) {
35+
return builder.getOrCreateFunction(std::forward<ArgTys>(args)...);
36+
}
37+
38+
template <class... ArgTys> SILFunction *createFunction(ArgTys &&... args) {
39+
return builder.createFunction(std::forward<ArgTys>(args)...);
40+
}
41+
};
42+
43+
} // namespace swift
44+
45+
#endif

lib/SILOptimizer/FunctionSignatureTransforms/FunctionSignatureOpts.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "swift/SIL/DebugUtils.h"
3535
#include "swift/SIL/SILCloner.h"
3636
#include "swift/SIL/SILFunction.h"
37-
#include "swift/SIL/SILFunctionBuilder.h"
37+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
3838
#include "swift/SIL/SILValue.h"
3939
#include "swift/SILOptimizer/Analysis/ARCAnalysis.h"
4040
#include "swift/SILOptimizer/Analysis/CallerAnalysis.h"
@@ -495,8 +495,7 @@ void FunctionSignatureTransform::createFunctionSignatureOptimizedFunction() {
495495
// The specialized function is an internal detail, so we need to disconnect it
496496
// from a parent class, if one exists, thus the override of the
497497
// classSubclassScope.
498-
SILFunctionBuilder builder(M);
499-
TransformDescriptor.OptimizedFunction = builder.createFunction(
498+
TransformDescriptor.OptimizedFunction = FunctionBuilder.createFunction(
500499
linkage, Name, NewFTy, NewFGenericEnv, F->getLocation(), F->isBare(),
501500
F->isTransparent(), F->isSerialized(), F->getEntryCount(), F->isThunk(),
502501
/*classSubclassScope=*/SubclassScope::NotApplicable,
@@ -795,8 +794,9 @@ class FunctionSignatureOpts : public SILFunctionTransform {
795794
ResultDescList.emplace_back(IR);
796795
}
797796

797+
SILOptFunctionBuilder FuncBuilder(*getPassManager());
798798
// Owned to guaranteed optimization.
799-
FunctionSignatureTransform FST(F, RCIA, EA, Mangler, AIM,
799+
FunctionSignatureTransform FST(FuncBuilder, F, RCIA, EA, Mangler, AIM,
800800
ArgumentDescList, ResultDescList);
801801

802802
bool Changed = false;

lib/SILOptimizer/FunctionSignatureTransforms/FunctionSignatureOpts.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/SILOptimizer/Analysis/RCIdentityAnalysis.h"
2828
#include "swift/SILOptimizer/PassManager/PassManager.h"
2929
#include "swift/SILOptimizer/Utils/Local.h"
30+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
3031
#include "swift/SILOptimizer/Utils/SpecializationMangler.h"
3132
#include "llvm/ADT/DenseMap.h"
3233

@@ -216,6 +217,8 @@ struct FunctionSignatureTransformDescriptor {
216217
};
217218

218219
class FunctionSignatureTransform {
220+
SILOptFunctionBuilder &FunctionBuilder;
221+
219222
/// A struct that contains all data that we use during our
220223
/// transformation. This is an initial step towards splitting this struct into
221224
/// multiple "transforms" that can be tested independently of each other.
@@ -284,12 +287,14 @@ class FunctionSignatureTransform {
284287
public:
285288
/// Constructor.
286289
FunctionSignatureTransform(
290+
SILOptFunctionBuilder &FunctionBuilder,
287291
SILFunction *F, RCIdentityAnalysis *RCIA, EpilogueARCAnalysis *EA,
288292
Mangle::FunctionSignatureSpecializationMangler &Mangler,
289293
llvm::SmallDenseMap<int, int> &AIM,
290294
llvm::SmallVector<ArgumentDescriptor, 4> &ADL,
291295
llvm::SmallVector<ResultDescriptor, 4> &RDL)
292-
: TransformDescriptor{F, nullptr, AIM, false, ADL, RDL}, RCIA(RCIA),
296+
: FunctionBuilder(FunctionBuilder),
297+
TransformDescriptor{F, nullptr, AIM, false, ADL, RDL}, RCIA(RCIA),
293298
EA(EA) {}
294299

295300
/// Return the optimized function.

lib/SILOptimizer/IPO/CapturePromotion.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#define DEBUG_TYPE "sil-capture-promotion"
4646
#include "swift/AST/GenericEnvironment.h"
4747
#include "swift/SIL/SILCloner.h"
48-
#include "swift/SIL/SILFunctionBuilder.h"
48+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
4949
#include "swift/SIL/TypeSubstCloner.h"
5050
#include "swift/SILOptimizer/PassManager/Passes.h"
5151
#include "swift/SILOptimizer/PassManager/Transforms.h"
@@ -197,7 +197,8 @@ class ClosureCloner : public SILClonerWithScopes<ClosureCloner> {
197197
friend class SILInstructionVisitor<ClosureCloner>;
198198
friend class SILCloner<ClosureCloner>;
199199

200-
ClosureCloner(SILFunction *Orig, IsSerialized_t Serialized,
200+
ClosureCloner(SILOptFunctionBuilder &FuncBuilder,
201+
SILFunction *Orig, IsSerialized_t Serialized,
201202
StringRef ClonedName,
202203
IndicesSet &PromotableIndices);
203204

@@ -206,7 +207,8 @@ class ClosureCloner : public SILClonerWithScopes<ClosureCloner> {
206207
SILFunction *getCloned() { return &getBuilder().getFunction(); }
207208

208209
private:
209-
static SILFunction *initCloned(SILFunction *Orig, IsSerialized_t Serialized,
210+
static SILFunction *initCloned(SILOptFunctionBuilder &FuncBuilder,
211+
SILFunction *Orig, IsSerialized_t Serialized,
210212
StringRef ClonedName,
211213
IndicesSet &PromotableIndices);
212214

@@ -306,11 +308,12 @@ ReachabilityInfo::isReachable(SILBasicBlock *From, SILBasicBlock *To) {
306308
return FromSet.test(FI->second);
307309
}
308310

309-
ClosureCloner::ClosureCloner(SILFunction *Orig, IsSerialized_t Serialized,
311+
ClosureCloner::ClosureCloner(SILOptFunctionBuilder &FuncBuilder,
312+
SILFunction *Orig, IsSerialized_t Serialized,
310313
StringRef ClonedName,
311314
IndicesSet &PromotableIndices)
312315
: SILClonerWithScopes<ClosureCloner>(
313-
*initCloned(Orig, Serialized, ClonedName, PromotableIndices)),
316+
*initCloned(FuncBuilder, Orig, Serialized, ClonedName, PromotableIndices)),
314317
Orig(Orig), PromotableIndices(PromotableIndices) {
315318
assert(Orig->getDebugScope()->Parent != getCloned()->getDebugScope()->Parent);
316319
}
@@ -403,7 +406,8 @@ static std::string getSpecializedName(SILFunction *F,
403406
/// *NOTE* PromotableIndices only contains the container value of the box, not
404407
/// the address value.
405408
SILFunction*
406-
ClosureCloner::initCloned(SILFunction *Orig, IsSerialized_t Serialized,
409+
ClosureCloner::initCloned(SILOptFunctionBuilder &FunctionBuilder,
410+
SILFunction *Orig, IsSerialized_t Serialized,
407411
StringRef ClonedName,
408412
IndicesSet &PromotableIndices) {
409413
SILModule &M = Orig->getModule();
@@ -428,8 +432,7 @@ ClosureCloner::initCloned(SILFunction *Orig, IsSerialized_t Serialized,
428432
&& "SILFunction missing DebugScope");
429433
assert(!Orig->isGlobalInit() && "Global initializer cannot be cloned");
430434

431-
SILFunctionBuilder builder(M);
432-
auto *Fn = builder.createFunction(
435+
auto *Fn = FunctionBuilder.createFunction(
433436
Orig->getLinkage(), ClonedName, ClonedTy, Orig->getGenericEnvironment(),
434437
Orig->getLocation(), Orig->isBare(), IsNotTransparent, Serialized,
435438
Orig->getEntryCount(), Orig->isThunk(), Orig->getClassSubclassScope(),
@@ -1130,7 +1133,8 @@ examineAllocBoxInst(AllocBoxInst *ABI, ReachabilityInfo &RI,
11301133
}
11311134

11321135
static SILFunction *
1133-
constructClonedFunction(PartialApplyInst *PAI, FunctionRefInst *FRI,
1136+
constructClonedFunction(SILOptFunctionBuilder &FuncBuilder,
1137+
PartialApplyInst *PAI, FunctionRefInst *FRI,
11341138
IndicesSet &PromotableIndices) {
11351139
SILFunction *F = PAI->getFunction();
11361140

@@ -1150,7 +1154,7 @@ constructClonedFunction(PartialApplyInst *PAI, FunctionRefInst *FRI,
11501154
}
11511155

11521156
// Otherwise, create a new clone.
1153-
ClosureCloner cloner(Orig, Serialized, ClonedName, PromotableIndices);
1157+
ClosureCloner cloner(FuncBuilder, Orig, Serialized, ClonedName, PromotableIndices);
11541158
cloner.populateCloned();
11551159
return cloner.getCloned();
11561160
}
@@ -1203,14 +1207,15 @@ static SILValue getOrCreateProjectBoxHelper(SILValue PartialOperand) {
12031207
/// necessary. Also, if the closure is cloned, the cloned function is added to
12041208
/// the worklist.
12051209
static SILFunction *
1206-
processPartialApplyInst(PartialApplyInst *PAI, IndicesSet &PromotableIndices,
1210+
processPartialApplyInst(SILOptFunctionBuilder &FuncBuilder,
1211+
PartialApplyInst *PAI, IndicesSet &PromotableIndices,
12071212
SmallVectorImpl<SILFunction*> &Worklist) {
12081213
SILModule &M = PAI->getModule();
12091214

12101215
auto *FRI = dyn_cast<FunctionRefInst>(PAI->getCallee());
12111216

12121217
// Clone the closure with the given promoted captures.
1213-
SILFunction *ClonedFn = constructClonedFunction(PAI, FRI, PromotableIndices);
1218+
SILFunction *ClonedFn = constructClonedFunction(FuncBuilder, PAI, FRI, PromotableIndices);
12141219
Worklist.push_back(ClonedFn);
12151220

12161221
// Initialize a SILBuilder and create a function_ref referencing the cloned
@@ -1338,9 +1343,11 @@ void CapturePromotionPass::processFunction(SILFunction *F,
13381343

13391344
// Do the actual promotions; all promotions on a single partial_apply are
13401345
// handled together.
1346+
SILOptFunctionBuilder FuncBuilder(*getPassManager());
13411347
for (auto &IndicesPair : IndicesMap) {
13421348
PartialApplyInst *PAI = IndicesPair.first;
1343-
SILFunction *ClonedFn = processPartialApplyInst(PAI, IndicesPair.second,
1349+
SILFunction *ClonedFn = processPartialApplyInst(FuncBuilder,
1350+
PAI, IndicesPair.second,
13441351
Worklist);
13451352
notifyAddFunction(ClonedFn);
13461353
}

0 commit comments

Comments
 (0)