Skip to content

Convert matrix out/inout arguments with a matrix cast - #8988

Open
Joel Kiptoo (Kiptoo-Deus) wants to merge 2 commits into
microsoft:mainfrom
Kiptoo-Deus:fix-matrix-out-param-conversion
Open

Joel Kiptoo (Kiptoo-Deus) wants to merge 2 commits into
microsoft:mainfrom
Kiptoo-Deus:fix-matrix-out-param-conversion

Conversation

@Kiptoo-Deus

@Kiptoo-Deus Joel Kiptoo (Kiptoo-Deus) commented Sep 30, 2026 •

Copy link
Copy Markdown

Fixes #8809.

When the argument of an out or inout matrix parameter has a different element type (for example a half3x3 argument for an out float3x3 parameter with -enable-16bit-types), DXC crashed while generating the copies around the call:

  • The copy back after the call (EmitHLSLOutParamConversionCopyBack) passed the matrix to ConvertScalarOrVector, which asserts: EmitNumericConversion can only be used with int/float scalars/vectors.
  • For inout, the copy in (EmitHLSLOutParamConversionInit) used a BitCast between the two matrix types, which asserts with Invalid cast!.

Both directions now emit the same HLCast matrix conversion as an explicit matrix cast (DefaultCast, or the unsigned variants depending on the element types), through a small EmitHLSLMatrixConversion helper. When the types already match, nothing changes.

Testing (macOS arm64, Release build with assertions):

  • Added HLSLFileCheck/hlsl/functions/arguments/matrix_out_param_conversion.hlsl, covering out float2x2 to half2x2 (fptrunc on the copy back), inout float2x2 with a half2x2 argument (fpext in, fptrunc out), out int2x2 to uint2x2, and inout float2x2 with a uint2x2 argument (uitofp in, fptoui out). It uses -Od so the conversions stay visible, and the uint2x2/float2x2 case checks uitofp/fptoui, so it fails if the signedness of the conversion is ignored.
  • Without the change, the shader from the issue, the inout case and the int to uint case all crash (exit 133). With it they compile, and with constant inputs the results fold to the expected values (for example the inout doubling of half2x2(1, 2, 3, 4) stores 2, 4, 6, 8).

When the argument of an out or inout matrix parameter has a different
element type (e.g. a half3x3 argument for an out float3x3 parameter),
the copy back after the call went through ConvertScalarOrVector, which
asserts on matrices, and the copy in for inout bitcast the matrix value,
which is not a valid cast either. Use the same HLCast matrix conversion
as an explicit matrix cast in both directions.

Fixes microsoft#8809
Copilot AI balanced review requested due to automatic review settings September 30, 2026 02:31
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The signedness-sensitive conversion paths need an observable integer-to-floating-point regression test.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Fixes DXC crashes when converting matrix out/inout arguments between element types.

Changes:

  • Adds explicit HLCast-based matrix conversions for copy-in and copy-back.
  • Adds regression coverage for half, float, int, and uint matrices.
  • Documents the fix in release notes.
File Description
tools/​clang/​lib/​CodeGen/​CGHLSLMS.cpp Implements matrix argument conversions.
tools/​clang/​test/​HLSLFileCheck/​hlsl/​functions/​arguments/​matrix_out_param_conversion.hlsl Adds regression tests.
docs/​ReleaseNotes.md Records the crash fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@damyanp

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

An int to uint matrix copy doesn't show which cast opcode was used, so
also cover an inout uint2x2 argument for a float2x2 parameter, which
needs uitofp on the way in and fptoui on the way out.
Copilot AI balanced review requested due to automatic review settings October 1, 2026 07:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation matches explicit matrix-cast semantics and includes focused regression coverage.

Review effort: Balanced
Findings: None

Resolved since last review (1)

This branch has not been deployed

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

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

[DXIL] Crash when a function output parameter of a matrix type requires a cast to another matrix type of equal dimensions (e.g., float3x3 -> half3x3)

3 participants