csullivan commented on a change in pull request #9525:
URL: https://github.com/apache/tvm/pull/9525#discussion_r766246562
##########
File path: src/runtime/hexagon/hexagon/hexagon_buffer.cc
##########
@@ -30,67 +34,135 @@ namespace tvm {
namespace runtime {
namespace hexagon {
-static size_t GetDataAlignment(const DLDataType dtype) {
- size_t align = (dtype.bits / 8) * dtype.lanes;
- if (align < kAllocAlignment) return kAllocAlignment;
- return align;
-}
+// TODO: Alignment not used
+struct Allocation {
+ Allocation(size_t nbytes, size_t alignment) : nbytes_(nbytes),
alignment_(alignment) {}
+ virtual ~Allocation() {}
+ Allocation(const Allocation&) = delete;
+ Allocation& operator=(const Allocation&) = delete;
+ Allocation(Allocation&&) = delete;
+ Allocation& operator=(Allocation&&) = delete;
-HexagonBuffer::HexagonBuffer(int ndim, const int64_t* shape, DLDataType dtype,
- Optional<String> scope) {
- ICHECK_LE(ndim, 1) << "Hexagon currently only supports flat allocations "
- << "and arrays of flat allocations.";
+ void* data_{nullptr};
+ size_t nbytes_;
+ size_t alignment_;
+};
- size_t alignment = GetDataAlignment(dtype);
- // TODO(csullivan): Extend to support arrays of allocations.
- // Move assignment from r-value constructed flat allocation.
- *this = HexagonBuffer(shape[0] * (dtype.bits / 8) * dtype.lanes, alignment,
scope);
-}
+struct DDRAllocation : public Allocation {
+ DDRAllocation(size_t nbytes, size_t alignment) : Allocation(nbytes,
alignment) {
+ data_ = malloc(nbytes);
+ }
+ ~DDRAllocation() {
+ free(data_);
+ }
+};
-HexagonBuffer::HexagonBuffer(size_t nbytes, size_t alignment, Optional<String>
scope) {
- void* ptr = nullptr;
- int ret = posix_memalign(&ptr, alignment, nbytes);
- if (ret != 0) {
- throw std::bad_alloc();
+struct VTCMAllocation : public Allocation {
+ VTCMAllocation(size_t nbytes, size_t alignment) : Allocation(nbytes,
alignment) {
+ #ifdef BUILD_FOR_HEXAGON
+ compute_res_attr_t res_info;
+ HEXAGON_SAFE_CALL(HAP_compute_res_attr_init(&res_info));
+ // TODO: Magic number 1
+ HEXAGON_SAFE_CALL(HAP_compute_res_attr_set_vtcm_param(&res_info, nbytes,
1));
+ // TODO: HEXAGON_SAFE_CALL?
+ // TODO: Magic number 10000
+ context_id_ = HAP_compute_res_acquire(&res_info, 10000);
+ if (context_id_) {
+ // TODO: HEXAGON_SAFE_CALL?
+ data_ = HAP_compute_res_attr_get_vtcm_ptr(&res_info);
+ if (!data_) {
+ HEXAGON_PRINT(ERROR, "ERROR: Allocated VTCM ptr is null.");
+ HEXAGON_SAFE_CALL(HAP_compute_res_release(context_id_));
+ return;
+ }
+ } else {
+ HEXAGON_PRINT(ERROR, "ERROR: Unable to acquire requeisted resource.");
+ return;
+ }
+ // HEXAGON_PRINT(ALWAYS, "VTCMAllocation() - Context ID: %u, VTCM ptr:
%p", context_id_, data_);
+ #else
+ data_ = malloc(nbytes);
+ #endif
}
- allocations_.push_back(ptr);
- SetStorageScope(scope);
+ ~VTCMAllocation() {
+ #ifdef BUILD_FOR_HEXAGON
+ // HEXAGON_PRINT(ALWAYS, "~VTCMAllocation() - Context ID: %u, VTCM ptr:
%p", context_id_, data_);
+ // TODO: Need to handle the else case(s) here
+ if (context_id_ && data_) {
+ HEXAGON_SAFE_CALL(HAP_compute_res_release(context_id_));
+ data_ = nullptr;
+ }
+ #else
+ free(data_);
+ #endif
+ }
+ #ifdef BUILD_FOR_HEXAGON
+ unsigned int context_id_{0};
+ #endif
+};
+
+template <HexagonBuffer::StorageScope S>
+std::unique_ptr<Allocation> Allocator(size_t nbytes, size_t alignment);
+
+template <>
+std::unique_ptr<Allocation>
Allocator<HexagonBuffer::StorageScope::kDDR>(size_t nbytes,
+
size_t alignment) {
+ return std::make_unique<DDRAllocation>(nbytes, alignment);;
}
-HexagonBuffer::HexagonBuffer(void* data, Optional<String> scope) :
managed_{false} {
+template <>
+std::unique_ptr<Allocation>
Allocator<HexagonBuffer::StorageScope::kVTCM>(size_t nbytes,
+
size_t alignment) {
+ return std::make_unique<VTCMAllocation>(nbytes, alignment);
+}
+
+HexagonBuffer::HexagonBuffer(size_t nbytes, size_t alignment, Optional<String>
scope) : ndim_(1), nbytes_(nbytes) {
SetStorageScope(scope);
- allocations_.push_back(data);
+
+ std::unique_ptr<Allocation> alloca = nullptr;
+ if (GetStorageScope() == StorageScope::kDDR) {
+ alloca = Allocator<StorageScope::kDDR>(nbytes, alignment);
+ }
+ else if (GetStorageScope() == StorageScope::kVTCM) {
+ alloca = Allocator<StorageScope::kVTCM>(nbytes, alignment);
+ }
+ CHECK(alloca != nullptr);
+ allocations_.push_back(alloca->data_);
+ managed_allocations_.push_back(std::move(alloca));
}
-HexagonBuffer::~HexagonBuffer() {
- if (managed_) {
- for (auto& ptr : allocations_) {
- free(ptr);
+HexagonBuffer::HexagonBuffer(size_t ndim, size_t nbytes, size_t alignment,
Optional<String> scope) : ndim_(ndim), nbytes_(ndim * nbytes) {
+ SetStorageScope(scope);
+ for (size_t i = 0; i < ndim; ++i) {
+ std::unique_ptr<Allocation> alloca = nullptr;
+ if (GetStorageScope() == StorageScope::kDDR) {
+ alloca = Allocator<StorageScope::kDDR>(nbytes, alignment);
+ }
+ else if (GetStorageScope() == StorageScope::kVTCM) {
+ alloca = Allocator<StorageScope::kVTCM>(nbytes, alignment);
}
+ CHECK(alloca != nullptr);
+ allocations_.push_back(alloca->data_);
+ managed_allocations_.push_back(std::move(alloca));
}
}
-HexagonBuffer::HexagonBuffer(HexagonBuffer&& other)
- : allocations_(other.allocations_),
- managed_(other.managed_),
- storage_scope_(other.storage_scope_) {
- other.allocations_.clear();
- other.managed_ = false;
- other.storage_scope_ = StorageScope::kDDR;
+// TODO: add ExternalAllocation class?
+HexagonBuffer::HexagonBuffer(void* data, size_t nbytes, Optional<String>
scope) : ndim_(1), nbytes_(nbytes) {
+ // TODO: disallow VTCM scope?
+ SetStorageScope(scope);
+ allocations_.push_back(data);
}
-HexagonBuffer& HexagonBuffer::operator=(HexagonBuffer&& other) {
- std::swap(allocations_, other.allocations_);
- std::swap(managed_, other.managed_);
- std::swap(storage_scope_, other.storage_scope_);
- return *this;
+HexagonBuffer::~HexagonBuffer() {
+ managed_allocations_.clear();
}
-void* HexagonBuffer::GetPointer() {
+void** HexagonBuffer::GetPointer() {
Review comment:
Did we decide to revert this change so that tests pass and revisit once
codegen supports pointer array indexing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]