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: the texture calls on more than one texel, which (1,1) alone could not see

vcvkka1d4d9c44729
diff --git a/docs/air.md b/docs/air.md
index 2eadbe3..4872208 100644
--- a/docs/air.md
+++ b/docs/air.md
@@ -33,7 +33,9 @@ And of the 119 distinct `air.*` operations those modules call (`tools/air/intrin
| of those, exactly defined and agreeing on every case (45 607 cases) | 71 |
| of those, approximate, so measured against libm and nothing concluded | 7 |
| of those, also *run* by a Gallium driver rather than folded (softpipe, 2 586 cases) | 47 |
-| textures, atomics, barriers, derivatives and simd: no reference here | 41 |
+| the atomics and the barrier, against a kernel written for them (`atomiccheck.py`) | 10 |
+| texture operations checked exactly, on several texels each (`texopcheck.py`) | 6 |
+| texture operations with no check: integer-typed, multisample, write, depth | 25 |
313 of 456 is 313 of 321 entry points — 97.5% — because 135 modules are `air.visible` library
functions and not entry points at all.
@@ -61,10 +63,19 @@ functions and not entry points at all.
however the lowering is written. That needs the phone. Two things are settled without running
anything: every atomic in the corpus asks for `memory_order_relaxed`, and a stronger order is
now refused by name instead of ignored, which is what the lowering used to do.
-- **Still unverified by anything: the derivatives and the simd reductions** — what they mean
- depends on neighbouring fragments or on the width of a simdgroup, and a folded constant cannot
- say it. The texture operations are covered differently and partly, by the six shaders that
- returned the exact texel on the Adreno.
+- **Every one of the 119 declarations the corpus calls now has some numerical check**, which is
+ new as of tonight and is the first time that has been true: 78 arithmetic ones against C's own
+ functions, 10 atomics and the barrier against a kernel written for them, and 31 texture ones
+ partly — 6 declarations exactly, on several texels each (section 4, item 6), and the rest not,
+ for reasons given there. The derivatives and the simd reductions have no check either, and no
+ module of the corpus calls them: they are unexercised code, not untested behaviour, and saying
+ "unverified" of them would overstate what is at stake.
+- **The weakest of the three is the textures**, and the gap is not in the fetch but in everything
+ Metal's sampler state decides. The 64-bit `constexpr sampler` word is still not decoded
+ (section 3.3), every coordinate checked is a texel centre where nearest filtering has no
+ weights to round, and the integer-typed reads and samples are not checked at all because
+ `air_run` binds one float texture and a shader declaring an integer one against it is a type
+ mismatch whose answer would mean nothing.
- **Nothing here tests Metal's own filtering or addressing.** The six texture shaders that drew on
the Adreno took their sampler from the API, which this harness chose: nearest, clamped, sampled
at a texel centre. That is evidence about binding, descriptors and ir3's texture instructions —
@@ -596,6 +607,43 @@ Three references, each independent of the translator:
those stay untested because the names Apple would emit for them are a guess, and a test of a
guessed name is a test of the guess.
+6. **The texture operations, on more than one texel.** `tools/air/texcheck.sh` runs the corpus's
+ own texture shaders and 6 of its 55 return the exact texel on the Adreno; 49 cannot be run,
+ most of them because they are `air.visible` library functions and not entry points. Two things
+ that leaves undone, and the second is the serious one:
+
+ - whole operations have no coverage from it at all — the texel fetch, the size queries;
+ - **every one of those six samples the same texel**, `(1, 1)`, because that is where
+ `air_run`'s vertex shader puts the coordinate. A lowering that ignored the coordinate
+ entirely and always fetched `(1, 1)` would pass all six of them.
+
+ `tools/air/texopcheck.py` generates a fragment shader per operation *and coordinate*, asking
+ for the corners as well as the middle. **All 17 calls return the exact texel**, covering six
+ declarations: `read_texture_2d` with 32- and with 16-bit coordinates, `get_width_texture_2d`,
+ `get_height_texture_2d`, `sample_texture_2d` and `sample_texture_3d`. The coordinates are
+ immediates here, unlike the arithmetic: a texture fetch is not constant-folded by NIR or by
+ ir3, so there is nothing for the compiler to answer in the shader's place and no buffer is
+ needed.
+
+ Made to fail: swapping the first two coordinate components is caught on all five asymmetric
+ coordinates and correctly *not* on the three symmetric ones, which is the difference between
+ this and `texcheck.sh` stated as a measurement rather than an argument.
+
+ **What it does not check**, and these are the texture half's real remaining gaps:
+
+ - Metal's own sampler state. The sampler is the API's — nearest, clamped — and every
+ coordinate is a texel centre, where nearest filtering has no weights to round and clamping
+ has no arithmetic. The 64-bit `constexpr sampler` word remains undecoded (section 3.3).
+ - The integer-typed reads and samples (`read_texture_2d.s.v4i16`,
+ `sample_texture_2d.u.v4i32` and the rest, 14 declarations). `air_run` binds one
+ `r32g32b32a32_float` texture, and a shader declaring an integer texture against it is a type
+ mismatch whose result would say nothing about the lowering. Binding a texture whose format
+ follows the declaration is a change to `air_run` that is not made.
+ - `write_texture_2d`, which needs the texture read back after the draw, and the multisample
+ reads, which need a multisample texture. Neither is bound.
+ - The depth sample: nothing binds a depth texture, and a shader reading an attachment nothing
+ stored to is one of the four tests this port has already had pass for the wrong reason.
+
Where a shader has a known GLSL equivalent (AppleMetalGLRenderer's shaders *are* the GL
pipeline's fixed functions), rendering both through the port's GLES and comparing images is a
fourth check; it is not built yet.
@@ -927,6 +975,7 @@ tools/air/atomiccheck.py [--keep DIR] # the atomics, with i
build/mesa-host/build/src/gallium/drivers/freedreno/air_run \
FILE[.metallib] [FUNCTION] # draw with it, print the pixel
tools/air/texcheck.sh [FILE.metallib ...] # every texture shader, the texel it returns
+tools/air/texopcheck.py [-v] [--keep DIR] # the texture calls, several texels each
```
`intrincheck.py` with no argument checks every operation the corpus calls; with one or more names
diff --git a/tools/air/check.sh b/tools/air/check.sh
index d3a91f4..076f811 100755
--- a/tools/air/check.sh
+++ b/tools/air/check.sh
@@ -101,4 +101,14 @@ echo "== the atomics, with a kernel written to make a lost update show"
python3 "$ROOT/tools/air/atomiccheck.py" 2>&1 | tail -8
atomic_rc=${PIPESTATUS[0]}
-exit $((reader_rc | cpu_rc | intrin_rc | run_rc | atomic_rc))
+echo
+echo "== the texture operations, on more than one texel"
+# texcheck.sh runs the corpus's own texture shaders, which all sample texel
+# (1, 1) because that is where air_run's vertex shader puts the coordinate: a
+# lowering that ignored the coordinate and always fetched (1, 1) would pass all
+# six of them. These ask for the corners too, so swapping x and y is caught on
+# the five asymmetric coordinates and correctly not on the symmetric ones.
+python3 "$ROOT/tools/air/texopcheck.py" 2>&1 | tail -6
+texop_rc=${PIPESTATUS[0]}
+
+exit $((reader_rc | cpu_rc | intrin_rc | run_rc | atomic_rc | texop_rc))
diff --git a/tools/air/texopcheck.py b/tools/air/texopcheck.py
new file mode 100755
index 0000000..39093c9
--- /dev/null
+++ b/tools/air/texopcheck.py
@@ -0,0 +1,285 @@
+#!/usr/bin/env python3
+"""Check the texture operations one at a time, on more than one texel.
+
+tools/air/texcheck.sh runs the corpus's own texture shaders, and 6 of its 55
+return the exact texel on the Adreno. Two things it cannot do:
+
+ - Most of the other 49 are air.visible library functions, which the harness
+ cannot invoke as entry points, so whole operations -- the texel fetch, the
+ size queries -- have no coverage at all from it.
+ - Every one of them samples the same texel, (1, 1), because that is where
+ air_run's vertex shader puts the coordinate. A lowering that ignored the
+ coordinate and always fetched (1, 1) would pass all six.
+
+So this generates a fragment shader per (operation, coordinate), the way
+tools/air/intrincheck.py does for the arithmetic, and asks for several texels
+including the corners. air_run fills texel (x, y, z) of slot n with
+(100 + x + 1000n, 200 + y, 300 + x + y + z, 400), so every coordinate has a
+different expected answer in all four channels and a confusion between any two
+of them gives a wrong number rather than a plausible one.
+
+Nothing is read from the corpus but the list of declarations, and the
+coordinates are immediates here rather than loaded from a buffer: a texture
+fetch is not constant-folded by either NIR or ir3, so there is nothing for the
+compiler to answer in the shader's place. That is the difference from the
+arithmetic, where the buffer was necessary.
+
+What is NOT checked, and why:
+
+ - Metal's own sampler state. air_run's sampler is the API's -- nearest,
+ clamped -- and the 64-bit constexpr sampler word is not decoded
+ (docs/air.md section 3.3). The coordinates here are texel centres, where
+ nearest filtering has no weights to round, so this says nothing about
+ filtering or addressing.
+ - The integer-typed reads and samples (read_texture_2d.s.v4i16,
+ sample_texture_2d.u.v4i32 and the rest). air_run binds one
+ r32g32b32a32_float texture, and a shader declaring an integer texture
+ against it is a type mismatch whose result would say nothing about the
+ lowering. Binding a texture whose format follows the declaration is a
+ change to air_run that is not made here.
+ - write_texture_2d and the multisample reads: the first needs the texture
+ read back, which air_run does not do, and the second a multisample
+ texture, which it does not bind.
+ - The depth sample: air_run binds no depth texture, and a shader reading an
+ attachment nothing stored to is one of the four tests this port has
+ already had pass for the wrong reason.
+
+usage: texopcheck.py [--keep DIR] [-v]
+"""
+
+import os
+import re
+import subprocess
+import sys
+import tempfile
+
+ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+AIR_RUN = os.environ.get(
+ "AIR_RUN",
+ os.path.join(ROOT, "build/mesa-host/build/src/gallium/drivers/freedreno/air_run"),
+)
+
+TEX = 4 # air_run binds a TEX x TEX (x TEX) texture
+
+
+def texel(x, y, z=0):
+ """What air_run put at (x, y, z) of slot 0."""
+ return [100 + x, 200 + y, 300 + x + y + z, 400]
+
+
+SKELETON = """source_filename = "tex"
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
+target triple = "air64-apple-ios16.5.0"
+
+%(decl)s
+
+define <{ <4 x float> }> @tex(%(params)s) local_unnamed_addr {
+%(body)s
+ %%out = insertvalue <{ <4 x float> }> undef, <4 x float> %%v, 0
+ ret <{ <4 x float> }> %%out
+}
+
+!llvm.module.flags = !{!0}
+!air.version = !{!1}
+!air.language_version = !{!2}
+!air.fragment = !{!3}
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 2, i32 5, i32 0}
+!2 = !{!"Metal", i32 3, i32 0, i32 0}
+!3 = !{ptr @tex, !4, !6}
+!4 = !{!5}
+!5 = !{!"air.render_target", i32 0, i32 0, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"}
+!6 = !{%(args)s}
+%(argmd)s
+"""
+
+TEXTURE_MD = ('!{i32 %d, !"air.texture", !"air.location_index", i32 0, i32 1, !"%s", '
+ '!"air.arg_type_name", !"texture%s", !"air.arg_name", !"t"}')
+SAMPLER_MD = ('!{i32 %d, !"air.sampler", !"air.location_index", i32 0, i32 1, '
+ '!"air.arg_type_name", !"sampler", !"air.arg_name", !"s"}')
+
+
+def build(decl, call, needs_sampler, kind, access):
+ """One shader: `call` computes %v from %t (and %s), `decl` declares it."""
+ params = ["ptr addrspace(1) %t"]
+ md = [TEXTURE_MD % (0, access, kind)]
+ if needs_sampler:
+ params.append("ptr addrspace(2) %s")
+ md.append(SAMPLER_MD % 1)
+ return SKELETON % dict(
+ decl=decl, params=", ".join(params), body=call,
+ args=", ".join("!%d" % (7 + i) for i in range(len(md))),
+ argmd="\n".join("!%d = %s" % (7 + i, m) for i, m in enumerate(md)))
+
+
+def cases(decls):
+ """Everything to try: (label, source, expected four floats)."""
+ out = []
+ have = {}
+ for d in decls:
+ m = re.match(r"^declare\s+(.*?)\s+@(air\.[A-Za-z0-9_.]+)\((.*)\)", d)
+ if m:
+ have[m.group(2)] = d
+
+ # -- the texel fetch, at four corners and the middle of the 4x4 ---------
+ name = "air.read_texture_2d.v4f32"
+ if name in have:
+ for (x, y) in [(0, 0), (1, 1), (2, 3), (3, 3), (3, 0)]:
+ body = (' %%r = tail call { <4 x float>, i8 } @%s(ptr addrspace(1) %%t, '
+ '<2 x i32> <i32 %d, i32 %d>, i32 0, i32 0)\n'
+ ' %%v = extractvalue { <4 x float>, i8 } %%r, 0' % (name, x, y))
+ out.append(("%s (%d,%d)" % (name, x, y),
+ build(have[name], body, False, "2d<float, access::read>",
+ "air.read"),
+ texel(x, y)))
+
+ # the same fetch with 16-bit coordinates, which is a separate declaration
+ name = "air.read_texture_2d.i16.v4f32"
+ if name in have:
+ for (x, y) in [(1, 1), (2, 3)]:
+ body = (' %%r = tail call { <4 x float>, i8 } @%s(ptr addrspace(1) %%t, '
+ '<2 x i16> <i16 %d, i16 %d>, i16 0, i32 0)\n'
+ ' %%v = extractvalue { <4 x float>, i8 } %%r, 0' % (name, x, y))
+ out.append(("%s (%d,%d)" % (name, x, y),
+ build(have[name], body, False, "2d<float, access::read>",
+ "air.read"),
+ texel(x, y)))
+
+ # -- the size queries ---------------------------------------------------
+ for name, want in (("air.get_width_texture_2d", TEX),
+ ("air.get_height_texture_2d", TEX)):
+ if name not in have:
+ continue
+ body = (' %%n = tail call i32 @%s(ptr addrspace(1) %%t, i32 0)\n'
+ ' %%f = sitofp i32 %%n to float\n'
+ ' %%v0 = insertelement <4 x float> undef, float %%f, i32 0\n'
+ ' %%v1 = insertelement <4 x float> %%v0, float %%f, i32 1\n'
+ ' %%v2 = insertelement <4 x float> %%v1, float %%f, i32 2\n'
+ ' %%v = insertelement <4 x float> %%v2, float %%f, i32 3' % name)
+ out.append((name, build(have[name], body, False,
+ "2d<float, access::read>", "air.read"),
+ [want] * 4))
+
+ # -- sampling, at the centre of several texels --------------------------
+ name = "air.sample_texture_2d.v4f32"
+ if name in have:
+ for (x, y) in [(0, 0), (1, 1), (2, 3), (3, 3)]:
+ u, v = (x + 0.5) / TEX, (y + 0.5) / TEX
+ body = (' %%r = tail call { <4 x float>, i8 } @%s(ptr addrspace(1) %%t, '
+ 'ptr addrspace(2) %%s, <2 x float> <float %.9e, float %.9e>, '
+ 'i1 true, <2 x i32> zeroinitializer, i1 false, float 0.0, '
+ 'float 0.0, i32 0)\n'
+ ' %%v = extractvalue { <4 x float>, i8 } %%r, 0' % (name, u, v))
+ out.append(("%s (%d,%d)" % (name, x, y),
+ build(have[name], body, True, "2d<float, sample>",
+ "air.sample"),
+ texel(x, y)))
+
+ name = "air.sample_texture_3d.v4f32"
+ if name in have:
+ for (x, y, z) in [(0, 0, 0), (1, 1, 1), (2, 3, 1), (3, 3, 3)]:
+ u, v, w = ((x + 0.5) / TEX, (y + 0.5) / TEX, (z + 0.5) / TEX)
+ body = (' %%r = tail call { <4 x float>, i8 } @%s(ptr addrspace(1) %%t, '
+ 'ptr addrspace(2) %%s, <3 x float> <float %.9e, float %.9e, '
+ 'float %.9e>, i1 true, <3 x i32> zeroinitializer, i1 false, '
+ 'float 0.0, float 0.0, i32 0)\n'
+ ' %%v = extractvalue { <4 x float>, i8 } %%r, 0' % (name, u, v, w))
+ out.append(("%s (%d,%d,%d)" % (name, x, y, z),
+ build(have[name], body, True, "3d<float, sample>",
+ "air.sample"),
+ texel(x, y, z)))
+ return out
+
+
+def collect_declarations():
+ air2nir = os.environ.get(
+ "AIR2NIR", os.path.join(ROOT, "build/mesa-host/build/src/compiler/air/air2nir"))
+ out = set()
+ for dirpath, _, names in os.walk(os.path.join(ROOT, "ios165/rootfs")):
+ for n in sorted(names):
+ if not n.endswith(".metallib"):
+ continue
+ ll = subprocess.run([air2nir, "--ll", os.path.join(dirpath, n)],
+ capture_output=True).stdout
+ for line in ll.decode("utf-8", "replace").splitlines():
+ if line.startswith("declare ") and "@air." in line:
+ line = re.sub(r"\s+local_unnamed_addr.*$", "", line)
+ out.add(re.sub(r"\s+#\d+$", "", line).strip())
+ return sorted(out)
+
+
+def run(d, i, source):
+ ll, bc = os.path.join(d, "t%03d.ll" % i), os.path.join(d, "t%03d.bc" % i)
+ with open(ll, "w") as f:
+ f.write(source)
+ q = subprocess.run(["llvm-as", "-o", bc, ll], capture_output=True)
+ if q.returncode:
+ return None, "llvm-as: " + q.stderr.decode().strip().splitlines()[0]
+ q = subprocess.run([AIR_RUN, bc], capture_output=True)
+ out = q.stdout.decode() + q.stderr.decode()
+ i = out.find("came back as")
+ if i < 0:
+ for line in out.splitlines():
+ if any(w in line for w in ("this harness", "Assertion", "error",
+ "does not bind", "stores nothing")):
+ return None, line.strip()
+ return None, (out.strip().splitlines() or ["no output"])[-1]
+ vals = []
+ for line in out[i:].splitlines()[1:]:
+ t = line.strip()
+ if not re.match(r"^-?[0-9]", t):
+ break
+ vals.append(float(t))
+ return vals, None
+
+
+def main(argv):
+ keep = None
+ verbose = "-v" in argv
+ if "--keep" in argv:
+ keep = argv[argv.index("--keep") + 1]
+ if not os.path.exists(AIR_RUN):
+ print("texopcheck: no air_run at %s (run tools/air/mk-host.sh)" % AIR_RUN,
+ file=sys.stderr)
+ return 1
+
+ todo = cases(collect_declarations())
+ if not todo:
+ print("texopcheck: no texture declaration in the corpus has a case here",
+ file=sys.stderr)
+ return 1
+
+ d = keep or tempfile.mkdtemp(prefix="texopcheck.")
+ os.makedirs(d, exist_ok=True)
+ exact = other = refused = 0
+ screen = None
+ for i, (label, source, want) in enumerate(todo):
+ got, err = run(d, i, source)
+ if got is None:
+ print("refused %-44s %s" % (label, err[:80]))
+ refused += 1
+ continue
+ if [float(w) for w in want] == got[:4]:
+ exact += 1
+ if verbose:
+ print("EXACT %-44s %s" % (label, " ".join("%g" % g for g in got[:4])))
+ else:
+ other += 1
+ print("WRONG %-44s got %s want %s"
+ % (label, " ".join("%g" % g for g in got[:4]),
+ " ".join("%g" % w for w in want)))
+
+ print()
+ print("texopcheck: %d texture calls run: %d returned the exact texel, %d did not, "
+ "%d refused" % (len(todo), exact, other, refused))
+ print("texopcheck: the coordinates differ between calls, so a lowering that ignored")
+ print("texopcheck: them and always fetched (1,1) would fail here and not in texcheck.sh")
+ print("texopcheck: nothing here tests Metal's sampler state: the sampler is the API's")
+ print("texopcheck: (nearest, clamped) and the 64-bit sampler word is not decoded")
+ if keep:
+ print("texopcheck: kept in %s" % d)
+ return 1 if (other or refused) else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main(sys.argv[1:]))