File: GuaranteedPhiVerifierPrivate.h

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • 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 (117 lines) | stat: -rw-r--r-- 4,042 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
//===--- GuaranteedPhiVerifierPrivate.h -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_SIL_GUARANTEEDPHIVERIFIER_H
#define SWIFT_SIL_GUARANTEEDPHIVERIFIER_H

#include "LinearLifetimeCheckerPrivate.h"

#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"

namespace swift {

class DeadEndBlocks;

/// GuaranteedPhiVerifier validates the lifetime of the Reborrow or
/// GuaranteedForwardingPhi lies within the lifetime of its base value. It uses
/// LinearLifetimeChecker for this.
///
/// A GuaranteedForwardingPhi can have different base values on different
/// control flow paths. Example:
///
/// sil [ossa] @test_forwarded_separate_base_values :
/// bb0(%0 : @owned $Wrapper1):
///   %1 = begin_borrow %0 : $Wrapper1
///   cond_br undef, bb1, bb2
///
/// bb1:
///   %3 = begin_borrow %0 : $Wrapper1
///   %4 = struct_extract %1 : $Wrapper1, #Wrapper1.val1
///   br bb3(%4 : $Wrapper2, %3 : $Wrapper1)
///
/// bb2:
///   %6 = begin_borrow %0 : $Wrapper1
///   %7 = struct_extract %6 : $Wrapper1, #Wrapper1.val1
///   br bb3(%7 : $Wrapper2, %6 : $Wrapper1)
///
/// bb3(%9 : @guaranteed $Wrapper2, %10 : @guaranteed $Wrapper1):
///   end_borrow %10 : $Wrapper1
///   end_borrow %1 : $Wrapper1
///   destroy_value %0 : $Wrapper1
///   %16 = tuple ()
///   return %16 : $()
/// }
///
/// Here %10 is the base value of %9 on one path and %1 is the base value of %9
/// on another path.
/// Similarly, a Reborrow can have different base values on different
/// control flow paths. Example:
/// sil [ossa] @test_reborrow_different_base_values :
/// bb0(%0 : @owned $Wrapper):
///   cond_br undef, bb1, bb2
///
/// bb1:
///   %2 = copy_value %0 : $Wrapper
///   %3 = begin_borrow %0 : $Wrapper
///   br bb3(%3 : $Wrapper, %2 : $Wrapper)
///
/// bb2:
///   %5 = copy_value %0 : $Wrapper
///   %6 = begin_borrow %5 : $Wrapper
///   br bb3(%6 : $Wrapper, %5 : $Wrapper)
///
/// bb3(%8 : @guaranteed $Wrapper, %9 : @owned $Wrapper):
///   end_borrow %8 : $Wrapper
///   destroy_value %9 : $Wrapper
///   destroy_value %0 : $Wrapper
///   %13 = tuple ()
///   return %13 : $()
/// }
///
/// Here %9 is the base value of %8 on path and %0 is the base value on another
/// path.
///
class GuaranteedPhiVerifier {
  /// A cache of dead-end basic blocks that we use to determine if we can
  /// ignore "leaks".
  DeadEndBlocks *deadEndBlocks = nullptr;
  /// The builder that the checker uses to emit error messages, crash if asked
  /// for, or supply back interesting info to the caller.
  LinearLifetimeChecker::ErrorBuilder errorBuilder;
  /// A map of reborrow phi/guaranteed forwarding phi to its base values.
  /// Note that a reborrow phi arg can have different base values based on
  /// different control flow paths.
  llvm::DenseMap<SILPhiArgument *, SmallPtrSet<SILValue, 8>>
      dependentPhiToBaseValueMap;

public:
  GuaranteedPhiVerifier(const SILFunction *func, DeadEndBlocks *deadEndBlocks,
                        LinearLifetimeChecker::ErrorBuilder errorBuilder)
      : deadEndBlocks(deadEndBlocks), errorBuilder(errorBuilder) {}

  /// Verify whether all reborrows of \p borrow are within the lifetime of the
  /// borrowed value.
  void verifyReborrows(BeginBorrowInst *borrow);
  /// Verify whether the GuaranteedForwardingPhi uses of \p borrow are within
  /// its lifetime.
  void verifyGuaranteedForwardingPhis(BorrowedValue borrow);

private:
  /// Verifies whether the \p phi's lifetime lies within the \p baseValue
  bool verifyDependentPhiLifetime(SILPhiArgument *phi, SILValue baseValue);
};

} // namespace swift

#endif