Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pow of literal 0.0f not optimized away in some cases #6557

Open
tex3d opened this issue Apr 22, 2024 · 1 comment
Open

pow of literal 0.0f not optimized away in some cases #6557

tex3d opened this issue Apr 22, 2024 · 1 comment
Labels
bug Bug, regression, crash correctness Bugs that impact shader correctness
Milestone

Comments

@tex3d
Copy link
Contributor

tex3d commented Apr 22, 2024

Description
When using float pow(float x, float y) intrinsic, supplying 0.0f for x should return 0.0f. This fails in some cases.

Steps to Reproduce

// dxc -T ps_6_0 -E main

#define TEST 0
// TEST = 0,3 - fails to optimize away log+mul+exp
// TEST = 1,2,4 - optimizes to return 0.0f

float main() : SV_Target0
{
#if TEST == 0
    float x = 0.0f;
    return pow(x, 2.2f);
#elif TEST == 1
    const float x = 0.0f;
    return pow(x, 2.2f);
#elif TEST == 2
    float x = 0.0f;
    return pow(x, 2.0f);
#elif TEST == 3
    return pow(saturate(0.0f), 2.2f);
#else
    return pow(0.0f, 2.2f);
#endif
}

See repro in Compiler Explorer.

Actual Behavior
When TEST = 0,3 - fails to optimize away log+mul+exp.
When TEST = 1,2,4 - optimizes to return 0.0f (expected behavior for all cases).

Environment

  • DXC version: 1.8.2403 and latest main
  • Host Operating System: any
@tex3d tex3d added bug Bug, regression, crash needs-triage Awaiting triage labels Apr 22, 2024
@tex3d
Copy link
Contributor Author

tex3d commented Apr 22, 2024

I think the root cause of this issue is that we generate DXIL op expansion log2+mul+exp (since we had no constant evaluation of the HL operation during CodeGen), and when it comes time to constant-evaluate the DXIL intrinsics, we bail if any FP exceptions occur (log2(0) = -INF), so the operations are not eliminated. In fact, I'm a bit surprised it succeeds for some of the cases here.

First, I think the general issue needs to be solved to produce the well-defined results for the DXIL ops instead of bailing in FP special cases.
Second, we should have evaluation of more HL operations on literals before lowering to DXIL expansions in the first place.
Third, for ops with reasonable special cases like this, we can detect these and replace them with the expected result during CodeGen.

@damyanp damyanp added correctness Bugs that impact shader correctness and removed needs-triage Awaiting triage labels Apr 23, 2024
@damyanp damyanp added this to the Dormant milestone Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug, regression, crash correctness Bugs that impact shader correctness
Projects
Status: Triaged
Development

No branches or pull requests

2 participants