|
5 | 5 | # CUDA context, and launches it via `cudacall`. Compilation is cached per |
6 | 6 | # `(MethodInstance, sm_arch, opt_level, num_ctas, occupancy, num_worker_warps, bytecode_version)`. |
7 | 7 |
|
| 8 | +using ObjectFile: readmeta, Sections |
8 | 9 | using CUDACore: CUDACore, CuArray, CuModule, CuFunction, cudacall, device, capability, |
9 | 10 | AbstractBackend, AbstractKernel, kernel_convert, kernel_compile |
10 | 11 | using CUDA_Compiler_jll |
@@ -369,110 +370,27 @@ function run_tileiras(bytecode::Vector{UInt8}, sm_arch::VersionNumber, |
369 | 370 | end |
370 | 371 | end |
371 | 372 |
|
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 | | - |
399 | 373 | """ |
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). |
413 | 381 | """ |
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") |
416 | 387 | section === nothing && error( |
417 | 388 | "CUBIN does not embed PTX (no .nv_debug_ptx_txt section); " * |
418 | 389 | "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" |
475 | 392 | end |
| 393 | +extract_ptx(cubin::AbstractVector{UInt8}) = extract_ptx(IOBuffer(cubin)) |
476 | 394 |
|
477 | 395 | """ |
478 | 396 | nvdisasm_cmd(args...) -> Cmd |
|
0 commit comments