From d3500af71b76dee2ad3224576ea54a0348939f2a Mon Sep 17 00:00:00 2001 From: George Hotz <72895+geohot@users.noreply.github.com> Date: Fri, 25 Oct 2024 13:58:48 +0700 Subject: [PATCH] move consts last in uop toposort (#7290) * move consts last in uop toposort * consts first in toposort --- tinygrad/codegen/linearize.py | 5 ++++- tinygrad/ops.py | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tinygrad/codegen/linearize.py b/tinygrad/codegen/linearize.py index 1f7d3fa32a..b7c4d26ea5 100644 --- a/tinygrad/codegen/linearize.py +++ b/tinygrad/codegen/linearize.py @@ -41,8 +41,11 @@ def linearize_uop(sink:UOp, skip_check:bool=not __debug__) -> List[UOp]: priority += u.arg[0] for p in range_phi[u]: priority += 10000*len([r for r in range_srcs[p] if not any(i in range_phi[u] for i in range_phi[r])]) - # prefer uops that are loop children + elif u.op is UOps.CONST: + # place consts first here, they don't do anything and it can cause issues with DEFINE_ACC + priority -= 100000000000 else: + # prefer uops that are loop children priority -= sum([(l.arg[0]+1) + 1000*l.arg[1] for l,ss in scope_children.items() if l.op is UOps.RANGE and u in ss]) if u.op is UOps.IF and len(u.src) == 1: priority += 10000000 # if penalty return priority diff --git a/tinygrad/ops.py b/tinygrad/ops.py index 61476c98d4..3d8d680ac2 100644 --- a/tinygrad/ops.py +++ b/tinygrad/ops.py @@ -101,10 +101,6 @@ def identity_element(op:BinaryOps, dt:DType): return dtypes.as_const({BinaryOps. # the order of these UOps controls the order of the toposort class UOps(FastEnum): - # consts! - VCONST = auto() - CONST = auto() - # uops that aren't rendered SINK = auto() CONTIGUOUS = auto() @@ -156,6 +152,10 @@ class UOps(FastEnum): ENDRANGE = auto() ENDIF = auto() + # consts last! + VCONST = auto() + CONST = auto() + BUFFER_UOPS = {UOps.LOAD, UOps.PRELOAD, UOps.STORE, UOps.VALID} COMMUTATIVE = {BinaryOps.ADD, BinaryOps.MUL, BinaryOps.MAX, BinaryOps.CMPNE, BinaryOps.XOR, BinaryOps.AND, BinaryOps.OR} END_FOR_UOP = {UOps.IF:(UOps.STORE, UOps.ENDIF), UOps.RANGE:(UOps.ASSIGN, UOps.ENDRANGE)}