Commit 9a771e4
[Frontend] Fix #238: replace var_info dict with typed CSEVariable attributes
Issue #238 was the visible symptom (silent bool/uint8 -> int8 downcast
via the lossy `MLIR_TO_DTYPE[var_info[1]]` round-trip at
mlir_codegen_backend.py:1535) of a deeper architectural smell: PyTorchSim
maintained a parallel `self.var_info` dict tracking `[vec_size,
mlir_dtype_string]` per csevar, duplicating type info that already lives
on Inductor's `CSEVariable.dtype`. The lossy MLIR->torch round-trip was
the only place this duplication actively caused corruption, but
collapsing the two systems is the structural fix.
Core changes:
- New type:
- `MLIRCSEVariable(common.CSEVariable)` carries `vec_size: int` and
inherits `dtype: Optional[torch.dtype]`. `mlir_dtype` is a derived
@Property from `dtype` via `DTYPE_TO_MLIR`. There is no separate
predicate/mask subclass: `torch.bool` maps to MLIR `"i1"` directly
(DTYPE_TO_MLIR[torch.bool] = "i1"). MLIR-to-LLVM lowering pads i1
storage to bytes, matching the wrapper C ABI (`uint8_t*`, one byte
per element). The wrapper architecturally cannot accept bit-packed
i1 storage (mlir_caller_codegen.py uses sizeof(ctype) loads), so the
`memref<...xi1>` -> `i8`-backed pipeline is the natural fit.
- `OpResult(vec_size, dtype)` frozen dataclass replaces the legacy
`[vec_size, mlir_dtype_string]` ret_info list. `OpResult.from_var`
and `OpResult.from_mlir` are classmethod constructors.
- `INDEX_DTYPE` singleton sentinel for MLIR `index` type (no torch
equivalent). `MLIR_TO_DTYPE["index"] = INDEX_DTYPE` and
`DTYPE_TO_MLIR[INDEX_DTYPE] = "index"` so the dicts are
bijective for all known types — clearer than overloading `None`.
- `MLIRCSE(common.CSE)` extends Inductor's CSE with a `vec_size` axis:
- `newvar` / `namedvar` construct `MLIRCSEVariable` directly,
bypassing the kernel-side `V.kernel.create_cse_var` hook (which is
no longer needed).
- `generate(buffer, code, *, vec_size=N, dtype=X, ...)` plumbs
`vec_size` to `newvar` via a transient instance attribute, calling
`super().generate(...)` for the rest. No need to reimplement the
upstream generate body.
- Handler proxy (mlir_common.py CSEProxy) rewritten to expect
`(code, OpResult|None)` from ops. Single uniform path:
`target_cse.generate(buf, code, dtype=ret.dtype, vec_size=ret.vec_size)`
— no post-hoc attribute assignment.
- All ops in mlir_ops.py, mlir_template.py, mlir_sort_template.py
return `(code, OpResult)` (or `OpResult.from_var` / `OpResult.from_mlir`
helpers). Legacy `[size, mlir_str]` shape gone.
- `register_var_info` / `register_var_cse` deleted. Six previously-named
csevars (`compute_idx`, `itervar_cses`, `init_iter`, `reduce_loop_idx`,
`idx_step_index`, `idx_base`) now use `cse.namedvar(..., dtype=...,
vec_size=...)` directly. `make_named_csevar` wrapper removed.
- ~108 read sites of `var_info[v][...]` migrated to attribute access
(`v.vec_size`, `v.mlir_dtype`). `var_info[v][1] == "i1"` patterns
collapse to `v.dtype == torch.bool` since the mask subclass is gone.
- `self.var_info` dict removed entirely.
- Issue #238 fix at mlir_codegen_backend.py:1535:
csevar = self.cse.varname_map[target_dim]
dtype = csevar.dtype
No more round-trip; the torch dtype set at csevar construction is
preserved end-to-end.
Files touched: mlir_common.py (foundation), mlir_codegen_backend.py
(#238 site + read migration + memory-entry call sites),
mlir_ops.py (ops layer ret_info migration), mlir_template.py +
mlir_sort_template.py (template ops + named csevar sites).
Sample-verified: test_add, test_softmax, test_sort (i1 mask path via cmp),
test_matmul, test_layernorm, test_indirect_access (#238 critical path),
test_expert_mask, test_transcendental, test_reduce.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 8306055 commit 9a771e4
5 files changed
Lines changed: 384 additions & 320 deletions
File tree
- PyTorchSimFrontend/mlir
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
295 | 295 | | |
296 | 296 | | |
297 | 297 | | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
| |||
549 | 549 | | |
550 | 550 | | |
551 | 551 | | |
552 | | - | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
553 | 558 | | |
554 | 559 | | |
555 | 560 | | |
| |||
593 | 598 | | |
594 | 599 | | |
595 | 600 | | |
596 | | - | |
| 601 | + | |
597 | 602 | | |
598 | 603 | | |
599 | 604 | | |
600 | | - | |
| 605 | + | |
601 | 606 | | |
602 | 607 | | |
603 | 608 | | |
| |||
644 | 649 | | |
645 | 650 | | |
646 | 651 | | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
647 | 655 | | |
648 | 656 | | |
649 | 657 | | |
650 | 658 | | |
651 | 659 | | |
652 | 660 | | |
653 | 661 | | |
654 | | - | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
655 | 666 | | |
656 | 667 | | |
657 | 668 | | |
| |||
664 | 675 | | |
665 | 676 | | |
666 | 677 | | |
667 | | - | |
668 | | - | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
669 | 682 | | |
670 | 683 | | |
671 | 684 | | |
| |||
683 | 696 | | |
684 | 697 | | |
685 | 698 | | |
686 | | - | |
687 | | - | |
688 | | - | |
| 699 | + | |
| 700 | + | |
689 | 701 | | |
690 | 702 | | |
691 | 703 | | |
| |||
794 | 806 | | |
795 | 807 | | |
796 | 808 | | |
797 | | - | |
| 809 | + | |
798 | 810 | | |
799 | 811 | | |
800 | 812 | | |
| |||
874 | 886 | | |
875 | 887 | | |
876 | 888 | | |
877 | | - | |
| 889 | + | |
878 | 890 | | |
879 | 891 | | |
880 | 892 | | |
| |||
1479 | 1491 | | |
1480 | 1492 | | |
1481 | 1493 | | |
1482 | | - | |
1483 | | - | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
1484 | 1500 | | |
1485 | 1501 | | |
1486 | 1502 | | |
| |||
1531 | 1547 | | |
1532 | 1548 | | |
1533 | 1549 | | |
1534 | | - | |
1535 | | - | |
1536 | | - | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
1537 | 1555 | | |
1538 | 1556 | | |
1539 | 1557 | | |
1540 | | - | |
| 1558 | + | |
1541 | 1559 | | |
1542 | | - | |
| 1560 | + | |
1543 | 1561 | | |
1544 | 1562 | | |
1545 | 1563 | | |
| |||
1559 | 1577 | | |
1560 | 1578 | | |
1561 | 1579 | | |
1562 | | - | |
| 1580 | + | |
1563 | 1581 | | |
1564 | 1582 | | |
1565 | 1583 | | |
| |||
1577 | 1595 | | |
1578 | 1596 | | |
1579 | 1597 | | |
1580 | | - | |
| 1598 | + | |
1581 | 1599 | | |
1582 | 1600 | | |
1583 | 1601 | | |
| |||
0 commit comments