Skip to content

Commit 57e08af

Browse files
committed
SIL: add ApplySite.calleeArgument(of operand: Operand, in callee: Function)
This is a safer API than using ``` let argIdx = applySite.calleeArgumentIndex(of: op) let arg = callee.arguments[argIdx] ``` because there is no potential misuse of the index.
1 parent f83fb1b commit 57e08af

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeEscapeEffects.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ private func isOperandOfRecursiveCall(_ op: Operand) -> Bool {
141141
if let applySite = inst as? FullApplySite,
142142
let callee = applySite.referencedFunction,
143143
callee == inst.parentFunction,
144-
let argIdx = applySite.calleeArgumentIndex(of: op),
145-
op.value == callee.arguments[argIdx] {
144+
let calleeArg = applySite.calleeArgument(of: op, in: callee),
145+
op.value == calleeArg {
146146
return true
147147
}
148148
return false

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyCopyBlock.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ private extension PartialApplyInst {
8989
guard isOnStack,
9090
let callee = referencedFunction,
9191
callee.isDefinition,
92-
let argIdx = calleeArgumentIndex(of: closure),
92+
let calleeArg = calleeArgument(of: closure, in: callee),
9393
// If the callee only _calls_ the closure argument, it does not escape.
94-
callee.arguments[argIdx].uses.allSatisfy(isCalleeOperandOfApply)
94+
calleeArg.uses.allSatisfy(isCalleeOperandOfApply)
9595
else {
9696
return true
9797
}

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,24 @@ extension ApplySite {
277277
return argumentOperands[callerArgIdx]
278278
}
279279

280-
/// Returns the argument index of an operand.
280+
/// Returns the argument of `operand` in a callee function.
281281
///
282-
/// Returns nil if 'operand' is not an argument operand. This is the case if
282+
/// Returns nil if `operand` is not an argument operand. This is the case if
283283
/// it's the callee function operand.
284+
public func calleeArgument(of operand: Operand, in callee: Function) -> FunctionArgument? {
285+
if let argIdx = calleeArgumentIndex(of: operand) {
286+
return callee.arguments[argIdx]
287+
}
288+
return nil
289+
}
290+
291+
/// Returns the argument index of an operand.
284292
///
285-
/// Warning: the returned integer can be misused as an index into
286-
/// the wrong collection. Replace uses of this API with safer APIs.
293+
/// Returns nil if `operand` is not an argument operand. This is the case if
294+
/// it's the callee function operand.
287295
///
288-
/// TODO: delete this API and rewrite the users.
296+
/// Warning: the returned integer can be misused as an index into the wrong collection.
297+
/// Use `calleeArgument(of:,in:)` if possible.
289298
public func calleeArgumentIndex(of operand: Operand) -> Int? {
290299
operandConventions.calleeArgumentIndex(of: operand)
291300
}

0 commit comments

Comments
 (0)