Skip to content

Commit faa8e2a

Browse files
committed
Use ObjectFile.jl for the CUBIN section lookup and stop reformatting PTX
1 parent 22f2252 commit faa8e2a

10 files changed

Lines changed: 27 additions & 102 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 5daeebd0aa500df005862f53e2ab8e8eefa06812
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit cb024c4fc15097e5fe29fc761209e2da5ca2451c

‎.claude/worktrees/range-to-tile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 3adb16b2e57c6ffbc9fdd958f90f2f3e0cf0b6a8

‎.claude/worktrees/tile-literals‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 7b7bc4d4bc2eb4034d31e7ddf24433f453d07e66

‎.claude/worktrees/tile-range-ctor‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 22673004e9e67be4d47f6e828c75c4651beea277

‎.claude/worktrees/tile-ranges‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit c8e4ce9e80ef8e0a2208e98925156d6ab4e34a92

‎Project.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ GPUArrays = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
1818
GPUToolbox = "096a3bc2-3ced-46d0-87f4-dd12716f4bfc"
1919
IRStructurizer = "93e32bba-5bb8-402b-805d-ffb066edee93"
2020
LMDB = "11f193de-5e89-5f17-923a-7d207d56daf9"
21+
ObjectFile = "d8793406-e978-5875-9003-1fc021f44a92"
2122
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
2223
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
2324
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
@@ -47,6 +48,7 @@ GPUToolbox = "3"
4748
IRStructurizer = "0.6.3"
4849
LMDB = "3"
4950
Microfloats = "0.2"
51+
ObjectFile = "0.5"
5052
PrecompileTools = "1"
5153
Preferences = "1.5"
5254
REPL = "1"

‎docs/src/man/debugging.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ code, disassembled with `nvdisasm`. Both compile the Tile IR with `tileiras`
6666
but do not need a GPU: `sm_arch` may be omitted when a CUDA device is
6767
available, and must be given explicitly otherwise.
6868

69-
The PTX is recovered from a debug section `tileiras` embeds in the CUBIN, in
70-
which comment lines are blanked; the code itself is unaffected.
69+
The PTX is read from a debug section `tileiras` embeds in the CUBIN.
7170

7271
Note that `tileiras` always generates architecture-specific code: targeting
7372
`sm_arch=v"10.0"` produces `sm_100a` PTX and SASS, which only runs on that

‎src/compiler/reflection.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ thread-level SIMT program the tile-level kernel is lowered to, with every
230230
compiler decision (thread mapping, CTA size, pipelining, synchronization)
231231
already made. When no GPU is available, pass `sm_arch` explicitly.
232232
233-
The PTX is recovered from the `.nv_debug_ptx_txt` section the assembler embeds
234-
in the CUBIN when compiling with line info, so header comments are blanked.
233+
The PTX is read from the `.nv_debug_ptx_txt` section the assembler embeds in
234+
the CUBIN when compiling with line info.
235235
"""
236236
function code_ptx(io::IO, @nospecialize(f), @nospecialize(argtypes); kwargs...)
237237
cubin = compile_to_cubin(f, argtypes; kwargs...)

‎src/launch.jl‎

Lines changed: 16 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# CUDA context, and launches it via `cudacall`. Compilation is cached per
66
# `(MethodInstance, sm_arch, opt_level, num_ctas, occupancy, num_worker_warps, bytecode_version)`.
77

8+
using ObjectFile: readmeta, Sections
89
using CUDACore: CUDACore, CuArray, CuModule, CuFunction, cudacall, device, capability,
910
AbstractBackend, AbstractKernel, kernel_convert, kernel_compile
1011
using CUDA_Compiler_jll
@@ -369,110 +370,27 @@ function run_tileiras(bytecode::Vector{UInt8}, sm_arch::VersionNumber,
369370
end
370371
end
371372

372-
# Minimal ELF64 section lookup. Binutils does not recognize the CUBIN machine
373-
# type, so locate the section by parsing the headers directly. Returns the
374-
# section contents, or `nothing` if absent or not a little-endian ELF64 file.
375-
function elf_section(elf::Vector{UInt8}, name::String)
376-
read_at(T, pos) = reinterpret(T, @view elf[pos+1:pos+sizeof(T)])[]
377-
length(elf) >= 64 || return nothing
378-
(view(elf, 1:4) == b"\x7fELF" && elf[5] == 2 && elf[6] == 1) || return nothing
379-
shoff = Int(read_at(UInt64, 0x28))
380-
shentsize = Int(read_at(UInt16, 0x3a))
381-
shnum = Int(read_at(UInt16, 0x3c))
382-
shstrndx = Int(read_at(UInt16, 0x3e))
383-
(shstrndx < shnum && shoff + shnum * shentsize <= length(elf)) || return nothing
384-
strtab = Int(read_at(UInt64, shoff + shstrndx * shentsize + 0x18))
385-
for i in 0:shnum-1
386-
header = shoff + i * shentsize
387-
name_pos = strtab + Int(read_at(UInt32, header))
388-
name_end = findnext(iszero, elf, name_pos + 1)
389-
name_end === nothing && continue
390-
String(elf[name_pos+1:name_end-1]) == name || continue
391-
offset = Int(read_at(UInt64, header + 0x18))
392-
size = Int(read_at(UInt64, header + 0x20))
393-
offset + size <= length(elf) || return nothing
394-
return elf[offset+1:offset+size]
395-
end
396-
return nothing
397-
end
398-
399373
"""
400-
extract_ptx(cubin::Vector{UInt8}) -> String
401-
402-
The PTX source embedded in `cubin`. [`run_tileiras`](@ref) compiles with
403-
`--lineinfo`, which makes the assembler record the PTX it consumed in a
404-
`.nv_debug_ptx_txt` section (used by `cuda-gdb` to display PTX-level source).
405-
The section stores one NUL-terminated entry per PTX line, with comment and
406-
`.loc` lines blanked and leading whitespace stripped; runs of blank lines are
407-
collapsed and the original indentation is reconstructed to match what
408-
`tileiras` wrote before embedding: one tab per brace level in PTX code with
409-
labels and debug-`.section` contents flush-left, and — in the
410-
`.nv_intermediate_source_section` that CUDA 13.4+ emits — two-space
411-
scaffolding around a verbatim Tile IR block, identical to
412-
[`code_tiled`](@ref) output.
374+
extract_ptx(io::IO) -> String
375+
extract_ptx(cubin::AbstractVector{UInt8}) -> String
376+
377+
The PTX recorded in a CUBIN. [`run_tileiras`](@ref) compiles with
378+
`--lineinfo`, which makes the assembler embed the PTX it consumed in a
379+
`.nv_debug_ptx_txt` section (one NUL-terminated entry per line, used by
380+
`cuda-gdb` for PTX-level source display).
413381
"""
414-
function extract_ptx(cubin::Vector{UInt8})
415-
section = elf_section(cubin, ".nv_debug_ptx_txt")
382+
function extract_ptx(io::IO)
383+
# CUBINs are ELF64 files with a machine type binutils does not know;
384+
# ObjectFile.jl only needs the section headers.
385+
handle = only(readmeta(io))
386+
section = findfirst(Sections(handle), ".nv_debug_ptx_txt")
416387
section === nothing && error(
417388
"CUBIN does not embed PTX (no .nv_debug_ptx_txt section); " *
418389
"it was compiled without line info")
419-
lines = split(replace(String(section), '\0' => '\n'), '\n')
420-
io = IOBuffer()
421-
depth = 0
422-
mlir_base = -1 # depth of `.nv_intermediate_source_section`, or -1
423-
src_base = -1 # depth of `.source_begin`, or -1 outside the block
424-
section_base = -1 # depth of a debug `.section` block, or -1
425-
pending_section = false
426-
blank_run = true # suppress leading blanks; collapse runs
427-
for line in lines
428-
if isempty(line)
429-
blank_run || println(io)
430-
blank_run = true
431-
continue
432-
end
433-
blank_run = false
434-
closer = startswith(line, '}') || startswith(line, ')')
435-
d = max(closer ? depth - 1 : depth, 0)
436-
opens = count(==('{'), line)
437-
# Parentheses nest only as function-header parameter lists: a line
438-
# ending in `(` opens one, a line starting with `)` closes it.
439-
depth += opens - count(==('}'), line) +
440-
(endswith(line, '(') ? 1 : 0) - (startswith(line, ')') ? 1 : 0)
441-
442-
indent = if mlir_base >= 0
443-
src_base >= 0 ? " "^max(d - src_base, 0) :
444-
" "^max(d - mlir_base, 0)
445-
elseif pending_section || startswith(line, ".section") ||
446-
(section_base >= 0 && closer)
447-
# NVVM writes the `.section` directive and its braces with one
448-
# tab, the block contents flush-left.
449-
"\t"
450-
elseif section_base >= 0 || occursin(r"^[\w\$]+:$", line)
451-
""
452-
else
453-
"\t"^d
454-
end
455-
println(io, indent, line)
456-
457-
if startswith(line, ".nv_intermediate_source_section")
458-
mlir_base = d
459-
elseif mlir_base >= 0
460-
line == ".source_begin" && (src_base = d)
461-
line == ".source_end" && (src_base = -1)
462-
depth <= mlir_base && (mlir_base = -1; src_base = -1)
463-
elseif pending_section || startswith(line, ".section")
464-
if opens > 0
465-
section_base = d
466-
pending_section = false
467-
else
468-
pending_section = startswith(line, ".section")
469-
end
470-
elseif section_base >= 0 && depth <= section_base
471-
section_base = -1
472-
end
473-
end
474-
return rstrip(String(take!(io)), '\n') * "\n"
390+
lines = split(String(read(section)), '\0'; keepempty=false)
391+
return join(lines, '\n') * "\n"
475392
end
393+
extract_ptx(cubin::AbstractVector{UInt8}) = extract_ptx(IOBuffer(cubin))
476394

477395
"""
478396
nvdisasm_cmd(args...) -> Cmd

0 commit comments

Comments
 (0)