summaryrefslogtreecommitdiff
path: root/compiler/moco/pass/src/Passes/ConstantFoldAdd.cpp
blob: 018749b7801ff2e6cd3d2b20cf3efe4ab0c1349e (plain)
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
114
115
116
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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 "moco/Pass/Passes/ConstantFoldAdd.h"

#include "ConstantFoldHelper.h"

#include <moco/IR/Nodes/TFAdd.h>
#include <moco/IR/Nodes/TFConst.h>

#include <moco/Support/NodeAs.h>

namespace
{

struct Func final : public moco::BinaryFunc
{
  float apply(float lhs, float rhs) const { return lhs + rhs; }
  int32_t apply(int32_t lhs, int32_t rhs) const { return lhs + rhs; }
};

bool constantfold_add(moco::TFAdd *node)
{
  auto x_const = moco::as<moco::TFConst>(node->x());
  auto y_const = moco::as<moco::TFConst>(node->y());
  if (x_const == nullptr || y_const == nullptr)
    return false;

  if (x_const->dtype() != y_const->dtype())
    return false;
  // TODO support other types
  if (x_const->dtype() != loco::DataType::S32 && x_const->dtype() != loco::DataType::FLOAT32)
    return false;

  // NOTE we support limited shape of elementwise add or add with a scalar.
  //      valid_shape_for_constfold_binary_op() explains limited shape.
  auto x_shape = moco::tensor_shape(x_const);
  auto y_shape = moco::tensor_shape(y_const);
  if (!moco::valid_shape_for_constfold_binary_op(x_shape, y_shape))
    return false;

  loco::TensorShape output_shape;
  if (y_shape.rank() == 0 || y_shape.rank() == 1)
    output_shape = x_shape;
  else
    output_shape = y_shape;

  auto graph = node->graph();
  auto output_const = moco::new_const(graph, output_shape, x_const->dtype());
  Func f;

  if (x_const->dtype() == loco::DataType::S32)
  {
    moco::apply_binary<int32_t>(x_const, y_const, output_const, f);
  }
  else if (x_const->dtype() == loco::DataType::FLOAT32)
  {
    moco::apply_binary<float>(x_const, y_const, output_const, f);
  }

  // replace
  loco::replace(node).with(output_const);

  return true;
}

} // namespace

namespace moco
{

/**
 * @note This will Replace TFAdd with TFConst when inputs are TFConst
 *
 *       Before
 *                 A --- TFAdd --- C
 *                 B --/
 *       After
 *                 A --- TFAdd
 *                 B --/
 *                       TFConst ---------- C
 *       Where
 *                 A,B : inputs of TFAdd
 *                 C : a node that uses TFAdd as an input
 *                 TFAdd is disconnected from C
 *                 Nodes are drawn multiple times to simplify the diagram
 */
bool ConstantFoldAdd::run(loco::Graph *graph)
{
  bool changed = false;
  for (auto node : loco::active_nodes(loco::output_nodes(graph)))
  {
    if (auto add_node = as<moco::TFAdd>(node))
    {
      if (constantfold_add(add_node))
        changed = true;
    }
  }

  return changed;
}

} // namespace moco