Skip to content

Commit 2cd625a

Browse files
committed
Optimizer: add the Builder.insertCleanupAtFunctionExits utility
1 parent c3612ba commit 2cd625a

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ConstantCapturePropagation.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ private func cloneArgument(_ argumentOp: Operand,
195195
if partialApply.calleeArgumentConventions[calleeArgIdx].isGuaranteed {
196196
// If the original argument was passed as guaranteed, i.e. is _not_ destroyed in the closure, we have
197197
// to destroy the cloned argument at function exits.
198-
for block in targetFunction.blocks where block.terminator.isFunctionExiting {
199-
let builder = Builder(before: block.terminator, context)
198+
Builder.insertCleanupAtFunctionExits(of: targetFunction, context) { builder in
200199
builder.emitDestroy(of: clonedArg)
201200
}
202201
}

SwiftCompilerSources/Sources/Optimizer/ModulePasses/StackProtection.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,9 @@ private struct StackProtectionOptimization {
339339

340340
builder.createCopyAddr(from: argument, to: temporary, takeSource: true, initializeDest: true)
341341

342-
for block in function.blocks {
343-
let terminator = block.terminator
344-
if terminator.isFunctionExiting {
345-
let exitBuilder = Builder(before: terminator, location: terminator.location.asAutoGenerated, context)
346-
exitBuilder.createCopyAddr(from: temporary, to: argument, takeSource: true, initializeDest: true)
347-
exitBuilder.createDeallocStack(temporary)
348-
}
342+
Builder.insertCleanupAtFunctionExits(of: function, context) { builder in
343+
builder.createCopyAddr(from: temporary, to: argument, takeSource: true, initializeDest: true)
344+
builder.createDeallocStack(temporary)
349345
}
350346
log("move addr protection in \(function.name): \(argument)")
351347

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ extension Builder {
262262
}
263263
}
264264

265+
static func insertCleanupAtFunctionExits(
266+
of function: Function,
267+
_ context: some MutatingContext,
268+
insert: (Builder) -> ()
269+
) {
270+
for exitBlock in function.blocks where exitBlock.terminator.isFunctionExiting {
271+
let terminator = exitBlock.terminator
272+
let builder = Builder(before: terminator, location: terminator.location.asCleanup, context)
273+
insert(builder)
274+
}
275+
}
276+
265277
func destroyCapturedArgs(for paiOnStack: PartialApplyInst) {
266278
precondition(paiOnStack.isOnStack, "Function must only be called for `partial_apply`s on stack!")
267279
self.bridged.destroyCapturedArgs(paiOnStack.bridged)

0 commit comments

Comments
 (0)