Skip to content

Commit 2237d86

Browse files
authored
Merge pull request #82142 from eeckstein/mandatory-perf-opt
MandatoryPerformanceOptimizations: some refactoring
2 parents 4b2af07 + f0b3ec0 commit 2237d86

File tree

3 files changed

+14
-40
lines changed

3 files changed

+14
-40
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
5252
_ moduleContext: ModulePassContext) {
5353
while let f = worklist.pop() {
5454
moduleContext.transform(function: f) { context in
55-
if !context.loadFunction(function: f, loadCalleesRecursively: true) {
56-
return
55+
if context.loadFunction(function: f, loadCalleesRecursively: true) {
56+
optimize(function: f, context, moduleContext, &worklist)
5757
}
58-
optimize(function: f, context, moduleContext, &worklist)
5958
}
6059

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

317316
private extension FullApplySite {
318317
func resultIsUsedInGlobalInitialization() -> SmallProjectionPath? {
319-
guard parentFunction.isGlobalInitOnceFunction,
320-
let global = parentFunction.getInitializedGlobal() else {
318+
guard let global = parentFunction.initializedGlobal else {
321319
return nil
322320
}
323321

@@ -441,25 +439,6 @@ private extension Value {
441439
}
442440
}
443441

444-
private extension Function {
445-
/// Analyzes the global initializer function and returns global it initializes (from `alloc_global` instruction).
446-
func getInitializedGlobal() -> GlobalVariable? {
447-
if !isDefinition {
448-
return nil
449-
}
450-
for inst in self.entryBlock.instructions {
451-
switch inst {
452-
case let agi as AllocGlobalInst:
453-
return agi.global
454-
default:
455-
break
456-
}
457-
}
458-
459-
return nil
460-
}
461-
}
462-
463442
fileprivate struct FunctionWorklist {
464443
private(set) var functions = Array<Function>()
465444
private var pushedFunctions = Set<Function>()
@@ -481,20 +460,10 @@ fileprivate struct FunctionWorklist {
481460
pushIfNotVisited(f)
482461
}
483462

484-
// Annotated global init-once functions
485-
if f.isGlobalInitOnceFunction {
486-
if let global = f.getInitializedGlobal(),
487-
global.mustBeInitializedStatically {
488-
pushIfNotVisited(f)
489-
}
490-
}
491-
492-
// @const global init-once functions
493-
if f.isGlobalInitOnceFunction {
494-
if let global = f.getInitializedGlobal(),
495-
global.mustBeInitializedStatically {
496-
pushIfNotVisited(f)
497-
}
463+
// Initializers of globals which must be initialized statically.
464+
if let global = f.initializedGlobal,
465+
global.mustBeInitializedStatically {
466+
pushIfNotVisited(f)
498467
}
499468
}
500469
}

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,14 @@ private struct EscapesToValueVisitor : EscapeVisitor {
763763
}
764764

765765
extension Function {
766+
/// Analyzes the global initializer function and returns global it initializes (from `alloc_global` instruction).
766767
var initializedGlobal: GlobalVariable? {
767-
if !isGlobalInitOnceFunction {
768+
guard isGlobalInitOnceFunction,
769+
let firstBlock = blocks.first
770+
else {
768771
return nil
769772
}
770-
for inst in entryBlock.instructions {
773+
for inst in firstBlock.instructions {
771774
if let allocGlobal = inst as? AllocGlobalInst {
772775
return allocGlobal.global
773776
}

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ final public class GlobalVariable : CustomStringConvertible, HasShortDescription
6969
return bridged.canBeInitializedStatically()
7070
}
7171

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

0 commit comments

Comments
 (0)