Lto with objcode

Hi all,
I’m trying to write a lto pass and having are some problems.
There are two files a.c b.c, i compiled both of them to objcode with the commad

clang input.c -c -o output.o

Then link them and use my lto pass with the command

clang -flto -Xclang -load -Xclang a.o b.o -o output

I wish the command above can execute my pass in link time, but i found that the pass didn’t work :frowning:
I guess maybe i need to compile .c fiel to bitcode file instead of objcode?

Another question, can i convert objcode file to bitcode file?


Yes, you need to add -flto to the first compile command too.

Not really. I think there were experiments in this direction in the past but it’s a difficult problem and there’s certainly nothing in mainline LLVM for it.

LTO operates only on bitcode files. A native object file is essentially later in the pipeline of source to executable than bitcode, so LTO has nothing to work with in that case, hence why your pass hasn’t run. Compiling the .c file as a bitcode file (which you do by using -flto at compilation time) would solve that problem. Once you have a bitcode file, you can link it. Someone else will need to explain how you customise the passes during LTO, as I am not familiar with it (I doubt that -Xclang will do it).

LLVM supports C++ → bitcode → object file., but there is no support to reverse any of these steps.

There are pre- and post-link pass pipelines for full and thin lto. I don’t know how add custom passes to them without hacking LLVM.