Skip to content

ptodsl: support Python and/or short-circuit in AST rewrite (#1332) - #1346

Open
jimmychou0 wants to merge 5 commits into
hw-native-sys:mainfrom
jimmychou0:zjm/issue-fix-1332
Open

ptodsl: support Python and/or short-circuit in AST rewrite (#1332)#1346
jimmychou0 wants to merge 5 commits into
hw-native-sys:mainfrom
jimmychou0:zjm/issue-fix-1332

Conversation

@jimmychou0

Copy link
Copy Markdown
Contributor

Summary

Support Python and / or between PTODSL runtime predicates in @pto.jit / @pto.func AST-rewritten kernels (issue #1332).

Python and/or are not overloadable: evaluating a and b forces a truth check that calls __bool__ on a PTODSL runtime value during tracing and raises. This PR lowers ast.BoolOp expressions into device-side shor-circuit scf.if regions 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 _BoolOpRewriter pass that rewrites every ast.BoolOp into 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, and if/while conditions.
  • ptodsl/_control_flow.py: internal _short_circuit_and / _short_circuit_or helpers built on the existing pto.if_ + br.assign machinery, producing a result-bearing scf.if. The LHS is the control condition (integer operands use non-zero truthiness); the branch merge reuses the existing i1 reconciliation rules; Python bool literals materialize as i1 constants (flag and True).
  • Plain Python operands (lists, tuples, strings, ...) keep native Python truthiness at trace time (e.g. TileOps templates use loops or None where loops is a Python list); floating-point control values raise a clear diagnostic.
  • ptodsl/pto.py: export the two internal helpers.
  • No backend changes: existing scf.if / comparisons already express the semantics.

Tests

  • New ptodsl/tests/test_issue_1332_boolop_short_circuit.py covering: the issue's original and/or kernels, 3+ operand chains, RHS with division/load inside the guarded region, and/or in if conditions / native while tests / call arguments / return, static short-circuit (False and rhs, True or rhs never traced), runtime integer LHS with i1 merge, f and True bool-literal materialization, and diagnostics (float control, incompatible branch merge, ast_rewrite=False, source-less functions).
  • Docs: new Short-circuit and / or section in ptodsl/docs/user_guide/05-control-flow.md with a compile-mode example, plus a summary-table row.

Validation

  • Full fresh build (ptoas + PTOPythonModules) and the complete ptodsl/tests suite on dev-481211 (aarch64 Linux / LLVM 19 / Python 3.10): 42/42 pass, including test_jit_compile, test_scf_while_ast, test_issue_1126_bool_merge, all tilelib suites, and the new issue-1332 test.

Fixes #1332.

…-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
jimmychou0 marked this pull request as ready for review August 26, 2026 00:39
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.

[Feature] Support Python native and and or expressions in PTODSL

1 participant