Skip to content

Add comments to SwiftCompilerSources for shouldExpand() #77394

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
Nov 7, 2024
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 @@ -75,6 +75,9 @@ let deadStoreElimination = FunctionPass(name: "dead-store-elimination") {
}

private func tryEliminate(store: StoreInst, complexityBudget: inout Int, _ context: FunctionPassContext) {
// Check if the type can be expanded without a significant increase to code
// size. This pass splits values into its consitutent parts which effectively
// expands the value into projections which can increase code size.
if !store.hasValidOwnershipForDeadStoreElimination || !store.source.type.shouldExpand(context) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ private func eliminateRedundantLoads(in function: Function, ignoreArrays: Bool,
{
continue
}
// Check if the type can be expanded without a significant increase to
// code size.
// We block redundant load elimination because it might increase
// register pressure for large values. Furthermore, this pass also
// splits values into its projections (e.g
// shrinkMemoryLifetimeAndSplit).
if !load.type.shouldExpand(context) {
continue
}
Expand Down
13 changes: 13 additions & 0 deletions SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,19 @@ extension CheckedCastAddrBranchInst {
}

extension Type {
/// True if a type can be expanded without a significant increase to code
/// size.
/// Expanding a type can mean expressing it as a SSA value (which ultimately
/// is represented as multiple SSA values in LLVM IR) instead of indirectly
/// via memory operations (copy_addr), or exploding an SSA value into its
/// constituent projections.
/// Once a value is represented as its projections we don't "reconstitute" the
/// aggregate value anymore leading to register pressure and code size bloat.
/// Therefore, we try to keep "larger" values indirect and not exploated
/// throughout the pipeline.
///
/// False if expanding a type is invalid. For example, expanding a
/// struct-with-deinit drops the deinit.
func shouldExpand(_ context: some Context) -> Bool {
if !context.options.useAggressiveReg2MemForCodeSize {
return true
Expand Down