Skip to content

Optimizing generated termination checks to omit unfolds and condition…#879

Merged
marcoeilers merged 1 commit into
masterfrom
meilers_optimize_termination_checks
Jul 3, 2025
Merged

Optimizing generated termination checks to omit unfolds and condition…#879
marcoeilers merged 1 commit into
masterfrom
meilers_optimize_termination_checks

Conversation

@marcoeilers

Copy link
Copy Markdown
Contributor

For unfolding-expressions of the form unfolding acc(P(args), p) in e, the termination plugin currently generates the following (simplified) termination check:

<check for args>
<check for p>
if (*) {
  unfold acc(P(args), p)
  <check for e>
  assume false
}

If there are no conditions to check for e, this code is currently still generated; in particular, the predicate is still unfolded inside a branch, and subsequently, we assume false. The unfold is completely unnecessary in this scenario though and could just be omitted.
Even though the code generated by the plugin is subsequently run through the Simplifier, this block is not removed, because it's not empty (it unfolds a predicate and assumes false, after all).
While this is not a problem for individual unfolds, the effect can cascade into situations where we perform multiple unfolds inside branches after other unfolds only to not actually check anything in the end, as shown in viperproject/silicon#934.

So this PR modifies the termination plugin to generate some code blocks that exist to check termination conditions for some expressions only if there are actually termination conditions to check for those expressions, and omits them otherwise. In particular, we do this for unfoldings (omit the unfold, as explained above), implications, disjunctions, and some conjunctions (remove the need to evaluate the condition), let-expressions (remove the need to evaluate the bound expression).

@jcp19 jcp19 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marcoeilers marcoeilers merged commit 48acd1b into master Jul 3, 2025
6 of 7 checks passed
@marcoeilers marcoeilers deleted the meilers_optimize_termination_checks branch July 3, 2025 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants