-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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][CAPI, python bindings] Expose Operation::setSuccessor
#67922
Merged
+130
−11
Conversation
This file contains 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
@llvm/pr-subscribers-mlir ChangesExpose Full diff: https://github.com/llvm/llvm-project/pull/67922.diff 4 Files Affected:
diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index a6408317db69e61..cafe592a0370007 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -618,6 +618,10 @@ MLIR_CAPI_EXPORTED bool
mlirOperationRemoveDiscardableAttributeByName(MlirOperation op,
MlirStringRef name);
+/// Set `pos`-th successor of the operation.
+MLIR_CAPI_EXPORTED void
+mlirOperationSetSuccessor(MlirOperation op, MlirBlock block, intptr_t pos);
+
/// Returns the number of attributes attached to the operation.
/// Deprecated, please use `mlirOperationGetNumInherentAttributes` or
/// `mlirOperationGetNumDiscardableAttributes`.
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index aad74f511e7ef11..4855300df44873f 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -2924,7 +2924,17 @@ void mlir::python::populateIRCore(py::module &m) {
&PyOperation::getCapsule)
.def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyOperation::createFromCapsule)
.def_property_readonly("operation", [](py::object self) { return self; })
- .def_property_readonly("opview", &PyOperation::createOpView);
+ .def_property_readonly("opview", &PyOperation::createOpView)
+ .def_property_readonly(
+ "num_successors",
+ [](PyOperation &self) { mlirOperationGetNumSuccessors(self); })
+ .def("get_successor",
+ [](PyOperation &self, int pos) {
+ mlirOperationGetSuccessor(self, pos);
+ })
+ .def("set_successor", [](PyOperation &self, PyBlock block, int pos) {
+ mlirOperationSetSuccessor(self, block.get(), pos);
+ });
auto opViewClass =
py::class_<PyOpView, PyOperationBase>(m, "OpView", py::module_local())
@@ -3448,7 +3458,8 @@ void mlir::python::populateIRCore(py::module &m) {
mlirOpPrintingFlagsUseLocalScope(flags);
valueState = mlirAsmStateCreateForValue(self.get(), flags);
}
- mlirValuePrintAsOperand(self.get(), valueState, printAccum.getCallback(),
+ mlirValuePrintAsOperand(self.get(), valueState,
+ printAccum.getCallback(),
printAccum.getUserData());
// Release state if allocated locally.
if (!state) {
diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index 65b2b7466fb7c39..18de1cc00b47d5d 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -637,6 +637,11 @@ bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op,
return !!unwrap(op)->removeDiscardableAttr(unwrap(name));
}
+void mlirOperationSetSuccessor(MlirOperation op, MlirBlock block,
+ intptr_t pos) {
+ unwrap(op)->setSuccessor(unwrap(block), static_cast<unsigned>(pos));
+}
+
intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
}
diff --git a/mlir/test/python/dialects/cf.py b/mlir/test/python/dialects/cf.py
new file mode 100644
index 000000000000000..82e8678987a6a87
--- /dev/null
+++ b/mlir/test/python/dialects/cf.py
@@ -0,0 +1,42 @@
+# RUN: %PYTHON %s | FileCheck %s
+
+from mlir.ir import *
+from mlir.dialects import cf
+
+
+def constructAndPrintInModule(f):
+ print("\nTEST:", f.__name__)
+ with Context() as ctx, Location.unknown():
+ ctx.allow_unregistered_dialects = True
+ module = Module.create()
+ with InsertionPoint(module.body):
+ f()
+ print(module)
+ print(module.operation.verify())
+ return f
+
+
+# CHECK-LABEL: TEST: testBranchAndSetSuccessor
+@constructAndPrintInModule
+def testBranchAndSetSuccessor():
+ op1 = Operation.create("custom.op1", regions=1)
+
+ block0 = op1.regions[0].blocks.append()
+ ip = InsertionPoint(block0)
+ terminator = Operation.create("custom.terminator", ip=ip)
+
+ block1 = op1.regions[0].blocks.append()
+ ip = InsertionPoint(block1)
+ br1 = cf.BranchOp([], block1, ip=ip)
+
+ block2 = op1.regions[0].blocks.append()
+ ip = InsertionPoint(block2)
+ br2 = cf.BranchOp([], block1, ip=ip)
+
+ br1.operation.set_successor(block2, 0)
+
+
+# CHECK: ^bb1: // pred: ^bb2
+# CHECK: cf.br ^bb2
+# CHECK: ^bb2: // pred: ^bb1
+# CHECK: cf.br ^bb1
|
makslevental
changed the title
[mlir][CAPI, python bindings]
[mlir][CAPI, python bindings] Expose Oct 1, 2023
Operation::setSuccessor
makslevental
force-pushed
the
set_successor
branch
3 times, most recently
from
October 1, 2023 16:18
9aa6ba0
to
c70e386
Compare
jpienaar
approved these changes
Oct 1, 2023
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.
Looks good, thanks (I see this follows the C convention, I'd have flipped block and pos, but keeping consistent better).
makslevental
force-pushed
the
set_successor
branch
6 times, most recently
from
October 2, 2023 01:27
b375d88
to
f491e79
Compare
ftynse
requested changes
Oct 2, 2023
makslevental
force-pushed
the
set_successor
branch
from
October 2, 2023 16:35
f491e79
to
69020fb
Compare
Expose `Operation::setSuccessor`.
makslevental
force-pushed
the
set_successor
branch
from
October 2, 2023 16:35
69020fb
to
2807bd3
Compare
ftynse
approved these changes
Oct 2, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is useful for emitting (using the python bindings)
cf.br
to blocks that are declared lexically post block creation.