ptodsl: support Python and/or short-circuit in AST rewrite (#1332) - #1346
Open
jimmychou0 wants to merge 5 commits into
Open
ptodsl: support Python and/or short-circuit in AST rewrite (#1332)#1346jimmychou0 wants to merge 5 commits into
jimmychou0 wants to merge 5 commits into
Conversation
…-sys#1332) Python and/or between PTODSL runtime predicates used to call __bool__ on a runtime value during tracing. The AST rewriter now lowers ast.BoolOp expressions to device-side short-circuit scf.if regions (pto._short_circuit_and/or) that keep Python operand semantics, including non-zero integer truthiness, i1 bool-literal merges, and native truthiness for plain Python operands. No backend changes.
jimmychou0
force-pushed
the
zjm/issue-fix-1332
branch
from
August 25, 2026 11:04
7f1682d to
bc2a295
Compare
jimmychou0
marked this pull request as ready for review
August 26, 2026 00:39
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.
Summary
Support Python
and/orbetween PTODSL runtime predicates in@pto.jit/@pto.funcAST-rewritten kernels (issue #1332).Python
and/orare not overloadable: evaluatinga and bforces a truth check that calls__bool__on a PTODSL runtime value during tracing and raises. This PR lowersast.BoolOpexpressions into device-side shor-circuitscf.ifregions that keep Python short-circuit semantics: the RHS is only traced inside the branch that Python would actually evaluate.Implementation
ptodsl/_ast_rewrite.py: new_BoolOpRewriterpass that rewrites everyast.BoolOpinto a right-nested lazy helper call (a and b and c->pto._short_circuit_and(a, lambda: pto._short_circuit_and(b, lambda: c))). Covers assignments, call arguments,return, andif/whileconditions.ptodsl/_control_flow.py: internal_short_circuit_and/_short_circuit_orhelpers built on the existingpto.if_+br.assignmachinery, producing a result-bearingscf.if. The LHS is the control condition (integer operands use non-zero truthiness); the branch merge reuses the existingi1reconciliation rules; Pythonboolliterals materialize asi1constants (flag and True).loops or Nonewhereloopsis a Python list); floating-point control values raise a clear diagnostic.ptodsl/pto.py: export the two internal helpers.scf.if/ comparisons already express the semantics.Tests
ptodsl/tests/test_issue_1332_boolop_short_circuit.pycovering: the issue's originaland/orkernels, 3+ operand chains, RHS with division/load inside the guarded region,and/orinifconditions / nativewhiletests / call arguments /return, static short-circuit (False and rhs,True or rhsnever traced), runtime integer LHS withi1merge,f and Truebool-literal materialization, and diagnostics (float control, incompatible branch merge,ast_rewrite=False, source-less functions).and/orsection inptodsl/docs/user_guide/05-control-flow.mdwith a compile-mode example, plus a summary-table row.Validation
ptoas+PTOPythonModules) and the completeptodsl/testssuite on dev-481211 (aarch64 Linux / LLVM 19 / Python 3.10): 42/42 pass, includingtest_jit_compile,test_scf_while_ast,test_issue_1126_bool_merge, all tilelib suites, and the new issue-1332 test.Fixes #1332.