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
This commit is contained in:
Erik Eckstein
2025-09-03 09:00:00 +02:00
parent 65d69fe965
commit 231042b9a8
5 changed files with 30 additions and 30 deletions

View File

@@ -164,26 +164,6 @@ extension Instruction {
}
}
extension Cloner where Context == FunctionPassContext {
func getOrCreateEntryBlock() -> BasicBlock {
if let entryBlock = targetFunction.blocks.first {
return entryBlock
}
return targetFunction.appendNewBlock(context)
}
func cloneFunctionBody(from originalFunction: Function, entryBlockArguments: [Value]) {
entryBlockArguments.withBridgedValues { bridgedEntryBlockArgs in
let entryBlock = getOrCreateEntryBlock()
bridged.cloneFunctionBody(originalFunction.bridged, entryBlock.bridged, bridgedEntryBlockArgs)
}
}
func cloneFunctionBody(from originalFunction: Function) {
bridged.cloneFunctionBody(originalFunction.bridged)
}
}
func cloneFunction(from originalFunction: Function, toEmpty targetFunction: Function, _ context: FunctionPassContext) {
var cloner = Cloner(cloneToEmptyFunction: targetFunction, context)
defer { cloner.deinitialize() }

View File

@@ -10,7 +10,6 @@
//
//===----------------------------------------------------------------------===//
import OptimizerBridging
import SILBridging
/// Clones the initializer value of a GlobalVariable.
@@ -19,7 +18,7 @@ import SILBridging
/// from or to the static initializer value of a GlobalVariable.
///
public struct Cloner<Context: MutatingContext> {
public var bridged: BridgedCloner
private var bridged: BridgedCloner
public let context: Context
public enum GetClonedResult {
@@ -45,7 +44,7 @@ public struct Cloner<Context: MutatingContext> {
self.context = context
self.target = .function(inst.parentFunction)
}
public init(cloneToEmptyFunction: Function, _ context: Context) {
self.bridged = BridgedCloner(cloneToEmptyFunction.bridged, context._bridged)
self.context = context
@@ -63,6 +62,24 @@ public struct Cloner<Context: MutatingContext> {
return function
}
public func getOrCreateEntryBlock() -> BasicBlock {
if let entryBlock = targetFunction.blocks.first {
return entryBlock
}
return targetFunction.appendNewBlock(context)
}
public func cloneFunctionBody(from originalFunction: Function, entryBlockArguments: [Value]) {
entryBlockArguments.withBridgedValues { bridgedEntryBlockArgs in
let entryBlock = getOrCreateEntryBlock()
bridged.cloneFunctionBody(originalFunction.bridged, entryBlock.bridged, bridgedEntryBlockArgs)
}
}
public func cloneFunctionBody(from originalFunction: Function) {
bridged.cloneFunctionBody(originalFunction.bridged)
}
public mutating func clone(instruction: Instruction) -> Instruction {
let cloned = bridged.clone(instruction.bridged).instruction
if case .function = target {
@@ -71,7 +88,7 @@ public struct Cloner<Context: MutatingContext> {
}
return cloned
}
public mutating func cloneRecursivelyToGlobal(value: Value) -> Value {
guard let cloned = cloneRecursively(value: value, customGetCloned: { value, cloner in
guard let beginAccess = value as? BeginAccessInst else {
@@ -89,6 +106,11 @@ public struct Cloner<Context: MutatingContext> {
return cloned
}
/// Transitively clones `value` including its defining instruction's operands.
public mutating func cloneRecursively( value: Value) -> Value {
return cloneRecursively(value: value, customGetCloned: { _, _ in .defaultValue })!
}
/// Transitively clones `value` including its defining instruction's operands.
public mutating func cloneRecursively(value: Value, customGetCloned: (Value, inout Cloner) -> GetClonedResult) -> Value? {
if isCloned(value: value) {
@@ -134,7 +156,7 @@ public struct Cloner<Context: MutatingContext> {
public func getClonedBlock(for originalBlock: BasicBlock) -> BasicBlock {
bridged.getClonedBasicBlock(originalBlock.bridged).block
}
public func recordFoldedValue(_ origValue: Value, mappedTo mappedValue: Value) {
bridged.recordFoldedValue(origValue.bridged, mappedValue.bridged)
}