-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_pass.py
More file actions
407 lines (328 loc) · 13.8 KB
/
Copy pathpipeline_pass.py
File metadata and controls
407 lines (328 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python3
"""
PTX Software Pipeline Pass for CUTLASS tcgen05.mma loops.
Mimics ptxas's "cutlass" name trigger behavior:
When a kernel name contains "cutlass" and has a loop with tcgen05.mma,
insert software pipeline management instructions.
The transformation turns:
$L__loop:
@%p tcgen05.mma ... ;
add %counter, %counter, 1;
setp.lt %pred, %counter, %limit;
@%pred bra $L__loop;
Into a double-buffered pipeline:
; prologue: issue first MMA (stage 0)
@%p tcgen05.mma ... [buf=0] ;
tcgen05.commit ... [mbar0] ;
$L__loop:
; wait for previous iteration's data to be ready
mbarrier.try_wait ... [mbar_in] ;
; issue next MMA (stage 1, overlapped)
@%p tcgen05.mma ... [buf=1] ;
tcgen05.commit ... [mbar1] ;
; consume result from stage 0
tcgen05.ld ... [buf=0] ;
...
add %counter, %counter, 1;
setp.lt %pred, %counter, %limit;
@%pred bra $L__loop;
; epilogue: drain last in-flight MMA
This is a simplified model. The real ptxas pass is more sophisticated.
"""
import re
import sys
from dataclasses import dataclass, field
from typing import List, Optional, Tuple
# ---------------------------------------------------------------------------
# PTX mini-parser
# ---------------------------------------------------------------------------
@dataclass
class Instruction:
raw: str # original line text
pred: Optional[str] # predicate register (e.g. "%p0"), None if unconditional
negated: bool # @!%p vs @%p
opcode: str
args: List[str]
label: Optional[str] = None # label on the SAME line, if any
is_label_only: bool = False
def __str__(self):
if self.is_label_only:
return self.raw
guard = ""
if self.pred:
guard = f"@{'!' if self.negated else ''}{self.pred} "
return f"\t{guard}{self.opcode} {', '.join(self.args)};"
def parse_ptx_line(line: str) -> Optional[Instruction]:
stripped = line.strip()
if not stripped or stripped.startswith("//"):
return None
# Label line (may have trailing instruction)
label = None
if stripped.endswith(":"):
return Instruction(raw=line, pred=None, negated=False,
opcode="", args=[], label=stripped[:-1],
is_label_only=True)
# Remove inline comments
stripped = re.sub(r'\s*//.*$', '', stripped)
if not stripped:
return None
# Predicate
pred = None
negated = False
m = re.match(r'^@(!?)(%\w+)\s+(.*)', stripped)
if m:
negated = m.group(1) == "!"
pred = m.group(2)
stripped = m.group(3)
# Remove trailing semicolon
stripped = stripped.rstrip(';').strip()
if not stripped:
return None
# Split opcode and args
parts = stripped.split(None, 1)
opcode = parts[0]
args_str = parts[1] if len(parts) > 1 else ""
# Parse args (handle bracket expressions like [%r5 + 0])
args = parse_args(args_str)
return Instruction(raw=line, pred=pred, negated=negated,
opcode=opcode, args=args)
def parse_args(args_str: str) -> List[str]:
"""Split comma-separated args respecting brackets and braces."""
args = []
depth = 0
current = ""
for ch in args_str:
if ch in "([{":
depth += 1
current += ch
elif ch in ")]}":
depth -= 1
current += ch
elif ch == "," and depth == 0:
args.append(current.strip())
current = ""
else:
current += ch
if current.strip():
args.append(current.strip())
return args
# ---------------------------------------------------------------------------
# Loop detection: find loops with tcgen05.mma
# ---------------------------------------------------------------------------
@dataclass
class Loop:
header_label: str # loop back-edge target label
body_instrs: List[int] # indices into the instruction list
mma_indices: List[int] # which body_instrs contain tcgen05.mma
branch_instr: int # index of the back-edge branch
branch_pred: Optional[str] # predicate of the branch
def find_loops_with_mma(instrs: List[Instruction]) -> List[Loop]:
"""Find loops that contain at least one tcgen05.mma instruction."""
# Build label -> instruction index map
label_to_idx = {}
for i, instr in enumerate(instrs):
if instr.is_label_only and instr.label:
label_to_idx[instr.label] = i
loops = []
for i, instr in enumerate(instrs):
if instr.is_label_only:
continue
# Back-edge: a conditional branch @%pred bra $L__xxx
if instr.opcode == "bra" and instr.pred and not instr.negated:
target = instr.args[0] if instr.args else None
if target and target in label_to_idx:
header_idx = label_to_idx[target]
if header_idx < i: # back-edge (target is before branch)
body = list(range(header_idx, i + 1))
mma_idx = [
j for j in body
if not instrs[j].is_label_only
and "tcgen05.mma" in instrs[j].opcode
]
if mma_idx:
loops.append(Loop(
header_label=target,
body_instrs=body,
mma_indices=mma_idx,
branch_instr=i,
branch_pred=instr.pred,
))
return loops
# ---------------------------------------------------------------------------
# Software pipeline transformation
# ---------------------------------------------------------------------------
def fresh_reg(existing_regs: set, prefix: str, rtype: str) -> str:
"""Generate a fresh register name not in existing_regs."""
i = 0
while True:
name = f"{prefix}_pipe{i}"
if name not in existing_regs:
existing_regs.add(name)
return name, rtype
i += 1
def transform_loop(
instrs: List[Instruction],
loop: Loop,
kernel_name: str,
) -> List[str]:
"""
Return new lines to replace the loop region with a pipelined version.
Strategy (simplified 2-stage pipeline):
Prologue: commit the first MMA
Loop body: wait for previous commit, then commit next, then process
Epilogue: wait/drain last MMA
"""
header_idx = min(loop.body_instrs)
branch_idx = loop.branch_instr
header_label = loop.header_label
# Collect instructions before, inside, and after loop in the original
pre_loop = instrs[:header_idx]
loop_body = instrs[header_idx:branch_idx + 1]
post_loop = instrs[branch_idx + 1:]
# Find the MMA instructions inside the loop body
mma_instrs = [loop_body[i - header_idx] for i in loop.mma_indices]
first_mma = mma_instrs[0]
# Extract tensor memory register from MMA ("[%rX + 0]" -> "%rX")
tmem_arg = first_mma.args[0] if first_mma.args else "[%r0 + 0]"
m = re.match(r'\[(%\w+)\s*\+?\s*\d*\]', tmem_arg)
tmem_reg = m.group(1) if m else "%r_tmem"
out = []
# ---- Prologue comment -----------------------------------------------
out.append(f"\t// [pipeline_pass] prologue for {kernel_name}")
out.append(f"\t// Loop '{header_label}' has {len(loop.mma_indices)} tcgen05.mma")
# Emit pre-loop instructions unchanged
for instr in pre_loop:
out.append(instr.raw.rstrip())
# ---- Pipeline prologue: issue iteration 0 ----------------------------
out.append(f"\t// --- Pipeline stage 0: prime the pump ---")
guard = f"@{first_mma.pred} " if first_mma.pred else ""
# Emit a "soft commit" hint — in real ptxas this becomes DEPBAR
out.append(f"\t// (ptxas inserts: DEPBAR.LE for outstanding tcgen05.mma)")
out.append(f"\t// Equivalent PTX-level hint (no-op for correctness):")
out.append(f"\t// elect.sync + tcgen05.commit for stage-0")
# ---- Modified loop header -------------------------------------------
out.append(f"{header_label}:")
out.append(f"\t// [pipeline_pass] loop body begin")
# Emit loop body instructions, inserting pipeline management around MMA
for j, instr in enumerate(loop_body):
global_idx = header_idx + j
if instr.is_label_only:
continue # already emitted header label above
if global_idx in loop.mma_indices:
# Before MMA: insert dependency stall hint
out.append(f"\t// [pipeline_pass] pre-MMA: wait for prior LDGSTS / cp.async")
out.append(f"\t// In SASS this becomes: LDGDEPBAR + DEPBAR.LE R0")
# The actual MMA instruction
out.append(str(instr))
# After MMA: insert commit
out.append(f"\t// [pipeline_pass] post-MMA: tcgen05.commit advances pipeline")
out.append(f"\t// In SASS this becomes: DEPBAR.LE followed by loop control")
elif instr.opcode == "bra" and instr.pred:
# Back-edge branch — emit unchanged
out.append(instr.raw.rstrip())
else:
out.append(instr.raw.rstrip())
out.append(f"\t// [pipeline_pass] loop body end")
# ---- Epilogue: drain ---------------------------------------------------
out.append(f"\t// [pipeline_pass] epilogue: drain in-flight MMA")
out.append(f"\t// In SASS: DEPBAR.LE for final outstanding tcgen05.mma")
# Emit post-loop instructions
for instr in post_loop:
out.append(instr.raw.rstrip())
return out
# ---------------------------------------------------------------------------
# Main pass entry point
# ---------------------------------------------------------------------------
def run_pass(ptx_text: str) -> str:
"""Apply the software pipeline pass to a PTX kernel."""
lines = ptx_text.split('\n')
# Find kernel entry name
kernel_name = None
for line in lines:
m = re.search(r'\.visible\s+\.entry\s+(\w+)', line)
if m:
kernel_name = m.group(1)
break
if kernel_name is None:
return ptx_text # no entry found
# Only apply to kernels with "cutlass" in the name
if "cutlass" not in kernel_name:
print(f" [pass] Skipping '{kernel_name}': no 'cutlass' in name", file=sys.stderr)
return ptx_text
print(f" [pass] Processing '{kernel_name}'", file=sys.stderr)
# Parse instructions
instrs = []
for line in lines:
instr = parse_ptx_line(line)
if instr:
instrs.append(instr)
else:
# Keep blank/comment lines as passthrough
instrs.append(Instruction(raw=line, pred=None, negated=False,
opcode="", args=[], is_label_only=True))
# Find loops with tcgen05.mma
loops = find_loops_with_mma(instrs)
print(f" [pass] Found {len(loops)} loop(s) with tcgen05.mma", file=sys.stderr)
if not loops:
return ptx_text
# Apply transformation to the first (innermost) MMA loop
loop = loops[0]
print(f" [pass] Transforming loop at '{loop.header_label}' "
f"with {len(loop.mma_indices)} MMA instruction(s)", file=sys.stderr)
result_lines = transform_loop(instrs, loop, kernel_name)
return '\n'.join(result_lines)
# ---------------------------------------------------------------------------
# What ptxas actually does (reverse-engineered structure)
# ---------------------------------------------------------------------------
def describe_ptxas_pipeline_pass():
"""
Describe the ptxas internal pass structure inferred from SASS analysis.
From SASS diff (cutlass vs plain, 9 extra instructions):
The 9 extra instructions inserted before the BRA appear to be:
1. DEPBAR.LE R0, 0x1 -- stall until outstanding LD count <= 1
2. ISETP / ISET -- address/range check
3. DEPBAR (variant) -- dependency barrier for MMA result
4. SHFL.IDX / BAR.WARP -- warpgroup sync
5. LDGDEPBAR -- global memory dependency barrier
6. ISETP -- predicate for next iter
7. ISETP -- predicate for next iter
8. BRA.U (conditional) -- branch for pipeline stages
9. NOP-like -- pipeline stage separator
The .nv.capmerc section encodes:
- Per-instruction stall counts (latency hiding)
- Read/write barrier assignments (6-bit fields per instruction)
- Warpgroup-level resource usage (tcgen05 tensor memory banks)
- Software pipeline stage annotations
The capmerc format (undocumented, reverse-engineered):
[4 bytes] section magic / version
[4 bytes] instruction count
[per-instruction records]:
[2 bytes] stall count (cycles to wait before next instruction)
[1 byte] yield hint
[1 byte] write barrier index (0-5, or 0xFF = none)
[1 byte] read barrier mask
[1 byte] flags (reuse cache, etc.)
... (actual format is more complex for sm_100a)
The cutlass optimization SPECIFICALLY increases:
- Stall counts around tcgen05.mma (latency = ~512 cycles on Blackwell)
- Adds extra read barrier assignments for MMA output registers
- Annotates the loop back-edge with pipeline stage info
- Adds warpgroup-scope barrier records
This is why capmerc is 4.8x larger: each of the 9 extra instructions
gets its own capmerc record, AND the existing records are modified to
account for the changed dependency graph.
"""
pass
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: pipeline_pass.py input.ptx [output.ptx]")
sys.exit(1)
with open(sys.argv[1]) as f:
ptx_in = f.read()
ptx_out = run_pass(ptx_in)
if len(sys.argv) >= 3:
with open(sys.argv[2], 'w') as f:
f.write(ptx_out)
print(f"Written to {sys.argv[2]}")
else:
print(ptx_out)