Skip to content

Adding constant half precision value with 'h' postfix in HLSL promotes operation to full precision #7811

Open

Description

@wegatron

Description
When adding a constant half precision value in HLSL using the 'h' postfix (e.g. + 1.0h), the resulting operation is incorrectly promoted to full precision. However, if the constant does not use the 'h' postfix (e.g. + 1.0), the operation remains in half precision as expected. This behavior is inconsistent and leads to incorrect precision in generated output.

Steps to Reproduce
See live example here: https://godbolt.org/z/hEjazrfvj

Actual Behavior

  • Using + 1.0h causes the addition to be performed in full precision.
  • Using + 1.0 keeps the operation in half precision.
  • This is unexpected; both should result in half precision operations.

Environment

  • DXC version:
  • Host Operating System:

Activity

  1. added
    bug
    needs-triage
    on Oct 9, 2025
  2. damyanp commented on Oct 9, 2025

    @damyanp
    Member

    A couple of quick thoughts:

    • are you sure you want to use min16float3 rather than half3?
    • if you really want to use 16-bit floats then you should pass -enable-16bit-types on the command line and use half.
  3. wegatron commented on Oct 10, 2025

    @wegatron
    Author

    A couple of quick thoughts:

    • are you sure you want to use min16float3 rather than half3?
    • if you really want to use 16-bit floats then you should pass -enable-16bit-types on the command line and use half.

    I want to generate spirv with relaxed decoration.

  4. damyanp commented on Oct 10, 2025

    @damyanp
    Member

    Could you come up with a repro that demonstrates what you're expecting to see with -spirv please?

  5. s-perron commented on Oct 10, 2025

    @s-perron
    Collaborator

    This is a question for Chris B (@llvm-beanz). I believe this is expected behaviour. When you do not use -enable-16bit-types, the half types does not exist, and it the same as a float. This is then the same issue as #7791.

    See the recommended solution there. You need to cast the literals to min16float explicitly. There is not suffix for a min16float literal.

  6. wegatron commented on Oct 11, 2025

    @wegatron
    Author

    Could you come up with a repro that demonstrates what you're expecting to see with -spirv please?

    Compare these two case:
    The without h case (https://godbolt.org/z/nTEPocra6), the output is decorated as RelaxedPrecision

    OpDecorate %6 RelaxedPrecision
    ...
    %6 = OpVectorTimesScalar %v3float %5 %float_2
    

    in this case the spirv-cross output will be

    mediump vec3 normal_offset1 = (_input.normal * 2.0) - vec3(1.0);

    The with h case (https://godbolt.org/z/shMGf4qMa), the output have no RelaxPrecision decoration

    %20 = OpVectorTimesScalar %v3float %5 %float_2
    

    in this case the spirv-cross output will be

    mediump vec3 _42 = _input.normal;
    vec3 hp_copy_42 = _42;
    mediump vec3 normal_offset1 = (hp_copy_42 * 2.0) - vec3(1.0);
  7. wegatron commented on Oct 11, 2025

    @wegatron
    Author

    This is a question for @llvm-beanz. I believe this is expected behaviour. When you do not use -enable-16bit-types, the half types does not exist, and it the same as a float. This is then the same issue as #7791.

    See the recommended solution there. You need to cast the literals to min16float explicitly. There is not suffix for a min16float literal.

    Thank you for the clarification. I understand that enabling -enable-16bit-types would require additional extensions when converting to ESSL, and since these extensions have very low support rates on Android, we can only use relaxed precision decoration instead.

    To summarize my understanding:

    min16float corresponds to mediump in ES, representing a relaxed 16-bit float (which drivers may interpret as 32-bit float when necessary)

    float16_t corresponds to float16 in Vulkan GLSL, representing a strict 16-bit float

  8. s-perron commented on Oct 21, 2025

    @s-perron
    Collaborator

    The other part is that float16_t is the same as float in HLSL when you do not use -enable-16bit-types. That is why you see the behaviour you see.

  9. damyanp commented on Oct 29, 2025

    @damyanp
    Member

    Chris B (@llvm-beanz) - this seems to interact with -HV 202x as well. I'm going to put this in the 202x milestone so we can try and bottom out on what we think the right thing to do here is.

  10. moved this from For MSFT to Triaged in HLSL Triageon Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions