[logo] Introduce PhaseRunner (#6909)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 26 Aug 2019 07:25:42 +0000 (16:25 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 26 Aug 2019 07:25:42 +0000 (16:25 +0900)
This will introduce PhaseRunner that runs group of Pass(es) that is defined as a Phase

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
compiler/logo/include/logo/Phase.h [new file with mode: 0644]
compiler/logo/src/Phase.cpp [new file with mode: 0644]

diff --git a/compiler/logo/include/logo/Phase.h b/compiler/logo/include/logo/Phase.h
new file mode 100644 (file)
index 0000000..1a1a6f0
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+
+#ifndef __LOGO_PHASE_H__
+#define __LOGO_PHASE_H__
+
+#include <logo/Pass.h>
+
+#include <loco.h>
+
+#include <vector>
+#include <memory>
+
+namespace logo
+{
+
+// Phase is a collection of Pass(es)
+using Phase = std::vector<std::unique_ptr<Pass>>;
+
+enum class PhaseStrategy
+{
+  // Run all the passes until there is no pass that makes a change
+  Saturate,
+  // Same as Saturate but will restart from the first when there is a change
+  Restart,
+};
+
+template <PhaseStrategy S> class PhaseRunner;
+
+template <> class PhaseRunner<PhaseStrategy::Saturate>
+{
+public:
+  PhaseRunner(loco::Graph *graph) : _graph{graph}
+  {
+    // DO NOTHING
+  }
+
+public:
+  void run(const Phase &) const;
+
+private:
+  loco::Graph *_graph;
+};
+
+template <> class PhaseRunner<PhaseStrategy::Restart>
+{
+public:
+  PhaseRunner(loco::Graph *graph) : _graph{graph}
+  {
+    // DO NOTHING
+  }
+
+public:
+  void run(const Phase &) const;
+
+private:
+  loco::Graph *_graph;
+};
+
+} // namespace logo
+
+#endif // __LOGO_PHASE_H__
diff --git a/compiler/logo/src/Phase.cpp b/compiler/logo/src/Phase.cpp
new file mode 100644 (file)
index 0000000..6964302
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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 <logo/Phase.h>
+
+namespace logo
+{
+
+void PhaseRunner<PhaseStrategy::Saturate>::run(const Phase &phase) const
+{
+  for (bool changed = true; changed;)
+  {
+    changed = false;
+
+    for (auto &pass : phase)
+    {
+      bool pass_changed = pass->run(_graph);
+      changed = changed || pass_changed;
+    }
+  }
+}
+
+void PhaseRunner<PhaseStrategy::Restart>::run(const Phase &phase) const
+{
+  for (bool changed = true; changed;)
+  {
+    changed = false;
+
+    for (auto &pass : phase)
+    {
+      if (pass->run(_graph))
+      {
+        changed = true;
+      }
+
+      if (changed)
+      {
+        break;
+      }
+    }
+  }
+}
+
+} // namespace logo