Joel E. Denny 18f8106f31
[KernelInfo] Implement new LLVM IR pass for GPU code analysis (#102944)
This patch implements an LLVM IR pass, named kernel-info, that reports
various statistics for codes compiled for GPUs. The ultimate goal of
these statistics to help identify bad code patterns and ways to mitigate
them. The pass operates at the LLVM IR level so that it can, in theory,
support any LLVM-based compiler for programming languages supporting
GPUs. It has been tested so far with LLVM IR generated by Clang for
OpenMP offload codes targeting NVIDIA GPUs and AMD GPUs.

By default, the pass runs at the end of LTO, and options like
``-Rpass=kernel-info`` enable its remarks. Example `opt` and `clang`
command lines appear in `llvm/docs/KernelInfo.rst`. Remarks include
summary statistics (e.g., total size of static allocas) and individual
occurrences (e.g., source location of each alloca). Examples of its
output appear in tests in `llvm/test/Analysis/KernelInfo`.
2025-01-29 12:40:19 -05:00

1.1 KiB

The tests in this directory check that basic KernelInfoPrinter functionality behaves reasonably for LLVM IR produced by Clang OpenMP codegen.

So that these tests are straightforward to maintain and faithfully represent Clang OpenMP codegen, do not tweak or reduce the LLVM IR in them. Other tests more exhaustively check KernelInfoPrinter features using reduced LLVM IR.

The LLVM IR in each test file $TEST can be regenerated as follows in the case that Clang OpenMP codegen changes or it becomes desirable to adjust the source OpenMP program below. First, remove the existing LLVM IR from $TEST. Then, where $TARGET (e.g., nvptx64-nvidia-cuda-sm_70 or amdgcn-amd-amdhsa-gfx906) depends on $TEST:

$ cd /tmp
$ cat test.c
#pragma omp declare target
void f();
void g() {
  int i;
  int a[2];
  f();
  g();
}
#pragma omp end declare target

void h(int i) {
  #pragma omp target map(tofrom:i)
  {
    int i;
    int a[2];
    f();
    g();
  }
}

$ clang -g -fopenmp --offload-arch=native -save-temps -c test.c
$ llvm-dis test-openmp-$TARGET.bc
$ cat test-openmp-$TARGET.ll >> $TEST