@@ -65,12 +65,16 @@ let allocBoxToStack = FunctionPass(name: "allocbox-to-stack") {
65
65
let mandatoryAllocBoxToStack = ModulePass ( name: " mandatory-allocbox-to-stack " ) {
66
66
( moduleContext: ModulePassContext ) in
67
67
68
+ var worklist = FunctionWorklist ( )
69
+ worklist. pushIfNotVisited ( contentsOf: moduleContext. functions)
70
+
68
71
var originalsOfSpecializedFunctions = FunctionWorklist ( )
69
72
70
- for function in moduleContext . functions {
73
+ while let function = worklist . pop ( ) {
71
74
moduleContext. transform ( function: function) { context in
72
75
let specFns = tryConvertBoxesToStack ( in: function, context)
73
- originalsOfSpecializedFunctions. pushIfNotVisited ( contentsOf: specFns)
76
+ worklist. pushIfNotVisited ( contentsOf: specFns. specializedFunctions)
77
+ originalsOfSpecializedFunctions. pushIfNotVisited ( contentsOf: specFns. originalFunctions)
74
78
}
75
79
}
76
80
@@ -82,7 +86,7 @@ let mandatoryAllocBoxToStack = ModulePass(name: "mandatory-allocbox-to-stack") {
82
86
/// Converts all non-escaping `alloc_box` to `alloc_stack` and specializes called functions if a
83
87
/// box is passed to a function.
84
88
/// 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 {
86
90
var promotableBoxes = Array < ( AllocBoxInst , Flags ) > ( )
87
91
var functionsToSpecialize = FunctionSpecializations ( )
88
92
@@ -101,7 +105,7 @@ private func tryConvertBoxesToStack(in function: Function, _ context: FunctionPa
101
105
function. fixStackNesting ( context)
102
106
}
103
107
104
- return functionsToSpecialize. originalFunctions
108
+ return functionsToSpecialize
105
109
}
106
110
107
111
private func findPromotableBoxes( in function: Function ,
@@ -187,6 +191,7 @@ private struct FunctionSpecializations {
187
191
private var originalToSpecialized = Dictionary < Function , Function > ( )
188
192
189
193
var originalFunctions : [ Function ] { originals. functions }
194
+ var specializedFunctions : [ Function ] { originals. functions. lazy. map { originalToSpecialized [ $0] ! } }
190
195
191
196
mutating func add( promotableArguments: [ FunctionArgument ] ) {
192
197
for arg in promotableArguments {
0 commit comments