Skip to content

feat(codegen): validate bitwise op family - #2188

Draft
hashiqiqixian wants to merge 4 commits into
hw-native-sys:mainfrom
hashiqiqixian:feat/ptoas-b07-bitwise
Draft

feat(codegen): validate bitwise op family#2188
hashiqiqixian wants to merge 4 commits into
hw-native-sys:mainfrom
hashiqiqixian:feat/ptoas-b07-bitwise

Conversation

@hashiqiqixian

Copy link
Copy Markdown
Contributor

Summary

  • align tile.and_, tile.or_, tile.xor, tile.ands, tile.ors, and tile.xors with the current PTO-ISA dtype, valid-region, scalar, row-major layout, scratch, and aliasing contracts
  • remove tile dtype promotion/broadcasting from exact bitwise ops, preserve signed/unsigned tile widths across the cross-architecture IR contract, and reject unsigned scalar encodings that PTOAS cannot represent
  • add focused type and MemoryReuse coverage plus same-name runtime ST, and record A2/A3 hardware evidence in both status matrices

Validation

  • complete local diff review: passed
  • git diff --check: passed
  • Ruff check and format check for changed Python files: passed
  • serial CMake build and editable install on /data/chenshenai/test2: passed
  • focused bitwise contract UT: 48 passed
  • focused XOR aliasing UT: 1 passed
  • repository header, English-only, and docs parity checks: passed
  • A2/A3 codegen-only with latest PTOAS assembly: 18 passed; generated .pto files contain exact pto.tand, pto.tor, pto.txor, pto.tands, pto.tors, and pto.txors signatures
  • A2/A3 task-submit hardware, one precompile worker and one 18-case batch: 18/18 passed on device 1

Runtime coverage includes zero, all-one, and alternating bit patterns; signed/unsigned 8/16-bit tile forms; signed 8/16-bit scalar encoding; XOR scratch separation; and narrowed valid_shape. A5 hardware validation remains pending and is documented in the status matrix.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0b390eba-38b1-4e46-be72-60ed192f0c67

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Bitwise tile operations now enforce matching integer dtypes and valid shapes, support explicit scalar variants, require distinct XOR temporary buffers, register row-major requirements, add A2/A3 and A5 runtime coverage, and update English and Chinese documentation.

Changes

Bitwise tile operations

Layer / File(s) Summary
Bitwise IR contracts
src/ir/op/tile_ops/elementwise.cpp, tests/ut/ir/operators/test_tile_ops.py, tests/ut/ir/transforms/test_memory_reuse.py
Dedicated bitwise deduction validates supported dtypes, matching valid_shape, scalar types, and distinct XOR temporary buffers; unit tests cover accepted and rejected combinations and memory reuse.
Backend registration and runtime coverage
src/backend/common/pto_ops_elementwise.cpp, tests/st/runtime/ops/test_bitwise_binary.py
Scalar bitwise variants are added to the row-major allowlist, with parametrized A2/A3 and A5 tests for tile, immediate-scalar, SSA-scalar, and tail-shape cases.
Codegen and operation-status documentation
docs/en/dev/codegen/00-pto_codegen.md, docs/zh-cn/dev/codegen/00-pto_codegen.md, docs/en/dev/ptoas-op-status.md, docs/zh-cn/dev/ptoas-op-status.md
Tile-to-PTO mappings, ST verification notes, and aggregate coverage statistics are updated in English and Chinese documentation.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TestBitwiseBinaryFamily
  participant BitwiseCase
  participant TileBitwiseIR
  participant PTOAS
  TestBitwiseBinaryFamily->>BitwiseCase: create operation and tensor inputs
  BitwiseCase->>TileBitwiseIR: build bitwise tile operation
  TileBitwiseIR->>PTOAS: emit validated PTO bitwise instruction
  PTOAS-->>TestBitwiseBinaryFamily: return execution result
Loading

Possibly related PRs

Poem

A rabbit hops through bits so bright,
and, or, and xor take flight.
Shapes align and dtypes agree,
Scratch buffers stay alias-free.
Tests bloom across each tile—
Docs nibble carrots in style!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately reflects the main change: validating the bitwise op family.
Description check ✅ Passed The description is clearly related to the bitwise validation, tests, docs, and hardware coverage changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 92fc53b9ef

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/ir/op/tile_ops/elementwise.cpp
@hashiqiqixian
hashiqiqixian force-pushed the feat/ptoas-b07-bitwise branch from 4a27176 to 8ee9feb Compare July 29, 2026 17:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (1)
src/ir/op/tile_ops/elementwise.cpp (1)

342-349: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Make the tmp distinctness check resolve var identity, not ExprPtr identity.

args[2].get() != args[0].get() (line 347) misses aliased tile inputs when two different Var/IterArg expr nodes name the same underlying buffer; the same issue applies to DeduceTileOpBitwiseScalarType at line 383. Use an existing AsVarLike/resolve path from the IR utility layer and compare the resolved var/pointer identity instead before comparing memrefs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/ir/op/tile_ops/elementwise.cpp` around lines 342 - 349, Update the tmp
distinctness validation in the elementwise operator path and
DeduceTileOpBitwiseScalarType to resolve args[0], args[1], and args[2] through
the existing AsVarLike/IR resolution utilities before comparing identity. Reject
tmp when it aliases either source var or underlying buffer, even when the
ExprPtr nodes differ, while preserving the existing type and bitwise
compatibility checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/en/dev/ptoas-op-status.md`:
- Line 175: The pto.tnot status is incorrectly marked as ST-supported without
coverage or an explanatory note. Update the pto.tnot row in
docs/en/dev/ptoas-op-status.md at lines 175-175 and
docs/zh-cn/dev/ptoas-op-status.md at lines 161-161 identically: either change
the ST cell to ❌, or retain ✅ and add a Notes entry naming the pre-existing
same-name ST for tile.not.
- Around line 277-278: Recompute the Bitwise operation coverage statistics in
the affected documentation paragraph from the current tables: update the totals
to 123 covered and 49 uncovered, revise the remainder to be consistent with
those totals, and replace the incorrect “within these 204” wording so the stated
counts reconcile.

In `@src/ir/op/tile_ops/elementwise.cpp`:
- Around line 302-321: Extend CheckBitwiseTileMatches in
src/ir/op/tile_ops/elementwise.cpp to validate physical-shape rank and
per-dimension equality, matching the require_tdiv_contract checks while
preserving existing dtype and valid_shape validation. Update
tests/ut/ir/operators/test_tile_ops.py lines 3428-3437 so src0, src1, and tmp
share physical shape [8, 16] and retain the shared valid region.
- Around line 370-376: Update the scalar validation for tile bitwise operations
near DeduceTileOpIntScalarBinaryType so scalar operands are required to use
INT32, matching the ISA and codegen cast, instead of requiring scalar->dtype_ to
equal src->dtype_. Preserve validation that the tile/source and other operands
use the appropriate consistent dtype.

---

Nitpick comments:
In `@src/ir/op/tile_ops/elementwise.cpp`:
- Around line 342-349: Update the tmp distinctness validation in the elementwise
operator path and DeduceTileOpBitwiseScalarType to resolve args[0], args[1], and
args[2] through the existing AsVarLike/IR resolution utilities before comparing
identity. Reject tmp when it aliases either source var or underlying buffer,
even when the ExprPtr nodes differ, while preserving the existing type and
bitwise compatibility checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0c4b06ff-64f0-4dac-8d4a-c50aaa2cea15

📥 Commits

Reviewing files that changed from the base of the PR and between a55399d and 8ee9feb.

📒 Files selected for processing (9)
  • docs/en/dev/codegen/00-pto_codegen.md
  • docs/en/dev/ptoas-op-status.md
  • docs/zh-cn/dev/codegen/00-pto_codegen.md
  • docs/zh-cn/dev/ptoas-op-status.md
  • src/backend/common/pto_ops_elementwise.cpp
  • src/ir/op/tile_ops/elementwise.cpp
  • tests/st/runtime/ops/test_bitwise_binary.py
  • tests/ut/ir/operators/test_tile_ops.py
  • tests/ut/ir/transforms/test_memory_reuse.py

| pto.txor | TXOR | tile | ✅ | ✅ | ❌ | | — | verified on A2/A3 hardware for signed/unsigned 8/16-bit patterns, tmp, aliasing, and valid_shape; A5 hardware verification pending |
| pto.tshl | TSHL | tile | ✅ | ✅ | ❌ | ❌ | — | path exists; historical ISA/semantic issue requires revalidation against the current pin |
| pto.tshr | TSHR | tile | ✅ | ✅ | ❌ | ❌ | — | path exists; historical ISA/semantic issue requires revalidation against the current pin |
| pto.tnot | TNOT | tile | ✅ | ✅ | ❌ | ✅ | — | |

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

pto.tnot is marked ST ✅ in both status matrices without a supporting ST. This PR's new ST module covers and/or/xor/ands/ors/xors only; tile.not is not exercised, and both rows also leave the Notes cell empty while every other flipped row documents its verification scope.

  • docs/en/dev/ptoas-op-status.md#L175: revert the ST cell to ❌, or keep ✅ and add a Notes entry naming the pre-existing same-name ST for tile.not.
  • docs/zh-cn/dev/ptoas-op-status.md#L161: apply the identical correction so the ZH matrix stays in parity with the EN copy.
📍 Affects 2 files
  • docs/en/dev/ptoas-op-status.md#L175-L175 (this comment)
  • docs/zh-cn/dev/ptoas-op-status.md#L161-L161
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/en/dev/ptoas-op-status.md` at line 175, The pto.tnot status is
incorrectly marked as ST-supported without coverage or an explanatory note.
Update the pto.tnot row in docs/en/dev/ptoas-op-status.md at lines 175-175 and
docs/zh-cn/dev/ptoas-op-status.md at lines 161-161 identically: either change
the ST cell to ❌, or retain ✅ and add a Notes entry naming the pre-existing
same-name ST for tile.not.

Comment on lines +277 to +278
116 have same-name ST coverage (112 regular STs and 4 distributed STs); 56 lack same-name ST coverage
(46 regular and 10 distributed); within these 204, another 32 ops are not suitable for standalone STs.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Recount the ST column directly from the status tables (EN and ZH must agree).
for f in docs/en/dev/ptoas-op-status.md docs/zh-cn/dev/ptoas-op-status.md; do
  echo "== $f"
  awk -F'|' '/^\| *pto\./ {gsub(/ /,"",$0); print $0}' "$f" | awk -F'|' '{print $7}' | sort | uniq -c
done

Repository: hw-native-sys/pypto

Length of output: 288


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== stats paragraph =="
sed -n '240,290p' docs/en/dev/ptoas-op-status.md

echo
echo "== Bitwise table excerpt =="
sed -n '1,260p' docs/en/dev/ptoas-op-status.md | awk '
/^\|.*Bitwise|^\| \*+|^```/ {p=1}
p && /^\| (pto\.| \*\*\*)/ || (/^\`/ && prev_bitwise) {if (p && prev_bitwise) print; if (/^\| \*\*\*/ && !/Bitwise/) p=prev_bitwise=0; if (/^\|.*Bitwise/) prev_bitwise=1}'

Repository: hw-native-sys/pypto

Length of output: 4273


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
import re

p = Path('docs/en/dev/ptoas-op-status.md')
text = p.read_text()

# Header-style counts that were probably taken from the earlier Stats paragraph.
old_counts = {'covered': 112, 'distributed': 4, 'uncovered': 57, 'uncovered_distributed': 10}

# Extract only Bitwise rows: lines starting with `| pto.b*` and collect the 6th pipe-delimited token.
rows = []
in_bitwise = re.search(r'## Bitwise', text)
if not in_bitwise:
    raise SystemExit('Bitwise section not found')
after = text[in_bitwise.end():]
# Stop at the heading immediately following Bitwise or the table/footer.
for line in after.splitlines():
    if line.startswith('## ') or line.startswith('** ') or '/docs/en/dev/ptoas-op-status.md' in line:
        break
    m = re.match(r'\|\s*pto\.b[a-zA-Z_0-9]*(?:\s*\|)', line)
    if m:
        # Split on pipe and keep only columns until the first non-status column/notes column is ambiguous;
        # inspect the 7th field as the original script did.
        cells = [cell.strip() for cell in line.split('|')]
        rows.append((cells[0].strip(), cells[6].strip()))

print('Bitwise row count:', len(rows))
for op, st in rows:
    print(f'{op}: {st}')

covered = sum(1 for _, st in rows if st == '✅')
uncovered = sum(1 for _, st in rows if st in ('❌', '—') and False)  # placeholder
print('covered:', covered)

# Recompute the stats that should follow this table if Bitwise was previously all ❌ for ST.
def parse_stats(s):
    # simple parser for the exact sentence shape
    return eval(s)
PY

Repository: hw-native-sys/pypto

Length of output: 183


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== section/heading matches =="
rg -n '(Bitwise|ST coverage|same-name ST|within these 204|Stats' docs/en/dev/ptoas-op-status.md docs/zh-cn/dev/ptoas-op-status.md

echo
echo "== operation rows with pto.b / pto.t bitwise names =="
rg -n '^\| pto\.(bt|b[atsd]|tand|tor|txor|tnot|tands|tors|txors|\*.*B)/i' docs/en/dev/ptoas-op-status.md docs/zh-cn/dev/ptoas-op-status.md || true

echo
echo "== table rows with boolean/bitwise wording =="
rg -n '^\| pto\.|Bitwise|boolean|bitwise|BNAND|TAND|TOR|TXOR|TNAND' docs/en/dev/ptoas-op-status.md | head -n 120

Repository: hw-native-sys/pypto

Length of output: 303


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== section/heading matches =="
rg -n 'Bitwise|ST coverage|same-name ST|within these 204|Stats|Boolean|Bitwise' docs/en/dev/ptoas-op-status.md docs/zh-cn/dev/ptoas-op-status.md

echo
echo "== operation rows with pto.b / pto.t bitwise names =="
rg -n '^\| pto\.(bt|b[atsdnio]|tand|tor|txor|tnot|tands|tors|txors|t[nai])|BNAND|BAND|BOR|BXOR|NOTB|TAND|TOR|TXOR|TNAND|TNOR|TNAND' docs/en/dev/ptoas-op-status.md docs/zh-cn/dev/ptoas-op-status.md || true

echo
echo "== table rows with boolean/bitwise wording =="
rg -n '^\| pto\.|Boolean|bitwise|boolean|BNAND|TAND|TOR|TXOR|TNAND|TNOR|TNAND|Bitwise|Bit-wise|Bitwise ops' docs/en/dev/ptoas-op-status.md | head -n 140

Repository: hw-native-sys/pypto

Length of output: 22622


Recompute the stats from the current tables.

The Bitwise operations now add 7 and keep 4 , so the totals should be 123 covered / 49 uncovered, and the remainder below 32. Also 123 + 49 = 172, so the current “within these 204” wording still doesn’t add up.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/en/dev/ptoas-op-status.md` around lines 277 - 278, Recompute the Bitwise
operation coverage statistics in the affected documentation paragraph from the
current tables: update the totals to 123 covered and 49 uncovered, revise the
remainder to be consistent with those totals, and replace the incorrect “within
these 204” wording so the stated counts reconcile.

Comment on lines +302 to +321
static void CheckBitwiseTileMatches(const std::shared_ptr<const TileType>& reference,
const std::shared_ptr<const TileType>& candidate,
const std::string& candidate_name, const std::string& op_name) {
CHECK(candidate->dtype_ == reference->dtype_)
<< "The operator " << op_name << " requires all tile operands and dst to have the same dtype, but "
<< candidate_name << " has " << candidate->dtype_.ToString() << " and src has "
<< reference->dtype_.ToString();

const auto reference_valid_shape = GetValidShape(reference);
const auto candidate_valid_shape = GetValidShape(candidate);
CHECK(candidate_valid_shape.size() == reference_valid_shape.size())
<< "The operator " << op_name
<< " requires all tile operands and dst to have the same valid_shape rank";
for (size_t i = 0; i < reference_valid_shape.size(); ++i) {
CHECK(ProveValidExtentEqual(reference_valid_shape[i], candidate_valid_shape[i]) == ProofResult::kTrue)
<< "The operator " << op_name
<< " requires all tile operands and dst to have the same valid_shape, but " << candidate_name
<< " differs at dimension " << i;
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Locate files"
git ls-files | rg '(^|/)elementwise\.cpp$|(^|/)test_tile_ops\.py$|ptoas-op-status\.md$' || true

echo
echo "Inspect elementwise.cpp outline around tile helpers"
ast-grep outline src/ir/op/tile_ops/elementwise.cpp --view compact || true

echo
echo "Relevant elementwise.cpp sections"
sed -n '1,240p' src/ir/op/tile_ops/elementwise.cpp || true
echo "----"
sed -n '240,380p' src/ir/op/tile_ops/elementwise.cpp || true

echo
echo "Relevant test_tile_ops.py section"
sed -n '3380,3485p' tests/ut/ir/operators/test_tile_ops.py || true

Repository: hw-native-sys/pypto

Length of output: 25213


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Find Bitwise op registrations/usages"
rg -n "DeduceTileOpBitwise|CheckBitwiseTileMatches|RequireTile|Require.*Bitwise|tile\.and_|tile\.or_|tile\.xor|tile\.ands|tile\.ors|tile\.xors" src tests -S || true

echo
echo "Relevant type_inference files/functions"
fd -a 'type_inference\.(cpp|h|cc)$' . | sed 's#^\./##' | while read -r f; do
  echo "--- $f"
  rg -n "TileType|required|BroadcastShapes|GetValidShape|CheckBitwise|tile\.|Require" "$f" -A 3 -B 3 || true
done

Repository: hw-native-sys/pypto

Length of output: 18826


🌐 Web query:

PTO ISA op specifications bitwise tile operations tands tors txor requirements operands physical shape same tile

💡 Result:

In the PTO (Parallel Tile Operations) Virtual ISA, TANDS, TORS, and TXORS are tile-scalar instructions that perform bitwise AND, OR, and XOR operations between a tile and a scalar value, respectively [1][2]. Key specifications and requirements for these operations include: 1. Operation Requirements: - Elements: These instructions are intended for integral element types [3][4]. - Tiles: The source (src) and destination (dst) must be vector tiles and must use the same element type [3][4]. - Shape Matching: The runtime requires that the source tile and destination tile have matching valid regions, specifically src.GetValidRow == dst.GetValidRow and src.GetValidCol == dst.GetValidCol [3][4]. - Memory Constraints: In manual mode, it is unsupported to set the source tile and destination tile to the same memory [3][4]. 2. Related Tile-Tile Operations: - The ISA also includes corresponding tile-tile instructions: TAND, TOR, and TXOR [1][2]. - Unlike the scalar versions, these require two source tiles (src0, src1) [5][6]. - The TXOR operation specifically may require an additional temporary tile (tmp) for intermediate storage, which must meet specific memory and shape requirements (e.g., must not overlap with dst, src0, or src1) [6]. All tile operations in the PTO ISA generally utilize the dst tile's valid region as the iteration domain [3][4].

Citations:


🌐 Web query:

PTO ISA assembly elementwise ops tile tile bitwise XOR AND OR operands physical shape same tile

💡 Result:

In the Parallel Tile Operation (PTO) ISA, elementwise operations—such as bitwise XOR (TXOR) and bitwise AND (TAND)—are defined as tile-to-tile operations where the source and destination operands generally share the same physical shape [1][2][3]. For elementwise tile-to-tile instructions like TXOR and TAND: - The destination (dst), source 0 (src0), and source 1 (src1) tiles must match in their valid region dimensions (GetValidRow and GetValidCol) [2][3]. - All tile operands must typically use the same element type [2][3]. - These tiles must be row-major, and their physical shape (capacity) is enforced to be static for compile-time optimization [2][3][4]. Specific constraints for XOR/AND operations include: - The operation consumes and produces whole tiles, not sub-tiles [4]. - For instructions like TXOR, which may be implemented via decomposition (e.g., XOR(a, b) = AND(NOT(AND(a, b)), OR(a, b))), a temporary scratch tile (tmp) may be required [2]. This temporary tile must also have the same element type as the primary operands and satisfy shape constraints (its valid row/column dimensions must be at least as large as those of the destination) [2]. - In manual mode, resources must be bound explicitly using TASSIGN before these instructions are issued, and temporary buffers must not overlap in memory with the destination or source tiles [2][3][5].

Citations:


Keep bitwise tile operands constrained by physical shape. CheckBitwiseTileMatches only checks dtype and valid_shape, so currently accepts mismatched physical capacities like [8, 16] / [10, 20] / [12, 24]; add the same physical-shape rank and per-dimension equality checks as the require_tdiv_contract path. Update the contract test at tests/ut/ir/operators/test_tile_ops.py#L3428-3437 to use one shared physical shape (e.g. [8, 16] for src0, src1, and tmp) and keep the same shared valid region.

📍 Affects 2 files
  • src/ir/op/tile_ops/elementwise.cpp#L302-L321 (this comment)
  • tests/ut/ir/operators/test_tile_ops.py#L3428-L3437
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/ir/op/tile_ops/elementwise.cpp` around lines 302 - 321, Extend
CheckBitwiseTileMatches in src/ir/op/tile_ops/elementwise.cpp to validate
physical-shape rank and per-dimension equality, matching the
require_tdiv_contract checks while preserving existing dtype and valid_shape
validation. Update tests/ut/ir/operators/test_tile_ops.py lines 3428-3437 so
src0, src1, and tmp share physical shape [8, 16] and retain the shared valid
region.

Comment on lines +370 to +376
CHECK(IsBitwiseScalarDataType(src->dtype_))
<< "The operator " << op_name << " requires tile/scalar dtype in {INT8, INT16, INT32}, but got "
<< src->dtype_.ToString();
CHECK(scalar->dtype_ == src->dtype_)
<< "The operator " << op_name << " requires src, scalar, tmp (when present), and dst to have the "
<< "same dtype, but scalar has " << scalar->dtype_.ToString() << " and src has "
<< src->dtype_.ToString();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find ands/ors/xors call sites where the scalar operand may not match the tile dtype.
rg -nP --type=py -C4 '\b(pl\.|tile\.)(ands|ors|xors)\s*\(' --glob '!**/test_bitwise_binary.py'

Repository: hw-native-sys/pypto

Length of output: 157


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== candidate files =="
fd -a 'elementwise\.cpp|test_bitwise|.*tile.*' . | sed 's#^\./##' | head -200

echo "== relevant symbol definitions =="
rg -n "IsBitwiseScalarDataType|DeduceTileOpBitwiseScalarType|DeduceTileOpIntScalarBinaryType|shls|shrs|tile\.(ands|ors|xors)" -S .

echo "== elementwise.cpp around line 250-390 =="
sed -n '250,400p' src/ir/op/tile_ops/elementwise.cpp

echo "== py usage of tile and/or/x or scalar bitwise functions =="
rg -n --type=py 'tile\.(ands|ors|xors|and|or|xor|ands|ors|xors|shls|shrs)|\.ands\(|\.ors\(|\.xors\(' -S . | head -500

Repository: hw-native-sys/pypto

Length of output: 32149


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
import ast, pathlib

tiles = [p for p in pathlib.Path("tests").rglob("*.py")]
patterns = ("tile.ands", "tile.ors", "tile.xors", "ands", "ors", "xors")
hits = []
for p in tiles:
    try:
        tree = ast.parse(p.read_text(encoding="utf-8", errors="replace"))
    except SyntaxError:
        continue
    for node in ast.walk(tree):
        if isinstance(node, ast.Call):
            name = None
            if isinstance(node.func, ast.Attribute):
                inner = node.func.value
                target = (inner.id if isinstance(inner, ast.Name) else getattr(inner, "value", None))
                if target == "pl" and node.func.attr in patterns:
                    name = f"{target}.{node.func.attr}"
                elif target and inner.value == "pl" and node.func.attr in patterns:
                    name = f"{target}.{node.func.attr}"
                else:
                    name = None
            elif isinstance(node.func, ast.Name) and node.func.id in patterns:
                name = node.func.id
            if not name:
                continue
            scalar_arg = node.args[1] if len(node.args) >= 2 else None
            src_arg = node.args[0] if len(node.args) >= 1 else None
            hits.append((p, node.lineno, node.col_offset, name, (ast.dump(src_arg, include_attributes=False)[:120] if src_arg else None, ast.dump(scalar_arg, include_attributes=False)[:120] if scalar_arg else None)))

for p, line, col, name, (src, scalar) in hits:
    print(f"{p}:{line}:{col}:{name}:src={src}:scalar={scalar}")
PY

echo "== relevant tests around scalar bitwise width tests =="
sed -n '3900,4055p' tests/ut/ir/operators/test_tile_ops.py

echo "== read-only parser probe for hardcoded mixed dtype scalar bitwise kernels =="
rg -n '\b(pl\.(INT8|INT16|UINT8|UINT16)|pl\.Scalar\[.*(INT32|INT16|INT8)\]|pl\.Scalar\([^(]*(INT32|INT16|INT8)' -S . \
  --glob 'tests/**/*.py' --glob 'python/**/*.py' | rg 'tile.*(ands|ors|xors)|ands|ors|xors' || true

echo "== exact dtype equality helper behavior =="
rg -n "def IsBitwise|CHECK\\(|DeduceTileOpIntScalarBinaryType|DeduceTileOpBitwiseScalarType" src/ir/op/tile_ops/elementwise.cpp -C3

Repository: hw-native-sys/pypto

Length of output: 286


🌐 Web query:

hw-native-sys pypto tile.ands INT16 Scalar INT32 pypto tile bitwise scalar dtype

💡 Result:

PyPTO is a high-performance programming framework for AI accelerators, based on a tile-based programming model and the Parallel Tensor/Tile Operation (PTO) paradigm [1][2]. The framework uses intermediate representations to compile tensor-level operations into hardware instructions [2]. Regarding the specific terms and usage contexts: Data Types (Dtype) PyPTO supports a wide range of data types through its DataType class, including INT16 (DT_INT16), INT32 (DT_INT32), and various floating-point and unsigned integer formats (e.g., DT_FP32, DT_FP16, DT_BF16, DT_UINT8) [3]. Operations such as addition, multiplication, and subtraction generally require tensors to share the same data type [4][5][6]. Scalar Usage In operations like pypto.add, pypto.mul, and pypto.sub, the framework supports scalar inputs (int or float) alongside tensors [4][5][6]. When a scalar is provided: - If the tensor contains floating-point values, integer scalars are automatically converted to float [4][5][6]. - If the tensor contains integer values, providing a floating-point scalar will result in an error [4][5][6]. Tile and Bitwise Operations - Tile Programming: Computation is organized around Tiles—hardware-aware data blocks [2]. Users can configure tile shapes using set_vec_tile_shapes(m, n) before executing operations [4][5][6]. - Bitwise Operations: Bitwise operations such as pypto.bitwise_left_shift and pypto.bitwise_right_shift are supported for integer types (e.g., DT_INT16, DT_INT32) [7][8]. These operations accept both tensors and integers, requiring input types to match [7][8]. For further technical implementation, the hw-native-sys GitHub organization maintains the framework, including the core pypto repository, pto-isa (Tile Library), and ptoas (assembler/optimizer) [9][10][11].

Citations:


Align tile.ands/ors/xors scalar typing with the ISA spec.

DeduceTileOpIntScalarBinaryType already requires any integer scalar but codegen casts to i32; tighten these scalar bitwise ops either to require i32 scalars like the ISA, or keep the current permissive integer-wide form. Requiring exact tile/scalar dtype equality will reject narrow-tile kernels using pl.Scalar[pl.INT32], which is not represented here in an in-tree failure.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/ir/op/tile_ops/elementwise.cpp` around lines 370 - 376, Update the scalar
validation for tile bitwise operations near DeduceTileOpIntScalarBinaryType so
scalar operands are required to use INT32, matching the ISA and codegen cast,
instead of requiring scalar->dtype_ to equal src->dtype_. Preserve validation
that the tile/source and other operands use the appropriate consistent dtype.

@Little-oil

Copy link
Copy Markdown
Contributor

#2166 复核当前 head 8ee9febc 后,仍有一个公开 API 契约错误:

  1. 当前 IsBitwiseScalarDataType 只允许 signed INT8/16/32,并据此整体拒绝 unsigned tile-scalar bitwise。PTOAS 要求的是 scalar 使用 signless integer,并不代表 unsigned tile 不支持:UINT8/16/32 tile 应配同位宽的 signless i8/i16/i32 scalar encoding;固定 PTO-ISA 也有对应 unsigned 实现。

请不要把编码差异固化成 API 缺失:需要补 UINT tile 的 immediate、SSA scalar、负向和真机 ST。另请注意,PR body 的 18/18 真机记录早于当前扩展后的 full/row/column/combined-tail 矩阵;上述修复和当前矩阵完整通过后,再将相关行标为

@hashiqiqixian
hashiqiqixian force-pushed the feat/ptoas-b07-bitwise branch from 8ee9feb to 3d5b48a Compare July 30, 2026 01:59
@hashiqiqixian

Copy link
Copy Markdown
Contributor Author

Closing this later PTOAS batch for now so work can proceed serially from B02 and B03. The branch is preserved for reopening when its turn arrives.

@hashiqiqixian hashiqiqixian reopened this Jul 30, 2026
@hashiqiqixian
hashiqiqixian marked this pull request as draft July 30, 2026 02:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants