Skip to content

Commit 80ed35b

Browse files
committed
MandatoryAllocBoxToStack: also handle new specialized functions
Add new created specializations to the worklist so that those are optimized as well. rdar://154686063, rdar://154713388
1 parent cc17c04 commit 80ed35b

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

SwiftCompilerSources/Sources/Optimizer/DataStructures/Worklist.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ struct FunctionWorklist {
125125
}
126126
}
127127

128-
mutating func pushIfNotVisited(contentsOf functions: [Function]) {
128+
mutating func pushIfNotVisited<S: Sequence>(contentsOf functions: S) where S.Element == Function {
129129
for f in functions {
130130
pushIfNotVisited(f)
131131
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocBoxToStack.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ let allocBoxToStack = FunctionPass(name: "allocbox-to-stack") {
6565
let mandatoryAllocBoxToStack = ModulePass(name: "mandatory-allocbox-to-stack") {
6666
(moduleContext: ModulePassContext) in
6767

68+
var worklist = FunctionWorklist()
69+
worklist.pushIfNotVisited(contentsOf: moduleContext.functions)
70+
6871
var originalsOfSpecializedFunctions = FunctionWorklist()
6972

70-
for function in moduleContext.functions {
73+
while let function = worklist.pop() {
7174
moduleContext.transform(function: function) { context in
7275
let specFns = tryConvertBoxesToStack(in: function, context)
73-
originalsOfSpecializedFunctions.pushIfNotVisited(contentsOf: specFns)
76+
worklist.pushIfNotVisited(contentsOf: specFns.specializedFunctions)
77+
originalsOfSpecializedFunctions.pushIfNotVisited(contentsOf: specFns.originalFunctions)
7478
}
7579
}
7680

@@ -82,7 +86,7 @@ let mandatoryAllocBoxToStack = ModulePass(name: "mandatory-allocbox-to-stack") {
8286
/// Converts all non-escaping `alloc_box` to `alloc_stack` and specializes called functions if a
8387
/// box is passed to a function.
8488
/// Returns the list of original functions for which a specialization has been created.
85-
private func tryConvertBoxesToStack(in function: Function, _ context: FunctionPassContext) -> [Function] {
89+
private func tryConvertBoxesToStack(in function: Function, _ context: FunctionPassContext) -> FunctionSpecializations {
8690
var promotableBoxes = Array<(AllocBoxInst, Flags)>()
8791
var functionsToSpecialize = FunctionSpecializations()
8892

@@ -101,7 +105,7 @@ private func tryConvertBoxesToStack(in function: Function, _ context: FunctionPa
101105
function.fixStackNesting(context)
102106
}
103107

104-
return functionsToSpecialize.originalFunctions
108+
return functionsToSpecialize
105109
}
106110

107111
private func findPromotableBoxes(in function: Function,
@@ -187,6 +191,7 @@ private struct FunctionSpecializations {
187191
private var originalToSpecialized = Dictionary<Function, Function>()
188192

189193
var originalFunctions: [Function] { originals.functions }
194+
var specializedFunctions: [Function] { originals.functions.lazy.map { originalToSpecialized[$0]! } }
190195

191196
mutating func add(promotableArguments: [FunctionArgument]) {
192197
for arg in promotableArguments {

test/SILOptimizer/allocbox_to_stack_ownership.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,3 +1198,36 @@ bb0(%0 : $*T, %1 : $*T, %2 : $*T):
11981198
%15 = tuple ()
11991199
return %15 : $()
12001200
}
1201+
1202+
// CHECK-LABEL: sil [ossa] @alloc_box_in_specialized_callee :
1203+
// CHECK-NOT: alloc_box
1204+
// CHECK-LABEL: } // end sil function 'alloc_box_in_specialized_callee'
1205+
sil [ossa] @alloc_box_in_specialized_callee : $@convention(thin) (Int) -> () {
1206+
bb0(%0 : $Int):
1207+
%1 = alloc_box ${ var Int }
1208+
%2 = project_box %1, 0
1209+
store %0 to [trivial] %2
1210+
%4 = function_ref @callee_with_allocbox : $@convention(thin) (@guaranteed { var Int }) -> ()
1211+
%5 = apply %4(%1) : $@convention(thin) (@guaranteed { var Int }) -> ()
1212+
destroy_value %1
1213+
%r = tuple ()
1214+
return %r
1215+
}
1216+
1217+
// CHECK-LABEL: sil shared [ossa] @$s20callee_with_allocboxTf0s_n :
1218+
// CHECK-NOT: alloc_box
1219+
// CHECK-LABEL: } // end sil function '$s20callee_with_allocboxTf0s_n'
1220+
1221+
// CHECK-LABEL: sil [ossa] @callee_with_allocbox :
1222+
// CHECK-NOT: alloc_box
1223+
// CHECK-LABEL: } // end sil function 'callee_with_allocbox'
1224+
sil [ossa] @callee_with_allocbox : $@convention(thin) (@guaranteed { var Int }) -> () {
1225+
bb0(%0 : @guaranteed ${ var Int }):
1226+
%1 = alloc_box ${ var Int }
1227+
%2 = project_box %1 : ${ var Int }, 0
1228+
%3 = project_box %0 : ${ var Int }, 0
1229+
copy_addr %3 to %2
1230+
destroy_value %1
1231+
%r = tuple ()
1232+
return %r
1233+
}

0 commit comments

Comments
 (0)