[exo-tflite] Basic Classes for TFLDialect (#6682)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Tue, 20 Aug 2019 04:04:37 +0000 (13:04 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Tue, 20 Aug 2019 04:04:37 +0000 (13:04 +0900)
These commit adds basic classes for TFLDialect.

Namespace is 'locoex' since this is an extension of `loco`.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
12 files changed:
compiler/exo-tflite/src/Dialect/IR/TFLDialect.cpp [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLDialect.h [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLDialect.test.cpp [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNode.cpp [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNode.h [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNodeDecl.h [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNodeImpl.h [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNodeVisitor.forward.h [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNodeVisitor.h [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNodes.h [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst [new file with mode: 0644]
compiler/exo-tflite/src/Dialect/IR/TFLOpcode.h [new file with mode: 0644]

diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLDialect.cpp b/compiler/exo-tflite/src/Dialect/IR/TFLDialect.cpp
new file mode 100644 (file)
index 0000000..8cbf9a3
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * 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 "TFLDialect.h"
+
+namespace locoex
+{
+
+loco::Dialect *TFLDialect::get(void)
+{
+  static TFLDialect d;
+  return &d;
+}
+
+} // namespace locoex
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLDialect.h b/compiler/exo-tflite/src/Dialect/IR/TFLDialect.h
new file mode 100644 (file)
index 0000000..96463a9
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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 __LOCOEX_IR_TFLDIALECT_H__
+#define __LOCOEX_IR_TFLDIALECT_H__
+
+#include <loco/IR/Dialect.h>
+
+namespace locoex
+{
+
+class TFLDialect final : public loco::Dialect
+{
+private:
+  TFLDialect() = default;
+
+public:
+  TFLDialect(const TFLDialect &) = delete;
+  TFLDialect(TFLDialect &&) = delete;
+
+public:
+  static loco::Dialect *get(void);
+};
+
+} // namespace locoex
+
+#endif // __LOCOEX_IR_TFLDIALECT_H__
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLDialect.test.cpp b/compiler/exo-tflite/src/Dialect/IR/TFLDialect.test.cpp
new file mode 100644 (file)
index 0000000..136721e
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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 "TFLDialect.h"
+
+#include <gtest/gtest.h>
+
+TEST(TFLDialectTest, get)
+{
+  using locoex::TFLDialect;
+
+  auto d = TFLDialect::get();
+
+  // get() SHOULD return a valid(non-null) pointer
+  ASSERT_NE(d, nullptr);
+  // The return value SHOULD be stable across multiple invocations
+  ASSERT_EQ(d, TFLDialect::get());
+}
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNode.cpp b/compiler/exo-tflite/src/Dialect/IR/TFLNode.cpp
new file mode 100644 (file)
index 0000000..82d5f1e
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ * 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 "TFLNode.h"
+
+#include "TFLDialect.h"
+
+namespace locoex
+{
+
+const loco::Dialect *TFLNode::dialect(void) const { return TFLDialect::get(); }
+
+} // namespace locoex
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNode.h b/compiler/exo-tflite/src/Dialect/IR/TFLNode.h
new file mode 100644 (file)
index 0000000..eff69b1
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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 __LOCOEX_IR_TFLNODE_H__
+#define __LOCOEX_IR_TFLNODE_H__
+
+#include "TFLNodeDecl.h"
+#include "TFLNodeImpl.h"
+
+#endif // __LOCOEX_IR_TFLNODE_H__
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodeDecl.h b/compiler/exo-tflite/src/Dialect/IR/TFLNodeDecl.h
new file mode 100644 (file)
index 0000000..d13900a
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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 __LOCOEX_IR_TFLNODEDECL_H__
+#define __LOCOEX_IR_TFLNODEDECL_H__
+
+#include <loco/IR/Node.h>
+#include <loco/IR/Dialect.h>
+
+#include "TFLOpcode.h"
+#include "TFLNodeVisitor.forward.h"
+
+namespace locoex
+{
+
+struct TFLNode : public loco::Node
+{
+  virtual ~TFLNode() = default;
+
+  const loco::Dialect *dialect(void) const final;
+  virtual TFLOpcode opcode(void) const = 0;
+
+  template <typename T> T accept(TFLNodeVisitorBase<T> *) const;
+  template <typename T> T accept(TFLNodeMutableVisitorBase<T> *);
+};
+
+template <TFLOpcode Code> struct TFLNodeImpl : public TFLNode
+{
+  virtual ~TFLNodeImpl() = default;
+
+  uint32_t opnum(void) const final { return static_cast<uint32_t>(Code); }
+  TFLOpcode opcode(void) const final { return Code; }
+};
+
+} // namespace locoex
+
+#endif // __LOCOEX_IR_TFLNODEDECL_H__
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodeImpl.h b/compiler/exo-tflite/src/Dialect/IR/TFLNodeImpl.h
new file mode 100644 (file)
index 0000000..849a342
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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 __LOCOEX_IR_TFLNODEIMPL_H__
+#define __LOCOEX_IR_TFLNODEIMPL_H__
+
+#include "TFLNode.h"
+#include "TFLNodes.h"
+#include "TFLNodeVisitor.h"
+
+#include <cassert>
+
+namespace locoex
+{
+
+template <typename T> T TFLNode::accept(TFLNodeVisitorBase<T> *v) const
+{
+  switch (this->opcode())
+  {
+#define TFL_NODE(OPCODE, CLASS) \
+                                \
+  case TFLOpcode::OPCODE:       \
+    return v->visit(dynamic_cast<const CLASS *>(this));
+
+#include "TFLNodes.lst"
+#undef TFL_NODE
+
+    default:
+      break;
+  }
+
+  assert(false);
+}
+
+template <typename T> T TFLNode::accept(TFLNodeMutableVisitorBase<T> *v)
+{
+  switch (this->opcode())
+  {
+#define TFL_NODE(OPCODE, CLASS) \
+                                \
+  case TFLOpcode::OPCODE:       \
+    return v->visit(dynamic_cast<CLASS *>(this));
+
+#include "TFLNodes.lst"
+#undef TFL_NODE
+
+    default:
+      break;
+  }
+
+  assert(false);
+}
+
+} // namespace locoex
+
+#endif // __LOCOEX_IR_TFLNODEIMPL_H__
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodeVisitor.forward.h b/compiler/exo-tflite/src/Dialect/IR/TFLNodeVisitor.forward.h
new file mode 100644 (file)
index 0000000..e98057b
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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 __LOCOEX_IR_TFLNODE_VISITOR_FORWARD_H__
+#define __LOCOEX_IR_TFLNODE_VISITOR_FORWARD_H__
+
+namespace locoex
+{
+
+// NOTE These forward declarations SHOULD BE aligned with Node delcarations in
+// "TFLNodeVisitor.h"
+template <typename T> struct TFLNodeVisitorBase;
+template <typename T> struct TFLNodeMutableVisitorBase;
+
+} // namespace locoex
+
+#endif // __LOCOEX_IR_TFLNODE_VISITOR_FORWARD_H__
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodeVisitor.h b/compiler/exo-tflite/src/Dialect/IR/TFLNodeVisitor.h
new file mode 100644 (file)
index 0000000..93cb85f
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * 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 __LOCOEX_IR_TFLNODE_VISITOR_H__
+#define __LOCOEX_IR_TFLNODE_VISITOR_H__
+
+#include "TFLNode.h"
+#include "TFLNodes.h"
+
+#include <cassert>
+
+namespace locoex
+{
+
+/**
+ * DO NOT use this class. Use TFLNodeVisitor instead.
+ */
+template <typename T> struct TFLNodeVisitorBase
+{
+  virtual ~TFLNodeVisitorBase() = default;
+
+#define TFL_NODE(OPCODE, TFL_CLASS) virtual T visit(const TFL_CLASS *) = 0;
+
+#include "TFLNodes.lst"
+#undef TFL_NODE
+};
+
+template <typename T> struct TFLNodeVisitor : public TFLNodeVisitorBase<T>
+{
+  virtual ~TFLNodeVisitor() = default;
+
+#define TFL_NODE(OPCODE, TFL_CLASS) \
+                                    \
+  virtual T visit(const TFL_CLASS *node) { return visit(static_cast<const TFLNode *>(node)); }
+
+#include "TFLNodes.lst"
+#undef TFL_NODE
+
+  /// @brief Default fallback
+  virtual T visit(const TFLNode *node) { assert(false); }
+};
+
+/**
+ * DO NOT use this class. Use TFLNodeMutableVisitor instead.
+ */
+template <typename T> struct TFLNodeMutableVisitorBase
+{
+  virtual ~TFLNodeMutableVisitorBase() = default;
+
+#define TFL_NODE(OPCODE, TFL_CLASS) virtual T visit(TFL_CLASS *) = 0;
+
+#include "TFLNodes.lst"
+#undef TFL_NODE
+};
+
+template <typename T> struct TFLNodeMutableVisitor : public TFLNodeMutableVisitorBase<T>
+{
+  virtual ~TFLNodeMutableVisitor() = default;
+
+#define TFL_NODE(OPCODE, TFL_CLASS) \
+                                    \
+  virtual T visit(TFL_CLASS *node) { return visit(static_cast<TFLNode *>(node)); }
+
+#include "TFLNodes.lst"
+#undef TFL_NODE
+
+  /// @brief Default fallback
+  virtual T visit(TFLNode *node) { assert(false); }
+};
+
+} // namespace locoex
+
+#endif // __LOCOEX_IR_TFLNODE_VISITOR_H__
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.h b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.h
new file mode 100644 (file)
index 0000000..5f7910f
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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 __LOCOEX_IR_TFLNODES_H__
+#define __LOCOEX_IR_TFLNODES_H__
+
+#include "TFLNodeDecl.h"
+#include "TFLOpcode.h"
+
+#include <loco/IR/Node.h>
+#include <loco/IR/NodeMixins.h>
+
+namespace locoex
+{
+
+// TODO define TFLNodes here
+
+} // namespace locoex
+
+#endif // __LOCOEX_IR_TFLNODES_H__
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst
new file mode 100644 (file)
index 0000000..0a45cfd
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef TFL_NODE
+#error "Define TFL_NODE"
+#endif // TFL_NODE
+
+//
+// PLEASE SORT NODE DECLS IN ALPHABETICAL ORDER
+//
+// Example)
+// TFL_NODE(Relu, locoex::TFLRelu)
diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLOpcode.h b/compiler/exo-tflite/src/Dialect/IR/TFLOpcode.h
new file mode 100644 (file)
index 0000000..0c0ab64
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * 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 __LOCOEX_IR_TFLOPCODE_H__
+#define __LOCOEX_IR_TFLOPCODE_H__
+
+namespace locoex
+{
+
+enum class TFLOpcode
+{
+#define TFL_NODE(OPCODE, CLASS) OPCODE,
+#include "TFLNodes.lst"
+#undef TFL_NODE
+};
+
+} // namespace locoex
+
+#endif // __LOCOEX_IR_TFLOPCODE_H__