-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathStackPromotion.swift
346 lines (309 loc) · 13.6 KB
/
StackPromotion.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//===--- StackPromotion.swift - Stack promotion optimization --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import AST
import SIL
/// Promotes heap allocated objects to the stack.
///
/// It handles `alloc_ref` and `alloc_ref_dynamic` instructions of native swift
/// classes: if promoted, the `[stack]` attribute is set in the allocation
/// instruction and a `dealloc_stack_ref` is inserted at the end of the object's
/// lifetime.
/// The main criteria for stack promotion is that the allocated object must not
/// escape its function.
///
/// Example:
/// %k = alloc_ref $Klass
/// // .. some uses of %k
/// destroy_value %k // The end of %k's lifetime
///
/// is transformed to:
///
/// %k = alloc_ref [stack] $Klass
/// // .. some uses of %k
/// destroy_value %k
/// dealloc_stack_ref %k
///
/// The destroy/release of the promoted object remains in the SIL, but is effectively
/// a no-op, because a stack promoted object is initialized with an "immortal"
/// reference count.
/// Later optimizations can clean that up.
let stackPromotion = FunctionPass(name: "stack-promotion") {
(function: Function, context: FunctionPassContext) in
let deadEndBlocks = context.deadEndBlocks
var needFixStackNesting = false
for inst in function.instructions {
if let allocRef = inst as? AllocRefInstBase {
if !context.continueWithNextSubpassRun(for: allocRef) {
break
}
if tryPromoteAlloc(allocRef, deadEndBlocks, context) {
needFixStackNesting = true
}
}
}
if needFixStackNesting {
// Make sure that all stack allocating instructions are nested correctly.
function.fixStackNesting(context)
}
}
// Returns true if the allocation is promoted.
private func tryPromoteAlloc(_ allocRef: AllocRefInstBase,
_ deadEndBlocks: DeadEndBlocksAnalysis,
_ context: FunctionPassContext) -> Bool {
if allocRef.isObjC || allocRef.canAllocOnStack {
return false
}
// Usually resilient classes cannot be promoted anyway, because their initializers are
// not visible and let the object appear to escape.
if allocRef.type.nominal!.isResilient(in: allocRef.parentFunction) {
return false
}
if let dtor = (allocRef.type.nominal as? ClassDecl)?.destructor {
if dtor.isIsolated {
// Classes (including actors) with isolated deinit can escape implicitly.
//
// We could optimize this further and allow promotion if we can prove that
// deinit will take fast path (i.e. it will not schedule a job).
// But for now, let's keep things simple and disable promotion conservatively.
return false
}
}
// The most important check: does the object escape the current function?
if allocRef.isEscaping(context) {
return false
}
if deadEndBlocks.isDeadEnd(allocRef.parentBlock) {
// Allocations inside a code region which ends up in a no-return block may missing their
// final release. Therefore we extend their lifetime indefinitely, e.g.
//
// %k = alloc_ref $Klass
// ...
// unreachable // The end of %k's lifetime
//
// There is one exception: if it's in a loop (within the dead-end region) we must not
// extend its lifetime. In this case we can be sure that its final release is not
// missing, because otherwise the object would be leaking. For example:
//
// bb1:
// %k = alloc_ref $Klass
// ... // %k's lifetime must end somewhere here
// cond_br %c, bb1, bb2
// bb2:
// unreachable
//
// Therefore, if the allocation is inside a loop, we can treat it like allocations in
// non dead-end regions.
if !isInLoop(block: allocRef.parentBlock, context) {
allocRef.setIsStackAllocatable(context)
return true
}
}
// Try to find the top most dominator block which dominates all use points.
// * This block can be located "earlier" than the actual allocation block, in case the
// promoted object is stored into an "outer" object, e.g.
//
// bb0: // outerDominatingBlock _
// %o = alloc_ref $Outer |
// ... |
// bb1: // allocation block _ |
// %k = alloc_ref $Klass | | "outer"
// %f = ref_element_addr %o, #Outer.f | "inner" | liverange
// store %k to %f | liverange |
// ... | |
// destroy_value %o _| _|
//
// * Finding the `outerDominatingBlock` is not guaranteed to work.
// In this example, the top most dominator block is `bb0`, but `bb0` has no
// use points in the outer liverange. We'll get `bb3` as outerDominatingBlock.
// This is no problem because 1. it's an unusual case and 2. the `outerBlockRange`
// is invalid in this case and we'll bail later.
//
// bb0: // real top most dominating block
// cond_br %c, bb1, bb2
// bb1:
// %o1 = alloc_ref $Outer
// br bb3(%o1)
// bb2:
// %o2 = alloc_ref $Outer
// br bb3(%o1)
// bb3(%o): // resulting outerDominatingBlock: wrong!
// %k = alloc_ref $Klass
// %f = ref_element_addr %o, #Outer.f
// store %k to %f
// destroy_value %o
//
let domTree = context.dominatorTree
let outerDominatingBlock = getDominatingBlockOfAllUsePoints(context: context, allocRef, domTree: domTree)
// The "inner" liverange contains all use points which are dominated by the allocation block.
// Note that this `visit` cannot fail because otherwise our initial `isEscaping` check would have failed already.
var innerRange = allocRef.visit(using: ComputeInnerLiverange(of: allocRef, domTree, context), context)!
defer { innerRange.deinitialize() }
// The "outer" liverange contains all use points.
// Same here: this `visit` cannot fail.
var outerBlockRange = allocRef.visit(using: ComputeOuterBlockrange(dominatedBy: outerDominatingBlock, context), context)!
defer { outerBlockRange.deinitialize() }
assert(innerRange.blockRange.isValid, "inner range should be valid because we did a dominance check")
if !outerBlockRange.isValid {
// This happens if we fail to find a correct outerDominatingBlock.
return false
}
// Check if there is a control flow edge from the inner to the outer liverange, which
// would mean that the promoted object can escape to the outer liverange.
// This can e.g. be the case if the inner liverange does not post dominate the outer range:
// _
// %o = alloc_ref $Outer |
// cond_br %c, bb1, bb2 |
// bb1: _ |
// %k = alloc_ref $Klass | | outer
// %f = ref_element_addr %o, #Outer.f | inner | range
// store %k to %f | range |
// br bb2 // branch from inner to outer _| |
// bb2: |
// destroy_value %o _|
//
// Or if there is a loop with a back-edge from the inner to the outer range:
// _
// %o = alloc_ref $Outer |
// br bb1 |
// bb1: _ |
// %k = alloc_ref $Klass | | outer
// %f = ref_element_addr %o, #Outer.f | inner | range
// store %k to %f | range |
// cond_br %c, bb1, bb2 // inner -> outer _| |
// bb2: |
// destroy_value %o _|
//
if innerRange.blockRange.hasControlFlowEdge(to: outerBlockRange) {
return false
}
// There shouldn't be any critical exit edges from the liverange, because that would mean
// that the promoted allocation is leaking.
// Just to be on the safe side, do a check and bail if we find critical exit edges: we
// cannot insert instructions on critical edges.
if innerRange.blockRange.containsCriticalExitEdges(deadEndBlocks: deadEndBlocks) {
return false
}
// Do the transformation!
// Insert `dealloc_stack_ref` instructions at the exit- and end-points of the inner liverange.
for exitInst in innerRange.exits {
if !deadEndBlocks.isDeadEnd(exitInst.parentBlock) {
let builder = Builder(before: exitInst, context)
builder.createDeallocStackRef(allocRef)
}
}
for endInst in innerRange.ends {
Builder.insert(after: endInst, location: allocRef.location, context) {
(builder) in builder.createDeallocStackRef(allocRef)
}
}
allocRef.setIsStackAllocatable(context)
return true
}
private func getDominatingBlockOfAllUsePoints(context: FunctionPassContext,
_ value: SingleValueInstruction,
domTree: DominatorTree) -> BasicBlock {
struct FindDominatingBlock : EscapeVisitorWithResult {
var result: BasicBlock
let domTree: DominatorTree
mutating func visitUse(operand: Operand, path: EscapePath) -> UseResult {
let defBlock = operand.value.parentBlock
if defBlock.dominates(result, domTree) {
result = defBlock
}
return .continueWalk
}
}
return value.visit(using: FindDominatingBlock(result: value.parentBlock, domTree: domTree), context)!
}
private struct ComputeInnerLiverange : EscapeVisitorWithResult {
var result: InstructionRange
let domTree: DominatorTree
init(of instruction: Instruction, _ domTree: DominatorTree, _ context: FunctionPassContext) {
result = InstructionRange(begin: instruction, context)
self.domTree = domTree
}
mutating func visitUse(operand: Operand, path: EscapePath) -> UseResult {
let user = operand.instruction
let beginBlockOfRange = result.blockRange.begin
if beginBlockOfRange.dominates(user.parentBlock, domTree) {
result.insert(user)
}
return .continueWalk
}
}
private struct ComputeOuterBlockrange : EscapeVisitorWithResult {
var result: BasicBlockRange
init(dominatedBy: BasicBlock, _ context: FunctionPassContext) {
result = BasicBlockRange(begin: dominatedBy, context)
}
mutating func visitUse(operand: Operand, path: EscapePath) -> UseResult {
let user = operand.instruction
result.insert(user.parentBlock)
let value = operand.value
let operandsDefinitionBlock = value.parentBlock
// Also insert the operand's definition. Otherwise we would miss allocation
// instructions (for which the `visitUse` closure is not called).
result.insert(operandsDefinitionBlock)
// We need to explicitly add predecessor blocks of phis because they
// are not necesesarily visited during the down-walk in `isEscaping()`.
// This is important for the special case where there is a back-edge from the
// inner range to the inner rage's begin-block:
//
// bb0: // <- need to be in the outer range
// br bb1(%some_init_val)
// bb1(%arg):
// %k = alloc_ref $Klass // innerInstRange.begin
// cond_br bb2, bb1(%k) // back-edge to bb1 == innerInstRange.blockRange.begin
//
if let phi = Phi(value) {
result.insert(contentsOf: phi.predecessors)
}
return .continueWalk
}
}
private extension BasicBlockRange {
/// Returns true if there is a direct edge connecting this range with the `otherRange`.
func hasControlFlowEdge(to otherRange: BasicBlockRange) -> Bool {
func isOnlyInOtherRange(_ block: BasicBlock) -> Bool {
return !inclusiveRangeContains(block) && otherRange.inclusiveRangeContains(block)
}
for lifeBlock in inclusiveRange {
assert(otherRange.inclusiveRangeContains(lifeBlock), "range must be a subset of other range")
for succ in lifeBlock.successors {
if isOnlyInOtherRange(succ) && succ != otherRange.begin {
return true
}
// The entry of the begin-block is conceptually not part of the range. We can check if
// it's part of the `otherRange` by checking the begin-block's predecessors.
if succ == begin && begin.predecessors.contains(where: { isOnlyInOtherRange($0) }) {
return true
}
}
}
return false
}
func containsCriticalExitEdges(deadEndBlocks: DeadEndBlocksAnalysis) -> Bool {
exits.contains { !deadEndBlocks.isDeadEnd($0) && !$0.hasSinglePredecessor }
}
}
private func isInLoop(block startBlock: BasicBlock, _ context: FunctionPassContext) -> Bool {
var worklist = BasicBlockWorklist(context)
defer { worklist.deinitialize() }
worklist.pushIfNotVisited(contentsOf: startBlock.successors)
while let block = worklist.pop() {
if block == startBlock {
return true
}
worklist.pushIfNotVisited(contentsOf: block.successors)
}
return false
}