Issue with Executing First MachineFunctionPass (original) (raw)

January 25, 2025, 7:48pm 1

Hello all,

I am currently trying to write my first MachineFunctionPass, but I’m having trouble getting it to work. None of the threads I have found addressed my particular problem, so I thought I would ask my question since I’m still new to LLVM.

Right now, I simply want to create an X86 MachineFunctionPass based on
this tutorial and other passes in the X86 backend. The pass will print out a message for every MachineFunction it visits, so I am not doing anything too complicated.

I was able to write my pass, and LLVM was successfully rebuilt; however, my pass is not executing when I run llc on any .ll file. I know this is the case because there is no output telling me which MachineFunctions I visited from executing llc. Additionally, when I insert a debug print statement into my pass (i.e. LLVM_DEBUG(dbgs() << getPassName() << "\n");) and execute llc test.ll --debug-pass=Structure, the pass name is not appearing anywhere in the debug output.

I also decided to check if I was able to edit a different MachineFunctionPass, so I edited X86ReturnThunks.cpp to include the following line:

bool X86ReturnThunks::runOnMachineFunction(MachineFunction &MF) {
LLVM_DEBUG(dbgs() << getPassName() << “\n”);
LLVM_DEBUG(dbgs() << “******** Hi I edited this ********\n”); // Line I added

}

I noticed that when remaking my LLVM build and executing llc test.ll --debug-pass=Structure, the line I added does not appear in any of the debug output, only X86ReturnThunks (the pass name) appears.

I’ll be honest, I’m not entirely sure what is wrong unless I am not rebuilding LLVM correctly. I have even tried a full rebuild of LLVM that included my changes, but it didn’t work still. If anyone has suggestions for what I might be doing wrong, it would be greatly appreciated.

kparzysz January 25, 2025, 7:52pm 2

You’re likely not running the llc that you built. Also, did you build the LLVM with static or shared libraries?

Edit:
If you do make <some-target> (but not install), the binaries end up located at <build-directory>/bin. Make sure you run your llc from there.

Looking into it, I did not use the BUILD_SHARED_LIBS option when generating my make files, so it seems I’m building LLVM with static libraries since that is the default behavior.

That’s a bit weird a different version of llc might be running. When I rebuild LLVM, I noticed llc was indeed being rebuilt, so I presumed the older executable would be removed for the newer one.

It seems I was executing the wrong executable this entire time… 💀

Thank you for the suggestion, it’s now working!!! 🕶️