block.cc 1.6 KB
Newer Older
Z
zhangbo9674 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#include "paddle/ir/core/block.h"
16
#include "paddle/ir/core/enforce.h"
17 18
#include "paddle/ir/core/operation.h"
#include "paddle/ir/core/region.h"
Z
zhangbo9674 已提交
19 20 21

namespace ir {
Block::~Block() { clear(); }
22 23 24
void Block::push_back(Operation *op) { insert(ops_.end(), op); }

void Block::push_front(Operation *op) { insert(ops_.begin(), op); }
25

26 27
Operation *Block::GetParentOp() const {
  return parent_ ? parent_->GetParent() : nullptr;
28 29 30
}

Block::iterator Block::insert(const_iterator iterator, Operation *op) {
31 32 33
  Block::iterator iter = ops_.insert(iterator, op);
  op->SetParent(this, iter);
  return iter;
34
}
Z
zhangbo9674 已提交
35

36 37 38 39 40 41
Block::iterator Block::erase(const_iterator position) {
  IR_ENFORCE((*position)->GetParent() == this, "iterator not own this block.");
  (*position)->Destroy();
  return ops_.erase(position);
}

Z
zhangbo9674 已提交
42 43
void Block::clear() {
  while (!empty()) {
44
    ops_.back()->Destroy();
Z
zhangbo9674 已提交
45 46 47
    ops_.pop_back();
  }
}
48 49 50 51 52 53

void Block::SetParent(Region *parent, Region::iterator position) {
  parent_ = parent;
  position_ = position;
}

Z
zhangbo9674 已提交
54
}  // namespace ir