Skip to content

Commit 4bdd3c5

Browse files
committed
Optimizer: add the FunctionPassContext.completeLifetime(of: Value) utility
Implemented by bridging the OSSALifetimeCompletion utility
1 parent 83e49c1 commit 4bdd3c5

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@ struct FunctionPassContext : MutatingContext {
422422

423423
return buildFn(specializedFunction, nestedFunctionPassContext)
424424
}
425+
426+
/// Makes sure that the lifetime of `value` ends at all control flow paths, even in dead-end blocks.
427+
/// Inserts destroys in dead-end blocks if those are missing.
428+
func completeLifetime(of value: Value) {
429+
if _bridged.completeLifetime(value.bridged) {
430+
notifyInstructionsChanged()
431+
}
432+
}
425433
}
426434

427435
struct SimplifyContext : MutatingContext {

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ struct BridgedPassContext {
397397
SwiftInt paramCount,
398398
BridgedFunction bridgedApplySiteCallee,
399399
bool isSerialized) const;
400+
401+
bool completeLifetime(BridgedValue value) const;
400402
};
401403

402404
bool FullApplySite_canInline(BridgedInstruction apply);

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/Demangling/Demangler.h"
2222
#include "swift/SIL/ApplySite.h"
2323
#include "swift/SIL/DynamicCasts.h"
24+
#include "swift/SIL/OSSALifetimeCompletion.h"
2425
#include "swift/SIL/SILBridging.h"
2526
#include "swift/SIL/SILCloner.h"
2627
#include "swift/SIL/SILFunction.h"
@@ -2041,6 +2042,16 @@ ClosureSpecializer_createEmptyFunctionWithSpecializedSignature(BridgedStringRef
20412042
return {specializedApplySiteCallee};
20422043
}
20432044

2045+
bool BridgedPassContext::completeLifetime(BridgedValue value) const {
2046+
SILValue v = value.getSILValue();
2047+
SILFunction *f = v->getFunction();
2048+
DeadEndBlocks *deb = invocation->getPassManager()->getAnalysis<DeadEndBlocksAnalysis>()->get(f);
2049+
DominanceInfo *domInfo = invocation->getPassManager()->getAnalysis<DominanceAnalysis>()->get(f);
2050+
OSSALifetimeCompletion completion(f, domInfo, *deb);
2051+
auto result = completion.completeOSSALifetime(v, OSSALifetimeCompletion::Boundary::Availability);
2052+
return result == LifetimeCompletion::WasCompleted;
2053+
}
2054+
20442055
bool FullApplySite_canInline(BridgedInstruction apply) {
20452056
return swift::SILInliner::canInlineApplySite(
20462057
swift::FullApplySite(apply.unbridged()));

0 commit comments

Comments
 (0)