1 =============================
2 User Guide for NVPTX Back-end
3 =============================
13 To support GPU programming, the NVPTX back-end supports a subset of LLVM IR
14 along with a defined set of conventions used to represent GPU programming
15 concepts. This document provides an overview of the general usage of the back-
16 end, including a description of the conventions used and the set of accepted
21 This document assumes a basic familiarity with CUDA and the PTX
22 assembly language. Information about the CUDA Driver API and the PTX assembly
23 language can be found in the `CUDA documentation
24 <http://docs.nvidia.com/cuda/index.html>`_.
31 Marking Functions as Kernels
32 ----------------------------
34 In PTX, there are two types of functions: *device functions*, which are only
35 callable by device code, and *kernel functions*, which are callable by host
36 code. By default, the back-end will emit device functions. Metadata is used to
37 declare a function as a kernel function. This metadata is attached to the
38 ``nvvm.annotations`` named metadata object, and has the following format:
42 !0 = !{<function-ref>, metadata !"kernel", i32 1}
44 The first parameter is a reference to the kernel function. The following
45 example shows a kernel function calling a device function in LLVM IR. The
46 function ``@my_kernel`` is callable from host code, but ``@my_fmad`` is not.
50 define float @my_fmad(float %x, float %y, float %z) {
51 %mul = fmul float %x, %y
52 %add = fadd float %mul, %z
56 define void @my_kernel(float* %ptr) {
57 %val = load float, float* %ptr
58 %ret = call float @my_fmad(float %val, float %val, float %val)
59 store float %ret, float* %ptr
63 !nvvm.annotations = !{!1}
64 !1 = !{void (float*)* @my_kernel, !"kernel", i32 1}
66 When compiled, the PTX kernel functions are callable by host-side code.
74 The NVPTX back-end uses the following address space mapping:
76 ============= ======================
77 Address Space Memory Space
78 ============= ======================
85 ============= ======================
87 Every global variable and pointer type is assigned to one of these address
88 spaces, with 0 being the default address space. Intrinsics are provided which
89 can be used to convert pointers between the generic and non-generic address
92 As an example, the following IR will define an array ``@g`` that resides in
97 @g = internal addrspace(1) global [4 x i32] [ i32 0, i32 1, i32 2, i32 3 ]
99 LLVM IR functions can read and write to this array, and host-side code can
100 copy data to it by name with the CUDA Driver API.
102 Note that since address space 0 is the generic space, it is illegal to have
103 global variables in address space 0. Address space 0 is the default address
104 space in LLVM, so the ``addrspace(N)`` annotation is *required* for global
111 The NVPTX target uses the module triple to select between 32/64-bit code
112 generation and the driver-compiler interface to use. The triple architecture
113 can be one of ``nvptx`` (32-bit PTX) or ``nvptx64`` (64-bit PTX). The
114 operating system should be one of ``cuda`` or ``nvcl``, which determines the
115 interface used by the generated code to communicate with the driver. Most
116 users will want to use ``cuda`` as the operating system, which makes the
117 generated PTX compatible with the CUDA Driver API.
119 Example: 32-bit PTX for CUDA Driver API: ``nvptx-nvidia-cuda``
121 Example: 64-bit PTX for CUDA Driver API: ``nvptx64-nvidia-cuda``
125 .. _nvptx_intrinsics:
130 Address Space Conversion
131 ------------------------
133 '``llvm.nvvm.ptr.*.to.gen``' Intrinsics
134 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139 These are overloaded intrinsics. You can use these on any pointer types.
143 declare i8* @llvm.nvvm.ptr.global.to.gen.p0i8.p1i8(i8 addrspace(1)*)
144 declare i8* @llvm.nvvm.ptr.shared.to.gen.p0i8.p3i8(i8 addrspace(3)*)
145 declare i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8(i8 addrspace(4)*)
146 declare i8* @llvm.nvvm.ptr.local.to.gen.p0i8.p5i8(i8 addrspace(5)*)
151 The '``llvm.nvvm.ptr.*.to.gen``' intrinsics convert a pointer in a non-generic
152 address space to a generic address space pointer.
157 These intrinsics modify the pointer value to be a valid generic address space
161 '``llvm.nvvm.ptr.gen.to.*``' Intrinsics
162 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
167 These are overloaded intrinsics. You can use these on any pointer types.
171 declare i8 addrspace(1)* @llvm.nvvm.ptr.gen.to.global.p1i8.p0i8(i8*)
172 declare i8 addrspace(3)* @llvm.nvvm.ptr.gen.to.shared.p3i8.p0i8(i8*)
173 declare i8 addrspace(4)* @llvm.nvvm.ptr.gen.to.constant.p4i8.p0i8(i8*)
174 declare i8 addrspace(5)* @llvm.nvvm.ptr.gen.to.local.p5i8.p0i8(i8*)
179 The '``llvm.nvvm.ptr.gen.to.*``' intrinsics convert a pointer in the generic
180 address space to a pointer in the target address space. Note that these
181 intrinsics are only useful if the address space of the target address space of
182 the pointer is known. It is not legal to use address space conversion
183 intrinsics to convert a pointer from one non-generic address space to another
184 non-generic address space.
189 These intrinsics modify the pointer value to be a valid pointer in the target
190 non-generic address space.
193 Reading PTX Special Registers
194 -----------------------------
196 '``llvm.nvvm.read.ptx.sreg.*``'
197 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
204 declare i32 @llvm.nvvm.read.ptx.sreg.tid.x()
205 declare i32 @llvm.nvvm.read.ptx.sreg.tid.y()
206 declare i32 @llvm.nvvm.read.ptx.sreg.tid.z()
207 declare i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
208 declare i32 @llvm.nvvm.read.ptx.sreg.ntid.y()
209 declare i32 @llvm.nvvm.read.ptx.sreg.ntid.z()
210 declare i32 @llvm.nvvm.read.ptx.sreg.ctaid.x()
211 declare i32 @llvm.nvvm.read.ptx.sreg.ctaid.y()
212 declare i32 @llvm.nvvm.read.ptx.sreg.ctaid.z()
213 declare i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
214 declare i32 @llvm.nvvm.read.ptx.sreg.nctaid.y()
215 declare i32 @llvm.nvvm.read.ptx.sreg.nctaid.z()
216 declare i32 @llvm.nvvm.read.ptx.sreg.warpsize()
221 The '``@llvm.nvvm.read.ptx.sreg.*``' intrinsics provide access to the PTX
222 special registers, in particular the kernel launch bounds. These registers
223 map in the following way to CUDA builtins:
225 ============ =====================================
226 CUDA Builtin PTX Special Register Intrinsic
227 ============ =====================================
228 ``threadId`` ``@llvm.nvvm.read.ptx.sreg.tid.*``
229 ``blockIdx`` ``@llvm.nvvm.read.ptx.sreg.ctaid.*``
230 ``blockDim`` ``@llvm.nvvm.read.ptx.sreg.ntid.*``
231 ``gridDim`` ``@llvm.nvvm.read.ptx.sreg.nctaid.*``
232 ============ =====================================
238 '``llvm.nvvm.barrier0``'
239 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
246 declare void @llvm.nvvm.barrier0()
251 The '``@llvm.nvvm.barrier0()``' intrinsic emits a PTX ``bar.sync 0``
252 instruction, equivalent to the ``__syncthreads()`` call in CUDA.
258 For the full set of NVPTX intrinsics, please see the
259 ``include/llvm/IR/IntrinsicsNVVM.td`` file in the LLVM source tree.
264 Linking with Libdevice
265 ======================
267 The CUDA Toolkit comes with an LLVM bitcode library called ``libdevice`` that
268 implements many common mathematical functions. This library can be used as a
269 high-performance math library for any compilers using the LLVM NVPTX target.
270 The library can be found under ``nvvm/libdevice/`` in the CUDA Toolkit and
271 there is a separate version for each compute architecture.
273 For a list of all math functions implemented in libdevice, see
274 `libdevice Users Guide <http://docs.nvidia.com/cuda/libdevice-users-guide/index.html>`_.
276 To accommodate various math-related compiler flags that can affect code
277 generation of libdevice code, the library code depends on a special LLVM IR
278 pass (``NVVMReflect``) to handle conditional compilation within LLVM IR. This
279 pass looks for calls to the ``@__nvvm_reflect`` function and replaces them
280 with constants based on the defined reflection parameters. Such conditional
281 code often follows a pattern:
285 float my_function(float a) {
286 if (__nvvm_reflect("FASTMATH"))
287 return my_function_fast(a);
289 return my_function_precise(a);
292 The default value for all unspecified reflection parameters is zero.
294 The ``NVVMReflect`` pass should be executed early in the optimization
295 pipeline, immediately after the link stage. The ``internalize`` pass is also
296 recommended to remove unused math functions from the resulting PTX. For an
297 input IR module ``module.bc``, the following compilation flow is recommended:
299 1. Save list of external functions in ``module.bc``
300 2. Link ``module.bc`` with ``libdevice.compute_XX.YY.bc``
301 3. Internalize all functions not in list from (1)
302 4. Eliminate all unused internal functions
303 5. Run ``NVVMReflect`` pass
304 6. Run standard optimization pipeline
308 ``linkonce`` and ``linkonce_odr`` linkage types are not suitable for the
309 libdevice functions. It is possible to link two IR modules that have been
310 linked against libdevice using different reflection variables.
312 Since the ``NVVMReflect`` pass replaces conditionals with constants, it will
313 often leave behind dead code of the form:
319 br i1 true, label %foo, label %bar
326 Therefore, it is recommended that ``NVVMReflect`` is executed early in the
327 optimization pipeline before dead-code elimination.
329 The NVPTX TargetMachine knows how to schedule ``NVVMReflect`` at the beginning
330 of your pass manager; just use the following code when setting up your pass
331 manager and the PassBuilder will use ``registerPassBuilderCallbacks`` to let
332 NVPTXTargetMachine::registerPassBuilderCallbacks add the the pass to the
337 std::unique_ptr<TargetMachine> TM = ...;
339 ModulePassManager MPM;
340 PB.parsePassPipeline(MPM, ...);
342 Reflection Parameters
343 ---------------------
345 The libdevice library currently uses the following reflection parameters to
346 control code generation:
348 ==================== ======================================================
350 ==================== ======================================================
351 ``__CUDA_FTZ=[0,1]`` Use optimized code paths that flush subnormals to zero
352 ==================== ======================================================
354 The value of this flag is determined by the "nvvm-reflect-ftz" module flag.
355 The following sets the ftz flag to 1.
359 !llvm.module.flag = !{!0}
360 !0 = !{i32 4, !"nvvm-reflect-ftz", i32 1}
362 (``i32 4`` indicates that the value set here overrides the value in another
363 module we link with. See the `LangRef <LangRef.html#module-flags-metadata>`
369 The most common way to execute PTX assembly on a GPU device is to use the CUDA
370 Driver API. This API is a low-level interface to the GPU driver and allows for
371 JIT compilation of PTX code to native GPU machine code.
373 Initializing the Driver API:
380 // Initialize the driver API
382 // Get a handle to the first compute device
383 cuDeviceGet(&device, 0);
384 // Create a compute device context
385 cuCtxCreate(&context, 0, device);
387 JIT compiling a PTX string to a device binary:
394 // JIT compile a null-terminated PTX string
395 cuModuleLoadData(&module, (void*)PTXString);
397 // Get a handle to the "myfunction" kernel function
398 cuModuleGetFunction(&function, module, "myfunction");
400 For full examples of executing PTX assembly, please see the `CUDA Samples
401 <https://developer.nvidia.com/cuda-downloads>`_ distribution.
407 ptxas complains of undefined function: __nvvm_reflect
408 -----------------------------------------------------
410 When linking with libdevice, the ``NVVMReflect`` pass must be used. See
411 :ref:`libdevice` for more information.
414 Tutorial: A Simple Compute Kernel
415 =================================
417 To start, let us take a look at a simple compute kernel written directly in
418 LLVM IR. The kernel implements vector addition, where each thread computes one
419 element of the output vector C from the input vectors A and B. To make this
420 easier, we also assume that only a single CTA (thread block) will be launched,
421 and that it will be one dimensional.
429 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
430 target triple = "nvptx64-nvidia-cuda"
432 ; Intrinsic to read X component of thread ID
433 declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() readnone nounwind
435 define void @kernel(float addrspace(1)* %A,
436 float addrspace(1)* %B,
437 float addrspace(1)* %C) {
440 %id = tail call i32 @llvm.nvvm.read.ptx.sreg.tid.x() readnone nounwind
442 ; Compute pointers into A, B, and C
443 %ptrA = getelementptr float, float addrspace(1)* %A, i32 %id
444 %ptrB = getelementptr float, float addrspace(1)* %B, i32 %id
445 %ptrC = getelementptr float, float addrspace(1)* %C, i32 %id
448 %valA = load float, float addrspace(1)* %ptrA, align 4
449 %valB = load float, float addrspace(1)* %ptrB, align 4
452 %valC = fadd float %valA, %valB
455 store float %valC, float addrspace(1)* %ptrC, align 4
460 !nvvm.annotations = !{!0}
461 !0 = !{void (float addrspace(1)*,
463 float addrspace(1)*)* @kernel, !"kernel", i32 1}
466 We can use the LLVM ``llc`` tool to directly run the NVPTX code generator:
470 # llc -mcpu=sm_20 kernel.ll -o kernel.ptx
475 If you want to generate 32-bit code, change ``p:64:64:64`` to ``p:32:32:32``
476 in the module data layout string and use ``nvptx-nvidia-cuda`` as the
480 The output we get from ``llc`` (as of LLVM 3.4):
485 // Generated by LLVM NVPTX Back-End
494 .visible .entry kernel(
495 .param .u64 kernel_param_0,
496 .param .u64 kernel_param_1,
497 .param .u64 kernel_param_2
505 ld.param.u64 %rl1, [kernel_param_0];
507 mul.wide.s32 %rl2, %r1, 4;
508 add.s64 %rl3, %rl1, %rl2;
509 ld.param.u64 %rl4, [kernel_param_1];
510 add.s64 %rl5, %rl4, %rl2;
511 ld.param.u64 %rl6, [kernel_param_2];
512 add.s64 %rl7, %rl6, %rl2;
513 ld.global.f32 %f1, [%rl3];
514 ld.global.f32 %f2, [%rl5];
515 add.f32 %f3, %f1, %f2;
516 st.global.f32 [%rl7], %f3;
521 Dissecting the Kernel
522 ---------------------
524 Now let us dissect the LLVM IR that makes up this kernel.
529 The data layout string determines the size in bits of common data types, their
530 ABI alignment, and their storage size. For NVPTX, you should use one of the
537 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
543 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
549 In this example, we use the ``@llvm.nvvm.read.ptx.sreg.tid.x`` intrinsic to
550 read the X component of the current thread's ID, which corresponds to a read
551 of register ``%tid.x`` in PTX. The NVPTX back-end supports a large set of
552 intrinsics. A short list is shown below; please see
553 ``include/llvm/IR/IntrinsicsNVVM.td`` for the full list.
556 ================================================ ====================
557 Intrinsic CUDA Equivalent
558 ================================================ ====================
559 ``i32 @llvm.nvvm.read.ptx.sreg.tid.{x,y,z}`` threadIdx.{x,y,z}
560 ``i32 @llvm.nvvm.read.ptx.sreg.ctaid.{x,y,z}`` blockIdx.{x,y,z}
561 ``i32 @llvm.nvvm.read.ptx.sreg.ntid.{x,y,z}`` blockDim.{x,y,z}
562 ``i32 @llvm.nvvm.read.ptx.sreg.nctaid.{x,y,z}`` gridDim.{x,y,z}
563 ``void @llvm.nvvm.barrier0()`` __syncthreads()
564 ================================================ ====================
570 You may have noticed that all of the pointer types in the LLVM IR example had
571 an explicit address space specifier. What is address space 1? NVIDIA GPU
572 devices (generally) have four types of memory:
574 - Global: Large, off-chip memory
575 - Shared: Small, on-chip memory shared among all threads in a CTA
576 - Local: Per-thread, private memory
577 - Constant: Read-only memory shared across all threads
579 These different types of memory are represented in LLVM IR as address spaces.
580 There is also a fifth address space used by the NVPTX code generator that
581 corresponds to the "generic" address space. This address space can represent
582 addresses in any other address space (with a few exceptions). This allows
583 users to write IR functions that can load/store memory using the same
584 instructions. Intrinsics are provided to convert pointers between the generic
585 and non-generic address spaces.
587 See :ref:`address_spaces` and :ref:`nvptx_intrinsics` for more information.
593 In PTX, a function can be either a `kernel` function (callable from the host
594 program), or a `device` function (callable only from GPU code). You can think
595 of `kernel` functions as entry-points in the GPU program. To mark an LLVM IR
596 function as a `kernel` function, we make use of special LLVM metadata. The
597 NVPTX back-end will look for a named metadata node called
598 ``nvvm.annotations``. This named metadata must contain a list of metadata that
599 describe the IR. For our purposes, we need to declare a metadata node that
600 assigns the "kernel" attribute to the LLVM IR function that should be emitted
601 as a PTX `kernel` function. These metadata nodes take the form:
605 !{<function ref>, metadata !"kernel", i32 1}
607 For the previous example, we have:
611 !nvvm.annotations = !{!0}
612 !0 = !{void (float addrspace(1)*,
614 float addrspace(1)*)* @kernel, !"kernel", i32 1}
616 Here, we have a single metadata declaration in ``nvvm.annotations``. This
617 metadata annotates our ``@kernel`` function with the ``kernel`` attribute.
623 Generating PTX from LLVM IR is all well and good, but how do we execute it on
624 a real GPU device? The CUDA Driver API provides a convenient mechanism for
625 loading and JIT compiling PTX to a native GPU device, and launching a kernel.
626 The API is similar to OpenCL. A simple example showing how to load and
627 execute our vector addition code is shown below. Note that for brevity this
628 code does not perform much error checking!
632 You can also use the ``ptxas`` tool provided by the CUDA Toolkit to offline
633 compile PTX to machine code (SASS) for a specific GPU architecture. Such
634 binaries can be loaded by the CUDA Driver API in the same way as PTX. This
635 can be useful for reducing startup time by precompiling the PTX kernels.
646 void checkCudaErrors(CUresult err) {
647 assert(err == CUDA_SUCCESS);
650 /// main - Program entry point
651 int main(int argc, char **argv) {
659 // CUDA initialization
660 checkCudaErrors(cuInit(0));
661 checkCudaErrors(cuDeviceGetCount(&devCount));
662 checkCudaErrors(cuDeviceGet(&device, 0));
665 checkCudaErrors(cuDeviceGetName(name, 128, device));
666 std::cout << "Using CUDA Device [0]: " << name << "\n";
668 int devMajor, devMinor;
669 checkCudaErrors(cuDeviceComputeCapability(&devMajor, &devMinor, device));
670 std::cout << "Device Compute Capability: "
671 << devMajor << "." << devMinor << "\n";
673 std::cerr << "ERROR: Device 0 is not SM 2.0 or greater\n";
677 std::ifstream t("kernel.ptx");
679 std::cerr << "kernel.ptx not found\n";
682 std::string str((std::istreambuf_iterator<char>(t)),
683 std::istreambuf_iterator<char>());
685 // Create driver context
686 checkCudaErrors(cuCtxCreate(&context, 0, device));
688 // Create module for object
689 checkCudaErrors(cuModuleLoadDataEx(&cudaModule, str.c_str(), 0, 0, 0));
691 // Get kernel function
692 checkCudaErrors(cuModuleGetFunction(&function, cudaModule, "kernel"));
695 CUdeviceptr devBufferA;
696 CUdeviceptr devBufferB;
697 CUdeviceptr devBufferC;
699 checkCudaErrors(cuMemAlloc(&devBufferA, sizeof(float)*16));
700 checkCudaErrors(cuMemAlloc(&devBufferB, sizeof(float)*16));
701 checkCudaErrors(cuMemAlloc(&devBufferC, sizeof(float)*16));
703 float* hostA = new float[16];
704 float* hostB = new float[16];
705 float* hostC = new float[16];
708 for (unsigned i = 0; i != 16; ++i) {
710 hostB[i] = (float)(2*i);
714 checkCudaErrors(cuMemcpyHtoD(devBufferA, &hostA[0], sizeof(float)*16));
715 checkCudaErrors(cuMemcpyHtoD(devBufferB, &hostB[0], sizeof(float)*16));
718 unsigned blockSizeX = 16;
719 unsigned blockSizeY = 1;
720 unsigned blockSizeZ = 1;
721 unsigned gridSizeX = 1;
722 unsigned gridSizeY = 1;
723 unsigned gridSizeZ = 1;
726 void *KernelParams[] = { &devBufferA, &devBufferB, &devBufferC };
728 std::cout << "Launching kernel\n";
731 checkCudaErrors(cuLaunchKernel(function, gridSizeX, gridSizeY, gridSizeZ,
732 blockSizeX, blockSizeY, blockSizeZ,
733 0, NULL, KernelParams, NULL));
735 // Retrieve device data
736 checkCudaErrors(cuMemcpyDtoH(&hostC[0], devBufferC, sizeof(float)*16));
739 std::cout << "Results:\n";
740 for (unsigned i = 0; i != 16; ++i) {
741 std::cout << hostA[i] << " + " << hostB[i] << " = " << hostC[i] << "\n";
745 // Clean up after ourselves
751 checkCudaErrors(cuMemFree(devBufferA));
752 checkCudaErrors(cuMemFree(devBufferB));
753 checkCudaErrors(cuMemFree(devBufferC));
754 checkCudaErrors(cuModuleUnload(cudaModule));
755 checkCudaErrors(cuCtxDestroy(context));
761 You will need to link with the CUDA driver and specify the path to cuda.h.
765 # clang++ sample.cpp -o sample -O2 -g -I/usr/local/cuda-5.5/include -lcuda
767 We don't need to specify a path to ``libcuda.so`` since this is installed in a
768 system location by the driver, not the CUDA toolkit.
770 If everything goes as planned, you should see the following output when
771 running the compiled program:
775 Using CUDA Device [0]: GeForce GTX 680
776 Device Compute Capability: 3.0
798 You will likely see a different device identifier based on your hardware
801 Tutorial: Linking with Libdevice
802 ================================
804 In this tutorial, we show a simple example of linking LLVM IR with the
805 libdevice library. We will use the same kernel as the previous tutorial,
806 except that we will compute ``C = pow(A, B)`` instead of ``C = A + B``.
807 Libdevice provides an ``__nv_powf`` function that we will use.
811 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
812 target triple = "nvptx64-nvidia-cuda"
814 ; Intrinsic to read X component of thread ID
815 declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() readnone nounwind
817 declare float @__nv_powf(float, float)
819 define void @kernel(float addrspace(1)* %A,
820 float addrspace(1)* %B,
821 float addrspace(1)* %C) {
824 %id = tail call i32 @llvm.nvvm.read.ptx.sreg.tid.x() readnone nounwind
826 ; Compute pointers into A, B, and C
827 %ptrA = getelementptr float, float addrspace(1)* %A, i32 %id
828 %ptrB = getelementptr float, float addrspace(1)* %B, i32 %id
829 %ptrC = getelementptr float, float addrspace(1)* %C, i32 %id
832 %valA = load float, float addrspace(1)* %ptrA, align 4
833 %valB = load float, float addrspace(1)* %ptrB, align 4
835 ; Compute C = pow(A, B)
836 %valC = call float @__nv_powf(float %valA, float %valB)
839 store float %valC, float addrspace(1)* %ptrC, align 4
844 !nvvm.annotations = !{!0}
845 !0 = !{void (float addrspace(1)*,
847 float addrspace(1)*)* @kernel, !"kernel", i32 1}
850 To compile this kernel, we perform the following steps:
852 1. Link with libdevice
853 2. Internalize all but the public kernel function
854 3. Run ``NVVMReflect`` and set ``__CUDA_FTZ`` to 0
855 4. Optimize the linked module
856 5. Codegen the module
859 These steps can be performed by the LLVM ``llvm-link``, ``opt``, and ``llc``
860 tools. In a complete compiler, these steps can also be performed entirely
861 programmatically by setting up an appropriate pass configuration (see
866 # llvm-link t2.bc libdevice.compute_20.10.bc -o t2.linked.bc
867 # opt -internalize -internalize-public-api-list=kernel -nvvm-reflect-list=__CUDA_FTZ=0 -nvvm-reflect -O3 t2.linked.bc -o t2.opt.bc
868 # llc -mcpu=sm_20 t2.opt.bc -o t2.ptx
872 The ``-nvvm-reflect-list=_CUDA_FTZ=0`` is not strictly required, as any
873 undefined variables will default to zero. It is shown here for evaluation
877 This gives us the following PTX (excerpt):
882 // Generated by LLVM NVPTX Back-End
891 .visible .entry kernel(
892 .param .u64 kernel_param_0,
893 .param .u64 kernel_param_1,
894 .param .u64 kernel_param_2
903 ld.param.u64 %rl2, [kernel_param_0];
905 ld.param.u64 %rl3, [kernel_param_1];
906 mul.wide.s32 %rl4, %r3, 4;
907 add.s64 %rl5, %rl2, %rl4;
908 ld.param.u64 %rl6, [kernel_param_2];
909 add.s64 %rl7, %rl3, %rl4;
910 add.s64 %rl1, %rl6, %rl4;
911 ld.global.f32 %f1, [%rl5];
912 ld.global.f32 %f2, [%rl7];
913 setp.eq.f32 %p1, %f1, 0f3F800000;
914 setp.eq.f32 %p2, %f2, 0f00000000;
915 or.pred %p3, %p1, %p2;
919 mov.f32 %f110, 0f3F800000;
920 st.global.f32 [%rl1], %f110;
922 BB0_2: // %__nv_isnanf.exit.i
924 setp.gtu.f32 %p4, %f4, 0f7F800000;
926 // %bb.3: // %__nv_isnanf.exit5.i
928 setp.le.f32 %p5, %f5, 0f7F800000;
930 BB0_4: // %.critedge1.i
931 add.f32 %f110, %f1, %f2;
932 st.global.f32 [%rl1], %f110;
934 BB0_5: // %__nv_isinff.exit.i
938 BB0_26: // %__nv_truncf.exit.i.i.i.i.i
939 mul.f32 %f90, %f107, 0f3FB8AA3B;
940 cvt.rzi.f32.f32 %f91, %f90;
941 mov.f32 %f92, 0fBF317200;
942 fma.rn.f32 %f93, %f91, %f92, %f107;
943 mov.f32 %f94, 0fB5BFBE8E;
944 fma.rn.f32 %f95, %f91, %f94, %f93;
945 mul.f32 %f89, %f95, 0f3FB8AA3B;
947 ex2.approx.ftz.f32 %f88,%f89;
949 add.f32 %f96, %f91, 0f00000000;
950 ex2.approx.f32 %f97, %f96;
951 mul.f32 %f98, %f88, %f97;
952 setp.lt.f32 %p15, %f107, 0fC2D20000;
953 selp.f32 %f99, 0f00000000, %f98, %p15;
954 setp.gt.f32 %p16, %f107, 0f42D20000;
955 selp.f32 %f110, 0f7F800000, %f99, %p16;
956 setp.eq.f32 %p17, %f110, 0f7F800000;
959 fma.rn.f32 %f110, %f110, %f108, %f110;
960 BB0_28: // %__internal_accurate_powf.exit.i
961 setp.lt.f32 %p18, %f1, 0f00000000;
962 setp.eq.f32 %p19, %f3, 0f3F800000;
963 and.pred %p20, %p18, %p19;
968 xor.b32 %r10, %r9, -2147483648;
970 BB0_30: // %__nv_powf.exit
971 st.global.f32 [%rl1], %f110;