Skip to content

Commit

Permalink
Auto merge of #116767 - cjgillot:alloc-normalize, r=oli-obk
Browse files Browse the repository at this point in the history
Normalize alloc-id in tests.

AllocIds are globally numbered in a rustc invocation. This makes them very sensitive to changes unrelated to what is being tested. This commit normalizes them by renumbering, in order of appearance in the output.

The renumbering allows to keep the identity, that a simple `allocN` wouldn't. This is useful when we have memory dumps.

cc `@saethlin`
r? `@oli-obk`
  • Loading branch information
bors committed Oct 17, 2023
2 parents 94ba57c + 1f90d85 commit 09df610
Show file tree
Hide file tree
Showing 106 changed files with 361 additions and 326 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ dependencies = [
"getopts",
"glob",
"home",
"indexmap 2.0.0",
"lazycell",
"libc",
"miow",
Expand Down
1 change: 1 addition & 0 deletions src/tools/compiletest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ colored = "2"
diff = "0.1.10"
unified-diff = "0.2.1"
getopts = "0.2"
indexmap = "2.0.0"
miropt-test-tools = { path = "../miropt-test-tools" }
build_helper = { path = "../build_helper" }
tracing = "0.1"
Expand Down
33 changes: 33 additions & 0 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4258,6 +4258,39 @@ impl<'test> TestCx<'test> {
V0_BACK_REF_RE.replace_all(&normalized, V0_BACK_REF_PLACEHOLDER).into_owned();
}

// AllocId are numbered globally in a compilation session. This can lead to changes
// depending on the exact compilation flags and host architecture. Meanwhile, we want
// to keep them numbered, to see if the same id appears multiple times.
// So we remap to deterministic numbers that only depend on the subset of allocations
// that actually appear in the output.
// We use uppercase ALLOC to distinguish from the non-normalized version.
{
let mut seen_allocs = indexmap::IndexSet::new();

// The alloc-id appears in pretty-printed allocations.
let re = Regex::new(r"╾─*a(lloc)?([0-9]+)(\+0x[0-9]+)?─*╼").unwrap();
normalized = re
.replace_all(&normalized, |caps: &Captures<'_>| {
// Renumber the captured index.
let index = caps.get(2).unwrap().as_str().to_string();
let (index, _) = seen_allocs.insert_full(index);
let offset = caps.get(3).map_or("", |c| c.as_str());
// Do not bother keeping it pretty, just make it deterministic.
format!("╾ALLOC{index}{offset}╼")
})
.into_owned();

// The alloc-id appears in a sentence.
let re = Regex::new(r"\balloc([0-9]+)\b").unwrap();
normalized = re
.replace_all(&normalized, |caps: &Captures<'_>| {
let index = caps.get(1).unwrap().as_str().to_string();
let (index, _) = seen_allocs.insert_full(index);
format!("ALLOC{index}")
})
.into_owned();
}

// Custom normalization rules
for rule in custom_rules {
let re = Regex::new(&rule.0).expect("bad regex in custom normalization rule");
Expand Down
8 changes: 4 additions & 4 deletions tests/mir-opt/building/custom/consts.statics.built.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ fn statics() -> () {
let mut _2: *mut i32;

bb0: {
_1 = const {alloc1: &i32};
_2 = const {alloc2: *mut i32};
_1 = const {ALLOC0: &i32};
_2 = const {ALLOC1: *mut i32};
return;
}
}

alloc2 (static: T, size: 4, align: 4) {
ALLOC1 (static: T, size: 4, align: 4) {
0a 0a 0a 0a │ ....
}

alloc1 (static: S, size: 4, align: 4) {
ALLOC0 (static: S, size: 4, align: 4) {
05 05 05 05 │ ....
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ fn main() -> () {
}
}

alloc1 (size: 3, align: 1) {
ALLOC0 (size: 3, align: 1) {
66 6f 6f │ foo
}
36 changes: 18 additions & 18 deletions tests/mir-opt/const_allocation.main.ConstProp.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> () {
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = const {alloc1: &&[(Option<i32>, &[&str])]};
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
_1 = (*_2);
StorageDead(_2);
StorageDead(_1);
Expand All @@ -17,43 +17,43 @@ fn main() -> () {
}
}

alloc1 (static: FOO, size: 8, align: 4) {
─alloc19─03 00 00 00 │ ╾──╼....
ALLOC9 (static: FOO, size: 8, align: 4) {
ALLOC003 00 00 00 │ ╾──╼....
}

alloc19 (size: 48, align: 4) {
0x0000 00 00 00 __ __ __ __ ╾─alloc6──00 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾─alloc10─02 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00─alloc15─03 00 00 00 │ ....*...╾──╼....
ALLOC0 (size: 48, align: 4) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC100 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾ALLOC202 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00ALLOC303 00 00 00 │ ....*...╾──╼....
}

alloc6 (size: 0, align: 4) {}
ALLOC1 (size: 0, align: 4) {}

alloc10 (size: 16, align: 4) {
─alloc9──03 00 00 00─alloc11─03 00 00 00 │ ╾──╼....╾──╼....
ALLOC2 (size: 16, align: 4) {
ALLOC403 00 00 00ALLOC503 00 00 00 │ ╾──╼....╾──╼....
}

alloc9 (size: 3, align: 1) {
ALLOC4 (size: 3, align: 1) {
66 6f 6f │ foo
}

alloc11 (size: 3, align: 1) {
ALLOC5 (size: 3, align: 1) {
62 61 72 │ bar
}

alloc15 (size: 24, align: 4) {
0x00 │ ╾─alloc14─03 00 00 00─alloc16─03 00 00 00 │ ╾──╼....╾──╼....
0x10 │ ╾─alloc17─04 00 00 00 │ ╾──╼....
ALLOC3 (size: 24, align: 4) {
0x00 │ ╾ALLOC603 00 00 00ALLOC703 00 00 00 │ ╾──╼....╾──╼....
0x10 │ ╾ALLOC804 00 00 00 │ ╾──╼....
}

alloc14 (size: 3, align: 1) {
ALLOC6 (size: 3, align: 1) {
6d 65 68 │ meh
}

alloc16 (size: 3, align: 1) {
ALLOC7 (size: 3, align: 1) {
6d 6f 70 │ mop
}

alloc17 (size: 4, align: 1) {
ALLOC8 (size: 4, align: 1) {
6d c3 b6 70 │ m..p
}
40 changes: 20 additions & 20 deletions tests/mir-opt/const_allocation.main.ConstProp.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> () {
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = const {alloc1: &&[(Option<i32>, &[&str])]};
_2 = const {ALLOC9: &&[(Option<i32>, &[&str])]};
_1 = (*_2);
StorageDead(_2);
StorageDead(_1);
Expand All @@ -17,47 +17,47 @@ fn main() -> () {
}
}

alloc1 (static: FOO, size: 16, align: 8) {
───────alloc19───────03 00 00 00 00 00 00 00 │ ╾──────╼........
ALLOC9 (static: FOO, size: 16, align: 8) {
ALLOC003 00 00 00 00 00 00 00 │ ╾──────╼........
}

alloc19 (size: 72, align: 8) {
0x0000 00 00 00 __ __ __ __ ╾───────alloc6────────╼ │ ....░░░░╾──────╼
ALLOC0 (size: 72, align: 8) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC1╼ │ ....░░░░╾──────╼
0x1000 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
0x20 │ ╾───────alloc10───────02 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00───────alloc15───────╼ │ ....*...╾──────╼
0x20 │ ╾ALLOC202 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00ALLOC3╼ │ ....*...╾──────╼
0x4003 00 00 00 00 00 00 00 │ ........
}

alloc6 (size: 0, align: 8) {}
ALLOC1 (size: 0, align: 8) {}

alloc10 (size: 32, align: 8) {
0x00 │ ╾───────alloc9────────03 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾───────alloc11───────03 00 00 00 00 00 00 00 │ ╾──────╼........
ALLOC2 (size: 32, align: 8) {
0x00 │ ╾ALLOC403 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾ALLOC503 00 00 00 00 00 00 00 │ ╾──────╼........
}

alloc9 (size: 3, align: 1) {
ALLOC4 (size: 3, align: 1) {
66 6f 6f │ foo
}

alloc11 (size: 3, align: 1) {
ALLOC5 (size: 3, align: 1) {
62 61 72 │ bar
}

alloc15 (size: 48, align: 8) {
0x00 │ ╾───────alloc14───────03 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾───────alloc16───────03 00 00 00 00 00 00 00 │ ╾──────╼........
0x20 │ ╾───────alloc17───────04 00 00 00 00 00 00 00 │ ╾──────╼........
ALLOC3 (size: 48, align: 8) {
0x00 │ ╾ALLOC603 00 00 00 00 00 00 00 │ ╾──────╼........
0x10 │ ╾ALLOC703 00 00 00 00 00 00 00 │ ╾──────╼........
0x20 │ ╾ALLOC804 00 00 00 00 00 00 00 │ ╾──────╼........
}

alloc14 (size: 3, align: 1) {
ALLOC6 (size: 3, align: 1) {
6d 65 68 │ meh
}

alloc16 (size: 3, align: 1) {
ALLOC7 (size: 3, align: 1) {
6d 6f 70 │ mop
}

alloc17 (size: 4, align: 1) {
ALLOC8 (size: 4, align: 1) {
6d c3 b6 70 │ m..p
}
34 changes: 17 additions & 17 deletions tests/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> () {
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]};
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
_1 = (*_2);
StorageDead(_2);
StorageDead(_1);
Expand All @@ -17,42 +17,42 @@ fn main() -> () {
}
}

alloc1 (static: FOO, size: 8, align: 4) {
─alloc23─03 00 00 00 │ ╾──╼....
ALLOC9 (static: FOO, size: 8, align: 4) {
ALLOC003 00 00 00 │ ╾──╼....
}

alloc23 (size: 48, align: 4) {
0x0000 00 00 00 __ __ __ __ ╾─alloc10─00 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾─alloc15─02 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00─alloc21─03 00 00 00 │ ....*...╾──╼....
ALLOC0 (size: 48, align: 4) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC100 00 00 00 │ ....░░░░╾──╼....
0x1000 00 00 00 __ __ __ __ ╾ALLOC202 00 00 00 │ ....░░░░╾──╼....
0x2001 00 00 00 2a 00 00 00ALLOC303 00 00 00 │ ....*...╾──╼....
}

alloc10 (size: 0, align: 4) {}
ALLOC1 (size: 0, align: 4) {}

alloc15 (size: 8, align: 4) {
─alloc13─╼ ╾─alloc14─╼ │ ╾──╼╾──╼
ALLOC2 (size: 8, align: 4) {
ALLOC4╼ ╾ALLOC5╼ │ ╾──╼╾──╼
}

alloc13 (size: 1, align: 1) {
ALLOC4 (size: 1, align: 1) {
05.
}

alloc14 (size: 1, align: 1) {
ALLOC5 (size: 1, align: 1) {
06.
}

alloc21 (size: 12, align: 4) {
─a18+0x3╼ ╾─alloc19─╼ ╾─a20+0x2╼ │ ╾──╼╾──╼╾──╼
ALLOC3 (size: 12, align: 4) {
ALLOC6+0x3╼ ╾ALLOC7╼ ╾ALLOC8+0x2╼ │ ╾──╼╾──╼╾──╼
}

alloc18 (size: 4, align: 1) {
ALLOC6 (size: 4, align: 1) {
2a 45 15 6f │ *E.o
}

alloc19 (size: 1, align: 1) {
ALLOC7 (size: 1, align: 1) {
2a │ *
}

alloc20 (size: 4, align: 1) {
ALLOC8 (size: 4, align: 1) {
2a 45 15 6f │ *E.o
}
36 changes: 18 additions & 18 deletions tests/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> () {
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]};
_2 = const {ALLOC9: &&[(Option<i32>, &[&u8])]};
_1 = (*_2);
StorageDead(_2);
StorageDead(_1);
Expand All @@ -17,45 +17,45 @@ fn main() -> () {
}
}

alloc1 (static: FOO, size: 16, align: 8) {
───────alloc23───────03 00 00 00 00 00 00 00 │ ╾──────╼........
ALLOC9 (static: FOO, size: 16, align: 8) {
ALLOC003 00 00 00 00 00 00 00 │ ╾──────╼........
}

alloc23 (size: 72, align: 8) {
0x0000 00 00 00 __ __ __ __ ╾───────alloc10───────╼ │ ....░░░░╾──────╼
ALLOC0 (size: 72, align: 8) {
0x0000 00 00 00 __ __ __ __ ╾ALLOC1╼ │ ....░░░░╾──────╼
0x1000 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
0x20 │ ╾───────alloc15───────02 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00───────alloc21───────╼ │ ....*...╾──────╼
0x20 │ ╾ALLOC202 00 00 00 00 00 00 00 │ ╾──────╼........
0x3001 00 00 00 2a 00 00 00ALLOC3╼ │ ....*...╾──────╼
0x4003 00 00 00 00 00 00 00 │ ........
}

alloc10 (size: 0, align: 8) {}
ALLOC1 (size: 0, align: 8) {}

alloc15 (size: 16, align: 8) {
───────alloc13───────╼ ╾───────alloc14───────╼ │ ╾──────╼╾──────╼
ALLOC2 (size: 16, align: 8) {
ALLOC4╼ ╾ALLOC5╼ │ ╾──────╼╾──────╼
}

alloc13 (size: 1, align: 1) {
ALLOC4 (size: 1, align: 1) {
05.
}

alloc14 (size: 1, align: 1) {
ALLOC5 (size: 1, align: 1) {
06.
}

alloc21 (size: 24, align: 8) {
0x00 │ ╾─────alloc18+0x3─────╼ ╾───────alloc19───────╼ │ ╾──────╼╾──────╼
0x10 │ ╾─────alloc20+0x2─────╼ │ ╾──────╼
ALLOC3 (size: 24, align: 8) {
0x00 │ ╾ALLOC6+0x3╼ ╾ALLOC7╼ │ ╾──────╼╾──────╼
0x10 │ ╾ALLOC8+0x2╼ │ ╾──────╼
}

alloc18 (size: 4, align: 1) {
ALLOC6 (size: 4, align: 1) {
2a 45 15 6f │ *E.o
}

alloc19 (size: 1, align: 1) {
ALLOC7 (size: 1, align: 1) {
2a │ *
}

alloc20 (size: 4, align: 1) {
ALLOC8 (size: 4, align: 1) {
2a 45 15 6f │ *E.o
}
Loading

0 comments on commit 09df610

Please sign in to comment.