Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

air_run: a 16-bit target for a half return, which is half of what was missing

vcvkk1e67cd6b3df4
diff --git a/docs/air.md b/docs/air.md
index 76b2bc2..fda6d5e 100644
--- a/docs/air.md
+++ b/docs/air.md
@@ -187,13 +187,31 @@ functions and not entry points at all.
once and NIR folds the conversion into a 32-bit one. The generated Adreno code for
`air.convert.f.f16.u.i32` is four `cov.u32f32` and not one `cov.u32f16`.
- The pool has the value that would decide it, for when the harness can. In `f16` integers are
- exact to 2048 and spaced by 2 above, so **2051** is the discriminator — between 2050 and 2052
- with an odd lower neighbour, where round-to-nearest-even gives 2052 and truncation gives 2050 —
- and 2049 is carried beside it as a midpoint where both rules agree, to show that a midpoint is
- not automatically a test. What is needed to use them is a 16-bit render target in `air_run` and a
- `--check` that reads a half back from it; until then the rounding fix above is measured for
- 32-bit destinations only, and the patch's header says so.
+ The pool has the value that would decide it. In `f16` integers are exact to 2048 and spaced by 2
+ above, so **2051** is the discriminator — between 2050 and 2052 with an odd lower neighbour,
+ where round-to-nearest-even gives 2052 and truncation gives 2050 — and 2049 is carried beside it
+ as a midpoint where both rules agree, to show that a midpoint is not automatically a test.
+
+ **`air_run` now has the 16-bit render target, and it is not enough.** Picking
+ `R16G16B16A16_FLOAT` for a `half` output was necessary and was only one of two causes; the other
+ is in the generator, which packs every float-ish result into a `float4` with an explicit
+ `fpext half ... to float` (`intrincheck.py`, `emit_buffered`). The widening therefore happens
+ inside the shader and no choice of target can undo it.
+
+ **Going further would mean guessing, so it stops here.** A half-returning fragment shader needs
+ `!"air.render_target"` metadata naming the type, and the corpus is the authority on that
+ vocabulary — the whole `air.*` meaning was read out of `AGXCompilerCore` rather than invented. Of
+ the 456 modules, the render-target type names used are `float4` (275), `int4` (268), `uint4` (268)
+ and `ushort` (2). **There is no `half4` among them.** The token does occur once somewhere in the
+ shared cache, which makes the spelling plausible and attests nothing about this use of it.
+
+ And the spelling is the smaller problem. A `half` render target appears in **no** module of the
+ corpus, so a shader built around one would exercise a path through this frontend that Apple's own
+ shaders never take and that nothing else here tests. A disagreement would then sit between two
+ explanations — the GPU rounds wrong, or this frontend mishandles a construct it has never seen —
+ and an ambiguous measurement is worth less than a stated gap. So the ten declarations stay set
+ aside, and **the rounding fix is measured for 32-bit destinations only**, which is what the
+ patch's header claims and no more.
**Which rule it is, as against merely "not round-to-nearest", is settled by one value.**
`0x01000003` is 2²⁴+3, half way between the representable 2²⁴+2 and 2²⁴+4; round-half-to-even and
diff --git a/tools/air/intrincheck.py b/tools/air/intrincheck.py
index 1d6b7f4..a6deff1 100755
--- a/tools/air/intrincheck.py
+++ b/tools/air/intrincheck.py
@@ -1375,9 +1375,11 @@ def check_log(outdir, logpath, verbose):
"agreement about very little: %s"
% (len(thin), ", ".join("%s (%d)" % (k, per_decl[k]["n"]) for k in thin)))
if half_set_aside:
- print("intrincheck: %d declarations set aside: they return half, and air_run reads "
- "back through a 32-bit float target, so the half conversion is optimised away "
- "and the reference describes an operation the shader does not perform (%s)"
+ print("intrincheck: %d declarations set aside: they return half, which this suite packs into a "
+ "float4 output (an fpext in emit_buffered), so the conversion is widened inside "
+ "the shader and the reference describes an operation it does not perform. "
+ "air_run has the 16-bit target; the generator is what still needs a half "
+ "render target, and the corpus attests no type name for one (%s)"
% (len(half_set_aside), ", ".join(sorted(half_set_aside))))
denorm = sum(v["denorm"] for v in per_decl.values())
if denorm:
diff --git a/tools/mesa/air/air_run.c b/tools/mesa/air/air_run.c
index cea22fa..afff189 100644
--- a/tools/mesa/air/air_run.c
+++ b/tools/mesa/air/air_run.c
@@ -45,6 +45,7 @@
#include <xf86drm.h>
#endif
#include "util/format/u_format.h"
+#include "util/half_float.h"
#include "util/u_inlines.h"
#include "util/u_sampler.h"
#include "util/u_debug.h"
@@ -573,6 +574,7 @@ main(int argc, char **argv)
*/
enum pipe_format rt_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
bool rt_is_float = true;
+ bool rt_is_half = false;
/* Which attachment to read back: the lowest one the shader writes, not
* target 0. clearFragment04 writes FRAG_RESULT_DATA1 alone, and target 0
* then holds the clear colour -- a correct result that looks exactly like
@@ -595,6 +597,33 @@ main(int argc, char **argv)
*/
rt_format = PIPE_FORMAT_R32G32B32A32_UINT;
rt_is_float = false;
+ } else if (base == GLSL_TYPE_FLOAT16) {
+ /* A half return needs a half target. Through a 32-bit one the half is
+ * widened the moment it is written, and NIR then folds the conversion
+ * that produced it into a 32-bit one: the generated Adreno code for
+ * air.convert.f.f16.u.i32 was four cov.u32f32 and not one cov.u32f16,
+ * so the shader computed f32(x) while the reference computed f16(x)
+ * and the comparison meant nothing. Ten declarations of the corpus
+ * return half and every one of them was being reported as agreeing on
+ * the strength of a case that was never compared (docs/air.md,
+ * section 4 item 4).
+ *
+ * The chain is still u32 -> f32 -> f16 rather than a single
+ * conversion wherever NIR widens before the store, so a value needing
+ * to round at both steps can differ from one direct conversion by
+ * double rounding. None of the pool's values does, but a future one
+ * might, and that is a property of the harness and not of the GPU.
+ */
+ /* As of now nothing reaches this branch: tools/air/intrincheck.py packs
+ * every float-ish result into a float4, so no generated shader declares
+ * a half output. It is here because the target was one of the two things
+ * missing and is the half that belongs on this side; the other half needs
+ * a half render target in the generator, whose air.arg_type_name the
+ * corpus does not attest (docs/air.md, section 4 item 4).
+ */
+ rt_format = PIPE_FORMAT_R16G16B16A16_FLOAT;
+ rt_is_float = true;
+ rt_is_half = true;
} else {
rt_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
rt_is_float = true;
@@ -885,7 +914,9 @@ main(int argc, char **argv)
printf("air_run: the pixel at (0, 0) of render target %u came back as:\n",
read_rt);
for (unsigned c = 0; c < 4; c++) {
- if (rt_is_float)
+ if (rt_is_half)
+ printf(" %.9g\n", (double)_mesa_half_to_float(((uint16_t *)pix)[c]));
+ else if (rt_is_float)
printf(" %.9g\n", (double)((float *)pix)[c]);
else
printf(" %u\n", ((uint32_t *)pix)[c]);