From a1475914c51050e11e3d92dba1dcbb15ca6a2c16 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Tue, 1 Aug 2023 11:31:18 +0800 Subject: [PATCH] =?UTF-8?q?[IR=20Operation]=20=E2=9A=94Elden=20chapter=201?= =?UTF-8?q?.3=E2=9A=94=20(#55657)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- paddle/fluid/pybind/ir.cc | 7 ++++++- paddle/ir/core/operation.cc | 6 +++--- paddle/ir/core/operation.h | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/paddle/fluid/pybind/ir.cc b/paddle/fluid/pybind/ir.cc index ead5e6889d9..e895275c6ae 100644 --- a/paddle/fluid/pybind/ir.cc +++ b/paddle/fluid/pybind/ir.cc @@ -94,7 +94,12 @@ void BindBlock(py::module *m) { void BindOperation(py::module *m) { py::class_ op(*m, "Operation"); op.def("name", &Operation::name) - .def("get_parent", &Operation::GetParent, return_value_policy::reference) + .def("get_parent", + py::overload_cast<>(&Operation::GetParent), + return_value_policy::reference) + .def("get_parent", + py::overload_cast<>(&Operation::GetParent, py::const_), + return_value_policy::reference) .def("num_results", &Operation::num_results) .def("result", &Operation::result) .def("operands", diff --git a/paddle/ir/core/operation.cc b/paddle/ir/core/operation.cc index ffecb9922ad..7e91c4c0700 100644 --- a/paddle/ir/core/operation.cc +++ b/paddle/ir/core/operation.cc @@ -210,7 +210,7 @@ Attribute Operation::attribute(const std::string &key) const { return attributes_.at(key); } -Region *Operation::GetParentRegion() const { +Region *Operation::GetParentRegion() { return parent_ ? parent_->GetParent() : nullptr; } @@ -218,8 +218,8 @@ Operation *Operation::GetParentOp() const { return parent_ ? parent_->GetParentOp() : nullptr; } -Program *Operation::GetParentProgram() { - Operation *op = this; +const Program *Operation::GetParentProgram() const { + Operation *op = const_cast(this); while (Operation *parent_op = op->GetParentOp()) { op = parent_op; } diff --git a/paddle/ir/core/operation.h b/paddle/ir/core/operation.h index 8518853160c..d47a99486c7 100644 --- a/paddle/ir/core/operation.h +++ b/paddle/ir/core/operation.h @@ -109,13 +109,23 @@ class IR_API alignas(8) Operation final { return info_.HasInterface(); } - Block *GetParent() const { return parent_; } + const Block *GetParent() const { return parent_; } - Region *GetParentRegion() const; + Block *GetParent() { + return const_cast( + const_cast(this)->GetParent()); + } + + Region *GetParentRegion(); Operation *GetParentOp() const; - Program *GetParentProgram(); + const Program *GetParentProgram() const; + + Program *GetParentProgram() { + return const_cast( + const_cast(this)->GetParentProgram()); + } operator Block::iterator() { return position_; } -- GitLab