Skip to content

[SPIR-V] Typed Enum Implicit Conversions Fail #6884

Closed

Description

Description
When using typed enums in math operations, they can completely fail to resolve to correct values. Casting the enum to its type in-place can resolve the issue.

Steps to Reproduce

  1. Create a typed enum, in our case an enum : int { }.
  2. Use that enum in a floating-point operation.
  3. Observe that the enum value is now only ever compiled to 0.

Actual Behavior
Shader playground minimal example of failure here.

enum : int
{
	//Hottest star is <50000K and the coldest star is >600K
	STAR_MAX_TEMPERATURE = 50000,
	STAR_MIN_TEMPERATURE = 600,
 	STAR_TEMPERATURE_RANGE = (STAR_MAX_TEMPERATURE - STAR_MIN_TEMPERATURE),
};

...

float x = 3.0;
buffer[threadId] = STAR_TEMPERATURE_RANGE * x;

compiles to

%int_0 = OpConstant %int 0

Instead of the expected

%int_148200 = OpConstant %int 148200

Shader playground minimal example of success here.

Environment

  • DXC version trunk (I don't see one clearly on shader-playground, but we're targeting cs_6_6, and are using 1.8 locally)
  • Host Operating System: Ubuntu 22.04 LTS, or whatever shader-playground runs on (also probably linux, but I don't know).

Activity

  1. added
    bug
    needs-triage
    spirv
    on Aug 28, 2024
  2. moved this from Triaged to For Google in HLSL Triageon Aug 29, 2024
  3. s-perron commented on Sep 16, 2024

    @s-perron
    Collaborator

    The initial code generated by DXC looks correct: https://godbolt.org/z/onr1n3boP. The optimizer must be doing something wrong.

  4. s-perron commented on Sep 16, 2024

    @s-perron
    Collaborator

    My last comment is wrong. DXC is generating an incorrect instruction:

             %51 = OpBitcast %float %50
    

    This should be an OpConvertUToF and not a bitcast. The bitcast of 49,600 results in subnormal, which can be flushed to 0.

  5. moved this from For Google to Triaged in HLSL Triageon Sep 16, 2024
  6. Keenuts commented on Oct 21, 2024

    @Keenuts
    Collaborator

    This should be an OpConvertUToF and not a bitcast. The bitcast of 49,600 results in subnormal, which can be flushed to 0.

    Yep, seems like this is the reason. Looking into it.

  7. added a commit that references this issue on Oct 21, 2024
    c5b1c54
  8. added a commit that references this issue on Nov 12, 2024
    5230473
  9. added a commit that references this issue on Dec 3, 2024
    eb1223e
  10. added a commit that references this issue on Jul 24, 2025
    d9bb7b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugspirv

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions