operation_utils.h 3.0 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

  template <class InputIt>
55
  void AddOperands(InputIt first, InputIt last);
56 57

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

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

  Region* AddRegion() {
    regions.emplace_back(new Region);
    return regions.back().get();
  }
74 75 76
};

template <class InputIt>
77
void OperationArgument::AddOperands(InputIt first, InputIt last) {
78
  while (first != last) {
79
    inputs.emplace_back(*first++);
80 81 82
  }
}
template <class InputIt>
83
void OperationArgument::AddTypes(InputIt first, InputIt last) {
84
  while (first != last) {
85
    output_types.emplace_back(*first++);
86 87 88
  }
}
template <class InputIt>
89
void OperationArgument::AddAttributes(InputIt first, InputIt last) {
90
  while (first != last) {
91
    attributes[first->first] = first->second;
92 93 94 95 96
    ++first;
  }
}

}  // namespace ir