CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tools / multitarget / spirv-host.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* spirv-host: INTERIM C launcher that runs one SPIR-V module from backend/spirv/emit.om on a CPU |
| 3 | * implementation: PoCL (OpenCL, format SPIRV_CL) or Mesa lavapipe (Vulkan, format SPIRV_VK). |
| 4 | * It is a host-only conformance harness; it never generates or edits SPIR-V and never chooses results. |
| 5 | * |
| 6 | * Why C: seeded Omega has no checked shared-library call yet, so a .om program cannot call |
| 7 | * libOpenCL or libvulkan (Omega extension proposal E2, ADR 0043, aien-dev/omega-mv#888). Migration-pending: this file only |
| 8 | * builds with -DCUDAPP_MIGRATION_PENDING_SPIRV_HOST=1, which defaults OFF (mk/multitarget.mk). |
| 9 | * Successor: backend/vulkan/host.om and backend/opencl/host.om over E2's checked calls. |
| 10 | * |
| 11 | * GPU isolation: the OpenCL loader is pinned to one absolute CPU library (MT_POCL_LIBRARY) and only |
| 12 | * CL_DEVICE_TYPE_CPU is enumerated; the Vulkan loader is pinned to the lavapipe manifest |
| 13 | * (MT_LVP_ICD) with layers disabled, and the device must report VK_PHYSICAL_DEVICE_TYPE_CPU. |
| 14 | * Before exit the process scans /proc/self/maps and /proc/self/fd and fails (exit 9) on any mapping |
| 15 | * or descriptor whose path contains MT_FORBID ("nvidia" by default; the gate builds a control copy |
| 16 | * with a pattern that must trip). |
| 17 | * |
| 18 | * usage: spirv-host cl|vk MODULE.spv KERNEL COUNT IN.bin [IN.bin] OUT.bin |
| 19 | * COUNT is the output element count (n); every input file holds COUNT*IN_RATIO floats, where |
| 20 | * IN_RATIO = (input bytes) / (4*COUNT) must be a positive integer. Buffers are passed in order |
| 21 | * (inputs, then output); cl also gets uint n as the last argument. Output starts as the NaN |
| 22 | * pattern 0xFFC0FFEE so an unwritten element can never compare equal to a real result. |
| 23 | * Exit: 0 ran; 2 usage; 3 environment; 4 input; 5 device; 6 program; 7 kernel; 8 run; 9 isolation. |
| 24 | */ |
| 25 | #if !defined(CUDAPP_MIGRATION_PENDING_SPIRV_HOST) || CUDAPP_MIGRATION_PENDING_SPIRV_HOST != 1 |
| 26 | #error "spirv-host is migration-pending C: build with -DCUDAPP_MIGRATION_PENDING_SPIRV_HOST=1 (default OFF)" |
| 27 | #endif |
| 28 | #define _GNU_SOURCE |
| 29 | #define CL_TARGET_OPENCL_VERSION 300 |
| 30 | #include <CL/cl.h> |
| 31 | #include <vulkan/vulkan.h> |
| 32 | |
| 33 | #include <dirent.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <stdint.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #ifndef MT_POCL_LIBRARY |
| 42 | #define MT_POCL_LIBRARY "" |
| 43 | #endif |
| 44 | #ifndef MT_LVP_ICD |
| 45 | #define MT_LVP_ICD "/usr/share/vulkan/icd.d/lvp_icd.json" |
| 46 | #endif |
| 47 | #ifndef MT_FORBID |
| 48 | #define MT_FORBID "nvidia" |
| 49 | #endif |
| 50 | #define MAX_BUFS 3 |
| 51 | #define POISON 0xFFC0FFEEu |
| 52 | #define LOCAL 64u |
| 53 | |
| 54 | static int die(int code, const char *what) { fprintf(stderr, "spirv-host: %s\n", what); return code; } |
| 55 | |
| 56 | static void *slurp(const char *path, size_t *size) |
| 57 | { |
| 58 | FILE *f = fopen(path, "rb"); |
| 59 | if (!f) return NULL; |
| 60 | if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; } |
| 61 | long n = ftell(f); |
| 62 | if (n <= 0 || fseek(f, 0, SEEK_SET) != 0) { fclose(f); return NULL; } |
| 63 | void *p = malloc((size_t)n); |
| 64 | if (p && fread(p, 1, (size_t)n, f) != (size_t)n) { free(p); p = NULL; } |
| 65 | fclose(f); |
| 66 | *size = (size_t)n; |
| 67 | return p; |
| 68 | } |
| 69 | |
| 70 | /* Fails when any mapping or open descriptor of this process names MT_FORBID. */ |
| 71 | static int isolation(void) |
| 72 | { |
| 73 | char line[4096]; |
| 74 | FILE *m = fopen("/proc/self/maps", "r"); |
| 75 | if (!m) return die(9, "isolation: cannot read /proc/self/maps"); |
| 76 | int bad = 0; |
| 77 | while (fgets(line, sizeof line, m)) |
| 78 | if (strstr(line, MT_FORBID)) { bad = 1; fprintf(stderr, "spirv-host: isolation: mapped %s", line); break; } |
| 79 | fclose(m); |
| 80 | DIR *d = opendir("/proc/self/fd"); |
| 81 | if (!d) return die(9, "isolation: cannot read /proc/self/fd"); |
| 82 | struct dirent *e; |
| 83 | char link[300], target[4096]; |
| 84 | while (!bad && (e = readdir(d)) != NULL) { |
| 85 | if (e->d_name[0] == '.') continue; |
| 86 | snprintf(link, sizeof link, "/proc/self/fd/%s", e->d_name); |
| 87 | ssize_t k = readlink(link, target, sizeof target - 1); |
| 88 | if (k <= 0) continue; |
| 89 | target[k] = 0; |
| 90 | if (strstr(target, MT_FORBID)) { bad = 1; fprintf(stderr, "spirv-host: isolation: open fd %s\n", target); } |
| 91 | } |
| 92 | closedir(d); |
| 93 | if (bad) return die(9, "isolation FAILED: forbidden path \"" MT_FORBID "\" present"); |
| 94 | fprintf(stderr, "spirv-host: isolation: no mapping or descriptor contains \"%s\"\n", MT_FORBID); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | typedef struct { const char *api, *module, *kernel; uint32_t count; int nin; const char *in[2], *out; |
| 99 | void *code; size_t code_size; float *buf[MAX_BUFS]; size_t bytes[MAX_BUFS]; } Job; |
| 100 | |
| 101 | static int run_cl(Job *j) |
| 102 | { |
| 103 | const char *lib = MT_POCL_LIBRARY; |
| 104 | if (lib[0] != '/') return die(3, "build needs an absolute MT_POCL_LIBRARY"); |
| 105 | if (setenv("OCL_ICD_VENDORS", lib, 1) || unsetenv("OCL_ICD_FILENAMES") || unsetenv("OPENCL_LAYERS") || |
| 106 | setenv("OPENCL_LAYER_PATH", "/dev/null", 1) || setenv("POCL_DEVICES", "cpu", 1)) |
| 107 | return die(3, "cannot pin OpenCL loader environment"); |
| 108 | cl_platform_id plat; cl_uint np = 0; cl_int err; |
| 109 | if (clGetPlatformIDs(1, &plat, &np) != CL_SUCCESS || np != 1) return die(5, "expected exactly one OpenCL platform (pinned PoCL)"); |
| 110 | char text[512]; |
| 111 | if (clGetPlatformInfo(plat, CL_PLATFORM_NAME, sizeof text, text, NULL) != CL_SUCCESS || |
| 112 | strcmp(text, "Portable Computing Language") != 0) return die(5, "platform is not PoCL"); |
| 113 | clGetPlatformInfo(plat, CL_PLATFORM_VERSION, sizeof text, text, NULL); |
| 114 | fprintf(stderr, "spirv-host: cl library=%s platform=\"%s\"\n", lib, text); |
| 115 | cl_device_id dev; |
| 116 | if (clGetDeviceIDs(plat, CL_DEVICE_TYPE_CPU, 1, &dev, NULL) != CL_SUCCESS) return die(5, "no CPU device"); |
| 117 | cl_device_type ty = 0; |
| 118 | clGetDeviceInfo(dev, CL_DEVICE_TYPE, sizeof ty, &ty, NULL); |
| 119 | if (ty != CL_DEVICE_TYPE_CPU) return die(5, "device is not CL_DEVICE_TYPE_CPU"); |
| 120 | clGetDeviceInfo(dev, CL_DEVICE_NAME, sizeof text, text, NULL); |
| 121 | fprintf(stderr, "spirv-host: cl device=\"%s\" type=CPU\n", text); |
| 122 | cl_context ctx = clCreateContext(NULL, 1, &dev, NULL, NULL, &err); |
| 123 | if (err != CL_SUCCESS) return die(5, "clCreateContext"); |
| 124 | cl_command_queue q = clCreateCommandQueueWithProperties(ctx, dev, NULL, &err); |
| 125 | if (err != CL_SUCCESS) return die(5, "clCreateCommandQueue"); |
| 126 | cl_program prog = clCreateProgramWithIL(ctx, j->code, j->code_size, &err); |
| 127 | if (err != CL_SUCCESS) return die(6, "clCreateProgramWithIL refused the module"); |
| 128 | if (clBuildProgram(prog, 1, &dev, "", NULL, NULL) != CL_SUCCESS) { |
| 129 | char log[4096] = { 0 }; |
| 130 | clGetProgramBuildInfo(prog, dev, CL_PROGRAM_BUILD_LOG, sizeof log - 1, log, NULL); |
| 131 | fprintf(stderr, "%s\n", log); |
| 132 | return die(6, "clBuildProgram failed"); |
| 133 | } |
| 134 | cl_kernel k = clCreateKernel(prog, j->kernel, &err); |
| 135 | if (err != CL_SUCCESS) return die(7, "clCreateKernel: entry point not found"); |
| 136 | cl_mem mem[MAX_BUFS]; |
| 137 | int nb = j->nin + 1; |
| 138 | for (int i = 0; i < nb; i++) { |
| 139 | mem[i] = clCreateBuffer(ctx, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, j->bytes[i], j->buf[i], &err); |
| 140 | if (err != CL_SUCCESS || clSetKernelArg(k, (cl_uint)i, sizeof mem[i], &mem[i]) != CL_SUCCESS) |
| 141 | return die(7, "buffer or argument"); |
| 142 | } |
| 143 | cl_uint n = j->count; |
| 144 | if (clSetKernelArg(k, (cl_uint)nb, sizeof n, &n) != CL_SUCCESS) return die(7, "argument n"); |
| 145 | size_t global = ((size_t)j->count + LOCAL - 1) / LOCAL * LOCAL, local = LOCAL; |
| 146 | if (clEnqueueNDRangeKernel(q, k, 1, NULL, &global, &local, 0, NULL, NULL) != CL_SUCCESS || |
| 147 | clEnqueueReadBuffer(q, mem[nb - 1], CL_TRUE, 0, j->bytes[nb - 1], j->buf[nb - 1], 0, NULL, NULL) != CL_SUCCESS || |
| 148 | clFinish(q) != CL_SUCCESS) |
| 149 | return die(8, "enqueue, read back or finish failed"); |
| 150 | fprintf(stderr, "spirv-host: cl dispatched global=%zu local=%zu\n", global, local); |
| 151 | for (int i = 0; i < nb; i++) clReleaseMemObject(mem[i]); |
| 152 | clReleaseKernel(k); clReleaseProgram(prog); clReleaseCommandQueue(q); clReleaseContext(ctx); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | #define VKOK(x, code, what) do { if ((x) != VK_SUCCESS) return die(code, what); } while (0) |
| 157 | |
| 158 | static int memtype(VkPhysicalDevice pd, uint32_t bits) |
| 159 | { |
| 160 | VkPhysicalDeviceMemoryProperties mp; |
| 161 | vkGetPhysicalDeviceMemoryProperties(pd, &mp); |
| 162 | const VkMemoryPropertyFlags want = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 163 | for (uint32_t i = 0; i < mp.memoryTypeCount; i++) |
| 164 | if ((bits & (1u << i)) && (mp.memoryTypes[i].propertyFlags & want) == want) return (int)i; |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | static int run_vk(Job *j) |
| 169 | { |
| 170 | if (setenv("VK_DRIVER_FILES", MT_LVP_ICD, 1) || setenv("VK_ICD_FILENAMES", MT_LVP_ICD, 1) || |
| 171 | unsetenv("VK_ADD_DRIVER_FILES") || unsetenv("VK_INSTANCE_LAYERS") || unsetenv("VK_LAYER_PATH") || |
| 172 | unsetenv("VK_ADD_LAYER_PATH") || setenv("VK_LOADER_LAYERS_DISABLE", "~all~", 1)) |
| 173 | return die(3, "cannot pin Vulkan loader environment"); |
| 174 | VkApplicationInfo app = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "cudapp-spirv-host", |
| 175 | .apiVersion = VK_API_VERSION_1_1 }; |
| 176 | VkInstanceCreateInfo ici = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app }; |
| 177 | VkInstance inst; |
| 178 | VKOK(vkCreateInstance(&ici, NULL, &inst), 5, "vkCreateInstance"); |
| 179 | uint32_t npd = 0; |
| 180 | VKOK(vkEnumeratePhysicalDevices(inst, &npd, NULL), 5, "vkEnumeratePhysicalDevices"); |
| 181 | if (npd != 1) return die(5, "expected exactly one Vulkan device (pinned lavapipe)"); |
| 182 | VkPhysicalDevice pd; |
| 183 | VKOK(vkEnumeratePhysicalDevices(inst, &npd, &pd), 5, "vkEnumeratePhysicalDevices"); |
| 184 | VkPhysicalDeviceProperties pp; |
| 185 | vkGetPhysicalDeviceProperties(pd, &pp); |
| 186 | if (pp.deviceType != VK_PHYSICAL_DEVICE_TYPE_CPU) return die(5, "device is not VK_PHYSICAL_DEVICE_TYPE_CPU"); |
| 187 | fprintf(stderr, "spirv-host: vk icd=%s device=\"%s\" type=CPU api=%u.%u.%u driver=0x%x\n", MT_LVP_ICD, pp.deviceName, |
| 188 | VK_API_VERSION_MAJOR(pp.apiVersion), VK_API_VERSION_MINOR(pp.apiVersion), VK_API_VERSION_PATCH(pp.apiVersion), |
| 189 | pp.driverVersion); |
| 190 | uint32_t nq = 0; |
| 191 | vkGetPhysicalDeviceQueueFamilyProperties(pd, &nq, NULL); |
| 192 | VkQueueFamilyProperties qf[16]; |
| 193 | if (nq > 16) nq = 16; |
| 194 | vkGetPhysicalDeviceQueueFamilyProperties(pd, &nq, qf); |
| 195 | uint32_t fam = UINT32_MAX; |
| 196 | for (uint32_t i = 0; i < nq; i++) if (qf[i].queueFlags & VK_QUEUE_COMPUTE_BIT) { fam = i; break; } |
| 197 | if (fam == UINT32_MAX) return die(5, "no compute queue"); |
| 198 | float prio = 1.0f; |
| 199 | VkDeviceQueueCreateInfo qci = { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .queueFamilyIndex = fam, |
| 200 | .queueCount = 1, .pQueuePriorities = &prio }; |
| 201 | VkDeviceCreateInfo dci = { .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, .queueCreateInfoCount = 1, .pQueueCreateInfos = &qci }; |
| 202 | VkDevice dev; |
| 203 | VKOK(vkCreateDevice(pd, &dci, NULL, &dev), 5, "vkCreateDevice"); |
| 204 | VkQueue q; |
| 205 | vkGetDeviceQueue(dev, fam, 0, &q); |
| 206 | int nb = j->nin + 1; |
| 207 | VkBuffer b[MAX_BUFS]; VkDeviceMemory m[MAX_BUFS]; void *map[MAX_BUFS]; |
| 208 | for (int i = 0; i < nb; i++) { |
| 209 | VkBufferCreateInfo bci = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = j->bytes[i], |
| 210 | .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE }; |
| 211 | VKOK(vkCreateBuffer(dev, &bci, NULL, &b[i]), 5, "vkCreateBuffer"); |
| 212 | VkMemoryRequirements mr; |
| 213 | vkGetBufferMemoryRequirements(dev, b[i], &mr); |
| 214 | int t = memtype(pd, mr.memoryTypeBits); |
| 215 | if (t < 0) return die(5, "no host-visible coherent memory type"); |
| 216 | VkMemoryAllocateInfo mai = { .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, .memoryTypeIndex = (uint32_t)t }; |
| 217 | VKOK(vkAllocateMemory(dev, &mai, NULL, &m[i]), 5, "vkAllocateMemory"); |
| 218 | VKOK(vkBindBufferMemory(dev, b[i], m[i], 0), 5, "vkBindBufferMemory"); |
| 219 | VKOK(vkMapMemory(dev, m[i], 0, j->bytes[i], 0, &map[i]), 5, "vkMapMemory"); |
| 220 | memcpy(map[i], j->buf[i], j->bytes[i]); |
| 221 | } |
| 222 | VkShaderModuleCreateInfo smci = { .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, .codeSize = j->code_size, .pCode = j->code }; |
| 223 | VkShaderModule sm; |
| 224 | VKOK(vkCreateShaderModule(dev, &smci, NULL, &sm), 6, "vkCreateShaderModule refused the module"); |
| 225 | VkDescriptorSetLayoutBinding lb[MAX_BUFS]; |
| 226 | for (int i = 0; i < nb; i++) |
| 227 | lb[i] = (VkDescriptorSetLayoutBinding){ .binding = (uint32_t)i, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, |
| 228 | .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT }; |
| 229 | VkDescriptorSetLayoutCreateInfo dlci = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, .bindingCount = (uint32_t)nb, .pBindings = lb }; |
| 230 | VkDescriptorSetLayout dsl; |
| 231 | VKOK(vkCreateDescriptorSetLayout(dev, &dlci, NULL, &dsl), 6, "vkCreateDescriptorSetLayout"); |
| 232 | VkPipelineLayoutCreateInfo plci = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .setLayoutCount = 1, .pSetLayouts = &dsl }; |
| 233 | VkPipelineLayout pl; |
| 234 | VKOK(vkCreatePipelineLayout(dev, &plci, NULL, &pl), 6, "vkCreatePipelineLayout"); |
| 235 | VkComputePipelineCreateInfo cpci = { .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, .layout = pl, |
| 236 | .stage = { .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_COMPUTE_BIT, |
| 237 | .module = sm, .pName = j->kernel } }; |
| 238 | VkPipeline pipe; |
| 239 | VKOK(vkCreateComputePipelines(dev, VK_NULL_HANDLE, 1, &cpci, NULL, &pipe), 7, "vkCreateComputePipelines (entry point?)"); |
| 240 | VkDescriptorPoolSize ps = { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, (uint32_t)nb }; |
| 241 | VkDescriptorPoolCreateInfo dpci = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, .maxSets = 1, .poolSizeCount = 1, .pPoolSizes = &ps }; |
| 242 | VkDescriptorPool dp; |
| 243 | VKOK(vkCreateDescriptorPool(dev, &dpci, NULL, &dp), 7, "vkCreateDescriptorPool"); |
| 244 | VkDescriptorSetAllocateInfo dsai = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, .descriptorPool = dp, .descriptorSetCount = 1, .pSetLayouts = &dsl }; |
| 245 | VkDescriptorSet ds; |
| 246 | VKOK(vkAllocateDescriptorSets(dev, &dsai, &ds), 7, "vkAllocateDescriptorSets"); |
| 247 | VkDescriptorBufferInfo dbi[MAX_BUFS]; |
| 248 | VkWriteDescriptorSet wds[MAX_BUFS]; |
| 249 | for (int i = 0; i < nb; i++) { |
| 250 | dbi[i] = (VkDescriptorBufferInfo){ b[i], 0, j->bytes[i] }; |
| 251 | wds[i] = (VkWriteDescriptorSet){ .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = ds, .dstBinding = (uint32_t)i, |
| 252 | .descriptorCount = 1, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, .pBufferInfo = &dbi[i] }; |
| 253 | } |
| 254 | vkUpdateDescriptorSets(dev, (uint32_t)nb, wds, 0, NULL); |
| 255 | VkCommandPoolCreateInfo cpi = { .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .queueFamilyIndex = fam }; |
| 256 | VkCommandPool cp; |
| 257 | VKOK(vkCreateCommandPool(dev, &cpi, NULL, &cp), 8, "vkCreateCommandPool"); |
| 258 | VkCommandBufferAllocateInfo cbai = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = cp, |
| 259 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1 }; |
| 260 | VkCommandBuffer cb; |
| 261 | VKOK(vkAllocateCommandBuffers(dev, &cbai, &cb), 8, "vkAllocateCommandBuffers"); |
| 262 | VkCommandBufferBeginInfo cbbi = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT }; |
| 263 | VKOK(vkBeginCommandBuffer(cb, &cbbi), 8, "vkBeginCommandBuffer"); |
| 264 | vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); |
| 265 | vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pl, 0, 1, &ds, 0, NULL); |
| 266 | uint32_t groups = (j->count + LOCAL - 1) / LOCAL; |
| 267 | vkCmdDispatch(cb, groups, 1, 1); |
| 268 | VkMemoryBarrier mb = { .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, .dstAccessMask = VK_ACCESS_HOST_READ_BIT }; |
| 269 | vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &mb, 0, NULL, 0, NULL); |
| 270 | VKOK(vkEndCommandBuffer(cb), 8, "vkEndCommandBuffer"); |
| 271 | VkSubmitInfo si = { .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 1, .pCommandBuffers = &cb }; |
| 272 | VKOK(vkQueueSubmit(q, 1, &si, VK_NULL_HANDLE), 8, "vkQueueSubmit"); |
| 273 | VKOK(vkQueueWaitIdle(q), 8, "vkQueueWaitIdle"); |
| 274 | fprintf(stderr, "spirv-host: vk dispatched groups=%u local=%u\n", groups, LOCAL); |
| 275 | memcpy(j->buf[nb - 1], map[nb - 1], j->bytes[nb - 1]); |
| 276 | vkDestroyCommandPool(dev, cp, NULL); vkDestroyDescriptorPool(dev, dp, NULL); vkDestroyPipeline(dev, pipe, NULL); |
| 277 | vkDestroyPipelineLayout(dev, pl, NULL); vkDestroyDescriptorSetLayout(dev, dsl, NULL); vkDestroyShaderModule(dev, sm, NULL); |
| 278 | for (int i = 0; i < nb; i++) { vkUnmapMemory(dev, m[i]); vkDestroyBuffer(dev, b[i], NULL); vkFreeMemory(dev, m[i], NULL); } |
| 279 | vkDestroyDevice(dev, NULL); |
| 280 | vkDestroyInstance(inst, NULL); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | int main(int argc, char **argv) |
| 285 | { |
| 286 | if (argc != 7 && argc != 8) return die(2, "usage: spirv-host cl|vk MODULE.spv KERNEL COUNT IN.bin [IN.bin] OUT.bin"); |
| 287 | Job j = { .api = argv[1], .module = argv[2], .kernel = argv[3], .nin = argc - 6, .out = argv[argc - 1] }; |
| 288 | if (strcmp(j.api, "cl") != 0 && strcmp(j.api, "vk") != 0) return die(2, "api must be cl or vk"); |
| 289 | char *end; |
| 290 | unsigned long c = strtoul(argv[4], &end, 10); |
| 291 | if (*end || c == 0 || c > (1ul << 26)) return die(2, "COUNT must be 1..67108864"); |
| 292 | j.count = (uint32_t)c; |
| 293 | for (int i = 0; i < j.nin; i++) j.in[i] = argv[5 + i]; |
| 294 | j.code = slurp(j.module, &j.code_size); |
| 295 | if (!j.code || j.code_size % 4) return die(4, "module unreadable or not whole 32-bit words"); |
| 296 | for (int i = 0; i < j.nin; i++) { |
| 297 | j.buf[i] = slurp(j.in[i], &j.bytes[i]); |
| 298 | if (!j.buf[i] || j.bytes[i] % ((size_t)4 * j.count)) return die(4, "input unreadable or not a multiple of COUNT floats"); |
| 299 | } |
| 300 | j.bytes[j.nin] = (size_t)4 * j.count; |
| 301 | uint32_t *o = malloc(j.bytes[j.nin]); |
| 302 | if (!o) return die(4, "out of memory"); |
| 303 | for (uint32_t i = 0; i < j.count; i++) o[i] = POISON; |
| 304 | j.buf[j.nin] = (float *)o; |
| 305 | int rc = strcmp(j.api, "cl") == 0 ? run_cl(&j) : run_vk(&j); |
| 306 | if (rc) return rc; |
| 307 | rc = isolation(); |
| 308 | if (rc) return rc; |
| 309 | /* 0600, not fopen's 0666 & ~umask (CodeQL cpp/world-writable-file-creation) */ |
| 310 | int fd = open(j.out, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
| 311 | FILE *f = fd >= 0 ? fdopen(fd, "wb") : NULL; |
| 312 | if (!f && fd >= 0) close(fd); |
| 313 | if (!f || fwrite(o, 1, j.bytes[j.nin], f) != j.bytes[j.nin] || fclose(f) != 0) return die(4, "cannot write output"); |
| 314 | return 0; |
| 315 | } |