Skip to content

HLSL: Zero-init OpUndef Phi incomings to avoid uninitialized reads#2616

Merged
HansKristian-Work merged 1 commit intoKhronosGroup:mainfrom
xen2:hlsl_opphi_undef
Apr 27, 2026
Merged

HLSL: Zero-init OpUndef Phi incomings to avoid uninitialized reads#2616
HansKristian-Work merged 1 commit intoKhronosGroup:mainfrom
xen2:hlsl_opphi_undef

Conversation

@xen2
Copy link
Copy Markdown
Contributor

@xen2 xen2 commented Apr 24, 2026

When an OpPhi has OpUndef on an incoming edge, SPIRV-Cross emitted a read of the source variable. If that source was uninitialized too, FXC rejects the HLSL with X4555 or X4000.

OpUndef allows any value per spec, so we substitute zero:

  • flush_phi: emit lhs = 0 when the Phi source is OpUndef.
  • Loop variable declarations: add = 0 when static_expression is OpUndef, via a new undef_loop_variable_initializer_suffix helper.

This 2nd version also make sure to not affect GLSL which doesn't need it.

Test

Added shaders-hlsl-no-opt/asm/comp/phi-undef-loop.asm.comp — a minimal SPIR-V loop with OpUndef as the preheader Phi incoming.

Without the patch, the generated HLSL is:

uint _19; // not set
for (uint _22 = 0u; _22 < 10u; _19++, _22++) // error X4555: cannot use casts on l-values (due to _19++ which is not set)
{
    _4.Store(0, _19);
}

With the patch, _19 is declared as uint _19 = 0u; and FXC accepts it.

@xen2 xen2 force-pushed the hlsl_opphi_undef branch 2 times, most recently from 5593ac8 to 5e4061f Compare April 24, 2026 07:52
@xen2 xen2 force-pushed the hlsl_opphi_undef branch from 5e4061f to b93818b Compare April 24, 2026 09:10
Copy link
Copy Markdown
Contributor

@HansKristian-Work HansKristian-Work left a comment

Choose a reason for hiding this comment

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

Seems reasonable.

@HansKristian-Work HansKristian-Work merged commit 9ae1c7d into KhronosGroup:main Apr 27, 2026
10 checks passed
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.

2 participants