Skip to content

Commit 5a902ce

Browse files
committed
linker/inline: add pointer type caching
We need pointer types, and re-checking all the types to see if we already have one is rather slow, it's better to keep track.
1 parent e161317 commit 5a902ce

File tree

1 file changed

+18
-7
lines changed
  • crates/rustc_codegen_spirv/src/linker

1 file changed

+18
-7
lines changed

crates/rustc_codegen_spirv/src/linker/inline.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,26 @@ pub fn inline(sess: &Session, module: &mut Module) -> super::Result<()> {
3737
.find(|inst| inst.class.opcode == Op::TypeVoid)
3838
.map(|inst| inst.result_id.unwrap())
3939
.unwrap_or(0);
40+
let ptr_map: FxHashMap<_, _> = module
41+
.types_global_values
42+
.iter()
43+
.filter_map(|inst| {
44+
if inst.class.opcode == Op::TypePointer
45+
&& inst.operands[0].unwrap_storage_class() == StorageClass::Function
46+
{
47+
Some((inst.operands[1].unwrap_id_ref(), inst.result_id.unwrap()))
48+
} else {
49+
None
50+
}
51+
})
52+
.collect();
4053
// Drop all the functions we'll be inlining. (This also means we won't waste time processing
4154
// inlines in functions that will get inlined)
4255
let mut inliner = Inliner {
4356
header: module.header.as_mut().unwrap(),
4457
types_global_values: &mut module.types_global_values,
4558
void,
59+
ptr_map,
4660
functions: &functions,
4761
disallowed_argument_types: &disallowed_argument_types,
4862
disallowed_return_types: &disallowed_return_types,
@@ -283,6 +297,7 @@ struct Inliner<'m, 'map> {
283297
header: &'m mut ModuleHeader,
284298
types_global_values: &'m mut Vec<Instruction>,
285299
void: Word,
300+
ptr_map: FxHashMap<Word, Word>,
286301
functions: &'map FunctionMap,
287302
disallowed_argument_types: &'map FxHashSet<Word>,
288303
disallowed_return_types: &'map FxHashSet<Word>,
@@ -297,14 +312,9 @@ impl Inliner<'_, '_> {
297312
}
298313

299314
fn ptr_ty(&mut self, pointee: Word) -> Word {
300-
// TODO: This is horribly slow, fix this
301-
let existing = self.types_global_values.iter().find(|inst| {
302-
inst.class.opcode == Op::TypePointer
303-
&& inst.operands[0].unwrap_storage_class() == StorageClass::Function
304-
&& inst.operands[1].unwrap_id_ref() == pointee
305-
});
315+
let existing = self.ptr_map.get(&pointee);
306316
if let Some(existing) = existing {
307-
return existing.result_id.unwrap();
317+
return *existing;
308318
}
309319
let inst_id = self.id();
310320
self.types_global_values.push(Instruction::new(
@@ -316,6 +326,7 @@ impl Inliner<'_, '_> {
316326
Operand::IdRef(pointee),
317327
],
318328
));
329+
self.ptr_map.insert(pointee, inst_id);
319330
inst_id
320331
}
321332

0 commit comments

Comments
 (0)