Skip to content

Commit 6511b75

Browse files
committed
[silgen] Replace all uses of SILFunctionBuilder with SILGenFunctionBuilder.
This initial implementation just delegates from SILGenFunctionBuilder to SILFunctionBuilder. I was going to start transforming verbose uses of SILFunctionBuilder into higher level APIs on SILGenFunctionBuilder, but I have run out of time. This is a good incremental step forward that will let me hide the constructor of SILFunctionBuilder after I update the optimizer and thus ensure that SILFunctionBuilder is only used through appropriate composition APIs. rdar://42301529
1 parent b3c9dbe commit 6511b75

File tree

8 files changed

+68
-22
lines changed

8 files changed

+68
-22
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "ManagedValue.h"
1515
#include "RValue.h"
1616
#include "SILGenFunction.h"
17+
#include "SILGenFunctionBuilder.h"
1718
#include "Scope.h"
1819
#include "swift/AST/DiagnosticsSIL.h"
1920
#include "swift/AST/GenericEnvironment.h"
@@ -29,7 +30,6 @@
2930
#include "swift/SIL/PrettyStackTrace.h"
3031
#include "swift/SIL/SILArgument.h"
3132
#include "swift/SIL/SILDebugScope.h"
32-
#include "swift/SIL/SILFunctionBuilder.h"
3333
#include "swift/SIL/SILProfiler.h"
3434
#include "swift/Serialization/SerializedModuleLoader.h"
3535
#include "swift/Serialization/SerializedSILLoader.h"
@@ -430,7 +430,7 @@ SILFunction *SILGenModule::emitTopLevelFunction(SILLocation Loc) {
430430
None,
431431
C);
432432

433-
SILFunctionBuilder builder(M);
433+
SILGenFunctionBuilder builder(*this);
434434
return builder.createFunction(
435435
SILLinkage::Public, SWIFT_ENTRY_POINT_FUNCTION, topLevelType, nullptr,
436436
Loc, IsBare, IsNotTransparent, IsNotSerialized, ProfileCounter(),
@@ -523,7 +523,7 @@ SILFunction *SILGenModule::getFunction(SILDeclRef constant,
523523
return emitted;
524524

525525
// Note: Do not provide any SILLocation. You can set it afterwards.
526-
SILFunctionBuilder builder(M);
526+
SILGenFunctionBuilder builder(*this);
527527
auto *F = builder.getOrCreateFunction(constant.hasDecl() ? constant.getDecl()
528528
: (Decl *)nullptr,
529529
constant, forDefinition);
@@ -1033,7 +1033,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
10331033
TupleType::getEmpty(C), type->getExtInfo());
10341034
auto initSILType = getLoweredType(initType).castTo<SILFunctionType>();
10351035

1036-
SILFunctionBuilder builder(M);
1036+
SILGenFunctionBuilder builder(*this);
10371037
auto *f = builder.createFunction(SILLinkage::Private, funcName, initSILType,
10381038
nullptr, SILLocation(binding), IsNotBare,
10391039
IsNotTransparent, IsNotSerialized);

lib/SILGen/SILGenConstructor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
#include "LValue.h"
1616
#include "RValue.h"
1717
#include "SILGenFunction.h"
18+
#include "SILGenFunctionBuilder.h"
1819
#include "Scope.h"
1920
#include "swift/AST/ASTMangler.h"
2021
#include "swift/AST/GenericEnvironment.h"
2122
#include "swift/AST/ParameterList.h"
2223
#include "swift/Basic/Defer.h"
2324
#include "swift/SIL/SILArgument.h"
24-
#include "swift/SIL/SILFunctionBuilder.h"
2525
#include "swift/SIL/SILUndef.h"
2626
#include "swift/SIL/TypeLowering.h"
2727

@@ -905,7 +905,7 @@ static SILValue getBehaviorInitStorageFn(SILGenFunction &SGF,
905905
SGF.getASTContext());
906906

907907
// TODO: Generate the body of the thunk.
908-
SILFunctionBuilder builder(SGF.SGM.M);
908+
SILGenFunctionBuilder builder(SGF);
909909
thunkFn = builder.getOrCreateFunction(
910910
SILLocation(behaviorVar), behaviorInitName, SILLinkage::PrivateExternal,
911911
initConstantTy, IsBare, IsTransparent, IsSerialized);

lib/SILGen/SILGenExpr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "ResultPlan.h"
2323
#include "SILGen.h"
2424
#include "SILGenDynamicCast.h"
25+
#include "SILGenFunctionBuilder.h"
2526
#include "Scope.h"
2627
#include "SwitchEnumBuilder.h"
2728
#include "Varargs.h"
@@ -40,7 +41,6 @@
4041
#include "swift/Basic/type_traits.h"
4142
#include "swift/SIL/DynamicCasts.h"
4243
#include "swift/SIL/SILArgument.h"
43-
#include "swift/SIL/SILFunctionBuilder.h"
4444
#include "swift/SIL/SILUndef.h"
4545
#include "swift/SIL/TypeLowering.h"
4646
#include "llvm/ADT/STLExtras.h"
@@ -2999,7 +2999,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
29992999
auto name = Mangle::ASTMangler()
30003000
.mangleKeyPathGetterThunkHelper(property, genericSig, baseType,
30013001
interfaceSubs);
3002-
SILFunctionBuilder builder(SGM.M);
3002+
SILGenFunctionBuilder builder(SGM);
30033003
auto thunk = builder.getOrCreateSharedFunction(
30043004
loc, name, signature, IsBare, IsNotTransparent, IsNotSerialized,
30053005
ProfileCounter(), IsThunk);
@@ -3122,7 +3122,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
31223122
baseType,
31233123
interfaceSubs);
31243124

3125-
SILFunctionBuilder builder(SGM.M);
3125+
SILGenFunctionBuilder builder(SGM);
31263126
auto thunk = builder.getOrCreateSharedFunction(
31273127
loc, name, signature, IsBare, IsNotTransparent, IsNotSerialized,
31283128
ProfileCounter(), IsThunk);
@@ -3278,7 +3278,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
32783278

32793279
auto name = Mangle::ASTMangler().mangleKeyPathEqualsHelper(indexTypes,
32803280
genericSig);
3281-
SILFunctionBuilder builder(SGM.M);
3281+
SILGenFunctionBuilder builder(SGM);
32823282
equals = builder.getOrCreateSharedFunction(
32833283
loc, name, signature, IsBare, IsNotTransparent, IsNotSerialized,
32843284
ProfileCounter(), IsThunk);
@@ -3443,7 +3443,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
34433443

34443444
auto name = Mangle::ASTMangler().mangleKeyPathHashHelper(indexTypes,
34453445
genericSig);
3446-
SILFunctionBuilder builder(SGM.M);
3446+
SILGenFunctionBuilder builder(SGM);
34473447
hash = builder.getOrCreateSharedFunction(loc, name, signature, IsBare,
34483448
IsNotTransparent, IsNotSerialized,
34493449
ProfileCounter(), IsThunk);

lib/SILGen/SILGenFunction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
#include "SILGenFunction.h"
1919
#include "RValue.h"
20+
#include "SILGenFunctionBuilder.h"
2021
#include "Scope.h"
2122
#include "swift/AST/Initializer.h"
2223
#include "swift/SIL/SILArgument.h"
23-
#include "swift/SIL/SILFunctionBuilder.h"
2424
#include "swift/SIL/SILProfiler.h"
2525
#include "swift/SIL/SILUndef.h"
2626

@@ -462,7 +462,7 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
462462
ValueDecl *UIApplicationMainDecl = results.front();
463463

464464
auto mainRef = SILDeclRef(UIApplicationMainDecl).asForeign();
465-
SILFunctionBuilder builder(SGM.M);
465+
SILGenFunctionBuilder builder(SGM);
466466
auto UIApplicationMainFn =
467467
builder.getOrCreateFunction(mainClass, mainRef, NotForDefinition);
468468
auto fnTy = UIApplicationMainFn->getLoweredFunctionType();
@@ -581,7 +581,7 @@ void SILGenFunction::emitArtificialTopLevel(ClassDecl *mainClass) {
581581
/*error result*/ None,
582582
getASTContext());
583583

584-
SILFunctionBuilder builder(SGM.M);
584+
SILGenFunctionBuilder builder(SGM);
585585
auto NSApplicationMainFn = builder.getOrCreateFunction(
586586
mainClass, "NSApplicationMain", SILLinkage::PublicExternal,
587587
NSApplicationMainType, IsBare, IsTransparent, IsNotSerialized);

lib/SILGen/SILGenFunctionBuilder.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- SILGenFunctionBuilder.h ------------------------------------------===//
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_SILGEN_SILGENFUNCTIONBUILDER_H
14+
#define SWIFT_SILGEN_SILGENFUNCTIONBUILDER_H
15+
16+
#include "swift/SIL/SILFunctionBuilder.h"
17+
18+
namespace swift {
19+
namespace Lowering {
20+
21+
class LLVM_LIBRARY_VISIBILITY SILGenFunctionBuilder {
22+
SILFunctionBuilder builder;
23+
24+
public:
25+
SILGenFunctionBuilder(SILGenModule &SGM) : builder(SGM.M) {}
26+
SILGenFunctionBuilder(SILGenFunction &SGF) : builder(SGF.SGM.M) {}
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 Lowering
44+
} // namespace swift
45+
46+
#endif

lib/SILGen/SILGenMaterializeForSet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
#include "LValue.h"
162162
#include "RValue.h"
163163
#include "SILGen.h"
164+
#include "SILGenFunctionBuilder.h"
164165
#include "Scope.h"
165166
#include "swift/AST/ASTMangler.h"
166167
#include "swift/AST/Decl.h"
@@ -170,7 +171,6 @@
170171
#include "swift/AST/Types.h"
171172
#include "swift/SIL/PrettyStackTrace.h"
172173
#include "swift/SIL/SILArgument.h"
173-
#include "swift/SIL/SILFunctionBuilder.h"
174174
#include "swift/SIL/SILUndef.h"
175175
#include "swift/SIL/TypeLowering.h"
176176
#include "llvm/Support/raw_ostream.h"
@@ -717,7 +717,7 @@ SILFunction *MaterializeForSetEmitter::createCallback(SILFunction &F,
717717
auto callbackLinkage =
718718
F.isSerialized() ? SILLinkage::Shared : SILLinkage::Private;
719719

720-
SILFunctionBuilder builder(SGM.M);
720+
SILGenFunctionBuilder builder(SGM);
721721
auto callback = builder.createFunction(
722722
callbackLinkage, CallbackName, callbackType, genericEnv,
723723
SILLocation(Witness), IsBare, F.isTransparent(), F.isSerialized(),

lib/SILGen/SILGenThunk.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
#include "ManagedValue.h"
2424
#include "SILGenFunction.h"
25+
#include "SILGenFunctionBuilder.h"
2526
#include "Scope.h"
2627
#include "swift/AST/ASTMangler.h"
2728
#include "swift/AST/DiagnosticsSIL.h"
2829
#include "swift/AST/GenericEnvironment.h"
2930
#include "swift/SIL/PrettyStackTrace.h"
3031
#include "swift/SIL/SILArgument.h"
31-
#include "swift/SIL/SILFunctionBuilder.h"
3232
#include "swift/SIL/TypeLowering.h"
3333

3434
using namespace swift;
@@ -50,7 +50,7 @@ SILFunction *SILGenModule::getDynamicThunk(SILDeclRef constant,
5050
// Mangle the constant with a TD suffix.
5151
auto name = constant.mangle(SILDeclRef::ManglingKind::DynamicThunk);
5252

53-
SILFunctionBuilder builder(M);
53+
SILGenFunctionBuilder builder(*this);
5454
auto F = builder.getOrCreateFunction(
5555
constant.getDecl(), name, SILLinkage::Shared, constantTy, IsBare,
5656
IsTransparent, IsSerializable, ProfileCounter(), IsThunk);
@@ -291,7 +291,7 @@ getOrCreateReabstractionThunk(CanSILFunctionType thunkType,
291291

292292
auto loc = RegularLocation::getAutoGeneratedLocation();
293293

294-
SILFunctionBuilder builder(M);
294+
SILGenFunctionBuilder builder(*this);
295295
return builder.getOrCreateSharedFunction(
296296
loc, name, thunkDeclType, IsBare, IsTransparent, IsSerializable,
297297
ProfileCounter(), IsReabstractionThunk);

lib/SILGen/SILGenType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "ManagedValue.h"
2121
#include "SILGenFunction.h"
22+
#include "SILGenFunctionBuilder.h"
2223
#include "Scope.h"
2324
#include "swift/AST/ASTMangler.h"
2425
#include "swift/AST/GenericEnvironment.h"
@@ -28,7 +29,6 @@
2829
#include "swift/SIL/FormalLinkage.h"
2930
#include "swift/SIL/PrettyStackTrace.h"
3031
#include "swift/SIL/SILArgument.h"
31-
#include "swift/SIL/SILFunctionBuilder.h"
3232
#include "swift/SIL/SILVTableVisitor.h"
3333
#include "swift/SIL/SILWitnessVisitor.h"
3434
#include "swift/SIL/TypeLowering.h"
@@ -131,7 +131,7 @@ SILGenModule::emitVTableMethod(ClassDecl *theClass,
131131

132132
// Emit the thunk.
133133
SILLocation loc(derivedDecl);
134-
SILFunctionBuilder builder(M);
134+
SILGenFunctionBuilder builder(*this);
135135
auto thunk = builder.createFunction(
136136
SILLinkage::Private, name, overrideInfo.SILFnType,
137137
cast<AbstractFunctionDecl>(derivedDecl)->getGenericEnvironment(), loc,
@@ -665,7 +665,7 @@ SILFunction *SILGenModule::emitProtocolWitness(
665665
if (witnessRef.isAlwaysInline())
666666
InlineStrategy = AlwaysInline;
667667

668-
SILFunctionBuilder builder(M);
668+
SILGenFunctionBuilder builder(*this);
669669
auto *f = builder.createFunction(
670670
linkage, nameBuffer, witnessSILFnType, genericEnv,
671671
SILLocation(witnessRef.getDecl()), IsNotBare, IsTransparent, isSerialized,

0 commit comments

Comments
 (0)