5

CUDA PTX is analogous to assembly, and as such reveals the source code. I have read Section 3.1 of the CUDA Programming Guide and Section 3.2.7 from the online CUDA compiler documentation. I have a basic understanding of the -arch versus -code compiler options.

If I understand correctly, specifying -arch compute_XX makes PTX. Whereas -code sm_XX makes both PTX and cubin.

I desire only cubin, such that no PTX is in the resulting image. How can I achieve this?

Preferably via Visual Studio settings, although I only find the -gencode option within Visual Studio Project Settings.

1 Answer 1

11

PTX is not quite analogous to assembly. PTX is an intermediate representation of the program that can be compiled to the different, incompatible instruction set architectures (ISA) that Nvidia GPUs have been using over time. Usually, a new ISA for Nvidia GPUs comes with an updated version of PTX that can represent the new features of the ISA.

  • The -arch and -code options to nvcc work slightly differently to what you describe. They are not (mutual exclusive) alternatives, rather they determine different aspects.
    -arch controls which PTX version is used as the intermediate representation. As such it is combined with a compute_XX PTX version.
    -code controls what code is embedded into the resulting binary - either machine code for the specified ISA if used in the -code sm_XX form, or PTX to be just-in-time compiled by the GPU driver if -code compute_XX is specified.
    As a special shortcut, specifying only -arch sm_XX will embed both the compiled code for the specified ISA and PTX code into the binary - this probably is the situation that you are referring to that you want to avoid.
    Finally the -gencode option allows you to specify multiple -arch/-code pairs, with the resulting binary containing separate code for each of the pairs.
  • You can use nvprune to remove all but the desired ISA code from a binary.
  • If unsure, you can always use cuobjdump to check what is in a specific binary.

So the way to prevent any PTX code from being present in your resulting binary is to call nvcc as nvcc -arch compute_XX -code sm_XX (or use multiple such pairs together with -gencode).

2
  • Thank you for the enlightening answer. It helps to understand that PTX is a required intermediate. Then the statement in my question is wrong; -code sm_XX does not make both PTX+cubin, it makes only cubin. Commented Jan 26, 2017 at 14:42
  • 1
    @TysonHilmer Just to point out the obvious: Code can be reverse engineered from a binary representation as well via a process of disassembly (that is, converting binary code into the equivalent assembly language instructions) and annotation. In the x86 and ARM worlds there are very powerful tools available to assist with that task, so this is a platform-independent phenomenon. I am not aware of similar tools for GPU code, but with a bit of practice, one can certainly decipher SASS code. So all you are doing by removing PTX is discouraging casual code readers, not determined attackers.
    – njuffa
    Commented Jan 26, 2017 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.