-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[mlir][python] bind block predecessors and successors #145116
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
[mlir][python] bind block predecessors and successors #145116
Conversation
173c5c3
to
2160339
Compare
@llvm/pr-subscribers-mlir Author: Maksim Levental (makslevental) Changesbind Full diff: https://github.com/llvm/llvm-project/pull/145116.diff 4 Files Affected:
|
04ef6e4
to
c49fd8a
Compare
69186b6
to
bedc679
Compare
@@ -986,6 +986,20 @@ MLIR_CAPI_EXPORTED MlirValue mlirBlockGetArgument(MlirBlock block, | |||
MLIR_CAPI_EXPORTED void | |||
mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData); | |||
|
|||
/// Returns the number of successor blocks of the block. | |||
MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumSuccessors(MlirBlock block); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's have tests for the C API as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a test
Block::pred_iterator it = b->pred_begin(); | ||
std::advance(it, pos); | ||
return wrap(*it); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather avoid iterating over the use-def list every time... This goes through block's use-def chain, maybe there is a way to expose a BlockOperand
(and incidentally OpOperand
if it isn't) and a getNextUse
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think so
/// Implement a predecessor iterator for blocks. This works by walking the use |
Compare with SuccessorRange
just below there. But maybe I'm wrong and it's just not clicking for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry just to add a litlle more "context"; if you look at getSuccessors
and getSuccessor(unsigned)
they work differently but still in a way that doesn't seem possible for getPredecessors
:
SuccessorRange getSuccessors() { return SuccessorRange(this); }
...
SuccessorRange::SuccessorRange(Block *block) : SuccessorRange() {
if (block->empty() || llvm::hasSingleElement(*block->getParent()))
return;
Operation *term = &block->back();
if ((count = term->getNumSuccessors()))
base = term->getBlockOperands().data();
}
and
Block *Block::getSuccessor(unsigned i) {
assert(i < getNumSuccessors());
return getTerminator()->getSuccessor(i);
}
...
class Operation {
...
Block *getSuccessor(unsigned index) {
assert(index < getNumSuccessors());
return getBlockOperands()[index].get();
}
...
}
compared with
using pred_iterator = PredecessorIterator;
pred_iterator pred_begin() {
return pred_iterator((BlockOperand *)getFirstUse());
}
pred_iterator pred_end() { return pred_iterator(nullptr); }
iterator_range<pred_iterator> getPredecessors() {
return {pred_begin(), pred_end()};
}
so while I agree that iterating the chain isn't great I don't see what else can be done (other than caching those predecessors, which I'm sure we don't want to do either).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define predecessor
as returning an iterable object that only has __next__
?
Not a big problem if we don't, we use indexed accessors for the linked list of blocks as well because I thought it whoever was using Python didn't care about that level of performance tweaking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is there's no way to implement GetNextPredecessor
like GetNextBlockInRegion
https://github.com/llvm/llvm-project/blob/main/mlir/lib/CAPI/IR/IR.cpp#L969
without holding an instance of PredecessorIterator
(even forgetting that it needs to be mapped into C).
Anyway ya I'm gonna leave this as is but I'll add a comment mentioning that it's expensive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added warning
bedc679
to
c760955
Compare
8776917
to
035e009
Compare
dc0e8af
to
92b2868
Compare
92b2868
to
006d927
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Alex's comment, potentially file an issue or add a TODO.
mlir/test/CAPI/ir.c
Outdated
MlirBlock middleBlock = mlirBlockGetNextInRegion(entryBlock); | ||
MlirBlock successorBlock = mlirBlockGetNextInRegion(middleBlock); | ||
|
||
assert(mlirBlockGetNumPredecessors(entryBlock) == 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would not be tested in opt builds. I think one would need explicit tests and return non zero for these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are other asserts in here but sure I'll replace with fprintf
0bd5835
to
91333eb
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
91333eb
to
198b74d
Compare
bind `block.getSuccessor` and `block.getPredecessors`.
bind `block.getSuccessor` and `block.getPredecessors`.
bind
block.getSuccessor
andblock.getPredecessors
.