Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions backends/vulkan/runtime/api/Adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <executorch/backends/vulkan/runtime/api/Shader.h>
#include <executorch/backends/vulkan/runtime/api/Utils.h>

#include <executorch/backends/vulkan/runtime/api/memory/Allocator.h>

#include <array>
#include <mutex>
#include <ostream>
Expand Down Expand Up @@ -136,7 +138,7 @@ class Adapter final {
ComputePipelineCache compute_pipeline_cache_;
// Memory Management
SamplerCache sampler_cache_;
MemoryAllocator vma_;
Allocator vma_;

public:
// Physical Device metadata
Expand Down Expand Up @@ -194,7 +196,7 @@ class Adapter final {
return sampler_cache_;
}

inline MemoryAllocator& vma() {
inline Allocator& vma() {
return vma_;
}

Expand Down
4 changes: 3 additions & 1 deletion backends/vulkan/runtime/api/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

#include <executorch/backends/vulkan/runtime/api/Descriptor.h>
#include <executorch/backends/vulkan/runtime/api/Pipeline.h>
#include <executorch/backends/vulkan/runtime/api/Resource.h>
#include <executorch/backends/vulkan/runtime/api/Shader.h>
#include <executorch/backends/vulkan/runtime/api/Utils.h>

#include <executorch/backends/vulkan/runtime/api/memory/Buffer.h>
#include <executorch/backends/vulkan/runtime/api/memory/Image.h>

namespace vkcompute {
namespace api {

Expand Down
4 changes: 3 additions & 1 deletion backends/vulkan/runtime/api/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include <executorch/backends/vulkan/runtime/api/Adapter.h>
#include <executorch/backends/vulkan/runtime/api/Command.h>
#include <executorch/backends/vulkan/runtime/api/Descriptor.h>
#include <executorch/backends/vulkan/runtime/api/Fence.h>
#include <executorch/backends/vulkan/runtime/api/Pipeline.h>
#include <executorch/backends/vulkan/runtime/api/QueryPool.h>
#include <executorch/backends/vulkan/runtime/api/Resource.h>
#include <executorch/backends/vulkan/runtime/api/Runtime.h>
#include <executorch/backends/vulkan/runtime/api/Shader.h>
#include <executorch/backends/vulkan/runtime/api/Utils.h>

#include <executorch/backends/vulkan/runtime/api/memory/Buffer.h>

namespace vkcompute {
namespace api {

Expand Down
4 changes: 3 additions & 1 deletion backends/vulkan/runtime/api/Descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

#include <executorch/backends/vulkan/runtime/api/vk_api.h>

#include <executorch/backends/vulkan/runtime/api/Resource.h>
#include <executorch/backends/vulkan/runtime/api/Shader.h>

#include <executorch/backends/vulkan/runtime/api/memory/Buffer.h>
#include <executorch/backends/vulkan/runtime/api/memory/Image.h>

#include <unordered_map>

namespace vkcompute {
Expand Down
7 changes: 4 additions & 3 deletions backends/vulkan/runtime/api/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
#pragma once
// @lint-ignore-every CLANGTIDY facebook-hte-BadMemberName

#include <executorch/backends/vulkan/runtime/api/vk_api.h>

#include <executorch/backends/vulkan/runtime/api/StringUtil.h>

#include <exception>
#include <ostream>
#include <string>
#include <vector>

#include <executorch/backends/vulkan/runtime/api/StringUtil.h>
#include <executorch/backends/vulkan/runtime/api/vk_api.h>

#define VK_CHECK(function) \
do { \
const VkResult result = (function); \
Expand Down
76 changes: 76 additions & 0 deletions backends/vulkan/runtime/api/Fence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <executorch/backends/vulkan/runtime/api/Fence.h>

namespace vkcompute {
namespace api {

VulkanFence::VulkanFence()
: device_(VK_NULL_HANDLE), handle_(VK_NULL_HANDLE), waiting_(false) {}

VulkanFence::VulkanFence(VkDevice device)
: device_(device), handle_(VK_NULL_HANDLE), waiting_(VK_NULL_HANDLE) {
const VkFenceCreateInfo fence_create_info{
VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, // sType
nullptr, // pNext
0u, // flags
};

VK_CHECK(vkCreateFence(device_, &fence_create_info, nullptr, &handle_));
}

VulkanFence::VulkanFence(VulkanFence&& other) noexcept
: device_(other.device_), handle_(other.handle_), waiting_(other.waiting_) {
other.handle_ = VK_NULL_HANDLE;
other.waiting_ = false;
}

VulkanFence& VulkanFence::operator=(VulkanFence&& other) noexcept {
device_ = other.device_;
handle_ = other.handle_;
waiting_ = other.waiting_;

other.device_ = VK_NULL_HANDLE;
other.handle_ = VK_NULL_HANDLE;
other.waiting_ = false;

return *this;
}

VulkanFence::~VulkanFence() {
if (VK_NULL_HANDLE == handle_) {
return;
}
vkDestroyFence(device_, handle_, nullptr);
}

void VulkanFence::wait() {
// if get_submit_handle() has not been called, then this will no-op
if (waiting_) {
VkResult fence_status = VK_NOT_READY;
// Run the wait in a loop to keep the CPU hot. A single call to
// vkWaitForFences with no timeout may cause the calling thread to be
// scheduled out.
do {
// The timeout (last) arg is in units of ns
fence_status = vkWaitForFences(device_, 1u, &handle_, VK_TRUE, 100000);

VK_CHECK_COND(
fence_status != VK_ERROR_DEVICE_LOST,
"Vulkan Fence: Device lost while waiting for fence!");
} while (fence_status != VK_SUCCESS);

VK_CHECK(vkResetFences(device_, 1u, &handle_));

waiting_ = false;
}
}

} // namespace api
} // namespace vkcompute
98 changes: 98 additions & 0 deletions backends/vulkan/runtime/api/Fence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

// @lint-ignore-every CLANGTIDY facebook-hte-BadMemberName

#include <executorch/backends/vulkan/runtime/api/vk_api.h>

#include <executorch/backends/vulkan/runtime/api/Exception.h>

#include <stack>

namespace vkcompute {
namespace api {

class VulkanFence final {
public:
// TODO: This is required for the lazy allocation pattern in api/Tensor.
// It will be disabled pending future refactors.
explicit VulkanFence();

explicit VulkanFence(VkDevice);

VulkanFence(const VulkanFence&) = delete;
VulkanFence& operator=(const VulkanFence&) = delete;

VulkanFence(VulkanFence&&) noexcept;
VulkanFence& operator=(VulkanFence&&) noexcept;

~VulkanFence();

private:
VkDevice device_;
VkFence handle_;
bool waiting_;

public:
// Used to get the handle for a queue submission.
VkFence get_submit_handle() {
if (handle_ != VK_NULL_HANDLE) {
// Indicate we are now waiting for this fence to be signaled
waiting_ = true;
}
return handle_;
}

VkFence handle() {
return handle_;
}

// Trigger a synchronous wait for the fence to be signaled
void wait();

bool waiting() const {
return waiting_;
}

operator bool() const {
return (VK_NULL_HANDLE != handle_);
}
};

// A pool to track created Fences and reuse ones that are available.
// Only intended to be modified by one thread at a time.
struct FencePool final {
VkDevice device_;

std::stack<VulkanFence> pool_;

explicit FencePool(VkDevice device) : device_(device), pool_{} {}

// Returns an rvalue reference to a fence, so that it can be moved
inline VulkanFence get_fence() {
if (pool_.empty()) {
VulkanFence new_fence = VulkanFence(device_);
return new_fence;
}

VulkanFence top_fence = std::move(pool_.top());
pool_.pop();

return top_fence;
}

// Marks the fence as available
inline void return_fence(VulkanFence& fence) {
pool_.push(std::move(fence));
}
};

} // namespace api
} // namespace vkcompute
4 changes: 3 additions & 1 deletion backends/vulkan/runtime/api/Pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

#include <executorch/backends/vulkan/runtime/api/vk_api.h>

#include <executorch/backends/vulkan/runtime/api/Resource.h>
#include <executorch/backends/vulkan/runtime/api/Shader.h>

#include <executorch/backends/vulkan/runtime/api/memory/Buffer.h>
#include <executorch/backends/vulkan/runtime/api/memory/Image.h>

#include <mutex>
#include <unordered_map>

Expand Down
Loading