1

I have a CUDA C/C++ programm for CUDA 7.5. And as known: libNVVM Library - an optimizing compiler library that generates PTX from NVVM IR.

I can get PTX by using: nvcc -ptx <file>.cu -o <file>.ptx

But how can I get NVVM IR (LLVM IR) from <file>.cu?

And how can I compile NVVM IR (LLVM IR) or Optimized IR for the target architecture?

Do I need for this third-party libraries or programs such as: libcuda.lang, ...?

enter image description here

enter image description here

The NVVM compiler (which is based on LLVM) generates PTX code from NVVM IR.

NVVM IR and NVVM compilers are mostly agnostic about the source language being used. The PTX codegen part of a NVVM compiler needs to know the source language because of the difference in DCI (driver/compiler interface).

Technically speaking, NVVM IR is LLVM IR with a set of rules, restrictions, and conventions, plus a set of supported intrinsic functions. A program specified in NVVM IR is always a legal LLVM program. A legal LLVM program may not be a legal NVVM program.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Alex
  • 12,578
  • 15
  • 99
  • 195
  • 1
    https://github.com/apc-llc/nvcc-llvm-ir – talonmies Dec 09 '15 at 14:49
  • Hi Alex, could you get LLVM IR with clang++/llvm for CUDA code? I too was wondering about this: `clang++ -S -emit-llvm -o axpy -I/samples/common/inc -L/ axpy.cu -lcudart_static -lcuda -ldl -lrt -pthread`! – algoProg Oct 13 '16 at 19:59

1 Answers1

3

The very short answer is that you cannot do this. NVIDIA's parser is proprietary and closed source, and they have not exposed the IR code generator in a way which can be used as you are asking about.

That said, you are not the first person to wonder about this, and you might be able to find some useful, but completely unofficial and unsupported information here.

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • Thank you! I.e. *nvcc* is the compiler driver and is proprietary and closed source. But *nvopencc* is .cu-parser and based on open64 and is released under GPL, isn't it? https://habrastorage.org/files/553/c7e/300/553c7e3007ff4fba8ba815a10799a144.jpg PDF: http://www.nvidia.com/object/io_1213955090354.html – Alex Jan 02 '16 at 11:10
  • 1
    @Alex: nvopencc isn't the LVMM based parser/compiler, it is first generation, deprecated open64 based compiler that CUDA originally shipped with. That also emits PTX directly. The current LVVM based parser is proprietary and closed source and buried in libNVMM, with no external APIs or documentation which would allow access to the emitted LVMM IR code. – talonmies Jan 02 '16 at 11:15
  • 1
    Thank you for the clarification. But if we can compile .cu-files without nvcc by using clang+llvm: `clang++ -o axpy -I/samples/common/inc -L/ axpy.cu -lcudart_static -lcuda -ldl -lrt -pthread` then can we get llvm-ir by using "clang++ **-S -emit-llvm** -o axpy -I/samples/common/inc -L/ axpy.cu -lcudart_static -lcuda -ldl -lrt -pthread"? http://llvm.org/docs/CompileCudaWithLLVM.html – Alex Jan 02 '16 at 19:13
  • Have you read the link I gave you? Yes clang++ can compile simple CUDA code, but the open source clang parser can't do anything like the optimisation that the NVIDIA parser can perform. So you can get some form of IR, but how good it is or how well it will compile into PTX is something I have no experience with. – talonmies Jan 02 '16 at 19:26