Skip to content

Replace bimap dependency with a more efficient pair of maps, and arena-allocate slices. #951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/rustc_codegen_spirv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ regex = { version = "1", features = ["perf"] }

# Normal dependencies.
ar = "0.9.0"
bimap = "0.6"
indexmap = "1.6.0"
rspirv = "0.11"
rustc-demangle = "0.1.21"
Expand Down
37 changes: 21 additions & 16 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use rustc_middle::ty::{
};
use rustc_middle::{bug, span_bug};
use rustc_span::def_id::DefId;
use rustc_span::Span;
use rustc_span::DUMMY_SP;
use rustc_span::{Span, Symbol};
use rustc_target::abi::call::{ArgAbi, ArgAttributes, FnAbi, PassMode};
use rustc_target::abi::{
Abi, Align, FieldsShape, LayoutS, Primitive, Scalar, Size, TagEncoding, VariantIdx, Variants,
Expand Down Expand Up @@ -300,6 +300,7 @@ impl<'tcx> ConvSpirvType<'tcx> for PointeeTy<'tcx> {

impl<'tcx> ConvSpirvType<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
fn spirv_type(&self, span: Span, cx: &CodegenCx<'tcx>) -> Word {
// FIXME(eddyb) use `AccumulateVec`s just like `rustc` itself does.
let mut argument_types = Vec::new();

let return_type = match self.ret.mode {
Expand Down Expand Up @@ -332,7 +333,7 @@ impl<'tcx> ConvSpirvType<'tcx> for FnAbi<'tcx, Ty<'tcx>> {

SpirvType::Function {
return_type,
arguments: argument_types,
arguments: &argument_types,
}
.def(span, cx)
}
Expand Down Expand Up @@ -364,8 +365,8 @@ impl<'tcx> ConvSpirvType<'tcx> for TyAndLayout<'tcx> {
def_id: def_id_for_spirv_type_adt(*self),
size: Some(Size::ZERO),
align: Align::from_bytes(0).unwrap(),
field_types: Vec::new(),
field_offsets: Vec::new(),
field_types: &[],
field_offsets: &[],
field_names: None,
}
.def_with_name(cx, span, TyLayoutNameKey::from(*self)),
Expand Down Expand Up @@ -416,23 +417,24 @@ impl<'tcx> ConvSpirvType<'tcx> for TyAndLayout<'tcx> {
} else {
Some(self.size)
};
// FIXME(eddyb) use `ArrayVec` here.
let mut field_names = Vec::new();
if let TyKind::Adt(adt, _) = self.ty.kind() {
if let Variants::Single { index } = self.variants {
for i in self.fields.index_by_increasing_offset() {
let field = &adt.variants()[index].fields[i];
field_names.push(field.name.to_ident_string());
field_names.push(field.name);
}
}
}
SpirvType::Adt {
def_id: def_id_for_spirv_type_adt(*self),
size,
align: self.align.abi,
field_types: vec![a, b],
field_offsets: vec![a_offset, b_offset],
field_types: &[a, b],
field_offsets: &[a_offset, b_offset],
field_names: if field_names.len() == 2 {
Some(field_names)
Some(&field_names)
} else {
None
},
Expand Down Expand Up @@ -598,8 +600,8 @@ fn trans_aggregate<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>
def_id: def_id_for_spirv_type_adt(ty),
size: Some(Size::ZERO),
align: Align::from_bytes(0).unwrap(),
field_types: Vec::new(),
field_offsets: Vec::new(),
field_types: &[],
field_offsets: &[],
field_names: None,
}
.def_with_name(cx, span, TyLayoutNameKey::from(ty))
Expand Down Expand Up @@ -664,6 +666,7 @@ pub fn auto_struct_layout<'tcx>(
cx: &CodegenCx<'tcx>,
field_types: &[Word],
) -> (Vec<Size>, Option<Size>, Align) {
// FIXME(eddyb) use `AccumulateVec`s just like `rustc` itself does.
let mut field_offsets = Vec::with_capacity(field_types.len());
let mut offset = Some(Size::ZERO);
let mut max_align = Align::from_bytes(0).unwrap();
Expand All @@ -688,6 +691,7 @@ pub fn auto_struct_layout<'tcx>(
fn trans_struct<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>) -> Word {
let size = if ty.is_unsized() { None } else { Some(ty.size) };
let align = ty.align.abi;
// FIXME(eddyb) use `AccumulateVec`s just like `rustc` itself does.
let mut field_types = Vec::new();
let mut field_offsets = Vec::new();
let mut field_names = Vec::new();
Expand All @@ -699,17 +703,18 @@ fn trans_struct<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>) -
if let Variants::Single { index } = ty.variants {
if let TyKind::Adt(adt, _) = ty.ty.kind() {
let field = &adt.variants()[index].fields[i];
field_names.push(field.name.to_ident_string());
field_names.push(field.name);
} else {
field_names.push(format!("{}", i));
// FIXME(eddyb) this looks like something that should exist in rustc.
field_names.push(Symbol::intern(&format!("{i}")));
}
} else {
if let TyKind::Adt(_, _) = ty.ty.kind() {
} else {
span_bug!(span, "Variants::Multiple not TyKind::Adt");
}
if i == 0 {
field_names.push("discriminant".to_string());
field_names.push(cx.sym.discriminant);
} else {
cx.tcx.sess.fatal("Variants::Multiple has multiple fields")
}
Expand All @@ -719,9 +724,9 @@ fn trans_struct<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>) -
def_id: def_id_for_spirv_type_adt(ty),
size,
align,
field_types,
field_offsets,
field_names: Some(field_names),
field_types: &field_types,
field_offsets: &field_offsets,
field_names: Some(&field_names),
}
.def_with_name(cx, span, TyLayoutNameKey::from(ty))
}
Expand Down
18 changes: 8 additions & 10 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
semantics
}

fn memset_const_pattern(&self, ty: &SpirvType, fill_byte: u8) -> Word {
fn memset_const_pattern(&self, ty: &SpirvType<'tcx>, fill_byte: u8) -> Word {
match *ty {
SpirvType::Void => self.fatal("memset invalid on void pattern"),
SpirvType::Bool => self.fatal("memset invalid on bool pattern"),
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
SpirvType::Vector { element, count } | SpirvType::Matrix { element, count } => {
let elem_pat = self.memset_const_pattern(&self.lookup_type(element), fill_byte);
self.constant_composite(
ty.clone().def(self.span(), self),
ty.def(self.span(), self),
iter::repeat(elem_pat).take(count as usize),
)
.def(self)
Expand All @@ -221,7 +221,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let elem_pat = self.memset_const_pattern(&self.lookup_type(element), fill_byte);
let count = self.builder.lookup_const_u64(count).unwrap() as usize;
self.constant_composite(
ty.clone().def(self.span(), self),
ty.def(self.span(), self),
iter::repeat(elem_pat).take(count),
)
.def(self)
Expand All @@ -242,7 +242,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

fn memset_dynamic_pattern(&self, ty: &SpirvType, fill_var: Word) -> Word {
fn memset_dynamic_pattern(&self, ty: &SpirvType<'tcx>, fill_var: Word) -> Word {
match *ty {
SpirvType::Void => self.fatal("memset invalid on void pattern"),
SpirvType::Bool => self.fatal("memset invalid on bool pattern"),
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let count = self.builder.lookup_const_u64(count).unwrap() as usize;
self.emit()
.composite_construct(
ty.clone().def(self.span(), self),
ty.def(self.span(), self),
None,
iter::repeat(elem_pat).take(count),
)
Expand All @@ -280,7 +280,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let elem_pat = self.memset_dynamic_pattern(&self.lookup_type(element), fill_var);
self.emit()
.composite_construct(
ty.clone().def(self.span(), self),
ty.def(self.span(), self),
None,
iter::repeat(elem_pat).take(count as usize),
)
Expand Down Expand Up @@ -1260,9 +1260,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
};
let pointee_kind = self.lookup_type(pointee);
let result_pointee_type = match pointee_kind {
SpirvType::Adt {
ref field_types, ..
} => field_types[idx as usize],
SpirvType::Adt { field_types, .. } => field_types[idx as usize],
SpirvType::Array { element, .. }
| SpirvType::RuntimeArray { element, .. }
| SpirvType::Vector { element, .. }
Expand Down Expand Up @@ -2345,7 +2343,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
),
};

for (argument, argument_type) in args.iter().zip(argument_types) {
for (argument, &argument_type) in args.iter().zip(argument_types) {
assert_ty_eq!(self, argument.ty, argument_type);
}
let libm_intrinsic = self.libm_intrinsics.borrow().get(&callee_val).copied();
Expand Down
2 changes: 1 addition & 1 deletion crates/rustc_codegen_spirv/src/builder/spirv_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ impl<'cx, 'tcx> Builder<'cx, 'tcx> {
(OperandKind::LiteralContextDependentNumber, Some(word)) => {
assert!(matches!(inst.class.opcode, Op::Constant | Op::SpecConstant));
let ty = inst.result_type.unwrap();
fn parse(ty: SpirvType, w: &str) -> Result<dr::Operand, String> {
fn parse(ty: SpirvType<'_>, w: &str) -> Result<dr::Operand, String> {
fn fmt(x: impl ToString) -> String {
x.to_string()
}
Expand Down
Loading