PTX generation examples?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

PTX generation examples?

Larry Gritz-2
I have an app that uses LLVM API calls from C++ to generate IR and JIT it for x86 (for subsequent live execution).  I'm still using the old JIT, for what it's worth.

I want to modify it (for prototype/experimental purposes for now) to JIT PTX (into a big string buffer?).

Docs are sketchy.  I can wade through it and figure it out by trial and error, but would be so very happy if somebody could point me to code or docs addressing all the issues I'll face: how to find out at runtime if the libLLVM I'm linked against can generate PTX, how to change the initialization or JIT commands to request PTX rather than x86, anything I need to know about differences in the IR I should present, etc.

Any pointers would be greatly appreciated, thanks.

--
Larry Gritz
[hidden email]




_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PTX generation examples?

Eli Bendersky



On Fri, Dec 6, 2013 at 11:34 AM, Larry Gritz <[hidden email]> wrote:
I have an app that uses LLVM API calls from C++ to generate IR and JIT it for x86 (for subsequent live execution).  I'm still using the old JIT, for what it's worth.

I want to modify it (for prototype/experimental purposes for now) to JIT PTX (into a big string buffer?).

Docs are sketchy.  I can wade through it and figure it out by trial and error, but would be so very happy if somebody could point me to code or docs addressing all the issues I'll face: how to find out at runtime if the libLLVM I'm linked against can generate PTX, how to change the initialization or JIT commands to request PTX rather than x86, anything I need to know about differences in the IR I should present, etc.

Any pointers would be greatly appreciated, thanks.
___________________

You'll have to switch to MCJIT for this purpose. Legacy JIT doesn't emit PTX.

Eli 

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PTX generation examples?

Larry Gritz-2
OK, fine -- an example of MCJIT that sets up for PTX JIT would also be helpful.


On Dec 6, 2013, at 12:32 PM, Eli Bendersky <[hidden email]> wrote:


You'll have to switch to MCJIT for this purpose. Legacy JIT doesn't emit PTX.

Eli 

--
Larry Gritz
[hidden email]




_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PTX generation examples?

Justin Holewinski-2
There is no MCJIT support for PTX at the moment (mainly because PTX does not have a binary format, and is not machine code per se).

To generate PTX at run-time, you just set up a standard codegen pass manager like you would like an off-line compiler.  The output will be a string buffer that contains the PTX, which you can load into the CUDA runtime.

As for determining if PTX support is compiled into the LLVM binary you are using, you could register all targets and then check if you can create a Target for the "nvptx" or "nvptx64" triple:

  InitializeAllTargets();
  InitializeAllTargetMCs();
  InitializeAllAsmPrinters();
  InitializeAllAsmParsers();

  std::string Err;
  const Target *Tgt = TargetRegistry::lookupTarget("nvptx64", Err);
  if (Tgt) {
    // nvptx target is available
  } else {
    // nvptx target is not available
  }



More information about the PTX target can be found at: http://llvm.org/docs/NVPTXUsage.html



On Fri, Dec 6, 2013 at 3:43 PM, Larry Gritz <[hidden email]> wrote:
OK, fine -- an example of MCJIT that sets up for PTX JIT would also be helpful.


On Dec 6, 2013, at 12:32 PM, Eli Bendersky <[hidden email]> wrote:


You'll have to switch to MCJIT for this purpose. Legacy JIT doesn't emit PTX.

Eli 

--
Larry Gritz
[hidden email]




_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




--

Thanks,

Justin Holewinski

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PTX generation examples?

Larry Gritz-2
Ah, that's helpful.  I knew that I'd need to end up with PTX as text, not a true binary, but I would have figured that it would come out of MCJIT. Thanks for helping to steer me away from the wrong trail.

OK, one more question: Can anybody clarify the pros and cons of generating the PTX through the standard LLVM distro, versus using the "libnvvm" that comes with the Cuda SDK?

-- lg


On Dec 9, 2013, at 7:00 AM, Justin Holewinski <[hidden email]> wrote:

There is no MCJIT support for PTX at the moment (mainly because PTX does not have a binary format, and is not machine code per se).

To generate PTX at run-time, you just set up a standard codegen pass manager like you would like an off-line compiler.  The output will be a string buffer that contains the PTX, which you can load into the CUDA runtime.

As for determining if PTX support is compiled into the LLVM binary you are using, you could register all targets and then check if you can create a Target for the "nvptx" or "nvptx64" triple:

  InitializeAllTargets();
  InitializeAllTargetMCs();
  InitializeAllAsmPrinters();
  InitializeAllAsmParsers();

  std::string Err;
  const Target *Tgt = TargetRegistry::lookupTarget("nvptx64", Err);
  if (Tgt) {
    // nvptx target is available
  } else {
    // nvptx target is not available
  }



More information about the PTX target can be found at: http://llvm.org/docs/NVPTXUsage.html



On Fri, Dec 6, 2013 at 3:43 PM, Larry Gritz <[hidden email]> wrote:
OK, fine -- an example of MCJIT that sets up for PTX JIT would also be helpful.


On Dec 6, 2013, at 12:32 PM, Eli Bendersky <[hidden email]> wrote:


You'll have to switch to MCJIT for this purpose. Legacy JIT doesn't emit PTX.

Eli 

--
Larry Gritz
[hidden email]




_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




--

Thanks,

Justin Holewinski

--
Larry Gritz
[hidden email]




_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PTX generation examples?

Justin Holewinski-2
The NVPTX target in upstream LLVM is basically the same NVPTX target from libNVVM, ported to upstream LLVM with a couple of proprietary features removed.

One thing to consider is that libNVVM is based on LLVM 3.0 and only IR-compatible up to LLVM 3.2.  So if you use the LLVM 3.3, 3.4, or trunk libraries to generate IR, it will not be compatible with libNVVM due to differences in the IR and bitcode formats.  Even dumping the IR to text first will not work because of the new attributes syntax.

With libNVVM, you get the same compiler middle-end and back-end as the shipped nvcc compiler, but you're limited to LLVM 3.0-3.2.  With upstream LLVM, the target handles new IR features and can take advantage of improvements to the LLVM core optimizers.  If you're already invested in LLVM 3.3+, I would recommend sticking with it and using the upstream NVPTX target.  The libdevice math library that ships with the CUDA toolkit is fully compatible with upstream LLVM, not just libNVVM.

FYI: I'm the maintainer of the NVPTX target in LLVM.  Feel free to contact me with any questions you have.


On Mon, Dec 9, 2013 at 12:19 PM, Larry Gritz <[hidden email]> wrote:
Ah, that's helpful.  I knew that I'd need to end up with PTX as text, not a true binary, but I would have figured that it would come out of MCJIT. Thanks for helping to steer me away from the wrong trail.

OK, one more question: Can anybody clarify the pros and cons of generating the PTX through the standard LLVM distro, versus using the "libnvvm" that comes with the Cuda SDK?

-- lg


On Dec 9, 2013, at 7:00 AM, Justin Holewinski <[hidden email]> wrote:

There is no MCJIT support for PTX at the moment (mainly because PTX does not have a binary format, and is not machine code per se).

To generate PTX at run-time, you just set up a standard codegen pass manager like you would like an off-line compiler.  The output will be a string buffer that contains the PTX, which you can load into the CUDA runtime.

As for determining if PTX support is compiled into the LLVM binary you are using, you could register all targets and then check if you can create a Target for the "nvptx" or "nvptx64" triple:

  InitializeAllTargets();
  InitializeAllTargetMCs();
  InitializeAllAsmPrinters();
  InitializeAllAsmParsers();

  std::string Err;
  const Target *Tgt = TargetRegistry::lookupTarget("nvptx64", Err);
  if (Tgt) {
    // nvptx target is available
  } else {
    // nvptx target is not available
  }



More information about the PTX target can be found at: http://llvm.org/docs/NVPTXUsage.html



On Fri, Dec 6, 2013 at 3:43 PM, Larry Gritz <[hidden email]> wrote:
OK, fine -- an example of MCJIT that sets up for PTX JIT would also be helpful.


On Dec 6, 2013, at 12:32 PM, Eli Bendersky <[hidden email]> wrote:


You'll have to switch to MCJIT for this purpose. Legacy JIT doesn't emit PTX.

Eli 

--
Larry Gritz
[hidden email]




_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




--

Thanks,

Justin Holewinski

--
Larry Gritz
[hidden email]






--

Thanks,

Justin Holewinski

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev