A binding/statement-level dataflow block rewriter. Notes ----- Due to the immutable and copy-on-write nature of TVM AST nodes, the rewriting is not done in place. Instead, a new DataflowBlock is created and returned with mutated_dfb. Similarly, its new root Function is crea
| 27 | |
| 28 | @tvm_ffi.register_object("relax.DataflowBlockRewrite") |
| 29 | class DataflowBlockRewrite(Object): |
| 30 | """ |
| 31 | A binding/statement-level dataflow block rewriter. |
| 32 | |
| 33 | Notes |
| 34 | ----- |
| 35 | Due to the immutable and copy-on-write nature of TVM AST nodes, the rewriting is not done in |
| 36 | place. Instead, a new DataflowBlock is created and returned with mutated_dfb. Similarly, its new |
| 37 | root Function is created and returned by mutated_root_fn. To apply this change for an IRModule, |
| 38 | use mutate_irmodule which rewrites the old function that registered in the constructor. |
| 39 | """ |
| 40 | |
| 41 | __slots__ = ("__dict__",) |
| 42 | |
| 43 | def __init__(self, dfb: DataflowBlock, root_fn: Function): |
| 44 | """ |
| 45 | Construct a rewriter with the DataflowBlock to rewrite and its root function. |
| 46 | |
| 47 | Parameters |
| 48 | ---------- |
| 49 | dfb : DataflowBlock |
| 50 | The DataflowBlock to rewrite. |
| 51 | root_fn : Function |
| 52 | The root function of the DataflowBlock. |
| 53 | """ |
| 54 | self.func_name = root_fn.__name__ if hasattr(root_fn, "__name__") else None |
| 55 | self.__init_handle_by_constructor__( |
| 56 | _ffi_api.DataflowBlockRewrite, |
| 57 | dfb, |
| 58 | root_fn, # type: ignore |
| 59 | ) |
| 60 | |
| 61 | def replace_all_uses(self, old_var: Var, new_var: Var) -> None: |
| 62 | """ |
| 63 | Replace all uses of old_var with new_var. |
| 64 | |
| 65 | Parameters |
| 66 | ---------- |
| 67 | old_var : Var |
| 68 | The old variable to replace. |
| 69 | new_var : Var |
| 70 | The new variable to replace with. |
| 71 | """ |
| 72 | _ffi_api.dfb_rewrite_replace_all_uses(self, old_var, new_var) # type: ignore |
| 73 | |
| 74 | def add_binding(self, binding: Binding) -> None: |
| 75 | return _ffi_api.dfb_rewrite_add_binding(self, binding) # type: ignore |
| 76 | |
| 77 | def add(self, expr: Expr, name: str | None = None, is_dfvar: bool = False) -> None: |
| 78 | """ |
| 79 | Add a new statement to the DataflowBlock with an automatically generated variable name. |
| 80 | |
| 81 | Parameters |
| 82 | ---------- |
| 83 | expr : Expr |
| 84 | The expression to add. |
| 85 | name : Optional[str], optional |
| 86 | Variable name, by default None |
no outgoing calls
searching dependent graphs…