File: AddressUtils.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (561 lines) | stat: -rw-r--r-- 21,601 bytes parent folder | download
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//===--- AddressUtils.swift - Utilities for handling SIL addresses -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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 SIL

private let verbose = false

private func log(_ message: @autoclosure () -> String) {
  if verbose {
    print("### \(message())")
  }
}

/// Classify address uses. This can be used by def-use walkers to
/// ensure complete handling of all legal SIL patterns.
///
/// TODO: Integrate this with SIL verification to ensure completeness.
///
/// TODO: Convert AddressDefUseWalker to conform to AddressUtils after
/// checking that the additional instructions are handled correctly by
/// escape analysis.
///
/// TODO: Verify that pointerEscape is only called for live ranges in which
/// `findPointerEscape()` returns true.
protocol AddressUseVisitor {
  var context: Context { get }

  /// An address projection produces a single address result and does not
  /// escape its address operand in any other way.
  mutating func projectedAddressUse(of operand: Operand, into value: Value)
    -> WalkResult

  /// An access scope: begin_access, begin_apply, load_borrow.
  mutating func scopedAddressUse(of operand: Operand) -> WalkResult

  /// end_access, end_apply, abort_apply, end_borrow.
  mutating func scopeEndingAddressUse(of operand: Operand) -> WalkResult

  /// An address leaf use propagates neither the address bits, nor the
  /// in-memory value beyond the instruction.
  ///
  /// StoringInstructions are leaf uses.
  mutating func leafAddressUse(of operand: Operand) -> WalkResult

  /// An address used by an apply.
  mutating func appliedAddressUse(of operand: Operand, by apply: FullApplySite)
    -> WalkResult

  /// A loaded address use propagates the value at the address.
  mutating func loadedAddressUse(of operand: Operand, into value: Value)
    -> WalkResult

  /// A loaded address use propagates the value at the address to the
  /// destination address operand.
  mutating func loadedAddressUse(of operand: Operand, into address: Operand)
    -> WalkResult

  /// A non-address owned `value` whose ownership depends on the in-memory
  /// value at `address`, such as `mark_dependence %value on %address`.
  mutating func dependentAddressUse(of operand: Operand, into value: Value)
    -> WalkResult

  /// A pointer escape may propagate the address beyond the current instruction.
  mutating func escapingAddressUse(of operand: Operand) -> WalkResult

  /// A unknown address use. This should never be called in valid SIL.
  mutating func unknownAddressUse(of operand: Operand) -> WalkResult
}

extension AddressUseVisitor {
  /// Classify an address-type operand, dispatching to one of the
  /// protocol methods above.
  mutating func classifyAddress(operand: Operand) -> WalkResult {
    switch operand.instruction {
    case is BeginAccessInst, is LoadBorrowInst, is StoreBorrowInst:
      return scopedAddressUse(of: operand)

    case is EndAccessInst, is EndApplyInst, is AbortApplyInst, is EndBorrowInst:
      return scopeEndingAddressUse(of: operand)

    case let markDep as MarkDependenceInst:
      if markDep.valueOperand == operand {
        return projectedAddressUse(of: operand, into: markDep)
      }
      assert(markDep.baseOperand == operand)
      // If another address depends on the current address,
      // handle it like a projection.
      if markDep.type.isAddress {
        return projectedAddressUse(of: operand, into: markDep)
      }
      switch markDep.dependenceKind {
      case .Unresolved:
        if LifetimeDependence(markDep, context) == nil {
          break
        }
        fallthrough
      case .NonEscaping:
        // Note: This is unreachable from InteriorUseVisitor because the base address of a `mark_dependence
        // [nonescaping]` must be a `begin_access`, and interior liveness does not check uses of the accessed address.
        return dependentAddressUse(of: operand, into: markDep)
      case .Escaping:
        break
      }
      // A potentially escaping value depends on this address.
      return escapingAddressUse(of: operand)

    case let pai as PartialApplyInst where !pai.mayEscape:
      return dependentAddressUse(of: operand, into: pai)

    case let pai as PartialApplyInst where pai.mayEscape:
      return escapingAddressUse(of: operand)

    case is ReturnInst, is ThrowInst, is YieldInst, is AddressToPointerInst:
      return escapingAddressUse(of: operand)

    case is StructElementAddrInst, is TupleElementAddrInst,
         is IndexAddrInst, is TailAddrInst, is TuplePackElementAddrInst, 
         is InitEnumDataAddrInst, is UncheckedTakeEnumDataAddrInst,
         is InitExistentialAddrInst, is OpenExistentialAddrInst,
         is ProjectBlockStorageInst, is UncheckedAddrCastInst,
         is MarkUninitializedInst, is DropDeinitInst,
         is CopyableToMoveOnlyWrapperAddrInst,
         is MoveOnlyWrapperToCopyableAddrInst,
         is MarkUnresolvedNonCopyableValueInst:
      let svi = operand.instruction as! SingleValueInstruction
      return projectedAddressUse(of: operand, into: svi)

    case let apply as FullApplySite:
      return appliedAddressUse(of: operand, by: apply)

    case is SwitchEnumAddrInst, is CheckedCastAddrBranchInst,
         is SelectEnumAddrInst, is InjectEnumAddrInst,
         is StoreInst, is StoreUnownedInst, is StoreWeakInst,
         is AssignInst, is AssignByWrapperInst, is AssignOrInitInst,
         is TupleAddrConstructorInst, is InitBlockStorageHeaderInst,
         is RetainValueAddrInst, is ReleaseValueAddrInst,
         is DestroyAddrInst, is DeallocStackInst, 
         is DeinitExistentialAddrInst,
         is IsUniqueInst, is MarkFunctionEscapeInst,
         is PackElementSetInst:
      return leafAddressUse(of: operand)

    case is LoadInst, is LoadUnownedInst,  is LoadWeakInst, 
         is ValueMetatypeInst, is ExistentialMetatypeInst,
         is PackElementGetInst:
      let svi = operand.instruction as! SingleValueInstruction
      return loadedAddressUse(of: operand, into: svi)

    case let sdai as SourceDestAddrInstruction
           where sdai.sourceOperand == operand:
      return loadedAddressUse(of: operand, into: sdai.destinationOperand)

    case let sdai as SourceDestAddrInstruction
           where sdai.destinationOperand == operand:
      return leafAddressUse(of: operand)

    case let builtin as BuiltinInst:
      switch builtin.id {
      case .Copy where builtin.operands[1] == operand: // source
        return loadedAddressUse(of: operand, into: builtin.operands[0])

      case .Copy where builtin.operands[0] == operand: // dest
        return leafAddressUse(of: operand)

      // Builtins that cannot load a nontrivial value.
      case .TSanInoutAccess, .ResumeThrowingContinuationReturning,
           .ResumeNonThrowingContinuationReturning, .GenericAdd,
           .GenericFAdd, .GenericAnd, .GenericAShr, .GenericLShr, .GenericOr,
           .GenericFDiv, .GenericMul, .GenericFMul, .GenericSDiv,
           .GenericExactSDiv, .GenericShl, .GenericSRem, .GenericSub,
           .GenericFSub, .GenericUDiv, .GenericExactUDiv, .GenericURem,
           .GenericFRem, .GenericXor, .TaskRunInline, .ZeroInitializer,
           .GetEnumTag, .InjectEnumTag:
        return leafAddressUse(of: operand)
      default:
        // TODO: SIL verification should check that this exhaustively
        // recognizes all builtin address uses.
        return .abortWalk
      }

    case is BranchInst, is CondBranchInst:
      fatalError("address phi is not allowed")

    default:
      if operand.instruction.isIncidentalUse {
        return leafAddressUse(of: operand)
      }
      // Unkown instruction.
      return unknownAddressUse(of: operand)
    }
  }
}

extension AccessBase {
  /// If this access base has a single initializer, return it, along
  /// with the initialized address. This does not guarantee that all
  /// uses of that address are dominated by the store or even that the
  /// store is a direct use of `address`.
  func findSingleInitializer(_ context: some Context) -> (initialAddress: Value, initializingStore: Instruction)? {
    let baseAddr: Value
    switch self {
    case let .stack(allocStack):
      baseAddr = allocStack
    case let .argument(arg):
      guard arg.convention.isIndirectOut else {
        return nil
      }
      baseAddr = arg
    default:
      return nil
    }
    return AddressInitializationWalker.findSingleInitializer(ofAddress: baseAddr, context: context)
  }
}

// Walk the address def-use paths to find a single initialization.
//
// Implements AddressUseVisitor to guarantee that we can't miss any
// stores. This separates escapingAddressUse from leafAddressUse.
//
// Main entry point:
//  static func findSingleInitializer(ofAddress: Value, context: some Context)
//
// TODO: Make AddressDefUseWalker always conform to AddressUseVisitor once we're
// ready to debug changes to escape analysis etc...
//
// Future:
// AddressUseVisitor
//    (how to transitively follow uses, complete classification)
// -> AddressPathDefUseWalker
//    (follow projections and track Path,
//     client handles all other uses, such as access scopes)
// -> AddressProjectionDefUseWalker
//    (follow projections, track Path, ignore access scopes,
//     merge all other callbacks into only two:
//     instantaneousAddressUse vs. escapingAddressUse)
//
// FIXME: This currently assumes that isAddressInitialization catches
// writes to the memory address. We need a complete abstraction that
// distinguishes between `mayWriteToMemory` for dependence vs. actual
// modification of memory.
struct AddressInitializationWalker: AddressDefUseWalker, AddressUseVisitor {
  let context: any Context

  var walkDownCache = WalkerCache<SmallProjectionPath>()

  var isProjected = false
  var initializingStore: Instruction?

  static func findSingleInitializer(ofAddress baseAddr: Value, context: some Context)
    -> (initialAddress: Value, initializingStore: Instruction)? {

    var walker = AddressInitializationWalker(context: context)
    if walker.walkDownUses(ofAddress: baseAddr, path: SmallProjectionPath()) == .abortWalk {
      return nil
    }
    guard let initializingStore = walker.initializingStore else {
      return nil
    }
    return (initialAddress: baseAddr, initializingStore: initializingStore)
  }

  private mutating func setInitializer(instruction: Instruction) -> WalkResult {
    // An initializer must be unique and store the full value.
    if initializingStore != nil || isProjected {
      initializingStore = nil
      return .abortWalk
    }
    initializingStore = instruction
    return .continueWalk
  }
}

// Implement AddressDefUseWalker
extension AddressInitializationWalker {
  mutating func leafUse(address: Operand, path: SmallProjectionPath)
    -> WalkResult {
    isProjected = !path.isEmpty
    return classifyAddress(operand: address)
  }
}

// Implement AddresUseVisitor
extension AddressInitializationWalker {
  /// An address projection produces a single address result and does not
  /// escape its address operand in any other way.
  mutating func projectedAddressUse(of operand: Operand, into value: Value)
    -> WalkResult {
    // AddressDefUseWalker should catch most of these.
    return .abortWalk
  }

  mutating func scopedAddressUse(of operand: Operand) -> WalkResult {
    // AddressDefUseWalker currently skips most of these.
    return .abortWalk
  }

  mutating func scopeEndingAddressUse(of operand: Operand) -> WalkResult {
    // AddressDefUseWalker currently skips most of these.
    return .continueWalk
  }

  mutating func leafAddressUse(of operand: Operand) -> WalkResult {
    if operand.isAddressInitialization {
      return setInitializer(instruction: operand.instruction)
    }
    // FIXME: check mayWriteToMemory but ignore non-stores. Currently,
    // stores should all be checked my isAddressInitialization, but
    // this is not robust.
    return .continueWalk
  }

  mutating func appliedAddressUse(of operand: Operand, by apply: FullApplySite)
    -> WalkResult {
    if operand.isAddressInitialization {
      return setInitializer(instruction: operand.instruction)
    }
    guard let convention = apply.convention(of: operand) else {
      return .continueWalk
    }
    return convention.isIndirectIn ? .continueWalk : .abortWalk
  }

  mutating func loadedAddressUse(of operand: Operand, into value: Value)
    -> WalkResult {
    return .continueWalk
  }

  mutating func loadedAddressUse(of operand: Operand, into address: Operand)
    -> WalkResult {
    return .continueWalk
  }

  mutating func dependentAddressUse(of operand: Operand, into value: Value)
    -> WalkResult {
    return .continueWalk
  }

  mutating func escapingAddressUse(of operand: Operand) -> WalkResult {
    return .abortWalk
  }

  mutating func unknownAddressUse(of operand: Operand) -> WalkResult {
    return .abortWalk
  }
}

/// A live range representing the ownership of addressible memory.
///
/// This live range represents the minimal guaranteed lifetime of the object being addressed. Uses of derived addresses
/// may be extended up to the ends of this scope without violating ownership.
///
/// .liveOut objects (@in_guaranteed, @out) and .global variables have no instruction range.
///
/// .local objects (alloc_stack, yield, @in, @inout) report the single live range of the full assignment that reaches
/// this address.
///
/// .owned values (boxes and references) simply report OSSA liveness.
///
/// .borrow values report each borrow scope's range. The effective live range is their intersection. A valid use must
/// lie within all ranges.
///
/// FIXME: .borrow values should be represented with a single multiply-defined instruction range. Otherwise we will run
/// out of blockset bits as soon as we have multiple ranges (each range uses three sets). It is ok to take the
/// union of the borrow ranges since all address uses that may be extended will be already be dominated by the current
/// address. Alternatively, we can have a utility that folds two InstructionRanges together as an intersection, and
/// repeatedly fold the range of each borrow introducer.
///
/// Example:
///
///     %x = alloc_box
///     %b = begin_borrow %x -+ begin ownership range
///     %p = project_box %b   | <--- accessBase
///     %a = begin_access %p  |
///     end_access %a         | <--- address use
///     end_borrow %b        -+ end ownership range
///     destroy_value %x
///
/// This may return multiple ranges if a borrowed reference has multiple introducers:
///
///     %b1 = begin_borrow           -+        range1
///     %b2 = begin_borrow            |  -+ -+ range2
///     %s  = struct (%b1, %2)        |   |  |
///     %e  = struct_extract %s, #s.0 |   |  | intersection
///     %d = ref_element_addr %e      |   |  | where ownership
///     %a = begin_access %d          |   |  | is valid
///     end_access %a                 |   |  |
///     end_borrow %b1               -+   | -+
///     ...                               |
///     end_borrow %b2                   -+
///
/// Note: The resulting live range must be deinitialized in stack order.
enum AddressOwnershipLiveRange : CustomStringConvertible {
  case liveOut(FunctionArgument)
  case global(GlobalVariable)
  case local(Value, InstructionRange) // Value represents the local allocation
  case owned(Value, InstructionRange)
  case borrow(SingleInlineArray<(BeginBorrowValue, InstructionRange)>)

  mutating func deinitialize() {
    switch self {
    case .liveOut, .global:
      break
    case var .local(_, range):
      range.deinitialize()
    case var .owned(_, range):
      range.deinitialize()
    case var .borrow(ranges):
      for idx in ranges.indices {
        ranges[idx].1.deinitialize()
      }
    }
  }

  /// Return nil if the live range is unknown.
  static func compute(for address: Value, at begin: Instruction,
                      _ localReachabilityCache: LocalVariableReachabilityCache,
                      _ context: FunctionPassContext) -> AddressOwnershipLiveRange? {
    let accessBase = address.accessBase
    switch accessBase {
    case .box, .class, .tail:
      return computeValueLiveRange(of: accessBase.reference!, context)
    case let .stack(allocStack):
      return computeLocalLiveRange(allocation: allocStack, begin: begin, localReachabilityCache, context)
    case let .global(global):
      return .global(global)
    case let .argument(arg):
      switch arg.convention {
      case .indirectInGuaranteed, .indirectOut:
        return .liveOut(arg)
      case .indirectIn, .indirectInout, .indirectInoutAliasable:
        return computeLocalLiveRange(allocation: arg, begin: begin, localReachabilityCache, context)
      default:
        return nil
      }
    case let .yield(result):
      let apply = result.parentInstruction as! BeginApplyInst
      switch apply.convention(of: result) {
      case .indirectInGuaranteed:
        var range = InstructionRange(for: address, context)
        _ = BorrowingInstruction(apply)!.visitScopeEndingOperands(context) {
          range.insert($0.instruction)
          return .continueWalk
        }
        return .local(result, range)
      case .indirectIn, .indirectInout, .indirectInoutAliasable:
        return computeLocalLiveRange(allocation: result, begin: begin, localReachabilityCache, context)
      default:
        return nil
      }
    case .storeBorrow(let sb):
      return computeValueLiveRange(of: sb.source, context)
    case .pointer, .unidentified:
      return nil
    }
  }

  /// Does this inclusive range include `inst`, assuming that `inst` occurs after a definition of the live range.
  func coversUse(_ inst: Instruction) -> Bool {
    switch self {
    case .liveOut, .global:
      return true
    case let .local(_, range), let .owned(_, range):
      return range.inclusiveRangeContains(inst)
    case let .borrow(borrowRanges):
      for (_, range) in borrowRanges {
        if !range.inclusiveRangeContains(inst) {
          return false
        }
      }
      return true
    }
  }

  var description: String {
    switch self {
    case let .liveOut(arg):
      return "liveOut: \(arg)"
    case let .global(global):
      return "global: \(global)"
    case let .local(allocation, range):
      return "local: \(allocation)\n\(range)"
    case let .owned(value, range):
      return "owned: \(value)\n\(range)"
    case let .borrow(borrowRanges):
      var str = ""
      for (borrow, range) in borrowRanges {
         str += "borrow: \(borrow)\n\(range)"
      }
      return str
    }
  }
}

extension AddressOwnershipLiveRange {
  /// Compute the ownership live range of any non-address value.
  ///
  /// For an owned value, simply compute its liveness. For a guaranteed value, return a separate range for each borrow
  /// introducer.
  ///
  /// For address values, use AccessBase.computeOwnershipRange.
  ///
  /// FIXME: This should use computeLinearLiveness rather than computeKnownLiveness as soon as lifetime completion
  /// runs immediately after SILGen.
  private static func computeValueLiveRange(of value: Value, _ context: FunctionPassContext)
    -> AddressOwnershipLiveRange? {
    switch value.ownership {
    case .none, .unowned:
      // This is unexpected for a value with derived addresses.
      return nil
    case .owned:
      return .owned(value, computeKnownLiveness(for: value, context))
    case .guaranteed:
      return .borrow(computeBorrowLiveRange(for: value, context))
    }
  }

  private static func computeLocalLiveRange(allocation: Value, begin: Instruction,
                                            _ localReachabilityCache: LocalVariableReachabilityCache,
                                            _ context: some Context) -> AddressOwnershipLiveRange? {
    guard let localReachability = localReachabilityCache.reachability(for: allocation, context) else {
      return nil
    }
    var reachingAssignments = Stack<LocalVariableAccess>(context)
    defer { reachingAssignments.deinitialize() }

    if !localReachability.gatherReachingAssignments(for: begin, in: &reachingAssignments) {
      return nil
    }
    // Any one of the reaching assignment is sufficient to compute the minimal live range. The caller presumably only
    // cares about the live range that is dominated by 'begin'. Since all assignments gathered above reach
    // 'begin', their live ranges must all be identical on all paths through 'addressInst'.
    let assignment = reachingAssignments.first!

    var reachableUses = Stack<LocalVariableAccess>(context)
    defer { reachableUses.deinitialize() }

    localReachability.gatherKnownReachableUses(from: assignment, in: &reachableUses)

    let assignmentInst = assignment.instruction ?? allocation.parentFunction.entryBlock.instructions.first!
    var range = InstructionRange(begin: assignmentInst, context)
    for localAccess in reachableUses {
      if localAccess.kind == .escape {
        log("Local variable: \(allocation)\n    escapes at \(localAccess.instruction!)")
      }
      for end in localAccess.instruction!.endInstructions {
        range.insert(end)
      }
    }
    return .local(allocation, range)
  }
}