Skip to content

Commit 5e11357

Browse files
authored
Merge branch 'release/6.2' into 62-reassign-immortal
2 parents 80d19ff + d03fd67 commit 5e11357

File tree

18 files changed

+285
-15
lines changed

18 files changed

+285
-15
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LetPropertyLowering.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ private func insertEndInitInstructions(
122122
use.set(to: ssaUpdater.getValue(atEndOf: use.instruction.parentBlock), context)
123123
}
124124
}
125+
// This peephole optimization is required to avoid ownership errors.
126+
replacePhisWithIncomingValues(phis: ssaUpdater.insertedPhis, context)
125127
}
126128

127129
private func constructLetInitRegion(

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
122122
}
123123
}
124124
case let metatype as MetatypeInst:
125-
if context.options.enableEmbeddedSwift {
125+
if context.options.enableEmbeddedSwift,
126+
metatype.type.representationOfMetatype == .thick {
126127
let instanceType = metatype.type.loweredInstanceTypeOfMetatype(in: function)
127128
if instanceType.isClass {
128129
specializeVTable(forClassType: instanceType, errorLocation: metatype.location, moduleContext) {

SwiftCompilerSources/Sources/Optimizer/Utilities/AddressUtils.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,10 @@ extension AddressOwnershipLiveRange {
648648
let addressOwnershipLiveRangeTest = FunctionTest("address_ownership_live_range") {
649649
function, arguments, context in
650650
let address = arguments.takeValue()
651+
let begin = arguments.takeInstruction()
651652
print("Address: \(address)")
652653
print("Base: \(address.accessBase)")
653-
let begin = address.definingInstructionOrTerminator ?? {
654-
assert(address is FunctionArgument)
655-
return function.instructions.first!
656-
}()
654+
print("Begin: \(begin)")
657655
let localReachabilityCache = LocalVariableReachabilityCache()
658656
guard var ownershipRange = AddressOwnershipLiveRange.compute(for: address, at: begin,
659657
localReachabilityCache, context) else {

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
640640
}
641641

642642
// Indirect arguments cannot escape the function, but loaded values from such can.
643-
if !followLoads(at: path) {
643+
if argOp.value.type.isAddress && !followLoads(at: path) {
644644
if let beginApply = apply as? BeginApplyInst {
645645
// begin_apply can yield an address value.
646646
if !indirectResultEscapes(of: beginApply, path: path) {

SwiftCompilerSources/Sources/Optimizer/Utilities/LifetimeDependenceUtils.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,8 @@ extension LifetimeDependenceDefUseWalker {
975975
return yieldedDependence(result: localAccess.operand!)
976976
case .incomingArgument:
977977
fatalError("Incoming arguments are never reachable")
978+
case .deadEnd:
979+
return .continueWalk
978980
}
979981
}
980982

SwiftCompilerSources/Sources/Optimizer/Utilities/LocalVariableUtils.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct LocalVariableAccess: CustomStringConvertible {
7272
case storeBorrow // scoped initialization of temporaries
7373
case apply // indirect arguments
7474
case escape // alloc_box captures
75+
case deadEnd // unreachable
7576
}
7677
let kind: Kind
7778
// All access have an operand except .incomingArgument and .outgoingArgument.
@@ -102,7 +103,7 @@ struct LocalVariableAccess: CustomStringConvertible {
102103
case .`init`, .modify:
103104
return true
104105
}
105-
case .load, .dependenceSource, .dependenceDest:
106+
case .load, .dependenceSource, .dependenceDest, .deadEnd:
106107
return false
107108
case .incomingArgument, .outgoingArgument, .store, .storeBorrow, .inoutYield:
108109
return true
@@ -153,6 +154,8 @@ struct LocalVariableAccess: CustomStringConvertible {
153154
str += "apply"
154155
case .escape:
155156
str += "escape"
157+
case .deadEnd:
158+
str += "deadEnd"
156159
}
157160
if let inst = instruction {
158161
str += "\(inst)"
@@ -201,7 +204,7 @@ class LocalVariableAccessInfo: CustomStringConvertible {
201204
self.hasEscaped = true
202205
case .inoutYield:
203206
self._isFullyAssigned = false
204-
case .incomingArgument, .outgoingArgument:
207+
case .incomingArgument, .outgoingArgument, .deadEnd:
205208
fatalError("Function arguments are never mapped to LocalVariableAccessInfo")
206209
}
207210
}
@@ -753,8 +756,8 @@ extension LocalVariableReachableAccess {
753756
///
754757
/// This does not include the destroy or reassignment of the value set by `modifyInst`.
755758
///
756-
/// Returns true if all possible reachable uses were visited. Returns false if any escapes may reach `modifyInst` are
757-
/// reachable from `modifyInst`.
759+
/// Returns true if all possible reachable uses were visited. Returns false if any escapes may reach `modifyInst` or
760+
/// are reachable from `modifyInst`.
758761
///
759762
/// This does not gather the escaping accesses themselves. When escapes are reachable, it also does not guarantee that
760763
/// previously reachable accesses are gathered.
@@ -855,6 +858,8 @@ extension LocalVariableReachableAccess {
855858
}
856859
if block.terminator.isFunctionExiting {
857860
accessStack.push(LocalVariableAccess(.outgoingArgument, block.terminator))
861+
} else if block.successors.isEmpty {
862+
accessStack.push(LocalVariableAccess(.deadEnd, block.terminator))
858863
} else {
859864
for successor in block.successors { blockList.pushIfNotVisited(successor) }
860865
}

SwiftCompilerSources/Sources/Optimizer/Utilities/SSAUpdater.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ struct SSAUpdater<Context: MutatingContext> {
5050
context.notifyInstructionsChanged()
5151
return context._bridged.SSAUpdater_getValueInMiddleOfBlock(block.bridged).value
5252
}
53+
54+
var insertedPhis: [Phi] {
55+
var phis = [Phi]()
56+
let numPhis = context._bridged.SSAUpdater_getNumInsertedPhis()
57+
phis.reserveCapacity(numPhis)
58+
for idx in 0..<numPhis {
59+
phis.append(Phi(context._bridged.SSAUpdater_getInsertedPhi(idx).value)!)
60+
}
61+
return phis
62+
}
5363
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ struct BridgedPassContext {
367367
BRIDGED_INLINE void SSAUpdater_addAvailableValue(BridgedBasicBlock block, BridgedValue value) const;
368368
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueAtEndOfBlock(BridgedBasicBlock block) const;
369369
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueInMiddleOfBlock(BridgedBasicBlock block) const;
370+
BRIDGED_INLINE SwiftInt SSAUpdater_getNumInsertedPhis() const;
371+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getInsertedPhi(SwiftInt idx) const;
370372

371373
// Options
372374

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,14 @@ BridgedValue BridgedPassContext::SSAUpdater_getValueInMiddleOfBlock(BridgedBasic
548548
invocation->getSSAUpdater()->getValueInMiddleOfBlock(block.unbridged())};
549549
}
550550

551+
SwiftInt BridgedPassContext::SSAUpdater_getNumInsertedPhis() const {
552+
return (SwiftInt)invocation->getInsertedPhisBySSAUpdater().size();
553+
}
554+
555+
BridgedValue BridgedPassContext::SSAUpdater_getInsertedPhi(SwiftInt idx) const {
556+
return {invocation->getInsertedPhisBySSAUpdater()[idx]};
557+
}
558+
551559
bool BridgedPassContext::enableStackProtection() const {
552560
swift::SILModule *mod = invocation->getPassManager()->getModule();
553561
return mod->getOptions().EnableStackProtection;

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class SwiftPassInvocation {
7575
SILModule::SlabList allocatedSlabs;
7676

7777
SILSSAUpdater *ssaUpdater = nullptr;
78+
SmallVector<SILPhiArgument *, 4> insertedPhisBySSAUpdater;
7879

7980
SwiftPassInvocation *nestedSwiftPassInvocation = nullptr;
8081

@@ -178,8 +179,9 @@ class SwiftPassInvocation {
178179

179180
void initializeSSAUpdater(SILFunction *fn, SILType type,
180181
ValueOwnershipKind ownership) {
182+
insertedPhisBySSAUpdater.clear();
181183
if (!ssaUpdater)
182-
ssaUpdater = new SILSSAUpdater;
184+
ssaUpdater = new SILSSAUpdater(&insertedPhisBySSAUpdater);
183185
ssaUpdater->initialize(fn, type, ownership);
184186
}
185187

@@ -188,6 +190,8 @@ class SwiftPassInvocation {
188190
return ssaUpdater;
189191
}
190192

193+
ArrayRef<SILPhiArgument *> getInsertedPhisBySSAUpdater() { return insertedPhisBySSAUpdater; }
194+
191195
SwiftPassInvocation *initializeNestedSwiftPassInvocation(SILFunction *newFunction) {
192196
assert(!nestedSwiftPassInvocation && "Nested Swift pass invocation already initialized");
193197
nestedSwiftPassInvocation = new SwiftPassInvocation(passManager, transform, newFunction);

0 commit comments

Comments
 (0)