How to output intermediate representation after every LLVM pass and backend step

I’m looking for command line flags in Clang/LLVM that behave similarly to GCC’s debugging options for dumping compiler internals at various stages.

Specifically, I need to see the LLVM intermediate representation state both before and after every optimization pass runs. I also want to examine the abstract syntax tree output from Clang and view all the backend compilation stages like instruction selection, register assignment, and machine code generation.

So far I only discovered the -ccc-print-phases flag in Clang, but this just shows basic phase names like preprocessing and linking without any actual IR output. I also found -cc1-ast-dump for viewing ASTs, but I need more comprehensive debugging output especially for the code generation pipeline.

Is there a way to get detailed dumps at each compilation step similar to how GCC handles this?

you can try -mllvm -print-module-scope with -O1 -Xclang -disable-llvm-passes to see unoptimized IR. then, enable passes one by one. for machine code, -debug-only=regalloc shows reg allocation and -debug-only=machine-scheduler for scheduling. don’t forget the -mllvm prefix for backend flags!

Want detailed LLVM IR dumps? Use -print-after-all and -print-before-all to see IR state before and after each optimization pass. You can use them separately or together depending on what you’re debugging. For backend stages, -debug-only=isel shows instruction selection details, while -print-machineinstrs lets you see machine instructions during code generation. Pipe the output to a file since there’s tons of data. Also, -filter-print-funcs=function_name narrows output to specific functions so you don’t get dumps from every function in your program.

Oh nice! What specific optimization are you debugging? I’ve had good luck with -mllvm -print-after=<pass-name> to target individual passes instead of dumping everything. Are you working with C++ templates or plain C? Templates make the IR super messy - you might wanna -mllvm -print-module-scope to keep it readable.