Skip to content

Commit 78cb4ca

Browse files
committed
Optimizer: extract replacing an apply into a utility function ApplySite.replace(withCallTo:)
1 parent 17ca4d9 commit 78cb4ca

File tree

3 files changed

+44
-57
lines changed

3 files changed

+44
-57
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocBoxToStack.swift

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -257,31 +257,8 @@ private struct FunctionSpecializations {
257257
return argOp.value
258258
}
259259
}
260-
let specializedCallee = builder.createFunctionRef(originalToSpecialized[callee]!)
261-
262-
switch apply {
263-
case let applyInst as ApplyInst:
264-
let newApply = builder.createApply(function: specializedCallee, applyInst.substitutionMap, arguments: newArgs, isNonThrowing: applyInst.isNonThrowing)
265-
applyInst.replace(with: newApply, context)
266-
case let partialAp as PartialApplyInst:
267-
let newApply = builder.createPartialApply(function: specializedCallee, substitutionMap:
268-
partialAp.substitutionMap,
269-
capturedArguments: newArgs,
270-
calleeConvention: partialAp.calleeConvention,
271-
hasUnknownResultIsolation: partialAp.hasUnknownResultIsolation,
272-
isOnStack: partialAp.isOnStack)
273-
partialAp.replace(with: newApply, context)
274-
case let tryApply as TryApplyInst:
275-
builder.createTryApply(function: specializedCallee, tryApply.substitutionMap, arguments: newArgs,
276-
normalBlock: tryApply.normalBlock, errorBlock: tryApply.errorBlock)
277-
context.erase(instruction: tryApply)
278-
case let beginApply as BeginApplyInst:
279-
let newApply = builder.createBeginApply(function: specializedCallee, beginApply.substitutionMap,
280-
arguments: newArgs)
281-
beginApply.replace(with: newApply, context)
282-
default:
283-
fatalError("unknown apply")
284-
}
260+
apply.replace(withCallTo: originalToSpecialized[callee]!, arguments: newArgs, context)
261+
285262
// It is important to delete the dead `function_ref`. Otherwise it will still reference the original
286263
// function which prevents deleting it in the mandatory-allocbox-to-stack pass.
287264
if fri.uses.isEmpty {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ClosureSpecialization.swift

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -538,38 +538,7 @@ private struct SpecializationInfo {
538538

539539
let newApplyArgs = getNewApplyArguments(context)
540540

541-
let builder = Builder(before: apply, context)
542-
let funcRef = builder.createFunctionRef(specializedFunction)
543-
544-
switch apply {
545-
case let oldPartialApply as PartialApplyInst:
546-
let newPartialApply = builder.createPartialApply(
547-
function: funcRef, substitutionMap: SubstitutionMap(),
548-
capturedArguments: newApplyArgs, calleeConvention: oldPartialApply.calleeConvention,
549-
hasUnknownResultIsolation: oldPartialApply.hasUnknownResultIsolation,
550-
isOnStack: oldPartialApply.isOnStack)
551-
oldPartialApply.replace(with: newPartialApply, context)
552-
553-
case let oldApply as ApplyInst:
554-
let newApply = builder.createApply(function: funcRef, SubstitutionMap(), arguments: newApplyArgs,
555-
isNonThrowing: oldApply.isNonThrowing,
556-
isNonAsync: oldApply.isNonAsync)
557-
oldApply.replace(with: newApply, context)
558-
559-
case let oldTryApply as TryApplyInst:
560-
builder.createTryApply(function: funcRef, SubstitutionMap(), arguments: newApplyArgs,
561-
normalBlock: oldTryApply.normalBlock, errorBlock: oldTryApply.errorBlock,
562-
isNonAsync: oldTryApply.isNonAsync)
563-
context.erase(instruction: oldTryApply)
564-
565-
case let oldBeginApply as BeginApplyInst:
566-
let newApply = builder.createBeginApply(function: funcRef, SubstitutionMap(), arguments: newApplyArgs,
567-
isNonThrowing: oldBeginApply.isNonThrowing,
568-
isNonAsync: oldBeginApply.isNonAsync)
569-
oldBeginApply.replace(with: newApply, context)
570-
default:
571-
fatalError("unknown apply")
572-
}
541+
apply.replace(withCallTo: specializedFunction, arguments: newApplyArgs, context)
573542
}
574543

575544
private func insertCompensatingDestroysForOwnedClosureArguments(_ context: FunctionPassContext) {

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,47 @@ extension FullApplySite {
241241
}
242242
}
243243

244+
extension ApplySite {
245+
func replace(withCallTo callee: Function, arguments newArguments: [Value], _ context: some MutatingContext) {
246+
let builder = Builder(before: self, context)
247+
let calleeRef = builder.createFunctionRef(callee)
248+
249+
switch self {
250+
case let applyInst as ApplyInst:
251+
let newApply = builder.createApply(function: calleeRef,
252+
applyInst.substitutionMap,
253+
arguments: newArguments,
254+
isNonThrowing: applyInst.isNonThrowing)
255+
applyInst.replace(with: newApply, context)
256+
257+
case let partialAp as PartialApplyInst:
258+
let newApply = builder.createPartialApply(function: calleeRef,
259+
substitutionMap: partialAp.substitutionMap,
260+
capturedArguments: newArguments,
261+
calleeConvention: partialAp.calleeConvention,
262+
hasUnknownResultIsolation: partialAp.hasUnknownResultIsolation,
263+
isOnStack: partialAp.isOnStack)
264+
partialAp.replace(with: newApply, context)
265+
266+
case let tryApply as TryApplyInst:
267+
builder.createTryApply(function: calleeRef,
268+
tryApply.substitutionMap,
269+
arguments: newArguments,
270+
normalBlock: tryApply.normalBlock, errorBlock: tryApply.errorBlock)
271+
context.erase(instruction: tryApply)
272+
273+
case let beginApply as BeginApplyInst:
274+
let newApply = builder.createBeginApply(function: calleeRef,
275+
beginApply.substitutionMap,
276+
arguments: newArguments)
277+
beginApply.replace(with: newApply, context)
278+
279+
default:
280+
fatalError("unknown apply")
281+
}
282+
}
283+
}
284+
244285
extension Builder {
245286
static func insert(after inst: Instruction, _ context: some MutatingContext, insertFunc: (Builder) -> ()) {
246287
Builder.insert(after: inst, location: inst.location, context, insertFunc: insertFunc)

0 commit comments

Comments
 (0)