You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "wilx (via GitHub)" <gi...@apache.org> on 2026/07/18 18:50:40 UTC

[PR] [Vulkan] Fix SPIR-V 1.4+ entry-point interfaces [tvm]

wilx opened a new pull request, #20028:
URL: https://github.com/apache/tvm/pull/20028

   ## What changed
   
   This updates Vulkan code generation for targets requesting SPIR-V 1.4 or newer:
   
   - propagate `max_spirv_version` into `SPIRVSupport` and emit that version in the SPIR-V header;
   - track storage-buffer arguments, uniform buffers, push constants, and other module-scope variables;
   - include those variables in the `OpEntryPoint` interface list for SPIR-V 1.4+;
   - retain TVM's historical SPIR-V 1.0 output for older targets; and
   - add a minimal SPIR-V 1.5/Vulkan 1.2 conformance regression test.
   
   ## Why this is necessary
   
   SPIR-V 1.4 changed the `OpEntryPoint` contract: every module-scope variable used by an entry point must be listed in its interface operands. TVM previously listed only built-in variables.
   
   TVM also always emitted a SPIR-V 1.0 header, even when the Vulkan target advertised SPIR-V 1.5. That caused validation to use the older rules and masked the incomplete interface list. Reassembling a generated storage-buffer kernel as SPIR-V 1.5 and validating it for Vulkan 1.2 produced:
   
   ```text
   Interface variable id <4> is used by entry point 'main_kernel' id <2>,
   but is not listed as an interface
   %A_ptr = OpVariable ... StorageBuffer
   ```
   
   The regression test uses the smallest useful kernel, `B[0] = A[0] + 1`. It requires a SPIR-V 1.5 header, reassembles the generated instructions with `spirv-as`, and independently validates them with `spirv-val --target-env vulkan1.2`. The test skips when those SPIR-V Tools executables are unavailable.
   
   After the fix, its entry point contains both storage buffers:
   
   ```text
   OpEntryPoint GLCompute ... "main_kernel" %BuiltInLocalInvocationId %A_ptr %B_ptr
   ```
   
   ## Validation
   
   - Built TVM's Vulkan and LLVM-enabled compiler successfully.
   - Ran the complete Vulkan codegen test module:
   
     ```text
     31 passed, 24 skipped
     ```
   
   - Confirmed `git diff --check` passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Vulkan] Fix SPIR-V 1.4+ entry-point interfaces [tvm]

Posted by "wilx (via GitHub)" <gi...@apache.org>.
wilx commented on PR #20028:
URL: https://github.com/apache/tvm/pull/20028#issuecomment-5012524861

   FYI, this is completely ChatGPT/Codex generated. I was just experimenting with it and it found this issue. HTH.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Vulkan] Fix SPIR-V 1.4+ entry-point interfaces [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #20028:
URL: https://github.com/apache/tvm/pull/20028#discussion_r3610498126


##########
tests/python/codegen/test_target_codegen_vulkan.py:
##########
@@ -86,6 +88,63 @@ def main(A: T.Buffer((1024,), dtype), B: T.Buffer((1024,), dtype)):
     assert len(matches) == 1
 
 
+@pytest.mark.gpu
+@pytest.mark.skipif(
+    not tvm.testing.device_enabled(
+        {
+            "kind": "vulkan",
+            "supports_storage_buffer_storage_class": 1,
+        }
+    ),
+    reason="vulkan not enabled",
+)
+def test_spirv_15_storage_buffer_entry_point_interfaces(tmp_path):
+    """Validate a minimal storage-buffer kernel as SPIR-V 1.5 for Vulkan 1.2."""
+    spirv_as = shutil.which("spirv-as")
+    spirv_val = shutil.which("spirv-val")
+    if spirv_as is None or spirv_val is None:
+        pytest.skip("spirv-as and spirv-val are required")
+
+    target = tvm.target.Target(
+        {
+            "kind": "vulkan",
+            "vulkan_api_version": (1 << 22) | (2 << 12),
+            "max_spirv_version": 0x10500,
+            "supports_storage_buffer_storage_class": 1,
+        }
+    )
+
+    @I.ir_module(s_tir=True)
+    class Module:
+        @T.prim_func(s_tir=True)
+        def main(A: T.Buffer((1,), "float32"), B: T.Buffer((1,), "float32")):
+            for i in T.thread_binding(1, thread="threadIdx.x"):
+                B[i] = A[i] + T.float32(1)
+
+    executable = tvm.tirx.build(Module, target=target)
+    assembly = executable.imports[0].inspect_source()
+    assert "; Version: 1.5" in assembly
+    assembly_path = tmp_path / "storage_buffer.spvasm"
+    binary_path = tmp_path / "storage_buffer.spv"
+    assembly_path.write_text(assembly, encoding="utf-8")
+
+    # Reassembling the generated instructions for SPIR-V 1.5

Review Comment:
   given this depends on spriv tools, i would skip this test, instead rely on local validaiton that the patch works



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Vulkan] Fix SPIR-V 1.4+ entry-point interfaces [tvm]

Posted by "wilx (via GitHub)" <gi...@apache.org>.
wilx commented on code in PR #20028:
URL: https://github.com/apache/tvm/pull/20028#discussion_r3610571904


##########
tests/python/codegen/test_target_codegen_vulkan.py:
##########
@@ -86,6 +88,63 @@ def main(A: T.Buffer((1024,), dtype), B: T.Buffer((1024,), dtype)):
     assert len(matches) == 1
 
 
+@pytest.mark.gpu
+@pytest.mark.skipif(
+    not tvm.testing.device_enabled(
+        {
+            "kind": "vulkan",
+            "supports_storage_buffer_storage_class": 1,
+        }
+    ),
+    reason="vulkan not enabled",
+)
+def test_spirv_15_storage_buffer_entry_point_interfaces(tmp_path):
+    """Validate a minimal storage-buffer kernel as SPIR-V 1.5 for Vulkan 1.2."""
+    spirv_as = shutil.which("spirv-as")
+    spirv_val = shutil.which("spirv-val")
+    if spirv_as is None or spirv_val is None:
+        pytest.skip("spirv-as and spirv-val are required")
+
+    target = tvm.target.Target(
+        {
+            "kind": "vulkan",
+            "vulkan_api_version": (1 << 22) | (2 << 12),
+            "max_spirv_version": 0x10500,
+            "supports_storage_buffer_storage_class": 1,
+        }
+    )
+
+    @I.ir_module(s_tir=True)
+    class Module:
+        @T.prim_func(s_tir=True)
+        def main(A: T.Buffer((1,), "float32"), B: T.Buffer((1,), "float32")):
+            for i in T.thread_binding(1, thread="threadIdx.x"):
+                B[i] = A[i] + T.float32(1)
+
+    executable = tvm.tirx.build(Module, target=target)
+    assembly = executable.imports[0].inspect_source()
+    assert "; Version: 1.5" in assembly
+    assembly_path = tmp_path / "storage_buffer.spvasm"
+    binary_path = tmp_path / "storage_buffer.spv"
+    assembly_path.write_text(assembly, encoding="utf-8")
+
+    # Reassembling the generated instructions for SPIR-V 1.5

Review Comment:
   Removed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Vulkan] Fix SPIR-V 1.4+ entry-point interfaces [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen merged PR #20028:
URL: https://github.com/apache/tvm/pull/20028


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Vulkan] Fix SPIR-V 1.4+ entry-point interfaces [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #20028:
URL: https://github.com/apache/tvm/pull/20028#discussion_r3608971763


##########
tests/python/codegen/test_target_codegen_vulkan.py:
##########
@@ -86,6 +88,64 @@ def main(A: T.Buffer((1024,), dtype), B: T.Buffer((1024,), dtype)):
     assert len(matches) == 1
 
 
+@pytest.mark.gpu
+@pytest.mark.skipif(
+    not tvm.testing.device_enabled(
+        {
+            "kind": "vulkan",
+            "supports_storage_buffer_storage_class": 1,
+        }
+    ),
+    reason="vulkan not enabled",
+)
+def test_spirv_15_storage_buffer_entry_point_interfaces(tmp_path):
+    """Validate a minimal storage-buffer kernel as SPIR-V 1.5 for Vulkan 1.2."""
+    spirv_as = shutil.which("spirv-as")
+    spirv_val = shutil.which("spirv-val")
+    if spirv_as is None or spirv_val is None:
+        pytest.skip("spirv-as and spirv-val are required")
+
+    target = tvm.target.Target(
+        {
+            "kind": "vulkan",
+            "vulkan_api_version": (1 << 22) | (2 << 12),
+            "max_spirv_version": 0x10500,
+            "supports_storage_buffer_storage_class": 1,
+        }
+    )
+
+    @I.ir_module(s_tir=True)
+    class Module:
+        @T.prim_func(s_tir=True)
+        def main(A: T.Buffer((1,), "float32"), B: T.Buffer((1,), "float32")):
+            for i in T.thread_binding(1, thread="threadIdx.x"):
+                B[i] = A[i] + T.float32(1)
+
+    executable = tvm.tirx.build(Module, target=target)
+    assembly = executable.imports[0].inspect_source()
+    assert "; Version: 1.5" in assembly
+    assembly_path = tmp_path / "storage_buffer.spvasm"
+    binary_path = tmp_path / "storage_buffer.spv"
+    assembly_path.write_text(assembly, encoding="utf-8")
+
+    # TVM currently emits a SPIR-V 1.0 header even when the target supports
+    # SPIR-V 1.5.  Reassembling the generated instructions for SPIR-V 1.5
+    # makes spirv-val enforce the Vulkan 1.2 entry-point interface rules.

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The comment states that 'TVM currently emits a SPIR-V 1.0 header even when the target supports SPIR-V 1.5.' However, this PR specifically fixes this behavior so that TVM now correctly emits the requested SPIR-V version (e.g., 1.5) in the header. This comment should be updated to reflect the new correct behavior.
   
   ```suggestion
       # Reassembling the generated instructions for SPIR-V 1.5
       # makes spirv-val enforce the Vulkan 1.2 entry-point interface rules.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org