Skip to content

Commit

Permalink
clean up potential_query_instability with FxIndexMap and UnordMap
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Feb 12, 2024
1 parent 9aa232e commit ac51577
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 78 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_borrowck/src/region_infer/values.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::FxIndexSet;
use rustc_index::bit_set::SparseBitMatrix;
use rustc_index::interval::IntervalSet;
Expand Down Expand Up @@ -42,7 +41,7 @@ pub(crate) struct LivenessValues {

/// Which regions are live. This is exclusive with the fine-grained tracking in `points`, and
/// currently only used for validating promoteds (which don't care about more precise tracking).
live_regions: Option<FxHashSet<RegionVid>>,
live_regions: Option<FxIndexSet<RegionVid>>,

/// For each region: the points where it is live.
///
Expand Down Expand Up @@ -104,10 +103,6 @@ impl LivenessValues {
self.points.as_ref().expect("use with_specific_points").rows()
}

/// Iterate through each region that has a value in this set.
// We are passing query instability implications to the caller.
#[rustc_lint_query_instability]
#[allow(rustc::potential_query_instability)]
pub(crate) fn live_regions_unordered(&self) -> impl Iterator<Item = RegionVid> + '_ {
self.live_regions.as_ref().unwrap().iter().copied()
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
//
// add_location doesn't care about ordering so not a problem for the live regions to be
// unordered.
#[allow(rustc::potential_query_instability)]
for region in liveness_constraints.live_regions_unordered() {
self.cx
.borrowck_context
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_borrowck/src/universal_regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]

use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::Diagnostic;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
Expand Down Expand Up @@ -180,7 +180,7 @@ struct UniversalRegionIndices<'tcx> {
/// basically equivalent to an `GenericArgs`, except that it also
/// contains an entry for `ReStatic` -- it might be nice to just
/// use an args, and then handle `ReStatic` another way.
indices: FxHashMap<ty::Region<'tcx>, RegionVid>,
indices: FxIndexMap<ty::Region<'tcx>, RegionVid>,

/// The vid assigned to `'static`. Used only for diagnostics.
pub fr_static: RegionVid,
Expand Down Expand Up @@ -325,9 +325,6 @@ impl<'tcx> UniversalRegions<'tcx> {
}

/// Gets an iterator over all the early-bound regions that have names.
/// Iteration order may be unstable, so this should only be used when
/// iteration order doesn't affect anything
#[allow(rustc::potential_query_instability)]
pub fn named_universal_regions<'s>(
&'s self,
) -> impl Iterator<Item = (ty::Region<'tcx>, ty::RegionVid)> + 's {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{looks_like_rust_object_file, ModuleCodegen, ModuleKind};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{DiagCtxt, FatalError};
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::bug;
Expand Down Expand Up @@ -787,7 +788,7 @@ pub unsafe fn optimize_thin_module(
#[derive(Debug, Default)]
pub struct ThinLTOKeysMap {
// key = llvm name of importing module, value = LLVM cache key
keys: FxHashMap<String, String>,
keys: UnordMap<String, String>,
}

impl ThinLTOKeysMap {
Expand All @@ -797,16 +798,15 @@ impl ThinLTOKeysMap {
let mut writer = io::BufWriter::new(file);
// The entries are loaded back into a hash map in `load_from_file()`, so
// the order in which we write them to file here does not matter.
#[allow(rustc::potential_query_instability)]
for (module, key) in &self.keys {
for (module, key) in self.keys.items().into_sorted_stable_ord() {
writeln!(writer, "{module} {key}")?;
}
Ok(())
}

fn load_from_file(path: &Path) -> io::Result<Self> {
use std::io::BufRead;
let mut keys = FxHashMap::default();
let mut keys = UnordMap::default();
let file = File::open(path)?;
for line in io::BufReader::new(file).lines() {
let line = line?;
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ fn codegenned_and_inlined_items(tcx: TyCtxt<'_>) -> DefIdSet {
let mut result = items.clone();

for cgu in cgus {
#[allow(rustc::potential_query_instability)]
for item in cgu.items().keys() {
if let mir::mono::MonoItem::Fn(ref instance) = item {
let did = instance.def_id();
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_codegen_ssa/src/assert_module_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use crate::errors;
use rustc_ast as ast;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::unord::UnordMap;
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
use rustc_hir::def_id::LOCAL_CRATE;
Expand Down Expand Up @@ -218,8 +218,8 @@ pub enum ComparisonKind {
}

struct TrackerData {
actual_reuse: FxHashMap<String, CguReuse>,
expected_reuse: FxHashMap<String, (String, Span, CguReuse, ComparisonKind)>,
actual_reuse: UnordMap<String, CguReuse>,
expected_reuse: UnordMap<String, (String, Span, CguReuse, ComparisonKind)>,
}

pub struct CguReuseTracker {
Expand Down Expand Up @@ -267,9 +267,7 @@ impl CguReuseTracker {

fn check_expected_reuse(&self, sess: &Session) {
if let Some(ref data) = self.data {
#[allow(rustc::potential_query_instability)]
let mut keys = data.expected_reuse.keys().collect::<Vec<_>>();
keys.sort_unstable();
let keys = data.expected_reuse.keys().into_sorted_stable_ord();
for cgu_name in keys {
let &(ref cgu_user_name, ref error_span, expected_reuse, comparison_kind) =
data.expected_reuse.get(cgu_name).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::memmap::Mmap;
use rustc_session::cstore::DllImport;
use rustc_session::Session;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub trait ArchiveBuilderBuilder {
&'a self,
rlib: &'a Path,
outdir: &Path,
bundled_lib_file_names: &FxHashSet<Symbol>,
bundled_lib_file_names: &FxIndexSet<Symbol>,
) -> Result<(), ExtractBundledLibsError<'_>> {
let archive_map = unsafe {
Mmap::map(
Expand Down
25 changes: 11 additions & 14 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_arena::TypedArena;
use rustc_ast::CRATE_NODE_ID;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_errors::{DiagCtxt, ErrorGuaranteed};
Expand Down Expand Up @@ -534,9 +534,9 @@ fn link_staticlib<'a>(

let native_libs = codegen_results.crate_info.native_libraries[&cnum].iter();
let relevant = native_libs.clone().filter(|lib| relevant_lib(sess, lib));
let relevant_libs: FxHashSet<_> = relevant.filter_map(|lib| lib.filename).collect();
let relevant_libs: FxIndexSet<_> = relevant.filter_map(|lib| lib.filename).collect();

let bundled_libs: FxHashSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
let bundled_libs: FxIndexSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
ab.add_archive(
path,
Box::new(move |fname: &str| {
Expand Down Expand Up @@ -564,10 +564,7 @@ fn link_staticlib<'a>(
.extract_bundled_libs(path, tempdir.as_ref(), &relevant_libs)
.unwrap_or_else(|e| sess.dcx().emit_fatal(e));

// We sort the libraries below
#[allow(rustc::potential_query_instability)]
let mut relevant_libs: Vec<Symbol> = relevant_libs.into_iter().collect();
relevant_libs.sort_unstable();
let relevant_libs: Vec<Symbol> = relevant_libs.into_iter().collect();
for filename in relevant_libs {
let joined = tempdir.as_ref().join(filename.as_str());
let path = joined.as_path();
Expand Down Expand Up @@ -682,13 +679,14 @@ fn link_dwarf_object<'a>(
}

// Input rlibs contain .o/.dwo files from dependencies.
#[allow(rustc::potential_query_instability)]
let input_rlibs = cg_results
.crate_info
.used_crate_source
.values()
.filter_map(|csource| csource.rlib.as_ref())
.map(|(path, _)| path);
.items()
.filter_map(|(_, csource)| csource.rlib.as_ref())
.map(|(path, _)| path)
.into_sorted_stable_ord();

for input_rlib in input_rlibs {
debug!(?input_rlib);
package.add_input_object(input_rlib)?;
Expand Down Expand Up @@ -2218,7 +2216,6 @@ fn linker_with_args<'a>(
.expect("failed to find crate type in dependency format list");

// We sort the libraries below
#[allow(rustc::potential_query_instability)]
let mut native_libraries_from_nonstatics = codegen_results
.crate_info
.native_libraries
Expand Down Expand Up @@ -2456,7 +2453,7 @@ fn add_native_libs_from_crate(
codegen_results: &CodegenResults,
tmpdir: &Path,
search_paths: &SearchPaths,
bundled_libs: &FxHashSet<Symbol>,
bundled_libs: &FxIndexSet<Symbol>,
cnum: CrateNum,
link_static: bool,
link_dynamic: bool,
Expand Down Expand Up @@ -2777,7 +2774,7 @@ fn add_static_crate<'a>(
codegen_results: &CodegenResults,
tmpdir: &Path,
cnum: CrateNum,
bundled_lib_file_names: &FxHashSet<Symbol>,
bundled_lib_file_names: &FxIndexSet<Symbol>,
) {
let src = &codegen_results.crate_info.used_crate_source[&cnum];
let cratepath = &src.rlib.as_ref().unwrap().0;
Expand Down
16 changes: 7 additions & 9 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use crate::{CachedModuleCodegen, CompiledModule, CrateInfo, MemFlags, ModuleCode

use rustc_ast::expand::allocator::{global_fn_name, AllocatorKind, ALLOCATOR_METHODS};
use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
use rustc_data_structures::sync::par_map;
use rustc_data_structures::unord::UnordMap;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem;
Expand Down Expand Up @@ -851,6 +852,8 @@ impl CrateInfo {
// `compiler_builtins` are always placed last to ensure that they're linked correctly.
used_crates.extend(compiler_builtins);

let crates = tcx.crates(());
let n_crates = crates.len();
let mut info = CrateInfo {
target_cpu,
crate_types,
Expand All @@ -862,19 +865,15 @@ impl CrateInfo {
is_no_builtins: Default::default(),
native_libraries: Default::default(),
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
crate_name: Default::default(),
crate_name: UnordMap::with_capacity(n_crates),
used_crates,
used_crate_source: Default::default(),
used_crate_source: UnordMap::with_capacity(n_crates),
dependency_formats: tcx.dependency_formats(()).clone(),
windows_subsystem,
natvis_debugger_visualizers: Default::default(),
};
let crates = tcx.crates(());

let n_crates = crates.len();
info.native_libraries.reserve(n_crates);
info.crate_name.reserve(n_crates);
info.used_crate_source.reserve(n_crates);

for &cnum in crates.iter() {
info.native_libraries
Expand All @@ -901,7 +900,7 @@ impl CrateInfo {
// by the compiler, but that's ok because all this stuff is unstable anyway.
let target = &tcx.sess.target;
if !are_upstream_rust_objects_already_included(tcx.sess) {
let missing_weak_lang_items: FxHashSet<Symbol> = info
let missing_weak_lang_items: FxIndexSet<Symbol> = info
.used_crates
.iter()
.flat_map(|&cnum| tcx.missing_lang_items(cnum))
Expand All @@ -915,7 +914,6 @@ impl CrateInfo {

// This loop only adds new items to values of the hash map, so the order in which we
// iterate over the values is not important.
#[allow(rustc::potential_query_instability)]
info.linked_symbols
.iter_mut()
.filter(|(crate_type, _)| {
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ extern crate tracing;
extern crate rustc_middle;

use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::unord::UnordMap;
use rustc_hir::def_id::CrateNum;
use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
Expand Down Expand Up @@ -152,16 +154,16 @@ impl From<&cstore::NativeLib> for NativeLib {
pub struct CrateInfo {
pub target_cpu: String,
pub crate_types: Vec<CrateType>,
pub exported_symbols: FxHashMap<CrateType, Vec<String>>,
pub linked_symbols: FxHashMap<CrateType, Vec<(String, SymbolExportKind)>>,
pub exported_symbols: UnordMap<CrateType, Vec<String>>,
pub linked_symbols: FxIndexMap<CrateType, Vec<(String, SymbolExportKind)>>,
pub local_crate_name: Symbol,
pub compiler_builtins: Option<CrateNum>,
pub profiler_runtime: Option<CrateNum>,
pub is_no_builtins: FxHashSet<CrateNum>,
pub native_libraries: FxHashMap<CrateNum, Vec<NativeLib>>,
pub crate_name: FxHashMap<CrateNum, Symbol>,
pub native_libraries: FxIndexMap<CrateNum, Vec<NativeLib>>,
pub crate_name: UnordMap<CrateNum, Symbol>,
pub used_libraries: Vec<NativeLib>,
pub used_crate_source: FxHashMap<CrateNum, Lrc<CrateSource>>,
pub used_crate_source: UnordMap<CrateNum, Lrc<CrateSource>>,
pub used_crates: Vec<CrateNum>,
pub dependency_formats: Lrc<Dependencies>,
pub windows_subsystem: Option<String>,
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_data_structures/src/unord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@ impl<K: Eq + Hash, V> UnordMap<K, V> {
UnordItems(self.inner.into_iter())
}

#[inline]
pub fn keys(&self) -> UnordItems<&K, impl Iterator<Item = &K>> {
UnordItems(self.inner.keys())
}

/// Returns the entries of this map in stable sort order (as defined by `ToStableHashKey`).
///
/// The `cache_sort_key` parameter controls if [slice::sort_by_cached_key] or
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::unord::UnordMap;
use rustc_errors::{
codes::*, pluralize, struct_span_code_err, AddToDiagnostic, Applicability, Diagnostic,
DiagnosticBuilder, ErrCode, ErrorGuaranteed, StashKey,
Expand Down Expand Up @@ -1716,7 +1717,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.fields
.iter_enumerated()
.map(|(i, field)| (field.ident(tcx).normalize_to_macros_2_0(), (i, field)))
.collect::<FxHashMap<_, _>>();
.collect::<UnordMap<_, _>>();

let mut seen_fields = FxHashMap::default();

Expand Down Expand Up @@ -1960,18 +1961,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
adt_ty: Ty<'tcx>,
span: Span,
remaining_fields: FxHashMap<Ident, (FieldIdx, &ty::FieldDef)>,
remaining_fields: UnordMap<Ident, (FieldIdx, &ty::FieldDef)>,
variant: &'tcx ty::VariantDef,
ast_fields: &'tcx [hir::ExprField<'tcx>],
args: GenericArgsRef<'tcx>,
) {
let len = remaining_fields.len();

#[allow(rustc::potential_query_instability)]
let mut displayable_field_names: Vec<&str> =
remaining_fields.keys().map(|ident| ident.as_str()).collect();
// sorting &str primitives here, sort_unstable is ok
displayable_field_names.sort_unstable();
let displayable_field_names: Vec<&str> =
remaining_fields.items().map(|(ident, _)| ident.as_str()).into_sorted_stable_ord();

let mut truncated_fields_error = String::new();
let remaining_fields_names = match &displayable_field_names[..] {
Expand Down
Loading

0 comments on commit ac51577

Please sign in to comment.