Skip to content

Commit e6b9cb9

Browse files
authored
Merge pull request #78290 from eeckstein/sil-infrastructure
Some AST and SIL infrastructure additions and improvements
2 parents e0ccd1b + 8935350 commit e6b9cb9

25 files changed

+254
-166
lines changed

SwiftCompilerSources/Sources/AST/SubstitutionMap.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public struct SubstitutionMap: CustomStringConvertible {
5656
return Conformance(bridged: bridgedSubs.getConformance(index))
5757
}
5858
}
59+
60+
public var replacementTypes: TypeArray {
61+
TypeArray(bridged: bridged.getReplacementTypes())
62+
}
63+
64+
/// The single replacement type if it's guarnateed that the substitution map has a single replacement type.
65+
public var replacementType: Type {
66+
assert(replacementTypes.count == 1)
67+
return replacementTypes[0]
68+
}
5969
}

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public struct Type: CustomStringConvertible, NoReflectionChildren {
4646
/// A Type that is statically known to be canonical.
4747
/// For example, typealiases are resolved.
4848
public struct CanonicalType: CustomStringConvertible, NoReflectionChildren {
49+
public enum TraitResult {
50+
case isNot
51+
case canBe
52+
case `is`
53+
}
54+
4955
public let bridged: BridgedCanType
5056

5157
public init(bridged: BridgedCanType) { self.bridged = bridged }
@@ -63,4 +69,38 @@ public struct CanonicalType: CustomStringConvertible, NoReflectionChildren {
6369
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
6470
return type.subst(with: substitutionMap).canonical
6571
}
72+
73+
public var canBeClass: TraitResult { bridged.canBeClass().result }
74+
}
75+
76+
public struct TypeArray : RandomAccessCollection, CustomReflectable {
77+
public let bridged: BridgedASTTypeArray
78+
79+
public var startIndex: Int { return 0 }
80+
public var endIndex: Int { return bridged.getCount() }
81+
82+
public init(bridged: BridgedASTTypeArray) {
83+
self.bridged = bridged
84+
}
85+
86+
public subscript(_ index: Int) -> Type {
87+
Type(bridged: bridged.getAt(index))
88+
}
89+
90+
public var customMirror: Mirror {
91+
let c: [Mirror.Child] = map { (label: nil, value: $0) }
92+
return Mirror(self, children: c)
93+
}
94+
}
95+
96+
extension BridgedCanType.TraitResult {
97+
var result: CanonicalType.TraitResult {
98+
switch self {
99+
case .IsNot: return .isNot
100+
case .CanBe: return .canBe
101+
case .Is: return .is
102+
default:
103+
fatalError("wrong type TraitResult enum case")
104+
}
105+
}
66106
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocVectorLowering.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ private func createOutlinedGlobal(
224224
return
225225
}
226226

227-
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0]!
227+
let function = allocVectorBuiltin.parentFunction
228+
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0].loweredType(in: function)
228229
let outlinedGlobal = context.createGlobalVariable(
229-
name: context.mangleOutlinedVariable(from: allocVectorBuiltin.parentFunction),
230+
name: context.mangleOutlinedVariable(from: function),
230231
type: elementType, linkage: .private, isLet: false)
231232

232233
let globalBuilder = Builder(staticInitializerOf: outlinedGlobal, context)
@@ -249,8 +250,7 @@ private func createOutlinedGlobal(
249250
let globalAddr = builder.createGlobalAddr(global: outlinedGlobal, dependencyToken: nil)
250251
let rawVectorPointer = builder.createAddressToPointer(address: globalAddr, pointerType: allocVectorBuiltin.type,
251252
needStackProtection: false)
252-
allocVectorBuiltin.uses.replaceAll(with: rawVectorPointer, context)
253-
context.erase(instruction: allocVectorBuiltin)
253+
allocVectorBuiltin.replace(with: rawVectorPointer, context)
254254
}
255255

256256
private func createStackAllocatedVector(
@@ -259,13 +259,13 @@ private func createStackAllocatedVector(
259259
_ context: FunctionPassContext
260260
) {
261261
let builder = Builder(before: allocVectorBuiltin, context)
262-
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0]!
262+
let function = allocVectorBuiltin.parentFunction
263+
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0].loweredType(in: function)
263264
let allocVec = builder.createAllocVector(capacity: allocVectorBuiltin.operands[1].value, elementType: elementType)
264265
let rawVectorPointer = builder.createAddressToPointer(address: allocVec, pointerType: allocVectorBuiltin.type,
265266
needStackProtection: true)
266267

267-
allocVectorBuiltin.uses.replaceAll(with: rawVectorPointer, context)
268-
context.erase(instruction: allocVectorBuiltin)
268+
allocVectorBuiltin.replace(with: rawVectorPointer, context)
269269

270270
for endInst in liverange.ends {
271271
let builder = Builder(after: endInst, context)

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ClosureSpecialization.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ private func rewriteApplyInstruction(using specializedCallee: Function, callSite
301301
}
302302
}
303303

304-
oldApply.uses.replaceAll(with: newApply, context)
305-
context.erase(instruction: oldApply)
304+
oldApply.replace(with: newApply, context)
306305
}
307306

308307
// ===================== Utility functions and extensions ===================== //
@@ -1381,4 +1380,4 @@ let rewrittenCallerBodyTest = FunctionTest("closure_specialize_rewritten_caller_
13811380
print("Rewritten caller body for: \(function.name):")
13821381
print("\(function)\n")
13831382
}
1384-
}
1383+
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CopyToBorrowOptimization.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ private extension LoadInst {
260260
private func remove(copy: CopyValueInst, collectedUses: Uses, liverange: InstructionRange) {
261261
let context = collectedUses.context
262262
replaceMoveWithBorrow(of: copy, replacedBy: copy.fromValue, liverange: liverange, collectedUses: collectedUses)
263-
copy.uses.replaceAll(with: copy.fromValue, context)
264-
context.erase(instruction: copy)
263+
copy.replace(with: copy.fromValue, context)
265264

266265
for forwardingUse in collectedUses.forwardingUses {
267266
forwardingUse.changeOwnership(from: .owned, to: .guaranteed, context)
@@ -301,8 +300,7 @@ private func replaceMoveWithBorrow(
301300
isLexical: moveInst.isLexical,
302301
hasPointerEscape: moveInst.hasPointerEscape,
303302
isFromVarDecl: moveInst.isFromVarDecl)
304-
moveInst.uses.replaceAll(with: bbi, context)
305-
context.erase(instruction: moveInst)
303+
moveInst.replace(with: bbi, context)
306304
createEndBorrows(for: bbi, atEndOf: liverange, collectedUses: collectedUses)
307305
}
308306

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ private func optimizeNonOptionalBridging(_ apply: ApplyInst,
152152
let replacement = builder.createUncheckedEnumData(enum: optionalReplacement,
153153
caseIndex: someCase,
154154
resultType: bridgeToObjcCall.type)
155-
bridgeToObjcCall.uses.replaceAll(with: replacement, context)
156-
context.erase(instruction: bridgeToObjcCall)
155+
bridgeToObjcCall.replace(with: replacement, context)
157156
return true
158157
}
159158

@@ -219,8 +218,7 @@ private func optimizeNonOptionalBridging(_ apply: ApplyInst,
219218

220219
// Now replace the bridged value with the original value in the destination block.
221220
let replacement = s.makeAvailable(in: bridgeToObjcCall.parentBlock, context)
222-
bridgeToObjcCall.uses.replaceAll(with: replacement, context)
223-
context.erase(instruction: bridgeToObjcCall)
221+
bridgeToObjcCall.replace(with: replacement, context)
224222
return true
225223
}
226224

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjectOutliner.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,7 @@ private func replace(object allocRef: AllocRefInstBase,
413413
}
414414

415415
rewriteUses(of: allocRef, context)
416-
allocRef.uses.replaceAll(with: globalValue, context)
417-
context.erase(instruction: allocRef)
416+
allocRef.replace(with: globalValue, context)
418417
return globalValue
419418
}
420419

@@ -431,13 +430,11 @@ private func rewriteUses(of startValue: Value, _ context: FunctionPassContext) {
431430
if !beginDealloc.parentFunction.hasOwnership {
432431
builder.createStrongRelease(operand: beginDealloc.reference)
433432
}
434-
beginDealloc.uses.replaceAll(with: beginDealloc.reference, context)
435-
context.erase(instruction: beginDealloc)
433+
beginDealloc.replace(with: beginDealloc.reference, context)
436434
case is EndCOWMutationInst, is EndInitLetRefInst, is MoveValueInst:
437435
let svi = inst as! SingleValueInstruction
438436
worklist.pushIfNotVisited(usersOf: svi)
439-
svi.uses.replaceAll(with: svi.operands[0].value, context)
440-
context.erase(instruction: svi)
437+
svi.replace(with: svi.operands[0].value, context)
441438
case let upCast as UpcastInst:
442439
worklist.pushIfNotVisited(usersOf: upCast)
443440
case let refCast as UncheckedRefCastInst:
@@ -454,7 +451,7 @@ private func rewriteUses(of startValue: Value, _ context: FunctionPassContext) {
454451

455452
private extension InstructionWorklist {
456453
mutating func pushIfNotVisited(usersOf value: Value) {
457-
pushIfNotVisited(contentsOf: value.uses.lazy.map { $0.instruction })
454+
pushIfNotVisited(contentsOf: value.users)
458455
}
459456
}
460457

@@ -571,8 +568,7 @@ private func replace(findStringCall: ApplyInst,
571568
findStringCall.arguments[1],
572569
cacheAddr])
573570

574-
findStringCall.uses.replaceAll(with: newCall, context)
575-
context.erase(instruction: findStringCall)
571+
findStringCall.replace(with: newCall, context)
576572
}
577573

578574
private extension GlobalValueInst {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ private func replace(load: LoadInst, with availableValues: [AvailableValue], _ c
279279
//
280280
newValue = ssaUpdater.getValue(inMiddleOf: load.parentBlock)
281281
}
282-
load.uses.replaceAll(with: newValue, context)
283-
context.erase(instruction: load)
282+
load.replace(with: newValue, context)
284283
}
285284

286285
private func provideValue(

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyApply.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ private func tryTransformThickToThinCallee(of apply: ApplyInst, _ context: Simpl
5656
isNonThrowing: apply.isNonThrowing,
5757
isNonAsync: apply.isNonAsync,
5858
specializationInfo: apply.specializationInfo)
59-
apply.uses.replaceAll(with: newApply, context)
60-
context.erase(instruction: apply)
59+
apply.replace(with: newApply, context)
6160
return true
6261
}
6362
return false

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginAndLoadBorrow.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ extension LoadBorrowInst : Simplifyable, SILCombineSimplifyable {
5959
}
6060
let builder = Builder(before: self, context)
6161
let loadCopy = builder.createLoad(fromAddress: address, ownership: .copy)
62-
let forwardedOwnedValue = replace(guaranteedValue: self, withOwnedValue: loadCopy, context)
63-
copy.uses.replaceAll(with: forwardedOwnedValue, context)
64-
context.erase(instruction: copy)
62+
let forwardedOwnedValue = replaceGuaranteed(value: self, withOwnedValue: loadCopy, context)
63+
copy.replace(with: forwardedOwnedValue, context)
6564
context.erase(instructionIncludingAllUsers: self)
6665
}
6766
}
@@ -118,9 +117,8 @@ private func tryReplaceCopy(
118117
}
119118
let builder = Builder(before: beginBorrow, context)
120119
let copiedOperand = builder.createCopyValue(operand: beginBorrow.borrowedValue)
121-
let forwardedOwnedValue = replace(guaranteedValue: beginBorrow, withOwnedValue: copiedOperand, context)
122-
copy.uses.replaceAll(with: forwardedOwnedValue, context)
123-
context.erase(instruction: copy)
120+
let forwardedOwnedValue = replaceGuaranteed(value: beginBorrow, withOwnedValue: copiedOperand, context)
121+
copy.replace(with: forwardedOwnedValue, context)
124122
context.erase(instructionIncludingAllUsers: beginBorrow)
125123
return true
126124
}
@@ -140,7 +138,7 @@ private func tryReplaceCopy(
140138
/// destroy_value %2
141139
/// ```
142140
private func convertAllUsesToOwned(of beginBorrow: BeginBorrowInst, _ context: SimplifyContext) {
143-
let forwardedOwnedValue = replace(guaranteedValue: beginBorrow, withOwnedValue: beginBorrow.borrowedValue, context)
141+
let forwardedOwnedValue = replaceGuaranteed(value: beginBorrow, withOwnedValue: beginBorrow.borrowedValue, context)
144142
beginBorrow.borrowedValue.replaceAllDestroys(with: forwardedOwnedValue, context)
145143
context.erase(instructionIncludingAllUsers: beginBorrow)
146144
}
@@ -216,29 +214,29 @@ private extension ForwardingInstruction {
216214
///
217215
/// Returns the last owned value in a forwarding-chain or `ownedValue` if `guaranteedValue` has
218216
/// no forwarding uses.
219-
private func replace(guaranteedValue: Value, withOwnedValue ownedValue: Value, _ context: SimplifyContext) -> Value {
217+
private func replaceGuaranteed(value: Value, withOwnedValue ownedValue: Value, _ context: SimplifyContext) -> Value {
220218
var result = ownedValue
221219
var numForwardingUses = 0
222-
for use in guaranteedValue.uses {
220+
for use in value.uses {
223221

224222
switch use.instruction {
225223
case let tei as TupleExtractInst:
226224
numForwardingUses += 1
227225
let dti = Builder(before: tei, context).createDestructureTuple(tuple: ownedValue)
228-
result = replace(guaranteedValue: tei, withOwnedValue: dti.results[tei.fieldIndex], context)
226+
result = replaceGuaranteed(value: tei, withOwnedValue: dti.results[tei.fieldIndex], context)
229227
context.erase(instruction: tei)
230228
case let sei as StructExtractInst:
231229
numForwardingUses += 1
232230
let dsi = Builder(before: sei, context).createDestructureStruct(struct: ownedValue)
233-
result = replace(guaranteedValue: sei, withOwnedValue: dsi.results[sei.fieldIndex], context)
231+
result = replaceGuaranteed(value: sei, withOwnedValue: dsi.results[sei.fieldIndex], context)
234232
context.erase(instruction: sei)
235233
case let fwdInst as (SingleValueInstruction & ForwardingInstruction) where
236234
fwdInst.isSingleForwardedOperand(use):
237235
// Other forwarding instructions beside tuple_extract and struct_extract
238236
numForwardingUses += 1
239237
use.set(to: ownedValue, context)
240238
fwdInst.setForwardingOwnership(to: .owned, context)
241-
result = replace(guaranteedValue: fwdInst, withOwnedValue: fwdInst, context)
239+
result = replaceGuaranteed(value: fwdInst, withOwnedValue: fwdInst, context)
242240
case is EndBorrowInst:
243241
break
244242
default:

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBeginCOWMutation.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ private extension BeginCOWMutationInst {
8686

8787
for use in buffer.uses.ignoreDebugUses {
8888
let endCOW = use.instruction as! EndCOWMutationInst
89-
endCOW.uses.replaceAll(with: instance, context)
90-
context.erase(instruction: endCOW)
89+
endCOW.replace(with: instance, context)
9190
}
9291
context.erase(instructionIncludingDebugUses: self)
9392
return true

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBranch.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ private extension BranchInst {
5353
if let phi = Phi(arg),
5454
let bfi = phi.borrowedFrom
5555
{
56-
bfi.uses.replaceAll(with: op.value, context)
57-
context.erase(instruction: bfi)
56+
bfi.replace(with: op.value, context)
5857
} else {
5958
arg.uses.replaceAll(with: op.value, context)
6059
}

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ extension BuiltinInst : OnoneSimplifyable {
3333
.Alignof:
3434
optimizeTargetTypeConst(context)
3535
case .DestroyArray:
36-
if let elementType = substitutionMap.replacementTypes[0],
37-
elementType.isTrivial(in: parentFunction)
38-
{
36+
let elementType = substitutionMap.replacementType.loweredType(in: parentFunction, maximallyAbstracted: true)
37+
if elementType.isTrivial(in: parentFunction) {
3938
context.erase(instruction: self)
4039
return
4140
}
@@ -129,24 +128,18 @@ private extension BuiltinInst {
129128
}
130129

131130
func optimizeCanBeClass(_ context: SimplifyContext) {
132-
guard let ty = substitutionMap.replacementTypes[0] else {
133-
return
134-
}
135131
let literal: IntegerLiteralInst
136-
switch ty.canBeClass {
137-
case .IsNot:
132+
switch substitutionMap.replacementType.canonical.canBeClass {
133+
case .isNot:
138134
let builder = Builder(before: self, context)
139135
literal = builder.createIntegerLiteral(0, type: type)
140-
case .Is:
136+
case .is:
141137
let builder = Builder(before: self, context)
142138
literal = builder.createIntegerLiteral(1, type: type)
143-
case .CanBe:
139+
case .canBe:
144140
return
145-
default:
146-
fatalError()
147141
}
148-
uses.replaceAll(with: literal, context)
149-
context.erase(instruction: self)
142+
self.replace(with: literal, context)
150143
}
151144

152145
func optimizeAssertConfig(_ context: SimplifyContext) {
@@ -167,10 +160,7 @@ private extension BuiltinInst {
167160
}
168161

169162
func optimizeTargetTypeConst(_ context: SimplifyContext) {
170-
guard let ty = substitutionMap.replacementTypes[0] else {
171-
return
172-
}
173-
163+
let ty = substitutionMap.replacementType.loweredType(in: parentFunction, maximallyAbstracted: true)
174164
let value: Int?
175165
switch id {
176166
case .Sizeof:

0 commit comments

Comments
 (0)