Skip to content

Eliminate redundant exception checks in try/except bodies#1091

Merged
tautschnig merged 7 commits into
mainfrom
tautschnig/isError_transform
May 5, 2026
Merged

Eliminate redundant exception checks in try/except bodies#1091
tautschnig merged 7 commits into
mainfrom
tautschnig/isError_transform

Conversation

@tautschnig
Copy link
Copy Markdown
Contributor

Only insert isError(maybe_except) checks after statements that actually modify maybe_except (procedure calls that may throw). Previously, every statement in a try body got an isError check, creating massive ite nesting in verification conditions.

For a chosen benchmark this reduces 62 down to 8 isError checks (87% reduction).

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Only insert isError(maybe_except) checks after statements that
actually modify maybe_except (procedure calls that may throw).
Previously, every statement in a try body got an isError check,
creating massive ite nesting in verification conditions.

For a chosen benchmark this reduces 62 down to 8 isError checks (87%
reduction).

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes Python try/except translation in PythonToLaurel by reducing redundant isError(maybe_except) checks, aiming to shrink verification-condition size while preserving exception-control-flow behavior.

Changes:

  • Add a modifiesMaybeExcept predicate to decide when to insert isError(maybe_except) checks in try bodies.
  • Emit exitToHandler checks only after statements deemed capable of updating maybe_except, instead of after every statement.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Strata/Languages/Python/PythonToLaurel.lean
Comment thread Strata/Languages/Python/PythonToLaurel.lean Outdated
tautschnig and others added 2 commits April 30, 2026 22:11
- Make modifiesMaybeExcept recursive: descend into IfThenElse, While,
  and Block to detect maybe_except assignments in nested control flow.
  Previously only checked top-level and one level of Block.
- Add test_try_except_nested.py: exception-raising call inside an if
  branch within a try body, exercising the nested detection path.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
The recursive modifiesMaybeExcept correctly eliminates redundant
exception checks at lines 12 and 15 (which were inside try/except
bodies with nested control flow), reducing from 6 passed + 5
inconclusive to 2 passed + 3 inconclusive.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@MikaelMayer MikaelMayer left a comment

Choose a reason for hiding this comment

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

The optimization idea is sound: skipping isError(maybe_except) checks after statements that provably don't modify maybe_except is a valid way to reduce ite nesting. The existing review comments about the shallow traversal in modifiesMaybeExcept (missing writes inside IfThenElse, While, nested try, With, etc.) are the main correctness concern and I won't repeat them.

One additional observation: the existing test suite has no try-body that contains a modeled procedure call (one that actually sets maybe_except). Every current try-body test uses only simple assignments or raise. This means the optimization's core behavior — inserting isError checks selectively after throwing calls — is exercised by zero tests, not just the nested-construct edge case.

Comment thread Strata/Languages/Python/PythonToLaurel.lean Outdated
thanhnguyen-aws
thanhnguyen-aws previously approved these changes May 1, 2026
tautschnig and others added 2 commits May 4, 2026 10:17
Exercises the core optimization: isError checks are selectively
inserted after modeled calls (Any_get for dict access, PAdd for
arithmetic) that set maybe_except. Covers:
- Dict access in try body (top-level)
- Arithmetic in try body (top-level)
- Dict access inside nested if in try body

All 14 checks pass, confirming the optimization correctly inserts
isError checks after modeled calls while skipping non-modifying
statements.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
thanhnguyen-aws
thanhnguyen-aws previously approved these changes May 4, 2026
@tautschnig
Copy link
Copy Markdown
Contributor Author

@keyboardDrummer-bot Please resolve git conflicts.

The optimization only eliminates redundant isError checks in the try
body. The procedure calls at lines 12 and 15 are in except handlers,
so their verification results remain in the output.
@tautschnig tautschnig added this pull request to the merge queue May 5, 2026
Merged via the queue into main with commit 5dccfcc May 5, 2026
22 checks passed
@tautschnig tautschnig deleted the tautschnig/isError_transform branch May 5, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants