Skip to content

Commit 84418d2

Browse files
authored
Merge pull request #83037 from meg-gupta/fixfso2
Bailout SwiftCompilerSource's function signature opts for functions with lifetime dependencies
2 parents 8d5084b + 455990a commit 84418d2

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/FunctionSignatureTransforms.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@
1212

1313
import SIL
1414

15+
private extension ArgumentConventions {
16+
func isLifetimeSourceOrTarget(index argIndex: Int) -> Bool {
17+
// Check if `argIndex` is a lifetime target
18+
if self[parameterDependencies: argIndex] != nil {
19+
return true
20+
}
21+
22+
// Check if `argIndex` is a lifetime source in resultDependencies
23+
if self[resultDependsOn: argIndex] != nil {
24+
return true
25+
}
26+
27+
// Check if `argIndex` is a lifetime source in parameterDependencies
28+
for targetIndex in firstParameterIndex..<self.count {
29+
if getDependence(target: targetIndex, source: argIndex) != nil {
30+
return true
31+
}
32+
}
33+
34+
return false
35+
}
36+
}
37+
1538
/// Replace an apply with metatype arguments with an apply to a specialized function, where the
1639
/// metatype values are not passed, but rematerialized in the entry block of the specialized function
1740
///
@@ -52,6 +75,15 @@ func specializeByRemovingMetatypeArguments(apply: FullApplySite, _ context: Modu
5275
return
5376
}
5477

78+
// If a function has lifetime dependencies, bailout if dead arguments precede lifetime sources or targets
79+
if callee.convention.hasLifetimeDependencies() {
80+
for (argIndex, _) in callee.arguments.enumerated() where argIndex >= deadArgIndices.first! {
81+
if callee.argumentConventions.isLifetimeSourceOrTarget(index: argIndex) {
82+
return
83+
}
84+
}
85+
}
86+
5587
let specializedFuncName = context.mangle(withDeadArguments: deadArgIndices, from: callee)
5688

5789
let specializedCallee: Function

test/SILOptimizer/mandatory_performance_optimizations.sil

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -mandatory-performance-optimizations | %FileCheck %s
1+
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -mandatory-performance-optimizations -enable-experimental-feature Lifetimes | %FileCheck %s
22

33
// REQUIRES: swift_in_compiler
4+
// REQUIRES: swift_feature_Lifetimes
45

56
sil_stage canonical
67

@@ -222,6 +223,78 @@ bb0(%0 : $Int, %1 : $@thick Int.Type, %2 : @owned $Builtin.NativeObject):
222223
return %2 : $Builtin.NativeObject
223224
}
224225

226+
sil [no_locks] [perf_constraint] [ossa] @dont_remove_metatype_arg_lifetime : $@convention(thin) (Int, UnsafeRawPointer) -> @lifetime(borrow 1) Span<Int> {
227+
bb0(%0 : $Int, %1 : $UnsafeRawPointer):
228+
%3 = metatype $@thick Int.Type
229+
%7 = function_ref @metatype_arg_lifetime : $@convention(thin) (Int, @thick Int.Type, UnsafeRawPointer) -> @lifetime(borrow 2) Span<Int>
230+
%8 = apply %7(%0, %3, %1) : $@convention(thin) (Int, @thick Int.Type, UnsafeRawPointer) -> @lifetime(borrow 2) Span<Int>
231+
return %8
232+
}
233+
234+
sil [ossa] @get_span : $@convention(thin) (UnsafeRawPointer) -> @lifetime(borrow 0) Span<Int>
235+
236+
// CHECK-NOT: sil [signature_optimized_thunk] [ossa] @metatype_arg_lifetime :
237+
sil [ossa] @metatype_arg_lifetime : $@convention(thin) (Int, @thick Int.Type, UnsafeRawPointer) -> @lifetime(borrow 2) Span<Int> {
238+
bb0(%0 : $Int, %1 : $@thick Int.Type, %2 : $UnsafeRawPointer):
239+
fix_lifetime %1 : $@thick Int.Type
240+
%3 = function_ref @get_span : $@convention(thin) (UnsafeRawPointer) -> @lifetime(borrow 0) Span<Int>
241+
%4 = apply %3(%2) : $@convention(thin) (UnsafeRawPointer) -> @lifetime(borrow 0) Span<Int>
242+
return %4
243+
}
244+
245+
sil [no_locks] [perf_constraint] [ossa] @can_remove_metatype_arg_lifetime : $@convention(thin) (Int, UnsafeRawPointer) -> @lifetime(borrow 1) Span<Int> {
246+
bb0(%0 : $Int, %1 : $UnsafeRawPointer):
247+
%3 = metatype $@thick Int.Type
248+
%7 = function_ref @metatype_arg_lifetime_opt : $@convention(thin) (UnsafeRawPointer, Int, @thick Int.Type) -> @lifetime(borrow 0) Span<Int>
249+
%8 = apply %7(%1, %0, %3) : $@convention(thin) (UnsafeRawPointer, Int, @thick Int.Type) -> @lifetime(borrow 0) Span<Int>
250+
return %8
251+
}
252+
253+
// CHECK: sil [signature_optimized_thunk] [ossa] @metatype_arg_lifetime_opt :
254+
sil [ossa] @metatype_arg_lifetime_opt : $@convention(thin) (UnsafeRawPointer, Int, @thick Int.Type) -> @lifetime(borrow 0) Span<Int> {
255+
bb0(%2 : $UnsafeRawPointer, %0 : $Int, %1 : $@thick Int.Type):
256+
fix_lifetime %1 : $@thick Int.Type
257+
%3 = function_ref @get_span : $@convention(thin) (UnsafeRawPointer) -> @lifetime(borrow 0) Span<Int>
258+
%4 = apply %3(%2) : $@convention(thin) (UnsafeRawPointer) -> @lifetime(borrow 0) Span<Int>
259+
return %4
260+
}
261+
262+
sil [no_locks] [perf_constraint] [ossa] @dont_remove_metatype_arg_lifetime_inout : $@convention(thin) (Int, @lifetime(copy 2) @inout MutableSpan<Int>, @owned MutableSpan<Int>) -> () {
263+
bb0(%0 : $Int, %1 : $*MutableSpan<Int>, %2 : @owned $MutableSpan<Int>):
264+
%3 = metatype $@thick Int.Type
265+
%7 = function_ref @metatype_arg_lifetime_inout : $@convention(thin) (Int, @thick Int.Type, @lifetime(copy 3) @inout MutableSpan<Int>, @owned MutableSpan<Int>) -> ()
266+
%8 = apply %7(%0, %3, %1, %2) : $@convention(thin) (Int, @thick Int.Type, @lifetime(copy 3) @inout MutableSpan<Int>, @owned MutableSpan<Int>) -> ()
267+
%r = tuple ()
268+
return %r
269+
}
270+
271+
// CHECK-NOT: sil [signature_optimized_thunk] [ossa] @metatype_arg_lifetime_inout :
272+
sil [ossa] @metatype_arg_lifetime_inout : $@convention(thin) (Int, @thick Int.Type, @lifetime(copy 3) @inout MutableSpan<Int>, @owned MutableSpan<Int>) -> () {
273+
bb0(%0 : $Int, %1 : $@thick Int.Type, %2 : $*MutableSpan<Int>, %3 : @owned $MutableSpan<Int>):
274+
fix_lifetime %1 : $@thick Int.Type
275+
destroy_value %3
276+
%r = tuple ()
277+
return %r
278+
}
279+
280+
sil [no_locks] [perf_constraint] [ossa] @dont_remove_metatype_arg_lifetime_inout_source : $@convention(thin) (Int, @lifetime(copy 2) @inout MutableSpan<Int>, @owned MutableSpan<Int>) -> () {
281+
bb0(%0 : $Int, %1 : $*MutableSpan<Int>, %2 : @owned $MutableSpan<Int>):
282+
%3 = metatype $@thick Int.Type
283+
%7 = function_ref @metatype_arg_lifetime_inout_source : $@convention(thin) (@lifetime(copy 3) @inout MutableSpan<Int>, Int, @thick Int.Type, @owned MutableSpan<Int>) -> ()
284+
%8 = apply %7(%1, %0, %3, %2) : $@convention(thin) (@lifetime(copy 3) @inout MutableSpan<Int>, Int, @thick Int.Type, @owned MutableSpan<Int>) -> ()
285+
%r = tuple ()
286+
return %r
287+
}
288+
289+
// CHECK-NOT: sil [signature_optimized_thunk] [ossa] @metatype_arg_lifetime_inout_source :
290+
sil [ossa] @metatype_arg_lifetime_inout_source : $@convention(thin) (@lifetime(copy 3) @inout MutableSpan<Int>, Int, @thick Int.Type, @owned MutableSpan<Int>) -> () {
291+
bb0(%2 : $*MutableSpan<Int>, %0 : $Int, %1 : $@thick Int.Type, %3 : @owned $MutableSpan<Int>):
292+
fix_lifetime %1 : $@thick Int.Type
293+
destroy_value %3
294+
%r = tuple ()
295+
return %r
296+
}
297+
225298
// CHECK-LABEL: sil [no_locks] [perf_constraint] [ossa] @remove_metatype_arg_throws :
226299
// CHECK: [[F:%.*]] = function_ref @$s19metatype_arg_throwsTf4ndn_n : $@convention(thin) (Int, @owned Builtin.NativeObject) -> (@owned Builtin.NativeObject, @error any Error)
227300
// CHECK: try_apply [[F]](%0, %1) : $@convention(thin) (Int, @owned Builtin.NativeObject) -> (@owned Builtin.NativeObject, @error any Error), normal bb1, error bb2

0 commit comments

Comments
 (0)