Debugging LLVM with lldb python plugin

I’m working on a project to reduce compile time of benchmark. With the help Linux profiler perf, I was able to find the hottest function in LLVM for my benchmark.

The function turned out to be ScalarEvolution::forgetTopMostLoop(). This function is called to clean up LLVM’s internal loop information after a loop is transformed. For example, LoopRotation changes the structure of the loop, so the loop info needs to be recomputed.

I decided to collect data on which loop’s info is being cleaned up from which Pass of the compiler. There are tools to collect call stack trace of a function, but I also wanted to know the name of LoopHeader Block.

lldb’s python plugin is the perfect tool for this job.

Fire up lldb
lldb -- opt benchamrk.fe.opt -o benchmark.opt.bc

Setup a breakpoint
b ScalarEvolution.cpp:1789

Add a python script to the breakpoint
br co add -s python 1

and here is my python script to capture the Loop Name & Call Stack.

thread = frame.GetThread()
process = thread.GetProcess()
loop_name = str(thread.frames[0].EvaluateExpression("L->getName()"))
if loop_name:
cs = []
for frame in thread:
func_name = frame.GetFunction().name
with open("loop_forgetTopMostLoop.txt", "a") as f:
f.write(", ")

Now run the application on lldb

C++ Developer. Worked on Windows, Linux & MacOS. CUDA development. LLVM Compiler development