[loco] Add DataType attribute to Graph Input/Output (#4097)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 4 Jul 2019 10:27:03 +0000 (19:27 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 4 Jul 2019 10:27:03 +0000 (19:27 +0900)
This commit allows loco frontends to record the data type of graph-level
input/output in GraphInput/GraphOutput.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/loco/include/loco/IR/Graph.h
contrib/loco/src/IR/Graph.test.cpp

index 2f06321..7e77e0b 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __LOCO_IR_GRAPH_H__
 #define __LOCO_IR_GRAPH_H__
 
+#include "loco/IR/DataType.h"
 #include "loco/IR/Nodes.h"
 
 #include "loco/ADT/ObjectPool.h"
 namespace loco
 {
 
+// TODO Introduce Named trait
+enum class Trait
+{
+  // Any "DataTyped" class has the following methods
+  // - DataType dtype(void) const;
+  // - void dtype(const DataType &value);
+  DataTyped,
+};
+
+template <Trait T> class Mixin;
+
+// TODO Re-implement NodeMixin<NodeTrait::DataType> using this mixin
+template <> class Mixin<Trait::DataTyped>
+{
+public:
+  Mixin() = default;
+
+public:
+  const DataType &dtype(void) const { return _dtype; }
+  void dtype(const DataType &value) { _dtype = value; }
+
+private:
+  DataType _dtype = DataType::Unknown;
+};
+
 /**
  * @brief Trait for elements with name
  */
@@ -49,7 +75,7 @@ private:
 /**
  * @brief Graph-level Input Metadata
  */
-class GraphInput final : private NamedEntity
+class GraphInput final : private NamedEntity, public Mixin<Trait::DataTyped>
 {
 public:
   LOCO_NAMED_ENTITY_EXPOSE;
@@ -72,7 +98,7 @@ private:
 /**
  * @brief Graph-level Output Metadata
  */
-class GraphOutput final : private NamedEntity
+class GraphOutput final : private NamedEntity, public Mixin<Trait::DataTyped>
 {
 public:
   LOCO_NAMED_ENTITY_EXPOSE;
index ea683ee..0a68c04 100644 (file)
@@ -44,6 +44,21 @@ TEST(NamedTest, setter_and_getter)
   ASSERT_EQ(elem.name(), "name");
 }
 
+TEST(DataTypedMixinTest, constructor)
+{
+  loco::Mixin<loco::Trait::DataTyped> mixin;
+
+  ASSERT_EQ(mixin.dtype(), loco::DataType::Unknown);
+}
+
+TEST(DataTypedMixinTest, setter_and_getter)
+{
+  loco::Mixin<loco::Trait::DataTyped> mixin;
+
+  mixin.dtype(loco::DataType::FLOAT32);
+  ASSERT_EQ(mixin.dtype(), loco::DataType::FLOAT32);
+}
+
 TEST(GraphTest, create_and_destroy_node)
 {
   auto g = loco::make_graph();