Skip to content

Commit 4212c61

Browse files
committed
Optimizer: add Context.createSpecializedFunctionDeclaration
Originally this was a "private" utility for the ClosureSpecialization pass. Now, make it a general utility which can be used for all kind of function specializations.
1 parent 57e08af commit 4212c61

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ClosureSpecialization.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ private func getOrCreateSpecializedFunction(basedOn callSite: CallSite, _ contex
234234
let specializedParameters = applySiteCallee.convention.getSpecializedParameters(basedOn: callSite)
235235

236236
let specializedFunction =
237-
context.createFunctionForClosureSpecialization(from: applySiteCallee, withName: specializedFunctionName,
238-
withParams: specializedParameters,
239-
withSerialization: applySiteCallee.isSerialized)
237+
context.createSpecializedFunctionDeclaration(from: applySiteCallee, withName: specializedFunctionName,
238+
withParams: specializedParameters,
239+
makeThin: true, makeBare: true)
240240

241241
context.buildSpecializedFunction(specializedFunction: specializedFunction,
242242
buildFn: { (emptySpecializedFunction, functionPassContext) in

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,17 @@ struct FunctionPassContext : MutatingContext {
419419
return gv.globalVar
420420
}
421421

422-
func createFunctionForClosureSpecialization(from applySiteCallee: Function, withName specializedFunctionName: String,
423-
withParams specializedParameters: [ParameterInfo],
424-
withSerialization isSerialized: Bool) -> Function
422+
func createSpecializedFunctionDeclaration(from original: Function, withName specializedFunctionName: String,
423+
withParams specializedParameters: [ParameterInfo],
424+
makeThin: Bool = false,
425+
makeBare: Bool = false) -> Function
425426
{
426427
return specializedFunctionName._withBridgedStringRef { nameRef in
427428
let bridgedParamInfos = specializedParameters.map { $0._bridged }
428429

429430
return bridgedParamInfos.withUnsafeBufferPointer { paramBuf in
430-
_bridged.ClosureSpecializer_createEmptyFunctionWithSpecializedSignature(nameRef, paramBuf.baseAddress,
431-
paramBuf.count,
432-
applySiteCallee.bridged,
433-
isSerialized).function
431+
_bridged.createSpecializedFunctionDeclaration(nameRef, paramBuf.baseAddress, paramBuf.count,
432+
original.bridged, makeThin, makeBare).function
434433
}
435434
}
436435
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,12 @@ struct BridgedPassContext {
392392
BRIDGED_INLINE bool enableAddressDependencies() const;
393393

394394
// Closure specializer
395-
SWIFT_IMPORT_UNSAFE BridgedFunction ClosureSpecializer_createEmptyFunctionWithSpecializedSignature(BridgedStringRef specializedName,
395+
SWIFT_IMPORT_UNSAFE BridgedFunction createSpecializedFunctionDeclaration(BridgedStringRef specializedName,
396396
const BridgedParameterInfo * _Nullable specializedBridgedParams,
397397
SwiftInt paramCount,
398-
BridgedFunction bridgedApplySiteCallee,
399-
bool isSerialized) const;
398+
BridgedFunction bridgedOriginal,
399+
bool makeThin,
400+
bool makeBare) const;
400401

401402
bool completeLifetime(BridgedValue value) const;
402403
};

lib/SILOptimizer/Utils/OptimizerBridging.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,14 @@ void BridgedPassContext::moveFunctionBody(BridgedFunction sourceFunc, BridgedFun
452452
}
453453

454454
BridgedFunction BridgedPassContext::
455-
ClosureSpecializer_createEmptyFunctionWithSpecializedSignature(BridgedStringRef specializedName,
456-
const BridgedParameterInfo * _Nullable specializedBridgedParams,
457-
SwiftInt paramCount,
458-
BridgedFunction bridgedApplySiteCallee,
459-
bool isSerialized) const {
460-
auto *applySiteCallee = bridgedApplySiteCallee.getFunction();
461-
auto applySiteCalleeType = applySiteCallee->getLoweredFunctionType();
455+
createSpecializedFunctionDeclaration(BridgedStringRef specializedName,
456+
const BridgedParameterInfo * _Nullable specializedBridgedParams,
457+
SwiftInt paramCount,
458+
BridgedFunction bridgedOriginal,
459+
bool makeThin,
460+
bool makeBare) const {
461+
auto *original = bridgedOriginal.getFunction();
462+
auto originalType = original->getLoweredFunctionType();
462463

463464
llvm::SmallVector<SILParameterInfo> specializedParams;
464465
for (unsigned idx = 0; idx < paramCount; ++idx) {
@@ -468,18 +469,19 @@ ClosureSpecializer_createEmptyFunctionWithSpecializedSignature(BridgedStringRef
468469
// The specialized function is always a thin function. This is important
469470
// because we may add additional parameters after the Self parameter of
470471
// witness methods. In this case the new function is not a method anymore.
471-
auto extInfo = applySiteCalleeType->getExtInfo();
472-
extInfo = extInfo.withRepresentation(SILFunctionTypeRepresentation::Thin);
472+
auto extInfo = originalType->getExtInfo();
473+
if (makeThin)
474+
extInfo = extInfo.withRepresentation(SILFunctionTypeRepresentation::Thin);
473475

474476
auto ClonedTy = SILFunctionType::get(
475-
applySiteCalleeType->getInvocationGenericSignature(), extInfo,
476-
applySiteCalleeType->getCoroutineKind(),
477-
applySiteCalleeType->getCalleeConvention(), specializedParams,
478-
applySiteCalleeType->getYields(), applySiteCalleeType->getResults(),
479-
applySiteCalleeType->getOptionalErrorResult(),
480-
applySiteCalleeType->getPatternSubstitutions(),
481-
applySiteCalleeType->getInvocationSubstitutions(),
482-
applySiteCallee->getModule().getASTContext());
477+
originalType->getInvocationGenericSignature(), extInfo,
478+
originalType->getCoroutineKind(),
479+
originalType->getCalleeConvention(), specializedParams,
480+
originalType->getYields(), originalType->getResults(),
481+
originalType->getOptionalErrorResult(),
482+
originalType->getPatternSubstitutions(),
483+
originalType->getInvocationSubstitutions(),
484+
original->getModule().getASTContext());
483485

484486
SILOptFunctionBuilder functionBuilder(*invocation->getTransform());
485487

@@ -493,23 +495,23 @@ ClosureSpecializer_createEmptyFunctionWithSpecializedSignature(BridgedStringRef
493495
// It's also important to disconnect this specialized function from any
494496
// classes (the classSubclassScope), because that may incorrectly
495497
// influence the linkage.
496-
getSpecializedLinkage(applySiteCallee, applySiteCallee->getLinkage()), specializedName.unbridged(),
497-
ClonedTy, applySiteCallee->getGenericEnvironment(),
498-
applySiteCallee->getLocation(), IsBare, applySiteCallee->isTransparent(),
499-
isSerialized ? IsSerialized : IsNotSerialized, IsNotDynamic, IsNotDistributed,
500-
IsNotRuntimeAccessible, applySiteCallee->getEntryCount(),
501-
applySiteCallee->isThunk(),
498+
getSpecializedLinkage(original, original->getLinkage()), specializedName.unbridged(),
499+
ClonedTy, original->getGenericEnvironment(),
500+
original->getLocation(), makeBare ? IsBare : original->isBare(), original->isTransparent(),
501+
original->getSerializedKind(), IsNotDynamic, IsNotDistributed,
502+
IsNotRuntimeAccessible, original->getEntryCount(),
503+
original->isThunk(),
502504
/*classSubclassScope=*/SubclassScope::NotApplicable,
503-
applySiteCallee->getInlineStrategy(), applySiteCallee->getEffectsKind(),
504-
applySiteCallee, applySiteCallee->getDebugScope());
505+
original->getInlineStrategy(), original->getEffectsKind(),
506+
original, original->getDebugScope());
505507

506-
if (!applySiteCallee->hasOwnership()) {
508+
if (!original->hasOwnership()) {
507509
specializedApplySiteCallee->setOwnershipEliminated();
508510
}
509511

510-
for (auto &Attr : applySiteCallee->getSemanticsAttrs())
512+
for (auto &Attr : original->getSemanticsAttrs())
511513
specializedApplySiteCallee->addSemanticsAttr(Attr);
512-
514+
513515
return {specializedApplySiteCallee};
514516
}
515517

0 commit comments

Comments
 (0)