Reason about TailDuplicator correctness heuristics

I’m learning the heuristics in Tail Duplicator. The comments are quite educational but I become very interested to know how did the initial implementation catch the erroneous scenarios and add early returns.

It’s not intuitive to me these early returns are needed (or otherwise cause errors). My question is, how are these knowledge typically gained (from a methodology perspective)? Any suggestions to teach myself to gain these knowledge? Are some correctness heuristics driven by failed assertions in the relevant llvm libraries (if yes, are assertion-driven heuristics common)?

p.s. For performance heuristics, like Do not duplicate 'return' instructions if this is a pre-regalloc run or Avoid duplicating calls before register allocation. ( are easier to understand.

Examples of correctness heuristics

// Non-duplicable things shouldn't be tail-duplicated.
// CFI instructions are marked as non-duplicable, because Darwin compact
// unwind info emission can't handle multiple prologue setups. In case of
// DWARF, allow them be duplicated, so that their existence doesn't prevent
// tail duplication of some basic blocks, that would be duplicated otherwise.

This is from

// Convergent instructions can be duplicated only if doing so doesn't add
// new control dependencies, which is what we're going to do here.

This is from

The initial implementation probably didn’t. In particular noduplicate was added specifically to stop this transformation

1 Like