MandatoryPerformanceOptimizations: only set the [perf_constraint] flag for callees of performance constraint functions

It used to also set it for functions which are referenced from a global with a const/section attribute - even if not performance attribute was present in the whole module. This is unnecessary and can lead to worse code generation.

rdar://152665294
This commit is contained in:
Erik Eckstein
2025-06-06 20:07:07 +02:00
parent 5dadb5dfd9
commit d6cbaf6ebd
2 changed files with 29 additions and 14 deletions

View File

@@ -40,6 +40,12 @@ let mandatoryPerformanceOptimizations = ModulePass(name: "mandatory-performance-
}
optimizeFunctionsTopDown(using: &worklist, moduleContext)
// It's not required to set the perf_constraint flag on all functions in embedded mode.
// Embedded mode already implies that flag.
if !moduleContext.options.enableEmbeddedSwift {
setPerformanceConstraintFlags(moduleContext)
}
}
private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
@@ -49,13 +55,6 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
if !context.loadFunction(function: f, loadCalleesRecursively: true) {
return
}
// It's not required to set the perf_constraint flag on all functions in embedded mode.
// Embedded mode already implies that flag.
if !moduleContext.options.enableEmbeddedSwift {
f.set(isPerformanceConstraint: true, context)
}
optimize(function: f, context, moduleContext, &worklist)
}
@@ -68,6 +67,17 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
}
}
private func setPerformanceConstraintFlags(_ moduleContext: ModulePassContext) {
var worklist = FunctionWorklist()
for f in moduleContext.functions where f.performanceConstraints != .none && f.isDefinition {
worklist.pushIfNotVisited(f)
}
while let f = worklist.pop() {
moduleContext.transform(function: f) { f.set(isPerformanceConstraint: true, $0) }
worklist.addCallees(of: f, moduleContext)
}
}
fileprivate struct PathFunctionTuple: Hashable {
var path: SmallProjectionPath
var function: Function