Frage

I try to do some optimization on LLVM bitcode without generating final executable binary. I link all project bitcode.In this test there is no main function in bitcode, but LLVM needs to find a main function in module to internilize other functions, how can I change pass or passmanager that instead of looking main function as entry point for program, looks my special function like foo1 and suppose foo1 has main function's rule?

War es hilfreich?

Lösung

No, LLVM doesn't "look for main", it acts according to the linkage types of functions in your LLVM IR files.

The default linkage type is external meaning that the function may be required from some other object that hasn't been linked yet. The linker will not remove or internalize such functions, and it won't remove functions they call (unless inlined...). But if a function has internal or private linkage, it can be internalized or even removed if not called from other externally visible functions.


Update: As Oak points out in the comments, when the LLVM internalize pass is run in the default way, it indeed preserves main. However, you can control it by running the internalize pass on your own, passing it a list of symbols to preserve.

  • If you need link-time optimizations, call PassManagerBuilder::populatePassManager with Internalize set to false.
  • Add your own InternalizePass, with just the symbols you need.

This is already done in another place in the code base, in LTOCodeGenerator.cpp. Look at the comment in LTOCodeGenerator::generateObjectFile:

  // Enabling internalize here would use its AllButMain variant. It
  // keeps only main if it exists and does nothing for libraries. Instead
  // we create the pass ourselves with the symbol list provided by the linker.
  if (!DisableOpt) {
    PassManagerBuilder().populateLTOPassManager(passes,
                                              /*Internalize=*/false,
                                              !DisableInline,
                                              DisableGVNLoadPRE);
  }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top