Skip to content

MandatoryPerformanceOptimizations: some refactoring #82142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
_ moduleContext: ModulePassContext) {
while let f = worklist.pop() {
moduleContext.transform(function: f) { context in
if !context.loadFunction(function: f, loadCalleesRecursively: true) {
return
if context.loadFunction(function: f, loadCalleesRecursively: true) {
optimize(function: f, context, moduleContext, &worklist)
}
optimize(function: f, context, moduleContext, &worklist)
}

// Generic specialization takes care of removing metatype arguments of generic functions.
Expand Down Expand Up @@ -316,8 +315,7 @@ private func shouldInline(apply: FullApplySite, callee: Function, alreadyInlined

private extension FullApplySite {
func resultIsUsedInGlobalInitialization() -> SmallProjectionPath? {
guard parentFunction.isGlobalInitOnceFunction,
let global = parentFunction.getInitializedGlobal() else {
guard let global = parentFunction.initializedGlobal else {
return nil
}

Expand Down Expand Up @@ -441,25 +439,6 @@ private extension Value {
}
}

private extension Function {
/// Analyzes the global initializer function and returns global it initializes (from `alloc_global` instruction).
func getInitializedGlobal() -> GlobalVariable? {
if !isDefinition {
return nil
}
for inst in self.entryBlock.instructions {
switch inst {
case let agi as AllocGlobalInst:
return agi.global
default:
break
}
}

return nil
}
}

fileprivate struct FunctionWorklist {
private(set) var functions = Array<Function>()
private var pushedFunctions = Set<Function>()
Expand All @@ -481,20 +460,10 @@ fileprivate struct FunctionWorklist {
pushIfNotVisited(f)
}

// Annotated global init-once functions
if f.isGlobalInitOnceFunction {
if let global = f.getInitializedGlobal(),
global.mustBeInitializedStatically {
pushIfNotVisited(f)
}
}

// @const global init-once functions
if f.isGlobalInitOnceFunction {
if let global = f.getInitializedGlobal(),
global.mustBeInitializedStatically {
pushIfNotVisited(f)
}
// Initializers of globals which must be initialized statically.
if let global = f.initializedGlobal,
global.mustBeInitializedStatically {
pushIfNotVisited(f)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,11 +763,14 @@ private struct EscapesToValueVisitor : EscapeVisitor {
}

extension Function {
/// Analyzes the global initializer function and returns global it initializes (from `alloc_global` instruction).
var initializedGlobal: GlobalVariable? {
if !isGlobalInitOnceFunction {
guard isGlobalInitOnceFunction,
let firstBlock = blocks.first
else {
return nil
}
for inst in entryBlock.instructions {
for inst in firstBlock.instructions {
if let allocGlobal = inst as? AllocGlobalInst {
return allocGlobal.global
}
Expand Down
2 changes: 2 additions & 0 deletions SwiftCompilerSources/Sources/SIL/GlobalVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ final public class GlobalVariable : CustomStringConvertible, HasShortDescription
return bridged.canBeInitializedStatically()
}

/// True if the global has an attribute, like `@const` or `@section` which requires the global to be
/// initialized statically.
public var mustBeInitializedStatically: Bool {
return bridged.mustBeInitializedStatically()
}
Expand Down