Skip to content

Commit 9322d45

Browse files
authored
Merge pull request swiftlang#18519 from gottesmm/pr-72542d14e7d0af720589446b3f02f6bb867b4bea
2 parents 1ccad42 + f500f00 commit 9322d45

File tree

8 files changed

+52
-28
lines changed

8 files changed

+52
-28
lines changed

include/swift/SIL/TypeSubstCloner.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@
3030

3131
namespace swift {
3232

33-
/// TypeSubstCloner - a utility class for cloning code while remapping types.
34-
template<typename ImplClass>
33+
/// \brief A utility class for cloning code while remapping types.
34+
///
35+
/// \tparam FunctionBuilderTy Function builder type injected by
36+
/// subclasses. Used to break a circular dependency from SIL <=>
37+
/// SILOptimizer that would be caused by us needing to use
38+
/// SILOptFunctionBuilder here.
39+
template<typename ImplClass, typename FunctionBuilderTy>
3540
class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
3641
friend class SILInstructionVisitor<ImplClass>;
3742
friend class SILCloner<ImplClass>;
@@ -158,7 +163,6 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
158163
Inlining(Inlining) {
159164
}
160165

161-
162166
protected:
163167
SILType remapType(SILType Ty) {
164168
SILType &Sty = TypeCache[Ty];
@@ -309,7 +313,8 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
309313
/// necessary when inlining said function into a new generic context.
310314
/// \param SubsMap - the substitutions of the inlining/specialization process.
311315
/// \param RemappedSig - the generic signature.
312-
static SILFunction *remapParentFunction(SILModule &M,
316+
static SILFunction *remapParentFunction(FunctionBuilderTy &FuncBuilder,
317+
SILModule &M,
313318
SILFunction *ParentFunction,
314319
SubstitutionMap SubsMap,
315320
GenericSignature *RemappedSig,
@@ -344,8 +349,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
344349
// Create a new function with this mangled name with an empty
345350
// body. There won't be any IR generated for it (hence the linkage),
346351
// but the symbol will be refered to by the debug info metadata.
347-
SILFunctionBuilder B(M);
348-
ParentFunction = B.getOrCreateFunction(
352+
ParentFunction = FuncBuilder.getOrCreateFunction(
349353
ParentFunction->getLocation(), MangledName, SILLinkage::Shared,
350354
ParentFunction->getLoweredFunctionType(), ParentFunction->isBare(),
351355
ParentFunction->isTransparent(), ParentFunction->isSerialized(), 0,
@@ -375,7 +379,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
375379
SILFunction &Original;
376380
/// True, if used for inlining.
377381
bool Inlining;
378-
};
382+
};
379383

380384
} // end namespace swift
381385

include/swift/SILOptimizer/Utils/GenericCloner.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030

3131
namespace swift {
3232

33-
class GenericCloner : public TypeSubstCloner<GenericCloner> {
33+
class GenericCloner
34+
: public TypeSubstCloner<GenericCloner, SILOptFunctionBuilder> {
35+
using SuperTy = TypeSubstCloner<GenericCloner, SILOptFunctionBuilder>;
36+
37+
SILOptFunctionBuilder &FuncBuilder;
3438
IsSerialized_t Serialized;
3539
const ReabstractionInfo &ReInfo;
3640
CloneCollector::CallbackType Callback;
@@ -47,8 +51,8 @@ class GenericCloner : public TypeSubstCloner<GenericCloner> {
4751
SubstitutionMap ParamSubs,
4852
StringRef NewName,
4953
CloneCollector::CallbackType Callback)
50-
: TypeSubstCloner(*initCloned(FuncBuilder, F, Serialized, ReInfo, NewName), *F,
51-
ParamSubs), ReInfo(ReInfo), Callback(Callback) {
54+
: SuperTy(*initCloned(FuncBuilder, F, Serialized, ReInfo, NewName), *F,
55+
ParamSubs), FuncBuilder(FuncBuilder), ReInfo(ReInfo), Callback(Callback) {
5256
assert(F->getDebugScope()->Parent != getCloned()->getDebugScope()->Parent);
5357
}
5458
/// Clone and remap the types in \p F according to the substitution

include/swift/SILOptimizer/Utils/SILInliner.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "llvm/ADT/DenseMap.h"
2222
#include "swift/SIL/TypeSubstCloner.h"
23+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
2324
#include <functional>
2425

2526
namespace swift {
@@ -35,9 +36,10 @@ enum class InlineCost : unsigned {
3536
/// disappear at the LLVM IR level are assigned a cost of 'Free'.
3637
InlineCost instructionInlineCost(SILInstruction &I);
3738

38-
class SILInliner : public TypeSubstCloner<SILInliner> {
39+
class SILInliner : public TypeSubstCloner<SILInliner, SILOptFunctionBuilder> {
3940
friend class SILInstructionVisitor<SILInliner>;
4041
friend class SILCloner<SILInliner>;
42+
using SuperTy = TypeSubstCloner<SILInliner, SILOptFunctionBuilder>;
4143

4244
public:
4345
enum class InlineKind { MandatoryInline, PerformanceInline };
@@ -62,15 +64,17 @@ class SILInliner : public TypeSubstCloner<SILInliner> {
6264
llvm::SmallDenseMap<const SILDebugScope *, const SILDebugScope *, 8>
6365
InlinedScopeCache;
6466
CloneCollector::CallbackType Callback;
67+
SILOptFunctionBuilder &FuncBuilder;
6568

6669
public:
67-
SILInliner(SILFunction &To, SILFunction &From, InlineKind IKind,
70+
SILInliner(SILOptFunctionBuilder &FuncBuilder,
71+
SILFunction &To, SILFunction &From, InlineKind IKind,
6872
SubstitutionMap ApplySubs,
6973
SILOpenedArchetypesTracker &OpenedArchetypesTracker,
7074
CloneCollector::CallbackType Callback = nullptr)
71-
: TypeSubstCloner<SILInliner>(To, From, ApplySubs,
72-
OpenedArchetypesTracker, true),
73-
IKind(IKind), CalleeFunction(&Original), Callback(Callback) {
75+
: SuperTy(To, From, ApplySubs, OpenedArchetypesTracker, true),
76+
IKind(IKind), CalleeFunction(&Original), Callback(Callback),
77+
FuncBuilder(FuncBuilder) {
7478
// CalleeEntryBB is initialized later in case the callee is modified.
7579
}
7680

lib/SILOptimizer/IPO/CapturePropagation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ namespace {
9393
/// caller, so the cloned function will have a mix of locations from different
9494
/// functions.
9595
class CapturePropagationCloner
96-
: public TypeSubstCloner<CapturePropagationCloner> {
97-
using SuperTy = TypeSubstCloner<CapturePropagationCloner>;
96+
: public TypeSubstCloner<CapturePropagationCloner, SILOptFunctionBuilder> {
97+
using SuperTy =
98+
TypeSubstCloner<CapturePropagationCloner, SILOptFunctionBuilder>;
9899
friend class SILInstructionVisitor<CapturePropagationCloner>;
99100
friend class SILCloner<CapturePropagationCloner>;
100101

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/SILOptimizer/Utils/CFG.h"
2020
#include "swift/SILOptimizer/Utils/Devirtualize.h"
2121
#include "swift/SILOptimizer/Utils/Local.h"
22+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
2223
#include "swift/SILOptimizer/Utils/SILInliner.h"
2324
#include "llvm/ADT/DenseSet.h"
2425
#include "llvm/ADT/ImmutableSet.h"
@@ -462,7 +463,8 @@ tryDevirtualizeApplyHelper(FullApplySite InnerAI, SILBasicBlock::iterator I,
462463
///
463464
/// \returns true if successful, false if failed due to circular inlining.
464465
static bool
465-
runOnFunctionRecursively(SILFunction *F, FullApplySite AI,
466+
runOnFunctionRecursively(SILOptFunctionBuilder &FuncBuilder,
467+
SILFunction *F, FullApplySite AI,
466468
DenseFunctionSet &FullyInlinedSet,
467469
ImmutableFunctionSet::Factory &SetFactory,
468470
ImmutableFunctionSet CurrentInliningSet,
@@ -516,7 +518,7 @@ runOnFunctionRecursively(SILFunction *F, FullApplySite AI,
516518
continue;
517519

518520
// Then recursively process it first before trying to inline it.
519-
if (!runOnFunctionRecursively(CalleeFunction, InnerAI,
521+
if (!runOnFunctionRecursively(FuncBuilder, CalleeFunction, InnerAI,
520522
FullyInlinedSet, SetFactory,
521523
CurrentInliningSet, CHA)) {
522524
// If we failed due to circular inlining, then emit some notes to
@@ -549,7 +551,7 @@ runOnFunctionRecursively(SILFunction *F, FullApplySite AI,
549551
OpenedArchetypesTracker.registerUsedOpenedArchetypes(PAI);
550552
}
551553

552-
SILInliner Inliner(*F, *CalleeFunction,
554+
SILInliner Inliner(FuncBuilder, *F, *CalleeFunction,
553555
SILInliner::InlineKind::MandatoryInline, Subs,
554556
OpenedArchetypesTracker);
555557
if (!Inliner.canInlineFunction(InnerAI)) {
@@ -629,6 +631,7 @@ class MandatoryInlining : public SILModuleTransform {
629631
DenseFunctionSet FullyInlinedSet;
630632
ImmutableFunctionSet::Factory SetFactory;
631633

634+
SILOptFunctionBuilder FuncBuilder(*getPassManager());
632635
for (auto &F : *M) {
633636
// Don't inline into thunks, even transparent callees.
634637
if (F.isThunk())
@@ -638,7 +641,7 @@ class MandatoryInlining : public SILModuleTransform {
638641
if (F.wasDeserializedCanonical())
639642
continue;
640643

641-
runOnFunctionRecursively(&F,
644+
runOnFunctionRecursively(FuncBuilder, &F,
642645
FullApplySite(), FullyInlinedSet, SetFactory,
643646
SetFactory.getEmptySet(), CHA);
644647
}

lib/SILOptimizer/Transforms/PerformanceInliner.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/SILOptimizer/Utils/Devirtualize.h"
2020
#include "swift/SILOptimizer/Utils/Generics.h"
2121
#include "swift/SILOptimizer/Utils/PerformanceInlinerUtils.h"
22+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
2223
#include "swift/Strings.h"
2324
#include "llvm/ADT/Statistic.h"
2425
#include "llvm/Support/CommandLine.h"
@@ -45,6 +46,8 @@ namespace {
4546
using Weight = ShortestPathAnalysis::Weight;
4647

4748
class SILPerformanceInliner {
49+
SILOptFunctionBuilder &FuncBuilder;
50+
4851
/// Specifies which functions not to inline, based on @_semantics and
4952
/// global_init attributes.
5053
InlineSelection WhatToInline;
@@ -164,11 +167,12 @@ class SILPerformanceInliner {
164167
SmallVectorImpl<FullApplySite> &Applies);
165168

166169
public:
167-
SILPerformanceInliner(InlineSelection WhatToInline, DominanceAnalysis *DA,
170+
SILPerformanceInliner(SILOptFunctionBuilder &FuncBuilder,
171+
InlineSelection WhatToInline, DominanceAnalysis *DA,
168172
SILLoopAnalysis *LA, SideEffectAnalysis *SEA,
169173
OptimizationMode OptMode, OptRemark::Emitter &ORE)
170-
: WhatToInline(WhatToInline), DA(DA), LA(LA), SEA(SEA), CBI(DA), ORE(ORE),
171-
OptMode(OptMode) {}
174+
: FuncBuilder(FuncBuilder), WhatToInline(WhatToInline), DA(DA), LA(LA),
175+
SEA(SEA), CBI(DA), ORE(ORE), OptMode(OptMode) {}
172176

173177
bool inlineCallsIntoFunction(SILFunction *F);
174178
};
@@ -826,7 +830,7 @@ bool SILPerformanceInliner::inlineCallsIntoFunction(SILFunction *Caller) {
826830
// the substitution list.
827831
OpenedArchetypesTracker.registerUsedOpenedArchetypes(AI.getInstruction());
828832

829-
SILInliner Inliner(*Caller, *Callee,
833+
SILInliner Inliner(FuncBuilder, *Caller, *Callee,
830834
SILInliner::InlineKind::PerformanceInline,
831835
AI.getSubstitutionMap(),
832836
OpenedArchetypesTracker);
@@ -896,7 +900,9 @@ class SILPerformanceInlinerPass : public SILFunctionTransform {
896900

897901
auto OptMode = getFunction()->getEffectiveOptimizationMode();
898902

899-
SILPerformanceInliner Inliner(WhatToInline, DA, LA, SEA, OptMode, ORE);
903+
SILOptFunctionBuilder FuncBuilder(*getPassManager());
904+
SILPerformanceInliner Inliner(FuncBuilder, WhatToInline, DA, LA, SEA,
905+
OptMode, ORE);
900906

901907
assert(getFunction()->isDefinition() &&
902908
"Expected only functions with bodies!");

lib/SILOptimizer/Utils/GenericCloner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ const SILDebugScope *GenericCloner::remapScope(const SILDebugScope *DS) {
182182
ParentFunction = getCloned();
183183
else if (ParentFunction)
184184
ParentFunction = remapParentFunction(
185-
M, ParentFunction, SubsMap,
185+
FuncBuilder, M, ParentFunction, SubsMap,
186186
Original.getLoweredFunctionType()->getGenericSignature());
187187

188188
auto *ParentScope = DS->Parent.dyn_cast<const SILDebugScope *>();

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#define DEBUG_TYPE "sil-inliner"
14+
1415
#include "swift/SILOptimizer/Utils/SILInliner.h"
16+
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
1517
#include "swift/SIL/SILDebugScope.h"
1618
#include "llvm/ADT/STLExtras.h"
1719
#include "llvm/Support/Debug.h"
@@ -445,7 +447,7 @@ SILInliner::getOrCreateInlineScope(const SILDebugScope *CalleeScope) {
445447
auto *ParentFunction = CalleeScope->Parent.dyn_cast<SILFunction *>();
446448
if (ParentFunction)
447449
ParentFunction = remapParentFunction(
448-
M, ParentFunction, SubsMap,
450+
FuncBuilder, M, ParentFunction, SubsMap,
449451
CalleeFunction->getLoweredFunctionType()->getGenericSignature(),
450452
ForInlining);
451453

0 commit comments

Comments
 (0)