Skip to content

Add API to list coopMat/coopVec types and combinations#10076

Open
cmarcelo wants to merge 1 commit intoshader-slang:masterfrom
cmarcelo:cmat-metadata
Open

Add API to list coopMat/coopVec types and combinations#10076
cmarcelo wants to merge 1 commit intoshader-slang:masterfrom
cmarcelo:cmat-metadata

Conversation

@cmarcelo
Copy link
Copy Markdown
Contributor

@cmarcelo cmarcelo commented Feb 18, 2026

Expose cooperative matrix and cooperative vector metadata through
a new ICooperativeTypesMetadata interface queried from IMetadata.

This information can be used to query the combinations of types that
the graphics driver supports.

Closes #10021.

@cmarcelo cmarcelo requested a review from a team as a code owner February 18, 2026 22:43
Copilot AI review requested due to automatic review settings February 18, 2026 22:43
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 18, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This pull request adds comprehensive support for cooperative matrix and vector operations across the Slang compiler. It introduces new public API types and metadata interfaces for exposing cooperative type information, extends IR with five new cooperative instructions and validation logic, implements codegen for multiple targets (CUDA OptiX, HLSL, SPIR-V), and adds three new scalar types (BFloat16, FloatE4M3, FloatE5M2) plus pointer-sized integer types.

Changes

Cohort / File(s) Summary
Public API & Metadata Interfaces
include/slang.h, source/compiler-core/slang-artifact-associated-impl.h, source/compiler-core/slang-artifact-associated-impl.cpp
Added public enumerations (SlangScope, SlangCooperativeMatrixUse, SlangCooperativeVectorMatrixLayout), scalar types (BFloat16, FloatE4M3, FloatE5M2, IntPtr, UIntPtr), cooperative metadata structs (CooperativeMatrixType, CooperativeMatrixCombination, CooperativeVectorType, CooperativeVectorCombination), and new ICooperativeTypesMetadata interface. Extended ArtifactPostEmitMetadata to implement this interface with corresponding query methods and storage lists.
IR Instructions & Validation
source/slang/slang-ir-insts.lua, source/slang/slang-ir-insts-stable-names.lua, source/slang/slang-ir-validate.h, source/slang/slang-ir-validate.cpp, source/slang/slang-ir-metadata.h, source/slang/slang-ir-metadata.cpp
Defined five new IR instructions (CoopMatMulAdd, CoopVecMatMul, CoopVecMatMulAdd, CoopVecOuterProductAccumulate, CoopVecReduceSumAccumulate) with operand schemas, added comprehensive validation logic including operand count checks and type constraints, and implemented cooperative metadata collection from IR with mapping utilities for scalar types and enums.
SPIR-V Codegen
source/slang/slang-emit-spirv.cpp
Implemented SPIR-V emission for all five cooperative instructions with proper capability/extension declarations, operand mask derivation, component-type and layout mapping to SPIR-V enums, and validation of transpose/stride parameters.
CUDA & OptiX Codegen
source/slang/slang-emit-cuda.cpp, prelude/slang-cuda-prelude.h
Added CUDA codegen for cooperative vector ops targeting OptiX with mapped enum types, intrinsic calls, feature gating diagnostics, and WMMA validation. Updated CUDA prelude templates to use OptiX enums directly instead of Slang integer codes, removing compatibility translation layer.
HLSL Codegen
source/slang/slang-emit-hlsl.h, source/slang/slang-emit-hlsl.cpp
Added HLSL codegen support with new mapping helpers (emitMappedCoopVecComponentType, emitMappedCoopVecMatrixLayout) and statement handlers emitting HLSL builtin calls (__builtin_MatVecMul, __builtin_MatVecMulAdd, __builtin_OuterProductAccumulate, __builtin_VectorAccumulate).
C-Like & Slang Prelude
source/slang/slang-emit-c-like.cpp, source/slang/hlsl.meta.slang
Updated C-like emitter to handle cooperative ops as statements rather than expressions, and refactored Slang prelude to use intrinsic-backed entry points instead of per-target branching, removing SPIR-V/CUDA/HLSL assembly paths and legacy mutation helpers.
Type & Reflection Support
source/slang/slang-reflection-api.cpp, source/slang/slang-reflection-json.cpp, source/core/slang-type-text-util.cpp, source/slang/slang-ir.cpp, source/slang/slang-ir.h
Extended reflection to recognize and handle new scalar types, updated scalar-type name mappings and JSON emission, added new scalar cases to C++ TypeReflection::ScalarType enum, updated IR module version compatibility to 13, and marked cooperative multiply ops as side-effect-free.
Emission Integration
source/slang/slang-emit.cpp
Integrated cooperative validation and metadata collection into the post-link IR pipeline.
WASM & Test Support
source/slang-wasm/slang-wasm-bindings.cpp, tools/slang-unit-test/unit-test-cooperative-type-metadata.cpp, tools/slang-unit-test/unit-test-special-scalar-reflection.cpp
Extended WASM bindings to export new scalar type constants, added comprehensive cooperative metadata unit tests validating type counts and combinations, and added reflection tests for special scalar types.
Shader Codegen Tests
tests/cooperative-matrix/mat-mul-add-cuda-codegen.slang, tests/cooperative-vector/matrix-mul-hlsl-codegen.slang, tests/cooperative-vector/matrix-mul-spirv-codegen.slang, tests/cooperative-vector/training-cuda-codegen.slang, tests/cooperative-vector/training-hlsl-codegen.slang, tests/cooperative-vector/training-spirv-codegen.slang
Added shader test files validating codegen for cooperative matrix/vector operations across CUDA, HLSL, and SPIR-V targets with FileCheck expectations.
Diagnostic Tests
tests/cuda/optix-coopvec.slang, tests/cuda/optix-coopvec-packed-input-diagnostic.slang, tests/cuda/optix-coopvec-transpose-diagnostic.slang
Added CUDA codegen tests verifying unsupported cooperative vector intrinsic diagnostics and training operation code patterns.
Tool Utilities
tools/gfx/slang.slang, tools/render-test/shader-input-layout.cpp, tools/slang-test/slang-test-main.cpp
Extended GPU graphics tool with new scalar type enum, updated render test to handle new scalar types with conversion helpers, and extended comparison utility to treat new float formats as floating-point.
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.85% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: adding an API to expose and list cooperative matrix/vector types and their combinations.
Description check ✅ Passed The PR description clearly relates to the changeset, describing the addition of ICooperativeTypesMetadata interface and cooperative matrix/vector metadata exposure.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds a new API to expose cooperative matrix type metadata, allowing applications to query which cooperative matrix type combinations are used in compiled shaders. This addresses issue #10021, where applications need to verify driver support for specific cooperative matrix type combinations used in shaders.

Changes:

  • Adds new public API types (SlangScope, SlangCooperativeMatrixUse, SlangCooperativeComponentType, SlangCooperativeMatrixType) and ICooperativeMatrixMetadata interface to slang.h
  • Implements metadata collection that recursively scans IR to identify all cooperative matrix types used in the compiled code
  • Includes comprehensive unit tests for both the new cooperative matrix metadata API and the binarySearch changes from dependency PRs

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
include/slang.h Adds new enums, struct, and interface for querying cooperative matrix types used in compiled shaders
source/compiler-core/slang-artifact-associated-impl.h Extends ArtifactPostEmitMetadata to implement ICooperativeMatrixMetadata interface
source/compiler-core/slang-artifact-associated-impl.cpp Implements the cooperative matrix metadata query methods with proper error handling
source/slang/slang-ir-metadata.cpp Adds IR type conversion helpers and recursive metadata collection for cooperative matrix types
source/core/slang-list.h Updates binarySearch to return bitwise negation of insertion index on miss (from PR #10048)
source/slang/slang-check-decl.cpp Replaces binarySearch with indexOf for declaration ordering (from PR #10047)
source/slang/slang-language-server-auto-format.cpp Updates exclusion range check to use >= 0 instead of != -1 (from PR #10048)
tools/slang-unit-test/unit-test-cooperative-type-metadata.cpp Comprehensive tests for the cooperative matrix metadata API
tools/slang-unit-test/unit-test-list.cpp Unit tests for the updated binarySearch behavior

@cmarcelo
Copy link
Copy Markdown
Contributor Author

Some context for this PR in #10021 (comment).

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@source/compiler-core/slang-artifact-associated-impl.cpp`:
- Around line 283-293: ArtifactPostEmitMetadata::getInterface is returning the
wrong interface pointer for the slang::IMetadata GUID (it returns
IArtifactPostEmitMetadata*), violating COM rules; update the GUID checks so that
when guid == slang::IMetadata::getTypeGuid() you return
static_cast<slang::IMetadata*>(this), when guid ==
IArtifactPostEmitMetadata::getTypeGuid() you return
static_cast<IArtifactPostEmitMetadata*>(this), and keep the ISlangUnknown,
ICastable and slang::ICooperativeMatrixMetadata cases returning their
corresponding static_casts so each GUID returns the correctly typed interface
pointer.

In `@source/slang/slang-check-decl.cpp`:
- Around line 3581-3582: The two indexOf() results (from
ancestor->getMembers().indexOf(subAncestor) and indexOf(supAncestor)) must be
validated for -1 before being used in arithmetic; update the code to check for
-1 and either assert (e.g., SLANG_ASSERT) or apply the same fallback/ordering
logic as used in _compareDeclsInCommonParentByOrderOfDeclaration() so a missing
member does not produce incorrect ordering. Locate the variables subIndex and
supIndex in this block and add the check/early-return or normalized ordering
fallback when either index is -1, and log or assert with clear context
referencing subAncestor/supAncestor to aid debugging.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tools/slang-unit-test/unit-test-cooperative-type-metadata.cpp`:
- Around line 37-50: The test must guard and skip when SPIR-V or the
spvCooperativeMatrixKHR capability is unavailable; before proceeding with
targetDesc/compilation use the project's test-skip pattern (SLANG_IGNORE_TEST)
to check that globalSession->findProfile("spirv_1_6") is non-null and that
int32_t(globalSession->findCapability("spvCooperativeMatrixKHR")) is non-zero
(or otherwise indicates support), and if either check fails call
SLANG_IGNORE_TEST with an explanatory message instead of continuing; update the
logic around globalSession, targetDesc.format (SLANG_SPIRV), and
capabilityOption so the test is skipped early when those features are absent.

Copy link
Copy Markdown
Collaborator

@jkwak-work jkwak-work left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR wouldn't provide the information needed to query the combination of types that the graphics driver supports.

include/slang.h Outdated

Query this interface from an `IMetadata*`.
*/
struct ICooperativeMatrixMetadata : public ISlangUnknown
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if we want to have a separate struct for the purpose.
Wouldn't be much simpler to move the functions into IMetadata as initially proposed in the issue comment?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just use the existing IArtifactPostEmitMetadata interface.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong but that looks like is not public, should I make it public?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be much simpler to move the functions into IMetadata as initially proposed in #10021 (comment)?

Wouldn't that break ABI of IMetadata?

Copy link
Copy Markdown
Contributor Author

@cmarcelo cmarcelo Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@csyonghe my understanding is that it would break ABI compatibility to add new virtual methods to IMetadata; so on this iteration I've kept as separate interface. Let me know if we still want to just add the functions to IMetadata.

@jkwak-work jkwak-work self-assigned this Feb 19, 2026
@jkwak-work jkwak-work marked this pull request as draft February 19, 2026 00:41
include/slang.h Outdated

Query this interface from an `IMetadata*`.
*/
struct ICooperativeMatrixMetadata : public ISlangUnknown
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just use the existing IArtifactPostEmitMetadata interface.

@cmarcelo cmarcelo changed the title Add API to identify what cooperative matrix types were used Add API to list coopMat/coopVec types and combinations Feb 22, 2026
@cmarcelo
Copy link
Copy Markdown
Contributor Author

@coderabbitai summary

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 22, 2026

✅ Actions performed

Summary regeneration triggered.

@jkwak-work jkwak-work added the pr: non-breaking PRs without breaking changes label Feb 24, 2026
Copy link
Copy Markdown
Collaborator

@csyonghe csyonghe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I think we need to refactor the way coopMatMulAdd and other coop-vec intrinsics are implemented in the compiler first. We need to move the lowering logic from hlsl.meta.slang (library code) into each backends first so we can map each instrinsics to its dedicated slang IR opcode to simplify the meta data collection logic.

@jkwak-work
Copy link
Copy Markdown
Collaborator

@cmarcelo , were you able to make some progress on this PR?
Can you share some update?

@cmarcelo
Copy link
Copy Markdown
Contributor Author

cmarcelo commented Mar 5, 2026

@cmarcelo , were you able to make some progress on this PR? Can you share some update?

Working on the suggestions. Will update PR here in the next few days.

github-actions[bot]

This comment was marked as outdated.

@cmarcelo cmarcelo force-pushed the cmat-metadata branch 2 times, most recently from 01070ec to a00945c Compare March 9, 2026 03:11
github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

@cmarcelo cmarcelo force-pushed the cmat-metadata branch 2 times, most recently from 8d6a9d6 to ae3fd3c Compare April 1, 2026 15:13
github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

@cmarcelo cmarcelo force-pushed the cmat-metadata branch 2 times, most recently from 40f4137 to 652560c Compare April 9, 2026 04:10
github-actions[bot]

This comment was marked as outdated.

Expose cooperative matrix and cooperative vector metadata through
a new `ICooperativeTypesMetadata` interface queried from `IMetadata`.

This information can be used to query the combinations of types that
the graphics driver supports.

Closes shader-slang#10021.
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: ✅ Clean — no significant issues found

This PR adds a new ICooperativeTypesMetadata COM interface (queryable via castAs from IMetadata) that exposes cooperative matrix and cooperative vector type/combination metadata collected after target-specific lowering. The implementation is correct: metadata collection is positioned after validateCooperativeOperations (ensuring safe cast<> usage) and after lowerCooperativeVectors (ensuring only native constructs are reported). Tests cover 7 scenarios across SPIRV, HLSL, CUDA, and CUDA+optix targets, including edge cases (empty shaders, lowered targets, training ops, mixed usage, error arguments).

Changes Overview

Public API (include/slang.h)

  • What changed: Added SlangScope enum, four POD structs (CooperativeMatrixType, CooperativeMatrixCombination, CooperativeVectorType, CooperativeVectorCombination), and the ICooperativeTypesMetadata interface with 8 methods (count + getByIndex for each of the four collections). Structs define operator== where needed for deduplication. CooperativeVectorType intentionally omits operator== because entries are aggregated by componentType (merging maxSize and usedForTrainingOp).

COM implementation (source/compiler-core/slang-artifact-associated-impl.cpp, source/compiler-core/slang-artifact-associated-impl.h)

  • What changed: ArtifactPostEmitMetadata now additionally inherits ICooperativeTypesMetadata. The getInterface method was extended to handle both IMetadata and ICooperativeTypesMetadata GUIDs (the IMetadata case was previously missing — this PR fixes that gap). All 8 interface methods perform null-pointer and bounds validation returning SLANG_E_INVALID_ARG. Four List<> members store the collected metadata.

IR metadata collection pass (source/slang/slang-ir-metadata.cpp, source/slang/slang-ir-metadata.h)

  • What changed: New collectCooperativeMetadata function performs a breadth-first traversal of the IR module, collecting cooperative types from IRCoopMatrixType/IRCoopVectorType instructions (via getResolvedInstForDecorations) and combinations from IRCoopMatMulAdd/IRCoopVecMatMulAdd/training ops. Types are stored in sorted order using custom operator< (defined in the slang namespace within the .cpp) with _insertSortedUnique for deduplication. Cooperative vector types use _insertOrUpdateCooperativeVectorType to merge entries by componentType. Helper functions validate IR values using std::in_range (first usage of this C++20 utility in the codebase — project targets C++20) and explicit enum case enumeration without default to get compiler warnings on enum growth.

Pipeline integration (source/slang/slang-emit.cpp)

  • What changed: collectCooperativeMetadata is called immediately after validateCooperativeOperations and after all target-specific lowering passes. This ensures cast<> usage in the metadata collector is safe (validation guarantees operand types) and that targets which lower cooperative types to arrays (plain CUDA, Metal, WGSL, GLSL) correctly report empty metadata lists.

Unit tests (tools/slang-unit-test/unit-test-cooperative-type-metadata.cpp)

  • What changed: 859-line test file with 7 test cases covering: subgroup and workgroup matrix types/combinations (SPIRV + CUDA), cooperative vector types/combinations with packed inputs (SPIRV + HLSL + CUDA+optix), training operations (outer product, reduce-sum), mixed training/non-training, lowered vector targets (plain CUDA produces empty), and empty shader baseline. Tests validate null-pointer and out-of-bounds error returns, exact count matching, and per-entry field correctness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: non-breaking PRs without breaking changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a way to know what types are used for CoopVec/Mat operations

5 participants