Skip to content

Commit 231042b

Browse files
committed
SIL: some Cloner cleanups and improvements
* move some Cloner utilities from ContextCommon.swift directly into Cloner.swift * add an `cloneRecursively` overload which doesn't require the `customGetCloned` closure argument * some small cleanups
1 parent 65d69fe commit 231042b

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/ContextCommon.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,6 @@ extension Instruction {
164164
}
165165
}
166166

167-
extension Cloner where Context == FunctionPassContext {
168-
func getOrCreateEntryBlock() -> BasicBlock {
169-
if let entryBlock = targetFunction.blocks.first {
170-
return entryBlock
171-
}
172-
return targetFunction.appendNewBlock(context)
173-
}
174-
175-
func cloneFunctionBody(from originalFunction: Function, entryBlockArguments: [Value]) {
176-
entryBlockArguments.withBridgedValues { bridgedEntryBlockArgs in
177-
let entryBlock = getOrCreateEntryBlock()
178-
bridged.cloneFunctionBody(originalFunction.bridged, entryBlock.bridged, bridgedEntryBlockArgs)
179-
}
180-
}
181-
182-
func cloneFunctionBody(from originalFunction: Function) {
183-
bridged.cloneFunctionBody(originalFunction.bridged)
184-
}
185-
}
186-
187167
func cloneFunction(from originalFunction: Function, toEmpty targetFunction: Function, _ context: FunctionPassContext) {
188168
var cloner = Cloner(cloneToEmptyFunction: targetFunction, context)
189169
defer { cloner.deinitialize() }

SwiftCompilerSources/Sources/SIL/Utilities/Cloner.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import OptimizerBridging
1413
import SILBridging
1514

1615
/// Clones the initializer value of a GlobalVariable.
@@ -19,7 +18,7 @@ import SILBridging
1918
/// from or to the static initializer value of a GlobalVariable.
2019
///
2120
public struct Cloner<Context: MutatingContext> {
22-
public var bridged: BridgedCloner
21+
private var bridged: BridgedCloner
2322
public let context: Context
2423

2524
public enum GetClonedResult {
@@ -45,7 +44,7 @@ public struct Cloner<Context: MutatingContext> {
4544
self.context = context
4645
self.target = .function(inst.parentFunction)
4746
}
48-
47+
4948
public init(cloneToEmptyFunction: Function, _ context: Context) {
5049
self.bridged = BridgedCloner(cloneToEmptyFunction.bridged, context._bridged)
5150
self.context = context
@@ -63,6 +62,24 @@ public struct Cloner<Context: MutatingContext> {
6362
return function
6463
}
6564

65+
public func getOrCreateEntryBlock() -> BasicBlock {
66+
if let entryBlock = targetFunction.blocks.first {
67+
return entryBlock
68+
}
69+
return targetFunction.appendNewBlock(context)
70+
}
71+
72+
public func cloneFunctionBody(from originalFunction: Function, entryBlockArguments: [Value]) {
73+
entryBlockArguments.withBridgedValues { bridgedEntryBlockArgs in
74+
let entryBlock = getOrCreateEntryBlock()
75+
bridged.cloneFunctionBody(originalFunction.bridged, entryBlock.bridged, bridgedEntryBlockArgs)
76+
}
77+
}
78+
79+
public func cloneFunctionBody(from originalFunction: Function) {
80+
bridged.cloneFunctionBody(originalFunction.bridged)
81+
}
82+
6683
public mutating func clone(instruction: Instruction) -> Instruction {
6784
let cloned = bridged.clone(instruction.bridged).instruction
6885
if case .function = target {
@@ -71,7 +88,7 @@ public struct Cloner<Context: MutatingContext> {
7188
}
7289
return cloned
7390
}
74-
91+
7592
public mutating func cloneRecursivelyToGlobal(value: Value) -> Value {
7693
guard let cloned = cloneRecursively(value: value, customGetCloned: { value, cloner in
7794
guard let beginAccess = value as? BeginAccessInst else {
@@ -89,6 +106,11 @@ public struct Cloner<Context: MutatingContext> {
89106
return cloned
90107
}
91108

109+
/// Transitively clones `value` including its defining instruction's operands.
110+
public mutating func cloneRecursively( value: Value) -> Value {
111+
return cloneRecursively(value: value, customGetCloned: { _, _ in .defaultValue })!
112+
}
113+
92114
/// Transitively clones `value` including its defining instruction's operands.
93115
public mutating func cloneRecursively(value: Value, customGetCloned: (Value, inout Cloner) -> GetClonedResult) -> Value? {
94116
if isCloned(value: value) {
@@ -134,7 +156,7 @@ public struct Cloner<Context: MutatingContext> {
134156
public func getClonedBlock(for originalBlock: BasicBlock) -> BasicBlock {
135157
bridged.getClonedBasicBlock(originalBlock.bridged).block
136158
}
137-
159+
138160
public func recordFoldedValue(_ origValue: Value, mappedTo mappedValue: Value) {
139161
bridged.recordFoldedValue(origValue.bridged, mappedValue.bridged)
140162
}

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ struct BridgedCloner {
15381538
bool isValueCloned(BridgedValue v) const;
15391539
void recordClonedInstruction(BridgedInstruction origInst, BridgedInstruction clonedInst) const;
15401540
void recordFoldedValue(BridgedValue orig, BridgedValue mapped) const;
1541-
BridgedInstruction clone(BridgedInstruction inst);
1541+
BridgedInstruction clone(BridgedInstruction inst) const;
15421542
};
15431543

15441544
struct BridgedVerifier {

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class SILLoopInfo;
4848
class SILLoop;
4949
class SwiftPassInvocation;
5050
class SILVTable;
51-
class SpecializationCloner;
5251
}
5352

5453
struct BridgedPassContext;

lib/SIL/Utils/SILBridging.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,7 @@ BridgedInstruction BridgedBuilder::createSwitchEnumAddrInst(BridgedValue enumAdd
552552
// BridgedCloner
553553
//===----------------------------------------------------------------------===//
554554

555-
// Need to put ClonerWithFixedLocation into namespace swift to forward reference
556-
// it in OptimizerBridging.h.
555+
// Need to put the cloner Impl classes into namespace swift to forward reference it from SILBridging.h.
557556
namespace swift {
558557

559558
class BridgedClonerImpl : public SILCloner<BridgedClonerImpl> {
@@ -668,7 +667,7 @@ void BridgedCloner::recordFoldedValue(BridgedValue orig, BridgedValue mapped) co
668667
cloner->recordFoldedValue(orig.getSILValue(), mapped.getSILValue());
669668
}
670669

671-
BridgedInstruction BridgedCloner::clone(BridgedInstruction inst) {
670+
BridgedInstruction BridgedCloner::clone(BridgedInstruction inst) const {
672671
return {cloner->cloneInst(inst.unbridged())->asSILNode()};
673672
}
674673

0 commit comments

Comments
 (0)