Skip to content

[SPIR-V] min16float operations wrongly promoted to high precision in SPIR-V/GLSL output #7791

Closed as not planned

Description

@wegatron

Description
When compiling HLSL code using min16float3 math, the expectation is that all intermediate operations and assignments use 16-bit (mediump) precision. However, DXC's SPIR-V/GLSL output promotes these operations to 32-bit (highp) precision, which is not correct and leads to unnecessary performance loss on some hardware.

For example, given the following HLSL code:

min16float3 normal_offset1 = (input.normal * 2.0f) - 1.0f;

the generated GLSL code looks like:

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

Here, math is performed in highp rather than mediump as expected.
expected output

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

The full demo code is mini16float_test.hlsl.txt

You can compile it with dxc like this:

dxc -T ps_5_0 -E main -HV 2018 -spirv -fvk-use-gl-layout -ffinite-math-only -Zpr -O0 mini16float_test.hlsl -Fo dxc_test.spv
spirv-cross dxc_test.spv --output dxc_test.frag --es --version 310

And I have also test this using glslang, it does not have this bug

glslang -V --target-env spirv1.4 -D -e main -S frag -Od -o gl_test.spv mini16float_test.hlsl
spirv-cross gl_test.spv --output gl_test.frag --es --version 310

Steps to Reproduce

  1. Compile the above HLSL code with DXC targeting SPIR-V/GLSL.
  2. Observe the output, and see that internal variables and operations are promoted to high precision.

Actual Behavior

  • The calculation is done in highp (32-bit float), not mediump (16-bit float) as declared.
  • This breaks precision expectations and may negatively impact performance.

Environment

  • DXC version: 1.8.2505.1
  • Host Operating System: Windows

Activity

  1. added
    bug
    spirv
    needs-triage
    on Sep 29, 2025
  2. s-perron commented on Sep 29, 2025

    @s-perron
    Collaborator

    The issue is that the floating point literal are not min16float. So when a min16float value is multiplied by a regular float, The min16float is implicitly cast to a float, and a but 32-bit multiplication is done.

    I'll have to check what the spec says, but I believe this is the correct and intended bahviour. If you want the operations done on as min16float, cast the literals to min16float. See https://godbolt.org/z/E6arxT5fz. Note that all of the floating point arithmetic now has the RelaxedPrecision decoration.

  3. s-perron commented on Sep 29, 2025

    @s-perron
    Collaborator

    Chris B (@llvm-beanz) Can you confirm that this is the correct behaviour according to the spec?

  4. wegatron commented on Sep 30, 2025

    @wegatron
    Author

    The issue is that the floating point literal are not min16float. So when a min16float value is multiplied by a regular float, The min16float is implicitly cast to a float, and a but 32-bit multiplication is done.

    I'll have to check what the spec says, but I believe this is the correct and intended bahviour. If you want the operations done on as min16float, cast the literals to min16float. See https://godbolt.org/z/E6arxT5fz. Note that all of the floating point arithmetic now has the RelaxedPrecision decoration.

    When I test On Adreno's offline compiler, when mediump float is multiplied by a highp float, the highp is implicitly cast to mediump.
    for this glsl code the offline compiler shows 16 bit ALU instructions

    #version 310 es
    precision highp float;
    precision highp int;
    
    layout(location = 0) in vec2 input_texCoord;
    layout(location = 1) in mediump vec3 input_normal;
    layout(location = 0) out vec4 _entryPointOutput_color;
    
    void main()
    {
        _entryPointOutput_color = vec4(input_normal * 2.0f, 1.0);
    }
        Adreno Offline Compiler (AOC)
        -----------------------------
        AOC Version     : 6.2.0
        Compiler Version: E031.50.08.00
    
    
    ============ Shader Stats FS ============
    Total instruction count                                     :   9
    ALU instruction count - 32 bit                              :   3
    ALU instruction count - 16 bit                              :   3
    

    But for this glsl code the offline compiler show no 16 bit ALU instructions

    #version 310 es
    precision highp float;
    precision highp int;
    
    layout(location = 0) in vec2 in_var_TEXCOORD0;
    layout(location = 1) in mediump vec3 in_var_NORMAL;
    layout(location = 0) out vec4 out_var_SV_TARGET0;
    
    void main()
    {
        mediump vec3 _27 = in_var_NORMAL;
        vec3 hp_copy_27 = _27;
        out_var_SV_TARGET0 = vec4(hp_copy_27 * 2.0, 1.0);
    }
        Adreno Offline Compiler (AOC)
        -----------------------------
        AOC Version     : 6.2.0
        Compiler Version: E031.50.08.00
    
    
    ============ Shader Stats FS ============
    
             Main Shader Stats
    Total instruction count                                     :   9
    ALU instruction count - 32 bit                              :   6
    ALU instruction count - 16 bit                              :   0
    
  5. wegatron commented on Sep 30, 2025

    @wegatron
    Author

    If i change the code like this

        float scale = 2.0f; // add a temporary variable
        min16float3 normal_offset1 = (input.normal * scale);

    the spirv-cross's output becomes:

        float scale = 2.0;
        mediump vec3 normal_offset1 = _input.normal * scale;
  6. llvm-beanz commented on Sep 30, 2025

    @llvm-beanz
    Collaborator

    First and foremost, HLSL's minimum precision types (e.g. min16float) are not the same as the GLSL precision qualifiers (i.e. highp, mediump, lowp`). I don't think we would ever expect the two features to be interchangeable, nor can we control whether or not a specific vendor's driver compiler or external tools (e.g. SPIRV-Cross) handles them correctly.

    As Steven Perron (@s-perron) pointed out, in your example, you are using explicitly typed float literals:

    min16float3 normal_offset1 = (input.normal * 2.0f) - 1.0f;

    That means that we promote input.normal to an fp32 value for the intermediate computations then demote the result back down for assignment. This is how you wrote the code, and the compiler emits a warning letting you know that you did it:

    :21:17: warning: conversion from larger type 'vector' to smaller type 'min16float3', possible loss of data [-Wconversion] min16float3 normal_offset1 = (input.normal * 2.0f) - 1.0f; ^

    Compiler Explorer

    If instead you remove the f suffix you get the result you want:

    Compiler Explorer

    The relevant section of the HLSL language spec is missing explicit reference to the minimum precision types, but they are lower rank than half (see: https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Expr.conv). All float types prefer implicitly converting to higher precision before converting to lower precision.

    This is also true in GLSL, but HLSL and GLSL have slightly different ways of specifying the types of literals.

    Important note!!!

    In HLSL 202x, HLSL's behavior here is changing slightly too. We've removed the "literal" types from the language so the compiler will no longer attempt to magically deduce the types of literals. Instead you must explicitly type them. For normal floating point types that means using the h, f, or l suffix to specify half, float, or double respectively. For the minimum precision types you must use an explicit cast either as a C cast expression or a C++ functional cast syntax.

    The forward-looking code for this case then looks like:

    min16float3 normal_offset1 = (input.normal * min16float(2.0)) - min16float(1.0);

    Compiler Explorer

    Since this seems like user error I'm going to close this.

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

Metadata

Metadata

Labels

bugneeds-triagespirv

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions