utils.cc 2.8 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
// 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/cinn/hlir/framework/new_ir/utils.h"

namespace cinn {
namespace hlir {
namespace framework {
namespace newir {

const std::unordered_map<std::string, std::string> CompatibleInfo::OP_NAMES = {
    {"pd.full", "fill_constant"}};

std::string CompatibleInfo::OpName(const ::ir::Operation& op) {
  std::string name = op.name();
  if (OP_NAMES.count(name)) {
    return OP_NAMES.at(name);
  }
  auto pos = name.find(".");
  if (pos == std::string::npos) {
    return name;
  }
  auto cinn_op_name = name.substr(pos + 1);
  VLOG(4) << "GetOpName: " << name << " -> " << cinn_op_name;
  return cinn_op_name;
}

39 40
std::string CompatibleInfo::ValueName(const ::ir::Value& value) {
  return CompatibleInfo::kNamePrefix +
41 42 43 44 45 46 47 48 49 50 51 52
         std::to_string(std::hash<::ir::Value>()(value));
}

std::string CompatibleInfo::OpFuncName(const ::ir::Operation& op) {
  std::string op_name = OpName(op);
  std::string func_name =
      cinn::common::Context::Global().NewName("fn_" + op_name);
  return func_name;
}

std::string CompatibleInfo::GroupOpsName(
    const std::vector<::ir::Operation*>& ops) {
53
  std::string name = "fn";
54 55
  for (auto* op : ops) {
    std::string op_name = OpName(*op);
56
    name += "_" + cinn::common::Context::Global().NewName(op_name);
57 58 59 60 61 62 63 64 65 66
  }
  return name;
}

std::vector<std::string> CompatibleInfo::InputNames(const ::ir::Operation& op,
                                                    bool allow_duplicate) {
  std::vector<std::string> names;
  std::unordered_set<std::string> repeat;
  for (int i = 0; i < op.num_operands(); ++i) {
    auto value = op.operand_source(i);
67
    std::string name = CompatibleInfo::ValueName(value);
68 69 70 71 72 73 74 75 76 77 78 79 80 81
    if (!allow_duplicate && repeat.count(name)) {
      continue;
    }
    repeat.insert(name);
    names.push_back(name);
  }
  return names;
}

std::vector<std::string> CompatibleInfo::OutputNames(
    const ::ir::Operation& op) {
  std::vector<std::string> names;
  for (int i = 0; i < op.num_results(); ++i) {
    auto value = op.result(i);
82
    std::string name = CompatibleInfo::ValueName(value);
83 84 85 86 87 88 89 90 91
    names.push_back(std::move(name));
  }
  return names;
}

}  // namespace newir
}  // namespace framework
}  // namespace hlir
}  // namespace cinn