block_operand.cc 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
// 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.

#include "paddle/ir/core/block_operand.h"
#include "paddle/ir/core/block.h"
#include "paddle/ir/core/block_operand_impl.h"
#include "paddle/ir/core/enforce.h"

namespace ir {

#define CHECK_BLOCKOPEREND_NULL_IMPL(func_name)                 \
  IR_ENFORCE(impl_,                                             \
             "impl_ pointer is null when call func:" #func_name \
             " , in class: BlockOperand.")

BlockOperand &BlockOperand::operator=(const BlockOperand &rhs) {
  if (this == &rhs) return *this;
  impl_ = rhs.impl_;
  return *this;
}

BlockOperand::operator bool() const { return impl_ && impl_->source(); }

BlockOperand BlockOperand::next_use() const {
  CHECK_BLOCKOPEREND_NULL_IMPL(next_use);
  return impl_->next_use();
}

Block *BlockOperand::source() const {
  CHECK_BLOCKOPEREND_NULL_IMPL(source);
  return impl_->source();
}

void BlockOperand::set_source(Block *source) {
  CHECK_BLOCKOPEREND_NULL_IMPL(set_source);
  impl_->set_source(source);
}

Operation *BlockOperand::owner() const {
  CHECK_BLOCKOPEREND_NULL_IMPL(owner);
  return impl_->owner();
}

void BlockOperand::RemoveFromUdChain() {
  CHECK_BLOCKOPEREND_NULL_IMPL(RemoveFromUdChain);
  return impl_->RemoveFromUdChain();
}

// details
namespace detail {

Operation *BlockOperandImpl::owner() const { return owner_; }

BlockOperand BlockOperandImpl::next_use() const { return next_use_; }

Block *BlockOperandImpl::source() const { return source_; }

void BlockOperandImpl::set_source(Block *source) {
  RemoveFromUdChain();
  if (!source) {
    return;
  }
  source_ = source;
  InsertToUdChain();
}

BlockOperandImpl::BlockOperandImpl(Block *source, ir::Operation *owner)
    : source_(source), owner_(owner) {
  if (!source) {
    return;
  }
  InsertToUdChain();
}

void BlockOperandImpl::InsertToUdChain() {
  prev_use_addr_ = source_->first_use_addr();
  next_use_ = source_->first_use();
  if (next_use_) {
    next_use_.impl()->prev_use_addr_ = &next_use_;
  }
  source_->set_first_use(this);
}

void BlockOperandImpl::RemoveFromUdChain() {
  if (!source_) return;
  if (!prev_use_addr_) return;
  if (prev_use_addr_ == source_->first_use_addr()) {
    source_->set_first_use(next_use_);
  } else {
    *prev_use_addr_ = next_use_;
  }
  if (next_use_) {
    next_use_.impl()->prev_use_addr_ = prev_use_addr_;
  }
  next_use_ = nullptr;
  prev_use_addr_ = nullptr;
  source_ = nullptr;
}

BlockOperandImpl::~BlockOperandImpl() { RemoveFromUdChain(); }
}  // namespace detail
}  // namespace ir