From 1eb110cd7d0cde6d02a9d37e2c5d6562a673e91b Mon Sep 17 00:00:00 2001 From: Christopher Milan Date: Sat, 17 Jan 2026 23:05:12 -0800 Subject: [PATCH] fix memory corruption in NIR, reenable process replay (#14204) --- .github/workflows/test.yml | 2 -- tinygrad/helpers.py | 1 - tinygrad/renderer/nir.py | 6 ++++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ecaacaf116..09d320215f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -759,7 +759,6 @@ jobs: - name: Run TRANSCENDENTAL math run: TRANSCENDENTAL=2 python -m pytest -n=auto test/test_ops.py::TestOps::test_sin test/test_ops.py::TestOps::test_cos test/test_ops.py::TestOps::test_tan test/test_ops.py::TestOps::test_exp test/test_ops.py::TestOps::test_log --durations=20 - name: Run process replay tests - if: matrix.backend != 'lvp' uses: ./.github/actions/process-replay # ****** OSX Tests ****** @@ -889,7 +888,6 @@ jobs: - name: Run pytest (${{ matrix.backend }}) run: python3 -m pytest -n=auto test/ --ignore=test/models --ignore=test/unit --durations=20 - name: Run process replay tests - if: matrix.backend != 'lvp' uses: ./.github/actions/process-replay - name: Run macOS-specific unit test if: matrix.backend == 'cpu' diff --git a/tinygrad/helpers.py b/tinygrad/helpers.py index dfcea9761d..19231d6675 100644 --- a/tinygrad/helpers.py +++ b/tinygrad/helpers.py @@ -469,7 +469,6 @@ def to_mv(ptr:int, sz:int) -> memoryview: return memoryview((ctypes.c_uint8 * sz def mv_address(mv): return ctypes.addressof(ctypes.c_char.from_buffer(mv)) def to_char_p_p(options: list[bytes], to_type=ctypes.c_char): return (ctypes.POINTER(to_type) * len(options))(*[ctypes.cast(ctypes.create_string_buffer(o), ctypes.POINTER(to_type)) for o in options]) -def charptr(s:str|bytes): return ctypes.cast(ctypes.c_char_p(s if isinstance(s, bytes) else s.encode()), ctypes.POINTER(ctypes.c_char)) def flat_mv(mv:memoryview): return mv if len(mv) == 0 else mv.cast("B", shape=(mv.nbytes,)) # *** tqdm diff --git a/tinygrad/renderer/nir.py b/tinygrad/renderer/nir.py index 4c2abce6f7..b562f16743 100644 --- a/tinygrad/renderer/nir.py +++ b/tinygrad/renderer/nir.py @@ -1,10 +1,11 @@ from typing import Callable, cast, Any from tinygrad.dtype import AddrSpace, DType, PtrDType, ImageDType, dtypes -from tinygrad.helpers import DEBUG, OSX, unwrap, charptr, fromimport +from tinygrad.helpers import DEBUG, OSX, unwrap, fromimport from tinygrad.renderer import Renderer from tinygrad.renderer.cstyle import CUDARenderer from tinygrad.uop.ops import GroupOp, Ops, UOp, PatternMatcher, UPat, range_str from tinygrad.runtime.autogen import mesa +from tinygrad.runtime.support.c import POINTER import base64, ctypes, ctypes.util, struct, functools, inspect, contextlib, itertools def g(s:str): return getattr(mesa, s) @@ -187,7 +188,8 @@ class NIRRenderer(Renderer): elif u.op is Ops.AFTER: self.r[u] = self.r[u.src[0]] elif u.op == Ops.SINK: - if u.arg is not None: self.b.shader.contents.info.name = charptr(u.arg.function_name.encode()) + if u.arg is not None: + self.b.shader.contents.info.name = ctypes.cast(ctypes.create_string_buffer(u.arg.function_name.encode()), POINTER[ctypes.c_char]) elif u.op == Ops.DEFINE_LOCAL: self.r[u] = nimm(self.b, self.b.shader.contents.info.shared_size, dtypes.long) self.b.shader.contents.info.shared_size += u.dtype.nbytes()