Skip to content

Emit optional binary SPIR-V (.spv) via SPIRV-Tools#36

Closed
NripeshN wants to merge 1 commit into
mainfrom
feat/spirv-binary-emission
Closed

Emit optional binary SPIR-V (.spv) via SPIRV-Tools#36
NripeshN wants to merge 1 commit into
mainfrom
feat/spirv-binary-emission

Conversation

@NripeshN

Copy link
Copy Markdown
Member

Summary

The Vulkan target previously emitted SPIR-V only as text (.spvasm). This adds optional binary SPIR-V (.spv) emission — real, GPU-loadable output — by assembling the generated assembly in-process with the SPIRV-Tools library.

  • Optional dependency (mirrors the existing optional MLIR path): find_package(SPIRV-Tools CONFIG QUIET); when present, CROSSGL_HAVE_SPIRV_TOOLS=1 is defined and the library linked. A build without SPIRV-Tools still succeeds and simply emits only .spvasm (verified via -DCMAKE_DISABLE_FIND_PACKAGE_SPIRV-Tools=ONcglc links, feature disabled).
  • Assembler (SPIRVAssembler.h/.cpp): assembleVulkanSpirvText() uses the SPIRV-Tools C API (spvTextToBinary) with SPV_ENV_VULKAN_1_2, matched to the backend's vulkan1.2 / SPIR-V 1.5 target. Returns std::nullopt (stub) when the library is absent, so callers compile either way.
  • Emission: the Vulkan backend writes a sibling <module>.spv next to the .spvasm (the package metadata already recognizes .spv). On assembly failure it surfaces the exact SPIRV-Tools diagnostic rather than hiding it.

Verification

  • Builds with SPIRV-Tools found (feature on) and with it disabled (stub path) — both confirmed.
  • Full suite green: 100% tests passed, 0 tests failed out of 2453.
  • A real emitted .spv validates: spirv-val --target-env vulkan1.2 <module>.spv → valid; SPIR-V magic word 0x07230203; byte-identical to spirv-as assembling the same text.
  • New spirv-val-gated smoke test (cglc_build_vulkan_spirv_tools_binary_smoke) that asserts the .spv exists, carries the magic word, and validates.

Follow-up

Native binary encoding (no SPIRV-Tools dependency) and wiring .spv into the .cglb package archive remain future work.

When SPIRV-Tools is available (detected like the optional MLIR path), the Vulkan
backend assembles the generated .spvasm text in-process (spvTextToBinary,
SPV_ENV_VULKAN_1_2 matching the vulkan1.2/SPIR-V 1.5 target) and writes a sibling
.spv binary. Fully optional: builds without SPIRV-Tools succeed and emit only
.spvasm. Adds SPIRVAssembler (.h/.cpp) and a spirv-val-gated smoke test.
@NripeshN

Copy link
Copy Markdown
Member Author

Closing: on review this is redundant. The Vulkan native path (buildVulkanPrototypeBinary) already produces a validated binary .spv via the spirv-as/spirv-val CLI, and this PR's in-process library write runs only after the spirv-as requirement check (so only when the CLI is present) and is overwritten byte-identically by the CLI command. The genuinely-additive change — making vulkan native builds self-contained (assemble + validate in-process via the linked SPIRV-Tools library, relaxing the spirv-as/spirv-val CLI requirements) — is a separate focused change; will revisit.

@NripeshN NripeshN closed this Jun 21, 2026
@NripeshN NripeshN deleted the feat/spirv-binary-emission branch June 21, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant