@@ -118,18 +118,81 @@ use crate::errors::{
118118 CouldntDumpMonoStats , SymbolAlreadyDefined , UnknownCguCollectionMode , UnknownPartitionStrategy ,
119119} ;
120120
121+ enum Partitioner {
122+ Default ( default:: DefaultPartitioning ) ,
123+ // Other partitioning strategies can go here.
124+ Unknown ,
125+ }
126+
127+ impl < ' tcx > Partition < ' tcx > for Partitioner {
128+ fn place_root_mono_items < I > (
129+ & mut self ,
130+ cx : & PartitioningCx < ' _ , ' tcx > ,
131+ mono_items : & mut I ,
132+ ) -> PreInliningPartitioning < ' tcx >
133+ where
134+ I : Iterator < Item = MonoItem < ' tcx > > ,
135+ {
136+ match self {
137+ Partitioner :: Default ( partitioner) => partitioner. place_root_mono_items ( cx, mono_items) ,
138+ Partitioner :: Unknown => cx. tcx . sess . emit_fatal ( UnknownPartitionStrategy ) ,
139+ }
140+ }
141+
142+ fn merge_codegen_units (
143+ & mut self ,
144+ cx : & PartitioningCx < ' _ , ' tcx > ,
145+ initial_partitioning : & mut PreInliningPartitioning < ' tcx > ,
146+ ) {
147+ match self {
148+ Partitioner :: Default ( partitioner) => {
149+ partitioner. merge_codegen_units ( cx, initial_partitioning)
150+ }
151+ Partitioner :: Unknown => cx. tcx . sess . emit_fatal ( UnknownPartitionStrategy ) ,
152+ }
153+ }
154+
155+ fn place_inlined_mono_items (
156+ & mut self ,
157+ cx : & PartitioningCx < ' _ , ' tcx > ,
158+ initial_partitioning : PreInliningPartitioning < ' tcx > ,
159+ ) -> PostInliningPartitioning < ' tcx > {
160+ match self {
161+ Partitioner :: Default ( partitioner) => {
162+ partitioner. place_inlined_mono_items ( cx, initial_partitioning)
163+ }
164+ Partitioner :: Unknown => cx. tcx . sess . emit_fatal ( UnknownPartitionStrategy ) ,
165+ }
166+ }
167+
168+ fn internalize_symbols (
169+ & mut self ,
170+ cx : & PartitioningCx < ' _ , ' tcx > ,
171+ post_inlining_partitioning : & mut PostInliningPartitioning < ' tcx > ,
172+ ) {
173+ match self {
174+ Partitioner :: Default ( partitioner) => {
175+ partitioner. internalize_symbols ( cx, post_inlining_partitioning)
176+ }
177+ Partitioner :: Unknown => cx. tcx . sess . emit_fatal ( UnknownPartitionStrategy ) ,
178+ }
179+ }
180+ }
181+
121182pub struct PartitioningCx < ' a , ' tcx > {
122183 tcx : TyCtxt < ' tcx > ,
123184 target_cgu_count : usize ,
124185 inlining_map : & ' a InliningMap < ' tcx > ,
125186}
126187
127- trait Partitioner < ' tcx > {
128- fn place_root_mono_items (
188+ trait Partition < ' tcx > {
189+ fn place_root_mono_items < I > (
129190 & mut self ,
130191 cx : & PartitioningCx < ' _ , ' tcx > ,
131- mono_items : & mut dyn Iterator < Item = MonoItem < ' tcx > > ,
132- ) -> PreInliningPartitioning < ' tcx > ;
192+ mono_items : & mut I ,
193+ ) -> PreInliningPartitioning < ' tcx >
194+ where
195+ I : Iterator < Item = MonoItem < ' tcx > > ;
133196
134197 fn merge_codegen_units (
135198 & mut self ,
@@ -150,26 +213,27 @@ trait Partitioner<'tcx> {
150213 ) ;
151214}
152215
153- fn get_partitioner < ' tcx > ( tcx : TyCtxt < ' tcx > ) -> Box < dyn Partitioner < ' tcx > > {
216+ fn get_partitioner ( tcx : TyCtxt < ' _ > ) -> Partitioner {
154217 let strategy = match & tcx. sess . opts . unstable_opts . cgu_partitioning_strategy {
155218 None => "default" ,
156219 Some ( s) => & s[ ..] ,
157220 } ;
158221
159222 match strategy {
160- "default" => Box :: new ( default:: DefaultPartitioning ) ,
161- _ => {
162- tcx. sess . emit_fatal ( UnknownPartitionStrategy ) ;
163- }
223+ "default" => Partitioner :: Default ( default:: DefaultPartitioning ) ,
224+ _ => Partitioner :: Unknown ,
164225 }
165226}
166227
167- pub fn partition < ' tcx > (
228+ pub fn partition < ' tcx , I > (
168229 tcx : TyCtxt < ' tcx > ,
169- mono_items : & mut dyn Iterator < Item = MonoItem < ' tcx > > ,
230+ mono_items : & mut I ,
170231 max_cgu_count : usize ,
171232 inlining_map : & InliningMap < ' tcx > ,
172- ) -> Vec < CodegenUnit < ' tcx > > {
233+ ) -> Vec < CodegenUnit < ' tcx > >
234+ where
235+ I : Iterator < Item = MonoItem < ' tcx > > ,
236+ {
173237 let _prof_timer = tcx. prof . generic_activity ( "cgu_partitioning" ) ;
174238
175239 let mut partitioner = get_partitioner ( tcx) ;
@@ -182,7 +246,9 @@ pub fn partition<'tcx>(
182246 partitioner. place_root_mono_items ( cx, mono_items)
183247 } ;
184248
185- initial_partitioning. codegen_units . iter_mut ( ) . for_each ( |cgu| cgu. create_size_estimate ( tcx) ) ;
249+ for cgu in & mut initial_partitioning. codegen_units {
250+ cgu. create_size_estimate ( tcx) ;
251+ }
186252
187253 debug_dump ( tcx, "INITIAL PARTITIONING:" , initial_partitioning. codegen_units . iter ( ) ) ;
188254
@@ -202,7 +268,9 @@ pub fn partition<'tcx>(
202268 partitioner. place_inlined_mono_items ( cx, initial_partitioning)
203269 } ;
204270
205- post_inlining. codegen_units . iter_mut ( ) . for_each ( |cgu| cgu. create_size_estimate ( tcx) ) ;
271+ for cgu in & mut post_inlining. codegen_units {
272+ cgu. create_size_estimate ( tcx) ;
273+ }
206274
207275 debug_dump ( tcx, "POST INLINING:" , post_inlining. codegen_units . iter ( ) ) ;
208276
@@ -380,7 +448,7 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co
380448 || {
381449 let mut codegen_units = partition (
382450 tcx,
383- & mut items. iter ( ) . cloned ( ) ,
451+ & mut items. iter ( ) . copied ( ) ,
384452 tcx. sess . codegen_units ( ) ,
385453 & inlining_map,
386454 ) ;
0 commit comments