Skip to content

Commit a43a5f7

Browse files
authored
Merge pull request #70535 from atrick/function-conventions
Migrate SwiftCompilerSources to FunctionConvention
2 parents 2752414 + 2128c21 commit a43a5f7

19 files changed

+522
-214
lines changed

SwiftCompilerSources/Sources/Optimizer/Analysis/AliasAnalysis.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,8 @@ private struct SideEffectsVisitor : EscapeVisitorWithResult {
188188
return .ignore
189189
}
190190
if user == apply {
191-
if let argIdx = apply.argumentIndex(of: operand) {
192-
let e = calleeAnalysis.getSideEffects(of: apply, forArgument: argIdx, path: path.projectionPath)
193-
result.merge(with: e)
194-
}
191+
let e = calleeAnalysis.getSideEffects(of: apply, operand: operand, path: path.projectionPath)
192+
result.merge(with: e)
195193
}
196194
return .continueWalk
197195
}
@@ -246,7 +244,7 @@ private struct IsIndirectResultWalker: AddressDefUseWalker {
246244

247245
mutating func leafUse(address: Operand, path: UnusedWalkingPath) -> WalkResult {
248246
if address.instruction == apply,
249-
let argIdx = apply.argumentIndex(of: address),
247+
let argIdx = apply.calleeArgumentIndex(of: address),
250248
argIdx < apply.numIndirectResultArguments {
251249
return .abortWalk
252250
}

SwiftCompilerSources/Sources/Optimizer/Analysis/CalleeAnalysis.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,18 @@ public struct CalleeAnalysis {
7878
}
7979

8080
/// Returns the argument specific side effects of an apply.
81-
public func getSideEffects(of apply: ApplySite, forArgument argumentIdx: Int, path: SmallProjectionPath) -> SideEffects.GlobalEffects {
82-
let calleeArgIdx = apply.calleeArgIndex(callerArgIndex: argumentIdx)
83-
let convention = apply.getArgumentConvention(calleeArgIndex: calleeArgIdx)
84-
let argument = apply.arguments[argumentIdx].at(path)
81+
public func getSideEffects(of apply: ApplySite, operand: Operand, path: SmallProjectionPath) -> SideEffects.GlobalEffects {
82+
var result = SideEffects.GlobalEffects()
83+
guard let calleeArgIdx = apply.calleeArgumentIndex(of: operand) else {
84+
return result
85+
}
86+
let convention = apply.convention(of: operand)!
87+
let argument = operand.value.at(path)
8588

8689
guard let callees = getCallees(callee: apply.callee) else {
8790
return .worstEffects.restrictedTo(argument: argument, withConvention: convention)
8891
}
8992

90-
var result = SideEffects.GlobalEffects()
9193
for callee in callees {
9294
let calleeEffects = callee.getSideEffects(forArgument: argument,
9395
atIndex: calleeArgIdx,

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeEscapeEffects.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private func isOperandOfRecursiveCall(_ op: Operand) -> Bool {
139139
if let applySite = inst as? FullApplySite,
140140
let callee = applySite.referencedFunction,
141141
callee == inst.parentFunction,
142-
let argIdx = applySite.argumentIndex(of: op),
142+
let argIdx = applySite.calleeArgumentIndex(of: op),
143143
op.value == callee.arguments[argIdx] {
144144
return true
145145
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ private struct CollectedEffects {
239239

240240
private mutating func handleApply(_ apply: ApplySite) {
241241
let callees = calleeAnalysis.getCallees(callee: apply.callee)
242-
let args = apply.arguments.enumerated().lazy.map {
243-
(calleeArgumentIndex: apply.calleeArgIndex(callerArgIndex: $0.0),
244-
callerArgument: $0.1)
242+
let args = apply.argumentOperands.lazy.map {
243+
(calleeArgumentIndex: apply.calleeArgumentIndex(of: $0)!,
244+
callerArgument: $0.value)
245245
}
246246
addEffects(ofFunctions: callees, withArguments: args)
247247
}
@@ -316,7 +316,7 @@ private struct CollectedEffects {
316316
if let calleePath = calleeEffect.copy { addEffects(.copy, to: argument, fromInitialPath: calleePath) }
317317
if let calleePath = calleeEffect.destroy { addEffects(.destroy, to: argument, fromInitialPath: calleePath) }
318318
} else {
319-
let convention = callee.getArgumentConvention(for: calleeArgIdx)
319+
let convention = callee.argumentConventions[calleeArgIdx]
320320
let wholeArgument = argument.at(defaultPath(for: argument))
321321
let calleeEffects = callee.getSideEffects(forArgument: wholeArgument,
322322
atIndex: calleeArgIdx,
@@ -444,7 +444,7 @@ private struct ArgumentEscapingWalker : ValueDefUseWalker, AddressDefUseWalker {
444444
return .continueWalk
445445

446446
case let apply as ApplySite:
447-
if apply.isCalleeOperand(value) {
447+
if apply.isCallee(operand: value) {
448448
// `CollectedEffects.handleApply` only handles argument operands of an apply, but not the callee operand.
449449
return .abortWalk
450450
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ObjCBridgingOptimization.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func isBridgeToSwiftCall(_ value: Value) -> ApplyInst? {
350350
return nil
351351
}
352352
guard bridgingCall.arguments.count == 2,
353-
bridgingCall.getArgumentConvention(calleeArgIndex: 0) == .directGuaranteed else {
353+
bridgingCall.calleeArgumentConventions[0] == .directGuaranteed else {
354354
return nil
355355
}
356356
return bridgingCall
@@ -362,7 +362,7 @@ func isBridgeToObjcCall(_ value: Value) -> ApplyInst? {
362362
let bridgingFunc = bridgingCall.referencedFunction,
363363
bridgingFunc.hasSemanticsAttribute("convertToObjectiveC"),
364364
bridgingCall.arguments.count == 1,
365-
bridgingCall.getArgumentConvention(calleeArgIndex: 0) == .directGuaranteed else {
365+
bridgingCall.calleeArgumentConventions[0] == .directGuaranteed else {
366366
return nil
367367
}
368368
return bridgingCall

SwiftCompilerSources/Sources/Optimizer/ModulePasses/ReadOnlyGlobalVariables.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ private struct FindWrites : AddressDefUseWalker {
5959
return address == ca.sourceOperand ? .continueWalk : .abortWalk
6060

6161
case let apply as FullApplySite:
62-
if let callerArgIdx = apply.argumentIndex(of: address) {
63-
let calleeArgIdx = apply.calleeArgIndex(callerArgIndex: callerArgIdx)
64-
let convention = apply.getArgumentConvention(calleeArgIndex: calleeArgIdx)
62+
if let calleeArgIdx = apply.calleeArgumentIndex(of: address) {
63+
let convention = apply.calleeArgumentConventions[calleeArgIdx]
6564
if convention.isIndirectIn {
6665
return .continueWalk
6766
}

SwiftCompilerSources/Sources/Optimizer/ModulePasses/StackProtection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ private struct StackProtectionOptimization {
260260

261261
for functionRefUse in fri.uses {
262262
guard let apply = functionRefUse.instruction as? ApplySite,
263-
let callerArgIdx = apply.callerArgIndex(calleeArgIndex: arg.index) else {
263+
let callerArgOp = apply.operand(forCalleeArgumentIndex: arg.index) else {
264264
if enableMoveInout {
265265
return NeedInsertMoves.yes
266266
}
267267
continue
268268
}
269-
let callerArg = apply.arguments[callerArgIdx]
269+
let callerArg = callerArgOp.value
270270
if callerArg.type.isAddress {
271271
// It's an indirect argument.
272272
switch callerArg.accessBase.isStackAllocated {

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ extension Operand {
491491

492492
extension Instruction {
493493
func setOperand(at index : Int, to value: Value, _ context: some MutatingContext) {
494-
if self is FullApplySite && index == ApplyOperands.calleeOperandIndex {
494+
if let apply = self as? FullApplySite, apply.isCallee(operand: operands[index]) {
495495
context.notifyCallsChanged()
496496
}
497497
context.notifyInstructionsChanged()

SwiftCompilerSources/Sources/Optimizer/Utilities/Devirtualization.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ extension DestroyValueInst : DevirtualizableDestroy {
107107
let builder = Builder(before: self, context)
108108
let subs = context.getContextSubstitutionMap(for: type)
109109
let deinitRef = builder.createFunctionRef(deinitializer)
110-
if deinitializer.getArgumentConvention(for: deinitializer.selfArgumentIndex).isIndirect {
110+
if deinitializer.argumentConventions[deinitializer.selfArgumentIndex].isIndirect {
111111
let allocStack = builder.createAllocStack(type)
112112
builder.createStore(source: destroyedValue, destination: allocStack, ownership: .initialize)
113113
builder.createApply(function: deinitRef, subs, arguments: [allocStack])
@@ -175,7 +175,7 @@ extension DestroyAddrInst : DevirtualizableDestroy {
175175
let builder = Builder(before: self, context)
176176
let subs = context.getContextSubstitutionMap(for: destroyedAddress.type)
177177
let deinitRef = builder.createFunctionRef(deinitializer)
178-
if !deinitializer.getArgumentConvention(for: deinitializer.selfArgumentIndex).isIndirect {
178+
if !deinitializer.argumentConventions[deinitializer.selfArgumentIndex].isIndirect {
179179
let value = builder.createLoad(fromAddress: destroyedAddress, ownership: .take)
180180
builder.createApply(function: deinitRef, subs, arguments: [value])
181181
} else {

SwiftCompilerSources/Sources/Optimizer/Utilities/EscapeUtils.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
555555
/// Handle an apply (full or partial) during the walk-down.
556556
private mutating
557557
func walkDownCallee(argOp: Operand, apply: ApplySite, path: Path) -> WalkResult {
558-
guard let argIdx = apply.argumentIndex(of: argOp) else {
558+
guard let calleeArgIdx = apply.calleeArgumentIndex(of: argOp) else {
559559
// The callee or a type dependent operand of the apply does not let escape anything.
560560
return .continueWalk
561561
}
@@ -591,8 +591,6 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
591591
return isEscaping
592592
}
593593

594-
let calleeArgIdx = apply.calleeArgIndex(callerArgIndex: argIdx)
595-
596594
for callee in callees {
597595
let effects = callee.effects
598596
if !effects.escapeEffects.canEscape(argumentIndex: calleeArgIdx,
@@ -617,12 +615,12 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
617615
case .escapingToArgument(let toArgIdx, let toPath):
618616
// Note: exclusive argument -> argument effects cannot appear, so we don't need to handle them here.
619617
if effect.matches(calleeArgIdx, argPath.projectionPath) {
620-
guard let callerToIdx = apply.callerArgIndex(calleeArgIndex: toArgIdx) else {
618+
guard let argOp = apply.operand(forCalleeArgumentIndex: toArgIdx) else {
621619
return isEscaping
622620
}
623621

624622
// Continue at the destination of an arg-to-arg escape.
625-
let arg = apply.arguments[callerToIdx]
623+
let arg = argOp.value
626624

627625
let p = Path(projectionPath: toPath, followStores: false, knownType: nil)
628626
if walkUp(addressOrValue: arg, path: p) == .abortWalk {
@@ -778,10 +776,10 @@ fileprivate struct EscapeWalker<V: EscapeVisitor> : ValueDefUseWalker,
778776
switch effect.kind {
779777
case .escapingToReturn(let toPath, let exclusive):
780778
if exclusive && path.projectionPath.matches(pattern: toPath) {
781-
guard let callerArgIdx = apply.callerArgIndex(calleeArgIndex: effect.argumentIndex) else {
779+
guard let argOp = apply.operand(forCalleeArgumentIndex: effect.argumentIndex) else {
782780
return .abortWalk
783781
}
784-
let arg = apply.arguments[callerArgIdx]
782+
let arg = argOp.value
785783

786784
let p = Path(projectionPath: effect.pathPattern, followStores: path.followStores, knownType: nil)
787785
if walkUp(addressOrValue: arg, path: p) == .abortWalk {

0 commit comments

Comments
 (0)