summaryrefslogtreecommitdiff
path: root/compiler/enco/core/src/Transforms/Duplicate.cpp
blob: 91f64a0ade933108259aec953f05dcd8daace6be (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2018 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 "Duplicate.h"

#include <map>
#include <set>

#include <cassert>

namespace
{

coco::Block *find_or_create_first_block(coco::Module *m)
{
  if (m->block()->empty())
  {
    auto blk = m->entity()->block()->create();
    m->block()->append(blk);
    return blk;
  }

  return m->block()->head();
}

} // namespace

namespace
{

class DuplicatePass
{
private:
  void runOnModule(coco::Module *m) const;

public:
  void runOnCode(enco::Code *) const;
};

void DuplicatePass::runOnModule(coco::Module *m) const
{
  // Let's find candidates
  std::set<coco::Bag *> candidates;

  for (uint32_t n = 0; n < m->entity()->bag()->size(); ++n)
  {
    auto bag = m->entity()->bag()->at(n);

    if (bag->isInput() && bag->isOutput())
    {
      candidates.insert(bag);
    }
  }

  // Return if there is no candidate
  if (candidates.empty())
  {
    return;
  }

  std::map<const coco::Bag *, coco::Input *> input_map;
  std::map<const coco::Bag *, coco::Output *> output_map;

  for (uint32_t n = 0; n < m->input()->size(); ++n)
  {
    auto input = m->input()->at(n);
    assert(input->bag() != nullptr);
    input_map[input->bag()] = input;
  }

  for (uint32_t n = 0; n < m->output()->size(); ++n)
  {
    auto output = m->output()->at(n);
    assert(output->bag() != nullptr);
    output_map[output->bag()] = output;
  }

  // For each in/out bag,
  //   1. Create a new bag of the same size
  //   2. Copy the content from the original bag
  //   3. Mark the newly created bag as an output
  for (const auto &candidate : candidates)
  {
    assert(coco::updaters(candidate).empty());
    assert(input_map.find(candidate) != input_map.end());
    assert(output_map.find(candidate) != output_map.end());

    auto src = candidate;
    auto dst = m->entity()->bag()->create(src->size());

    // Create a copy instruction
    auto shuffle = m->entity()->instr()->create<coco::Shuffle>();

    shuffle->from(src);
    shuffle->into(dst);

    for (uint32_t n = 0; n < src->size(); ++n)
    {
      shuffle->insert(coco::ElemID{n} /* FROM */, coco::ElemID{n} /* INTO */);
    }

    find_or_create_first_block(m)->instr()->prepend(shuffle);

    // Let's use the new bag as an output
    output_map.at(src)->bag(dst);
  }
}

void DuplicatePass::runOnCode(enco::Code *code) const { runOnModule(code->module()); }

} // namespace

namespace enco
{

void duplicate_inout_bag(enco::Code *code)
{
  DuplicatePass duplicate;
  duplicate.runOnCode(code);
}

} // namespace enco