Add syntax for cold blocks to CLIF.

This commit adds support for denoting cold blocks in the CLIF text
format as follows:

```plain

function %f() {
block0(...):
  ...

block1 cold:
  ...

block2(...) cold:
  ...

block3:
  ...
```

With this syntax, we are able to see the cold-block flag in CLIF, we can
write tests using it, and it is preserved when round-tripping.

Fixes #3701.
This commit is contained in:
Chris Fallin
2022-01-20 15:37:07 -08:00
parent 90e7cef56c
commit 51649d56b7
3 changed files with 65 additions and 6 deletions

View File

@@ -216,12 +216,18 @@ pub fn write_block_header(
block: Block,
indent: usize,
) -> fmt::Result {
let cold = if func.layout.is_cold(block) {
" cold"
} else {
""
};
// The `indent` is the instruction indentation. block headers are 4 spaces out from that.
write!(w, "{1:0$}{2}", indent - 4, "", block)?;
let mut args = func.dfg.block_params(block).iter().cloned();
match args.next() {
None => return writeln!(w, ":"),
None => return writeln!(w, "{}:", cold),
Some(arg) => {
write!(w, "(")?;
write_arg(w, func, arg)?;
@@ -232,7 +238,7 @@ pub fn write_block_header(
write!(w, ", ")?;
write_arg(w, func, arg)?;
}
writeln!(w, "):")
writeln!(w, "){}:", cold)
}
fn decorate_block<FW: FuncWriter>(
@@ -666,4 +672,26 @@ mod tests {
"function u0:0() fast {\nblock0(v3: i32):\n v0 -> v3\n v2 -> v0\n v4 = iconst.i32 42\n v5 = iadd v0, v0\n v1 -> v5\n v6 = iconst.i32 23\n v7 = iadd v1, v1\n}\n"
);
}
#[test]
fn cold_blocks() {
let mut func = Function::new();
{
let mut pos = FuncCursor::new(&mut func);
let block0 = pos.func.dfg.make_block();
pos.insert_block(block0);
pos.func.layout.set_cold(block0);
let block1 = pos.func.dfg.make_block();
pos.insert_block(block1);
pos.func.dfg.append_block_param(block1, types::I32);
pos.func.layout.set_cold(block1);
}
assert_eq!(
func.to_string(),
"function u0:0() fast {\nblock0 cold:\n\nblock1(v0: i32) cold:\n}\n"
);
}
}