Skip to content

Clarify @dialect.canonicalize vs @dialect.post_inference semantics — rules applied un-walked; add a walk option #676

Description

@weinbe58

Summary

Rules registered via @dialect.canonicalize and @dialect.post_inference are applied to the method's top node only (un-walked), so an ordinary single-node RewriteRule registered this way is silently a no-op. Requesting clarification on the intended contract — and on whether the two are meant to differ — plus a small ergonomic fix.

What the code does today

  • Registration appends rule() to rules.canonicalize / rules.inference (kirin/ir/dialect.py).
  • Application is identical in both passes:
    • Canonicalize.unsafe_run: for rule in dialect.rules.canonicalize: rule.rewrite(mt.code) (kirin/passes/canonicalize.py)
    • PostInference.unsafe_run: for rule in dialect.rules.inference: rule.rewrite(mt.code) (kirin/passes/post_inference.py)
  • Both are run to fixpoint by their callers: Default.canonicalize.fixpoint(mt) (default.py:41) and TypeInfer.post_inference.fixpoint(mt) (typeinfer.py:44).
  • RewriteRule.rewrite(node) (kirin/rewrite/abc.py) dispatches only on the node passed — no recursion — and mt.code is the top-level func.Function.

Net: both mechanisms are un-walked and fixpointed, so a rule whose rewrite_Statement targets statements nested in the body never sees them. The in-tree ilist post-inference rules (HintLen, Unroll, …) appear to work only because they're also run inside a Walk(...) in the fold chains.

Questions

  1. Is there an intended behavioral difference between canonicalize and post_inference rules? They're currently applied identically (un-walked, fixpointed by caller).
  2. Is the intended contract that registered rules self-walk, or that the framework should Walk them? Today registering an ordinary RewriteRule is silently a no-op.

Proposed enhancement

Add a walk: bool option to both decorators that wraps the rule in Walk at registration:

@dialect.post_inference(walk=True)
class MyRule(RewriteRule): ...

(Requires making the decorators parameterizable; they currently take the rule type directly.)

kirin-toolchain 0.22.11

cc: @kaihsin @neelay893

Metadata

Metadata

Assignees

No one assigned

    Labels

    category: enhancementCategory: this is an enhancement of an existing feature.experience: needs designExperience: this issue needs exploration and design before implementation.priority: mediumPriority: medium priority issues and tasks, default priority.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions