Skip to content

Link GPU device code to the conceptual stmt in the html output (simplified alternative to #9691) - #9713

Draft
mcourteaux wants to merge 1 commit into
mcourteaux/llvm-debug-symbols-altfrom
mcourteaux/gpu-debug-symbols-alt
Draft

mcourteaux wants to merge 1 commit into
mcourteaux/llvm-debug-symbols-altfrom
mcourteaux/gpu-debug-symbols-alt

Conversation

@mcourteaux

@mcourteaux mcourteaux commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #9712, which is stacked on #9711 and #9710. Draft: this is a simplified alternative to #9691, for comparing the two side by side.

What this does

The conceptual_stmt_html output links the device code of GPU kernels to the conceptual stmt it was made from, for CUDA (PTX) and for the APIs whose device code is C-like source (OpenCL, Metal, D3D12, WebGPU), in the same way as #9712 links the assembly: by line of IR, and within a line by IR node. Hover IR to see its device code, double-click to keep it shown and go there, and the same from device code to its IR.

The device code itself is unchanged, and has no line information in it. Device code that isn't text to read, like SPIR-V, is not linked.

How it works

Lowering prints the conceptual stmt with its source map before the GPU loops are offloaded, and each device code generator says where in it each line of its output came from: the line, and the IR node (CodeGen_GPU_Dev::source_lines). The Module keeps that per device code buffer, for the html.

  • C-like source: CodeGen_C::print_stmt() and print_expr() note where they print each Stmt and Expr, and where that is in the conceptual stmt. A shared helper turns that into a position per line of source. The line that assigns the value of an Expr is left to that Expr, after the lines of its operands.
  • PTX: compiled with line tables that point into the conceptual stmt, through the debug info of the PR below. One pass reads the .loc directives, for their line and column, and takes them out again.
  • Rewritten Stmts: the device code generators rewrite the kernels before printing them, which rebuilds the Stmts above whatever they change. Such a Stmt is found by the first node within it that the conceptual stmt has (StmtSourceMap::find_within). Bind let names in a scope in CodeGen_C instead of substituting #9710 makes sure CodeGen_C itself no longer rebuilds what it prints.

What is different from #9691

  • No gpu_debug_symbols target feature: no .loc in the PTX of the pipeline, and no #line directives in C-like source. This is for the html only.
  • No second copy of the device code in the Module: what is shown is what is compiled in.
  • No tracking of which Stmt replaces which (remember_replaced), and no set of all the nodes of the conceptual stmt.
  • The lines are always kept track of for GPU targets, since lowering no longer knows whether an html output will be made.
  • The html does not hide the assembly pane when all the stores are on a device.

Output and performance

The C source of four pipelines on CUDA, OpenCL, Metal, D3D12 and WebGPU targets, with the device code embedded in it, is the same as on main apart from the operand order that #9710 describes. That includes the PTX after its line information is taken out.

Lowering time of the generators in apps/, with tools/benchmark_apps_generators.py (-e stmt, wall clock, mean of 3 runs), main against #9712 and this PR. With a GPU target, lowering includes compiling the kernels to device code. Two of the 72 generators fail to build or run on all three, and are left out.

target main #9712 this PR
host-cuda 79639 ms 79574 ms (0.999x) 80010 ms (+371 ms, 1.005x)
host-opencl 7433 ms 7452 ms (1.003x) 7610 ms (+177 ms, 1.024x)

The time of the lowering passes themselves hardly moves (1.007x). The extra time is outside of them, which is where printing the conceptual stmt and keeping track of where the device code came from are. No single generator moves beyond the noise.

lowering time on host-cuda

lowering time on host-opencl

Screenshots

Taken with headless Chrome, of a small pipeline with a kernel that computes one Func at the tiles of another, with rainbow parentheses on.

OpenCL: the first + of the sum of three loads double-clicked in the conceptual stmt. The lines of OpenCL C of that sum have the color of the operator each was generated for, and the rest of the statement is marked faintly. The bar is in front of the line of the + itself:

conceptual stmt html of an OpenCL pipeline, with the OpenCL C of an operator selected

CUDA: the same operator, with the PTX made from it:

conceptual stmt html of a CUDA pipeline, with the PTX of an operator selected

OpenCL in the classic dark color scheme:

the OpenCL pipeline in the classic dark color scheme

Tests

correctness_debug_symbols checks, for CUDA and OpenCL, that the device code has no line information in it, and that the html links lines of device code to lines of the conceptual stmt that have IR on them. For OpenCL it also checks that the call of sin in the source is linked to the Stmt that calls it. It also checks that the device code is linked to several IR nodes. For the kernel of the screenshots, 62 of the 88 lines of OpenCL C are linked to 56 nodes, and 97 of the 212 lines of PTX to 41 nodes; the others are declarations, labels and the like.

The linking for Metal, D3D12 and WebGPU shares the code of OpenCL, but has no test of its own.

🤖 Generated with Claude Code

https://claude.ai/code/session_011FRemhS639vq75urwV44oe

@mcourteaux
mcourteaux added this pull request to stack #9714 November 22, 2026 15:12
@mcourteaux
mcourteaux force-pushed the mcourteaux/gpu-debug-symbols-alt branch from af257b6 to 5729392 Compare November 22, 2026 18:52
The conceptual_stmt_html output links the device code of GPU kernels to
the conceptual stmt it was made from, for CUDA and for the APIs with
C-like source (OpenCL, Metal, D3D12, WebGPU), in the same way as the
assembly: by line of IR, and within a line by IR node.

Lowering prints the conceptual stmt with its source map before the GPU
loops are offloaded, and each device code generator says where in it each
line of its output came from. The Module keeps that per device code
buffer, for the html.

- The C-like code generators note where print_stmt() and print_expr()
  print each Stmt and Expr. The line that assigns the value of an Expr is
  left to that Expr, after the lines of its operands.
- PTX is compiled with line tables that point into the conceptual stmt.
  The .loc directives are read, for their line and column, and taken out
  of the device code again.
- A Stmt that was rewritten since the conceptual stmt was printed is
  found by the first node within it that was printed
  (StmtSourceMap::find_within).

The device code itself is unchanged.

CodeGen_LLVM::init_debug_info() takes the source to point into; compile()
prints one itself if it has to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FRemhS639vq75urwV44oe
@mcourteaux
mcourteaux force-pushed the mcourteaux/gpu-debug-symbols-alt branch from 5729392 to a3cb0ae Compare November 22, 2026 19:37
@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 34 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.84%. Comparing base (be10264) to head (a3cb0ae).

Files with missing lines Patch % Lines
src/CodeGen_WebGPU_Dev.cpp 0.00% 8 Missing ⚠️
src/CodeGen_PTX_Dev.cpp 87.03% 0 Missing and 7 partials ⚠️
src/CodeGen_D3D12Compute_Dev.cpp 28.57% 5 Missing ⚠️
src/CodeGen_LLVM.cpp 86.66% 2 Missing and 2 partials ⚠️
src/CodeGen_C.cpp 87.50% 2 Missing and 1 partial ⚠️
src/CodeGen_OpenCL_Dev.cpp 71.42% 1 Missing and 1 partial ⚠️
src/StmtToHTML.cpp 94.28% 0 Missing and 2 partials ⚠️
src/CodeGen_GPU_Dev.cpp 92.85% 0 Missing and 1 partial ⚠️
src/CodeGen_Metal_Dev.cpp 87.50% 0 Missing and 1 partial ⚠️
src/OffloadGPULoops.cpp 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@                          Coverage Diff                          @@
##           mcourteaux/llvm-debug-symbols-alt    #9713      +/-   ##
=====================================================================
+ Coverage                              73.62%   73.84%   +0.22%     
=====================================================================
  Files                                    266      266              
  Lines                                  83009    83146     +137     
  Branches                               20589    20631      +42     
=====================================================================
+ Hits                                   61113    61397     +284     
+ Misses                                 15399    15292     -107     
+ Partials                                6497     6457      -40     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant