Skip to content

Commit 2ddc0cb

Browse files
committed
Auto merge of #52335 - nnethercote:BitSlice-fixes, r=nikomatsakis
`BitSlice` fixes `propagate_bits_into_entry_set_for` and `BitSlice::bitwise` are hot for some benchmarks under NLL. I tried and failed to speed them up. (Increasing the size of `bit_slice::Word` from `usize` to `u128` caused a slowdown, even though decreasing the size of `bitvec::Word` from `u128` to `u64` also caused a slowdown. Weird.) Anyway, along the way I fixed up several problems in and around the `BitSlice` code. r? @nikomatsakis
2 parents 9d6f4e5 + f2b0b67 commit 2ddc0cb

File tree

6 files changed

+28
-44
lines changed

6 files changed

+28
-44
lines changed

src/librustc_data_structures/bitslice.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ impl BitSlice for [Word] {
2828
fn clear_bit(&mut self, idx: usize) -> bool {
2929
let words = self;
3030
debug!("clear_bit: words={} idx={}",
31-
bits_to_string(words, words.len() * mem::size_of::<Word>()), bit_str(idx));
31+
bits_to_string(words, words.len() * mem::size_of::<Word>() * 8), idx);
3232
let BitLookup { word, bit_in_word, bit_mask } = bit_lookup(idx);
33-
debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, bit_mask);
33+
debug!("word={} bit_in_word={} bit_mask=0x{:x}", word, bit_in_word, bit_mask);
3434
let oldv = words[word];
3535
let newv = oldv & !bit_mask;
3636
words[word] = newv;
@@ -42,7 +42,7 @@ impl BitSlice for [Word] {
4242
fn set_bit(&mut self, idx: usize) -> bool {
4343
let words = self;
4444
debug!("set_bit: words={} idx={}",
45-
bits_to_string(words, words.len() * mem::size_of::<Word>()), bit_str(idx));
45+
bits_to_string(words, words.len() * mem::size_of::<Word>() * 8), idx);
4646
let BitLookup { word, bit_in_word, bit_mask } = bit_lookup(idx);
4747
debug!("word={} bit_in_word={} bit_mask={}", word, bit_in_word, bit_mask);
4848
let oldv = words[word];
@@ -78,13 +78,6 @@ fn bit_lookup(bit: usize) -> BitLookup {
7878
BitLookup { word: word, bit_in_word: bit_in_word, bit_mask: bit_mask }
7979
}
8080

81-
82-
fn bit_str(bit: Word) -> String {
83-
let byte = bit >> 3;
84-
let lobits = 1 << (bit & 0b111);
85-
format!("[{}:{}-{:02x}]", bit, byte, lobits)
86-
}
87-
8881
pub fn bits_to_string(words: &[Word], bits: usize) -> String {
8982
let mut result = String::new();
9083
let mut sep = '[';
@@ -95,7 +88,7 @@ pub fn bits_to_string(words: &[Word], bits: usize) -> String {
9588
let mut i = 0;
9689
for &word in words.iter() {
9790
let mut v = word;
98-
loop { // for each byte in `v`:
91+
for _ in 0..mem::size_of::<Word>() { // for each byte in `v`:
9992
let remain = bits - i;
10093
// If less than a byte remains, then mask just that many bits.
10194
let mask = if remain <= 8 { (1 << remain) - 1 } else { 0xFF };
@@ -110,14 +103,15 @@ pub fn bits_to_string(words: &[Word], bits: usize) -> String {
110103
i += 8;
111104
sep = '-';
112105
}
106+
sep = '|';
113107
}
114108
result.push(']');
115109
return result
116110
}
117111

118112
#[inline]
119-
pub fn bitwise<Op:BitwiseOperator>(out_vec: &mut [usize],
120-
in_vec: &[usize],
113+
pub fn bitwise<Op:BitwiseOperator>(out_vec: &mut [Word],
114+
in_vec: &[Word],
121115
op: &Op) -> bool {
122116
assert_eq!(out_vec.len(), in_vec.len());
123117
let mut changed = false;
@@ -132,21 +126,21 @@ pub fn bitwise<Op:BitwiseOperator>(out_vec: &mut [usize],
132126

133127
pub trait BitwiseOperator {
134128
/// Applies some bit-operation pointwise to each of the bits in the two inputs.
135-
fn join(&self, pred1: usize, pred2: usize) -> usize;
129+
fn join(&self, pred1: Word, pred2: Word) -> Word;
136130
}
137131

138132
pub struct Intersect;
139133
impl BitwiseOperator for Intersect {
140134
#[inline]
141-
fn join(&self, a: usize, b: usize) -> usize { a & b }
135+
fn join(&self, a: Word, b: Word) -> Word { a & b }
142136
}
143137
pub struct Union;
144138
impl BitwiseOperator for Union {
145139
#[inline]
146-
fn join(&self, a: usize, b: usize) -> usize { a | b }
140+
fn join(&self, a: Word, b: Word) -> Word { a | b }
147141
}
148142
pub struct Subtract;
149143
impl BitwiseOperator for Subtract {
150144
#[inline]
151-
fn join(&self, a: usize, b: usize) -> usize { a & !b }
145+
fn join(&self, a: Word, b: Word) -> Word { a & !b }
152146
}

src/librustc_mir/dataflow/impls/borrowed_locals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a, 'tcx> BitDenotation for HaveBeenBorrowedLocals<'a, 'tcx> {
7474

7575
impl<'a, 'tcx> BitwiseOperator for HaveBeenBorrowedLocals<'a, 'tcx> {
7676
#[inline]
77-
fn join(&self, pred1: usize, pred2: usize) -> usize {
77+
fn join(&self, pred1: Word, pred2: Word) -> Word {
7878
pred1 | pred2 // "maybe" means we union effects of both preds
7979
}
8080
}

src/librustc_mir/dataflow/impls/borrows.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::ty::TyCtxt;
2020
use rustc::ty::{RegionKind, RegionVid};
2121
use rustc::ty::RegionKind::ReScope;
2222

23-
use rustc_data_structures::bitslice::BitwiseOperator;
23+
use rustc_data_structures::bitslice::{BitwiseOperator, Word};
2424
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2525
use rustc_data_structures::indexed_set::IdxSet;
2626
use rustc_data_structures::indexed_vec::IndexVec;
@@ -370,7 +370,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
370370

371371
impl<'a, 'gcx, 'tcx> BitwiseOperator for Borrows<'a, 'gcx, 'tcx> {
372372
#[inline]
373-
fn join(&self, pred1: usize, pred2: usize) -> usize {
373+
fn join(&self, pred1: Word, pred2: Word) -> Word {
374374
pred1 | pred2 // union effects of preds when computing reservations
375375
}
376376
}

src/librustc_mir/dataflow/impls/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use rustc::ty::TyCtxt;
1616
use rustc::mir::{self, Mir, Location};
17-
use rustc_data_structures::bitslice::{BitwiseOperator};
17+
use rustc_data_structures::bitslice::{BitwiseOperator, Word};
1818
use rustc_data_structures::indexed_set::{IdxSet};
1919
use rustc_data_structures::indexed_vec::Idx;
2020

@@ -663,35 +663,35 @@ impl<'a, 'gcx, 'tcx> BitDenotation for EverInitializedPlaces<'a, 'gcx, 'tcx> {
663663

664664
impl<'a, 'gcx, 'tcx> BitwiseOperator for MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
665665
#[inline]
666-
fn join(&self, pred1: usize, pred2: usize) -> usize {
666+
fn join(&self, pred1: Word, pred2: Word) -> Word {
667667
pred1 | pred2 // "maybe" means we union effects of both preds
668668
}
669669
}
670670

671671
impl<'a, 'gcx, 'tcx> BitwiseOperator for MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
672672
#[inline]
673-
fn join(&self, pred1: usize, pred2: usize) -> usize {
673+
fn join(&self, pred1: Word, pred2: Word) -> Word {
674674
pred1 | pred2 // "maybe" means we union effects of both preds
675675
}
676676
}
677677

678678
impl<'a, 'gcx, 'tcx> BitwiseOperator for DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
679679
#[inline]
680-
fn join(&self, pred1: usize, pred2: usize) -> usize {
680+
fn join(&self, pred1: Word, pred2: Word) -> Word {
681681
pred1 & pred2 // "definitely" means we intersect effects of both preds
682682
}
683683
}
684684

685685
impl<'a, 'gcx, 'tcx> BitwiseOperator for MovingOutStatements<'a, 'gcx, 'tcx> {
686686
#[inline]
687-
fn join(&self, pred1: usize, pred2: usize) -> usize {
687+
fn join(&self, pred1: Word, pred2: Word) -> Word {
688688
pred1 | pred2 // moves from both preds are in scope
689689
}
690690
}
691691

692692
impl<'a, 'gcx, 'tcx> BitwiseOperator for EverInitializedPlaces<'a, 'gcx, 'tcx> {
693693
#[inline]
694-
fn join(&self, pred1: usize, pred2: usize) -> usize {
694+
fn join(&self, pred1: Word, pred2: Word) -> Word {
695695
pred1 | pred2 // inits from both preds are in scope
696696
}
697697
}

src/librustc_mir/dataflow/impls/storage_liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a, 'tcx> BitDenotation for MaybeStorageLive<'a, 'tcx> {
6969

7070
impl<'a, 'tcx> BitwiseOperator for MaybeStorageLive<'a, 'tcx> {
7171
#[inline]
72-
fn join(&self, pred1: usize, pred2: usize) -> usize {
72+
fn join(&self, pred1: Word, pred2: Word) -> Word {
7373
pred1 | pred2 // "maybe" means we union effects of both preds
7474
}
7575
}

src/librustc_mir/dataflow/mod.rs

+7-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use syntax::ast::{self, MetaItem};
1212

1313
use rustc_data_structures::indexed_set::{IdxSet, IdxSetBuf};
1414
use rustc_data_structures::indexed_vec::Idx;
15-
use rustc_data_structures::bitslice::{bitwise, BitwiseOperator};
15+
use rustc_data_structures::bitslice::{bitwise, BitwiseOperator, Word};
1616
use rustc_data_structures::work_queue::WorkQueue;
1717

1818
use rustc::ty::{self, TyCtxt};
@@ -467,7 +467,7 @@ pub struct AllSets<E: Idx> {
467467
bits_per_block: usize,
468468

469469
/// Number of words associated with each block entry
470-
/// equal to bits_per_block / usize::BITS, rounded up.
470+
/// equal to bits_per_block / (mem::size_of::<Word> * 8), rounded up.
471471
words_per_block: usize,
472472

473473
/// For each block, bits generated by executing the statements in
@@ -734,9 +734,11 @@ impl<'a, 'tcx, D> DataflowAnalysis<'a, 'tcx, D> where D: BitDenotation
734734
dead_unwinds: &'a IdxSet<mir::BasicBlock>,
735735
denotation: D) -> Self where D: InitialFlow {
736736
let bits_per_block = denotation.bits_per_block();
737-
let usize_bits = mem::size_of::<usize>() * 8;
738-
let words_per_block = (bits_per_block + usize_bits - 1) / usize_bits;
739-
let num_overall = Self::num_bits_overall(mir, bits_per_block);
737+
let bits_per_word = mem::size_of::<Word>() * 8;
738+
let words_per_block = (bits_per_block + bits_per_word - 1) / bits_per_word;
739+
let bits_per_block_rounded_up = words_per_block * bits_per_word; // a multiple of word size
740+
let num_blocks = mir.basic_blocks().len();
741+
let num_overall = num_blocks * bits_per_block_rounded_up;
740742

741743
let zeroes = Bits::new(IdxSetBuf::new_empty(num_overall));
742744
let on_entry = Bits::new(if D::bottom_value() {
@@ -774,18 +776,6 @@ impl<'a, 'tcx, D> DataflowAnalysis<'a, 'tcx, D> where D: BitDenotation
774776
}
775777
}
776778
}
777-
778-
fn num_bits_overall(mir: &Mir, bits_per_block: usize) -> usize {
779-
let usize_bits = mem::size_of::<usize>() * 8;
780-
let words_per_block = (bits_per_block + usize_bits - 1) / usize_bits;
781-
782-
// (now rounded up to multiple of word size)
783-
let bits_per_block = words_per_block * usize_bits;
784-
785-
let num_blocks = mir.basic_blocks().len();
786-
let num_overall = num_blocks * bits_per_block;
787-
num_overall
788-
}
789779
}
790780

791781
impl<'a, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D> where D: BitDenotation

0 commit comments

Comments
 (0)