A dialect-agnostic IRBuilder that constructs any IR of TVM. Examples -------- An idiomatic use of this class is to put this inside the with-scope, call dialect-specific methods accordingly. Upon exiting the scope. .. code-block:: python from tvm.script.ir_builder impor
| 89 | |
| 90 | @_register_object("script.ir_builder.IRBuilder") |
| 91 | class IRBuilder(_Object): |
| 92 | """A dialect-agnostic IRBuilder that constructs any IR of TVM. |
| 93 | |
| 94 | Examples |
| 95 | -------- |
| 96 | An idiomatic use of this class is to put this inside the with-scope, |
| 97 | call dialect-specific methods accordingly. Upon exiting the scope. |
| 98 | |
| 99 | .. code-block:: python |
| 100 | |
| 101 | from tvm.script.ir_builder import tirx as T |
| 102 | from tvm.script.ir_builder import IRBuilder |
| 103 | |
| 104 | with IRBuilder() as builder: |
| 105 | with T.prim_func(...): # pushes a PrimFuncFrame (subclass of IRBuilderFrame) |
| 106 | # to `builder`'s stack of frames |
| 107 | buffer = T.match_buffer(...) |
| 108 | |
| 109 | return builder.get() # returns the constructed IR, i.e. tirx.PrimFunc |
| 110 | """ |
| 111 | |
| 112 | def __init__(self) -> None: |
| 113 | """Construct an IRBuilder.""" |
| 114 | self.__init_handle_by_constructor__( |
| 115 | _ffi_api.IRBuilder # type: ignore[attr-defined] # pylint: disable=no-member |
| 116 | ) |
| 117 | |
| 118 | def __enter__(self) -> "IRBuilder": |
| 119 | """Enter the with-scope for IRBuilder, which allows the IRBuilder to be discoverable |
| 120 | using `IRBuilder.current()`. |
| 121 | |
| 122 | Examples |
| 123 | -------- |
| 124 | .. code-block:: python |
| 125 | |
| 126 | from tvm.script.ir_builder import IRBuilder |
| 127 | |
| 128 | with IRBuilder() as builder: |
| 129 | assert IRBuilder.current() == builder |
| 130 | |
| 131 | """ |
| 132 | _ffi_api.IRBuilderEnter(self) # type: ignore[attr-defined] # pylint: disable=no-member |
| 133 | return self |
| 134 | |
| 135 | def __exit__(self, ptype, value, trace) -> None: # pylint: disable=unused-argument |
| 136 | _ffi_api.IRBuilderExit(self) # type: ignore[attr-defined] # pylint: disable=no-member |
| 137 | |
| 138 | @staticmethod |
| 139 | def current() -> "IRBuilder": |
| 140 | """Get the current IRBuilder put in the with-scope. |
| 141 | |
| 142 | Returns |
| 143 | ------- |
| 144 | builder : IRBuilder |
| 145 | The current IRBuilder. |
| 146 | """ |
| 147 | return _ffi_api.IRBuilderCurrent() # type: ignore[attr-defined] # pylint: disable=no-member |
| 148 |
no outgoing calls
searching dependent graphs…