Skip to content

Commit 37e659d

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 3e1dbab commit 37e659d

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
.iter()
3838
.find(|inst| inst.class.opcode == Op::TypeVoid)
3939
.map_or(0, |inst| inst.result_id.unwrap());
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,
@@ -281,6 +295,7 @@ struct Inliner<'m, 'map> {
281295
header: &'m mut ModuleHeader,
282296
types_global_values: &'m mut Vec<Instruction>,
283297
void: Word,
298+
ptr_map: FxHashMap<Word, Word>,
284299
functions: &'map FunctionMap,
285300
disallowed_argument_types: &'map FxHashSet<Word>,
286301
disallowed_return_types: &'map FxHashSet<Word>,
@@ -295,14 +310,9 @@ impl Inliner<'_, '_> {
295310
}
296311

297312
fn ptr_ty(&mut self, pointee: Word) -> Word {
298-
// TODO: This is horribly slow, fix this
299-
let existing = self.types_global_values.iter().find(|inst| {
300-
inst.class.opcode == Op::TypePointer
301-
&& inst.operands[0].unwrap_storage_class() == StorageClass::Function
302-
&& inst.operands[1].unwrap_id_ref() == pointee
303-
});
313+
let existing = self.ptr_map.get(&pointee);
304314
if let Some(existing) = existing {
305-
return existing.result_id.unwrap();
315+
return *existing;
306316
}
307317
let inst_id = self.id();
308318
self.types_global_values.push(Instruction::new(
@@ -314,6 +324,7 @@ impl Inliner<'_, '_> {
314324
Operand::IdRef(pointee),
315325
],
316326
));
327+
self.ptr_map.insert(pointee, inst_id);
317328
inst_id
318329
}
319330

0 commit comments

Comments
 (0)