(c:UOp)
| 94 | return None |
| 95 | |
| 96 | def transform_precompiled_call(c:UOp) -> UOp|None: |
| 97 | if not c.arg.precompile: return None |
| 98 | assert c.src[0].op is Ops.TUPLE, f"expected TUPLE body for precompiled FUNCTION, got {c.src[0].op}" |
| 99 | input_buffers = tuple(x.contiguous() if x.op not in {Ops.AFTER, Ops.BIND} else x for x in c.src[1:]) |
| 100 | |
| 101 | # add the outputs to the call |
| 102 | srcs = c.src[0].src |
| 103 | resolved = [c.gettuple(i) for i in range(len(srcs))] |
| 104 | outs = tuple(r.empty_like() for r in resolved) |
| 105 | targets = [o.param_like(len(c.src)-1+i).shrink_to(s.shape) for i,(o,s) in enumerate(zip(outs, srcs))] |
| 106 | |
| 107 | subs:dict[UOp, UOp] = {} |
| 108 | items:list[UOp] = [] |
| 109 | for s, t in zip(srcs, targets): |
| 110 | after_deps:list[UOp] = [] |
| 111 | while s.op is Ops.AFTER: |
| 112 | after_deps.extend(s.src[1:]) |
| 113 | s = s.src[0] |
| 114 | base = s.base |
| 115 | if base.op in {Ops.CONTIGUOUS, Ops.BUFFER} and base.shape == t.shape and base not in subs: |
| 116 | subs[base] = t.after(t.store(base.src[0])) if base.op is Ops.CONTIGUOUS else t |
| 117 | items.append(s.after(*after_deps) if after_deps else s) |
| 118 | else: |
| 119 | items.append(t.after(t.store(s), *after_deps)) |
| 120 | fxn = UOp.sink(*(x.substitute(subs) for x in items)) |
| 121 | |
| 122 | # body switches from TUPLE to SINK, so the node becomes an opaque CALL (not FUNCTION) |
| 123 | new_call = UOp(Ops.CALL, c.dtype, (fxn, *input_buffers, *outs), c.arg) |
| 124 | rets = tuple(o.after(new_call) for o in outs) |
| 125 | |
| 126 | # if the CALL has symbolic shapes, shrink the max-sized output to the actual symbolic shape |
| 127 | # NOTE: must use resolved shapes from the FUNCTION (which substitutes PARAMs with external args), not raw body shapes |
| 128 | rets = tuple(r.shrink_to(rs.shape) for r,rs in zip(rets, resolved)) |
| 129 | |
| 130 | return UOp.maketuple(*rets) |
| 131 | |
| 132 | # NOTE: adding rules to here is bad. these all need to run before the schedule cache |
| 133 | pm_early_transform_tensor_graph = PatternMatcher([ |
nothing calls this directly
no test coverage detected
searching dependent graphs…