tqchen opened a new pull request, #20423:
URL: https://github.com/apache/tvm/pull/20423
This PR introduces a modular, customizable parser infrastructure for
TVMScript through transpilation. The key idea is to deterministically transpile
language syntax into a corresponding sequence of builder calls while preserving
source locations and providing span context.
Key design elements include:
- Acquire source and its definition scope, collect syntax facts once,
rewrite the owned AST, and execute the resulting builder program with the
original lexical bindings.
- Each language variant customizes behavior through a `parser_protocol.py`
file, which defines special handling for syntax such as `for` and `if`.
- Create reusable span entries during rewriting. Attach locations to
meaningful expressions and pass spans directly to builder operations that own
them.
- Reuse existing builder and IR APIs, so new types and functions can be
added without changing the parser, provided they have well-defined builder
behavior.
- Generate specialization handling only when specialization is requested.
For example, this kernel:
```python
@T.prim_func
def add_one(A: T.Buffer((128,), "float32"), B: T.Buffer((16, 8), "float32")):
for i in range(128):
B[i // 8, i % 8] = A[i] + 1
```
is lowered to the following builder calls. This presentation wraps the
emitted program in `build_add_one` and gives its local scaffolding readable
aliases. `I` supplies shared builders, `T` is the dialect namespace, `_S` holds
source-span entries, and `_host0` and `_host1` are captured annotation helpers.
```python
def build_add_one():
with I.IRBuilder() as builder:
with T.function_(span=_S[6]) as fn:
T.func_name('add_one')
_parameter0 = T.arg('A', _host0(_S[1](T.Buffer(_S[0]((128,)),
'float32'))), span=_S[2])
_parameter1 = T.arg('B', _host1(_S[4](T.Buffer(_S[3]((16, 8)),
'float32'))), span=_S[5])
def build_body():
global T
A, B = fn.params
with T.for_(T.range_(128), span=_S[7], names='i') as i:
_value0 = _S[9](_S[8](A[i]) + 1)
T.setitem_(B, (_S[10](i // 8), _S[11](i % 8)), _value0,
span=_S[12])
build_body()
result = fn.function
result.__name__ = 'add_one'
T.check_well_formed_(result)
return fn.function
```
--
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]