Skip to content

Commit 302632c

Browse files
committed
Make SILFunction::create() private and change all direct uses of SILFunction::create() to instead use SILMod.getOrCreateFunction().
This centralizes the entrypoints for creating SILFunctions. Creating a SILFunction is intimately tied to a specific SILModule, so it makes sense to either centralize the creation on SILModule or SILFunction. Since a SILFunction is in a SILModule, it seems more natural to put it on SILModule. I purposely created a new override on SILMod that exactly matches the signature of SILFunction::create so that beyond the extra indirection through SILMod, this change should be NFC. We can refactor individual cases in later iterations of refactoring.
1 parent 48f37da commit 302632c

File tree

14 files changed

+61
-25
lines changed

14 files changed

+61
-25
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ class SILFunction
165165
const SILDebugScope *debugScope,
166166
DeclContext *DC);
167167

168-
public:
169168
static SILFunction *create(SILModule &M, SILLinkage linkage, StringRef name,
170169
CanSILFunctionType loweredType,
171170
GenericParamList *contextGenericParams,
@@ -180,6 +179,8 @@ class SILFunction
180179
SILFunction *InsertBefore = nullptr,
181180
const SILDebugScope *DebugScope = nullptr,
182181
DeclContext *DC = nullptr);
182+
183+
public:
183184
~SILFunction();
184185

185186
SILModule &getModule() const { return Module; }

include/swift/SIL/SILModule.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,27 @@ class SILModule {
443443
SILDeclRef constant,
444444
ForDefinition_t forDefinition);
445445

446+
/// \brief Return the declaration of a function, or create it if it does not
447+
/// exist.
448+
///
449+
/// This signature is a direct copy of the signature of SILFunction::create()
450+
/// in order to simplify refactoring all SILFunction creation use-sites to use
451+
/// SILModule. Eventually the uses should probably be refactored.
452+
SILFunction *getOrCreateFunction(SILLinkage linkage, StringRef name,
453+
CanSILFunctionType loweredType,
454+
GenericParamList *contextGenericParams,
455+
Optional<SILLocation> loc,
456+
IsBare_t isBareSILFunction,
457+
IsTransparent_t isTrans,
458+
IsFragile_t isFragile,
459+
IsThunk_t isThunk = IsNotThunk,
460+
SILFunction::ClassVisibility_t classVisibility = SILFunction::NotRelevant,
461+
Inline_t inlineStrategy = InlineDefault,
462+
EffectsKind EK = EffectsKind::Unspecified,
463+
SILFunction *InsertBefore = nullptr,
464+
const SILDebugScope *DebugScope = nullptr,
465+
DeclContext *DC = nullptr);
466+
446467
/// Look up the SILWitnessTable representing the lowering of a protocol
447468
/// conformance, and collect the substitutions to apply to the referenced
448469
/// witnesses, if any.

lib/Parse/ParseSIL.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ SILFunction *SILParser::getGlobalNameForDefinition(Identifier Name,
372372
Fn->getLoweredFunctionType(), Ty);
373373
P.diagnose(It->second.second, diag::sil_prior_reference);
374374
auto loc = SILFileLocation(Loc);
375-
Fn = SILFunction::create(SILMod, SILLinkage::Private, "", Ty,
376-
nullptr, loc, IsNotBare, IsNotTransparent, IsNotFragile);
375+
Fn = SILMod.getOrCreateFunction(SILLinkage::Private, "", Ty,
376+
nullptr, loc, IsNotBare, IsNotTransparent, IsNotFragile);
377377
Fn->setDebugScope(new (SILMod) SILDebugScope(loc, *Fn));
378378
}
379379

@@ -392,15 +392,15 @@ SILFunction *SILParser::getGlobalNameForDefinition(Identifier Name,
392392
// defined already.
393393
if (SILMod.lookUpFunction(Name.str()) != nullptr) {
394394
P.diagnose(Loc, diag::sil_value_redefinition, Name.str());
395-
auto fn = SILFunction::create(SILMod, SILLinkage::Private, "", Ty,
395+
auto *fn = SILMod.getOrCreateFunction(SILLinkage::Private, "", Ty,
396396
nullptr, loc, IsNotBare, IsNotTransparent,
397397
IsNotFragile);
398398
fn->setDebugScope(new (SILMod) SILDebugScope(loc, *fn));
399399
return fn;
400400
}
401401

402402
// Otherwise, this definition is the first use of this name.
403-
auto fn = SILFunction::create(SILMod, SILLinkage::Private, Name.str(),
403+
auto *fn = SILMod.getOrCreateFunction(SILLinkage::Private, Name.str(),
404404
Ty, nullptr, loc, IsNotBare, IsNotTransparent,
405405
IsNotFragile);
406406
fn->setDebugScope(new (SILMod) SILDebugScope(loc, *fn));
@@ -422,7 +422,7 @@ SILFunction *SILParser::getGlobalNameForReference(Identifier Name,
422422
if (FnRef->getLoweredFunctionType() != Ty) {
423423
P.diagnose(Loc, diag::sil_value_use_type_mismatch,
424424
Name.str(), FnRef->getLoweredFunctionType(), Ty);
425-
FnRef = SILFunction::create(SILMod, SILLinkage::Private, "", Ty, nullptr,
425+
FnRef = SILMod.getOrCreateFunction(SILLinkage::Private, "", Ty, nullptr,
426426
loc, IsNotBare, IsNotTransparent,
427427
IsNotFragile);
428428
FnRef->setDebugScope(new (SILMod) SILDebugScope(loc, *FnRef));
@@ -432,7 +432,7 @@ SILFunction *SILParser::getGlobalNameForReference(Identifier Name,
432432

433433
// If we didn't find a function, create a new one - it must be a forward
434434
// reference.
435-
auto Fn = SILFunction::create(SILMod, SILLinkage::Private,
435+
auto *Fn = SILMod.getOrCreateFunction(SILLinkage::Private,
436436
Name.str(), Ty, nullptr, loc, IsNotBare,
437437
IsNotTransparent, IsNotFragile);
438438
Fn->setDebugScope(new (SILMod) SILDebugScope(loc, *Fn));

lib/SIL/SILModule.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,24 @@ SILFunction *SILModule::getOrCreateSharedFunction(SILLocation loc,
398398
isThunk, SILFunction::NotRelevant);
399399
}
400400

401+
SILFunction *SILModule::getOrCreateFunction(SILLinkage linkage, StringRef name,
402+
CanSILFunctionType loweredType,
403+
GenericParamList *contextGenericParams,
404+
Optional<SILLocation> loc,
405+
IsBare_t isBareSILFunction,
406+
IsTransparent_t isTrans,
407+
IsFragile_t isFragile,
408+
IsThunk_t isThunk,
409+
SILFunction::ClassVisibility_t classVisibility,
410+
Inline_t inlineStrategy,
411+
EffectsKind EK,
412+
SILFunction *InsertBefore,
413+
const SILDebugScope *DebugScope,
414+
DeclContext *DC) {
415+
return SILFunction::create(*this, linkage, name, loweredType,
416+
contextGenericParams, loc, isBareSILFunction, isTrans, isFragile, isThunk, classVisibility, inlineStrategy, EK, InsertBefore, DebugScope, DC);
417+
}
418+
401419
ArrayRef<SILType> ValueBase::getTypes() const {
402420
// No results.
403421
if (TypeOrTypeList.isNull())

lib/SILGen/SILGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ SILFunction *SILGenModule::emitTopLevelFunction(SILLocation Loc) {
210210
None,
211211
C);
212212

213-
return SILFunction::create(M, SILLinkage::Public,
213+
return M.getOrCreateFunction(SILLinkage::Public,
214214
SWIFT_ENTRY_POINT_FUNCTION, topLevelType, nullptr,
215215
Loc, IsBare, IsNotTransparent, IsNotFragile,
216216
IsNotThunk, SILFunction::NotRelevant);
@@ -723,7 +723,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
723723
auto initSILType = getLoweredType(initType).castTo<SILFunctionType>();
724724

725725
auto *f =
726-
SILFunction::create(M, SILLinkage::Private, funcName,
726+
M.getOrCreateFunction(SILLinkage::Private, funcName,
727727
initSILType, nullptr, SILLocation(binding),
728728
IsNotBare, IsNotTransparent,
729729
makeModuleFragile ? IsFragile : IsNotFragile);

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1765,7 +1765,7 @@ SILGenModule::emitProtocolWitness(ProtocolConformance *conformance,
17651765
if (witness.isAlwaysInline())
17661766
InlineStrategy = AlwaysInline;
17671767

1768-
auto *f = SILFunction::create(M, linkage, nameBuffer,
1768+
auto *f = M.getOrCreateFunction(linkage, nameBuffer,
17691769
witnessSILType.castTo<SILFunctionType>(),
17701770
witnessContextParams,
17711771
SILLocation(witness.getDecl()),

lib/SILGen/SILGenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ SILGenModule::emitVTableMethod(SILDeclRef derived, SILDeclRef base) {
8888

8989
auto *derivedDecl = cast<AbstractFunctionDecl>(derived.getDecl());
9090
SILLocation loc(derivedDecl);
91-
auto thunk = SILFunction::create(M, SILLinkage::Private, name,
91+
auto thunk = M.getOrCreateFunction(SILLinkage::Private, name,
9292
overrideInfo.SILFnType,
9393
derivedDecl->getGenericParams(),
9494
loc, IsBare, IsNotTransparent, IsNotFragile);

lib/SILPasses/IPO/CapturePromotion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ ClosureCloner::initCloned(SILFunction *Orig, StringRef ClonedName,
452452
&& "SILFunction missing DebugScope");
453453
assert(!Orig->isGlobalInit() && "Global initializer cannot be cloned");
454454

455-
auto Fn =
456-
SILFunction::create(M, Orig->getLinkage(), ClonedName, SubstTy,
455+
auto *Fn =
456+
M.getOrCreateFunction(Orig->getLinkage(), ClonedName, SubstTy,
457457
Orig->getContextGenericParams(), Orig->getLocation(),
458458
Orig->isBare(), IsNotTransparent, Orig->isFragile(),
459459
Orig->isThunk(),

lib/SILPasses/IPO/CapturePropagation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ SILFunction *CapturePropagation::specializeConstClosure(PartialApplyInst *PAI,
233233
CanSILFunctionType NewFTy =
234234
Lowering::adjustFunctionType(PAI->getType().castTo<SILFunctionType>(),
235235
SILFunctionType::Representation::Thin);
236-
SILFunction *NewF = SILFunction::create(
237-
*getModule(), SILLinkage::Shared, Name, NewFTy,
236+
SILFunction *NewF = getModule()->getOrCreateFunction(SILLinkage::Shared, Name, NewFTy,
238237
/*contextGenericParams*/ nullptr, OrigF->getLocation(), OrigF->isBare(),
239238
OrigF->isTransparent(), OrigF->isFragile(), OrigF->isThunk(),
240239
OrigF->getClassVisibility(),

lib/SILPasses/IPO/ClosureSpecializer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,7 @@ ClosureSpecCloner::initCloned(const CallSiteDescriptor &CallSiteDesc,
546546

547547
// We make this function bare so we don't have to worry about decls in the
548548
// SILArgument.
549-
auto Fn = SILFunction::create(
550-
M, ClosureUser->getLinkage(), ClonedName, ClonedTy,
549+
auto *Fn = M.getOrCreateFunction(ClosureUser->getLinkage(), ClonedName, ClonedTy,
551550
ClosureUser->getContextGenericParams(), ClosureUser->getLocation(),
552551
IsBare, ClosureUser->isTransparent(), ClosureUser->isFragile(),
553552
ClosureUser->isThunk(), ClosureUser->getClassVisibility(),

0 commit comments

Comments
 (0)