Skip to content

Commit e3140e0

Browse files
committed
Add new generalized cloner.
1 parent bc86fe5 commit e3140e0

File tree

15 files changed

+323
-258
lines changed

15 files changed

+323
-258
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocBoxToStack.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,9 @@ private struct FunctionSpecializations {
311311
// This can happen if a previous run of the pass already created this specialization.
312312
return
313313
}
314-
let cloner = SpecializationCloner(emptySpecializedFunction: specializedFunc, context)
315-
cloner.cloneFunctionBody(from: original)
316-
317314
context.buildSpecializedFunction(specializedFunction: specializedFunc) { (specializedFunc, specContext) in
315+
cloneFunction(from: original, toEmpty: specializedFunc, specContext)
316+
318317
replaceBoxWithStackArguments(in: specializedFunc, original: original, specContext)
319318
}
320319
context.notifyNewFunction(function: specializedFunc, derivedFrom: original)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ClosureSpecialization.swift

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,10 @@ private func getOrCreateSpecializedFunction(
247247
context.buildSpecializedFunction(
248248
specializedFunction: specializedFunction,
249249
buildFn: { (emptySpecializedFunction, functionPassContext) in
250-
let closureSpecCloner = SpecializationCloner(
251-
emptySpecializedFunction: emptySpecializedFunction, functionPassContext)
250+
var closureSpecCloner = Cloner(
251+
cloneToEmptyFunction: emptySpecializedFunction, functionPassContext)
252252
closureSpecCloner.cloneAndSpecializeFunctionBody(using: pullbackClosureInfo)
253+
closureSpecCloner.deinitialize()
253254
})
254255

255256
return (specializedFunction, false)
@@ -731,15 +732,14 @@ private func markConvertedAndReabstractedClosuresAsUsed(
731732
}
732733
}
733734

734-
extension SpecializationCloner {
735+
extension Cloner where Context == FunctionPassContext {
735736
fileprivate func cloneAndSpecializeFunctionBody(using pullbackClosureInfo: PullbackClosureInfo) {
736737
self.cloneEntryBlockArgsWithoutOrigClosures(usingOrigCalleeAt: pullbackClosureInfo)
737738

738739
let (allSpecializedEntryBlockArgs, closureArgIndexToAllClonedReleasableClosures) =
739740
cloneAllClosures(at: pullbackClosureInfo)
740741

741-
self.cloneFunctionBody(
742-
from: pullbackClosureInfo.pullbackFn, entryBlockArguments: allSpecializedEntryBlockArgs)
742+
self.cloneFunctionBody(from: pullbackClosureInfo.pullbackFn, entryBlockArguments: allSpecializedEntryBlockArgs)
743743

744744
self.insertCleanupCodeForClonedReleasableClosures(
745745
from: pullbackClosureInfo,
@@ -750,8 +750,8 @@ extension SpecializationCloner {
750750
usingOrigCalleeAt pullbackClosureInfo: PullbackClosureInfo
751751
) {
752752
let originalEntryBlock = pullbackClosureInfo.pullbackFn.entryBlock
753-
let clonedFunction = self.cloned
754-
let clonedEntryBlock = self.entryBlock
753+
let clonedFunction = self.targetFunction
754+
let clonedEntryBlock = self.getOrCreateEntryBlock()
755755

756756
originalEntryBlock.arguments
757757
.enumerated()
@@ -782,7 +782,8 @@ extension SpecializationCloner {
782782
)
783783
{
784784
func entryBlockArgsWithOrigClosuresSkipped() -> [Value?] {
785-
var clonedNonClosureEntryBlockArgs = self.entryBlock.arguments.makeIterator()
785+
let clonedEntryBlock = self.getOrCreateEntryBlock()
786+
var clonedNonClosureEntryBlockArgs = clonedEntryBlock.arguments.makeIterator()
786787

787788
return pullbackClosureInfo.pullbackFn
788789
.entryBlock
@@ -823,8 +824,8 @@ extension SpecializationCloner {
823824
{
824825
let (origToClonedValueMap, capturedArgRange) = self.addEntryBlockArgs(
825826
forValuesCapturedBy: closureArgDesc)
826-
let clonedFunction = self.cloned
827-
let clonedEntryBlock = self.entryBlock
827+
let clonedFunction = self.targetFunction
828+
let clonedEntryBlock = self.getOrCreateEntryBlock()
828829
let clonedClosureArgs = Array(clonedEntryBlock.arguments[capturedArgRange])
829830

830831
let builder =
@@ -853,8 +854,8 @@ extension SpecializationCloner {
853854
-> (origToClonedValueMap: [HashableValue: Value], capturedArgRange: Range<Int>)
854855
{
855856
var origToClonedValueMap: [HashableValue: Value] = [:]
856-
let clonedFunction = self.cloned
857-
let clonedEntryBlock = self.entryBlock
857+
let clonedFunction = self.targetFunction
858+
let clonedEntryBlock = self.getOrCreateEntryBlock()
858859

859860
let capturedArgRangeStart = clonedEntryBlock.arguments.count
860861

@@ -908,8 +909,8 @@ extension SpecializationCloner {
908909
}
909910
}
910911

911-
if self.context.needFixStackNesting {
912-
self.context.fixStackNesting(in: self.cloned)
912+
if (self.context.needFixStackNesting) {
913+
self.context.fixStackNesting(in: targetFunction)
913914
}
914915
}
915916
}
@@ -1425,12 +1426,10 @@ private struct PullbackClosureInfo {
14251426
}
14261427

14271428
func specializedCalleeName(_ context: FunctionPassContext) -> String {
1428-
let closureArgs = Array(self.closureArgDescriptors.map { $0.closure })
1429-
let closureIndices = Array(self.closureArgDescriptors.map { $0.closureArgIndex })
1430-
1431-
return context.mangle(
1432-
withClosureArguments: closureArgs, closureArgIndices: closureIndices,
1433-
from: pullbackFn)
1429+
let closureArgs = Array(self.closureArgDescriptors.map {
1430+
(argumentIndex: $0.closureArgIndex, argumentValue: $0.closure)
1431+
})
1432+
return context.mangle(withClosureArguments: closureArgs, from: pullbackFn)
14341433
}
14351434
}
14361435

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private indirect enum GlobalInitValue {
271271

272272
/// Creates SIL for this global init value in the initializer of the `global`.
273273
func materialize(into global: GlobalVariable, from function: Function, _ context: FunctionPassContext) {
274-
var cloner = StaticInitCloner(cloneTo: global, context)
274+
var cloner = Cloner(cloneToGlobal: global, context)
275275
defer { cloner.deinitialize() }
276276
let builder = Builder(staticInitializerOf: global, context)
277277

@@ -280,7 +280,7 @@ private indirect enum GlobalInitValue {
280280

281281
private func materializeRecursively(
282282
type: Type,
283-
_ cloner: inout StaticInitCloner<FunctionPassContext>,
283+
_ cloner: inout Cloner<FunctionPassContext>,
284284
_ builder: Builder,
285285
_ function: Function
286286
) -> Value {
@@ -289,7 +289,7 @@ private indirect enum GlobalInitValue {
289289
fatalError("cannot materialize undefined init value")
290290

291291
case .constant(let value):
292-
return cloner.clone(value)
292+
return cloner.cloneRecursivelyToGlobal(value: value)
293293

294294
case .aggregate(let fields):
295295
if type.isStruct {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,13 @@ private func constructObject(of allocRef: AllocRefInstBase,
364364
inInitializerOf global: GlobalVariable,
365365
_ storesToClassFields: [StoreInst], _ storesToTailElements: [StoreInst],
366366
_ context: FunctionPassContext) {
367-
var cloner = StaticInitCloner(cloneTo: global, context)
367+
var cloner = Cloner(cloneToGlobal: global, context)
368368
defer { cloner.deinitialize() }
369369

370370
// Create the initializers for the fields
371371
var objectArgs = [Value]()
372372
for store in storesToClassFields {
373-
objectArgs.append(cloner.clone(store.source as! SingleValueInstruction))
373+
objectArgs.append(cloner.cloneRecursivelyToGlobal(value: store.source as! SingleValueInstruction))
374374
}
375375
let globalBuilder = Builder(staticInitializerOf: global, context)
376376

@@ -382,15 +382,15 @@ private func constructObject(of allocRef: AllocRefInstBase,
382382
for elementIdx in 0..<allocRef.numTailElements! {
383383
let tupleElems = (0..<numTailTupleElems).map { tupleIdx in
384384
let store = storesToTailElements[elementIdx * numTailTupleElems + tupleIdx]
385-
return cloner.clone(store.source as! SingleValueInstruction)
385+
return cloner.cloneRecursivelyToGlobal(value: store.source as! SingleValueInstruction)
386386
}
387387
let tuple = globalBuilder.createTuple(type: allocRef.tailAllocatedTypes[0], elements: tupleElems)
388388
objectArgs.append(tuple)
389389
}
390390
} else {
391391
// The non-tuple element case.
392392
for store in storesToTailElements {
393-
objectArgs.append(cloner.clone(store.source as! SingleValueInstruction))
393+
objectArgs.append(cloner.cloneRecursivelyToGlobal(value: store.source as! SingleValueInstruction))
394394
}
395395
}
396396
}

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyLoad.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ extension LoadInst : OnoneSimplifiable, SILCombineSimplifiable {
104104
if !globalInitVal.canBeCopied(into: parentFunction, context) {
105105
return false
106106
}
107-
var cloner = StaticInitCloner(cloneBefore: self, context)
107+
var cloner = Cloner(cloneBefore: self, context)
108108
defer { cloner.deinitialize() }
109109

110-
let initVal = cloner.clone(globalInitVal)
110+
let initVal = cloner.cloneRecursivelyToGlobal(value: globalInitVal)
111111

112112
uses.replaceAll(with: initVal, context)
113113
// Also erases a builtin "once" on which the global_addr depends on. This is fine
@@ -323,7 +323,7 @@ private extension Value {
323323
return false
324324
}
325325
if let fri = value as? FunctionRefInst {
326-
if function.isAnySerialized,
326+
if function.isAnySerialized,
327327
!fri.referencedFunction.hasValidLinkageForFragileRef(function.serializedKind)
328328
{
329329
return false
@@ -447,4 +447,3 @@ private func getGlobalInitialization(
447447
}
448448
return nil
449449
}
450-

SwiftCompilerSources/Sources/Optimizer/PassManager/ContextCommon.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,28 @@ extension MutatingContext {
152152
bridgedPassContext.notifyDependencyOnBodyOf(otherFunction.bridged)
153153
}
154154
}
155+
extension Cloner where Context == FunctionPassContext {
156+
func getOrCreateEntryBlock() -> BasicBlock {
157+
if let entryBlock = targetFunction.blocks.first {
158+
return entryBlock
159+
}
160+
return targetFunction.appendNewBlock(context)
161+
}
162+
163+
func cloneFunctionBody(from originalFunction: Function, entryBlockArguments: [Value]) {
164+
entryBlockArguments.withBridgedValues { bridgedEntryBlockArgs in
165+
let entryBlock = getOrCreateEntryBlock()
166+
bridged.cloneFunctionBody(originalFunction.bridged, entryBlock.bridged, bridgedEntryBlockArgs)
167+
}
168+
}
169+
170+
func cloneFunctionBody(from originalFunction: Function) {
171+
bridged.cloneFunctionBody(originalFunction.bridged)
172+
}
173+
}
174+
175+
func cloneFunction(from originalFunction: Function, toEmpty targetFunction: Function, _ context: FunctionPassContext) {
176+
var cloner = Cloner(cloneToEmptyFunction: targetFunction, context)
177+
defer { cloner.deinitialize() }
178+
cloner.cloneFunctionBody(from: originalFunction)
179+
}

SwiftCompilerSources/Sources/Optimizer/PassManager/FunctionPassContext.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,11 @@ struct FunctionPassContext : MutatingContext {
9292
return String(taking: bridgedPassContext.mangleOutlinedVariable(function.bridged))
9393
}
9494

95-
func mangle(withClosureArguments closureArgs: [Value], closureArgIndices: [Int], from applySiteCallee: Function) -> String {
96-
closureArgs.withBridgedValues { bridgedClosureArgsRef in
97-
closureArgIndices.withBridgedArrayRef{bridgedClosureArgIndicesRef in
98-
String(taking: bridgedPassContext.mangleWithClosureArgs(
99-
bridgedClosureArgsRef,
100-
bridgedClosureArgIndicesRef,
101-
applySiteCallee.bridged
102-
))
103-
}
95+
func mangle(withClosureArguments closureArgs: [(argumentIndex: Int, argumentValue: Value)],
96+
from applySiteCallee: Function
97+
) -> String {
98+
closureArgs.withBridgedArrayRef{ bridgedClosureArgs in
99+
String(taking: bridgedPassContext.mangleWithClosureArgs(bridgedClosureArgs, applySiteCallee.bridged))
104100
}
105101
}
106102

SwiftCompilerSources/Sources/Optimizer/Utilities/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
swift_compiler_sources(Optimizer
1010
AddressUtils.swift
11-
SpecializationCloner.swift
1211
Devirtualization.swift
1312
EscapeUtils.swift
1413
FunctionSignatureTransforms.swift
@@ -18,5 +17,4 @@ swift_compiler_sources(Optimizer
1817
LocalVariableUtils.swift
1918
OptUtils.swift
2019
OwnershipLiveness.swift
21-
StaticInitCloner.swift
2220
)

SwiftCompilerSources/Sources/Optimizer/Utilities/SpecializationCloner.swift

Lines changed: 0 additions & 52 deletions
This file was deleted.

SwiftCompilerSources/Sources/Optimizer/Utilities/StaticInitCloner.swift

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)