microsoft / microsoft/DirectXShaderCompiler

Optimizer incorrectly transforms sdiv to udiv when divisor is shifted INT_MIN

Open
#8,094 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug correctness
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

Description

struct UniformBuffer {
    uint a;          // Value: 31
};

struct StorageBuffer {
    int b;
};

cbuffer u_input : register(b0) { UniformBuffer u_input; }
RWByteAddressBuffer s_output : register(u1);

[numthreads(1, 1, 1)]
void main()
{
    // Should be 10 / (INT_MIN >> 31) = 10 / -1 = -10
    s_output.Store(0, int(10) / (int(-2147483647 - 1) >> u_input.a));
}

Expected Behavior
Given u_input.a = 31, the shader should output -10.

Actual Behavior
At -O0, the output is -10 (correct).
At -O1 and higher, the output is 0.

Steps to Reproduce
https://shader-playground.timjones.io/2ffb96015bb368393bb19608833d7d87
The difference between -O0 and -O1 seems to be this:

At -O0,
  %7 = ashr i32 -2147483648, %6
- %8 = sdiv i32 10, %7

At -O1,
  %7 = ashr i32 -2147483648, %6
+ %8 = udiv i32 10, %7

Environment

  • DXC version dxcompiler.dll: 1.10 - 1.9.0.15173 (main, d6b78b745); dxil.dll: 1.10(1.9.0.15173)
  • Host Operating System Microsoft Windows 11 Home,10.0.26200 N/A Build 26200

Contributor guide

Open the contributing guide

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.

Research direction

Start with the linked Shader Playground reproduction and compare the generated IR at -O0 with -O1, focusing on the sdiv-to-udiv transformation after the arithmetic shift. Done means optimization preserves signed division for this shifted INT_MIN case and the shader produces -10 instead of 0.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.