Skip to content

Commit c2d9d67

Browse files
authored
Merge pull request #82561 from eeckstein/fix-mandatory-perfopt
MandatoryPerformanceOptimization: don't recursive into referenced functions if they are not called
2 parents f8664ad + da484f3 commit c2d9d67

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,24 @@ extension FunctionWorklist {
466466
for inst in function.instructions {
467467
switch inst {
468468
case let fri as FunctionRefInst:
469-
pushIfNotVisited(fri.referencedFunction)
469+
// In embedded swift all reachable functions must be handled - even if they are not called,
470+
// e.g. referenced by a global.
471+
if context.options.enableEmbeddedSwift {
472+
pushIfNotVisited(fri.referencedFunction)
473+
}
474+
case let apply as ApplySite:
475+
if let callee = apply.referencedFunction {
476+
pushIfNotVisited(callee)
477+
}
478+
case let bi as BuiltinInst:
479+
switch bi.id {
480+
case .Once, .OnceWithContext:
481+
if let fri = bi.operands[1].value as? FunctionRefInst {
482+
pushIfNotVisited(fri.referencedFunction)
483+
}
484+
default:
485+
break
486+
}
470487
case let alloc as AllocRefInst:
471488
if context.options.enableEmbeddedSwift {
472489
addVTableMethods(forClassType: alloc.type, context)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
// RUN: %target-swift-frontend -emit-module -parse-as-library -enable-library-evolution %t/module.swift -emit-module-path=%t/Module.swiftmodule -module-name=Module
4+
// RUN: %target-swift-frontend -emit-sil -o /dev/null -parse-as-library %t/main.swift -I%t -enable-experimental-feature SymbolLinkageMarkers
5+
6+
// REQUIRES: swift_feature_SymbolLinkageMarkers
7+
8+
// Check that this compiles successfully
9+
10+
//--- module.swift
11+
12+
public struct X: ~Copyable {
13+
public init() {}
14+
deinit {
15+
print("deinit")
16+
}
17+
}
18+
19+
//--- main.swift
20+
21+
import Module
22+
23+
@_section("__TEXT,__mysection")
24+
var g: () -> () = { _ = X() }

test/SILOptimizer/mandatory_performance_optimizations.sil

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ bb0(%0 : $Int, %1 : @owned $Builtin.NativeObject):
215215
return %8 : $Builtin.NativeObject
216216
}
217217

218-
// CHECK-LABEL: sil [signature_optimized_thunk] [perf_constraint] [ossa] @metatype_arg :
218+
// CHECK-LABEL: sil [signature_optimized_thunk] [ossa] @metatype_arg :
219219
sil [ossa] @metatype_arg : $@convention(thin) (Int, @thick Int.Type, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
220220
bb0(%0 : $Int, %1 : $@thick Int.Type, %2 : @owned $Builtin.NativeObject):
221221
fix_lifetime %1 : $@thick Int.Type
@@ -243,7 +243,7 @@ bb2(%13 : @owned $any Error):
243243
throw %13 : $any Error
244244
}
245245

246-
// CHECK-LABEL: sil [signature_optimized_thunk] [perf_constraint] [ossa] @metatype_arg_throws :
246+
// CHECK-LABEL: sil [signature_optimized_thunk] [ossa] @metatype_arg_throws :
247247
sil [ossa] @metatype_arg_throws : $@convention(thin) (Int, @thick Int.Type, @owned Builtin.NativeObject) -> (@owned Builtin.NativeObject, @error any Error) {
248248
bb0(%0 : $Int, %1 : $@thick Int.Type, %2 : @owned $Builtin.NativeObject):
249249
fix_lifetime %1 : $@thick Int.Type

0 commit comments

Comments
 (0)