Skip to content

Commit 7cf5f05

Browse files
authored
Cranelift: remove slow invariant validation in cfg(fuzzing) from MachBuffer. (#4038)
Following the merge of regalloc2 support, this became slower because we are stricter about the critical-edge invariant, generating a separate edge block for every out-edge even if two or more out-edges go to the same successor (this is significant in cases of `br_table` with many entries having the same target block, for example). Many of those edge blocks are empty and end up collapsed by the MachBuffer, which leads to a large set of aliased labels. The invariant validation will dutifully iterate over all the data structures at every step, validating all of our conditions. But this gets way slower in the new context, to the point that we'll probably have some fuzz timeouts. This was pointed out in [1] but I missed removing this in #3989. Given that `MachBuffer` has been around for nearly two years now, has been fuzzed continuously with the invariant validation for that time, and also has a correctness proof in the comments, it's probably reasonable to remove this high (recently increased) cost from the fuzzing-specific compilation configuration. [1] #3989 (comment)
1 parent cf533a8 commit 7cf5f05

1 file changed

Lines changed: 0 additions & 57 deletions

File tree

cranelift/codegen/src/machinst/buffer.rs

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -326,62 +326,6 @@ impl<I: VCodeInst> MachBuffer<I> {
326326
}
327327
}
328328

329-
/// Debug-only: check invariants of labels and branch-records described
330-
/// under "Branch-optimization Correctness" above.
331-
///
332-
/// These invariants are checked at branch-simplification
333-
/// time. Note that they may be temporarily violated at other
334-
/// times, e.g. after calling `add_{cond,uncond}_branch()` and
335-
/// before emitting branch bytes.
336-
fn check_label_branch_invariants(&self) {
337-
if !cfg!(fuzzing) {
338-
return;
339-
}
340-
let cur_off = self.cur_offset();
341-
// Check that every entry in latest_branches has *correct*
342-
// labels_at_this_branch lists. We do not check completeness because
343-
// that would require building a reverse index, which is too slow even
344-
// for a debug invariant check.
345-
let mut last_end = 0;
346-
for b in &self.latest_branches {
347-
debug_assert!(b.start < b.end);
348-
debug_assert!(b.end <= cur_off);
349-
debug_assert!(b.start >= last_end);
350-
last_end = b.end;
351-
for &l in &b.labels_at_this_branch {
352-
debug_assert_eq!(self.resolve_label_offset(l), b.start);
353-
debug_assert_eq!(self.label_aliases[l.0 as usize], UNKNOWN_LABEL);
354-
}
355-
}
356-
357-
// Check that every label is unresolved, or resolved at or
358-
// before cur_offset. If at cur_offset, must be in
359-
// `labels_at_tail`. We skip labels that are aliased to
360-
// others already.
361-
for (i, &off) in self.label_offsets.iter().enumerate() {
362-
let label = MachLabel(i as u32);
363-
if self.label_aliases[i] != UNKNOWN_LABEL {
364-
continue;
365-
}
366-
debug_assert!(off == UNKNOWN_LABEL_OFFSET || off <= cur_off);
367-
if off == cur_off {
368-
debug_assert!(
369-
self.labels_at_tail_off == cur_off && self.labels_at_tail.contains(&label)
370-
);
371-
}
372-
}
373-
374-
// Check that every label in `labels_at_tail_off` is precise, i.e.,
375-
// resolves to the cur offset.
376-
debug_assert!(self.labels_at_tail_off <= cur_off);
377-
if self.labels_at_tail_off == cur_off {
378-
for &l in &self.labels_at_tail {
379-
debug_assert_eq!(self.resolve_label_offset(l), cur_off);
380-
debug_assert_eq!(self.label_aliases[l.0 as usize], UNKNOWN_LABEL);
381-
}
382-
}
383-
}
384-
385329
/// Current offset from start of buffer.
386330
pub fn cur_offset(&self) -> CodeOffset {
387331
self.data.len() as CodeOffset
@@ -536,7 +480,6 @@ impl<I: VCodeInst> MachBuffer<I> {
536480
// offset and added it to the list (which contains all labels at the
537481
// current offset).
538482

539-
self.check_label_branch_invariants();
540483
self.optimize_branches();
541484

542485
// Post-invariant: by `optimize_branches()` (see argument there).

0 commit comments

Comments
 (0)