<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>Hi, Alexey,</p>
    <p>Thanks a bunch for working on this. A couple quick questions
      below...<br>
    </p>
    <div class="moz-cite-prefix">On 11/06/2017 12:37 PM, Alexey Bataev
      wrote:<br>
    </div>
    <blockquote
cite="mid:AM5PR0902MB1923ADF3DAB67DACD533EFD485500@AM5PR0902MB1923.eurprd09.prod.outlook.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      <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 moz-do-not-send="true"
          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>
      </p>
    </blockquote>
    <br>
    Is this a change? It looks like this is already the behavior of the
    driver's CUDA target code:<br>
    <p><tt>  if (Args.hasFlag(options::OPT_cuda_noopt_device_debug,<br>
                           options::OPT_no_cuda_noopt_device_debug,
        false)) {<br>
            // ptxas does not accept -g option if optimization is
        enabled, so<br>
            // we ignore the compiler's -O* options if we want debug
        info.<br>
            CmdArgs.push_back("-g");<br>
            CmdArgs.push_back("--dont-merge-basicblocks");<br>
            CmdArgs.push_back("--return-at-end");<br>
          } else if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {<br>
            // Map the -O we received to -O{0,1,2,3}.</tt><br>
    </p>
    <blockquote
cite="mid:AM5PR0902MB1923ADF3DAB67DACD533EFD485500@AM5PR0902MB1923.eurprd09.prod.outlook.com"
      type="cite">
      <p>
            •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>
    </blockquote>
    <br>
    Can you clarify what "unable to translate" mean? Does it refuse to
    compile the code, drop all debug info, drop all debug info except
    for line-table information, something else?<br>
    <br>
    Thanks again,<br>
    Hal<br>
    <br>
    <blockquote
cite="mid:AM5PR0902MB1923ADF3DAB67DACD533EFD485500@AM5PR0902MB1923.eurprd09.prod.outlook.com"
      type="cite">
      <p>
      </p>
      <p><br>
      </p>
      <p>This approach was implemented in <a moz-do-not-send="true"
          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>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
  </body>
</html>