2020-02-19 22:11:22 +08:00
|
|
|
//===- VulkanRuntime.cpp - MLIR Vulkan runtime ------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares Vulkan runtime API.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef VULKAN_RUNTIME_H
|
|
|
|
#define VULKAN_RUNTIME_H
|
|
|
|
|
|
|
|
#include "mlir/Support/LogicalResult.h"
|
|
|
|
|
2020-05-09 05:06:03 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2020-03-17 19:49:00 +08:00
|
|
|
#include <vulkan/vulkan.h>
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
using DescriptorSetIndex = uint32_t;
|
|
|
|
using BindingIndex = uint32_t;
|
|
|
|
|
|
|
|
/// Struct containing information regarding to a device memory buffer.
|
|
|
|
struct VulkanDeviceMemoryBuffer {
|
|
|
|
BindingIndex bindingIndex{0};
|
|
|
|
VkDescriptorType descriptorType{VK_DESCRIPTOR_TYPE_MAX_ENUM};
|
|
|
|
VkDescriptorBufferInfo bufferInfo{};
|
2020-06-26 22:59:17 +08:00
|
|
|
VkBuffer hostBuffer{VK_NULL_HANDLE};
|
|
|
|
VkDeviceMemory hostMemory{VK_NULL_HANDLE};
|
|
|
|
VkBuffer deviceBuffer{VK_NULL_HANDLE};
|
2020-02-19 22:11:22 +08:00
|
|
|
VkDeviceMemory deviceMemory{VK_NULL_HANDLE};
|
2020-06-26 22:59:17 +08:00
|
|
|
uint32_t bufferSize{0};
|
2020-02-19 22:11:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Struct containing information regarding to a host memory buffer.
|
|
|
|
struct VulkanHostMemoryBuffer {
|
|
|
|
/// Pointer to a host memory.
|
|
|
|
void *ptr{nullptr};
|
|
|
|
/// Size of a host memory in bytes.
|
|
|
|
uint32_t size{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Struct containing the number of local workgroups to dispatch for each
|
|
|
|
/// dimension.
|
|
|
|
struct NumWorkGroups {
|
|
|
|
uint32_t x{1};
|
|
|
|
uint32_t y{1};
|
|
|
|
uint32_t z{1};
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Struct containing information regarding a descriptor set.
|
|
|
|
struct DescriptorSetInfo {
|
|
|
|
/// Index of a descriptor set in descriptor sets.
|
|
|
|
DescriptorSetIndex descriptorSet{0};
|
[mlir] NFC: fix trivial typo in source files
Summary: fix trivial typos in the source files
Reviewers: mravishankar, antiagainst, nicolasvasilache, herhut, rriddle, aartbik
Reviewed By: antiagainst, rriddle
Subscribers: mehdi_amini, rriddle, jpienaar, burmako, shauheen, antiagainst, nicolasvasilache, csigg, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, Joonsoo, bader, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D76876
2020-03-27 02:51:37 +08:00
|
|
|
/// Number of descriptors in a set.
|
2020-02-19 22:11:22 +08:00
|
|
|
uint32_t descriptorSize{0};
|
|
|
|
/// Type of a descriptor set.
|
|
|
|
VkDescriptorType descriptorType{VK_DESCRIPTOR_TYPE_MAX_ENUM};
|
|
|
|
};
|
|
|
|
|
|
|
|
/// VulkanHostMemoryBuffer mapped into a descriptor set and a binding.
|
2020-05-09 05:06:03 +08:00
|
|
|
using ResourceData = std::unordered_map<
|
|
|
|
DescriptorSetIndex,
|
|
|
|
std::unordered_map<BindingIndex, VulkanHostMemoryBuffer>>;
|
|
|
|
|
|
|
|
/// SPIR-V storage classes.
|
|
|
|
/// Note that this duplicates spirv::StorageClass but it keeps the Vulkan
|
|
|
|
/// runtime library detached from SPIR-V dialect, so we can avoid pick up lots
|
|
|
|
/// of dependencies.
|
|
|
|
enum class SPIRVStorageClass {
|
|
|
|
Uniform = 2,
|
|
|
|
StorageBuffer = 12,
|
|
|
|
};
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
/// StorageClass mapped into a descriptor set and a binding.
|
|
|
|
using ResourceStorageClassBindingMap =
|
2020-05-09 05:06:03 +08:00
|
|
|
std::unordered_map<DescriptorSetIndex,
|
|
|
|
std::unordered_map<BindingIndex, SPIRVStorageClass>>;
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
/// Vulkan runtime.
|
|
|
|
/// The purpose of this class is to run SPIR-V compute shader on Vulkan
|
|
|
|
/// device.
|
|
|
|
/// Before the run, user must provide and set resource data with descriptors,
|
|
|
|
/// SPIR-V shader, number of work groups and entry point. After the creation of
|
|
|
|
/// VulkanRuntime, special methods must be called in the following
|
|
|
|
/// sequence: initRuntime(), run(), updateHostMemoryBuffers(), destroy();
|
2020-04-05 10:30:01 +08:00
|
|
|
/// each method in the sequence returns success or failure depends on the Vulkan
|
2020-02-19 22:11:22 +08:00
|
|
|
/// result code.
|
|
|
|
class VulkanRuntime {
|
|
|
|
public:
|
|
|
|
explicit VulkanRuntime() = default;
|
|
|
|
VulkanRuntime(const VulkanRuntime &) = delete;
|
|
|
|
VulkanRuntime &operator=(const VulkanRuntime &) = delete;
|
|
|
|
|
|
|
|
/// Sets needed data for Vulkan runtime.
|
|
|
|
void setResourceData(const ResourceData &resData);
|
|
|
|
void setResourceData(const DescriptorSetIndex desIndex,
|
|
|
|
const BindingIndex bindIndex,
|
|
|
|
const VulkanHostMemoryBuffer &hostMemBuffer);
|
|
|
|
void setShaderModule(uint8_t *shader, uint32_t size);
|
|
|
|
void setNumWorkGroups(const NumWorkGroups &numberWorkGroups);
|
|
|
|
void setResourceStorageClassBindingMap(
|
|
|
|
const ResourceStorageClassBindingMap &stClassData);
|
|
|
|
void setEntryPoint(const char *entryPointName);
|
|
|
|
|
|
|
|
/// Runtime initialization.
|
|
|
|
LogicalResult initRuntime();
|
|
|
|
|
|
|
|
/// Runs runtime.
|
|
|
|
LogicalResult run();
|
|
|
|
|
|
|
|
/// Updates host memory buffers.
|
|
|
|
LogicalResult updateHostMemoryBuffers();
|
|
|
|
|
|
|
|
/// Destroys all created vulkan objects and resources.
|
|
|
|
LogicalResult destroy();
|
|
|
|
|
|
|
|
private:
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Pipeline creation methods.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LogicalResult createInstance();
|
|
|
|
LogicalResult createDevice();
|
2020-03-04 00:25:10 +08:00
|
|
|
LogicalResult getBestComputeQueue();
|
2020-02-19 22:11:22 +08:00
|
|
|
LogicalResult createMemoryBuffers();
|
|
|
|
LogicalResult createShaderModule();
|
|
|
|
void initDescriptorSetLayoutBindingMap();
|
|
|
|
LogicalResult createDescriptorSetLayout();
|
|
|
|
LogicalResult createPipelineLayout();
|
|
|
|
LogicalResult createComputePipeline();
|
|
|
|
LogicalResult createDescriptorPool();
|
|
|
|
LogicalResult allocateDescriptorSets();
|
|
|
|
LogicalResult setWriteDescriptors();
|
|
|
|
LogicalResult createCommandPool();
|
2020-03-04 00:25:10 +08:00
|
|
|
LogicalResult createQueryPool();
|
2020-02-19 22:11:22 +08:00
|
|
|
LogicalResult createComputeCommandBuffer();
|
|
|
|
LogicalResult submitCommandBuffersToQueue();
|
2020-06-26 22:59:17 +08:00
|
|
|
// Copy resources from host (staging buffer) to device buffer or from device
|
|
|
|
// buffer to host buffer.
|
|
|
|
LogicalResult copyResource(bool deviceToHost);
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Helper methods.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Maps storage class to a descriptor type.
|
|
|
|
LogicalResult
|
2020-05-09 05:06:03 +08:00
|
|
|
mapStorageClassToDescriptorType(SPIRVStorageClass storageClass,
|
2020-02-19 22:11:22 +08:00
|
|
|
VkDescriptorType &descriptorType);
|
|
|
|
|
|
|
|
/// Maps storage class to buffer usage flags.
|
|
|
|
LogicalResult
|
2020-05-09 05:06:03 +08:00
|
|
|
mapStorageClassToBufferUsageFlag(SPIRVStorageClass storageClass,
|
2020-02-19 22:11:22 +08:00
|
|
|
VkBufferUsageFlagBits &bufferUsage);
|
|
|
|
|
|
|
|
LogicalResult countDeviceMemorySize();
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Vulkan objects.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2020-03-04 00:25:10 +08:00
|
|
|
VkInstance instance{VK_NULL_HANDLE};
|
|
|
|
VkPhysicalDevice physicalDevice{VK_NULL_HANDLE};
|
|
|
|
VkDevice device{VK_NULL_HANDLE};
|
|
|
|
VkQueue queue{VK_NULL_HANDLE};
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
/// Specifies VulkanDeviceMemoryBuffers divided into sets.
|
2020-05-09 05:06:03 +08:00
|
|
|
std::unordered_map<DescriptorSetIndex, std::vector<VulkanDeviceMemoryBuffer>>
|
2020-02-19 22:11:22 +08:00
|
|
|
deviceMemoryBufferMap;
|
|
|
|
|
|
|
|
/// Specifies shader module.
|
2020-03-04 00:25:10 +08:00
|
|
|
VkShaderModule shaderModule{VK_NULL_HANDLE};
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
/// Specifies layout bindings.
|
2020-05-09 05:06:03 +08:00
|
|
|
std::unordered_map<DescriptorSetIndex,
|
|
|
|
std::vector<VkDescriptorSetLayoutBinding>>
|
2020-02-19 22:11:22 +08:00
|
|
|
descriptorSetLayoutBindingMap;
|
|
|
|
|
|
|
|
/// Specifies layouts of descriptor sets.
|
2020-05-09 05:06:03 +08:00
|
|
|
std::vector<VkDescriptorSetLayout> descriptorSetLayouts;
|
2020-03-04 00:25:10 +08:00
|
|
|
VkPipelineLayout pipelineLayout{VK_NULL_HANDLE};
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
/// Specifies descriptor sets.
|
2020-05-09 05:06:03 +08:00
|
|
|
std::vector<VkDescriptorSet> descriptorSets;
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
/// Specifies a pool of descriptor set info, each descriptor set must have
|
|
|
|
/// information such as type, index and amount of bindings.
|
2020-05-09 05:06:03 +08:00
|
|
|
std::vector<DescriptorSetInfo> descriptorSetInfoPool;
|
2020-03-04 00:25:10 +08:00
|
|
|
VkDescriptorPool descriptorPool{VK_NULL_HANDLE};
|
|
|
|
|
|
|
|
/// Timestamp query.
|
|
|
|
VkQueryPool queryPool{VK_NULL_HANDLE};
|
|
|
|
// Number of nonoseconds for timestamp to increase 1
|
|
|
|
float timestampPeriod{0.f};
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
/// Computation pipeline.
|
2020-03-04 00:25:10 +08:00
|
|
|
VkPipeline pipeline{VK_NULL_HANDLE};
|
|
|
|
VkCommandPool commandPool{VK_NULL_HANDLE};
|
2020-05-09 05:06:03 +08:00
|
|
|
std::vector<VkCommandBuffer> commandBuffers;
|
2020-02-19 22:11:22 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Vulkan memory context.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
uint32_t queueFamilyIndex{0};
|
2020-03-04 00:25:10 +08:00
|
|
|
VkQueueFamilyProperties queueFamilyProperties{};
|
2020-06-26 22:59:17 +08:00
|
|
|
uint32_t hostMemoryTypeIndex{VK_MAX_MEMORY_TYPES};
|
|
|
|
uint32_t deviceMemoryTypeIndex{VK_MAX_MEMORY_TYPES};
|
2020-02-19 22:11:22 +08:00
|
|
|
VkDeviceSize memorySize{0};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Vulkan execution context.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
NumWorkGroups numWorkGroups;
|
|
|
|
const char *entryPoint{nullptr};
|
|
|
|
uint8_t *binary{nullptr};
|
|
|
|
uint32_t binarySize{0};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Vulkan resource data and storage classes.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ResourceData resourceData;
|
|
|
|
ResourceStorageClassBindingMap resourceStorageClassData;
|
|
|
|
};
|
|
|
|
#endif
|