Skip to content

Commit

Permalink
Add ostream formatters for TargetPtr/TargetVal. (apache#5592)
Browse files Browse the repository at this point in the history
  • Loading branch information
areusch authored and Trevor Morris committed Jun 9, 2020
1 parent 7c4270b commit d61732f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
16 changes: 1 addition & 15 deletions src/runtime/micro/host_driven/utvm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions src/runtime/micro/host_driven/utvm_runtime_enum.h
Original file line number Diff line number Diff line change
@@ -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_
28 changes: 28 additions & 0 deletions src/runtime/micro/micro_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/runtime/micro/micro_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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);
};

/*!
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/micro/target_data_layout_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <vector>

#include "host_driven/utvm_runtime.h"
#include "host_driven/utvm_runtime_enum.h"

namespace tvm {
namespace runtime {
Expand Down

0 comments on commit d61732f

Please sign in to comment.