You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SPIR-V] min16float operations wrongly promoted to high precision in SPIR-V/GLSL output #7791
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.
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.
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.
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;
voidmain()
{
_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
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:
That means that we promoteinput.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;
^
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:
Activity
s-perron commentedon Sep 29, 2025
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.
s-perron commentedon Sep 29, 2025
Chris B (@llvm-beanz) Can you confirm that this is the correct behaviour according to the spec?
wegatron commentedon Sep 30, 2025
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
But for this glsl code the offline compiler show no 16 bit ALU instructions
wegatron commentedon Sep 30, 2025
If i change the code like this
the spirv-cross's output becomes:
llvm-beanz commentedon Sep 30, 2025
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:
That means that we promote
input.normalto 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:Compiler Explorer
If instead you remove the
fsuffix 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, orlsuffix to specifyhalf,float, ordoublerespectively. 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:
Compiler Explorer
Since this seems like user error I'm going to close this.