`-Od`/`-O0` miscompile: branch condition folded to false

Open
#9,053 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
54/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
compilers

Research direction

Start by compiling the provided HLSL reproducer with dxc -T cs_6_0 -E main -Od repro.hlsl -Fc out.txt, then compare its output with -O1 and -O3. Trace how the branch condition is handled in the compiler and find why the mark.Store path and binding disappear at -Od; done means the mark write is retained for nonzero x at -Od.

Written by the indexing model from the issue text.

Description

bug needs-triage

Description
At -Od and -O0 DXC deletes a reachable branch: if (zero < y) is compiled as if it were always false. -O1 and -O3 produce correct code.

Steps to Reproduce

// For any x != 0 the mark branch runs (0 < x), but at -Od DXC removes it.
ByteAddressBuffer inp : register(t0);
RWByteAddressBuffer mark : register(u1);
RWByteAddressBuffer unmark : register(u2);

uint f(uint a, uint b) { return b == 0u ? a : 0u; }   // f(0, 1) == 0

[numthreads(1, 1, 1)]
void main()
{
    uint x = inp.Load(0);
    uint zero = f(0u, 1u);
    uint y = x != 0u ? x : 0u;                         // == x
    if (zero < y) {
        mark.Store(0, 1u);
    } else {
        unmark.Store(0, 1u);
    }

    uint2 c = uint2(0u, 0u);                           // never iterates, but needed
    while (c.x != 0u) { c.x -= 1u; }
}

Compile with dxc -T cs_6_0 -E main -Od repro.hlsl -Fc out.txt

Godbolt of -Od: https://godbolt.org/z/aY85vcY33
Godbolt of -O3: https://godbolt.org/z/osoP7qGhK

Actual Behavior
For any input x != 0, mark should be written. However, at -Od the output contains no store to mark, and mark is missing from the resource bindings.

Environment

  • DXC version: (libdxcompiler.so: 1.9(5512-01b62ad4)(1.9.2609.5)) and trunk (via godbolt)
  • Host Operating System: Linux x86_64 release binaries.

AI Discolosure
Opus 5.5 was used for test-case minimization.

Dominant language
C++
Stars
3.7k
Forks
926
Avg merge
3d 22h
Merged PRs (30d)
45

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/DirectXShaderCompiler

All issues in microsoft/DirectXShaderCompiler

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.