Compile assembly to llvm bitcode

Hi all,

Is it possible to compile assembler source code to llvm bitcode? And if yes, then how?

I’m trying to compile a project to llvm bitcode. The project has both C (.c) sources and some assembly(.S) sources. The make compiles all sources to object (.o) files and then links them together. What I’m trying to do is replace all commands compiling to .o with ‘clang -c -emit-llvm …’ to get bitcodes and then use “llvm-link” to link them together in one .bc. However the llvm-link fails as the bitcodes I got from .s files are not recognized as llvm bitcodes (the header does not correspond to llvm bitcode header).

Best,
Anahit.

There’s no compiler driver support for that task, no.

Why are you trying to compile everything to bitcode/link them together in that way? (might be other solutions to consider)

If you are able to treat the assembly routines as calls to external functions, you should be able to use LLD or gold for this use case instead of llvm-link. In this case, the .s files will be compiled to native objects, symbol resolution will occur as usual, LLD will perform an LTO link, and there should be a way to get the fully linked and optimized bitcode blob before it is compiled to a native object file. At the very least, you can do this with -save-temps. I can’t find a better way to do it at the moment, although I see -lto-obj-path=, which controls where the native object file goes.