Skip to content

Tales from my reverse engineering of my NVIDIA GPU, bye to MLIR and livin la vida loca - #176

Merged
Zaneham merged 42 commits into
masterfrom
ggml-sass
Oct 26, 2026
Merged

Zaneham merged 42 commits into
masterfrom
ggml-sass

Conversation

@Zaneham

@Zaneham Zaneham commented Oct 26, 2026

Copy link
Copy Markdown
Owner

With this, all of ggml-cuda compiles down through Booth, which was great!

This is the big chunk of ggml-cuda work that's been sitting on my machine. Templates handed out through function pointers now deduce and instantiate, ternaries only run the arm they take, pointers step by elements, ldmatrix and mma.sync reach SASS as LDSM/HMMA/IMMA, plus a pile of smaller NVPTX and SASS fixes along the way.

I'm moving the MLIR frontend out for now because 1) Ya boy is tired and 2) I think I need to contribute to MLIR over in LLVM for a bit so I can understand what's going on with this thing. I liked experimenting with it and sure learnt quite a bit but it was a huge amount of code that I had to think about every time I fiddled with my own Intermediate representation.

A __shared__ array passed to a __device__ helper kept the callee's
ptr<global> type once mem2reg forwarded it past the parameter copy,
so isel picked a global access for shared memory.
sasplit only split at the pair form, a predicated branch directly before
the closing branch. isel puts a phi copy between the two, so the block
stayed whole, the taken edge never reached the liveness graph, and a
value used only on that edge lost its registers straight after its def.
rope_neox read row_indices out of a pair that by then held row_dst and
i3, which is the null load the ggml run died on.
scan_launches never registered the locals of the body it walked, so an
argument naming one fell through to sema, which types an unresolved
name as int. set_rows_cuda<src_t, idx_t> deduced its inner call from
const src_t * and const idx_t * locals and built k_set_rows<int, int>
three times over instead of the twelve real instances. vscn already
does this for tscan.
A struct lives in a frame slot here, but is_phi took its register file
from the type and moved it as a u32, which the verifier refused as
E840. Each predecessor now hands the merge the address of its value's
slot through the ordinary phi copy, and the merge copies into a slot of
the phi's own, so a value redefined round a loop cannot change under
it. A phi fed by another phi of the same block would need the copies
ordered, and one fed by a constant has no slot, so both refuse by name.
All sixteen mmf instances stopped here.
A binding holds a BIR type, which has a width and no C identity, so
the host registration has to guess a spelling. long long named a
second instantiation on Linux, where int64_t is long, and the launch
went to a stub nothing had registered: invalid device function from
set_rows and cpy. int64_t is what nearly all CUDA source writes and is
right on both ABIs when it does. The prologue brings in stdint.h so
the name is always there. Interim, the spelling is still a guess until
bindings carry C identity or the stubs key on Booth's own type.
asfix walked gep and bitcast only. A ternary over two pointers is a
select typed from the declared char *, so it read as global, the pass
stopped there, and cfold then folded the constant condition away and
left a global cast of a shared pointer. mmf's tile is exactly that,
compute_base = has_ids ? shmem_base + n : shmem_base, and every store
into it came out st.global through a shared offset, which on Latimer is
a store to nought.

The pass is a meet now. Roots keep the space they were made with, a
derived pointer takes the one its operands agree on, an operand not yet
known is passed over so a pointer stepped round a loop settles, and a
real disagreement stays as declared.
trace_to_param said it walked alloca-load chains and stopped dead at the
alloca, so a pointer param mem2reg had left in its entry slot was never
seen as read and its channel went missing. It follows the one store that
fills the slot now. A load or store whose own address is a local is not
an access through a param either, so those are passed over, or a scalar
read back out of its slot counted as a channel.
… memory

tpure let a subscript, a deref and an arrow through as safe to evaluate
on both sides, so p ? p[i] : x loaded through p before asking whether p
was there. soft_max does exactly that with sinks, which Qwen2 does not
have, and the load went to nought. canonical.cu has said as much in a
comment all along. Arms made of values still fold to a select, and
rpi315 pins both halves now.
rslv_p cast every integer constant to a 32-bit immediate, so i *
3000000007LL multiplied by -1294967289. One that does not survive the
cast is put together in a register now, low half zero-extended, high
half shifted up, the two ored. A phi cannot take one, its copy runs in
the predecessor and the register would be made in the merge, so that
refuses by name.
p + n was a gep and the other four spellings were an add or a sub on
the pointer, which every back end read as bytes. x += row * stride
moved a float pointer a quarter of the way, so any kernel that steps
its arguments per block wrote over its neighbours: rms_norm, get_rows,
set_rows, cpy, soft_max and mul_mat, which is most of ggml. One block
hides it because row is nought. They are all a gep now, and anything
else done to a pointer in place refuses with E102.
One comment line, asked for, so the comment row of the ratchet goes to
6510 with it.
There is no 8-bit register file, so an i8 load is a u8 into a 16-bit
register, and sext and sitofp then extended from 16. -120 came back as
136. Every q8_0 quant is an int8, so get_rows on a Q8_0 embedding table
handed the model the wrong vector for every token, and the K-quants
went with it. A signed widen of an 8-bit value goes through shl 24 and
an arithmetic shr 24 first now. Unsigned bytes were right already.
make_half2(float, float) stored two floats into two halves as they
came, four bytes into a two byte slot each, so the pair read back as
whatever the second write left and every product in the half2 branch
of mul_mat_vec_f was nought. Both constructor sites go through stfit
now, the same coercion an assignment gets. Found by a probe that
printed the products, after the PTX and SASS images were both wrong the
same way, which put it in the front end.
0b2129f called stfit inside set_op, after the store was already
emitted, so the fptrunc came out one instruction too late and the
store read a value nothing had written yet. Every make_half2 was a
pair of noughts, which is the same symptom as before with a different
cause. rpi322 now checks the order as well as the presence.
arg_type saw through a subscript to its element but not through a
dereference, so conv<D>(*(const S *) ci) deduced S as int and
ggml_cuda_cast<float>(float) went through fptosi. Every scalar copy in
ggml, cpy_1_scalar, did that, so the KV cache held floats rounded to
whole numbers. The pointee lookup is shared between the two shapes now.
@Zaneham Zaneham added enhancement New feature or request CUDA CUDA frontend and CUDA-specific behaviour backend: NVIDIA NVIDIA PTX backend Fortran Booths Fortran support. MLIR MLIR frontend (core dialect subset) labels Oct 26, 2026
@Zaneham
Zaneham merged commit dcf0b80 into master Oct 26, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend: NVIDIA NVIDIA PTX backend CUDA CUDA frontend and CUDA-specific behaviour enhancement New feature or request Fortran Booths Fortran support. MLIR MLIR frontend (core dialect subset)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant