Skip to content

Commit

Permalink
Add debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickpeterse committed Dec 8, 2024
1 parent a08ee42 commit a2d4801
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 41 deletions.
101 changes: 69 additions & 32 deletions compiler/src/mir/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,35 +398,6 @@ impl<'a, 'b> Specialize<'a, 'b> {
.specialize(reg.value_type);
}

// TODO: remove
//for block in &mut method.body.blocks {
// for ins in &mut block.instructions {
// let idx = match ins {
// Instruction::CallStatic(i) => i.type_arguments,
// Instruction::CallInstance(i) => i.type_arguments,
// Instruction::Send(i) => i.type_arguments,
// Instruction::CallDynamic(i) => i.type_arguments,
// _ => None,
// };
//
// let Some(targs) =
// idx.and_then(|i| mir.type_arguments.get_mut(i))
// else {
// continue;
// };
//
// for typ in targs.values_mut() {
// *typ = TypeSpecializer::new(
// &mut self.state.db,
// self.intern,
// &self.shapes,
// &mut self.classes,
// )
// .specialize(*typ);
// }
// }
//}

for block in &mut method.body.blocks {
for instruction in &mut block.instructions {
match instruction {
Expand Down Expand Up @@ -863,7 +834,26 @@ impl<'a, 'b> Specialize<'a, 'b> {
// This ensures that multiple call sites of `next` on `Iter[Int]`
// produce the same method ID, while call sites of `next` on
// `Iter[String]` produce a different method ID.
let spec_key = ordered_shapes_from_map(&base_shapes);
let mut spec_key = ordered_shapes_from_map(&base_shapes);

// TODO: remove?
for shape in &mut spec_key {
match shape {
Shape::Copy(i)
| Shape::Inline(i)
| Shape::InlineRef(i)
| Shape::InlineMut(i) => {
*i = TypeSpecializer::new(
&mut self.state.db,
self.intern,
&self.shapes,
&mut self.classes,
)
.specialize_class_instance(*i);
}
_ => {}
}
}

// TODO: remove
for shape in &spec_key {
Expand Down Expand Up @@ -956,13 +946,32 @@ impl<'a, 'b> Specialize<'a, 'b> {
return method;
}

let key: Vec<Shape> = class
let mut key: Vec<Shape> = class
.type_parameters(&self.state.db)
.into_iter()
.chain(method.type_parameters(&self.state.db))
.map(|p| *shapes.get(&p).unwrap())
.collect();

// TODO: remove?
for shape in &mut key {
match shape {
Shape::Copy(i)
| Shape::Inline(i)
| Shape::InlineRef(i)
| Shape::InlineMut(i) => {
*i = TypeSpecializer::new(
&mut self.state.db,
self.intern,
&self.shapes,
&mut self.classes,
)
.specialize_class_instance(*i);
}
_ => {}
}
}

// TODO: remove
for shape in &key {
let Some(ins) = shape.as_stack_instance() else { continue };
Expand Down Expand Up @@ -1116,11 +1125,39 @@ impl<'a, 'b> Specialize<'a, 'b> {
method.type_parameters(&self.state.db)
};

let method_shapes: Vec<_> = shape_params
let mut method_shapes: Vec<_> = shape_params
.into_iter()
.map(|p| *shapes.get(&p).unwrap())
.collect();

// TODO: remove?
for shape in &mut method_shapes {
match shape {
Shape::Copy(i)
| Shape::Inline(i)
| Shape::InlineRef(i)
| Shape::InlineMut(i) => {
*i = TypeSpecializer::new(
&mut self.state.db,
self.intern,
&self.shapes,
&mut self.classes,
)
.specialize_class_instance(*i);
}
_ => {}
}
}

// TODO: remove
for shape in &method_shapes {
let Some(ins) = shape.as_stack_instance() else { continue };
assert!(ins
.instance_of()
.specialization_source(&self.state.db)
.is_some());
}

let new = method.clone_for_specialization(&mut self.state.db);
let old_ret = method.return_type(&self.state.db);

Expand Down
14 changes: 6 additions & 8 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2046,14 +2046,6 @@ impl ClassInstance {
_ if self.instance_of.is_inline_type(db) => {
let ins = self.interned(db, interned);

// TODO: only produce shapes for specialized types?
if ins.instance_of.is_generic(db) {
assert!(ins
.instance_of
.specialization_source(db)
.is_some());
}

match default {
Shape::Mut => Shape::InlineMut(ins),
Shape::Ref => Shape::InlineRef(ins),
Expand Down Expand Up @@ -2927,6 +2919,12 @@ impl MethodId {
shapes: Vec<Shape>,
method: MethodId,
) {
// TODO: remove
for shape in &shapes {
let Some(ins) = shape.as_stack_instance() else { continue };
assert!(ins.instance_of().specialization_source(db).is_some());
}

self.get_mut(db).specializations.insert(shapes, method);
}

Expand Down
8 changes: 7 additions & 1 deletion types/src/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<'a, 'b, 'c> TypeSpecializer<'a, 'b, 'c> {
}
}

fn specialize_class_instance(
pub fn specialize_class_instance(
&mut self,
ins: ClassInstance,
) -> ClassInstance {
Expand Down Expand Up @@ -335,6 +335,12 @@ impl<'a, 'b, 'c> TypeSpecializer<'a, 'b, 'c> {
new.get_mut(self.db).type_parameters.insert(name, param);
}

// TODO: remove
for shape in &key {
let Some(ins) = shape.as_stack_instance() else { continue };
assert!(ins.instance_of().specialization_source(self.db).is_some());
}

new.set_shapes(self.db, key.clone());
class.get_mut(self.db).specializations.insert(key.clone(), new);

Expand Down

0 comments on commit a2d4801

Please sign in to comment.