Add ostream formatters for TargetPtr/TargetVal. (#5592)
authorAndrew Reusch <areusch@octoml.ai>
Fri, 15 May 2020 16:27:44 +0000 (09:27 -0700)
committerGitHub <noreply@github.com>
Fri, 15 May 2020 16:27:44 +0000 (09:27 -0700)
src/runtime/micro/host_driven/utvm_runtime.h
src/runtime/micro/host_driven/utvm_runtime_enum.h [new file with mode: 0644]
src/runtime/micro/micro_common.cc
src/runtime/micro/micro_common.h
src/runtime/micro/target_data_layout_encoder.h

index 1a4486c..8758c3a 100644 (file)
@@ -32,21 +32,7 @@ extern "C" {
 #include <tvm/runtime/c_backend_api.h>
 #include <tvm/runtime/c_runtime_api.h>
 
-/*!
- * \brief TODO
- */
-enum UTVMReturnCode {
-  UTVM_ERR_OK = 0,
-  UTVM_ERR_NOT_FINISHED = -1,
-  UTVM_ERR_TIMER_NOT_IMPLEMENTED = -2,
-  UTVM_ERR_TIMER_OVERFLOW = -3,
-  UTVM_ERR_WS_DOUBLE_FREE = -4,
-  UTVM_ERR_WS_OUT_OF_SPACE = -5,
-  UTVM_ERR_WS_TOO_MANY_ALLOCS = -6,
-  UTVM_ERR_WS_ZERO_SIZE_ALLOC = -7,
-  UTVM_ERR_WS_UNALIGNED_START = -8,
-  UTVM_ERR_WS_UNALIGNED_ALLOC_SIZE = -9,
-};
+#include "utvm_runtime_enum.h"
 
 /*!
  * \brief Task structure for uTVM
diff --git a/src/runtime/micro/host_driven/utvm_runtime_enum.h b/src/runtime/micro/host_driven/utvm_runtime_enum.h
new file mode 100644 (file)
index 0000000..17f8036
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 utvm_runtime_enum.h
+ * \brief Defines constants used both on the host and on device.
+ */
+#ifndef TVM_RUNTIME_MICRO_HOST_DRIVEN_UTVM_RUNTIME_ENUM_H_
+#define TVM_RUNTIME_MICRO_HOST_DRIVEN_UTVM_RUNTIME_ENUM_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*!
+ * \brief TODO
+ */
+enum UTVMReturnCode {
+  UTVM_ERR_OK = 0,
+  UTVM_ERR_NOT_FINISHED = -1,
+  UTVM_ERR_TIMER_NOT_IMPLEMENTED = -2,
+  UTVM_ERR_TIMER_OVERFLOW = -3,
+  UTVM_ERR_WS_DOUBLE_FREE = -4,
+  UTVM_ERR_WS_OUT_OF_SPACE = -5,
+  UTVM_ERR_WS_TOO_MANY_ALLOCS = -6,
+  UTVM_ERR_WS_ZERO_SIZE_ALLOC = -7,
+  UTVM_ERR_WS_UNALIGNED_START = -8,
+  UTVM_ERR_WS_UNALIGNED_ALLOC_SIZE = -9,
+};
+
+#ifdef __cplusplus
+}  // TVM_EXTERN_C
+#endif
+
+#endif  // TVM_RUNTIME_MICRO_HOST_DRIVEN_UTVM_RUNTIME_ENUM_H_
index 020df62..eba77f3 100644 (file)
@@ -99,5 +99,33 @@ size_t GetSectionSize(const std::string& binary_path, SectionKind section,
   return UpperAlignValue(size, word_size.bytes());
 }
 
+std::ostream& operator<<(std::ostream& os, const TargetVal& v) {
+  std::ios_base::fmtflags f(os.flags());
+  os << std::dec << "0x";
+  switch (v.width_bits()) {
+    case 8:
+      os << uint8_t(v.uint32());
+      break;
+    case 16:
+      os << uint16_t(v.uint32());
+      break;
+    case 32:
+      os << v.uint32();
+      break;
+    case 64:
+      os << v.uint64();
+      break;
+    default:
+      os << (v.uint64() & ((1 << v.width_bits()) - 1));
+  }
+  os.flags(f);
+  return os;
+}
+
+std::ostream& operator<<(std::ostream& os, const TargetPtr& v) {
+  os << "*" << v.value_;
+  return os;
+}
+
 }  // namespace runtime
 }  // namespace tvm
index 4375791..2c4684b 100644 (file)
@@ -132,6 +132,9 @@ class TargetVal {
     value_ = other.value_ & Bitmask();
     return *this;
   }
+
+ private:
+  friend std::ostream& operator<<(std::ostream& os, const TargetVal& v);
 };
 
 // TODO(weberlo, areusch): just get rid of `TargetPtr`.
@@ -201,6 +204,8 @@ class TargetPtr {
  private:
   /*! \brief raw value storing the pointer */
   TargetVal value_;
+
+  friend std::ostream& operator<<(std::ostream& os, const TargetPtr& v);
 };
 
 /*!
index 9778177..0744dd1 100644 (file)
@@ -26,7 +26,7 @@
 
 #include <vector>
 
-#include "host_driven/utvm_runtime.h"
+#include "host_driven/utvm_runtime_enum.h"
 
 namespace tvm {
 namespace runtime {