DirectXShaderCompiler icon indicating copy to clipboard operation
DirectXShaderCompiler copied to clipboard

[SPIR-V] Typed Enum Implicit Conversions Fail

Open xantos117 opened this issue 1 year ago • 3 comments

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).

xantos117 avatar Aug 28 '24 20:08 xantos117

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

s-perron avatar Sep 16 '24 15:09 s-perron

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.

s-perron avatar Sep 16 '24 15:09 s-perron

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.

Keenuts avatar Oct 21 '24 13:10 Keenuts