diff --git a/tools/tests/mali_compat_spirv_test.c b/tools/tests/mali_compat_spirv_test.c index 28b95eb6da..57dce75d75 100644 --- a/tools/tests/mali_compat_spirv_test.c +++ b/tools/tests/mali_compat_spirv_test.c @@ -125,6 +125,33 @@ static const uint32_t select_scalar[] = { }; static const size_t select_scalar_count = sizeof(select_scalar) / sizeof(uint32_t); +static const uint32_t nonsemantic[] = { + 0x07230203, 0x00010600, 0x00070000, 0x0000000c, 0x00000000, 0x00020011, 0x00000001, 0x0008000b, + 0x00000001, 0x536e6f4e, 0x6e616d65, 0x2e636974, 0x75626544, 0x69725067, 0x0066746e, 0x0003000e, + 0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000002, 0x6e69616d, 0x00000000, 0x00000003, + 0x00030010, 0x00000002, 0x00000007, 0x00030007, 0x00000004, 0x00000078, 0x00040047, 0x00000003, + 0x0000001e, 0x00000000, 0x00020013, 0x00000005, 0x00030016, 0x00000006, 0x00000020, 0x00040020, + 0x00000007, 0x00000003, 0x00000006, 0x0004003b, 0x00000007, 0x00000003, 0x00000003, 0x0004002b, + 0x00000006, 0x00000008, 0x3f800000, 0x00030021, 0x00000009, 0x00000005, 0x00050036, 0x00000005, + 0x00000002, 0x00000000, 0x00000009, 0x000200f8, 0x0000000a, 0x0006000c, 0x00000005, 0x0000000b, + 0x00000001, 0x00000001, 0x00000004, 0x0003003e, 0x00000003, 0x00000008, 0x000100fd, 0x00010038, +}; +static const size_t nonsemantic_count = sizeof(nonsemantic) / sizeof(uint32_t); + +static const uint32_t nonsemantic_extension[] = { + 0x07230203, 0x00010600, 0x00070000, 0x0000000c, 0x00000000, 0x00020011, 0x00000001, 0x0008000a, + 0x5f565053, 0x5f52484b, 0x5f6e6f6e, 0x616d6573, 0x6369746e, 0x666e695f, 0x0000006f, 0x0008000b, + 0x00000001, 0x536e6f4e, 0x6e616d65, 0x2e636974, 0x75626544, 0x69725067, 0x0066746e, 0x0003000e, + 0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000002, 0x6e69616d, 0x00000000, 0x00000003, + 0x00030010, 0x00000002, 0x00000007, 0x00030007, 0x00000004, 0x00000078, 0x00040047, 0x00000003, + 0x0000001e, 0x00000000, 0x00020013, 0x00000005, 0x00030016, 0x00000006, 0x00000020, 0x00040020, + 0x00000007, 0x00000003, 0x00000006, 0x0004003b, 0x00000007, 0x00000003, 0x00000003, 0x0004002b, + 0x00000006, 0x00000008, 0x3f800000, 0x00030021, 0x00000009, 0x00000005, 0x00050036, 0x00000005, + 0x00000002, 0x00000000, 0x00000009, 0x000200f8, 0x0000000a, 0x0006000c, 0x00000005, 0x0000000b, + 0x00000001, 0x00000001, 0x00000004, 0x0003003e, 0x00000003, 0x00000008, 0x000100fd, 0x00010038, +}; +static const size_t nonsemantic_extension_count = sizeof(nonsemantic_extension) / sizeof(uint32_t); + static int failures; static void check(int condition, const char *what) @@ -175,6 +202,179 @@ static void write_module(const char *path, const struct spirv_code *code) fclose(out); } +static struct device test_device; +static int failed_allocations; +static bool fail_allocations; +static int driver_calls; +static bool chain_was_layer_owned; +static VkStructureType first_link; +static uint32_t driver_version; +static uint32_t driver_subgroup_size; + +void *test_calloc(size_t count, size_t size) +{ + if (fail_allocations) { + failed_allocations++; + return NULL; + } + if (size && count > SIZE_MAX / size) + return NULL; + size_t bytes = count * size; + void *memory = malloc(bytes > 0 ? bytes : 1); + if (memory) + memset(memory, 0, bytes); + return memory; +} + +struct device *device_from_handle(const void *dispatchable) +{ + return &test_device; +} + +void layer_log(const char *format, ...) +{ +} + +void *map_get(const struct handle_map *map, uint64_t key) +{ + return NULL; +} + +void map_put(struct handle_map *map, uint64_t key, void *value) +{ +} + +void *map_remove(struct handle_map *map, uint64_t key) +{ + return NULL; +} + +static void read_stage(const VkPipelineShaderStageCreateInfo *stage, const void *app_chain) +{ + driver_calls++; + chain_was_layer_owned = stage->pNext != app_chain; + first_link = stage->pNext ? ((const VkBaseInStructure *)stage->pNext)->sType : VK_STRUCTURE_TYPE_MAX_ENUM; + driver_version = 0; + driver_subgroup_size = 0; + for (const VkBaseInStructure *s = stage->pNext; s; s = s->pNext) { + if (s->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO) + driver_subgroup_size = ((const VkPipelineShaderStageRequiredSubgroupSizeCreateInfo *)s)->requiredSubgroupSize; + if (s->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) { + const VkShaderModuleCreateInfo *info = (const VkShaderModuleCreateInfo *)s; + driver_version = spirv_version(info->pCode, info->codeSize / sizeof(uint32_t)); + } + } +} + +static const void *expected_chain; + +static VkResult VKAPI_CALL driver_compute(VkDevice device, VkPipelineCache cache, uint32_t count, + const VkComputePipelineCreateInfo *infos, + const VkAllocationCallbacks *allocator, VkPipeline *pipelines) +{ + read_stage(&infos[0].stage, expected_chain); + return VK_SUCCESS; +} + +static VkResult VKAPI_CALL driver_graphics(VkDevice device, VkPipelineCache cache, uint32_t count, + const VkGraphicsPipelineCreateInfo *infos, + const VkAllocationCallbacks *allocator, VkPipeline *pipelines) +{ + read_stage(&infos[0].pStages[0], expected_chain); + return VK_SUCCESS; +} + +static const VkShaderModuleCreateInfo app_module = { + .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, + .codeSize = sizeof(newer), + .pCode = newer, +}; + +static const VkPipelineShaderStageRequiredSubgroupSizeCreateInfo app_subgroup = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO, + .pNext = &app_module, + .requiredSubgroupSize = 16, +}; + +static const VkApplicationInfo app_unknown = { + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .pNext = &app_module, +}; + +static const VkPipelineShaderStageCreateInfo app_fragment = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .pNext = &app_subgroup, + .stage = VK_SHADER_STAGE_FRAGMENT_BIT, + .pName = "main", +}; + +static void check_pipelines(void) +{ + VkDevice device = (VkDevice)&test_device; + VkPipeline pipeline = VK_NULL_HANDLE; + pthread_mutex_init(&test_device.lock, NULL); + test_device.CreateComputePipelines = driver_compute; + test_device.CreateGraphicsPipelines = driver_graphics; + + VkComputePipelineCreateInfo compute = { + .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .pNext = &app_subgroup, + .stage = VK_SHADER_STAGE_COMPUTE_BIT, + .pName = "main"}, + }; + expected_chain = &app_subgroup; + check(CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline) == VK_SUCCESS, + "a compute pipeline with inline code behind another structure is created"); + check(chain_was_layer_owned, "the compute stage gets the layer's own chain"); + check(first_link == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO, + "the compute stage keeps its chain order"); + check(driver_subgroup_size == 16, "the compute stage keeps its required subgroup size"); + check(driver_version == 0x00010300u, "the compute stage's inline code reaches the driver as SPIR-V 1.3"); + check(app_module.pCode == newer && app_module.codeSize == sizeof(newer), + "the application's shader module create info is untouched"); + check(compute.stage.pNext == &app_subgroup, "the application's compute stage is untouched"); + + compute.stage.pNext = &app_module; + expected_chain = &app_module; + check(CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline) == VK_SUCCESS, + "a compute pipeline with inline code first in its chain is created"); + check(chain_was_layer_owned && driver_version == 0x00010300u, + "inline code first in the chain reaches the driver as SPIR-V 1.3"); + + compute.stage.pNext = &app_unknown; + expected_chain = &app_unknown; + check(CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline) == VK_SUCCESS, + "a compute pipeline behind an unknown structure is created"); + check(!chain_was_layer_owned && driver_version == 0x00010600u, + "a chain the layer cannot copy reaches the driver as the application wrote it"); + + VkGraphicsPipelineCreateInfo graphics = { + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .stageCount = 1, + .pStages = &app_fragment, + }; + expected_chain = &app_subgroup; + check(CreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphics, NULL, &pipeline) == VK_SUCCESS, + "a graphics pipeline with inline code behind another structure is created"); + check(chain_was_layer_owned && driver_subgroup_size == 16 && driver_version == 0x00010300u, + "the graphics stage reaches the driver capped with its chain intact"); + check(app_fragment.pNext == &app_subgroup && graphics.pStages == &app_fragment, + "the application's graphics stages are untouched"); + + driver_calls = 0; + failed_allocations = 0; + fail_allocations = true; + VkResult compute_result = CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline); + VkResult graphics_result = CreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphics, NULL, &pipeline); + fail_allocations = false; + check(failed_allocations > 0, "the allocations were made to fail"); + check(compute_result == VK_ERROR_OUT_OF_HOST_MEMORY, "a compute pipeline without memory reports it"); + check(graphics_result == VK_ERROR_OUT_OF_HOST_MEMORY, "a graphics pipeline without memory reports it"); + check(driver_calls == 0, "the driver is not called without memory"); + pthread_mutex_destroy(&test_device.lock); +} + int main(int argc, char **argv) { struct spirv_code capped = {0}; @@ -202,6 +402,8 @@ int main(int argc, char **argv) "a module with ShaderNonUniform and no SPV_EXT_descriptor_indexing is left alone"); check(!spirv_cap_version(select_struct, select_struct_count, &kept), "a module selecting a structure is left alone"); + check(!spirv_cap_version(nonsemantic, nonsemantic_count, &kept), + "a module importing a NonSemantic set without SPV_KHR_non_semantic_info is left alone"); check(kept.words == NULL, "no rejected module is handed back"); struct spirv_code extension = {0}; @@ -223,6 +425,16 @@ int main(int argc, char **argv) free(scalar.words); } + struct spirv_code debug = {0}; + check(spirv_cap_version(nonsemantic_extension, nonsemantic_extension_count, &debug), + "a module declaring SPV_KHR_non_semantic_info for its NonSemantic import is capped"); + if (debug.words) { + check(debug.words[1] == 0x00010300u, "the module with the NonSemantic import is SPIR-V 1.3"); + if (argc > 5) + write_module(argv[5], &debug); + free(debug.words); + } + struct spirv_code guest = {0}; check(spirv_cap_version(zinklike, zinklike_count, &guest), "a guest module with a private global is capped"); if (guest.words) { @@ -235,6 +447,8 @@ int main(int argc, char **argv) free(guest.words); } + check_pipelines(); + if (failures) printf("%d check(s) failed\n", failures); else diff --git a/tools/tests/test_mali_compat_spirv.py b/tools/tests/test_mali_compat_spirv.py index 011c95a58f..2ac6ea1b00 100644 --- a/tools/tests/test_mali_compat_spirv.py +++ b/tools/tests/test_mali_compat_spirv.py @@ -36,13 +36,14 @@ class MaliCompatSpirvTest(unittest.TestCase): guest = Path(tmp) / "guest.spv" extension = Path(tmp) / "extension.spv" scalar = Path(tmp) / "scalar.spv" + debug = Path(tmp) / "debug.spv" built = subprocess.run( [compiler, "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-UNDEBUG", - "-pthread", "-I", str(LAYER), "-I", str(vulkan), "-I", str(spirv), - str(HARNESS), str(LAYER / "spirv.c"), "-o", str(program)], + "-pthread", "-Dcalloc=test_calloc", "-I", str(LAYER), "-I", str(vulkan), "-I", str(spirv), + str(HARNESS), str(LAYER / "spirv.c"), str(LAYER / "pipeline.c"), "-o", str(program)], capture_output=True, text=True) self.assertEqual(built.returncode, 0, built.stdout + built.stderr) - ran = subprocess.run([str(program), str(module), str(guest), str(extension), str(scalar)], + ran = subprocess.run([str(program), str(module), str(guest), str(extension), str(scalar), str(debug)], capture_output=True, text=True, timeout=30) self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr) self.assertIn("mali compat spirv ok", ran.stdout) @@ -50,10 +51,11 @@ class MaliCompatSpirvTest(unittest.TestCase): self.assertTrue(guest.is_file()) self.assertTrue(extension.is_file()) self.assertTrue(scalar.is_file()) + self.assertTrue(debug.is_file()) validator = shutil.which("spirv-val") if not validator: return - for lowered in (module, guest, extension, scalar): + for lowered in (module, guest, extension, scalar, debug): checked = subprocess.run([validator, "--target-env", "vulkan1.1", str(lowered)], capture_output=True, text=True) self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr) diff --git a/tools/venus/layer/pipeline.c b/tools/venus/layer/pipeline.c index bae468ad7f..09abeb945e 100644 --- a/tools/venus/layer/pipeline.c +++ b/tools/venus/layer/pipeline.c @@ -5,6 +5,7 @@ #define HANDLE(h) ((uint64_t)(uintptr_t)(h)) #define MAX_STAGES 8 +#define MAX_LINKS 8 static void log_spirv_version(const char *what, const uint32_t *words, size_t count) { @@ -84,7 +85,7 @@ struct stage_code { const uint32_t *words; size_t count; /* The application's own, when the code comes inline in the stage. */ - VkShaderModuleCreateInfo *inline_info; + const VkShaderModuleCreateInfo *inline_info; /* A module whose distances were already taken out at creation. */ bool stripped_module; struct spirv_code capped; @@ -95,7 +96,7 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat memset(code, 0, sizeof(*code)); for (const VkBaseInStructure *s = stage->pNext; s; s = s->pNext) { if (s->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) { - code->inline_info = (VkShaderModuleCreateInfo *)s; + code->inline_info = (const VkShaderModuleCreateInfo *)s; code->words = code->inline_info->pCode; code->count = code->inline_info->codeSize / sizeof(uint32_t); if (spirv_cap_version(code->words, code->count, &code->capped)) { @@ -120,6 +121,15 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat } } +union stage_link { + VkBaseInStructure base; + VkDebugUtilsObjectNameInfoEXT name; + VkPipelineRobustnessCreateInfo robustness; + VkPipelineShaderStageModuleIdentifierCreateInfoEXT identifier; + VkPipelineShaderStageRequiredSubgroupSizeCreateInfo subgroup; + VkShaderModuleValidationCacheCreateInfoEXT validation_cache; +}; + struct pipeline_rewrite { VkPipelineRasterizationStateCreateInfo rasterization; /* VK_EXT_depth_clip_enable's state, taken out of the application's chain for the call. */ @@ -129,27 +139,60 @@ struct pipeline_rewrite { VkShaderModuleCreateInfo modules[MAX_STAGES]; struct spirv_code code[MAX_STAGES]; VkShaderModule temporary[MAX_STAGES]; - /* Rewritten where they are, when they are not first in their stage's chain. */ - VkShaderModuleCreateInfo *in_place[MAX_STAGES]; - const uint32_t *in_place_code[MAX_STAGES]; - size_t in_place_size[MAX_STAGES]; + union stage_link links[MAX_STAGES][MAX_LINKS]; }; +static size_t link_size(VkStructureType type) +{ + switch (type) { + case VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT: + return sizeof(VkDebugUtilsObjectNameInfoEXT); + case VK_STRUCTURE_TYPE_PIPELINE_ROBUSTNESS_CREATE_INFO: + return sizeof(VkPipelineRobustnessCreateInfo); + case VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_MODULE_IDENTIFIER_CREATE_INFO_EXT: + return sizeof(VkPipelineShaderStageModuleIdentifierCreateInfoEXT); + case VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO: + return sizeof(VkPipelineShaderStageRequiredSubgroupSizeCreateInfo); + case VK_STRUCTURE_TYPE_SHADER_MODULE_VALIDATION_CACHE_CREATE_INFO_EXT: + return sizeof(VkShaderModuleValidationCacheCreateInfoEXT); + default: + return 0; + } +} + +static bool link_inline_code(VkPipelineShaderStageCreateInfo *stage, struct pipeline_rewrite *rw, + const struct stage_code *code, uint32_t i) +{ + union stage_link *links = rw->links[i]; + const VkBaseInStructure **next = (const VkBaseInStructure **)&stage->pNext; + uint32_t used = 0; + for (const VkBaseInStructure *s = stage->pNext; s != (const VkBaseInStructure *)code->inline_info; + s = s->pNext) { + size_t size = link_size(s->sType); + if (!size || used == MAX_LINKS) + return false; + memcpy(&links[used], s, size); + *next = &links[used].base; + next = (const VkBaseInStructure **)&links[used].base.pNext; + used++; + } + rw->modules[i] = *code->inline_info; + rw->modules[i].pCode = rw->code[i].words; + rw->modules[i].codeSize = rw->code[i].count * sizeof(uint32_t); + *next = (const VkBaseInStructure *)&rw->modules[i]; + return true; +} + static void use_stage_code(struct device *dev, VkPipelineShaderStageCreateInfo *stage, struct pipeline_rewrite *rw, const struct stage_code *code, uint32_t i) { const struct spirv_code *out = &rw->code[i]; - if (code->inline_info && stage->pNext == code->inline_info) { - rw->modules[i] = *code->inline_info; - rw->modules[i].pCode = out->words; - rw->modules[i].codeSize = out->count * sizeof(uint32_t); - stage->pNext = &rw->modules[i]; - } else if (code->inline_info) { - rw->in_place[i] = code->inline_info; - rw->in_place_code[i] = code->inline_info->pCode; - rw->in_place_size[i] = code->inline_info->codeSize; - code->inline_info->pCode = out->words; - code->inline_info->codeSize = out->count * sizeof(uint32_t); + if (code->inline_info) { + const void *app_chain = stage->pNext; + if (!link_inline_code(stage, rw, code, i)) { + stage->pNext = app_chain; + layer_log("could not rewrite a pipeline stage's inline shader code"); + } } else { VkShaderModuleCreateInfo module_info = { .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, @@ -296,10 +339,6 @@ static void undo_rewrite(struct device *dev, struct pipeline_rewrite *rw) if (rw->clip_prev) rw->clip_prev->pNext = rw->clip_node; for (uint32_t i = 0; i < MAX_STAGES; i++) { - if (rw->in_place[i]) { - rw->in_place[i]->pCode = rw->in_place_code[i]; - rw->in_place[i]->codeSize = rw->in_place_size[i]; - } if (rw->temporary[i] != VK_NULL_HANDLE) dev->DestroyShaderModule(dev->handle, rw->temporary[i], NULL); free(rw->code[i].words); @@ -311,8 +350,13 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipeli const VkAllocationCallbacks *allocator, VkPipeline *pipelines) { struct device *dev = device_from_handle(device); - VkGraphicsPipelineCreateInfo *copies = malloc(count * sizeof(*copies)); + VkGraphicsPipelineCreateInfo *copies = calloc(count, sizeof(*copies)); struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites)); + if (count && (!copies || !rewrites)) { + free(copies); + free(rewrites); + return VK_ERROR_OUT_OF_HOST_MEMORY; + } for (uint32_t i = 0; i < count; i++) { copies[i] = infos[i]; rewrite_pipeline(dev, &copies[i], &rewrites[i]); @@ -330,8 +374,13 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelin const VkAllocationCallbacks *allocator, VkPipeline *pipelines) { struct device *dev = device_from_handle(device); - VkComputePipelineCreateInfo *copies = malloc(count * sizeof(*copies)); + VkComputePipelineCreateInfo *copies = calloc(count, sizeof(*copies)); struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites)); + if (count && (!copies || !rewrites)) { + free(copies); + free(rewrites); + return VK_ERROR_OUT_OF_HOST_MEMORY; + } for (uint32_t i = 0; i < count; i++) { copies[i] = infos[i]; struct stage_code code; diff --git a/tools/venus/layer/spirv.c b/tools/venus/layer/spirv.c index 4fa75e8608..5ff6d87728 100644 --- a/tools/venus/layer/spirv.c +++ b/tools/venus/layer/spirv.c @@ -922,6 +922,12 @@ static bool declares_extension(const struct module *m, const char *name) return false; } +static bool names_prefix(const uint32_t *words, uint32_t count, const char *prefix) +{ + size_t size = strlen(prefix); + return count * sizeof(uint32_t) > size && !memcmp(words, prefix, size); +} + static bool available_in_1_3(const struct module *m, const struct newer_feature *features, size_t count, uint32_t value) { @@ -1023,6 +1029,9 @@ static bool valid_in_1_3(const struct module *m, size_t i) return length >= 4 && !(m->w[i + 3] & NEWER_LOOP_CONTROLS); case SpvOpSelect: return length >= 6 && selects_scalar_vector_or_pointer(m, m->w[i + 1]); + case SpvOpExtInstImport: + return length >= 3 && (!names_prefix(&m->w[i + 2], length - 2, "NonSemantic.") || + declares_extension(m, "SPV_KHR_non_semantic_info")); case SpvOpCopyMemory: case SpvOpCopyMemorySized: { uint32_t at = op == SpvOpCopyMemory ? 3 : 4;