diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index e787f7c5c..ef7761ae5 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -64,6 +64,8 @@ jobs: run: bash tools/ios27/fullroot/firmware-test.sh - name: One iOS 27 root build at a time per work directory and per image (the lock, in a scratch directory) run: bash tools/ios27/fullroot/build-lock-test.sh + - name: "Metal frontend: a synchronous pipeline call always compiles and returns its result (no timeout refusal), completion handlers run on at most 3 workers, each once (host test of CompileQueue.h)" + run: sh tools/ios27/metal2/tests/run.sh - name: The backup tool only reads, copies exactly, and proves every copy run: python3 tools/backup-partitions-test.py - name: Saved var archives use the newest intact slot diff --git a/.gitignore b/.gitignore index 23bcd235a..0f78c001c 100644 --- a/.gitignore +++ b/.gitignore @@ -162,3 +162,4 @@ __pycache__/ # the loader S-Boot starts, as built (tools/ios27/s9/build-loader.sh) /xnuloader/sboot/out/ +tools/ios27/fullroot/warm-m2v/ diff --git a/docs/ios27/METAL.md b/docs/ios27/METAL.md index 23700e3af..036889621 100644 --- a/docs/ios27/METAL.md +++ b/docs/ios27/METAL.md @@ -94,3 +94,42 @@ profile of the textured case the per-draw path is spread over the runtime (`draw thirds of `end_pass` for scissors that differ every draw (it marks the bin grid one tile at a time; a draw whose scissor covers the whole pass skips it, as most of CoreAnimation's main-pass draws should). The M1 runs this single-threaded code several times faster than the S9's M3 at 1794 MHz. + +## Shader compiles: off the UI, bounded, shared and warm (19 Nov 2026) + +What an iOS 27 boot on the S9 showed (phone 2, `con-xnu-boot-L4-1118-2103.log`): every Settings open compiled the same 24 522-NIR compute +shader (250 224 bytes) for 24 to 28 s on one core with the GPU idle and the UI stalled; Preferences then began a fragment shader of 3 023 +NIR instructions that never ended (Preferences at 130 %, launchd at 40 %, for more than three minutes). The causes and what changed: + +| Cause | Change | +|---|---| +| PanVK compiled on the thread that asked for the pipeline, and the completion-handler `new...PipelineState` calls ran the compile on the caller too | `patches/frontend-async-compile.diff` + `metal2/CompileQueue.h` (second version, 20 Nov): the synchronous calls compile on the calling thread, every time, with no timeout, and return the pipeline or the compile's own error; the completion-handler calls return at once and compile on a worker (at most three at a time, FIFO, at the QoS of the caller), each handler called once; an asynchronous compile that failed after more than 10 s is not started again for 10 minutes. The rules and their host test: `tools/ios27/metal2/tests/run.sh` (also in CI) | +| A compile with no end: Bifrost's register allocator spills a node (or, past four rounds, up to 32) per round and rebuilds the interference graph each time | `mesa26-panvk-compile-budget.patch`: `STARBITE_PANVK_COMPILE_BUDGET_MS` (default 90 000, 0 = none) is a budget of one compile in the compiling thread's CPU time (not wall time: a compile that only waits for a core is not a runaway); the allocator's rounds, its LCRA solve and its liveness loop check it and leave the compile by `longjmp`; the pipeline fails (`PANVK: ... abandoned after N ms (compile budget)` on the console) | +| One cache directory per program (`/var/local/m2v-cache/`), so only backboardd's was ever filled, a sandboxed program fell back to its own caches directory, and Preferences or SpringBoard never saw what another had compiled | `patches/hotmetal2-shared-cache.diff`: one directory per user, `/var/local/m2v-cache/u` (the runtime's files are owner-only), for every process; `STARBITE_M2V_CACHE=/dir` (mtl2-env) names another | +| The directory was made by the first process to run, with nobody to ship anything in it | `steps.d/81-m2v-cache.sh` makes `/var/local/m2v-cache` (0777), removes the per-program directories, and installs a warm cache (below) | + +**Why the first version was reverted (#397).** It timed the synchronous calls out after `STARBITE_M2V_COMPILE_TIMEOUT_MS` (5000) and +failed them like a refused pipeline while the compile went on. On phone 2 (19 Nov, `con-xnu-boot-L6-1119-0030.log`) a cold cache made +backboardd's first render pipelines take longer than that; QuartzCore treats any refused render pipeline as fatal ("Metal failed to +build render pipeline"), backboardd aborted three times and launchd rebooted, twice. A synchronous Metal call cannot be answered "ask +again later", so the second version has no timeout at all on that path (CompileQueue.h, rule 1). With the timeout set to 0 (boot +`-0039.log`) nothing crashed but the screen stayed on the splash after six framebuffer swaps: the kernel's samples of that boot show +backboardd idle (render server and IOMFB threads in `mach_msg`, no thread in a compile) and SpringBoard's main thread inside a +synchronous XPC message to chronod (`-[CHSChronoServicesConnection _subscribeToExtensions]`) at 00:41:02 and 00:42:02, so SpringBoard +committed nothing new; neither SpringBoard nor chronod had opened the Metal device. That stall is not explained by the frontend; the +phone test of the second version (in its PR) samples chronod too (`sweetsample=`) in case it comes back. + +**A warm cache.** The runtime's entries are named by content: translated functions and pipelines under `v1//` (the build is the +bridge library's), PanVK's pipeline data in the content directory beside them (named by the SPIR-V, the pipeline state and PanVK's cache +UUID, `driver.rs`). PanVK itself cannot compile on the Mac for the Mali-G72 (it needs the kernel's GPU), so a warm cache is harvested from a +phone: boot the bundle that will ship, open Settings, Display, Appearance (Liquid Glass), the app switcher and Control Center until the +`PANVK: ... NIR instructions` lines stop, copy `/private/var/local/m2v-cache` off, run `tools/ios27/metal2/harvest-m2v-cache.sh PULLED` +(it keeps the newest build directory and the content directory of each `u/`, into `tools/ios27/fullroot/warm-m2v`, which is not in git), +and build the root: step 81 installs it, owner-only, owned by the uid. It holds for that bundle and that PanVK (about 33 MB); another +build finds none of it and compiles as before. + +**Format 500 is `MTLPixelFormatYCBCR8_420_2P`** (metal2vulkan's `generated.rs`), not an unknown format: the runtime has it (`pixel_format_info.rs`, +`bind/ycbcr_convert`, converted in the shader), and the frontend's texture over an IOSurface passes the surface's YCbCr matrix on. The +`texture memory fmt 500` line is that path (`how` is "memory" for textures over an IOSurface or a buffer: they have no device upload, by +design, so no `texture device` line follows), and nothing was refused. Why the reference image did not draw is not in that log: the pipelines that sample it may simply not have been made yet (the compute and +fragment compiles above were still running), which the two changes above address; a phone run of the new bundle will say. diff --git a/tools/ios27/fullroot/steps.d/81-m2v-cache.sh b/tools/ios27/fullroot/steps.d/81-m2v-cache.sh new file mode 100755 index 000000000..012540da4 --- /dev/null +++ b/tools/ios27/fullroot/steps.d/81-m2v-cache.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# 81-m2v-cache.sh ROOT : the Metal shader cache directory, and a warm cache in it. +# The plug-in (StarbiteMetal.bundle, HotMetal2.mm + patches/hotmetal2-shared-cache.diff) keeps one cache per USER, shared by every +# process of that user: /private/var/local/m2v-cache/u/ (v1// = translated shaders, v1// = PanVK's pipeline data; +# all named by content). It makes the directory itself, but only where /private/var/local/m2v-cache exists and is writable: this +# step makes it (0777), and removes the per-program directories (backboardd/, PosterBoard/, ...) older plug-ins left. +# WARM_M2V_CACHE= (default tools/ios27/fullroot/warm-m2v when it exists) holds u/ directories as harvest-m2v-cache.sh +# makes them from a phone's cache after a warm-up (Settings, Display, Liquid Glass, the app switcher...): they are copied in, owned +# by that uid, owner-only (the runtime refuses files others can write), so the first Settings open compiles nothing. +# A warm cache is valid for the bundle and PanVK it was harvested with (the entries live under the bridge's build hash and +# PanVK's cache UUID); another build finds none of it and compiles as before, leaving the stale files to the runtime's 30-day sweep. +# WARM_M2V_CACHE=none: no warm cache (and the existing directories are left as they are). +set -euo pipefail +R=$1 +H=$(cd "$(dirname "$0")" && pwd) +L=$R/private/var/local +C=$L/m2v-cache +install -d -m 0777 "$L" 2>/dev/null || mkdir -p "$L" +install -d -m 0777 "$C" 2>/dev/null || mkdir -p "$C" +chmod 0777 "$C" +# a plug-in before the shared cache: one directory per program +for d in "$C"/*/; do + [[ -d $d ]] || continue + n=$(basename "$d") + case $n in u[0-9]*) ;; *) rm -rf "$d"; echo "81-m2v-cache: removed the per-program cache $n";; esac +done +WARM=${WARM_M2V_CACHE:-$H/../warm-m2v} +if [[ $WARM == none || ! -d $WARM ]]; then + echo "81-m2v-cache: $C ready (0777); no warm cache (WARM_M2V_CACHE unset or $H/../warm-m2v absent)" + exit 0 +fi +files=0 +for u in "$WARM"/u[0-9]*/; do + [[ -d $u ]] || continue + uid=$(basename "$u"); uid=${uid#u} + rm -rf "$C/u$uid" + mkdir -p "$C/u$uid" + cp -R "$u." "$C/u$uid/" + find "$C/u$uid" -type d -exec chmod 0700 {} + + find "$C/u$uid" -type f -exec chmod 0600 {} + + chown -R "$uid:$uid" "$C/u$uid" 2>/dev/null || echo "81-m2v-cache: u$uid: chown to $uid failed (not root?): the files stay with the building user" + files=$((files + $(find "$C/u$uid" -type f | wc -l))) +done +echo "81-m2v-cache: warm cache from $WARM: $files files, $(du -sk "$C" | cut -f1) KiB in $C" diff --git a/tools/ios27/mesa-ios/mesa26-panvk-compile-budget.patch b/tools/ios27/mesa-ios/mesa26-panvk-compile-budget.patch new file mode 100644 index 000000000..fe05de43a --- /dev/null +++ b/tools/ios27/mesa-ios/mesa26-panvk-compile-budget.patch @@ -0,0 +1,155 @@ +diff -ru a/src/panfrost/compiler/bifrost/bi_ra.c b/src/panfrost/compiler/bifrost/bi_ra.c +--- a/src/panfrost/compiler/bifrost/bi_ra.c ++++ b/src/panfrost/compiler/bifrost/bi_ra.c +@@ -7,6 +7,7 @@ + #include "bi_debug.h" + #include "bi_builder.h" + #include "compiler.h" ++#include "pan_compiler.h" + #include "nodearray.h" + #include "valhall.h" + +@@ -145,6 +146,8 @@ + lcra_solve(struct lcra_state *l) + { + for (unsigned step = 0; step < l->node_count; ++step) { ++ if ((step & 1023) == 1023) ++ PAN_COMPILE_CHECK(); + if (l->solutions[step] != ~0) + continue; + if (l->affinity[step] == 0) +@@ -262,7 +265,11 @@ + bi_worklist_push_tail(&worklist, block); + } + ++ unsigned pops = 0; + while (!u_worklist_is_empty(&worklist)) { ++ if ((++pops & 255) == 0) ++ PAN_COMPILE_CHECK(); ++ + /* Pop off in reverse order since liveness is backwards */ + bi_block *blk = bi_worklist_pop_tail(&worklist); + +@@ -1295,6 +1302,7 @@ + + /* Otherwise, use the register file and spill until we succeed */ + while (!success && ((iter_count++) < max_iters)) { ++ PAN_COMPILE_CHECK(); + l = bi_allocate_registers(ctx, &success, true); + + if (success) { +diff -ru a/src/panfrost/compiler/pan_compiler.c b/src/panfrost/compiler/pan_compiler.c +--- a/src/panfrost/compiler/pan_compiler.c ++++ b/src/panfrost/compiler/pan_compiler.c +@@ -4,6 +4,31 @@ + */ + + #include "pan_compiler.h" ++#ifdef __APPLE__ ++#include ++ ++__thread jmp_buf *pan_compile_abort_jmp = NULL; ++__thread uint64_t pan_compile_deadline_ns = 0; ++ ++void ++pan_compile_check_deadline(void) ++{ ++ if (!pan_compile_abort_jmp || !pan_compile_deadline_ns) ++ return; ++ ++ /* CPU time of this thread, not wall time: a compile that is only waiting for a core (a ++ * phone at 400 % with daemons, the worker on an A55) is not a runaway one and must not be ++ * abandoned; the caller may be backboardd, for which a failed render pipeline is fatal. */ ++ struct timespec t; ++ clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t); ++ uint64_t now = (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; ++ if (now > pan_compile_deadline_ns) { ++ jmp_buf *target = pan_compile_abort_jmp; ++ pan_compile_abort_jmp = NULL; ++ longjmp(*target, 1); ++ } ++} ++#endif + #include "pan_nir.h" + + #include "bifrost/bi_debug.h" +diff -ru a/src/panfrost/compiler/pan_compiler.h b/src/panfrost/compiler/pan_compiler.h +--- a/src/panfrost/compiler/pan_compiler.h ++++ b/src/panfrost/compiler/pan_compiler.h +@@ -81,6 +81,23 @@ + struct util_dynarray *binary, + struct pan_shader_info *info); + ++#ifdef __APPLE__ ++#include ++#include ++/* Starbite: a CPU-time budget for one compile on the calling thread. A caller that ++ * wants one (PanVK's panvk_compile_nir) sets pan_compile_abort_jmp and ++ * pan_compile_deadline_ns (CLOCK_THREAD_CPUTIME_ID, ns) around pan_shader_compile(); the register ++ * allocator, whose spill rounds are what a runaway compile is made of, calls ++ * PAN_COMPILE_CHECK() and past the deadline the compile is left by longjmp (what it ++ * allocated is lost: a few MB, once per abandoned shader). */ ++extern __thread jmp_buf *pan_compile_abort_jmp; ++extern __thread uint64_t pan_compile_deadline_ns; ++void pan_compile_check_deadline(void); ++#define PAN_COMPILE_CHECK() pan_compile_check_deadline() ++#else ++#define PAN_COMPILE_CHECK() ((void)0) ++#endif ++ + #define PAN_PRINTF_BUFFER_SIZE 16384 + + /* Any address with the top bit set is treated OOB by the hardware when +diff -ru a/src/panfrost/vulkan/panvk_vX_shader.c b/src/panfrost/vulkan/panvk_vX_shader.c +--- a/src/panfrost/vulkan/panvk_vX_shader.c ++++ b/src/panfrost/vulkan/panvk_vX_shader.c +@@ -1060,8 +1060,49 @@ + clock_gettime(CLOCK_MONOTONIC, &t0); + #endif + struct util_dynarray binary = UTIL_DYNARRAY_INIT; ++#ifdef __APPLE__ ++ /* Starbite: a compile that uses more than STARBITE_PANVK_COMPILE_BUDGET_MS (default 90 s, ++ * 0 for none) of its thread's CPU time is abandoned and its pipeline fails with a real ++ * compile error (an iOS 27 Preferences fragment shader of 3023 NIR instructions never ++ * ended, the process at 130% for minutes). CPU time, not wall time: the longest real ++ * compile seen (a 24522-NIR compute shader) took 25 s of CPU, and a compile that is only ++ * starved of a core keeps going. The Metal frontend runs synchronous creation calls on the ++ * caller's thread with no timeout (this error is what such a caller gets, after the ++ * budget) and completion-handler calls on a worker, where a pipeline that failed after ++ * 10 s is not retried for 10 minutes. */ ++ jmp_buf budget_jmp; ++ { ++ const char *benv = getenv("STARBITE_PANVK_COMPILE_BUDGET_MS"); ++ unsigned long budget_ms = benv ? strtoul(benv, NULL, 0) : 90000; ++ if (budget_ms) { ++ struct timespec c0; ++ clock_gettime(CLOCK_THREAD_CPUTIME_ID, &c0); ++ pan_compile_deadline_ns = (uint64_t)c0.tv_sec * 1000000000ull + ++ (uint64_t)c0.tv_nsec + budget_ms * 1000000ull; ++ if (setjmp(budget_jmp)) { ++ pan_compile_abort_jmp = NULL; ++ pan_compile_deadline_ns = 0; ++ util_dynarray_fini(&binary); ++ char msg[160]; ++ int fd = open("/dev/console", O_WRONLY | O_NOCTTY); ++ int n = snprintf(msg, sizeof(msg), ++ "PANVK: %s shader %.60s abandoned after %lu ms of CPU (compile budget)\n", ++ _mesa_shader_stage_to_string(nir->info.stage), ++ nir->info.name ? nir->info.name : "?", budget_ms); ++ if (fd >= 0) { ++ write(fd, msg, n > 0 ? (size_t)n : 0); ++ close(fd); ++ } ++ return VK_ERROR_OUT_OF_HOST_MEMORY; ++ } ++ pan_compile_abort_jmp = &budget_jmp; ++ } ++ } ++#endif + pan_shader_compile(nir, &input, &binary, &shader->info); + #ifdef __APPLE__ ++ pan_compile_abort_jmp = NULL; ++ pan_compile_deadline_ns = 0; + clock_gettime(CLOCK_MONOTONIC, &t1); + { + long ms = (t1.tv_sec - t0.tv_sec) * 1000 + (t1.tv_nsec - t0.tv_nsec) / 1000000; diff --git a/tools/ios27/metal/mk-bundle27.sh b/tools/ios27/metal/mk-bundle27.sh index 9ddfd09d4..cd837ed96 100755 --- a/tools/ios27/metal/mk-bundle27.sh +++ b/tools/ios27/metal/mk-bundle27.sh @@ -14,7 +14,7 @@ rm -rf "$B" && mkdir -p "$B" $W/sym $W/stage # the frontend and HotMetal2 are built from a copy with patches/frontend-dexr.diff, hotmetal2-diag.diff, frontend-flush.diff and # frontend-turned.diff, frontend-dexr-surfaces.diff, frontend-nocopy-diag.diff, frontend-passlog.diff and frontend-nocopy-readonly.diff applied, in that order (NOPATCH=1: as they are in tools/ios27/metal2) rm -rf $W/src27 && mkdir -p $W/src27/tools/ios27 && cp -R tools/ios27/metal2 $W/src27/tools/ios27/ -[ -n "${NOPATCH:-}" ] || for p in frontend-dexr hotmetal2-diag frontend-flush frontend-turned frontend-dexr-surfaces frontend-nocopy-diag frontend-passlog frontend-nocopy-readonly; do patch -s -p1 -d $W/src27 < tools/ios27/metal/patches/$p.diff || exit 1; done +[ -n "${NOPATCH:-}" ] || for p in frontend-dexr hotmetal2-diag frontend-flush frontend-turned frontend-dexr-surfaces frontend-nocopy-diag frontend-passlog frontend-nocopy-readonly frontend-async-compile hotmetal2-shared-cache; do patch -s -p1 -d $W/src27 < tools/ios27/metal/patches/$p.diff || exit 1; done M2=$W/src27/tools/ios27/metal2 # frontend-turned.diff includes TurnShaders.inc: tools/ios27/metal2/TurnShaders.metal (the pass that writes a turned texture into its # surface) compiled by the same Xcode's Metal compiler and held as bytes diff --git a/tools/ios27/metal/mk-panvk27.sh b/tools/ios27/metal/mk-panvk27.sh index 60843e7ed..4c805cfc7 100755 --- a/tools/ios27/metal/mk-panvk27.sh +++ b/tools/ios27/metal/mk-panvk27.sh @@ -12,7 +12,8 @@ SB=${SB:-$(cd "$(dirname "$0")/../../.." && pwd)} # The libvulkan_panfrost.dylib of phone 1's c2 bundle (cc241e55..., 14 November) was built from exactly these patches, with the # shim of that day, except for the two braces mesa26-panvk-batch-stats.patch restores in panvk_vX_cmd_query.c. PATCHES="mesa26-panvk-ios mesa26-panvk-null-vs mesa26-panvk-shader-size-cap mesa26-bifrost-ra-multispill mesa26-panvk-host-images - mesa26-panvk-tiler-heap-halves mesa26-panvk-batch-stats mesa26-panvk-cpu-desc-copy mesa26-panvk-cpu-minmax mesa26-panvk-cpu-direct" + mesa26-panvk-tiler-heap-halves mesa26-panvk-batch-stats mesa26-panvk-cpu-desc-copy mesa26-panvk-cpu-minmax mesa26-panvk-cpu-direct + mesa26-panvk-compile-budget" # mesa26-panvk-cpu-desc-copy (18 November): the descriptor copies on the CPU at submission with STARBITE_PANVK_CPU_DESC=1 (off # without it), and the draw and copy counters of the shim's statistics line (docs/ios27/PERFORMANCE.md section 7). # mesa26-panvk-cpu-direct (18 November): with STARBITE_PANVK_CPU_DIRECT=1 an indexed draw whose range the CPU found is recorded diff --git a/tools/ios27/metal/patches/frontend-async-compile.diff b/tools/ios27/metal/patches/frontend-async-compile.diff new file mode 100644 index 000000000..6358e54cd --- /dev/null +++ b/tools/ios27/metal/patches/frontend-async-compile.diff @@ -0,0 +1,260 @@ +--- a/tools/ios27/metal2/NativeBridgeMetalFrontend.mm 2026-11-20 10:47:54 ++++ b/tools/ios27/metal2/NativeBridgeMetalFrontend.mm 2026-11-20 10:51:01 +@@ -13,6 +13,10 @@ + #import + #include + #include ++#include ++#include ++#include ++#include + #include + #include + #include +@@ -28,6 +32,7 @@ + #include "bridge/NativeMetalObjects.h" + #include "NativeBridgeMetalFrontend.h" + #include "PassLog.h" ++#include "CompileQueue.h" + + extern "C" void sys_dcache_flush(void *start, size_t length); + +@@ -2203,22 +2208,85 @@ + if(error)*error=nil;return YES; + } + @end ++ ++ ++// ---- where pipeline compiles run (the rules, and their host test, are in CompileQueue.h) ------------------------------------ ++// PanVK compiles a pipeline's shaders on the thread that asks for it: one of iOS 27's compute shaders is 24 522 NIR instructions ++// (25 s of a Mali-G72's CPU) and a Preferences fragment shader never ended. #396 moved every compile to a worker and timed the ++// synchronous callers out after 5 s; on phone 2 (19 Nov) that refused backboardd's first render pipelines on a cold cache and ++// QuartzCore aborted on the refusal (launchd rebooted after three). So now: ++// - the synchronous creation calls compile on the calling thread, every time, and return the pipeline or the compile's own ++// error (gCompiler.sync): no timeout, no slot, no negative cache. A compile that would never end is ended by PanVK's compile ++// budget (STARBITE_PANVK_COMPILE_BUDGET_MS of the compiling thread's CPU time), as a real compile error; ++// - the completion-handler calls return at once and compile on a worker (gCompiler.async, at most 3 at a time, FIFO); the ++// handler is called once, on the worker, with what the synchronous call would have returned. The worker queue has no QoS of ++// its own, so a compile runs at the QoS of the thread that asked for it (on this kernel utility/background run on the A55s). ++// An asynchronous compile that failed after more than 10 s is not started again for 10 minutes (the handler gets that error). ++// What makes a pipeline cheap is the shader cache (one per user, warmed by fullroot/steps.d/81-m2v-cache.sh), not a timer. ++static dispatch_queue_t compileQueue() { ++ static dispatch_queue_t queue;static dispatch_once_t once; ++ dispatch_once(&once,^{queue=dispatch_queue_create("starbite.m2v.compile",DISPATCH_QUEUE_CONCURRENT);}); ++ return queue; ++} ++static sbcompile::Compiler gCompiler(3,[](std::function task) { ++ dispatch_async_f(compileQueue(),new std::function(std::move(task)),[](void *p){auto *f=(std::function *)p;(*f)();delete f;}); ++},[]{return nowUs();}); ++// What a completion-handler compile hands back: the state the synchronous method made (or nil) and its error, as the handler wants them. ++struct AsyncPipelineResult { ++ __strong id state=nil;__strong NSError *error=nil;__strong id reflection=nil; ++}; ++static std::string errorText(NSError *error) { ++ const char *t=error.localizedDescription.UTF8String; ++ return t&&*t?t:"the pipeline could not be made"; ++} ++static uint64_t mixKey(uint64_t h,uint64_t v) {return (h^v)*1099511628211ull;} ++static uint64_t functionKey(id function) { ++ uint64_t h=1469598103934665603ull; ++ if(![function isKindOfClass:[NBFunction class]])return mixKey(h,(uintptr_t)function); ++ uint64_t a,b;memcpy(&a,((NBFunction *)function)->_bitCodeHash,8);memcpy(&b,((NBFunction *)function)->_bitCodeHash+8,8); ++ return mixKey(mixKey(h,a),b); ++} ++static uint64_t renderKey(MTLRenderPipelineDescriptor *d,MTLPipelineOption options) { ++ uint64_t h=mixKey(functionKey(d.vertexFunction),functionKey(d.fragmentFunction)); ++ for(NSUInteger i=0;i<8;++i) { ++ MTLRenderPipelineColorAttachmentDescriptor *c=d.colorAttachments[i]; ++ h=mixKey(h,c.pixelFormat);h=mixKey(h,(c.writeMask<<1)|(c.blendingEnabled?1:0)); ++ h=mixKey(h,(c.sourceRGBBlendFactor<<16)|(c.destinationRGBBlendFactor<<8)|c.rgbBlendOperation); ++ h=mixKey(h,(c.sourceAlphaBlendFactor<<16)|(c.destinationAlphaBlendFactor<<8)|c.alphaBlendOperation); ++ } ++ h=mixKey(h,d.depthAttachmentPixelFormat);h=mixKey(h,d.stencilAttachmentPixelFormat);h=mixKey(h,d.rasterSampleCount); ++ h=mixKey(h,d.alphaToCoverageEnabled);h=mixKey(h,d.alphaToOneEnabled);h=mixKey(h,options); ++ return h; ++} ++static id nbFailed(NSError **error,NSString *what,const std::string &why) { ++ NSString *text=[NSString stringWithFormat:@"%@: %s",what,why.c_str()]; ++ noteProblem(text);if(error)*error=nbError(text); ++ return nil; ++} + + static bool pipelineArchives(NSArray *list,std::vector &archives,NSError **error); + // A render pipeline of `d`, its colors in `keep` (bit i, color i) as the descriptor asks and the others through dexr(). + // A pipeline that dexr() changed keeps a copy of the descriptor for sweetExactPipeline. + static NBRenderPipeline *sweetRenderPipeline(MTLRenderPipelineDescriptor *d,MTLPipelineOption options,unsigned keep,NSError **error) { +- auto *b=new BuiltRenderDescriptor();unsigned held=0; +- if(!renderDescriptor(d,b,error,keep,&held)){delete b;return nil;} ++ auto b=std::make_shared();unsigned held=0; ++ if(!renderDescriptor(d,b.get(),error,keep,&held))return nil; + std::vector archives; +- if(!pipelineArchives(d.binaryArchives,archives,error)){delete b;return nil;} +- const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; ++ if(!pipelineArchives(d.binaryArchives,archives,error))return nil; + const uint64_t pipelineBegan=nowUs(); + const bool logJournal=!gJournalPath.empty(); + const NBCost costBegan=logJournal?NBCost::now():NBCost{}; +- NmbRenderPipeline *handle=nmb_render_pipeline_new_with_options(gDevice,&b->raw,&creation);delete b;gTimePipeline.add(pipelineBegan); +- if(logJournal)journalRenderPipeline(d,pipelineBegan,handle?nullptr:nmb_last_error(),NBCost::now().since(costBegan)); +- if(!handle)return failWith(error,@"render pipeline",nil); ++ NSArray *archiveObjects=[d.binaryArchives copy]; // kept by the compile (it may outlive this call) ++ id vertexFunction=d.vertexFunction,fragmentFunction=d.fragmentFunction; // and the functions ++ std::string why; ++ NmbRenderPipeline *handle=(NmbRenderPipeline *)gCompiler.sync([b,archives,archiveObjects,vertexFunction,fragmentFunction,options](std::string &err)->void * { ++ (void)archiveObjects;(void)vertexFunction;(void)fragmentFunction; ++ const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; ++ NmbRenderPipeline *made=nmb_render_pipeline_new_with_options(gDevice,&b->raw,&creation); // on the caller's thread ++ if(!made)err=nmb_last_error(); ++ return made; ++ },why);gTimePipeline.add(pipelineBegan); ++ if(logJournal)journalRenderPipeline(d,pipelineBegan,handle?nullptr:why.c_str(),NBCost::now().since(costBegan)); ++ if(!handle){NBLOG(@"native-metal: render pipeline refused: %s",why.c_str());return (NBRenderPipeline *)nbFailed(error,@"render pipeline",why);} + NBRenderPipeline *state=[NBRenderPipeline new];state.handle=handle;gPipelines++;if(error)*error=nil; + state.passName=d.fragmentFunction.name?:(d.label?:@"-"); + if(held){state.dexrColors=held;state.dexrDescriptor=[d copy];state.dexrOptions=options;} +@@ -2688,10 +2756,15 @@ + reflection:(MTLAutoreleasedComputePipelineReflection *)reflection error:(NSError **)error { + if(reflection)*reflection=nil; + if(![(id)function isKindOfClass:[NBFunction class]]){if(error)*error=nbError(@"foreign function");return nil;} +- const NmbPipelineOptions creation={(uint64_t)options,nullptr,0}; + const uint64_t pipelineBegan=nowUs(); +- NmbComputePipeline *handle=nmb_compute_pipeline_new_with_options(gDevice,[(NBFunction *)function handle],&creation);gTimePipeline.add(pipelineBegan); +- if(!handle){NBLOG(@"native-metal: compute pipeline (%@) refused: %@",[(NBFunction *)function name],lastError());return failWith>(error,@"compute pipeline",nil);} ++ NBFunction *fn=(NBFunction *)function;std::string why; ++ NmbComputePipeline *handle=(NmbComputePipeline *)gCompiler.sync([fn,options](std::string &err)->void * { ++ const NmbPipelineOptions creation={(uint64_t)options,nullptr,0}; ++ NmbComputePipeline *made=nmb_compute_pipeline_new_with_options(gDevice,[fn handle],&creation); ++ if(!made)err=nmb_last_error(); ++ return made; ++ },why);gTimePipeline.add(pipelineBegan); ++ if(!handle){NBLOG(@"native-metal: compute pipeline (%@) refused: %s",[fn name],why.c_str());return nbFailed(error,@"compute pipeline",why);} + NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;state.passName=[(NBFunction *)function name];gPipelines++;if(error)*error=nil; + return (id)state; + } +@@ -2701,45 +2774,107 @@ + - (id)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d options:(MTLPipelineOption)options + reflection:(MTLAutoreleasedComputePipelineReflection *)reflection error:(NSError **)error { + if(reflection)*reflection=nil; +- BuiltComputeDescriptor b;if(!computeDescriptor(d,&b,error))return nil; ++ auto b=std::make_shared();if(!computeDescriptor(d,b.get(),error))return nil; + std::vector archives; + if(!pipelineArchives(d.binaryArchives,archives,error))return nil; +- const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; + const uint64_t pipelineBegan=nowUs(); +- NmbComputePipeline *handle=nmb_compute_pipeline_new_with_descriptor(gDevice,&b.raw,&creation);gTimePipeline.add(pipelineBegan); +- if(!handle){NBLOG(@"native-metal: compute pipeline (%@) refused: %@",[(NBFunction *)d.computeFunction name],lastError());return failWith>(error,@"compute pipeline",nil);} ++ NSArray *archiveObjects=[d.binaryArchives copy]; // kept by the compile (it may outlive this call) ++ NBFunction *fn=(NBFunction *)d.computeFunction;std::string why; ++ NmbComputePipeline *handle=(NmbComputePipeline *)gCompiler.sync([b,archives,archiveObjects,fn,options](std::string &err)->void * { ++ (void)archiveObjects;(void)fn; ++ const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; ++ NmbComputePipeline *made=nmb_compute_pipeline_new_with_descriptor(gDevice,&b->raw,&creation); ++ if(!made)err=nmb_last_error(); ++ return made; ++ },why);gTimePipeline.add(pipelineBegan); ++ if(!handle){NBLOG(@"native-metal: compute pipeline (%@) refused: %s",[fn name],why.c_str());return nbFailed(error,@"compute pipeline",why);} + NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;state.label=d.label;state.passName=d.computeFunction.name?:d.label;gPipelines++; + if(error)*error=nil;return (id)state; + } + - (void)newComputePipelineStateWithFunction:(id)function completionHandler:(void (^)(id,NSError *))completionHandler { +- NSError *error=nil;id state=[(id)self newComputePipelineStateWithFunction:function error:&error]; +- if(completionHandler)dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{completionHandler(state,error);}); ++ auto box=std::make_shared(); ++ gCompiler.async(mixKey(functionKey(function),0x5c),[=](std::string &why)->void * { ++ NSError *error=nil; ++ box->state=[(id)self newComputePipelineStateWithFunction:function error:&error]; ++ box->error=error; ++ if(!box->state)why=errorText(error); ++ return box->state?(__bridge void *)box->state:nullptr; ++ },[=](void *,const std::string &why) { ++ if(!box->state && !box->error)box->error=nbError(@(why.c_str())); // refused from the negative cache: no compile ran ++ if(completionHandler)completionHandler(box->state,box->error); ++ }); + } + - (void)newComputePipelineStateWithFunction:(id)function options:(MTLPipelineOption)options completionHandler:(void (^)(id,MTLComputePipelineReflection *,NSError *))completionHandler { +- NSError *error=nil;MTLComputePipelineReflection *reflection=nil; +- id state=[(id)self newComputePipelineStateWithFunction:function options:options reflection:&reflection error:&error]; +- if(completionHandler)dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{completionHandler(state,reflection,error);}); ++ auto box=std::make_shared(); ++ gCompiler.async(mixKey(mixKey(functionKey(function),options),0x5d),[=](std::string &why)->void * { ++ NSError *error=nil;MTLComputePipelineReflection *reflection=nil; ++ box->state=[(id)self newComputePipelineStateWithFunction:function options:options reflection:&reflection error:&error]; ++ box->error=error;box->reflection=reflection; ++ if(!box->state)why=errorText(error); ++ return box->state?(__bridge void *)box->state:nullptr; ++ },[=](void *,const std::string &why) { ++ if(!box->state && !box->error)box->error=nbError(@(why.c_str())); // refused from the negative cache: no compile ran ++ if(completionHandler)completionHandler(box->state,box->reflection,box->error); ++ }); + } + - (void)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d completionHandler:(void (^)(id,NSError *))completionHandler { +- NSError *error=nil;id state=[self newComputePipelineStateWithDescriptor:d error:&error]; +- if(completionHandler)dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{completionHandler(state,error);}); ++ MTLComputePipelineDescriptor *copy=[d copy]; // the caller may change its descriptor as soon as this returns ++ auto box=std::make_shared(); ++ gCompiler.async(mixKey(mixKey(functionKey(copy.computeFunction),copy.maxTotalThreadsPerThreadgroup),0x5e),[=](std::string &why)->void * { ++ NSError *error=nil; ++ box->state=[self newComputePipelineStateWithDescriptor:copy error:&error]; ++ box->error=error; ++ if(!box->state)why=errorText(error); ++ return box->state?(__bridge void *)box->state:nullptr; ++ },[=](void *,const std::string &why) { ++ if(!box->state && !box->error)box->error=nbError(@(why.c_str())); // refused from the negative cache: no compile ran ++ if(completionHandler)completionHandler(box->state,box->error); ++ }); + } + - (void)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d options:(MTLPipelineOption)options completionHandler:(void (^)(id,MTLComputePipelineReflection *,NSError *))completionHandler { +- NSError *error=nil;MTLComputePipelineReflection *reflection=nil; +- id state=[(id)self newComputePipelineStateWithDescriptor:d options:options reflection:&reflection error:&error]; +- if(completionHandler)dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{completionHandler(state,reflection,error);}); ++ MTLComputePipelineDescriptor *copy=[d copy]; ++ auto box=std::make_shared(); ++ gCompiler.async(mixKey(mixKey(mixKey(functionKey(copy.computeFunction),options),copy.maxTotalThreadsPerThreadgroup),0x5f),[=](std::string &why)->void * { ++ NSError *error=nil;MTLComputePipelineReflection *reflection=nil; ++ box->state=[(id)self newComputePipelineStateWithDescriptor:copy options:options reflection:&reflection error:&error]; ++ box->error=error;box->reflection=reflection; ++ if(!box->state)why=errorText(error); ++ return box->state?(__bridge void *)box->state:nullptr; ++ },[=](void *,const std::string &why) { ++ if(!box->state && !box->error)box->error=nbError(@(why.c_str())); // refused from the negative cache: no compile ran ++ if(completionHandler)completionHandler(box->state,box->reflection,box->error); ++ }); + } + - (id)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d error:(NSError **)error { + return [(id)self newRenderPipelineStateWithDescriptor:d options:MTLPipelineOptionNone reflection:nil error:error]; + } + - (void)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d completionHandler:(void (^)(id,NSError *))completionHandler { +- NSError *error=nil;id state=[(id)self newRenderPipelineStateWithDescriptor:d error:&error]; +- if(completionHandler)dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{completionHandler(state,error);}); ++ MTLRenderPipelineDescriptor *copy=[d copy]; // the caller may change its descriptor as soon as this returns ++ auto box=std::make_shared(); ++ gCompiler.async(mixKey(renderKey(copy,MTLPipelineOptionNone),0x60),[=](std::string &why)->void * { ++ NSError *error=nil; ++ box->state=[(id)self newRenderPipelineStateWithDescriptor:copy error:&error]; ++ box->error=error; ++ if(!box->state)why=errorText(error); ++ return box->state?(__bridge void *)box->state:nullptr; ++ },[=](void *,const std::string &why) { ++ if(!box->state && !box->error)box->error=nbError(@(why.c_str())); // refused from the negative cache: no compile ran ++ if(completionHandler)completionHandler(box->state,box->error); ++ }); + } + - (void)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d options:(MTLPipelineOption)options completionHandler:(void (^)(id,MTLRenderPipelineReflection *,NSError *))completionHandler { +- NSError *error=nil;MTLRenderPipelineReflection *reflection=nil; +- id state=[(id)self newRenderPipelineStateWithDescriptor:d options:options reflection:&reflection error:&error]; +- if(completionHandler)dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{completionHandler(state,reflection,error);}); ++ MTLRenderPipelineDescriptor *copy=[d copy]; ++ auto box=std::make_shared(); ++ gCompiler.async(mixKey(renderKey(copy,options),0x61),[=](std::string &why)->void * { ++ NSError *error=nil;MTLRenderPipelineReflection *reflection=nil; ++ box->state=[(id)self newRenderPipelineStateWithDescriptor:copy options:options reflection:&reflection error:&error]; ++ box->error=error;box->reflection=reflection; ++ if(!box->state)why=errorText(error); ++ return box->state?(__bridge void *)box->state:nullptr; ++ },[=](void *,const std::string &why) { ++ if(!box->state && !box->error)box->error=nbError(@(why.c_str())); // refused from the negative cache: no compile ran ++ if(completionHandler)completionHandler(box->state,box->reflection,box->error); ++ }); + } + - (id)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d options:(MTLPipelineOption)options + reflection:(MTLAutoreleasedRenderPipelineReflection *)reflection error:(NSError **)error { diff --git a/tools/ios27/metal/patches/hotmetal2-shared-cache.diff b/tools/ios27/metal/patches/hotmetal2-shared-cache.diff new file mode 100644 index 000000000..e7a20d21d --- /dev/null +++ b/tools/ios27/metal/patches/hotmetal2-shared-cache.diff @@ -0,0 +1,31 @@ +--- a/tools/ios27/metal2/HotMetal2.mm 2026-11-18 21:32:59 ++++ b/tools/ios27/metal2/HotMetal2.mm 2026-11-18 21:33:27 +@@ -212,11 +212,24 @@ + setenv("PAN_I_WANT_A_BROKEN_VULKAN_DRIVER", "1", 1); + /* Bifrost v6 has most of Vulkan 1.2 as extensions but reports 1.0; metal2vulkan's floor is 1.2 */ + setenv("MESA_VK_VERSION_OVERRIDE", "1.2", 0); ++ /* ++ * One shader cache for every process of a user (not one per program): the translated shaders and PanVK's pipeline data are ++ * named by their content, so Preferences, SpringBoard and backboardd find what the first of them compiled, and the cache ++ * a root step ships warm (fullroot/steps.d/81-m2v-cache.sh) is the one they all read. The runtime's files are owner-only, ++ * so the directory is the user's: /var/local/m2v-cache/u. STARBITE_M2V_CACHE (mtl2-env) names another directory. ++ */ + char cache[256]; +- mkdir("/var/local/m2v-cache", 0777); +- chmod("/var/local/m2v-cache", 0777); +- snprintf(cache, sizeof(cache), "/var/local/m2v-cache/%s", getprogname()); +- mkdir(cache, 0777); ++ { ++ const char *named = getenv("STARBITE_M2V_CACHE"); ++ if (named && named[0] == '/') { ++ strlcpy(cache, named, sizeof(cache)); ++ } else { ++ mkdir("/var/local/m2v-cache", 0777); ++ chmod("/var/local/m2v-cache", 0777); ++ snprintf(cache, sizeof(cache), "/var/local/m2v-cache/u%u", (unsigned)geteuid()); ++ } ++ mkdir(cache, 0700); ++ } + if (access(cache, W_OK) != 0) { + /* a sandboxed process: its own caches directory */ + char dir[256]; diff --git a/tools/ios27/metal2/CompileQueue.h b/tools/ios27/metal2/CompileQueue.h new file mode 100644 index 000000000..492b0950f --- /dev/null +++ b/tools/ios27/metal2/CompileQueue.h @@ -0,0 +1,133 @@ +// CompileQueue.h: where the Metal frontend's pipeline compiles run (Starbite, iOS 27 port). Plain C++17, no Objective-C, so the +// rules below are tested on the host: tools/ios27/metal2/tests/compile-queue-test.cpp (run tools/ios27/metal2/tests/run.sh). +// +// The rules (scion-kernel #396 broke the first one twice on phone 2, 19 Nov 2026): +// 1. A synchronous creation call (newRenderPipelineStateWithDescriptor:error: and friends, and the draw-time remake of a pipeline) +// compiles on the calling thread, now, every time, and returns what the compile returned: the pipeline, or the compile's own +// error. No timeout, no refusal from a negative cache, no wait for a worker slot. QuartzCore treats any refused render pipeline +// as fatal (backboardd aborts "Metal failed to build render pipeline", launchd reboots after three), so "ask again later" is not +// an answer a synchronous caller can be given. A compile that never ends is PanVK's business: its compile budget +// (mesa26-panvk-compile-budget.patch, CPU time of the compiling thread) turns it into a real compile error. +// 2. A completion-handler creation call returns at once; the compile runs on a worker task and the handler is called exactly once, +// on that task, with the pipeline or the error. At most `slots` of these compile at the same time; the rest wait in a FIFO list +// (no thread is parked on a semaphore for them). A synchronous call never waits for these slots (rule 1). +// 3. An asynchronous compile that FAILED after more than `slowFailureUs` is not started again for the same key for `negativeTtlUs`: +// the handler gets the earlier failure at once. Only the asynchronous path uses this; a synchronous call always compiles. +// 4. Nothing is called with the lock held: a make() or a done() may call sync() or async() again (a completion handler that asks for +// the next pipeline, a worker compile that goes through the synchronous entry point). +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace sbcompile { + +using Make = std::function; // compile: the handle, or null and `error` +using Done = std::function; // owns the handle it is given +using Spawn = std::function)>; // run a task on another thread +using Clock = std::function; // microseconds, monotonic + +struct Stats { + std::atomic syncCalls{0}, asyncCalls{0}, asyncStarted{0}, asyncDone{0}, asyncRefusedNegative{0}, makeExceptions{0}; + std::atomic asyncRunning{0}, asyncRunningMax{0}; +}; + +class Compiler { +public: + Compiler(unsigned slots, Spawn spawn, Clock now, uint64_t slowFailureUs = 10000000ull, uint64_t negativeTtlUs = 600000000ull) + : slots_(slots ? slots : 1), spawn_(std::move(spawn)), now_(std::move(now)), slowFailureUs_(slowFailureUs), negativeTtlUs_(negativeTtlUs) {} + + // Rule 1. + void *sync(const Make &make, std::string &error) { + stats.syncCalls++; + return guarded(make, error); + } + + // Rules 2-4. + void async(uint64_t key, Make make, Done done) { + stats.asyncCalls++; + std::string refused; + { + std::lock_guard g(lock_); + auto failed = slowFailures_.find(key); + if (failed != slowFailures_.end()) { + if (now_() - failed->second.at < negativeTtlUs_) refused = failed->second.error; + else slowFailures_.erase(failed); + } + } + if (!refused.empty()) { + stats.asyncRefusedNegative++; + done(nullptr, "an earlier compile of this pipeline failed after a long time (not tried again for 10 minutes): " + refused); + return; + } + bool start = false; + { + std::lock_guard g(lock_); + pending_.push_back(Task{key, std::move(make), std::move(done)}); + if (running_ < slots_) { running_++; start = true; } + } + if (start) spawn_([this] { drain(); }); + } + + size_t pendingCount() { std::lock_guard g(lock_); return pending_.size(); } + bool idle() { std::lock_guard g(lock_); return pending_.empty() && running_ == 0; } + Stats stats; + +private: + struct Task { uint64_t key; Make make; Done done; }; + struct Failure { uint64_t at; std::string error; }; + + void *guarded(const Make &make, std::string &error) { + try { + return make(error); + } catch (...) { // an Objective-C exception too: in Objective-C++ catch (...) takes it + stats.makeExceptions++; + if (error.empty()) error = "the compile threw an exception"; + return nullptr; + } + } + + // One worker: take the oldest pending task, compile, report, and go on while there is work; give the slot back when there is none. + void drain() { + for (;;) { + Task task; + { + std::lock_guard g(lock_); + if (pending_.empty()) { running_--; return; } + task = std::move(pending_.front()); + pending_.pop_front(); + } + int now = ++stats.asyncRunning; + for (int seen = stats.asyncRunningMax.load(); now > seen && !stats.asyncRunningMax.compare_exchange_weak(seen, now);) {} + stats.asyncStarted++; + const uint64_t began = now_(); + std::string error; + void *handle = guarded(task.make, error); + if (!handle && now_() - began > slowFailureUs_) { + std::lock_guard g(lock_); + slowFailures_[task.key] = Failure{now_(), error}; + } + stats.asyncRunning--; + try { + if (task.done) task.done(handle, error); + } catch (...) { // a handler that throws loses its own call, not the worker's slot + } + stats.asyncDone++; + } + } + + const unsigned slots_; + Spawn spawn_; + Clock now_; + const uint64_t slowFailureUs_, negativeTtlUs_; + std::mutex lock_; + std::deque pending_; + unsigned running_ = 0; + std::unordered_map slowFailures_; +}; + +} // namespace sbcompile diff --git a/tools/ios27/metal2/harvest-m2v-cache.sh b/tools/ios27/metal2/harvest-m2v-cache.sh new file mode 100755 index 000000000..ba3ed3b74 --- /dev/null +++ b/tools/ios27/metal2/harvest-m2v-cache.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# harvest-m2v-cache.sh PULLED OUT : a warm cache for steps.d/81-m2v-cache.sh from a phone's /private/var/local/m2v-cache. +# PULLED = that directory, copied off the phone after a warm-up with the bundle that will ship (open Settings, Display & Brightness +# / Appearance, the app switcher, Control Center, a few apps; wait until `PANVK: ... compiled` lines stop). Keeps, per user +# directory (u/), the BUILD directories under v1/ that were written last (the running bundle's: its translated shaders) +# and the content directory (PanVK's pipeline data, valid for as long as PanVK is the same); drops older builds' directories. +# OUT (default tools/ios27/fullroot/warm-m2v) is what the step installs; it is not in git (cache entries are build artefacts). +set -euo pipefail +SRC=${1:?pulled m2v-cache directory} +OUT=${2:-$(cd "$(dirname "$0")/../fullroot" && pwd)/warm-m2v} +# the content directory's name is fixed: sha256 of the tag the runtime names it by +CONTENT=$(printf 'metal2vulkan shader cache content\0' | shasum -a 256 | cut -d' ' -f1) +rm -rf "$OUT"; mkdir -p "$OUT" +n=0 +for u in "$SRC"/u[0-9]*/; do + [[ -d $u/v1 ]] || continue + name=$(basename "$u") + mkdir -p "$OUT/$name/v1" + # newest build directory by the newest file in it + newest=""; best=0 + for b in "$u"v1/*/; do + id=$(basename "$b"); [[ $id == "$CONTENT" ]] && continue + t=$(find "$b" -type f -exec stat -f %m {} + 2>/dev/null | sort -n | tail -1); t=${t:-0} + if (( t >= best )); then best=$t; newest=$id; fi + done + [[ -n $newest ]] && cp -R "$u/v1/$newest" "$OUT/$name/v1/$newest" + [[ -d $u/v1/$CONTENT ]] && cp -R "$u/v1/$CONTENT" "$OUT/$name/v1/$CONTENT" + n=$((n + 1)) + echo "harvest: $name: build ${newest:-none}, content $([[ -d $u/v1/$CONTENT ]] && echo yes || echo NO), $(find "$OUT/$name" -type f | wc -l) files" +done +(( n )) || { echo "harvest: no u/ directory with v1/ in $SRC (a phone that ran the older per-program plug-in has backboardd/ instead: boot the new bundle first)"; exit 1; } +du -sk "$OUT" | awk '{print "harvest: " $1 " KiB in '"$OUT"'"}' diff --git a/tools/ios27/metal2/tests/compile-queue-test.cpp b/tools/ios27/metal2/tests/compile-queue-test.cpp new file mode 100644 index 000000000..aa3f3674a --- /dev/null +++ b/tools/ios27/metal2/tests/compile-queue-test.cpp @@ -0,0 +1,178 @@ +// compile-queue-test.cpp: host test of CompileQueue.h, the rules the Metal frontend's pipeline compiles follow. +// tools/ios27/metal2/tests/run.sh (c++ -std=c++17 -pthread; no Apple SDK needed: also runs on Linux) +// Each test names the phone failure it would have caught. Exit status 0 = all pass. +#include "../CompileQueue.h" + +#include +#include +#include +#include +#include +#include +#include + +using namespace std::chrono; +using sbcompile::Compiler; + +static int failures = 0; +#define CHECK(cond, ...) do { if (!(cond)) { failures++; std::printf(" FAIL %s:%d: %s: ", __FILE__, __LINE__, #cond); std::printf(__VA_ARGS__); std::printf("\n"); } } while (0) + +static uint64_t realNow() { return (uint64_t)duration_cast(steady_clock::now().time_since_epoch()).count(); } +static sbcompile::Spawn threads() { return [](std::function f) { std::thread(std::move(f)).detach(); }; } +static void *H(uintptr_t v) { return (void *)v; } + +// A gate the test opens by hand: compiles that should still be "running" wait on it. +struct Gate { + std::mutex m; std::condition_variable cv; bool open = false; + void wait() { std::unique_lock l(m); cv.wait(l, [&] { return open; }); } + void release() { { std::lock_guard g(m); open = true; } cv.notify_all(); } +}; +// Wait (bounded) until pred() holds. +template static bool eventually(P pred, int ms = 3000) { + for (int i = 0; i < ms; i += 5) { if (pred()) return true; std::this_thread::sleep_for(milliseconds(5)); } + return pred(); +} + +// Phone 2, 19 Nov 00:30 (L6 + root-b3): with STARBITE_M2V_COMPILE_TIMEOUT_MS=5000 a cold-cache compile of backboardd's first +// render pipelines took longer than the wait, the frontend refused them, QuartzCore aborted backboardd 3 times, launchd rebooted. +// A synchronous call must return the compile's result however long it takes, whatever the old timeout variable says. +static void syncNeverTimesOut() { + std::printf("sync: a slow compile is waited for (no timeout refusal; #396 failure 1)\n"); + setenv("STARBITE_M2V_COMPILE_TIMEOUT_MS", "50", 1); // ignored now: a leftover mtl2-env line must change nothing + Compiler c(3, threads(), realNow); + std::string err; + const uint64_t t0 = realNow(); + void *h = c.sync([](std::string &) { std::this_thread::sleep_for(milliseconds(400)); return H(0x51); }, err); + CHECK(h == H(0x51), "got %p err '%s'", h, err.c_str()); + CHECK(err.empty(), "err '%s'", err.c_str()); + CHECK(realNow() - t0 >= 400000, "returned before the compile finished"); + unsetenv("STARBITE_M2V_COMPILE_TIMEOUT_MS"); +} + +// A real compile error is passed through as it is (the caller decides: QuartzCore aborts, an app shows nothing). +static void syncPassesRealError() { + std::printf("sync: a compile error is the caller's error, verbatim\n"); + Compiler c(3, threads(), realNow); + std::string err; + void *h = c.sync([](std::string &e) { e = "PANVK: fragment shader abandoned after 90000 ms (compile budget)"; return (void *)nullptr; }, err); + CHECK(h == nullptr, "handle %p", h); + CHECK(err == "PANVK: fragment shader abandoned after 90000 ms (compile budget)", "err '%s'", err.c_str()); + std::string err2; + void *h2 = c.sync([](std::string &) -> void * { throw 42; }, err2); + CHECK(h2 == nullptr && !err2.empty(), "an exception in the compile must come back as an error, got %p '%s'", h2, err2.c_str()); +} + +// The night of 19 Nov blamed the frontend's 3-slot semaphore for b3t's missing frames (timeout 0): a synchronous compile on the +// render thread queued behind completion-handler compiles that never ended. Whatever the cause there was, it must not be possible: +// with every asynchronous slot taken by a compile that does not end, a synchronous call still compiles at once. +static void syncNotBehindAsyncSlots() { + std::printf("sync: never waits for the asynchronous slots (all slots busy, sync still returns)\n"); + Gate gate; + Compiler c(3, threads(), realNow); + std::atomic started{0}, done{0}; + for (int i = 0; i < 6; i++) + c.async(100 + i, [&](std::string &) { started++; gate.wait(); return H(1); }, [&](void *, const std::string &) { done++; }); + CHECK(eventually([&] { return started.load() == 3; }), "3 slots should be busy, started=%d", started.load()); + std::string err; + const uint64_t t0 = realNow(); + void *h = c.sync([](std::string &) { return H(0x77); }, err); + CHECK(h == H(0x77), "sync got %p '%s'", h, err.c_str()); + CHECK(realNow() - t0 < 200000, "sync took %llu us with the slots busy", (unsigned long long)(realNow() - t0)); + CHECK(started.load() == 3 && c.pendingCount() == 3, "the other 3 wait: started=%d pending=%zu", started.load(), c.pendingCount()); + gate.release(); + CHECK(eventually([&] { return done.load() == 6; }), "all 6 handlers, got %d", done.load()); + CHECK(eventually([&] { return c.idle(); }), "workers still running at the end"); +} + +// Completion handlers: every one called exactly once, never more than `slots` compiles at once, all of them finish. +static void asyncAllCompleteOnceWithinSlots() { + std::printf("async: 200 requests, 3 slots: each handler once, at most 3 at once, all done\n"); + Compiler c(3, threads(), realNow); + const int N = 200; + std::vector> calls(N); + std::atomic concurrent{0}, worst{0}; + for (int i = 0; i < N; i++) { + c.async((uint64_t)i, [&, i](std::string &e) -> void * { + int now = ++concurrent; + for (int w = worst.load(); now > w && !worst.compare_exchange_weak(w, now);) {} + std::this_thread::sleep_for(microseconds(200 + (i % 7) * 100)); + concurrent--; + if (i % 5 == 0) { e = "no"; return nullptr; } // failures release their slot like successes + return H(1000 + i); + }, [&, i](void *h, const std::string &e) { + calls[i]++; + if (i % 5 == 0) CHECK(!h && e == "no", "job %d: %p '%s'", i, h, e.c_str()); + else CHECK(h == H(1000 + i), "job %d got %p", i, h); + }); + } + CHECK(eventually([&] { return c.stats.asyncDone.load() == (uint64_t)N; }, 10000), "done %llu of %d", (unsigned long long)c.stats.asyncDone.load(), N); + for (int i = 0; i < N; i++) CHECK(calls[i].load() == 1, "handler %d called %d times", i, calls[i].load()); + CHECK(worst.load() <= 3, "%d compiles at once (slots 3)", worst.load()); + CHECK(c.pendingCount() == 0, "pending %zu", c.pendingCount()); + CHECK(eventually([&] { return c.idle(); }), "workers still running at the end"); +} + +// The completion-handler entry points of the frontend run the synchronous method on the worker (that is how they compile), and a +// handler often asks for the next pipeline. Neither may deadlock, including when all slots are in use. +static void reentrancy() { + std::printf("async: make() calling sync(), done() calling async(): no deadlock\n"); + Compiler c(1, threads(), realNow); + std::atomic second{0}; + c.async(1, [&](std::string &e) { return c.sync([](std::string &) { return H(2); }, e); }, + [&](void *h, const std::string &) { + CHECK(h == H(2), "nested sync result %p", h); + c.async(2, [](std::string &) { return H(3); }, [&](void *h2, const std::string &) { CHECK(h2 == H(3), "%p", h2); second++; }); + }); + CHECK(eventually([&] { return second.load() == 1; }), "the handler's own request did not complete"); + CHECK(eventually([&] { return c.idle(); }), "workers still running at the end"); +} + +// A handler that throws must not keep the slot (with one slot, the next request would never start). +static void throwingHandlerKeepsSlot() { + std::printf("async: a throwing handler does not lose the slot\n"); + Compiler c(1, threads(), realNow); + std::atomic ok{0}; + c.async(1, [](std::string &) { return H(1); }, [](void *, const std::string &) { throw 1; }); + c.async(2, [](std::string &) { return H(2); }, [&](void *, const std::string &) { ok++; }); + CHECK(eventually([&] { return ok.load() == 1; }), "second request never ran"); + CHECK(eventually([&] { return c.idle(); }), "workers still running at the end"); +} + +// Rule 3 with a fake clock: an asynchronous compile that failed slowly is answered from the negative cache for 10 minutes, then +// tried again; a synchronous call of the same pipeline always compiles. +static void negativeCacheAsyncOnly() { + std::printf("async: a slow failure is not retried for 10 min; sync ignores that\n"); + std::atomic clock{1000000}; + Compiler c(2, threads(), [&] { return clock.load(); }); + std::atomic makes{0}, done{0}; + std::string lastErr; + std::mutex m; + auto slowFail = [&](std::string &e) -> void * { makes++; clock += 11000000; e = "budget"; return nullptr; }; + auto rec = [&](void *h, const std::string &e) { std::lock_guard g(m); CHECK(!h, "%p", h); lastErr = e; done++; }; + c.async(9, slowFail, rec); + CHECK(eventually([&] { return done.load() == 1; }), "first"); + c.async(9, slowFail, rec); + CHECK(eventually([&] { return done.load() == 2; }), "second"); + CHECK(makes.load() == 1, "the second request compiled again (makes=%d)", makes.load()); + { std::lock_guard g(m); CHECK(lastErr.find("budget") != std::string::npos, "refusal should carry the real error: '%s'", lastErr.c_str()); } + std::string err; + void *h = c.sync([&](std::string &) { makes++; return H(5); }, err); + CHECK(h == H(5) && makes.load() == 2, "sync must compile regardless of the negative cache (%p, makes %d)", h, makes.load()); + clock += 600000001; + c.async(9, [&](std::string &) { makes++; return H(6); }, [&](void *h2, const std::string &) { CHECK(h2 == H(6), "%p", h2); done++; }); + CHECK(eventually([&] { return done.load() == 3; }), "after 10 min it is tried again"); + CHECK(makes.load() == 3, "makes %d", makes.load()); + CHECK(eventually([&] { return c.idle(); }), "workers still running at the end"); +} + +int main() { + syncNeverTimesOut(); + syncPassesRealError(); + syncNotBehindAsyncSlots(); + asyncAllCompleteOnceWithinSlots(); + reentrancy(); + throwingHandlerKeepsSlot(); + negativeCacheAsyncOnly(); + std::printf(failures ? "compile-queue-test: %d FAILED\n" : "compile-queue-test: all passed\n", failures); + return failures ? 1 : 0; +} diff --git a/tools/ios27/metal2/tests/run.sh b/tools/ios27/metal2/tests/run.sh new file mode 100755 index 000000000..a4c77764f --- /dev/null +++ b/tools/ios27/metal2/tests/run.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# tools/ios27/metal2/tests/run.sh: the host checks of the Metal frontend's compile rules (CompileQueue.h). +# 1. compile-queue-test.cpp (plain C++17 + threads; macOS or Linux), also under ThreadSanitizer when the compiler has it +# 2. the frontend patch routes every pipeline creation through gCompiler and has no timeout refusal left in it +set -e +D=$(cd "$(dirname "$0")" && pwd); M=$(cd "$D/.." && pwd); P=$(cd "$M/../metal/patches" && pwd) +T=${TMPDIR:-/tmp}/sb-compile-queue-test.$$ +CXX=${CXX:-c++} +trap 'rm -f "$T" "$T.tsan" "$T.out"' EXIT +$CXX -std=c++17 -O1 -g -Wall -Wextra -Wno-unused-parameter -pthread -o "$T" "$D/compile-queue-test.cpp" +"$T" +if $CXX -std=c++17 -O1 -g -fsanitize=thread -pthread -o "$T.tsan" "$D/compile-queue-test.cpp" 2>/dev/null; then + echo "-- again under ThreadSanitizer" + # Linux kernels with a large mmap randomisation (ubuntu 24.04) stop TSan before main ("unexpected memory mapping"): + # then run it without address randomisation (setarch -R), and skip only if that is not possible either. + if ! TSAN_OPTIONS=halt_on_error=1 "$T.tsan" > "$T.out" 2>&1; then + if grep -q 'unexpected memory mapping' "$T.out" && command -v setarch >/dev/null 2>&1; then + TSAN_OPTIONS=halt_on_error=1 setarch "$(uname -m)" -R "$T.tsan" > "$T.out" 2>&1 || { + if grep -q 'unexpected memory mapping' "$T.out"; then echo "-- (ThreadSanitizer cannot start on this kernel: skipped)"; else cat "$T.out"; exit 1; fi; } + elif grep -q 'unexpected memory mapping' "$T.out"; then + echo "-- (ThreadSanitizer cannot start on this kernel: skipped)" + else + cat "$T.out"; exit 1 + fi + fi + tail -1 "$T.out" +else + echo "-- (no ThreadSanitizer in $CXX: skipped)" +fi +F=${FRONTEND_DIFF:-$P/frontend-async-compile.diff} # FRONTEND_DIFF=: check another version of the patch +fail=0 +# the frontend's synchronous creation paths: no wait with a time limit and no "ask again" refusal (#396 failure 1) +if grep -nE '^\+.*(wait_for\(|still running after|ask again|COMPILE_TIMEOUT_MS")' "$F"; then + echo "frontend-async-compile.diff: a timed wait or a timeout refusal is back in the frontend (see CompileQueue.h rule 1)"; fail=1 +fi +# every place that makes a pipeline does it inside gCompiler.sync (synchronous) or gCompiler.async (completion handler) +n_new=$(grep -cE '^\+.*nmb_(render|compute)_pipeline_new' "$F" || true) +n_old=$(grep -cE '^-.*nmb_(render|compute)_pipeline_new' "$F" || true) +n_sync=$(grep -cE '^\+.*gCompiler\.sync\(' "$F" || true) +if [ "$n_old" -ne 3 ] || [ "$n_new" -ne 3 ] || [ "$n_sync" -ne 3 ]; then + echo "frontend-async-compile.diff: expected the 3 pipeline makers moved into 3 gCompiler.sync calls (old $n_old new $n_new sync $n_sync)"; fail=1 +fi +n_handlers=$(grep -cE '^\+.*gCompiler\.async\(' "$F" || true) +if [ "$n_handlers" -ne 6 ]; then + echo "frontend-async-compile.diff: expected the 6 completion-handler methods on gCompiler.async (found $n_handlers)"; fail=1 +fi +[ $fail = 0 ] && echo "frontend-async-compile.diff: rules hold (3 sync makers, 6 async handlers, no timed wait)" +exit $fail