24 #define VULKAN_WRAPPER_SYMBOL_EXPORT __declspec(dllexport)
26 #define VULKAN_WRAPPER_SYMBOL_EXPORT __attribute__((visibility("default")))
34 struct VulkanFunction {
38 VulkanFunction(VulkanModule *module,
const char *name)
39 : module(module), name(name) {}
46 VulkanModule(
const uint8_t *ptr,
size_t sizeInBytes)
47 : blob(ptr, ptr + sizeInBytes) {}
48 ~VulkanModule() =
default;
50 VulkanFunction *getFunction(
const char *name) {
51 return functions.emplace_back(std::make_unique<VulkanFunction>(
this, name))
55 uint8_t *blobData() {
return blob.data(); }
56 size_t blobSizeInBytes()
const {
return blob.size(); }
59 std::vector<uint8_t> blob;
60 std::vector<std::unique_ptr<VulkanFunction>> functions;
63 class VulkanRuntimeManager {
65 VulkanRuntimeManager() =
default;
66 VulkanRuntimeManager(
const VulkanRuntimeManager &) =
delete;
67 VulkanRuntimeManager operator=(
const VulkanRuntimeManager &) =
delete;
68 ~VulkanRuntimeManager() =
default;
72 std::lock_guard<std::mutex> lock(mutex);
73 vulkanRuntime.setResourceData(setIndex, bindIndex, memBuffer);
76 void setEntryPoint(
const char *entryPoint) {
77 std::lock_guard<std::mutex> lock(mutex);
78 vulkanRuntime.setEntryPoint(entryPoint);
82 std::lock_guard<std::mutex> lock(mutex);
83 vulkanRuntime.setNumWorkGroups(numWorkGroups);
86 void setShaderModule(uint8_t *shader, uint32_t size) {
87 std::lock_guard<std::mutex> lock(mutex);
88 vulkanRuntime.setShaderModule(shader, size);
92 std::lock_guard<std::mutex> lock(mutex);
93 if (failed(vulkanRuntime.initRuntime()) || failed(vulkanRuntime.run()) ||
94 failed(vulkanRuntime.updateHostMemoryBuffers()) ||
95 failed(vulkanRuntime.destroy())) {
96 std::cerr <<
"runOnVulkan failed";
107 template <
typename T,
int N>
126 return new VulkanRuntimeManager();
130 delete static_cast<VulkanRuntimeManager *
>(vkRuntimeManager);
138 size_t gpuBlobSize) {
140 return new VulkanModule(
static_cast<const uint8_t *
>(data), gpuBlobSize);
144 delete static_cast<VulkanModule *
>(vkModule);
151 return static_cast<VulkanModule *
>(vkModule)->getFunction(name);
156 size_t ,
size_t ,
size_t ,
157 size_t ,
void *vkRuntimeManager,
void **params,
158 void ** ,
size_t paramsCount) {
159 auto manager =
static_cast<VulkanRuntimeManager *
>(vkRuntimeManager);
164 const size_t paramsPerMemRef = 2;
165 if (paramsCount % paramsPerMemRef != 0) {
170 for (
size_t i = 0; i < paramsCount; i += paramsPerMemRef) {
171 void *memrefBufferBasePtr = *
static_cast<void **
>(params[i + 0]);
172 size_t memrefBufferSize = *
static_cast<size_t *
>(params[i + 1]);
174 static_cast<uint32_t
>(memrefBufferSize)};
175 manager->setResourceData(setIndex, bindIndex, memBuffer);
179 manager->setNumWorkGroups(
NumWorkGroups{
static_cast<uint32_t
>(gridX),
180 static_cast<uint32_t
>(gridY),
181 static_cast<uint32_t
>(gridZ)});
183 auto function =
static_cast<VulkanFunction *
>(vkKernel);
185 manager->setShaderModule(
186 function->module->blobData(),
187 static_cast<uint32_t
>(
function->module->blobSizeInBytes()));
188 manager->setEntryPoint(function->name.c_str());
190 manager->runOnVulkan();
203 std::fill_n(ptr->allocated, ptr->sizes[0], value);
210 std::fill_n(ptr->allocated, ptr->sizes[0] * ptr->sizes[1], value);
217 std::fill_n(ptr->allocated, ptr->sizes[0] * ptr->sizes[1] * ptr->sizes[2],
225 std::fill_n(ptr->allocated, ptr->sizes[0], value);
232 std::fill_n(ptr->allocated, ptr->sizes[0] * ptr->sizes[1], value);
239 std::fill_n(ptr->allocated, ptr->sizes[0] * ptr->sizes[1] * ptr->sizes[2],
247 std::fill_n(ptr->allocated, ptr->sizes[0], value);
254 std::fill_n(ptr->allocated, ptr->sizes[0] * ptr->sizes[1], value);
261 std::fill_n(ptr->allocated, ptr->sizes[0] * ptr->sizes[1] * ptr->sizes[2],
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource1DInt8(MemRefDescriptor< int8_t, 1 > *ptr, int8_t value)
Fills the given 1D int memref with the given int8 value.
VULKAN_WRAPPER_SYMBOL_EXPORT void mgpuStreamSynchronize(void *)
VULKAN_WRAPPER_SYMBOL_EXPORT void mgpuLaunchKernel(void *vkKernel, size_t gridX, size_t gridY, size_t gridZ, size_t, size_t, size_t, size_t, void *vkRuntimeManager, void **params, void **, size_t paramsCount)
VULKAN_WRAPPER_SYMBOL_EXPORT void mgpuStreamDestroy(void *vkRuntimeManager)
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource2DFloat(MemRefDescriptor< float, 2 > *ptr, float value)
Fills the given 2D float memref with the given float value.
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource1DFloat(MemRefDescriptor< float, 1 > *ptr, float value)
Fills the given 1D float memref with the given float value.
#define VULKAN_WRAPPER_SYMBOL_EXPORT
VULKAN_WRAPPER_SYMBOL_EXPORT void * mgpuModuleGetFunction(void *vkModule, const char *name)
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource2DInt(MemRefDescriptor< int32_t, 2 > *ptr, int32_t value)
Fills the given 2D int memref with the given int value.
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource2DInt8(MemRefDescriptor< int8_t, 2 > *ptr, int8_t value)
Fills the given 2D int memref with the given int8 value.
VULKAN_WRAPPER_SYMBOL_EXPORT void * mgpuModuleLoad(const void *data, size_t gpuBlobSize)
VULKAN_WRAPPER_SYMBOL_EXPORT void mgpuModuleUnload(void *vkModule)
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource3DInt(MemRefDescriptor< int32_t, 3 > *ptr, int32_t value)
Fills the given 3D int memref with the given int value.
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource3DFloat(MemRefDescriptor< float, 3 > *ptr, float value)
Fills the given 3D float memref with the given float value.
VULKAN_WRAPPER_SYMBOL_EXPORT void * mgpuStreamCreate()
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource1DInt(MemRefDescriptor< int32_t, 1 > *ptr, int32_t value)
Fills the given 1D int memref with the given int value.
VULKAN_WRAPPER_SYMBOL_EXPORT void _mlir_ciface_fillResource3DInt8(MemRefDescriptor< int8_t, 3 > *ptr, int8_t value)
Fills the given 3D int memref with the given int8 value.
uint32_t DescriptorSetIndex
Helper class to produce LLVM dialect operations extracting or inserting elements of a MemRef descript...
Struct containing the number of local workgroups to dispatch for each dimension.
Struct containing information regarding to a host memory buffer.