Skip to content

Commit 4573a4a

Browse files
committed
Auto merge of #86383 - shamatar:slice_len_lowering, r=bjorn3
Add MIR pass to lower call to `core::slice::len` into `Len` operand During some larger experiment with range analysis I've found that code like `let l = slice.len()` produces different MIR then one found in bound checks. This optimization pass replaces terminators that are calls to `core::slice::len` with just a MIR operand and Goto terminator. It uses some heuristics to remove the outer borrow that is made to call `core::slice::len`, but I assume it can be eliminated, just didn't find how. Would like to express my gratitude to `@oli-obk` who helped me a lot on Zullip
2 parents 406d4a9 + aa53928 commit 4573a4a

File tree

8 files changed

+185
-1
lines changed

8 files changed

+185
-1
lines changed

compiler/rustc_hir/src/lang_items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ language_item_table! {
310310

311311
Try, sym::Try, try_trait, Target::Trait;
312312

313+
SliceLen, sym::slice_len_fn, slice_len_fn, Target::Method(MethodKind::Inherent);
314+
313315
// Language items from AST lowering
314316
TryTraitFromResidual, sym::from_residual, from_residual_fn, Target::Method(MethodKind::Trait { body: false });
315317
TryTraitFromOutput, sym::from_output, from_output_fn, Target::Method(MethodKind::Trait { body: false });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//! This pass lowers calls to core::slice::len to just Len op.
2+
//! It should run before inlining!
3+
4+
use crate::transform::MirPass;
5+
use rustc_hir::def_id::DefId;
6+
use rustc_index::vec::IndexVec;
7+
use rustc_middle::mir::*;
8+
use rustc_middle::ty::{self, TyCtxt};
9+
10+
pub struct LowerSliceLenCalls;
11+
12+
impl<'tcx> MirPass<'tcx> for LowerSliceLenCalls {
13+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
14+
lower_slice_len_calls(tcx, body)
15+
}
16+
}
17+
18+
pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
19+
let language_items = tcx.lang_items();
20+
let slice_len_fn_item_def_id = if let Some(slice_len_fn_item) = language_items.slice_len_fn() {
21+
slice_len_fn_item
22+
} else {
23+
// there is no language item to compare to :)
24+
return;
25+
};
26+
27+
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
28+
29+
for block in basic_blocks {
30+
// lower `<[_]>::len` calls
31+
lower_slice_len_call(tcx, block, &*local_decls, slice_len_fn_item_def_id);
32+
}
33+
}
34+
35+
struct SliceLenPatchInformation<'tcx> {
36+
add_statement: Statement<'tcx>,
37+
new_terminator_kind: TerminatorKind<'tcx>,
38+
}
39+
40+
fn lower_slice_len_call<'tcx>(
41+
tcx: TyCtxt<'tcx>,
42+
block: &mut BasicBlockData<'tcx>,
43+
local_decls: &IndexVec<Local, LocalDecl<'tcx>>,
44+
slice_len_fn_item_def_id: DefId,
45+
) {
46+
let mut patch_found: Option<SliceLenPatchInformation<'_>> = None;
47+
48+
let terminator = block.terminator();
49+
match &terminator.kind {
50+
TerminatorKind::Call {
51+
func,
52+
args,
53+
destination: Some((dest, bb)),
54+
cleanup: None,
55+
from_hir_call: true,
56+
..
57+
} => {
58+
// some heuristics for fast rejection
59+
if args.len() != 1 {
60+
return;
61+
}
62+
let arg = match args[0].place() {
63+
Some(arg) => arg,
64+
None => return,
65+
};
66+
let func_ty = func.ty(local_decls, tcx);
67+
match func_ty.kind() {
68+
ty::FnDef(fn_def_id, _) if fn_def_id == &slice_len_fn_item_def_id => {
69+
// perform modifications
70+
// from something like `_5 = core::slice::<impl [u8]>::len(move _6) -> bb1`
71+
// into `_5 = Len(*_6)
72+
// goto bb1
73+
74+
// make new RValue for Len
75+
let deref_arg = tcx.mk_place_deref(arg);
76+
let r_value = Rvalue::Len(deref_arg);
77+
let len_statement_kind = StatementKind::Assign(Box::new((*dest, r_value)));
78+
let add_statement = Statement {
79+
kind: len_statement_kind,
80+
source_info: terminator.source_info.clone(),
81+
};
82+
83+
// modify terminator into simple Goto
84+
let new_terminator_kind = TerminatorKind::Goto { target: bb.clone() };
85+
86+
let patch = SliceLenPatchInformation { add_statement, new_terminator_kind };
87+
88+
patch_found = Some(patch);
89+
}
90+
_ => {}
91+
}
92+
}
93+
_ => {}
94+
}
95+
96+
if let Some(SliceLenPatchInformation { add_statement, new_terminator_kind }) = patch_found {
97+
block.statements.push(add_statement);
98+
block.terminator_mut().kind = new_terminator_kind;
99+
}
100+
}

compiler/rustc_mir/src/transform/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub mod generator;
3636
pub mod inline;
3737
pub mod instcombine;
3838
pub mod lower_intrinsics;
39+
pub mod lower_slice_len;
3940
pub mod match_branches;
4041
pub mod multiple_return_terminators;
4142
pub mod no_landing_pads;
@@ -479,6 +480,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
479480
// to them. We run some optimizations before that, because they may be harder to do on the state
480481
// machine than on MIR with async primitives.
481482
let optimizations_with_generators: &[&dyn MirPass<'tcx>] = &[
483+
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
482484
&unreachable_prop::UnreachablePropagation,
483485
&uninhabited_enum_branching::UninhabitedEnumBranching,
484486
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ symbols! {
680680
lateout,
681681
lazy_normalization_consts,
682682
le,
683+
len,
683684
let_chains,
684685
lhs,
685686
lib,
@@ -1147,6 +1148,7 @@ symbols! {
11471148
skip,
11481149
slice,
11491150
slice_alloc,
1151+
slice_len_fn,
11501152
slice_patterns,
11511153
slice_u8,
11521154
slice_u8_alloc,

library/core/src/slice/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl<T> [T] {
9696
/// assert_eq!(a.len(), 3);
9797
/// ```
9898
#[doc(alias = "length")]
99+
#[cfg_attr(not(bootstrap), lang = "slice_len_fn")]
99100
#[stable(feature = "rust1", since = "1.0.0")]
100101
#[rustc_const_stable(feature = "const_slice_len", since = "1.39.0")]
101102
#[inline]

library/std/src/thread/local/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ fn join_orders_after_tls_destructors() {
297297
.unwrap();
298298

299299
loop {
300-
match SYNC_STATE.compare_exchange_weak(
300+
match SYNC_STATE.compare_exchange(
301301
THREAD1_WAITING,
302302
MAIN_THREAD_RENDEZVOUS,
303303
Ordering::SeqCst,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
- // MIR for `bound` before LowerSliceLenCalls
2+
+ // MIR for `bound` after LowerSliceLenCalls
3+
4+
fn bound(_1: usize, _2: &[u8]) -> u8 {
5+
debug index => _1; // in scope 0 at $DIR/lower_slice_len.rs:4:14: 4:19
6+
debug slice => _2; // in scope 0 at $DIR/lower_slice_len.rs:4:28: 4:33
7+
let mut _0: u8; // return place in scope 0 at $DIR/lower_slice_len.rs:4:45: 4:47
8+
let mut _3: bool; // in scope 0 at $DIR/lower_slice_len.rs:5:8: 5:27
9+
let mut _4: usize; // in scope 0 at $DIR/lower_slice_len.rs:5:8: 5:13
10+
let mut _5: usize; // in scope 0 at $DIR/lower_slice_len.rs:5:16: 5:27
11+
let mut _6: &[u8]; // in scope 0 at $DIR/lower_slice_len.rs:5:16: 5:21
12+
let _7: usize; // in scope 0 at $DIR/lower_slice_len.rs:6:15: 6:20
13+
let mut _8: usize; // in scope 0 at $DIR/lower_slice_len.rs:6:9: 6:21
14+
let mut _9: bool; // in scope 0 at $DIR/lower_slice_len.rs:6:9: 6:21
15+
16+
bb0: {
17+
StorageLive(_3); // scope 0 at $DIR/lower_slice_len.rs:5:8: 5:27
18+
StorageLive(_4); // scope 0 at $DIR/lower_slice_len.rs:5:8: 5:13
19+
_4 = _1; // scope 0 at $DIR/lower_slice_len.rs:5:8: 5:13
20+
StorageLive(_5); // scope 0 at $DIR/lower_slice_len.rs:5:16: 5:27
21+
StorageLive(_6); // scope 0 at $DIR/lower_slice_len.rs:5:16: 5:21
22+
_6 = &(*_2); // scope 0 at $DIR/lower_slice_len.rs:5:16: 5:21
23+
- _5 = core::slice::<impl [u8]>::len(move _6) -> bb1; // scope 0 at $DIR/lower_slice_len.rs:5:16: 5:27
24+
- // mir::Constant
25+
- // + span: $DIR/lower_slice_len.rs:5:22: 5:25
26+
- // + literal: Const { ty: for<'r> fn(&'r [u8]) -> usize {core::slice::<impl [u8]>::len}, val: Value(Scalar(<ZST>)) }
27+
+ _5 = Len((*_6)); // scope 0 at $DIR/lower_slice_len.rs:5:16: 5:27
28+
+ goto -> bb1; // scope 0 at $DIR/lower_slice_len.rs:5:16: 5:27
29+
}
30+
31+
bb1: {
32+
StorageDead(_6); // scope 0 at $DIR/lower_slice_len.rs:5:26: 5:27
33+
_3 = Lt(move _4, move _5); // scope 0 at $DIR/lower_slice_len.rs:5:8: 5:27
34+
StorageDead(_5); // scope 0 at $DIR/lower_slice_len.rs:5:26: 5:27
35+
StorageDead(_4); // scope 0 at $DIR/lower_slice_len.rs:5:26: 5:27
36+
switchInt(move _3) -> [false: bb3, otherwise: bb2]; // scope 0 at $DIR/lower_slice_len.rs:5:5: 9:6
37+
}
38+
39+
bb2: {
40+
StorageLive(_7); // scope 0 at $DIR/lower_slice_len.rs:6:15: 6:20
41+
_7 = _1; // scope 0 at $DIR/lower_slice_len.rs:6:15: 6:20
42+
_8 = Len((*_2)); // scope 0 at $DIR/lower_slice_len.rs:6:9: 6:21
43+
_9 = Lt(_7, _8); // scope 0 at $DIR/lower_slice_len.rs:6:9: 6:21
44+
assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> bb4; // scope 0 at $DIR/lower_slice_len.rs:6:9: 6:21
45+
}
46+
47+
bb3: {
48+
_0 = const 42_u8; // scope 0 at $DIR/lower_slice_len.rs:8:9: 8:11
49+
goto -> bb5; // scope 0 at $DIR/lower_slice_len.rs:5:5: 9:6
50+
}
51+
52+
bb4: {
53+
_0 = (*_2)[_7]; // scope 0 at $DIR/lower_slice_len.rs:6:9: 6:21
54+
StorageDead(_7); // scope 0 at $DIR/lower_slice_len.rs:7:5: 7:6
55+
goto -> bb5; // scope 0 at $DIR/lower_slice_len.rs:5:5: 9:6
56+
}
57+
58+
bb5: {
59+
StorageDead(_3); // scope 0 at $DIR/lower_slice_len.rs:9:5: 9:6
60+
return; // scope 0 at $DIR/lower_slice_len.rs:10:2: 10:2
61+
}
62+
}
63+

src/test/mir-opt/lower_slice_len.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: -Z mir-opt-level=3
2+
3+
// EMIT_MIR lower_slice_len.bound.LowerSliceLenCalls.diff
4+
pub fn bound(index: usize, slice: &[u8]) -> u8 {
5+
if index < slice.len() {
6+
slice[index]
7+
} else {
8+
42
9+
}
10+
}
11+
12+
fn main() {
13+
let _ = bound(1, &[1, 2, 3]);
14+
}

0 commit comments

Comments
 (0)