未验证 提交 c595655b 编写于 作者: W winter-wang 提交者: GitHub

[IR] add positon member in block. (#54565)

上级 7d688871
...@@ -38,4 +38,10 @@ void Block::clear() { ...@@ -38,4 +38,10 @@ void Block::clear() {
ops_.pop_back(); ops_.pop_back();
} }
} }
void Block::SetParent(Region *parent, Region::iterator position) {
parent_ = parent;
position_ = position;
}
} // namespace ir } // namespace ir
...@@ -17,8 +17,9 @@ ...@@ -17,8 +17,9 @@
#include <cstddef> #include <cstddef>
#include <list> #include <list>
#include "paddle/ir/core/region.h"
namespace ir { namespace ir {
class Region;
class Operation; class Operation;
class Block { class Block {
...@@ -47,16 +48,19 @@ class Block { ...@@ -47,16 +48,19 @@ class Block {
void push_front(Operation *op); void push_front(Operation *op);
iterator insert(const_iterator iterator, Operation *op); iterator insert(const_iterator iterator, Operation *op);
void clear(); void clear();
operator Region::iterator() { return position_; }
private: private:
Block(Block &) = delete; Block(Block &) = delete;
Block &operator=(const Block &) = delete; Block &operator=(const Block &) = delete;
// Allow access to 'SetParent'.
friend class Region; friend class Region;
void SetParent(Region *parent) { parent_ = parent; } void SetParent(Region *parent, Region::iterator position);
private: private:
Region *parent_; // not owned Region *parent_; // not owned
Region::iterator position_;
std::list<Operation *> ops_; // owned std::list<Operation *> ops_; // owned
}; };
} // namespace ir } // namespace ir
...@@ -236,4 +236,9 @@ Region &Operation::GetRegion(unsigned index) { ...@@ -236,4 +236,9 @@ Region &Operation::GetRegion(unsigned index) {
return regions_[index]; return regions_[index];
} }
void Operation::SetParent(Block *parent, const Block::iterator &position) {
parent_ = parent;
position_ = position;
}
} // namespace ir } // namespace ir
...@@ -112,11 +112,9 @@ class alignas(8) Operation final { ...@@ -112,11 +112,9 @@ class alignas(8) Operation final {
} }
}; };
// Allow access to 'SetParent'.
friend class Block; friend class Block;
void SetParent(Block *parent, const Block::iterator &position) { void SetParent(Block *parent, const Block::iterator &position);
parent_ = parent;
position_ = position;
}
template <typename T> template <typename T>
struct CastUtil< struct CastUtil<
......
...@@ -18,27 +18,22 @@ ...@@ -18,27 +18,22 @@
namespace ir { namespace ir {
Region::~Region() { clear(); } Region::~Region() { clear(); }
void Region::push_back(Block *block) { void Region::push_back(Block *block) { insert(blocks_.end(), block); }
block->SetParent(this);
blocks_.push_back(block);
}
void Region::emplace_back() { push_back(new Block); } void Region::emplace_back() { push_back(new Block); }
void Region::push_front(Block *block) { void Region::push_front(Block *block) { insert(blocks_.begin(), block); }
block->SetParent(this);
blocks_.push_front(block);
}
Region::iterator Region::insert(const_iterator position, Block *block) { Region::iterator Region::insert(const_iterator position, Block *block) {
block->SetParent(this); Region::iterator iter = blocks_.insert(position, block);
return blocks_.insert(position, block); block->SetParent(this, iter);
return iter;
} }
void Region::TakeBody(Region &&other) { void Region::TakeBody(Region &&other) {
clear(); clear();
blocks_.swap(other.blocks_); blocks_.swap(other.blocks_);
for (auto &block : blocks_) { for (auto iter = blocks_.begin(); iter != blocks_.end(); ++iter) {
block->SetParent(this); (*iter)->SetParent(this, iter);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册