diff --git a/tinygrad_repo/tinygrad/runtime/ops_qcom.py b/tinygrad_repo/tinygrad/runtime/ops_qcom.py index 155449c..9a6ddbf 100644 --- a/tinygrad_repo/tinygrad/runtime/ops_qcom.py +++ b/tinygrad_repo/tinygrad/runtime/ops_qcom.py @@ -16,8 +16,19 @@ if getenv("IOCTL"): import extra.qcom_gpu_driver.opencl_ioctl # noqa: F401 # p BUFTYPE_BUF, BUFTYPE_TEX, BUFTYPE_IBO = 0, 1, 2 +# precompiled aarch64 machine code for the dcache_flush kernel below (dc cvac +# loop + dsb sy). Shipping the bytes avoids a runtime clang subprocess whose +# compile was getting killed by an msgq SIGUSR2 landing on the clang child's +# recycled TID -> modeld crash. Regenerate if the kernel source changes: +# prg = to_program(...); print(prg.src[4].arg.hex()) +_DCACHE_FLUSH_AARCH64 = bytes.fromhex("3f040071cb000054287c4092207a0bd500000191080500f1a1ffff549f3f03d5c0035fd6") + @functools.cache def dcache_flush(): + # fast path: load shipped machine code, no compile at all (device is aarch64) + if os.uname().machine in ("aarch64", "arm64"): + try: return Device["CPU"].runtime("dcache_flush", _DCACHE_FLUSH_AARCH64) + except Exception: pass from tinygrad.uop.ops import UOp, Ops, KernelInfo from tinygrad.codegen import to_program buf, n = UOp.param(0, dtypes.uint8.ptr()), UOp.param(1, dtypes.uint8.ptr()) diff --git a/tinygrad_repo/tinygrad/runtime/support/compiler_cpu.py b/tinygrad_repo/tinygrad/runtime/support/compiler_cpu.py index 2fc5ede..125d2e1 100644 --- a/tinygrad_repo/tinygrad/runtime/support/compiler_cpu.py +++ b/tinygrad_repo/tinygrad/runtime/support/compiler_cpu.py @@ -1,9 +1,13 @@ -import ctypes, subprocess +import ctypes, subprocess, signal from tinygrad.device import Compiler from tinygrad.helpers import getenv, capstone_flatdump, DEBUG, unwrap from tinygrad.runtime.support.elf import jit_loader from tinygrad.runtime.autogen import llvm +def _block_sigusr2(): + try: signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGUSR2}) + except (ValueError, OSError): pass + class ClangCompiler(Compiler): def __init__(self, arch:list[str], cachekey="compile_clang_jit"): assert len(arch) >= 2, f"invalid arch string: {','.join(arch)!r}, expected ',,[]' (eg. 'x86_64,znver2')" @@ -21,7 +25,8 @@ class ClangCompiler(Compiler): """Compile C source to ELF object file (before linking).""" # -fno-math-errno is required for __builtin_sqrt to become an instruction instead of a function call return subprocess.check_output([getenv("CC", 'clang'), '-c', '-x', 'c', '-O2', '-fPIC', '-ffreestanding', '-fno-math-errno', '-nostdlib', - '-fno-ident', f'--target={self.arch}-none-unknown-elf', *self.args, '-', '-o', '-'], input=src.encode('utf-8')) + '-fno-ident', f'--target={self.arch}-none-unknown-elf', *self.args, '-', '-o', '-'], input=src.encode('utf-8'), + preexec_fn=_block_sigusr2) def compile(self, src:str) -> bytes: return jit_loader(self.compile_to_obj(src))