Printing from LLVM for debugging the instruction pattern written for backend

I would like to investigate the reason why the code modified is not invoking the intended assembly instruction for the instruction pattern written in .td file.

I can examine the LLVM IR using the following

clang -c -Xclang -disable-O0-optnone -emit-llvm -g -S foo.c -o foo.bc

  1. Is this the write way to debug?
  2. I want to know the debugging info helps in this and if so, how can I use Debug info
  3. I am a beginner and please provide me an apt tutorial to learn writing the backend pattern for llvm.

Well, that command would generate debug info that (if you compiled all the way to an executable program) would help you debug the program as it ran. I think what you want, though, is not to debug the compiled program, instead you want to debug the compiler itself.

If you remove -g from the command you showed, that should get you optimized IR, and then for instruction selection you can use the llc tool to compile the .bc file. There should be options that will help you diagnose what is going on.

1 Like

Thank you .I will try it out and get back.

Hello,
a fellow beginner here, the presentation below was quite useful for me. See slides 32-40 for various debug printing options. Hope this helps.

1 Like

For a beginner like me this document is a good start.Thank you so much.

Sir,
Thank you sir for your valuable suggestion
I am instructed to perform a round trip through all the files visited and print a flag like “i am here” interrogate the flow of execution through llvm back-end.

My 1st motto is to print from llvm to standard i/o.
I have tried llc to get all the passes visited.

llc -print-after-all -o foo.s foo.opt.ll 2>&1| grep Dump

  1. Printing from llvm to standard i/o.
  2. Am I able to do debugging by setting break points for the entire LLVM like we do for C programs ?

I do this quite often.

If you build LLVM with debug-info enabled (Debug or RelWithDebInfo mode) then you should be able to use a debugger to set breakpoints in the opt or llc tools, same as any other C++ program.

Thanking you for using llc I could analyze few passes with that.The task was to give a simple program
that contains (inlined) assembly instruction and get the instructions flow inside LLVM compiler.So that I need to print from llvm.
Could you suggest a way for that sir?

llc --help-hidden | grep print shows that there are --print-before-all and --print-after-all options, which might be helpful to you. You can also selectively print before or after specific passes with --print-before=<pass-namess> and --print-after=<pass-names>.
These dumps are quite large, I think, so you probably want to capture them to a file and look at them afterward.

1 Like

Thank you sir.

Thank you sir.I have used that too.

llc -O2 -print-after-all -o foo.s foo.opt.ll 2>&1| grep Dump
llc -O2 -print-after-all -o foo.s foo.opt.ll

Printing an Instruction:

Instruction *I;
I->dump(); or outs() << *I << “\n”;

Printing an Entire Basic Block:

BB->dump() or outs() << *BB << “\n”;

  1. Can I add the following statement for debugging TableGen files and associated file ?

outs()<<“I am here\n”;

2.If it is possible could you point

  • which pass to be inspected
  • how to get the output of that pass

I don’t know enough about backends to give any further suggestions.
@RKSimon maybe you can provide some guidance?

1 Like

Thank you for your valuable help.