[enco] Introduce Dead Object Elimination (#1691)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 1 Oct 2018 07:19:53 +0000 (16:19 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 1 Oct 2018 07:19:53 +0000 (16:19 +0900)
* [enco] Introduce Dead Object Elimination

This commit introduces Dead Object Elimination (DOE) pass in enco
compilation pipeline.

DOE is a special case of Dead Bag Elimination (DBE), but inserted in
order to reduce compilation overhead.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Fix a typo (updater -> producer)

contrib/enco/core/src/Backend.cpp
contrib/enco/core/src/Transforms/DeadObjectElimination.cpp [new file with mode: 0644]
contrib/enco/core/src/Transforms/DeadObjectElimination.h [new file with mode: 0644]

index 81a6ff0..2dbb66f 100644 (file)
@@ -23,6 +23,7 @@
 #include "Transforms/FeatureUnification.h"
 #include "Transforms/Rewrite.h"
 #include "Transforms/Normalize.h"
+#include "Transforms/DeadObjectElimination.h"
 #include "Transforms/Optimizations.h"
 #include "Transforms/Split.h"
 
@@ -100,6 +101,12 @@ void BackendImpl::compile(coco::Module *m, coco::Data *d)
   NormalizePass normalize;
   normalize.runOnCode(&code);
 
+  // Eliminate dead object
+  //
+  // NOTE Dead Object Elimination (DOE) is performed before Copy lowering
+  //      in order to reduce compilation overhead.
+  eliminate_dead_object(&code);
+
   // Lower Copy as Shuffle
   lower_copy(&code);
 
diff --git a/contrib/enco/core/src/Transforms/DeadObjectElimination.cpp b/contrib/enco/core/src/Transforms/DeadObjectElimination.cpp
new file mode 100644 (file)
index 0000000..1f5970e
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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 "DeadObjectElimination.h"
+
+#include <set>
+
+namespace
+{
+
+std::set<coco::Object *> dead_objects(const coco::Module *m)
+{
+  std::set<coco::Object *> res;
+
+  for (uint32_t n = 0; n < m->entity()->object()->size(); ++n)
+  {
+    auto obj = m->entity()->object()->at(n);
+    auto bag = obj->bag();
+
+    if (coco::readers(bag).empty() && !(bag->isOutput()))
+    {
+      res.insert(obj);
+    }
+  }
+
+  return res;
+}
+
+} // namespace
+
+namespace enco
+{
+
+void eliminate_dead_object(enco::Code *code)
+{
+  auto m = code->module();
+
+  // Destroy a dead object and its producer
+  for (auto obj : dead_objects(m))
+  {
+    if (auto producer = coco::producer(obj))
+    {
+      auto ins = producer->loc();
+      assert(ins != nullptr);
+
+      ins->detach();
+      m->entity()->instr()->destroy(ins);
+    }
+
+    m->entity()->object()->destroy(obj);
+  }
+}
+
+} // namespace enco
diff --git a/contrib/enco/core/src/Transforms/DeadObjectElimination.h b/contrib/enco/core/src/Transforms/DeadObjectElimination.h
new file mode 100644 (file)
index 0000000..8cefe92
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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_DEAD_OBJECT_ELIMINATION_H__
+#define __ENCO_TRANSFORM_DEAD_OBJECT_ELIMINATION_H__
+
+#include "Code.h"
+
+namespace enco
+{
+
+/**
+ * @brief Eliminate dead objects in IR
+ *
+ * An object whose backing bag is unused is referred to as a dead object.
+ *
+ * Dead Object Elimination (DOE) eliminates such dead objects along with their producer.
+ */
+void eliminate_dead_object(enco::Code *code);
+
+} // namespace enco
+
+#endif // __ENCO_TRANSFORM_DEAD_OBJECT_ELIMINATION_H__