Skip to content

Commit

Permalink
Serialize BlockNode's cold field too when serializing a Layout (#…
Browse files Browse the repository at this point in the history
…4265)

This fixes a bug when the `cold` field would not be serialized, since
we're using a custom (de)serializer for `Layout`. This is now properly
handled by adding a boolean in the serialized stream.

This was caught during the work on #4155, as this would result in cache
mismatches between a function and itself.
  • Loading branch information
bnjbvr authored Jun 13, 2022
1 parent 3eb5ece commit 43d4f0b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cranelift/codegen/src/ir/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ impl Layout {
}
}

/// A single node in the linked-list of blocks.
// Whenever you add new fields here, don't forget to update the custom serializer for `Layout` too.
#[derive(Clone, Debug, Default)]
struct BlockNode {
prev: PackedOption<Block>,
Expand Down Expand Up @@ -803,7 +805,7 @@ impl<'f> DoubleEndedIterator for Insts<'f> {
///
/// ```plain
/// data = block_data * ;
/// block_data = "block_id" , "inst_count" , ( "inst_id" * ) ;
/// block_data = "block_id" , "cold" , "inst_count" , ( "inst_id" * ) ;
/// ```
#[cfg(feature = "enable-serde")]
mod serde {
Expand All @@ -821,14 +823,15 @@ mod serde {
where
S: Serializer,
{
let size = self.blocks().count() * 2
let size = self.blocks().count() * 3
+ self
.blocks()
.map(|block| self.block_insts(block).count())
.sum::<usize>();
let mut seq = serializer.serialize_seq(Some(size))?;
for block in self.blocks() {
seq.serialize_element(&block)?;
seq.serialize_element(&self.blocks[block].cold)?;
seq.serialize_element(&u32::try_from(self.block_insts(block).count()).unwrap())?;
for inst in self.block_insts(block) {
seq.serialize_element(&inst)?;
Expand Down Expand Up @@ -869,9 +872,15 @@ mod serde {
while let Some(block) = access.next_element::<Block>()? {
layout.append_block(block);

let cold = access
.next_element::<bool>()?
.ok_or_else(|| Error::missing_field("cold"))?;
layout.blocks[block].cold = cold;

let count = access
.next_element::<u32>()?
.ok_or_else(|| Error::missing_field("count"))?;

for _ in 0..count {
let inst = access
.next_element::<Inst>()?
Expand Down

0 comments on commit 43d4f0b

Please sign in to comment.