Skip to content

Commit

Permalink
rustc_codegen_spirv: use FxHash{Map,Set} instead of the std ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Apr 8, 2021
1 parent 98d7dec commit 8c89b49
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 159 deletions.
4 changes: 2 additions & 2 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::attr::{AggregatedSpirvAttributes, IntrinsicType};
use crate::codegen_cx::CodegenCx;
use crate::spirv_type::SpirvType;
use rspirv::spirv::{Capability, StorageClass, Word};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorReported;
use rustc_middle::bug;
use rustc_middle::ty::layout::{FnAbiExt, TyAndLayout};
Expand All @@ -18,7 +19,6 @@ use rustc_target::abi::{
};
use std::cell::RefCell;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt;

/// If a struct contains a pointer to itself, even indirectly, then doing a naiive recursive walk
Expand All @@ -28,7 +28,7 @@ use std::fmt;
/// tracking.
#[derive(Default)]
pub struct RecursivePointeeCache<'tcx> {
map: RefCell<HashMap<PointeeTy<'tcx>, PointeeDefState>>,
map: RefCell<FxHashMap<PointeeTy<'tcx>, PointeeDefState>>,
}

impl<'tcx> RecursivePointeeCache<'tcx> {
Expand Down
34 changes: 17 additions & 17 deletions crates/rustc_codegen_spirv/src/builder/spirv_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use rspirv::spirv::{
use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::traits::{AsmBuilderMethods, InlineAsmOperandRef};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::LlvmInlineAsmInner;
use rustc_middle::bug;
use rustc_span::source_map::Span;
use rustc_target::asm::{InlineAsmRegClass, InlineAsmRegOrRegClass, SpirVInlineAsmRegClass};
use std::collections::{HashMap, HashSet};

pub struct InstructionTable {
table: HashMap<&'static str, &'static rspirv::grammar::Instruction<'static>>,
table: FxHashMap<&'static str, &'static rspirv::grammar::Instruction<'static>>,
}

impl InstructionTable {
Expand Down Expand Up @@ -133,9 +133,9 @@ impl<'a, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'tcx> {
}
}

let mut id_map = HashMap::new();
let mut defined_ids = HashSet::new();
let mut id_to_type_map = HashMap::new();
let mut id_map = FxHashMap::default();
let mut defined_ids = FxHashSet::default();
let mut id_to_type_map = FxHashMap::default();
for operand in operands {
if let InlineAsmOperandRef::In { reg: _, value } = operand {
let value = value.immediate();
Expand Down Expand Up @@ -241,8 +241,8 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {

fn insert_inst(
&mut self,
id_map: &mut HashMap<&str, Word>,
defined_ids: &mut HashSet<Word>,
id_map: &mut FxHashMap<&str, Word>,
defined_ids: &mut FxHashSet<Word>,
inst: dr::Instruction,
) {
// Types declared must be registered in our type system.
Expand Down Expand Up @@ -339,9 +339,9 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {

fn codegen_asm<'a>(
&mut self,
id_map: &mut HashMap<&'a str, Word>,
defined_ids: &mut HashSet<Word>,
id_to_type_map: &mut HashMap<Word, Word>,
id_map: &mut FxHashMap<&'a str, Word>,
defined_ids: &mut FxHashSet<Word>,
id_to_type_map: &mut FxHashMap<Word, Word>,
mut tokens: impl Iterator<Item = Token<'a, 'cx, 'tcx>>,
) where
'cx: 'a,
Expand Down Expand Up @@ -433,8 +433,8 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {

fn parse_operands<'a>(
&mut self,
id_map: &mut HashMap<&'a str, Word>,
id_to_type_map: &HashMap<Word, Word>,
id_map: &mut FxHashMap<&'a str, Word>,
id_to_type_map: &FxHashMap<Word, Word>,
mut tokens: impl Iterator<Item = Token<'a, 'cx, 'tcx>>,
instruction: &mut dr::Instruction,
) where
Expand Down Expand Up @@ -537,7 +537,7 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {

fn infer_result_type(
&self,
id_to_type_map: &HashMap<Word, Word>,
id_to_type_map: &FxHashMap<Word, Word>,
instruction: &dr::Instruction,
) -> Option<Word> {
use crate::spirv_type_constraints::{instruction_signatures, InstSig, TyListPat, TyPat};
Expand Down Expand Up @@ -673,8 +673,8 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {

fn parse_id_out<'a>(
&mut self,
id_map: &mut HashMap<&'a str, Word>,
defined_ids: &mut HashSet<Word>,
id_map: &mut FxHashMap<&'a str, Word>,
defined_ids: &mut FxHashSet<Word>,
token: Token<'a, 'cx, 'tcx>,
) -> Option<OutRegister<'a>> {
match token {
Expand Down Expand Up @@ -762,7 +762,7 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {

fn parse_id_in<'a>(
&mut self,
id_map: &mut HashMap<&'a str, Word>,
id_map: &mut FxHashMap<&'a str, Word>,
token: Token<'a, 'cx, 'tcx>,
) -> Option<Word> {
match token {
Expand Down Expand Up @@ -904,7 +904,7 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {

fn parse_one_operand<'a>(
&mut self,
id_map: &mut HashMap<&'a str, Word>,
id_map: &mut FxHashMap<&'a str, Word>,
inst: &mut dr::Instruction,
kind: OperandKind,
tokens: &mut impl Iterator<Item = Token<'a, 'cx, 'tcx>>,
Expand Down
6 changes: 3 additions & 3 deletions crates/rustc_codegen_spirv/src/codegen_cx/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::spirv_type::SpirvType;
use rspirv::dr::Operand;
use rspirv::spirv::{Decoration, ExecutionModel, FunctionControl, StorageClass, Word};
use rustc_codegen_ssa::traits::{BaseTypeMethods, BuilderMethods};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir as hir;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{Instance, Ty, TyKind};
Expand All @@ -15,7 +16,6 @@ use rustc_target::abi::{
call::{ArgAbi, ArgAttribute, ArgAttributes, FnAbi, PassMode},
LayoutOf, Size,
};
use std::collections::HashMap;

impl<'tcx> CodegenCx<'tcx> {
// Entry points declare their "interface" (all uniforms, inputs, outputs, etc.) as parameters.
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<'tcx> CodegenCx<'tcx> {

let mut bx = Builder::new_block(self, stub_fn, "");
let mut call_args = vec![];
let mut decoration_locations = HashMap::new();
let mut decoration_locations = FxHashMap::default();
for (entry_arg_abi, hir_param) in arg_abis.iter().zip(hir_params) {
bx.set_span(hir_param.span);
self.declare_shader_interface_for_param(
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'tcx> CodegenCx<'tcx> {
op_entry_point_interface_operands: &mut Vec<Word>,
bx: &mut Builder<'_, 'tcx>,
call_args: &mut Vec<SpirvValue>,
decoration_locations: &mut HashMap<StorageClass, u32>,
decoration_locations: &mut FxHashMap<StorageClass, u32>,
) {
let attrs = AggregatedSpirvAttributes::parse(self, self.tcx.hir().attrs(hir_param.hir_id));

Expand Down
13 changes: 6 additions & 7 deletions crates/rustc_codegen_spirv/src/codegen_cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use rustc_target::abi::call::FnAbi;
use rustc_target::abi::{HasDataLayout, TargetDataLayout};
use rustc_target::spec::{HasTargetSpec, Target};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::iter::once;
use std::rc::Rc;
use std::str::FromStr;
Expand All @@ -41,27 +40,27 @@ pub struct CodegenCx<'tcx> {
/// Spir-v module builder
pub builder: BuilderSpirv,
/// Map from MIR function to spir-v function ID
pub instances: RefCell<HashMap<Instance<'tcx>, SpirvValue>>,
pub instances: RefCell<FxHashMap<Instance<'tcx>, SpirvValue>>,
/// Map from function ID to parameter list
pub function_parameter_values: RefCell<HashMap<Word, Vec<SpirvValue>>>,
pub function_parameter_values: RefCell<FxHashMap<Word, Vec<SpirvValue>>>,
pub type_cache: TypeCache<'tcx>,
/// Cache generated vtables
pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<PolyExistentialTraitRef<'tcx>>), SpirvValue>>,
pub ext_inst: RefCell<ExtInst>,
/// Invalid spir-v IDs that should be stripped from the final binary,
/// each with its own reason and span that should be used for reporting
/// (in the event that the value is actually needed)
zombie_decorations: RefCell<HashMap<Word, ZombieDecoration>>,
zombie_decorations: RefCell<FxHashMap<Word, ZombieDecoration>>,
/// Functions that have `#[spirv(unroll_loops)]`, and therefore should
/// get `LoopControl::UNROLL` applied to all of their loops' `OpLoopMerge`
/// instructions, during structuralization.
unroll_loops_decorations: RefCell<HashMap<Word, UnrollLoopsDecoration>>,
unroll_loops_decorations: RefCell<FxHashMap<Word, UnrollLoopsDecoration>>,
pub kernel_mode: bool,
/// Cache of all the builtin symbols we need
pub sym: Rc<Symbols>,
pub instruction_table: InstructionTable,
pub zombie_undefs_for_system_fn_addrs: RefCell<HashMap<Word, Word>>,
pub libm_intrinsics: RefCell<HashMap<Word, super::builder::libm_intrinsics::LibmIntrinsic>>,
pub zombie_undefs_for_system_fn_addrs: RefCell<FxHashMap<Word, Word>>,
pub libm_intrinsics: RefCell<FxHashMap<Word, super::builder::libm_intrinsics::LibmIntrinsic>>,

/// Simple `panic!("...")` and builtin panics (from MIR `Assert`s) call `#[lang = "panic"]`.
pub panic_fn_id: Cell<Option<Word>>,
Expand Down
6 changes: 3 additions & 3 deletions crates/rustc_codegen_spirv/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{linker, SpirvCodegenBackend, SpirvModuleBuffer, SpirvThinBuffer};
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared};
use rustc_codegen_ssa::back::write::CodegenContext;
use rustc_codegen_ssa::{CodegenResults, NativeLib};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::rustc_erase_owner;
use rustc_data_structures::sync::MetadataRef;
Expand All @@ -13,7 +14,6 @@ use rustc_session::config::{CrateType, DebugInfo, Lto, OptLevel, OutputFilenames
use rustc_session::output::{check_file_is_writeable, invalid_output_for_target, out_filename};
use rustc_session::utils::NativeLibKind;
use rustc_session::Session;
use std::collections::{HashMap, HashSet};
use std::env;
use std::ffi::{CString, OsStr};
use std::fs::File;
Expand Down Expand Up @@ -143,7 +143,7 @@ fn link_exe(
if !out_dir.is_dir() {
std::fs::create_dir_all(&out_dir).unwrap();
}
let mut hashmap = HashMap::new();
let mut hashmap = FxHashMap::default();
for (name, spv_binary) in map {
let mut module_filename = out_dir.clone();
module_filename.push(sanitize_filename::sanitize(&name));
Expand Down Expand Up @@ -374,7 +374,7 @@ fn create_archive(files: &[&Path], metadata: &[u8], out_filename: &Path) {
header.set_cksum();
builder.append(&header, metadata).unwrap();
}
let mut filenames = HashSet::new();
let mut filenames = FxHashSet::default();
filenames.insert(OsStr::new(".metadata"));
for file in files {
assert!(
Expand Down
12 changes: 6 additions & 6 deletions crates/rustc_codegen_spirv/src/linker/capability_computation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use rspirv::dr::{Instruction, Module};
use rspirv::spirv::{Capability, Op};
use std::collections::HashSet;
use rustc_data_structures::fx::FxHashSet;

pub fn remove_extra_capabilities(module: &mut Module) {
let used_capabilities = used_capabilities(module);
let removable_capabilities: HashSet<Capability> = [
let removable_capabilities: FxHashSet<Capability> = [
Capability::Int8,
Capability::Int16,
Capability::Int64,
Expand All @@ -24,8 +24,8 @@ pub fn remove_extra_capabilities(module: &mut Module) {
remove_capabilities(module, &to_remove);
}

fn used_capabilities(module: &Module) -> HashSet<Capability> {
let mut set = HashSet::new();
fn used_capabilities(module: &Module) -> FxHashSet<Capability> {
let mut set = FxHashSet::default();
for inst in module.all_inst_iter() {
set.extend(inst.class.capabilities);
match inst.class.opcode {
Expand Down Expand Up @@ -56,7 +56,7 @@ fn used_capabilities(module: &Module) -> HashSet<Capability> {
set
}

fn remove_capabilities(module: &mut Module, set: &HashSet<Capability>) {
fn remove_capabilities(module: &mut Module, set: &FxHashSet<Capability>) {
module.capabilities.retain(|inst| {
inst.class.opcode != Op::Capability || !set.contains(&inst.operands[0].unwrap_capability())
});
Expand Down Expand Up @@ -87,7 +87,7 @@ fn additional_extensions(module: &Module, inst: &Instruction) -> &'static [&'sta
}

pub fn remove_extra_extensions(module: &mut Module) {
let set: HashSet<&str> = module
let set: FxHashSet<&str> = module
.all_inst_iter()
.flat_map(|inst| {
let extensions = inst.class.extensions.iter().copied();
Expand Down
16 changes: 8 additions & 8 deletions crates/rustc_codegen_spirv/src/linker/dce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

use rspirv::dr::{Function, Instruction, Module};
use rspirv::spirv::{Op, Word};
use std::collections::HashSet;
use rustc_data_structures::fx::FxHashSet;

pub fn dce(module: &mut Module) {
let mut rooted = collect_roots(module);
while spread_roots(module, &mut rooted) {}
kill_unrooted(module, &rooted);
}

pub fn collect_roots(module: &Module) -> HashSet<Word> {
let mut rooted = HashSet::new();
pub fn collect_roots(module: &Module) -> FxHashSet<Word> {
let mut rooted = FxHashSet::default();
for inst in &module.entry_points {
root(inst, &mut rooted);
}
rooted
}

fn spread_roots(module: &Module, rooted: &mut HashSet<Word>) -> bool {
fn spread_roots(module: &Module, rooted: &mut FxHashSet<Word>) -> bool {
let mut any = false;
for inst in module.global_inst_iter() {
if let Some(id) = inst.result_id {
Expand All @@ -44,7 +44,7 @@ fn spread_roots(module: &Module, rooted: &mut HashSet<Word>) -> bool {
any
}

fn root(inst: &Instruction, rooted: &mut HashSet<Word>) -> bool {
fn root(inst: &Instruction, rooted: &mut FxHashSet<Word>) -> bool {
let mut any = false;
if let Some(id) = inst.result_type {
any |= rooted.insert(id);
Expand All @@ -57,7 +57,7 @@ fn root(inst: &Instruction, rooted: &mut HashSet<Word>) -> bool {
any
}

fn is_rooted(inst: &Instruction, rooted: &HashSet<Word>) -> bool {
fn is_rooted(inst: &Instruction, rooted: &FxHashSet<Word>) -> bool {
if let Some(result_id) = inst.result_id {
rooted.contains(&result_id)
} else {
Expand All @@ -69,7 +69,7 @@ fn is_rooted(inst: &Instruction, rooted: &HashSet<Word>) -> bool {
}
}

fn kill_unrooted(module: &mut Module, rooted: &HashSet<Word>) {
fn kill_unrooted(module: &mut Module, rooted: &FxHashSet<Word>) {
module
.ext_inst_imports
.retain(|inst| is_rooted(inst, rooted));
Expand All @@ -87,7 +87,7 @@ fn kill_unrooted(module: &mut Module, rooted: &HashSet<Word>) {
}

pub fn dce_phi(func: &mut Function) {
let mut used = HashSet::new();
let mut used = FxHashSet::default();
loop {
let mut changed = false;
for inst in func.all_inst_iter() {
Expand Down
Loading

0 comments on commit 8c89b49

Please sign in to comment.