Skip to content

Commit fbd8f95

Browse files
committed
Auto merge of #146292 - matthiaskrgr:rollup-bkjs887, r=matthiaskrgr
Rollup of 2 pull requests Successful merges: - #146254 (Use `Itertools::all_equal_value()` where applicable) - #146290 (Revert "Add LSX accelerated implementation for source file analysis") r? `@ghost` `@rustbot` modify labels: rollup
2 parents ebdf2ab + 1b492b0 commit fbd8f95

File tree

7 files changed

+38
-153
lines changed

7 files changed

+38
-153
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use itertools::Itertools as _;
12
use rustc_abi::{self as abi, FIRST_VARIANT};
23
use rustc_middle::ty::adjustment::PointerCoercion;
34
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
@@ -111,14 +112,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
111112
let size = bx.const_usize(dest.layout.size.bytes());
112113

113114
// Use llvm.memset.p0i8.* to initialize all same byte arrays
114-
if let Some(int) = bx.cx().const_to_opt_u128(v, false) {
115-
let bytes = &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()];
116-
let first = bytes[0];
117-
if bytes[1..].iter().all(|&b| b == first) {
118-
let fill = bx.cx().const_u8(first);
119-
bx.memset(start, fill, size, dest.val.align, MemFlags::empty());
120-
return true;
121-
}
115+
if let Some(int) = bx.cx().const_to_opt_u128(v, false)
116+
&& let bytes = &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()]
117+
&& let Ok(&byte) = bytes.iter().all_equal_value()
118+
{
119+
let fill = bx.cx().const_u8(byte);
120+
bx.memset(start, fill, size, dest.val.align, MemFlags::empty());
121+
return true;
122122
}
123123

124124
// Use llvm.memset.p0i8.* to initialize byte arrays
@@ -130,13 +130,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
130130
false
131131
};
132132

133-
match cg_elem.val {
134-
OperandValue::Immediate(v) => {
135-
if try_init_all_same(bx, v) {
136-
return;
137-
}
138-
}
139-
_ => (),
133+
if let OperandValue::Immediate(v) = cg_elem.val
134+
&& try_init_all_same(bx, v)
135+
{
136+
return;
140137
}
141138

142139
let count = self

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
use std::borrow::Cow;
8888

8989
use either::Either;
90+
use itertools::Itertools as _;
9091
use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
9192
use rustc_const_eval::const_eval::DummyMachine;
9293
use rustc_const_eval::interpret::{
@@ -1023,15 +1024,15 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10231024
}
10241025
};
10251026

1026-
if ty.is_array() && fields.len() > 4 {
1027-
let first = fields[0];
1028-
if fields.iter().all(|&v| v == first) {
1029-
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
1030-
if let Some(op) = self.try_as_operand(first, location) {
1031-
*rvalue = Rvalue::Repeat(op, len);
1032-
}
1033-
return Some(self.insert(ty, Value::Repeat(first, len)));
1027+
if ty.is_array()
1028+
&& fields.len() > 4
1029+
&& let Ok(&first) = fields.iter().all_equal_value()
1030+
{
1031+
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
1032+
if let Some(op) = self.try_as_operand(first, location) {
1033+
*rvalue = Rvalue::Repeat(op, len);
10341034
}
1035+
return Some(self.insert(ty, Value::Repeat(first, len)));
10351036
}
10361037

10371038
if let Some(value) =

compiler/rustc_mir_transform/src/lint_tail_expr_drop_order.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cell::RefCell;
22
use std::collections::hash_map;
33
use std::rc::Rc;
44

5+
use itertools::Itertools as _;
56
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
67
use rustc_data_structures::unord::{UnordMap, UnordSet};
78
use rustc_errors::Subdiagnostic;
@@ -339,9 +340,9 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
339340
// Suppose that all BIDs point into the same local,
340341
// we can remove the this local from the observed drops,
341342
// so that we can focus our diagnosis more on the others.
342-
if candidates.iter().all(|&(_, place)| candidates[0].1.local == place.local) {
343+
if let Ok(local) = candidates.iter().map(|&(_, place)| place.local).all_equal_value() {
343344
for path_idx in all_locals_dropped.iter() {
344-
if move_data.move_paths[path_idx].place.local == candidates[0].1.local {
345+
if move_data.move_paths[path_idx].place.local == local {
345346
to_exclude.insert(path_idx);
346347
}
347348
}

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//! The normal logic that a program with UB can be changed to do anything does not apply to
3535
//! pre-"runtime" MIR!
3636
37+
use itertools::Itertools as _;
3738
use rustc_index::{Idx, IndexSlice, IndexVec};
3839
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
3940
use rustc_middle::mir::*;
@@ -288,20 +289,13 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
288289
return false;
289290
};
290291

291-
let first_succ = {
292-
if let Some(first_succ) = terminator.successors().next() {
293-
if terminator.successors().all(|s| s == first_succ) {
294-
let count = terminator.successors().count();
295-
self.pred_count[first_succ] -= (count - 1) as u32;
296-
first_succ
297-
} else {
298-
return false;
299-
}
300-
} else {
301-
return false;
302-
}
292+
let Ok(first_succ) = terminator.successors().all_equal_value() else {
293+
return false;
303294
};
304295

296+
let count = terminator.successors().count();
297+
self.pred_count[first_succ] -= (count - 1) as u32;
298+
305299
debug!("simplifying branch {:?}", terminator);
306300
terminator.kind = TerminatorKind::Goto { target: first_succ };
307301
true

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use itertools::Itertools as _;
12
use rustc_ast::visit::{self, Visitor};
23
use rustc_ast::{
34
self as ast, CRATE_NODE_ID, Crate, ItemKind, ModKind, NodeId, Path, join_path_idents,
@@ -3469,16 +3470,11 @@ fn show_candidates(
34693470
err.note(note.to_string());
34703471
}
34713472
} else {
3472-
let (_, descr_first, _, _, _) = &inaccessible_path_strings[0];
3473-
let descr = if inaccessible_path_strings
3473+
let descr = inaccessible_path_strings
34743474
.iter()
3475-
.skip(1)
3476-
.all(|(_, descr, _, _, _)| descr == descr_first)
3477-
{
3478-
descr_first
3479-
} else {
3480-
"item"
3481-
};
3475+
.map(|&(_, descr, _, _, _)| descr)
3476+
.all_equal_value()
3477+
.unwrap_or("item");
34823478
let plural_descr =
34833479
if descr.ends_with('s') { format!("{descr}es") } else { format!("{descr}s") };
34843480

compiler/rustc_span/src/analyze_source_file.rs

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ cfg_select! {
8181
// use `loadu`, which supports unaligned loading.
8282
let chunk = unsafe { _mm_loadu_si128(chunk.as_ptr() as *const __m128i) };
8383

84-
// For each character in the chunk, see if its byte value is < 0,
85-
// which indicates that it's part of a UTF-8 char.
84+
// For character in the chunk, see if its byte value is < 0, which
85+
// indicates that it's part of a UTF-8 char.
8686
let multibyte_test = _mm_cmplt_epi8(chunk, _mm_set1_epi8(0));
8787
// Create a bit mask from the comparison results.
8888
let multibyte_mask = _mm_movemask_epi8(multibyte_test);
@@ -132,111 +132,8 @@ cfg_select! {
132132
}
133133
}
134134
}
135-
target_arch = "loongarch64" => {
136-
fn analyze_source_file_dispatch(
137-
src: &str,
138-
lines: &mut Vec<RelativeBytePos>,
139-
multi_byte_chars: &mut Vec<MultiByteChar>,
140-
) {
141-
use std::arch::is_loongarch_feature_detected;
142-
143-
if is_loongarch_feature_detected!("lsx") {
144-
unsafe {
145-
analyze_source_file_lsx(src, lines, multi_byte_chars);
146-
}
147-
} else {
148-
analyze_source_file_generic(
149-
src,
150-
src.len(),
151-
RelativeBytePos::from_u32(0),
152-
lines,
153-
multi_byte_chars,
154-
);
155-
}
156-
}
157-
158-
/// Checks 16 byte chunks of text at a time. If the chunk contains
159-
/// something other than printable ASCII characters and newlines, the
160-
/// function falls back to the generic implementation. Otherwise it uses
161-
/// LSX intrinsics to quickly find all newlines.
162-
#[target_feature(enable = "lsx")]
163-
unsafe fn analyze_source_file_lsx(
164-
src: &str,
165-
lines: &mut Vec<RelativeBytePos>,
166-
multi_byte_chars: &mut Vec<MultiByteChar>,
167-
) {
168-
use std::arch::loongarch64::*;
169-
170-
const CHUNK_SIZE: usize = 16;
171-
172-
let (chunks, tail) = src.as_bytes().as_chunks::<CHUNK_SIZE>();
173-
174-
// This variable keeps track of where we should start decoding a
175-
// chunk. If a multi-byte character spans across chunk boundaries,
176-
// we need to skip that part in the next chunk because we already
177-
// handled it.
178-
let mut intra_chunk_offset = 0;
179-
180-
for (chunk_index, chunk) in chunks.iter().enumerate() {
181-
// All LSX memory instructions support unaligned access, so using
182-
// vld is fine.
183-
let chunk = unsafe { lsx_vld::<0>(chunk.as_ptr() as *const i8) };
184-
185-
// For each character in the chunk, see if its byte value is < 0,
186-
// which indicates that it's part of a UTF-8 char.
187-
let multibyte_mask = lsx_vmskltz_b(chunk);
188-
// Create a bit mask from the comparison results.
189-
let multibyte_mask = lsx_vpickve2gr_w::<0>(multibyte_mask);
190-
191-
// If the bit mask is all zero, we only have ASCII chars here:
192-
if multibyte_mask == 0 {
193-
assert!(intra_chunk_offset == 0);
194-
195-
// Check for newlines in the chunk
196-
let newlines_test = lsx_vseqi_b::<{b'\n' as i32}>(chunk);
197-
let newlines_mask = lsx_vmskltz_b(newlines_test);
198-
let mut newlines_mask = lsx_vpickve2gr_w::<0>(newlines_mask);
199-
200-
let output_offset = RelativeBytePos::from_usize(chunk_index * CHUNK_SIZE + 1);
201-
202-
while newlines_mask != 0 {
203-
let index = newlines_mask.trailing_zeros();
204-
205-
lines.push(RelativeBytePos(index) + output_offset);
206-
207-
// Clear the bit, so we can find the next one.
208-
newlines_mask &= newlines_mask - 1;
209-
}
210-
} else {
211-
// The slow path.
212-
// There are multibyte chars in here, fallback to generic decoding.
213-
let scan_start = chunk_index * CHUNK_SIZE + intra_chunk_offset;
214-
intra_chunk_offset = analyze_source_file_generic(
215-
&src[scan_start..],
216-
CHUNK_SIZE - intra_chunk_offset,
217-
RelativeBytePos::from_usize(scan_start),
218-
lines,
219-
multi_byte_chars,
220-
);
221-
}
222-
}
223-
224-
// There might still be a tail left to analyze
225-
let tail_start = src.len() - tail.len() + intra_chunk_offset;
226-
if tail_start < src.len() {
227-
analyze_source_file_generic(
228-
&src[tail_start..],
229-
src.len() - tail_start,
230-
RelativeBytePos::from_usize(tail_start),
231-
lines,
232-
multi_byte_chars,
233-
);
234-
}
235-
}
236-
}
237135
_ => {
238-
// The target (or compiler version) does not support vector instructions
239-
// our specialized implementations need (x86 SSE2, loongarch64 LSX)...
136+
// The target (or compiler version) does not support SSE2 ...
240137
fn analyze_source_file_dispatch(
241138
src: &str,
242139
lines: &mut Vec<RelativeBytePos>,

compiler/rustc_span/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// tidy-alphabetical-start
1919
#![allow(internal_features)]
2020
#![cfg_attr(bootstrap, feature(round_char_boundary))]
21-
#![cfg_attr(target_arch = "loongarch64", feature(stdarch_loongarch))]
2221
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
2322
#![doc(rust_logo)]
2423
#![feature(array_windows)]

0 commit comments

Comments
 (0)