tqchen opened a new pull request, #18857:
URL: https://github.com/apache/tvm/pull/18857
## Summary
This PR enhances TVM's runtime error reporting for compiled functions by:
1. **Refactoring `AssertStmt`** to carry explicit `kind` (e.g. "TypeError",
"ValueError") and `message_parts` (array of string fragments) instead of a
single message string. This enables:
- Proper Python exception types at runtime (TypeError, ValueError vs
generic RuntimeError)
- String fragment reuse across assertions via
`TVMFFIErrorSetRaisedFromCStrParts`
2. **Consolidating all binding + error logic in `ArgBinder`** (renamed from
`arg_binder` to `tvm_ffi_binder`):
- Single public API: `BindPackedArg(i)` + `BindAllParams()`
- All type checking, value loading, buffer binding, and error messages
owned by ArgBinder
- Rich error messages with `AccessPath` tracking for cross-reference
(e.g. "expected to match a.shape[0]")
- Cached `StringImm` for signature string reuse across assertions
3. **Simplifying `make_packed_api.cc`** from ~300 to ~100 lines — just calls
`binder.BindPackedArg(i)` per param
### Example error messages (before → after)
```
# Before:
RuntimeError: Assert fail: ... Argument a.shape[0] has an unsatisfied
constraint
# After:
TypeError: Mismatched type on argument #1 when calling:
`add_one(a: Tensor([n0], float32), b: Tensor([n0], float32))`,
expected Tensor
ValueError: Mismatched b.shape[0] on argument #1 when calling:
`add_one(a: Tensor([n0], float32), b: Tensor([n0], float32))`,
expected to match a.shape[0]
```
## Changes
- **`include/tvm/tir/stmt.h`**: `AssertStmtNode` fields → `kind: StringImm`,
`condition: PrimExpr`, `message_parts: Array<StringImm>`
- **`src/tir/transform/tvm_ffi_binder.{h,cc}`**: New consolidated ArgBinder
with `BindPackedArg`, `BindAllParams`, private submethods with full docs
- **`src/tir/transform/make_packed_api.cc`**: Simplified to delegate to
ArgBinder
- **`src/target/llvm/codegen_cpu.{cc,h}`**: LLVM codegen using
`TVMFFIErrorSetRaisedFromCStrParts` via noinline helper
- **`src/target/source/codegen_c_host.cc`**: C host codegen with
message_parts array
- **All visitors/mutators**: Updated for new AssertStmt fields
- **TVMScript printer/parser**: Updated for new format
- **Removed**: `arg_binder.{h,cc}` (replaced by `tvm_ffi_binder`)
## Test plan
- [x] `tests/python/codegen/test_codegen_assert.py` — Phase 0 basic
AssertStmt codegen (7 tests, LLVM)
- [x] `tests/python/codegen/test_codegen_error_handling.py` — Phase 1 rich
error messages (9 tests × 2 backends = 18 tests, LLVM+C)
- [x] `tests/python/tir-transform/test_tir_transform_make_packed_api.py` —
existing transform tests (12 tests)
- [x] `tests/python/tir-base/test_tir_constructor.py` — updated constructor
test
- [x] cpplint + ruff clean
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]