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 <memory>
18
#include "paddle/ir/core/attribute.h"
19
#include "paddle/ir/core/op_info.h"
20
#include "paddle/ir/core/region.h"
21
#include "paddle/ir/core/type.h"
22
#include "paddle/ir/core/value.h"
23 24 25 26 27 28 29 30 31 32 33 34

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 {
35
  std::vector<OpResult> inputs;
36
  AttributeMap attributes;
37 38 39
  std::vector<Type> output_types;
  OpInfo info;
  std::vector<std::unique_ptr<Region>> regions;
40 41

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

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

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

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

64
  template <class InputIt>
65
  void AddOutputs(InputIt first, InputIt last);
66 67

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

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

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

}  // namespace ir