Skip to content

Commit

Permalink
compute required_consts before promotion, and add promoteds that may …
Browse files Browse the repository at this point in the history
…fail
  • Loading branch information
RalfJung committed Mar 12, 2024
1 parent e949b49 commit f205b14
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
17 changes: 9 additions & 8 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ fn mir_promoted(
body.tainted_by_errors = Some(error_reported);
}

// Collect `required_consts` *before* promotion, so if there are any consts being promoted
// we still add them to the list in the outer MIR body.
let mut required_consts = Vec::new();
let mut required_consts_visitor = RequiredConstsVisitor::new(&mut required_consts);
for (bb, bb_data) in traversal::reverse_postorder(&body) {
required_consts_visitor.visit_basic_block_data(bb, bb_data);
}
body.required_consts = required_consts;

// What we need to run borrowck etc.
let promote_pass = promote_consts::PromoteTemps::default();
pm::run_passes(
Expand All @@ -352,14 +361,6 @@ fn mir_promoted(
Some(MirPhase::Analysis(AnalysisPhase::Initial)),
);

// Promotion generates new consts; we run this after promotion to ensure they are accounted for.
let mut required_consts = Vec::new();
let mut required_consts_visitor = RequiredConstsVisitor::new(&mut required_consts);
for (bb, bb_data) in traversal::reverse_postorder(&body) {
required_consts_visitor.visit_basic_block_data(bb, bb_data);
}
body.required_consts = required_consts;

let promoted = promote_pass.promoted_fragments.into_inner();
(tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
}
Expand Down
51 changes: 35 additions & 16 deletions compiler/rustc_mir_transform/src/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {

let promotable_candidates = validate_candidates(&ccx, &mut temps, &all_candidates);

let promoted = promote_candidates(body, tcx, temps, promotable_candidates);
let promoted = promote_candidates(body, tcx, ccx.const_kind, temps, promotable_candidates);
self.promoted_fragments.set(promoted);
}
}
Expand Down Expand Up @@ -657,15 +657,9 @@ impl<'tcx> Validator<'_, 'tcx> {
}
}

// Ideally, we'd stop here and reject the rest.
// But for backward compatibility, we have to accept some promotion in const/static
// initializers. Inline consts are explicitly excluded, they are more recent so we have no
// backwards compatibility reason to allow more promotion inside of them.
let promote_all_fn = matches!(
self.const_kind,
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false })
);
if !promote_all_fn {
// Ideally, we'd stop here and reject the rest. But for backward compatibility, we have to
// accept some promotion in const/static initializers.
if !promote_all_fn(self.ccx.const_kind) {
return Err(Unpromotable);
}
// Make sure the callee is a `const fn`.
Expand All @@ -687,6 +681,18 @@ impl<'tcx> Validator<'_, 'tcx> {
}
}

/// Decides whether in this context we are okay with promoting all functions (and not just
/// `#[rustc_promotable]`).
/// For backwards compatibility, we do that in static/const initializers.
fn promote_all_fn(const_kind: Option<hir::ConstContext>) -> bool {
// Inline consts are explicitly excluded, they are more recent so we have no
// backwards compatibility reason to allow more promotion inside of them.
matches!(
const_kind,
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false })
)
}

// FIXME(eddyb) remove the differences for promotability in `static`, `const`, `const fn`.
fn validate_candidates(
ccx: &ConstCx<'_, '_>,
Expand All @@ -712,6 +718,9 @@ struct Promoter<'a, 'tcx> {
/// If true, all nested temps are also kept in the
/// source MIR, not moved to the promoted MIR.
keep_original: bool,

/// If true, add the new const (the promoted) to the required_consts of the parent MIR.
add_to_required: bool,
}

impl<'a, 'tcx> Promoter<'a, 'tcx> {
Expand Down Expand Up @@ -858,11 +867,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
let args = tcx.erase_regions(GenericArgs::identity_for_item(tcx, def));
let uneval = mir::UnevaluatedConst { def, args, promoted: Some(promoted_id) };

Operand::Constant(Box::new(ConstOperand {
span,
user_ty: None,
const_: Const::Unevaluated(uneval, ty),
}))
ConstOperand { span, user_ty: None, const_: Const::Unevaluated(uneval, ty) }
};

let blocks = self.source.basic_blocks.as_mut();
Expand Down Expand Up @@ -898,11 +903,15 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
let promoted_ref = local_decls.push(promoted_ref);
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);

let promoted_operand = promoted_operand(ref_ty, span);
if self.add_to_required {
self.source.required_consts.push(promoted_operand);
}
let promoted_ref_statement = Statement {
source_info: statement.source_info,
kind: StatementKind::Assign(Box::new((
Place::from(promoted_ref),
Rvalue::Use(promoted_operand(ref_ty, span)),
Rvalue::Use(Operand::Constant(Box::new(promoted_operand))),
))),
};
self.extra_statements.push((loc, promoted_ref_statement));
Expand Down Expand Up @@ -945,6 +954,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
fn promote_candidates<'tcx>(
body: &mut Body<'tcx>,
tcx: TyCtxt<'tcx>,
ccx_const_kind: Option<hir::ConstContext>,
mut temps: IndexVec<Local, TempState>,
candidates: Vec<Candidate>,
) -> IndexVec<Promoted, Body<'tcx>> {
Expand Down Expand Up @@ -984,6 +994,11 @@ fn promote_candidates<'tcx>(
None,
body.tainted_by_errors,
);
// We keep `required_consts` of the new MIR body empty. All consts mentioned here have
// already been added to the parent MIR's `required_consts` (that is computed before
// promotion), and no matter where this promoted const ends up, our parent MIR must be
// somewhere in the reachable dependency chain so we can rely on its required consts being
// evaluated.
promoted.phase = MirPhase::Analysis(AnalysisPhase::Initial);

let promoter = Promoter {
Expand All @@ -993,6 +1008,10 @@ fn promote_candidates<'tcx>(
temps: &mut temps,
extra_statements: &mut extra_statements,
keep_original: false,
// If we promote all functions, some of these promoteds could fail, so we better make
// sure they get all checked as `required_consts`. Otherwise, as an optimization we
// *don't* add the promoteds to the parent's `required_consts`.
add_to_required: promote_all_fn(ccx_const_kind),
};

let mut promoted = promoter.promote_candidate(candidate, promotions.len());
Expand Down

0 comments on commit f205b14

Please sign in to comment.