-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9af755c
[mlir][python] wip remove liveOpeartions
makslevental 809f0dc
"fix" testPostPassOpInvalidation
makslevental fd8a12a
try to fix testModuleCapsule
makslevental 8eb4d75
add check for Operation._CAPICreate
makslevental 8c72271
update docs
makslevental 70d6b56
comments
makslevental 685fbb5
update the docs
makslevental 57ad5a1
Update Python.md
makslevental d319108
Update Python.md
makslevental 8ee9d2c
Update Python.md
makslevental 7cc8a50
re-enable invalidating ops
makslevental e5e345b
comments
makslevental File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,13 +216,28 @@ 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; | ||
3. Invalidate all queried nodes (e.g., using `op._set_invalid()`). | ||
|
||
Ideally this should be done in a function body so that step (3) corresponds to the end of the function and there are no | ||
risks of Python wrapper objects leaking/living longer than necessary. In summary, you should scope your changes based on | ||
nesting i.e., change leaf nodes first before going up in hierarchy, and only in very rare cases query nested ops post | ||
modifying a parent op. | ||
|
||
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 +253,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 +1239,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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.