Roll back compile cache entry when the first trace throws#3635
Merged
zcbenz merged 2 commits intoJun 9, 2026
Merged
Conversation
The per-call lambda in compile() marks a cache entry non-empty before it traces it. If compile_trace (or the dfs, simplify, or fuse steps) throws on the first trace, the entry is left empty == false with no inputs, outputs, or tape. A later call with matching inputs then finds that entry, sees it is not empty, skips tracing, and replaces against an empty tape, returning empty outputs as a spurious success instead of re-tracing or re-raising. A nullary compiled function is the sharpest case since its inputs always match. Wrap the fill in a try/catch that resets entry.empty to true on any exception, so a retry re-traces cleanly and a failure stays a failure. Fixes ml-explore#3624
Collaborator
|
I preferred the other fix: #3624 (comment) |
Contributor
Author
|
Done, reworked to that approach in 0fde210. The flag move is all that is needed since a failed first trace now leaves the entry empty and the retry path overwrites the partially assigned fields when it re-traces. The regression test is unchanged and still covers the throwing first trace, the throwing retry, and the clean retrace. |
zcbenz
approved these changes
Jun 8, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed changes
Fixes #3624.
The per-call lambda built by
compile()marks a cache entry non-empty before it traces it:If
compile_trace(or the subsequent dfs, simplify, or fuse steps) throws on the first trace, the entry is left withempty == falsebut noinputs,outputs, or tape. A later call with matching inputs thenfind()s that entry, sees it is not empty, skips tracing, andcompile_replaces against an empty tape, returning empty outputs as a spurious success instead of re-tracing or re-raising. A nullary compiled function is the sharpest case since its inputs always match.This moves
entry.empty = falseto after the trace, dfs, simplify, and fuse steps complete, so a throwing first trace leaves the entry empty and a later call re-traces cleanly while a failure stays a failure. This is the approach @zcbenz endorsed on the issue. A retry reassignsconstants,inputs,outputs, and the tape when it re-enters the fill path, so the partially assigned fields from the failed attempt are overwritten.Testing
Added a C++ regression test in
tests/compile_tests.cppthat compiles a nullary function which raises on its first invocation, then calls it twice. Without the fix the second call returns 0 outputs instead of raising. With the fix both calls raise, and once the function stops raising a retry traces cleanly and returns the correct result.Verified on macOS (CPU-only build): the new test fails on
main(second call did not throw,out.size()was 0) and passes with the fix. The full compile suite (23 cases, 111 assertions) is green. Both touched files pass clang-format.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes