L2 Intermediate · Module 6: GEMM and memory optimization

6.8Reading PTX and SASS

CUDA C++ is compiled twice: to PTX, a portable virtual assembly, then to SASS, the machine code a specific GPU runs. Reading SASS is how you check what the compiler actually did: whether loads are vectorized, whether a loop was unrolled, whether a value spilled. This chapter teaches enough of both to answer those questions, using small kernels and the matmul from 6.2.

GPU required (Colab or a rented GPU)~25 min

By the end you will be able to

  • ✓Describe the CUDA compilation pipeline from .cu to PTX to SASS, and what each level is for
  • ✓Dump the PTX and SASS of a kernel, and map lines back to the source
  • ✓Recognize the common SASS instructions for loads, stores, arithmetic and control flow
  • ✓Use SASS to verify vectorization, __restrict__, unrolling, fast math and register tiling

Every chapter in this module asked “what did the compiler do?” This chapter shows how to look.

Two assembly languages

nvcc compiles device code in two steps:

  1. CUDA C++ → PTX. PTX (Parallel Thread Execution) is a virtual instruction set: typed, with unlimited virtual registers (%f1, %rd7), and stable across GPU generations. It is what the front end produces, and what Triton and other compilers emit too.
  2. PTX → SASS. ptxas turns PTX into SASS, the real machine code for one architecture (sm_86, sm_90 …). This is where registers are allocated, instructions are scheduled, and most optimization happens.

A binary usually carries SASS for the GPUs it was built for, plus PTX that the driver can compile at load time for newer ones. PTX shows what you asked for. SASS shows what runs.

Terminal window
nvcc -O2 -arch=sm_86 -ptx kernels.cu -o kernels.ptx # PTX, as text
nvcc -O2 -arch=sm_86 -cubin kernels.cu -o kernels.cubin # a GPU binary
cuobjdump -sass -fun saxpy kernels.cubin # its SASS, for one kernel

The lab wraps these in ./inspect.sh <kernel>. The online tool godbolt.org also shows both for CUDA.

One kernel, both levels

__global__ void saxpy(float a, const float* x, float* y, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) y[i] = a * x[i] + y[i];
}
PTX (abridged)
mad.lo.s32 %r1, %r3, %r4, %r5; // i = ctaid.x * ntid.x + tid.x
setp.ge.s32 %p1, %r1, %r2; // i >= n ?
@%p1 bra $L__BB0_2; // then skip to the end
mul.wide.s32 %rd5, %r1, 4; // byte offset = i * 4
ld.global.f32 %f2, [%rd6]; // x[i]
ld.global.f32 %f3, [%rd7]; // y[i]
fma.rn.f32 %f4, %f2, %f1, %f3; // a * x[i] + y[i]
st.global.f32 [%rd7], %f4; // y[i] = ...
SASS (sm_86)
S2R R4, SR_CTAID.X; // read blockIdx.x from a special register
S2R R3, SR_TID.X; // threadIdx.x
IMAD R4, R4, c[0x0][0x0], R3; // i = blockIdx.x * blockDim.x + threadIdx.x
ISETP.GE.AND P0, PT, R4, c[0x0][0x178], PT; // P0 = (i >= n)
@P0 EXIT; // predicated exit: threads with i >= n stop
IMAD.WIDE R2, R4, R5, c[0x0][0x168]; // &x[i] (64-bit)
IMAD.WIDE R4, R4, R5, c[0x0][0x170]; // &y[i]
LDG.E R2, [R2.64]; // load x[i] from global memory
LDG.E R7, [R4.64]; // load y[i]
FFMA R7, R2, c[0x0][0x160], R7; // a comes straight from constant bank 0
STG.E [R4.64], R7; // store y[i]
EXIT;

How to read it:

  • Registers are R0…R254 (32-bit), with RZ always zero. A .64 suffix on an address means a register pair.
  • Kernel parameters live in constant memory, c[0x0][...], and instructions read them directly as operands. blockDim.x is c[0x0][0x0].
  • Predicates P0…P6 hold booleans; @P0 runs an instruction only where P0 is true. Short branches become predication instead of jumps.
  • Names tell you the unit: LDG/STG global, LDS/STS shared, LDL/STL local (spills!), LDC constant; FFMA/FMUL/FADD FP32; HMMA tensor core; MUFU the special-function unit (exp2, rsqrt, sin); BAR barrier; S2R special registers.

What SASS can tell you

The lab has five pairs of kernels that differ in one detail. ./inspect.sh all counts prints each one’s instruction mix:

./inspect.sh all counts (sm_86, abridged)
saxpy LDG.E×2 IMAD.WIDE×2 STG.E×1 FFMA×1 ...
saxpy4 FFMA×4 LDG.E.128×2 IMAD.WIDE×2 STG.E.128×1 ...
scale_rows FMUL×8 LDG.E×7 STG.E×4 ...
scale_rows_restrict FMUL×8 STG.E×4 LDG.E.CONSTANT×4 ...
exp_accurate FFMA×2 MUFU.EX2×1 FFMA.SAT×1 FFMA.RM×1 FADD×1 FMUL×1 SHF.L.U32×1 ...
exp_fast FMUL×3 MUFU.EX2×1 FSETP.GEU.AND×1 ...
dot8_loop LDG.E×2 FFMA×1 IADD3×2 ISETP.NE.AND×1 BRA×2 ...
dot8_unrolled LDG.E×16 FFMA×8 ...

Vectorization. saxpy4 reads float4s, and the SASS has one LDG.E.128 per operand where saxpy had a 4-byte LDG.E: a quarter of the load instructions for the same bytes (4.7).

Aliasing. scale_rows computes out[r*4+j] = w[0] * w[j] * r for four j. Without __restrict__, the compiler must assume a store to out could change w, so it reloads w[0] after every store: 7 loads. With __restrict__, it loads each value once (4 loads) and marks them .CONSTANT, routing them through the read-only cache:

SASS: scale_rows (no __restrict__), excerpt
STG.E [R4.64], R7; // out[0] = ...
LDG.E R6, [R2.64]; // reload w[0]: the store might have changed it
LDG.E R9, [R2.64+0x4]; // w[1]

Fast math. Both exp kernels use the hardware MUFU.EX2 (2ˣ). expf first splits xlog⁡2ex \log_2 e into high and low parts with extra FMAs, for accuracy at large ∣x∣|x|; __expf (or -use_fast_math) skips that:

output
|x| <= 1 max relative error: expf 1.1e-07 __expf 5.5e-08
|x| <= 10 max relative error: expf 1.1e-07 __expf 3.9e-07
|x| <= 80 max relative error: expf 1.3e-07 __expf 3.6e-06

That’s 30× more error at ∣x∣=80|x| = 80, and usually fine for softmax, where inputs are x−max⁡≤0x - \max \le 0 (5.7).

Unrolling. With #pragma unroll 1, the dot product keeps a loop counter, a compare and a branch per iteration. Fully unrolled, it is 16 loads and 8 FMAs in a straight line, and all 16 loads can be in flight at once (6.6).

Reading the matmul

The real payoff is checking a hot loop. Here is the inner loop of the 6.2 mm_2d_vec kernel:

SASS: mm_2d_vec inner loop (sm_86), excerpt
STS.128 [R6.X4+0x1000], R92; // B tile → shared memory, 16 bytes
BAR.SYNC.DEFER_BLOCKING 0x0; // __syncthreads()
LDS.128 R76, [R2+0x1000]; // 4 values of A from shared memory
LDS.128 R80, [R3]; // 4 values of B
LDS.128 R72, [R2+0x1010]; // 4 more of A
FFMA R11, R76, R80, R84; // acc += a * b
FFMA R85, R80.reuse, R77, R85; // .reuse: keep R80 in the operand cache
FFMA R86, R80.reuse, R78, R86;
FFMA R87, R80.reuse, R79, R87;
...

Over the whole kernel there are 512 FFMA and 32 LDS.128: 16 FMAs per shared-memory instruction, which is the register tiling of 6.2 visible in the machine code. The .reuse flags show the compiler keeping a register in the operand-reuse cache across consecutive FMAs, saving register-file bandwidth. There are no LDL/STL (no spills), and the loads are 128-bit.

Lab: PTX and SASS 🔴

Terminal window
cd labs/m06/6.8
./inspect.sh saxpy # PTX and SASS
./inspect.sh all counts # instruction mix of every kernel
nvcc -O2 -arch=native kernels.cu -o kernels && ./kernels # runs and checks them

inspect.sh defaults to sm_86; set ARCH=sm_75 for a T4, sm_89 for RTX 40xx, sm_90 for H100. Compiling for a different architecture doesn’t need that GPU, so you can read H100 SASS on a laptop.

Sources

repo · BGEBEFORE-GPU-ENGINEERINGBEFORE-GPU-ENGINEERINGPhase 3, Ch 36–51: the compilation pipeline, PTX and SASS.
book · FREGLYAI Systems Performance EngineeringChris FreglyInspecting compiler output to verify optimizations.

Check your understanding

0 / 4 answered
  1. 1.What is the difference between PTX and SASS?

  2. 2.You see LDL and STL instructions inside a kernel's inner loop. What does that indicate?

  3. 3.Why did adding __restrict__ remove three loads from scale_rows?

  4. 4.The mm_2d_vec SASS has 512 FFMA and 32 LDS.128. What does that ratio show?