Skip to content

Commit 9df7217

Browse files
authored
Merge pull request #82141 from eeckstein/fix-mpo2-6.2
[6.2] MandatoryPerformanceOptimizations: only set the `[perf_constraint]` flag for callees of performance constraint functions
2 parents 5b71a7d + 779434a commit 9df7217

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ let mandatoryPerformanceOptimizations = ModulePass(name: "mandatory-performance-
4444
// Print errors for generic functions in vtables, which is not allowed in embedded Swift.
4545
checkVTablesForGenericFunctions(moduleContext)
4646
}
47+
48+
// It's not required to set the perf_constraint flag on all functions in embedded mode.
49+
// Embedded mode already implies that flag.
50+
if !moduleContext.options.enableEmbeddedSwift {
51+
setPerformanceConstraintFlags(moduleContext)
52+
}
4753
}
4854

4955
private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
@@ -53,13 +59,6 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
5359
if !context.loadFunction(function: f, loadCalleesRecursively: true) {
5460
return
5561
}
56-
57-
// It's not required to set the perf_constraint flag on all functions in embedded mode.
58-
// Embedded mode already implies that flag.
59-
if !moduleContext.options.enableEmbeddedSwift {
60-
f.set(isPerformanceConstraint: true, context)
61-
}
62-
6362
optimize(function: f, context, moduleContext, &worklist)
6463
}
6564

@@ -72,6 +71,17 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
7271
}
7372
}
7473

74+
private func setPerformanceConstraintFlags(_ moduleContext: ModulePassContext) {
75+
var worklist = FunctionWorklist()
76+
for f in moduleContext.functions where f.performanceConstraints != .none && f.isDefinition {
77+
worklist.pushIfNotVisited(f)
78+
}
79+
while let f = worklist.pop() {
80+
moduleContext.transform(function: f) { f.set(isPerformanceConstraint: true, $0) }
81+
worklist.addCallees(of: f, moduleContext)
82+
}
83+
}
84+
7585
fileprivate struct PathFunctionTuple: Hashable {
7686
var path: SmallProjectionPath
7787
var function: Function

test/IRGen/section.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ struct MyStruct {
1717
@_section("__TEXT,__mysection") @_used func foo() {}
1818
}
1919

20+
@_section("__TEXT,__mysection")
21+
var functionptr = testit
22+
func testit(_ e: consuming Any) -> Any { e }
23+
2024
// SIL: @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue var g0: Int { get set }
2125
// SIL: @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue var g1: (Int, Int) { get set }
2226
// SIL: @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue var g2: Bool { get set }
@@ -28,22 +32,23 @@ struct MyStruct {
2832
// SIL: @_section("__DATA,__mysection") @_hasStorage @_hasInitialValue static var static0: Int { get set }
2933
// SIL: @_section("__TEXT,__mysection") @_used func foo()
3034

31-
// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g0_WZ : $@convention(c)
35+
// SIL: sil private [global_init_once_fn] @$s7section2g0_WZ : $@convention(c)
3236
// SIL: sil hidden [global_init] @$s7section2g0Sivau : $@convention(thin)
33-
// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g1_WZ : $@convention(c)
37+
// SIL: sil private [global_init_once_fn] @$s7section2g1_WZ : $@convention(c)
3438
// SIL: sil hidden [global_init] @$s7section2g1Si_Sitvau : $@convention(thin)
35-
// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g2_WZ : $@convention(c)
39+
// SIL: sil private [global_init_once_fn] @$s7section2g2_WZ : $@convention(c)
3640
// SIL: sil hidden [global_init] @$s7section2g2Sbvau : $@convention(thin)
37-
// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g3_WZ : $@convention(c)
41+
// SIL: sil private [global_init_once_fn] @$s7section2g3_WZ : $@convention(c)
3842
// SIL: sil [global_init] @$s7section2g3Sbvau : $@convention(thin)
39-
// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g4_WZ : $@convention(c)
43+
// SIL: sil private [global_init_once_fn] @$s7section2g4_WZ : $@convention(c)
4044
// SIL: sil hidden [global_init] @$s7section2g4SpySiGSgvau : $@convention(thin)
41-
// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section2g5_WZ : $@convention(c)
45+
// SIL: sil private [global_init_once_fn] @$s7section2g5_WZ : $@convention(c)
4246
// SIL: sil hidden [global_init] @$s7section2g5SpySiGSgvau : $@convention(thin)
4347
// SIL: sil hidden [used] [section "__TEXT,__mysection"] @$s7section3fooyyF : $@convention(thin)
44-
// SIL: sil private [global_init_once_fn] [perf_constraint] @$s7section8MyStructV7static0_WZ : $@convention(c)
48+
// SIL: sil private [global_init_once_fn] @$s7section8MyStructV7static0_WZ : $@convention(c)
4549
// SIL: sil hidden [global_init] @$s7section8MyStructV7static0Sivau : $@convention(thin)
4650
// SIL: sil hidden [used] [section "__TEXT,__mysection"] @$s7section8MyStructV3fooyyF : $@convention(method)
51+
// SIL: sil hidden @$s7section6testityypypnF : $@convention(thin) (@in Any) -> @out Any {
4752

4853
// IR: @"$s7section2g0Sivp" = hidden global %TSi <{ {{(i64|i32)}} 1 }>, section "__DATA,__mysection"
4954
// IR: @"$s7section2g1Si_Sitvp" = hidden global <{ %TSi, %TSi }> <{ %TSi <{ {{(i64|i32)}} 42 }>, %TSi <{ {{(i64|i32)}} 43 }> }>, section "__DATA,__mysection"

0 commit comments

Comments
 (0)