Skip to content

Commit bf30a19

Browse files
Merge pull request #77394 from aschwaighofer/comments_shouldExpand
Add comments to SwiftCompilerSources for shouldExpand()
2 parents 5970f0e + 9093e1b commit bf30a19

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/DeadStoreElimination.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ let deadStoreElimination = FunctionPass(name: "dead-store-elimination") {
7575
}
7676

7777
private func tryEliminate(store: StoreInst, complexityBudget: inout Int, _ context: FunctionPassContext) {
78+
// Check if the type can be expanded without a significant increase to code
79+
// size. This pass splits values into its consitutent parts which effectively
80+
// expands the value into projections which can increase code size.
7881
if !store.hasValidOwnershipForDeadStoreElimination || !store.source.type.shouldExpand(context) {
7982
return
8083
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ private func eliminateRedundantLoads(in function: Function, ignoreArrays: Bool,
9797
{
9898
continue
9999
}
100+
// Check if the type can be expanded without a significant increase to
101+
// code size.
102+
// We block redundant load elimination because it might increase
103+
// register pressure for large values. Furthermore, this pass also
104+
// splits values into its projections (e.g
105+
// shrinkMemoryLifetimeAndSplit).
100106
if !load.type.shouldExpand(context) {
101107
continue
102108
}

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,19 @@ extension CheckedCastAddrBranchInst {
811811
}
812812

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

0 commit comments

Comments
 (0)