-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Rework optimization of global variables #65764
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
85aa1c1
handle debug_step in static initializers of globals and in the const …
eeckstein 3560360
Handle `builtin.once` in BottomUpFunctionOrder
eeckstein 8a8a895
alias analysis: compute more precise memory effects of `builtin "once"`
eeckstein a8c9aae
Swift Optimizer: add simplification for `cond_fail`
eeckstein 0a05124
Swift Optimizer: add simplification of `debug_step`
eeckstein 5a3ab6e
Swift Optimizer: add simplifications for `destructure_struct` and `de…
eeckstein 88973a3
Swift Optimizer: add simplification for `tuple_extract`
eeckstein 9b51e69
Swift Optimizer: constant fold builtins in the simplification passes
eeckstein c4096bc
Swift Optimizer: simplify `builtin "once"`
eeckstein e95c642
Swift SIL: add some APIs for global variables
eeckstein 3e04b86
make ModulePassContext conform to CustomStringConvertible
eeckstein 260e68b
Passmanager: fix a problem with skipping the inliner pass
eeckstein 47a7acd
LICM: hoist `builtin "once"` calls out of loops
eeckstein 56c09c3
CSE: cse `builtin "once"` calls
eeckstein 19b828b
StringOptimization: handle inlined global accessors.
eeckstein ee2924f
Inliner: don't distinguish between the "mid-level" and "late" inliner
eeckstein 78ce13d
WalkUtils: Don't treat `end_access` as leaf-use in the AddressDefUseW…
eeckstein 2b117fd
Swift Optimizer: add APIs to copy from or to a global static initializer
eeckstein 6d6b94e
Swift Optimizer: add the InitializeStaticGlobals function pass
eeckstein 88a4a97
Swift Optimizer: add simplification for `load`
eeckstein 960ca70
Swift Optimizer: add the module pass ReadOnlyGlobalVariables
eeckstein 1e6511e
Pass Pipeline: replace the old GlobalOpt with the new InitializeStati…
eeckstein df7c71b
Optimizer: remove the now obsolete GlobalOpt pass
eeckstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
//===--- InitializeStaticGlobals.swift -------------------------------------==// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SIL | ||
|
||
/// Converts a lazily initialized global to a statically initialized global variable. | ||
/// | ||
/// When this pass runs on a global initializer `[global_init_once_fn]` it tries to | ||
/// create a static initializer for the initialized global. | ||
/// | ||
/// ``` | ||
/// sil [global_init_once_fn] @globalinit { | ||
/// alloc_global @the_global | ||
/// %a = global_addr @the_global | ||
/// %i = some_const_initializer_insts | ||
/// store %i to %a | ||
/// } | ||
/// ``` | ||
/// The pass creates a static initializer for the global: | ||
/// ``` | ||
/// sil_global @the_global = { | ||
/// %initval = some_const_initializer_insts | ||
/// } | ||
/// ``` | ||
/// and removes the allocation and store instructions from the initializer function: | ||
/// ``` | ||
/// sil [global_init_once_fn] @globalinit { | ||
/// %a = global_addr @the_global | ||
/// %i = some_const_initializer_insts | ||
/// } | ||
/// ``` | ||
/// The initializer then becomes a side-effect free function which let's the builtin- | ||
/// simplification remove the `builtin "once"` which calls the initializer. | ||
/// | ||
let initializeStaticGlobalsPass = FunctionPass(name: "initialize-static-globals") { | ||
(function: Function, context: FunctionPassContext) in | ||
|
||
if !function.isGlobalInitOnceFunction { | ||
return | ||
} | ||
|
||
guard let (allocInst, storeToGlobal) = function.getGlobalInitialization() else { | ||
return | ||
} | ||
|
||
if !allocInst.global.canBeInitializedStatically { | ||
return | ||
} | ||
|
||
context.createStaticInitializer(for: allocInst.global, | ||
initValue: storeToGlobal.source as! SingleValueInstruction) | ||
context.erase(instruction: allocInst) | ||
context.erase(instruction: storeToGlobal) | ||
} | ||
|
||
private extension Function { | ||
/// Analyses the global initializer function and returns the `alloc_global` and `store` | ||
/// instructions which initialize the global. | ||
/// | ||
/// The function's single basic block must contain following code pattern: | ||
/// ``` | ||
/// alloc_global @the_global | ||
/// %a = global_addr @the_global | ||
/// %i = some_const_initializer_insts | ||
/// store %i to %a | ||
/// ``` | ||
func getGlobalInitialization() -> (allocInst: AllocGlobalInst, storeToGlobal: StoreInst)? { | ||
|
||
guard let block = singleBlock else { | ||
return nil | ||
} | ||
|
||
var allocInst: AllocGlobalInst? = nil | ||
var globalAddr: GlobalAddrInst? = nil | ||
var store: StoreInst? = nil | ||
|
||
for inst in block.instructions { | ||
switch inst { | ||
case is ReturnInst, | ||
is DebugValueInst, | ||
is DebugStepInst: | ||
break | ||
case let agi as AllocGlobalInst: | ||
if allocInst != nil { | ||
return nil | ||
} | ||
allocInst = agi | ||
case let ga as GlobalAddrInst: | ||
if globalAddr != nil { | ||
return nil | ||
} | ||
guard let agi = allocInst, agi.global == ga.global else { | ||
return nil | ||
} | ||
globalAddr = ga | ||
case let si as StoreInst: | ||
if store != nil { | ||
return nil | ||
} | ||
guard let ga = globalAddr else { | ||
return nil | ||
} | ||
if si.destination != ga { | ||
return nil | ||
} | ||
store = si | ||
default: | ||
if !inst.isValidInStaticInitializerOfGlobal { | ||
return nil | ||
} | ||
} | ||
} | ||
if let store = store { | ||
return (allocInst: allocInst!, storeToGlobal: store) | ||
} | ||
return nil | ||
} | ||
|
||
var singleBlock: BasicBlock? { | ||
let block = entryBlock | ||
if block.next != nil { | ||
return nil | ||
} | ||
return block | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyCondFail.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===--- SimplifyCondFail.swift -------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SIL | ||
|
||
extension CondFailInst : OnoneSimplifyable { | ||
func simplify(_ context: SimplifyContext) { | ||
|
||
/// Eliminates | ||
/// ``` | ||
/// %0 = integer_literal 0 | ||
/// cond_fail %0, "message" | ||
/// ``` | ||
if let literal = condition as? IntegerLiteralInst, | ||
literal.value.isZero() { | ||
|
||
context.erase(instruction: self) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyDebugStep.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===--- SimplifyDebugStep.swift ------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SIL | ||
|
||
extension DebugStepInst : Simplifyable { | ||
func simplify(_ context: SimplifyContext) { | ||
// When compiling with optimizations (note: it's not a OnoneSimplifyable transformation), | ||
// unconditionally remove debug_step instructions. | ||
context.erase(instruction: self) | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm baffled as to why
getGlobalInitialization
is a member ofFunction
as opposed to taking a Function as an argument.As a rule, if it doesn't require access to Function's internal state, don't make it part of Function's interface.
The implicit
self
in the implementation also makes it very hard to read the code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that in an extension you can't access internal state and a private extension doesn't add to the type's interface.