fix(spirv): rewrite OpConstantComposite of OpSpecConstant* as OpSpecConstantComposite

This commit is contained in:
maxjivi05 committed 2026-11-20 12:54:44 -05:00
1 parent e5028a1e0e
commit bc3ee739e1
5 files changed
+106 -2

No files matched your search

+39
View File
@@ -181,6 +181,26 @@ static const uint32_t nonsemantic_extension[] = {
};
static const size_t nonsemantic_extension_count = sizeof(nonsemantic_extension) / sizeof(uint32_t);
static const uint32_t spec_constant_composite[] = {
SpvMagicNumber, 0x00010300, 0, 10, 0,
(2u << 16) | SpvOpCapability, SpvCapabilityShader,
(3u << 16) | SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450,
(5u << 16) | SpvOpEntryPoint, SpvExecutionModelFragment, 8, 0x6e69616d, 0,
(3u << 16) | SpvOpExecutionMode, 8, SpvExecutionModeOriginUpperLeft,
(2u << 16) | SpvOpTypeVoid, 1,
(2u << 16) | SpvOpTypeBool, 2,
(4u << 16) | SpvOpTypeVector, 3, 2, 2,
(3u << 16) | SpvOpTypeFunction, 4, 1,
(3u << 16) | SpvOpSpecConstantTrue, 2, 5,
(3u << 16) | SpvOpSpecConstantFalse, 2, 6,
(5u << 16) | SpvOpConstantComposite, 3, 7, 5, 6,
(5u << 16) | SpvOpFunction, 1, 8, 0, 4,
(2u << 16) | SpvOpLabel, 9,
(1u << 16) | SpvOpReturn,
(1u << 16) | SpvOpFunctionEnd,
};
static const size_t spec_constant_composite_count = sizeof(spec_constant_composite) / sizeof(uint32_t);
static int failures;
static void check(int condition, const char *what)
@@ -538,6 +558,25 @@ int main(int argc, char **argv)
}
}
struct spirv_code fixed = {0};
check(!spirv_cap_version(spec_constant_composite, spec_constant_composite_count, &kept),
"a SPIR-V 1.3 module with spec-constant composites is left alone by the cap");
check(kept.words == NULL, "the cap does not hand back a module");
check(spirv_fix_spec_constant_composites(spec_constant_composite, spec_constant_composite_count, &fixed),
"OpConstantComposite of spec constants is rewritten to OpSpecConstantComposite");
if (fixed.words) {
if (argc > 9)
write_module(argv[9], &fixed);
check(count_opcodes(fixed.words, fixed.count, SpvOpConstantComposite) == 0,
"no OpConstantComposite is left");
check(count_opcodes(fixed.words, fixed.count, SpvOpSpecConstantComposite) == 1,
"the composite is now an OpSpecConstantComposite");
free(fixed.words);
}
check(!spirv_fix_spec_constant_composites(older, older_count, &kept),
"a module without spec constants is left alone by the fix");
check(kept.words == NULL, "nothing is handed back by the fix");
check_pipelines();
if (failures)
+4 -2
View File
@@ -40,6 +40,7 @@ class MaliCompatSpirvTest(unittest.TestCase):
private_readonly = Path(tmp) / "private-readonly.spv"
parameter = Path(tmp) / "parameter.spv"
converted = Path(tmp) / "converted.spv"
fixed = Path(tmp) / "fixed.spv"
built = subprocess.run(
[compiler, "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-UNDEBUG",
"-pthread", "-Dcalloc=test_calloc", "-I", str(LAYER), "-I", str(vulkan), "-I", str(spirv),
@@ -47,7 +48,7 @@ class MaliCompatSpirvTest(unittest.TestCase):
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), str(debug),
str(private_readonly), str(converted), str(parameter)],
str(private_readonly), str(converted), str(parameter), str(fixed)],
capture_output=True, text=True, timeout=30)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
self.assertIn("mali compat spirv ok", ran.stdout)
@@ -56,6 +57,7 @@ class MaliCompatSpirvTest(unittest.TestCase):
self.assertTrue(extension.is_file())
self.assertTrue(scalar.is_file())
self.assertTrue(debug.is_file())
self.assertTrue(fixed.is_file())
validator = shutil.which("spirv-val")
if not validator:
return
@@ -80,7 +82,7 @@ class MaliCompatSpirvTest(unittest.TestCase):
self.assertNotEqual(checked.returncode, 0, "the 1.3 form must be rejected")
expected = "NonWritable" if original == private_readonly else "UConvert"
self.assertIn(expected, checked.stdout + checked.stderr)
for lowered in (module, guest, extension, scalar, debug, parameter):
for lowered in (module, guest, extension, scalar, debug, parameter, fixed):
checked = subprocess.run([validator, "--target-env", "vulkan1.1", str(lowered)],
capture_output=True, text=True)
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
+1
View File
@@ -189,6 +189,7 @@ bool spirv_uses_distances(const uint32_t *words, size_t count);
bool spirv_strip_distances(const uint32_t *words, size_t count, struct spirv_code *out);
uint32_t spirv_version(const uint32_t *words, size_t count);
bool spirv_cap_version(const uint32_t *words, size_t count, struct spirv_code *out);
bool spirv_fix_spec_constant_composites(const uint32_t *words, size_t count, struct spirv_code *out);
bool spirv_uses_dual_source(const uint32_t *words, size_t count);
bool spirv_strip_dual_source(const uint32_t *words, size_t count, struct spirv_code *out);
+22
View File
@@ -29,6 +29,12 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShade
const uint32_t *code = have_capped ? capped.words : info->pCode;
size_t words = have_capped ? capped.count : count;
struct spirv_code fixed = {0};
if (spirv_fix_spec_constant_composites(code, words, &fixed)) {
code = fixed.words;
words = fixed.count;
}
struct spirv_code single = {0};
if ((dev->emulated & GAP_DUAL_SRC_BLEND) && spirv_uses_dual_source(code, words)) {
if (spirv_strip_dual_source(code, words, &single)) {
@@ -57,6 +63,7 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShade
free(capped.words);
free(single.words);
free(stripped.words);
free(fixed.words);
return result;
}
@@ -70,6 +77,7 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShade
free(capped.words);
free(single.words);
free(stripped.words);
free(fixed.words);
return VK_SUCCESS;
}
@@ -105,7 +113,15 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat
code->inline_info = (const VkShaderModuleCreateInfo *)s;
code->words = code->inline_info->pCode;
code->count = code->inline_info->codeSize / sizeof(uint32_t);
struct spirv_code fixed = {0};
if (spirv_fix_spec_constant_composites(code->words, code->count, &fixed)) {
code->capped = fixed;
code->words = fixed.words;
code->count = fixed.count;
}
struct spirv_code previous = code->capped;
if (spirv_cap_version(code->words, code->count, &code->capped)) {
free(previous.words);
log_spirv_version("lowered to 1.3", code->words, code->count);
code->words = code->capped.words;
code->count = code->capped.count;
@@ -124,6 +140,12 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat
code->words = original->words;
code->count = original->count;
code->stripped_module = true;
struct spirv_code fixed = {0};
if (spirv_fix_spec_constant_composites(code->words, code->count, &fixed)) {
code->capped = fixed;
code->words = fixed.words;
code->count = fixed.count;
}
}
}
+40
View File
@@ -1144,6 +1144,46 @@ static void trim_entry_interfaces(struct module *m)
}
}
}
bool spirv_fix_spec_constant_composites(const uint32_t *words, size_t count, struct spirv_code *out)
{
if (count < 5 || words[0] != SpvMagicNumber)
return false;
struct module m;
if (!load(&m, words, count))
return false;
bool changed = false;
for (size_t i = 5; i < m.count; i += LENGTH(m.w[i])) {
if (OPCODE(m.w[i]) != SpvOpConstantComposite)
continue;
uint32_t length = LENGTH(m.w[i]);
for (uint32_t k = 2; k < length; k++) {
size_t d = definition(&m, m.w[i + k]);
if (!d)
continue;
uint32_t op = OPCODE(m.w[d]);
if (op == SpvOpSpecConstantTrue || op == SpvOpSpecConstantFalse ||
op == SpvOpSpecConstant || op == SpvOpSpecConstantComposite) {
m.w[i] = (LENGTH(m.w[i]) << 16) | SpvOpSpecConstantComposite;
changed = true;
break;
}
}
}
if (!changed) {
unload(&m);
return false;
}
out->words = malloc(m.count * sizeof(uint32_t));
if (!out->words) {
unload(&m);
return false;
}
memcpy(out->words, m.w, m.count * sizeof(uint32_t));
out->count = m.count;
unload(&m);
return true;
}
uint32_t spirv_version(const uint32_t *words, size_t count)
{
if (count < 5 || words[0] != SpvMagicNumber)