operation_utils.h 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

#pragma once

17
#include "paddle/ir/core/attribute.h"
18
#include "paddle/ir/core/op_info.h"
19
#include "paddle/ir/core/region.h"
20
#include "paddle/ir/core/type.h"
21
#include "paddle/ir/core/value.h"
22 23 24 25 26 27 28 29 30 31 32 33

namespace ir {

using AttributeMap = std::unordered_map<std::string, Attribute>;

//===----------------------------------------------------------------------===//
// OperationArgument
//===----------------------------------------------------------------------===//

// This represents an operation arguments in an combined form, suitable for use
// with the builder APIs.
struct OperationArgument {
34
  std::vector<OpResult> inputs;
35
  AttributeMap attributes;
36 37 38
  std::vector<Type> output_types;
  OpInfo info;
  std::vector<std::unique_ptr<Region>> regions;
39 40

 public:
41 42 43
  OperationArgument(IrContext* ir_context, const std::string& name);
  explicit OperationArgument(OpInfo info) : info(info) {}
  OperationArgument(const std::vector<OpResult>& operands,
44
                    const AttributeMap& attributes,
45
                    const std::vector<Type>& types,
46 47 48
                    OpInfo info,
                    std::vector<std::unique_ptr<Region>>&& regions = {})
      : inputs(operands),
49
        attributes(attributes),
50 51 52
        output_types(types),
        info(info),
        regions(std::move(regions)) {}
53

54 55 56
  /// Add Operand.
  void AddOperand(OpResult operand) { inputs.emplace_back(operand); }

57
  template <class InputIt>
58
  void AddOperands(InputIt first, InputIt last);
59

60 61 62
  /// Add Output.
  void AddOutput(Type type) { output_types.emplace_back(type); }

63
  template <class InputIt>
64
  void AddTypes(InputIt first, InputIt last);
65 66

  /// Add an attribute with the specified name.
67
  void AddAttribute(const std::string& name, Attribute attr) {
68
    attributes[name] = attr;
69 70 71
  }
  /// Add an array of named attributes.
  template <class InputIt>
72
  void AddAttributes(InputIt first, InputIt last);
73
  /// Get the context held by this operation state.
74
  IrContext* getContext() const { return info.ir_context(); }
75 76 77 78 79

  Region* AddRegion() {
    regions.emplace_back(new Region);
    return regions.back().get();
  }
80 81 82
};

template <class InputIt>
83
void OperationArgument::AddOperands(InputIt first, InputIt last) {
84
  while (first != last) {
85
    inputs.emplace_back(*first++);
86 87 88
  }
}
template <class InputIt>
89
void OperationArgument::AddTypes(InputIt first, InputIt last) {
90
  while (first != last) {
91
    output_types.emplace_back(*first++);
92 93 94
  }
}
template <class InputIt>
95
void OperationArgument::AddAttributes(InputIt first, InputIt last) {
96
  while (first != last) {
97
    attributes[first->first] = first->second;
98 99 100 101 102
    ++first;
  }
}

}  // namespace ir