Search code examples
debuggingllvmllvm-clang

How to find hanging LLVM optimization pass?


I've written an LLVM pass that replaces a few store instructions with calls to a function that perform some book-keeping, and then performs the store in a special way. It works fine when I compile with -O0, but I can only guarantee the functionality of my pass when using -O3. When I compile with -O3(or -O1/-O2), it completes my pass successfully, and then hangs in some later optimization stage. Is there a way to discover which optimization pass is hanging / why?

Just so I don't have to provide it later, here is my code and my compile line.

clang++-5.0 -std=c++11 -Xclang -load -Xclang ../../plugin/build/mylib.so single_param.cc -c -I ../../libs/ -S -emit-llvm -O3

The problem is not in code generation because I'm only generating bitcode. I noticed that stores in -O3 (without my pass) include alias information, and I thought that since I'm deleting these instructions, some later optimization using this alias information might encounter some trouble, so I turned off most of the alias analysis using -fno-strict-aliasing

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>    

void __attribute__((noinline)) f(int *n){
  *n = *n + 1;
}
int main(){
  int a = 4;
  f(&a);

  return a;
 }

Solution

  • The way I was able to find the pass that was stalling was by turning remarks on with

    -Rpass=.* -Rpass-missed=.* -Rpass-analysis=.*
    

    I found that the only optimization pass giving remarks was tail call optimization, so I turned it off. I later found the problem with my code, but this is how I found the problem I was causing.