-
Notifications
You must be signed in to change notification settings - Fork 142
Add pure expression inliner infrastructure for quantifier bodies #4567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
feliperodri
wants to merge
4
commits into
model-checking:main
Choose a base branch
from
feliperodri:inline-pure-expressions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
32eb808
Add pure expression inliner infrastructure for quantifier bodies
feliperodri 45648b6
Fix clippy redundant_closure warning in substitute_symbol
feliperodri 8b2600a
Improve robustness of pure expression inliner
feliperodri 9f57a2a
Use idiomatic tuple access and restore dev doc walkthrough
feliperodri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Pure Expression Inliner | ||
|
|
||
| ## Overview | ||
|
|
||
| The pure expression inliner (`inline_as_pure_expr`) inlines function calls | ||
| within expression trees as side-effect-free expressions. Unlike the existing | ||
| `inline_function_calls_in_expr` which wraps inlined bodies in CBMC | ||
| `StatementExpression` nodes, this produces pure expression trees. | ||
|
|
||
| ## How It Works | ||
|
|
||
| For a function call `f(arg1, arg2)` where `f` is defined as: | ||
| ```c | ||
| ret_type f(param1, param2) { | ||
| local1 = expr1(param1); | ||
| local2 = expr2(local1, param2); | ||
| return local2; | ||
| } | ||
| ``` | ||
|
|
||
| The pure inliner: | ||
| 1. **Collects assignments**: `{local1 → expr1(param1), local2 → expr2(local1, param2)}` | ||
| 2. **Finds the return symbol**: `local2` | ||
| 3. **Resolves intermediates**: `local2` → `expr2(local1, param2)` → `expr2(expr1(param1), param2)` | ||
| 4. **Flattens StatementExpressions**: e.g., `({ assert(2!=0); assume(2!=0); i%2 })` → `i%2` | ||
| 5. **Substitutes parameters**: `expr2(expr1(arg1), arg2)` | ||
| 6. **Recursively inlines** any remaining function calls in the result | ||
|
|
||
| The resolution step (3) uses `Expr::substitute_symbol` iteratively until a | ||
| fixed point is reached. Change detection uses the `(Expr, bool)` return from | ||
| `substitute_symbol` — no string comparison needed. | ||
|
|
||
| ## Soundness Implications | ||
|
|
||
| **Checked arithmetic in quantifier bodies**: When flattening `StatementExpression` | ||
| nodes (e.g., from checked division or remainder), the pure inliner drops the | ||
| `Assert` and `Assume` statements that check for overflow and division by zero. | ||
|
|
||
| - **Division by zero** inside a quantifier body will NOT be detected. | ||
| - **Arithmetic overflow** inside a quantifier body will NOT be detected. | ||
|
|
||
| **Future improvement**: The dropped assertions could be hoisted outside the | ||
| quantifier as preconditions, preserving soundness while keeping the body pure. | ||
|
|
||
| ## Limitations | ||
|
|
||
| - **No control flow**: Functions with `if`/`else` or `match` that produce | ||
| multiple assignments to the return variable are not fully supported. The | ||
| inliner takes the last assignment and emits a `tracing::debug!` diagnostic. | ||
| - **No loops**: Functions containing loops cannot be inlined as pure expressions. | ||
| - **No recursion**: Recursive functions are detected and the original expression | ||
| is returned unchanged (with a `tracing::warn!` diagnostic). No ICE. | ||
| - **StatementExpression in substitute_symbol**: `Expr::substitute_symbol` does | ||
| NOT recurse into `StatementExpression` nodes. These must be flattened via | ||
| `inline_as_pure_expr` before substitution. | ||
|
|
||
| ## API | ||
|
|
||
| ```rust | ||
| // Public entry point — manages the visited set internally | ||
| pub fn inline_as_pure_expr_toplevel(&self, expr: &Expr) -> Expr; | ||
|
|
||
| // Expr method — returns (new_expr, changed) for reliable change detection | ||
| pub fn substitute_symbol(self, old_id: &InternedString, replacement: &Expr) -> (Expr, bool); | ||
| ``` | ||
|
|
||
| ## Files | ||
|
|
||
| - `cprover_bindings/src/goto_program/expr.rs` — `Expr::substitute_symbol()` | ||
| - `kani-compiler/src/codegen_cprover_gotoc/context/goto_ctx.rs` — `inline_as_pure_expr()`, | ||
| `inline_as_pure_expr_toplevel()`, `inline_call_as_pure_expr()`, | ||
| `collect_assignments_from_stmt()`, `find_return_symbol_in_stmt()`, | ||
| `resolve_intermediates_iterative()` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tautschnig this is similar to CBMC behavior, correct? Let me know if this raises any concerns.