14

I have a ModulePass that's working with the opt tool, but I'm having trouble figuring out how to make it available to clang at the command line. My current workflow for using my pass is:

  1. clang -c -emit-llvm [c-source code files]
  2. llvm-link [llvm bitcode files]
  3. opt -load [PassName].so -[pass-name] [linked llvm file]
  4. llc [resulting bitcode file]
  5. gcc [resulting assembler file] -o [target]

I would like to get my pass integrated with the clang command line so that it could be invoked as part of the build of existing software (e.g. c++ standard library) without having to remake the whole build system for each thing I compile. I've seen hints about how to do this, but I haven't been able to put the pieces together into a working setup.

Run an LLVM Pass Automatically with Clang describes exactly what I want, but the method appears to be deprecated in LLVM 3.4 (PassManagerBuilder has been moved to the legacy namespace).

LLVM - Run Own Pass automatically with clang seems to address the basic issue, but I was hoping I could do this without having to modify clang (which seems to be what's suggested there).

What is the best way to make a new pass available from clang using LLVM 3.4?

Community
  • 1
  • 1
Erik
  • 301
  • 2
  • 10
  • [Solution using the new pass manager](https://stackoverflow.com/questions/54447985/how-to-automatically-register-and-load-modern-pass-in-clang/75999804#75999804) – Igor Stoppa Apr 13 '23 at 09:05

1 Answers1

8

Clang still uses PassManagerBuilder as of 3.5 (see the PassManagerBuilderWrapper class in BackendUtil.cpp). So I believe extending it with RegisterStandardPasses, as in my blog post, is still the only way to add a pass to Clang's pass manager.

It's frustratingly difficult to find any information about how deprecated the "old" pass manager infrastructure is. But since Clang is still using it, it can't be that deprecated. :)

adrian
  • 1,447
  • 15
  • 24
  • Thanks for the answer, I'll continue with the PassManagerBuilder setup then. – Erik Nov 30 '14 at 13:32
  • 1
    It works on llvm-3.7. A note for registering ModulePass: change ``EP_EarlyAsPossible`` to ``EP_ModuleOptimizerEarly`` otherwise llvm may treat it as a FunctionPass and try to invoke ``runOnFunction``. – xywang Dec 16 '15 at 03:15
  • 1
    Make sure to use `-flegacy-pass-manager` on the latest llvm. – vaughan Jan 19 '22 at 12:02
  • Fast forward to 2023, the legacy pass manager is gone, for good. New approach here: https://stackoverflow.com/questions/54447985/how-to-automatically-register-and-load-modern-pass-in-clang/75999804#75999804 – Igor Stoppa Apr 20 '23 at 11:16