Skip to content

Commit c4c66b4

Browse files
committed
[SwiftCompilerSources] add parameter dependencies to conventions.
Add interfaces to FunctionConventions and ArgumentConventions for querying lifetime dependencies where the target is another parameter.
1 parent 103c59b commit c4c66b4

File tree

3 files changed

+64
-39
lines changed

3 files changed

+64
-39
lines changed

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public struct ApplyOperandConventions : Collection {
7171
calleeArgumentIndex(ofOperandIndex: operandIndex)!]
7272
}
7373

74+
public subscript(parameterDependencies operandIndex: Int)
75+
-> FunctionConvention.LifetimeDependencies? {
76+
return calleeArgumentConventions[parameterDependencies:
77+
calleeArgumentIndex(ofOperandIndex: operandIndex)!]
78+
}
79+
7480
public var firstParameterOperandIndex: Int {
7581
return ApplyOperandConventions.firstArgumentIndex +
7682
calleeArgumentConventions.firstParameterIndex
@@ -222,6 +228,16 @@ extension ApplySite {
222228
functionConvention.resultDependencies != nil
223229
}
224230

231+
public var hasLifetimeDependence: Bool {
232+
functionConvention.hasLifetimeDependencies()
233+
}
234+
235+
public func parameterDependencies(target operand: Operand) -> FunctionConvention.LifetimeDependencies? {
236+
let idx = operand.index
237+
return idx < operandConventions.startIndex ? nil
238+
: operandConventions[parameterDependencies: idx]
239+
}
240+
225241
public var yieldConventions: YieldConventions {
226242
YieldConventions(convention: functionConvention)
227243
}

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,22 @@ public struct ArgumentConventions : Collection, CustomStringConvertible {
248248
return convention.parameters[paramIdx]
249249
}
250250

251-
/// Return a dependence of the function results on the indexed parameter.
252-
public subscript(resultDependsOn argumentIndex: Int)
253-
-> LifetimeDependenceConvention? {
254-
guard let paramIdx = parameterIndex(for: argumentIndex) else {
251+
public subscript(parameterDependencies targetArgumentIndex: Int) -> FunctionConvention.LifetimeDependencies? {
252+
guard let targetParamIdx = parameterIndex(for: targetArgumentIndex) else {
255253
return nil
256254
}
257-
return convention.resultDependencies?[paramIdx]
255+
return convention.parameterDependencies(for: targetParamIdx)
256+
}
257+
258+
/// Return a dependence of the function results on the indexed parameter.
259+
public subscript(resultDependsOn argumentIndex: Int) -> LifetimeDependenceConvention? {
260+
findDependence(source: argumentIndex, in: convention.resultDependencies)
261+
}
262+
263+
/// Return a dependence of the target argument on the source argument.
264+
public func getDependence(target targetArgumentIndex: Int, source sourceArgumentIndex: Int)
265+
-> LifetimeDependenceConvention? {
266+
findDependence(source: sourceArgumentIndex, in: self[parameterDependencies: targetArgumentIndex])
258267
}
259268

260269
/// Number of SIL arguments for the function type's results
@@ -295,6 +304,14 @@ extension ArgumentConventions {
295304
let firstParamIdx = firstParameterIndex // bridging call
296305
return argIdx < firstParamIdx ? nil : argIdx - firstParamIdx
297306
}
307+
308+
private func findDependence(source argumentIndex: Int, in dependencies: FunctionConvention.LifetimeDependencies?)
309+
-> LifetimeDependenceConvention? {
310+
guard let paramIdx = parameterIndex(for: argumentIndex) else {
311+
return nil
312+
}
313+
return dependencies?[paramIdx]
314+
}
298315
}
299316

300317
public struct YieldConventions : Collection, CustomStringConvertible {

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,20 @@ public struct FunctionConvention : CustomStringConvertible {
7373
hasLoweredAddresses: hasLoweredAddresses)
7474
}
7575

76-
/// If the function result depends on any parameters, return a
77-
/// Collection of LifetimeDependenceConvention indexed on the
78-
/// function parameter.
79-
public var resultDependencies: ResultDependencies? {
80-
let bridgedDependencies = bridgedFunctionType.SILFunctionType_getLifetimeDependencies()
81-
let dependencies = LifetimeDependencies(bridged: bridgedDependencies)
82-
let targetIndex = parameters.count
83-
84-
for dependence in dependencies {
85-
if dependence.getTargetIndex() == targetIndex {
86-
return ResultDependencies(bridged: dependence,
87-
parameterCount: parameters.count,
88-
hasSelfParameter: hasSelfParameter)
89-
}
90-
}
91-
return nil
76+
/// If the function result depends on any parameters, return a Collection of LifetimeDependenceConventions for the
77+
/// dependence source parameters.
78+
public var resultDependencies: LifetimeDependencies? {
79+
lifetimeDependencies(for: parameters.count)
80+
}
81+
82+
/// If the parameter indexed by 'targetParameterIndex' is the target of any dependencies on other parameters, return a
83+
/// Collection of LifetimeDependenceConventions for the dependence source parameters.
84+
public func parameterDependencies(for targetParameterIndex: Int) -> LifetimeDependencies? {
85+
lifetimeDependencies(for: targetParameterIndex)
86+
}
87+
88+
public func hasLifetimeDependencies() -> Bool {
89+
return bridgedFunctionType.SILFunctionType_getLifetimeDependencies().count() != 0
9290
}
9391

9492
public var description: String {
@@ -248,28 +246,22 @@ public enum LifetimeDependenceConvention : CustomStringConvertible {
248246
}
249247

250248
extension FunctionConvention {
251-
struct LifetimeDependencies : Collection {
252-
let bridged: BridgedLifetimeDependenceInfoArray
253-
254-
var startIndex: Int { 0 }
255-
256-
var endIndex: Int { bridged.count() }
257-
258-
func index(after index: Int) -> Int {
259-
return index + 1
260-
}
261-
// Create a Swift LifetimeDependenceInfo for BridgedLifetimeDependenceInfo if this method needs
262-
// to be exposed outside FunctionConvention.
263-
// That will likely need bridging IndexSubset to Swift.
264-
subscript(_ index: Int) -> BridgedLifetimeDependenceInfo {
265-
return bridged.at(index)
249+
// 'targetIndex' is either the parameter index or parameters.count for the function result.
250+
private func lifetimeDependencies(for targetIndex: Int) -> LifetimeDependencies? {
251+
let bridgedDependenceInfoArray = bridgedFunctionType.SILFunctionType_getLifetimeDependencies()
252+
for infoIndex in 0..<bridgedDependenceInfoArray.count() {
253+
let bridgedDependenceInfo = bridgedDependenceInfoArray.at(infoIndex)
254+
if bridgedDependenceInfo.targetIndex == targetIndex {
255+
return LifetimeDependencies(bridged: bridgedDependenceInfo,
256+
parameterCount: parameters.count,
257+
hasSelfParameter: hasSelfParameter)
258+
}
266259
}
260+
return nil
267261
}
268-
}
269262

270-
extension FunctionConvention {
271263
/// Collection of LifetimeDependenceConvention? that parallels parameters.
272-
public struct ResultDependencies : Collection, CustomStringConvertible {
264+
public struct LifetimeDependencies : Collection, CustomStringConvertible {
273265
let bridged: BridgedLifetimeDependenceInfo
274266
let paramCount: Int
275267
let hasSelfParam: Bool

0 commit comments

Comments
 (0)