You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[mlir][docs] Migrate code examples to nanobind, make Python spelling … (#163933)
…consistent
Since the bindings now use nanobind, I changed the code examples and
mentions in the documentation prose to mention nanobind concepts and
symbols wherever applicable.
I also made the spelling of "Python" consistent by choosing the
uppercase name everywhere that's not an executable name, part of a URL,
or directory name.
----------------
Note that I left mentions of `PybindAdaptors.h` in because of
llvm/llvm-project#162309.
Are there any thoughts about adding a virtual environment setup guide
using [uv](https://docs.astral.sh/uv/)? It has gotten pretty popular,
and is much faster than a "vanilla" Python pip install. It can also
bootstrap an interpreter not present on the user's machine, for example
a free-threaded Python build, with the `-p` flag to the `uv venv`
virtual environment creation command.
Note that if you have installed (i.e. via `ninja install`, et al), then python
85
+
Note that if you have installed (i.e. via `ninja install`, et al), then Python
69
86
packages for all enabled projects will be in your install tree under
70
87
`python_packages/` (i.e. `python_packages/mlir_core`). Official distributions
71
88
are built with a more specialized setup.
@@ -74,23 +91,23 @@ are built with a more specialized setup.
74
91
75
92
### Use cases
76
93
77
-
There are likely two primary use cases for the MLIR python bindings:
94
+
There are likely two primary use cases for the MLIR Python bindings:
78
95
79
96
1. Support users who expect that an installed version of LLVM/MLIR will yield
80
97
the ability to `import mlir` and use the API in a pure way out of the box.
81
98
82
99
1. Downstream integrations will likely want to include parts of the API in
83
100
their private namespace or specially built libraries, probably mixing it
84
-
with other python native bits.
101
+
with other Python native bits.
85
102
86
103
### Composable modules
87
104
88
105
In order to support use case \#2, the Python bindings are organized into
89
106
composable modules that downstream integrators can include and re-export into
90
107
their own namespace if desired. This forces several design points:
91
108
92
-
* Separate the construction/populating of a `py::module` from
93
-
`PYBIND11_MODULE` global constructor.
109
+
* Separate the construction/populating of a `nb::module` from
110
+
`NB_MODULE` global constructor.
94
111
95
112
* Introduce headers for C++-only wrapper classes as other related C++ modules
96
113
will need to interop with it.
@@ -130,7 +147,7 @@ registration, etc.
130
147
131
148
### Loader
132
149
133
-
LLVM/MLIR is a non-trivial python-native project that is likely to co-exist with
150
+
LLVM/MLIR is a non-trivial Python-native project that is likely to co-exist with
134
151
other non-trivial native extensions. As such, the native extension (i.e. the
135
152
`.so`/`.pyd`/`.dylib`) is exported as a notionally private top-level symbol
136
153
(`_mlir`), while a small set of Python code is provided in
@@ -160,7 +177,7 @@ are) with non-RTTI polymorphic C++ code (the default compilation mode of LLVM).
160
177
### Ownership in the Core IR
161
178
162
179
There are several top-level types in the core IR that are strongly owned by
163
-
their python-side reference:
180
+
their Python-side reference:
164
181
165
182
*`PyContext` (`mlir.ir.Context`)
166
183
*`PyModule` (`mlir.ir.Module`)
@@ -219,23 +236,24 @@ Due to the validity and parenting accounting needs, `PyOperation` is the owner
219
236
for regions and blocks. Operations are also the only entities which are allowed to be in
220
237
a detached state.
221
238
222
-
**Note**: Multiple `PyOperation` objects (i.e., the Python objects themselves) can alias a single `mlir::Operation`.
223
-
This means, for example, if you have `py_op1` and `py_op2` which wrap the same `mlir::Operation op`
239
+
**Note**: Multiple `PyOperation` objects (i.e., the Python objects themselves) can alias a single `mlir::Operation`.
240
+
This means, for example, if you have `py_op1` and `py_op2` which wrap the same `mlir::Operation op`
224
241
and you somehow transform `op` (e.g., you run a pass on `op`) then walking the MLIR AST via either/or `py_op1`, `py_op2`
225
-
will reflect the same MLIR AST. This is perfectly safe and supported. What is not supported is invalidating any
226
-
operation while there exist multiple Python objects wrapping that operation **and then manipulating those wrappers**.
227
-
For example if `py_op1` and `py_op2` wrap the same operation under a root `py_op3` and then `py_op3` is
228
-
transformed such that the operation referenced (by `py_op1`, `py_op2`) is erased. Then `py_op1`, `py_op2`
229
-
become "undefined" in a sense; manipulating them in any way is "formally forbidden". Note, this also applies to
230
-
`SymbolTable` mutation, which is considered a transformation of the root `SymbolTable`-supporting operation for the
231
-
purposes of the discussion here. Metaphorically, one can think of this similarly to how STL container iterators are invalidated once the container itself is changed. The "best practices" recommendation is to structure your code such that
242
+
will reflect the same MLIR AST. This is perfectly safe and supported. What is not supported is invalidating any
243
+
operation while there exist multiple Python objects wrapping that operation **and then manipulating those wrappers**.
244
+
For example if `py_op1` and `py_op2` wrap the same operation under a root `py_op3` and then `py_op3` is
245
+
transformed such that the operation referenced (by `py_op1`, `py_op2`) is erased. Then `py_op1`, `py_op2`
246
+
become "undefined" in a sense; manipulating them in any way is "formally forbidden". Note, this also applies to
247
+
`SymbolTable` mutation, which is considered a transformation of the root `SymbolTable`-supporting operation for the
248
+
purposes of the discussion here. Metaphorically, one can think of this similarly to how STL container iterators are invalidated
249
+
once the container itself is changed. The "best practices" recommendation is to structure your code such that
0 commit comments