Add getter for opcode name (#6658)
authorAlexander Efimov/AI Tools Lab/./Samsung Electronics <a.efimov@samsung.com>
Tue, 20 Aug 2019 10:41:14 +0000 (13:41 +0300)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 20 Aug 2019 10:41:14 +0000 (13:41 +0300)
Added getter of opcode name in string format

Signed-off-by: Efimov Alexander <a.efimov@samsung.com>
compiler/mir/include/mir/Operation.h
compiler/mir/src/Operation.cpp
compiler/mir/unittests/Operation.cpp

index b70f36b..415cb56 100644 (file)
@@ -186,6 +186,11 @@ private:
   std::deque<Output> _outputs;
 };
 
+/**
+ * @return the opcode of operation in string format, like "Add", "Conv2d", etc.
+ */
+const std::string &getTypeName(Operation::Type type);
+
 } // namespace mir
 
 #endif //_MIR_OPERATION_H_
index 135d81c..cf613b3 100644 (file)
@@ -48,4 +48,20 @@ void Operation::accept(IVisitor *v)
   }
 }
 
+const std::string &getTypeName(Operation::Type type)
+{
+  switch (type)
+  {
+#define HANDLE_OP(OpType, OpClass)          \
+  case Operation::Type::OpType:             \
+  {                                         \
+    static const std::string name(#OpType); \
+    return name;                            \
+  }
+#include "mir/Operations.inc"
+#undef HANDLE_OP
+  }
+  throw std::runtime_error("unexpected opcode");
+}
+
 } // namespace mir
index cc6ed39..e8b769e 100644 (file)
@@ -80,3 +80,10 @@ TEST(Operation, ConcatAxisTest)
   ops::ConcatOp op_n3({input1.getOutput(0), input2.getOutput(0)}, -3);
   ASSERT_EQ(op_n3.getAxis(), 0);
 }
+
+TEST(Operation, OpNameTest)
+{
+#define HANDLE_OP(OpType, OpClass) ASSERT_EQ(getTypeName(Operation::Type::OpType), #OpType);
+#include "mir/Operations.inc"
+#undef HANDLE_OP
+}