-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[MLIR][Python] remove liveOperations
#155114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
9af755c
809f0dc
fd8a12a
8eb4d75
8c72271
70d6b56
685fbb5
57ad5a1
d319108
8ee9d2c
7cc8a50
e5e345b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,13 +216,26 @@ added to an attached operation, they need to be re-parented to the containing | |
module). | ||
|
||
Due to the validity and parenting accounting needs, `PyOperation` is the owner | ||
for regions and blocks and needs to be a top-level type that we can count on not | ||
aliasing. This let's us do things like selectively invalidating instances when | ||
mutations occur without worrying that there is some alias to the same operation | ||
in the hierarchy. Operations are also the only entity that are allowed to be in | ||
a detached state, and they are interned at the context level so that there is | ||
never more than one Python `mlir.ir.Operation` object for a unique | ||
`MlirOperation`, regardless of how it is obtained. | ||
for regions and blocks. Operations are also the only entities which are allowed to be in | ||
a detached state. | ||
|
||
**Note**: Multiple `PyOperation` objects (i.e., the Python objects themselves) can alias a single `mlir::Operation`. | ||
This means, for example, if you have `py_op1` and `py_op2` which wrap the same `mlir::Operation op` | ||
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` | ||
will reflect the same MLIR AST. This is perfectly safe and supported. What is not supported is invalidating any | ||
operation while there exist multiple Python objects wrapping that operation **and then manipulating those wrappers**. | ||
For example if `py_op1` and `py_op2` wrap the same operation under a root `py_op3` and then `py_op3` is | ||
transformed such that the operation referenced (by `py_op1`, `py_op2`) is erased. Then `py_op1`, `py_op2` | ||
become "undefined" in a sense; manipulating them in any way is "formally forbidden". Note, this also applies to | ||
`SymbolTable` mutation, which is considered a transformation of the root `SymbolTable`-supporting operation for the | ||
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 | ||
|
||
1. First, query/manipulate various Python wrapper objects `py_op1`, `py_op2`, `py_op3`, etc.; | ||
2. Second, Transform the AST/erase operations/etc. via a single root object; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/Transform/transform/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is effectively saying, scope your change based on nesting (e.g., change leafs first before going up in hierarchy, and/or only in very rare cases consider nested op post modifying parent). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure yea that's a very sophisticated way to put it :) i'll add that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
3. End. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what End means here. Invalidate/forget all queried nodes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea effectively - colloquially i meant "don't do anything else with the stuff you touched in steps 1 and 2" - but i'll change it to what you said There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
Ideally this should be done in a function body so that "End" corresponds to the end of the function and there are no | ||
risks of Python wrapper objects leaking/living longer than necessary. | ||
|
||
The C/C++ API allows for Region/Block to also be detached, but it simplifies the | ||
ownership model a lot to eliminate that possibility in this API, allowing the | ||
|
@@ -238,11 +251,6 @@ blocks. We may end up needing an op-local one at some point TBD, depending on | |
how hard it is to guarantee how mutations interact with their Python peer | ||
objects. We can cross that bridge easily when we get there. | ||
|
||
Module, when used purely from the Python API, can't alias anyway, so we can use | ||
it as a top-level ref type without a live-list for interning. If the API ever | ||
changes such that this cannot be guaranteed (i.e. by letting you marshal a | ||
native-defined Module in), then there would need to be a live table for it too. | ||
Comment on lines
-241
to
-244
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't even valid/true anymore - we do have (before this PR) a |
||
|
||
## User-level API | ||
|
||
### Context Management | ||
|
@@ -1229,4 +1237,4 @@ The exceptions to the free-threading compatibility: | |
- Usage of `Location.emit_error` is unsafe (due to thread-unsafe `llvm::raw_ostream`). | ||
- Usage of `Module.dump` is unsafe (due to thread-unsafe `llvm::raw_ostream`). | ||
- Usage of `mlir.dialects.transform.interpreter` is unsafe. | ||
- Usage of `mlir.dialects.gpu` and `gpu-module-to-binary` is unsafe. | ||
- Usage of `mlir.dialects.gpu` and `gpu-module-to-binary` is unsafe. |
Uh oh!
There was an error while loading. Please reload this page.