High memory consumption of ownership analysis during elimination of unneeded permissions
Running the ownership analysis on zstd currently crashes due to a lack of memory on my 32GB machine. I've tracked down the issue to a specific phase of the intra-procedural ownership analysis:
The intra-procedural analysis computes permission constraints of form a <= b for all variables in a function. After the computation for a function is complete, constraints on function-local variables are to be optimized away. This happens in such a way, that indirect constraints are retained, e.g. if there are constraints a <= c, b <= c and c <= d and c is to be removed, then those three constraints are replaced with new constraints a <= d and b <= d. This is implemented in c2rust-refactor::analysis::ownership::constraint::retain_perms and works totally fine in most cases.
The analysis sometimes introduces terms of the form min(a, b) <= c, which can be read as "there is one p of set {a, b}, such that p <= c". Consider following example:
min(q, p1, p2) <= min(p, r1, r2)
l1 <= q
l2 <= q
l3 <= q
q <= g1
q <= g2
q <= g3
q <= g4
Eliminating q will lead to the following set of new constraints just for the very first equation:
min(l1, p1, p2) <= min(g1, r1, r2)
min(l1, p1, p2) <= min(g2, r1, r2)
min(l1, p1, p2) <= min(g3, r1, r2)
min(l1, p1, p2) <= min(g4, r1, r2)
min(l2, p1, p2) <= min(g1, r1, r2)
min(l2, p1, p2) <= min(g2, r1, r2)
min(l2, p1, p2) <= min(g3, r1, r2)
min(l2, p1, p2) <= min(g4, r1, r2)
min(l3, p1, p2) <= min(g1, r1, r2)
min(l3, p1, p2) <= min(g2, r1, r2)
min(l3, p1, p2) <= min(g3, r1, r2)
min(l3, p1, p2) <= min(g4, r1, r2)
This quite blows up. Not only are there significantly more constraints, but also every min allocates a new slice in an arena that won't be deallocated until the analysis finishes. Even more problematic, if p1 is eliminated after q, that could potentially generate additional constraints for every constraint in the list. Running on zstd I think I observed cases, where the blow-up factor got over 5_000_000 new constraints for a single replaced constraint before it crashed. (That's assuming I didn't mess anything up counting)
I am not yet able to fully reason about the analysis, so I am not entirely sure on how to optimize this issue.
The function where this happened in zstd is ZSTD_compressBlock_opt_generic
Closing as c2rust refactor in its current form is no longer being developed.