<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>Hi everybody,</p>
<p>As you know, Cuda/NVPTX target has very limited support of the debug info in Clang/LLVM. Currently, LLVM supports only emission of the line numbers debug info.</p>
<p>This is caused by limitations of the Cuda/NVPTX codegen. Clang/LLVM translates the source code to LLVM IR, which is then lowered to PTX (parallel thread execution) intermediate file. This PTX file represents special kind of the assembler code in text format,
 which contains the code itself + (possibly) debug info. Then this PTX file is compiled by ptxas tool into the CUDA binary representation.</p>
<p><br>
</p>
<p>Debug info representation in PTX file.<br>
========================</p>
<p>According to PTX Writer's Guide to Interoperability, Debug information (<a class="moz-txt-link-freetext" href="http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#debug-information">http://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability/index.html#debug-information</a>)
 , debug information must be encoded in DWARF (Debug With Arbitrary Record Format). The responsibility for generating debug information is split between the PTX producer and the PTX-to-SASS backend. The PTX producer is responsible for emitting binary DWARF
 into the PTX file, using the .section and .b8-.b16-.b32-and-.b64 directives in PTX. This should contain the .debug_info and .debug_abbrev sections, and possibly optional sections .debug_pubnames and .debug_aranges. These sections are standard DWARF2 sections
 that refer to labels and registers in the PTX.<br>
<br>
The PTX-to-SASS backend is responsible for generating the .debug_line section from the .file and .loc directives in the PTX file. This section maps source lines to SASS addresses. The PTX-to-SASS backend also generates the .debug_frame section.</p>
<p>LLVM is able to emit debug info in DWARF. But ptxas compiler has some limitations, that make it hard to adapt LLVM for correct emission of the debug info in PTX files.</p>
<p><br>
</p>
<p>Limitations/features of the PTX format/ptxas compiler.<br>
==================================</p>
<p>a) Supports DWARF-2 only.<br>
b) Labels are allowed only in code section (only in functions).<br>
c) Does not support label arithmetic in DWARF sections.<br>
    “.b32 L1 – L2” as the size of the section is not allowed, so the sections sizes should be calculated explicitly.<br>
d) Debug info must point to the sections, not to labels inside these sections.<br>
    “.b32 .debug_abbrevs”<br>
e) Sections itself must be enclosed into braces<br>
    “.section .debug_info {…}”<br>
f) Frame info is non-register based<br>
    Based on function local “__local_depot” array, that represents the stack frame.<br>
g) All variables must have non-standard DW_AT_address_class attribute so the debuger had the info about address class of the variable - global or local. DWARF standard does support this attribute, but it can be appiled to pointer/reference types only, not variables.<br>
h) The first label in the function must follow the debug location macro. In LLVM, it is followed by the debug location macro.<br>
i) .debug_frame section is emitted by txas compiler.<br>
    DW_AT_frame_base must be set to dwarf::DW_FORM_data1 dwarf::DW_OP_call_frame_cfa value.<br>
j) Strings cannot be referenced by the labels, instead they must be inlined in the sections in form of array of chars.<br>
</p>
<p>Some changes in LLVM are required to support all these limitation/features in the output PTX files.</p>
<p>Required changes in LLVM.<br>
==================</p>
<p>•include/llvm/CodeGen/AsmPrinter.h.<br>
    •Add “virtual MCSymbol *getFunctionFrameSymbol(const MachineFunction *MF) const” for non-register-based frame info.<br>
    •Override “NVPTXMCAsmPrinter.cpp” to return the name of the “__local_depot” frame storage.<br>
•Add ”cuda-gdb” specific tuning.<br>
    •Inlined strings must be used in sections, not string references.<br>
    •Label arithmetic is replaced by the absolute section size evaluation.<br>
    •Use “AsmPrinter::doInitialization()” instead of NVPTX-custom manual initialization.<br>
    •Local variables address emitted as “__local_depot” + <var offset>.<br>
•Add NVPTX specific “NVPTXMCAsmStreamer” class.<br>
    •Requires moving to includes of “MCAsmStreamer” class declaration.<br>
    •Overrides emission of the labels (names of the section are emitted instead).<br>
    •Overrides emission of the sections (emit braces)<br>
    •Overrides string emission (as sequence of bytes, not as strings)<br>
    •Overrides emission of files/locations debug info<br>
</p>
<p>Required changes in Clang.<br>
=================</p>
<p>•Add option “-gcuda-gdb” to driver.<br>
    •Emit cuda-gdb compatible debug info (DWARF-2 by default + CudaGDB tuning).<br>
•Add options “-g --dont-merge-basicblocks --return-at-end” to “ptxas” call.<br>
    •ptxas is able to translate debug information only if -O0 optimization level is used. It means, that we can use optimization level in LLVM > O0, but still have to use O0 when calling ptxas compiler.<br>
</p>
<p><br>
</p>
<p>This approach was implemented in <a class="moz-txt-link-freetext" href="https://github.com/clang-ykt">
https://github.com/clang-ykt</a> to support debug info emission for NVPTX target when generating code for OpenMP offloading constructs. You can try to use it.<br>
</p>
</body>
</html>