[enco] Introduce enco_dump_op debugging helper (#2152)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 8 Nov 2018 01:19:34 +0000 (10:19 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 8 Nov 2018 01:19:34 +0000 (10:19 +0900)
This commit introduces enco_dump_op helper function to allows users
to inspect the details of coco::Op value from a debugger such as GDB.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/CMakeLists.txt
contrib/enco/core/src/Support/Debugging.cpp [new file with mode: 0644]
contrib/enco/core/src/Support/Debugging.h [new file with mode: 0644]
contrib/enco/core/src/Support/Debugging.test.cpp [new file with mode: 0644]

index 38debe7..0c2fcd7 100644 (file)
@@ -2,8 +2,13 @@ file(GLOB_RECURSE SOURCES "src/*.cpp")
 file(GLOB_RECURSE TESTS "src/*.test.cpp")
 list(REMOVE_ITEM SOURCES ${TESTS})
 
-add_library(enco_core STATIC ${SOURCES})
-set_target_properties(enco_core PROPERTIES POSITION_INDEPENDENT_CODE ON)
+###
+### enco_core is built as a shared library to support "interactive debugging".
+###
+### interactive debugging helpers are stripped during linking when enco_core is
+### built as a static library
+###
+add_library(enco_core SHARED ${SOURCES})
 target_include_directories(enco_core PRIVATE src)
 target_include_directories(enco_core PUBLIC include)
 target_link_libraries(enco_core enco_intf_cmdline)
diff --git a/contrib/enco/core/src/Support/Debugging.cpp b/contrib/enco/core/src/Support/Debugging.cpp
new file mode 100644 (file)
index 0000000..7faad96
--- /dev/null
@@ -0,0 +1,63 @@
+#include "Debugging.h"
+
+#include <pp/LinearDocument.h>
+#include <pp/MultiLineTextUtils.h>
+
+#include <iostream>
+
+#define DEBUGGING_API_P(NAME, TYPE, VAR)                         \
+  void NAME(const TYPE *);                                       \
+  void NAME(long p) { NAME(reinterpret_cast<const TYPE *>(p)); } \
+  void NAME(const TYPE *VAR)
+
+/**
+ * SECTION: Op
+ */
+namespace
+{
+
+std::string op_kind(const coco::Op *op)
+{
+  struct OpKind : public coco::Op::Visitor<std::string>
+  {
+    std::string visit(const coco::Load *) override { return "Load"; }
+    std::string visit(const coco::Conv2D *) override { return "Conv2D"; }
+    std::string visit(const coco::MaxPool2D *) override { return "MaxPool2D"; }
+    std::string visit(const coco::AvgPool2D *) override { return "AvgPool2D"; }
+    std::string visit(const coco::PadF *) override { return "PadF"; }
+    std::string visit(const coco::ReLU *) override { return "ReLU"; }
+    std::string visit(const coco::Add *) override { return "Add"; }
+    std::string visit(const coco::Mul *) override { return "Mul"; }
+    std::string visit(const coco::ConcatF *) override { return "ConcatF"; }
+  };
+
+  OpKind v;
+
+  return op->accept(v);
+}
+
+pp::LinearDocument describe(const coco::Op *op)
+{
+  pp::LinearDocument doc;
+
+  doc.append("addr: ", op);
+  doc.append("kind: ", op_kind(op));
+  doc.append("parent(instr): ", op->parent());
+  doc.append("up(op): ", op->up());
+
+  return doc;
+}
+
+} // namespace
+
+DEBUGGING_API_P(enco_dump_op, coco::Op, op)
+{
+  if (op != nullptr)
+  {
+    std::cout << describe(op) << std::endl;
+  }
+  else
+  {
+    std::cout << "(nullptr)" << std::endl;
+  }
+}
diff --git a/contrib/enco/core/src/Support/Debugging.h b/contrib/enco/core/src/Support/Debugging.h
new file mode 100644 (file)
index 0000000..98c126d
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+
+/**
+ * @file  Debugging.h
+ * @brief This file includes various interactive debugging helpers
+ */
+
+#ifndef __ENCO_SUPPORT_DEBUGGING_H__
+#define __ENCO_SUPPORT_DEBUGGING_H__
+
+#include <coco/IR.h>
+
+static_assert(sizeof(long) == sizeof(void *), "sizeof(long) == sizeof(pointer)");
+
+/**
+ * Debugging API with a single pointer argument
+ */
+#define DEBUGGING_API_P(NAME, TYPE) \
+  void NAME(const TYPE *);          \
+  void NAME(long);
+
+/**
+ * Print the details of coco::Op
+ *
+ * (gdb) call enco_dump_op(op)
+ * (gdb) call enco_dump_op(0x....)
+ */
+DEBUGGING_API_P(enco_dump_op, coco::Op);
+
+#undef DEBUGGING_API_P
+
+#endif // __ENCO_SUPPORT_DEBUGGING_H__
diff --git a/contrib/enco/core/src/Support/Debugging.test.cpp b/contrib/enco/core/src/Support/Debugging.test.cpp
new file mode 100644 (file)
index 0000000..eb20b32
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * 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 "Debugging.h"
+
+#include <gtest/gtest.h>
+
+// This test aims to check whether debugging API is actually defined
+TEST(DebuggingTest, defined) { enco_dump_op(nullptr); }