MCPcopy
hub / github.com/tinygrad/tinygrad / Kernel

Class Kernel

extra/gemm/amd_asm_matmul.py:169–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

167# =============================================================================
168
169class Kernel:
170 def __init__(self): self.instructions, self.labels, self.pos = [], {}, 0
171 def label(self, name): self.labels[name] = self.pos
172
173 def emit(self, inst, target=None):
174 self.instructions.append(inst)
175 inst._target, inst._pos = target, self.pos
176 self.pos += inst.size()
177 return inst
178
179 def waitcnt(self, lgkm=None, vm=None):
180 """Wait for memory operations. lgkm=N waits until N lgkm ops remain, vm=N waits until N vmem ops remain."""
181 vmcnt, lgkmcnt, expcnt = vm if vm is not None else 63, lgkm if lgkm is not None else 63, 7
182 waitcnt = (expcnt & 0x7) | ((lgkmcnt & 0x3f) << 4) | ((vmcnt & 0x3f) << 10)
183 self.emit(s_waitcnt(simm16=waitcnt))
184
185 def finalize(self):
186 """Patch branch offsets and return the finalized instruction list."""
187 for inst in self.instructions:
188 if inst._target is None: continue
189 offset_dwords = (self.labels[inst._target] - inst._pos - inst.size()) // 4
190 if not -32768 <= offset_dwords <= 32767: raise ValueError(f"branch to '{inst._target}' offset {offset_dwords} exceeds simm16 range")
191 inst.simm16 = offset_dwords
192 return self.instructions
193
194
195# =============================================================================

Callers 12

test_simpleMethod · 0.90
test_diamondMethod · 0.90
test_loopMethod · 0.90
test_loop_branchMethod · 0.90
test_loop_breakMethod · 0.90
test_switchMethod · 0.90
test_ping_pongMethod · 0.90
test_colored_blocksMethod · 0.90
test_jump_back_to_endMethod · 0.90
custom_handwrittenFunction · 0.90
custom_data_depsFunction · 0.90
build_kernelFunction · 0.70

Calls

no outgoing calls

Tested by 11

test_simpleMethod · 0.72
test_diamondMethod · 0.72
test_loopMethod · 0.72
test_loop_branchMethod · 0.72
test_loop_breakMethod · 0.72
test_switchMethod · 0.72
test_ping_pongMethod · 0.72
test_colored_blocksMethod · 0.72
test_jump_back_to_endMethod · 0.72
custom_handwrittenFunction · 0.72
custom_data_depsFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…