@@ -8,7 +8,7 @@ use rustc_hir::def_id::DefId;
88use rustc_middle:: mir:: { Body , MirDumper , MirPhase , RuntimePhase } ;
99use rustc_middle:: ty:: TyCtxt ;
1010use rustc_session:: Session ;
11- use rustc_session:: config:: OptLevel ;
11+ use rustc_session:: config:: { OptLevel , OptimizationObjective } ;
1212use rustc_span:: bug;
1313use tracing:: trace;
1414
@@ -96,13 +96,31 @@ pub(crate) enum PassPolicy {
9696 /// Whether this pass should be enabled in the absence of an explicit
9797 /// `-Zmir-enable-passes` override.
9898 enabled_by_default : bool ,
99+ /// Whether this pass trades runtime speed for binary size, and so is enabled only when the
100+ /// objective is size.
101+ size_only : bool ,
99102 } ,
100103}
101104
102105impl PassPolicy {
103106 /// Create a [`PassPolicy::Optional`] enabled by default under the given condition.
104107 pub ( crate ) fn optional ( enabled_by_default : bool ) -> Self {
105- Self :: Optional { enabled_by_default }
108+ Self :: Optional { enabled_by_default, size_only : false }
109+ }
110+
111+ /// Create a [`PassPolicy::Optional`] that is enabled only when size is the objective.
112+ pub ( crate ) fn size_only ( ) -> Self {
113+ Self :: Optional { enabled_by_default : false , size_only : true }
114+ }
115+
116+ fn runs_by_default ( self , ctx : & PassCtx < ' _ > ) -> bool {
117+ match self {
118+ PassPolicy :: Required => true ,
119+ PassPolicy :: Optional { enabled_by_default, size_only } => {
120+ enabled_by_default
121+ || ( size_only && ctx. optimization_objective ( ) == OptimizationObjective :: Size )
122+ }
123+ }
106124 }
107125}
108126
@@ -151,10 +169,18 @@ impl<'sess> PassCtx<'sess> {
151169 Self { session : tcx. sess , body_mir_opt_level }
152170 }
153171
154- /// The effective MIR optimization level for this body, including `#[optimize]` overrides.
172+ /// The MIR optimization level for this body, possibly overridden by `#[optimize]`.
173+ ///
174+ /// A pass deciding whether the compilation wants it should ask
175+ /// [`session_mir_opt_level`](Self::session_mir_opt_level) instead.
155176 pub ( super ) fn mir_opt_level ( & self ) -> usize {
156177 self . body_mir_opt_level
157178 }
179+
180+ /// The compilation's MIR optimization level, ignoring this body's `#[optimize]` override.
181+ pub ( super ) fn session_mir_opt_level ( & self ) -> usize {
182+ self . session . mir_opt_level ( )
183+ }
158184}
159185
160186impl std:: ops:: Deref for PassCtx < ' _ > {
@@ -218,8 +244,9 @@ where
218244 let policy = self . 1 . policy ( ctx) ;
219245 match policy {
220246 PassPolicy :: Required => bug ! ( "required pass cannot be gated by an opt level" ) ,
221- PassPolicy :: Optional { enabled_by_default } => PassPolicy :: Optional {
247+ PassPolicy :: Optional { enabled_by_default, size_only } => PassPolicy :: Optional {
222248 enabled_by_default : enabled_by_default && ctx. mir_opt_level ( ) >= self . 0 ,
249+ size_only,
223250 } ,
224251 }
225252 }
@@ -260,21 +287,26 @@ where
260287 . find_map ( |( name_, polarity) | if name == name_ { Some ( * polarity) } else { None } )
261288 } ;
262289
263- match pass. policy ( ctx) {
264- PassPolicy :: Required => true ,
265- PassPolicy :: Optional { enabled_by_default } => {
266- if let Some ( o) = pass_override ( ) {
267- trace ! (
268- pass = %name,
269- "{} as requested by flag" ,
270- if o { "Running" } else { "Not running" }
271- ) ;
272- o
273- } else {
274- enabled_by_default
275- }
276- }
290+ let policy = pass. policy ( ctx) ;
291+
292+ // Only `Optional` passes consult `-Zmir-enable-passes`; `Required` ones implement MIR semantics
293+ // and cannot be turned off.
294+ if let PassPolicy :: Optional { .. } = policy
295+ && let Some ( o) = pass_override ( )
296+ {
297+ trace ! (
298+ pass = %name,
299+ "{} as requested by flag" ,
300+ if o { "Running" } else { "Not running" }
301+ ) ;
302+ return o;
303+ }
304+
305+ let runs = policy. runs_by_default ( ctx) ;
306+ if !runs {
307+ trace ! ( pass = %name, "Not running: not enabled by default for this body" ) ;
277308 }
309+ runs
278310}
279311
280312fn run_passes_inner < ' tcx > (
0 commit comments