Skip to content

Commit 362c9e2

Browse files
authored
Rollup merge of rust-lang#147873 - RalfJung:deduce_param_attrs, r=tmiasko
comments for deduce_param_attrs Cc `@saethlin` since IIRC you experimented with codegen doing post-mono MIR ops? That seems to be in conflict with this pass. Cc `@tmiasko` r? `@scottmcm`
2 parents 3f1d4fb + 82f6c60 commit 362c9e2

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
180180
let llfn = cx.get_fn(instance);
181181

182182
let mut mir = tcx.instance_mir(instance.def);
183+
// Note that the ABI logic has deduced facts about the functions' parameters based on the MIR we
184+
// got here (`deduce_param_attrs`). That means we can *not* apply arbitrary further MIR
185+
// transforms as that may invalidate those deduced facts!
183186

184187
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
185188
debug!("fn_abi: {:?}", fn_abi);
@@ -317,6 +320,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
317320
}
318321
}
319322

323+
/// Replace `clone` calls that come from `use` statements with direct copies if possible.
320324
// FIXME: Move this function to mir::transform when post-mono MIR passes land.
321325
fn optimize_use_clone<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
322326
cx: &'a Bx::CodegenCx,

compiler/rustc_middle/src/middle/deduced_param_attrs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_macros::{Decodable, Encodable, HashStable};
33
use crate::ty::{Ty, TyCtxt, TypingEnv};
44

55
/// Flags that dictate how a parameter is mutated. If the flags are empty, the param is
6-
/// read-only. If non-empty, it is read-only with conditions.
6+
/// read-only. If non-empty, it is read-only if *all* flags' conditions are met.
77
#[derive(Clone, Copy, PartialEq, Debug, Decodable, Encodable, HashStable)]
88
pub struct DeducedReadOnlyParam(u8);
99

@@ -53,6 +53,7 @@ impl DeducedParamAttrs {
5353
ty: Ty<'tcx>,
5454
) -> bool {
5555
let read_only = self.read_only;
56+
// We have to check *all* set bits; only if all checks pass is this truly read-only.
5657
if read_only.contains(DeducedReadOnlyParam::MUTATED) {
5758
return false;
5859
}

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
//! body of the function instead of just the signature. These can be useful for optimization
55
//! purposes on a best-effort basis. We compute them here and store them into the crate metadata so
66
//! dependent crates can use them.
7+
//!
8+
//! Note that this *crucially* relies on codegen *not* doing any more MIR-level transformations
9+
//! after `optimized_mir`! We check for things that are *not* guaranteed to be preserved by MIR
10+
//! transforms, such as which local variables happen to be mutated.
711
812
use rustc_hir::def_id::LocalDefId;
913
use rustc_index::IndexVec;

0 commit comments

Comments
 (0)