move consts last in uop toposort (#7290)

* move consts last in uop toposort

* consts first in toposort
This commit is contained in:
George Hotz 2024-10-25 13:58:48 +07:00 committed by GitHub
commit d3500af71b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 5 deletions

View file

@ -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

View file

@ -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)}