[enco] Introduce IdenticalObjectReduction pass (#1694)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 1 Oct 2018 10:31:58 +0000 (19:31 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 1 Oct 2018 10:31:58 +0000 (19:31 +0900)
This commit implements Identical Object Reduction optimization as an
enco pass.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/src/Backend.cpp
contrib/enco/core/src/Transforms/IdenticalObjectReduction.cpp [new file with mode: 0644]
contrib/enco/core/src/Transforms/IdenticalObjectReduction.h [new file with mode: 0644]

index 617650e..645b048 100644 (file)
@@ -24,6 +24,7 @@
 #include "Transforms/Rewrite.h"
 #include "Transforms/Normalize.h"
 #include "Transforms/IndirectCopyElimination.h"
+#include "Transforms/IdenticalObjectReduction.h"
 #include "Transforms/DeadObjectElimination.h"
 #include "Transforms/Optimizations.h"
 #include "Transforms/Split.h"
@@ -103,6 +104,7 @@ void BackendImpl::compile(coco::Module *m, coco::Data *d)
   normalize.runOnCode(&code);
 
   eliminate_indirect_copy(&code);
+  reduce_identical_object(&code);
 
   // Eliminate dead object
   //
diff --git a/contrib/enco/core/src/Transforms/IdenticalObjectReduction.cpp b/contrib/enco/core/src/Transforms/IdenticalObjectReduction.cpp
new file mode 100644 (file)
index 0000000..a6cd8ac
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * 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 "IdenticalObjectReduction.h"
+#include "IRUtils.h"
+
+#include <set>
+
+namespace enco
+{
+
+void reduce_identical_object(enco::Code *code)
+{
+  auto m = code->module();
+
+  std::set<coco::Copy *> detached;
+
+  for (uint32_t n = 0; n < m->entity()->instr()->size(); ++n)
+  {
+    auto ins = m->entity()->instr()->at(n);
+    assert(ins != nullptr);
+    assert(ins->parent() != nullptr);
+
+    auto copy = ins->asCopy();
+
+    if (copy == nullptr)
+    {
+      // Skip if instruction is not a copy
+      continue;
+    }
+
+    // TODO Support non-Feature Objects
+    auto ifm = copy->from()->asFeature();
+    auto ofm = copy->into()->asFeature();
+
+    if (ifm->layout()->id() != ofm->layout()->id())
+    {
+      continue;
+    }
+
+    if (ifm->layout()->id() != coco::FeatureLayouts::BHWC::uid())
+    {
+      continue;
+    }
+
+    subst(copy->into(), copy->from());
+
+    copy->detach();
+    detached.insert(copy);
+  }
+
+  for (auto copy : detached)
+  {
+    m->entity()->instr()->destroy(copy);
+  }
+}
+
+} // namespace enco
diff --git a/contrib/enco/core/src/Transforms/IdenticalObjectReduction.h b/contrib/enco/core/src/Transforms/IdenticalObjectReduction.h
new file mode 100644 (file)
index 0000000..bbf624f
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.
+ */
+
+#ifndef __ENCO_TRANSFORM_IDENTICAL_OBJECT_REDUCTION_H__
+#define __ENCO_TRANSFORM_IDENTICAL_OBJECT_REDUCTION_H__
+
+#include "Code.h"
+
+namespace enco
+{
+
+/**
+ * @brief Reduce identically copied objects as its original object
+ *
+ * >>> BEFORE <<<
+ * %bag_0 = Bag(size: N)
+ * %bag_1 = Bag(size: N)
+ *
+ * %obj_0 = Feature(layout: BHWC) at %bag_0
+ * %obj_1 = Feature(layout: BHWC) at %bag_1
+ *
+ * copy(from: %obj_0, into: %obj_1)
+ * ...
+ * Use(%obj_0)
+ * Use(%obj_1)
+ * ...
+ *
+ * >>> AFTER <<<
+ * %bag_0 = Bag(size: N)
+ * %bag_1 = Bag(size: N)
+ *
+ * %obj_0 = Feature(layout: BHWC) at %bag_0
+ * %obj_1 = Feature(layout: BHWC) at %bag_1
+ *
+ * copy(from: %obj_0, into: %obj_1)
+ * ...
+ * Use(%obj_0)
+ * Use(%obj_0) <- %obj_1 is replaced
+ * ...
+ */
+void reduce_identical_object(enco::Code *code);
+
+} // namespace enco
+
+#endif // __ENCO_TRANSFORM_IDENTICAL_OBJECT_REDUCTION_H__