# Polly - GPGPU Code Generation

WARNING: This project was part of the Google Summer of Code 2012. It is currently not finished, but it is in the design and implementation stage. The ideas/plans described here may not yet be implemented in Polly and may change later on.

This project adds GPGPU code generation feature to Polly.

## Objective

The overall objective of this GSoC project is to create a preliminary implementation of GPGPU code generation for Polly. With this addition, users can parallelize some perfectly nested loops with Polly to execute on a heterogeneous platform, composed of CPU and GPU.

There are several successful projects about automatic source-to-source gpu code transformation. C-to-CUDA[1] uses the standard Pluto algorithms for computing an affine schedule and then applies a wavefront transformation to obtain one sequential and n-1 parallel loops. The parallel loops are then mapped onto the blocks and threads of GPU. PPCG[2] introduces some advanced algorithms which can expose much more parallelism than other methods . And It also introduces affine partition heuristics and code generation algorithms for locality enhancement in the registers and shared memory.

Since automatic GPGPU code generation is quite a complex problem and what we target is a low-level intermediate representation, LLVM IR, rather than a high-level language source, it is important for us to set a proper objective as a start step to give a complete solution to GPGPU code generation for LLVM IR.

Firstly, we plan to target two kinds of relatively simple test cases. One is comprised of pure parallel and perfectly nested loops, like the following code.

    parfor(int i=0 to M)
      parfor(int j=0 to N)
        LoopBody(i, j);

Another one is that all the loops in it are parallel except the inner-most one, just like this:

    parfor(int i=0 to M)
      parfor(int j=0 to N)
        non-parfor(int k=0 to K)
          LoopBody(i, j, k);

The LoopBody part should be limited to instructions or functions calls (intrinsics) which can be handled by LLVM's NVPTX backend.

On the other hand, we focus on building a preliminary and scalable framework of GPGPU code generation for polly. Thus we plan to employ relatively simple tiling and mapping algorithms and optimize them later.

## Work Flow

### GPGPU Code Generation In General

C-to-CUDA[1] and PPCG[2] propose similar steps to solve the automatic GPGPU code generation problem.

- Look for parallel loops.
- Create a polyhedral model from the loops.
- Tile and map the loops to GPU blocks and threads.
- Determine where to place the data.

### What has been done in Polly

Polly has implemented the 1st, 2nd and part of the 3rd of the above steps and many other analysis and transformation passes.

### What to do in Polly

Unlike many source-to-source optimizers such as C-to-CUDA and PPCG, Polly is a low-level optimizer, which means we can't use a source-level compiler (e.g. NVCC) to generate the final assembly for the device. We need manually insert device driver API calls to execute the generated kernel assembly text.

In this project, we assume that the device driver library has provided an interface to launch kernels in the form of assembly text. Fortunately, most of the mainstream GPU vendors provide such a feature in their products (see ptxjit of NVIDIA GPUs and CAL of AMD GPUs). Generally speaking, what we are going to do in Polly is:

- Find a way to tile the parallel loops.
- Find a way to extract the loop body and transform it into thread-centric parallel code.
- Find a way to store/load the thread-centric code into/from a device module.
- Find a way to pass the target machine information and generate code of the device module for the target.
- Find a way to map the tiled loop to GPU blocks and threads.
- Find a way to insert CUDA synchronization operations on-demand.
- Find a way to generate the memory copy operations between a host and a device.
- Implement/Wrap a runtime library to serve as the execution engine for the generated device code.

### The Work Flow

In this section, we assume that the host cpu is X86 and the device is NVIDIA CUDA-compatible. we will use the following test case to describe our work flow.

    for(i = 0; i < 128; i++)
          for(j = 0; j < 128; j++)
                  A[i][j] = i*128 + j;

The work flow of our code generator is as follows.

1.We first use Polly's jscop file importer to get a wanted 4-level parallel tiled code.

The "schedule" part of the pre-optimization jscop file is as the following:

    "schedule" : "{ Stmt_for_body3[i0, i1] -> schedule[0, i0, 0, i1, 0] }"

The jscop file describing the tiling transformation is:

    "schedule" : "{ Stmt_for_body3[i0, i1] -> schedule[0, o0, o1, o2, o3]:
                  o0 >= 0 and o0 <= 7 and o1 >= 0 and o1 <= 15 and
                  o2 >= 0 and o2 <= 7 and o3 >= 0 and o3 <= 15 and
                  i0 = 16o0 + o1 and i1 = 16o2 + o3 }"

We can test the schedule with the following command line.

    opt -load /path/to/polly/build/LLVMPolly.so -basic-aa -polly-import-jscop
        -polly-ast -analyze -q ./test.ll
        -polly-import-jscop-postfix=transformed+gpu

The output of this schedule is:

    for (c2=0;c2<=7;c2++) {
      for (c3=0;c3<=15;c3++) {
        for (c4=0;c4<=7;c4++) {
          for (c5=0;c5<=15;c5++) {
            Stmt_for_body3(16*c2+c3,16*c4+c5);
          }
        }
      }
    }

Now we get a 4-dimensional parallel loops with a single SCoP statement in it.

2.We then extract the loop body (or the inner-most non-parallel loop) into a LLVM function, tagging it with PTX_Kernel call convention.

3.We extract the PTX_kernel function into a temporary module, set the target triple (e.g. nvptx64-unknown-linux) for the module, transform the temporary module into a string, store it in the original module and erase the PTX_kernel function.

4.We replace the loops with their GPGPU counterpart. The GPGPU part of code is composed of a call to the llvm.codegen intrinsic and function calls to our GPU runtime library.

5.Finally, we generate the executable program with llc or run the optimized LLVM IRs with a JIT compiler like lli.

## Usage

1. Apply the llvm.codegen intrinsic patch to LLVM code base.

    cd /path/to/llvm/source
    git am /path/to/polly/source/utils/0001-Add-llvm.codegen-intrinsic.patch

2. Build the test case.

    /path/to/polly/source/test/create_ll.sh test.c

3. Get and edit the jscop file (take function "gpu_codegen" as an example).

    opt -load /path/to/polly/build/lib/LLVMPolly.so -basic-aa
        -polly-export-jscop ./test.ll
    cp gpu_codegen___%for.cond---%for.end8.jscop
       gpu_codegen___%for.cond---%for.end8.jscop.transformed+gpu
    vi gpu_codegen___%for.cond---%for.end8.jscop.transformed+gpu

(Please refer to section "The Work Flow" on how to edit the "schedule" part of a statement)

4. Optimize the code with GPGPU code generation.

    opt -load /path/to/polly/build/lib/LLVMPolly.so -basic-aa
        -polly-import-jscop-postfix=transformed+gpu -enable-polly-gpgpu
        -polly-gpgpu-triple=nvptx64-unknown-unknown -polly-codegen ./test.ll -S
        -o test.gpued.ll

5. Build the final assembly and executable.

    llc test.gpued.ll -o test.s
    gcc test.s -lGPURuntime -o test

(Please make sure that LD_LIBRARY_PATH is set properly so that /path/to/polly/build/lib/libGPURuntime.so is visible to gcc.)

## TODO List

Tasks | Status | Owner
--- | --- | ---
Tiling the Parallel Loops with An External Jscop File | Open, In Design | Yabin Hu
GPU Runtime Library Implementation | Coding Finished, In Reviewing |
llvm.codegen Intrinsic Implementation | Codeing Finished, To Be Reviewed |
Code Generation For Host | 50% Done |

## References

- Automatic C-to-CUDA Code Generation for Affine Programs.
  Muthu Manikandan Baskaran, J. Ramanujam and P. Sadayappan.
  International Conference on Compiler Construction (CC) 2010.
- PPCG Project
  http://freecode.com/projects/ppcg
- Where is the Data? Why You Cannot Debate GPU vs. CPU Performance Without the Answer.
  Chris Gregg and Kim Hazelwood
  International Symposium on Performance Analysis of Systems and Software (ISPASS) 2011.