Skip to content

Commit 9eddb24

Browse files
committed
EscapeUtils: replace ...byWalkingDown APIs with an initialWalkingDirection parameter
It simplifies the EscapeUtils API NFC
1 parent 7512acf commit 9eddb24

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeEscapeEffects.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ let computeEscapeEffects = FunctionPass(name: "compute-escape-effects") {
4747
}
4848

4949
// First check: is the argument (or a projected value of it) escaping at all?
50-
if !arg.at(.anything).isEscapingWhenWalkingDown(using: IgnoreRecursiveCallVisitor(),
51-
context) {
50+
if !arg.at(.anything).isEscaping(using: IgnoreRecursiveCallVisitor(),
51+
initialWalkingDirection: .down,
52+
context)
53+
{
5254
let effect = EscapeEffects.ArgumentEffect(.notEscaping, argumentIndex: arg.index,
5355
pathPattern: SmallProjectionPath(.anything))
5456
newEffects.append(effect)
@@ -84,7 +86,7 @@ func addArgEffects(_ arg: FunctionArgument, argPath ap: SmallProjectionPath,
8486
// containing one or more references.
8587
let argPath = arg.type.isClass ? ap : ap.push(.anyValueFields)
8688

87-
guard let result = arg.at(argPath).visitByWalkingDown(using: ArgEffectsVisitor(), context) else {
89+
guard let result = arg.at(argPath).visit(using: ArgEffectsVisitor(), initialWalkingDirection: .down, context) else {
8890
return false
8991
}
9092

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,6 @@ private extension PartialApplyInst {
544544
var followTrivialTypes: Bool { true }
545545
}
546546

547-
return self.isEscapingWhenWalkingDown(using: EscapesToApply(), context)
547+
return self.isEscaping(using: EscapesToApply(), initialWalkingDirection: .down, context)
548548
}
549549
}

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,19 @@ extension ProjectedValue {
7474
/// The provided `visitor` can be used to override the handling a certain defs and uses during
7575
/// the walk. See `EscapeVisitor` for details.
7676
///
77-
func isEscaping(using visitor: some EscapeVisitor = DefaultVisitor(),
78-
complexityBudget: Int = Int.max,
79-
_ context: some Context) -> Bool {
77+
func isEscaping(
78+
using visitor: some EscapeVisitor = DefaultVisitor(),
79+
initialWalkingDirection: EscapeUtilityTypes.WalkingDirection = .up,
80+
complexityBudget: Int = Int.max,
81+
_ context: some Context
82+
) -> Bool {
8083
var walker = EscapeWalker(visitor: visitor, complexityBudget: complexityBudget, context)
81-
return walker.walkUp(addressOrValue: value, path: path.escapePath) == .abortWalk
82-
}
83-
84-
/// Returns true if the function argument escapes, but ignoring any potential escapes in the caller.
85-
///
86-
/// This function is similar to `ProjectedValue.isEscaping()`, but it ignores any potential
87-
/// escapes which might have happened before the argument's function is called.
88-
/// Technically, this means that the walk starts downwards instead of upwards.
89-
///
90-
func isEscapingWhenWalkingDown(using visitor: some EscapeVisitor = DefaultVisitor(),
91-
_ context: some Context) -> Bool {
92-
var walker = EscapeWalker(visitor: visitor, context)
93-
return walker.walkDown(addressOrValue: value, path: path.escapePath) == .abortWalk
84+
let result: WalkResult
85+
switch initialWalkingDirection {
86+
case .up: result = walker.walkUp(addressOrValue: value, path: path.escapePath)
87+
case .down: result = walker.walkDown(addressOrValue: value, path: path.escapePath)
88+
}
89+
return result == .abortWalk
9490
}
9591

9692
/// Returns the result of the visitor if the projected value does not escape.
@@ -99,27 +95,19 @@ extension ProjectedValue {
9995
/// it returns the `result` of the `visitor`, if the projected value does not escape.
10096
/// Returns nil, if the projected value escapes.
10197
///
102-
func visit<V: EscapeVisitorWithResult>(using visitor: V,
103-
complexityBudget: Int = Int.max,
104-
_ context: some Context) -> V.Result? {
98+
func visit<V: EscapeVisitorWithResult>(
99+
using visitor: V,
100+
initialWalkingDirection: EscapeUtilityTypes.WalkingDirection = .up,
101+
complexityBudget: Int = Int.max,
102+
_ context: some Context
103+
) -> V.Result? {
105104
var walker = EscapeWalker(visitor: visitor, complexityBudget: complexityBudget, context)
106-
if walker.walkUp(addressOrValue: value, path: path.escapePath) == .abortWalk {
107-
walker.visitor.cleanupOnAbort()
108-
return nil
105+
let result: WalkResult
106+
switch initialWalkingDirection {
107+
case .up: result = walker.walkUp(addressOrValue: value, path: path.escapePath)
108+
case .down: result = walker.walkDown(addressOrValue: value, path: path.escapePath)
109109
}
110-
return walker.visitor.result
111-
}
112-
113-
/// Returns the result of the visitor if the projected value does not escape - ignoring
114-
/// any potential escapes in the caller.
115-
///
116-
/// This function is similar to `isEscapingIgnoringCallerEscapes() -> Bool`, but instead
117-
/// of returning a Bool, it returns the `result` of the `visitor`.
118-
///
119-
func visitByWalkingDown<V: EscapeVisitorWithResult>(using visitor: V,
120-
_ context: some Context) -> V.Result? {
121-
var walker = EscapeWalker(visitor: visitor, context)
122-
if walker.walkDown(addressOrValue: value, path: path.escapePath) == .abortWalk {
110+
if result == .abortWalk {
123111
walker.visitor.cleanupOnAbort()
124112
return nil
125113
}
@@ -129,19 +117,25 @@ extension ProjectedValue {
129117

130118
extension Value {
131119
/// The un-projected version of `ProjectedValue.isEscaping()`.
132-
func isEscaping(using visitor: some EscapeVisitor = DefaultVisitor(),
133-
_ context: some Context) -> Bool {
134-
return self.at(SmallProjectionPath()).isEscaping(using: visitor, context)
135-
}
136-
137-
func isEscapingWhenWalkingDown(using visitor: some EscapeVisitor = DefaultVisitor(),
138-
_ context: some Context) -> Bool {
139-
return self.at(SmallProjectionPath()).isEscapingWhenWalkingDown(using: visitor, context)
120+
func isEscaping(
121+
using visitor: some EscapeVisitor = DefaultVisitor(),
122+
initialWalkingDirection: EscapeUtilityTypes.WalkingDirection = .up,
123+
_ context: some Context
124+
) -> Bool {
125+
return self.at(SmallProjectionPath()).isEscaping(using: visitor,
126+
initialWalkingDirection: initialWalkingDirection,
127+
context)
140128
}
141129

142130
/// The un-projected version of `ProjectedValue.visit()`.
143-
func visit<V: EscapeVisitorWithResult>(using visitor: V, _ context: some Context) -> V.Result? {
144-
return self.at(SmallProjectionPath()).visit(using: visitor, context)
131+
func visit<V: EscapeVisitorWithResult>(
132+
using visitor: V,
133+
initialWalkingDirection: EscapeUtilityTypes.WalkingDirection = .up,
134+
_ context: some Context
135+
) -> V.Result? {
136+
return self.at(SmallProjectionPath()).visit(using: visitor,
137+
initialWalkingDirection: initialWalkingDirection,
138+
context)
145139
}
146140
}
147141

@@ -198,6 +192,11 @@ struct DefaultVisitor : EscapeVisitor {}
198192

199193
struct EscapeUtilityTypes {
200194

195+
enum WalkingDirection {
196+
case up
197+
case down
198+
}
199+
201200
/// The EscapePath is updated and maintained during the up-walk and down-walk.
202201
///
203202
/// It's passed to the EscapeVisitor's `visitUse` and `visitDef`.

0 commit comments

Comments
 (0)