Idiomatic Linking, Optimization, and Compilation of LLVM Bitcode Files with Meson

141 Views Asked by At

I'm hoping to get pointed in the right direction for the most idiomatic way to link and compile LLVM bitcode (.bc) files generated by different compilers using the Meson build system.

In a nutshell (from ISPC FAQ):

$ ispc --emit-llvm -o foo_ispc.bc foo.ispc
$ clang -O2 -c -emit-llvm -o foo_cpp.bc foo.cpp

$ llvm-link foo_ispc.bc foo_cpp.bc -o - | opt -O3 -o foo_opt.bc
$ llc -filetype=obj foo_opt.bc -o foo.o

The purpose is to facilitate inlining and cross-function optimizations in code compiled with Intel's ISPC compiler and vanilla clang.

I've found examples of defining a generator for ISPC, the gap in knowledge is how to best link/optimize/compile LLVM bitcode (.bc) files using LLVM's bitcode linker (llvm-link), optimizer (opt), and static compiler (llc). I cannot find any meson build scripts in the wild that that achieve this.

I can see that assembler (.S) and LLVM IR (.ll) files are supported by library() and executable(), but cannot find any mentions of LLVM bitcode in the docs nor the source.

Thanks a bunch!

1

There are 1 best solutions below

0
On

If you are looking for a way to make library() and executable() work with .bc files, sadly, your only option is to submit an upstream change. There's a similar question in the FAQ that sums up the reasoning:

https://mesonbuild.com/FAQ.html#i-have-proprietary-compiler-toolchain-x-that-does-not-work-with-meson-how-can-i-make-it-work

Meson needs to know several details about each compiler in order to compile code with it. These include things such as which compiler flags to use for each option and how to detect the compiler from its output. This information can not be input via a configuration file, instead it requires changes to Meson's source code that need to be submitted to Meson master repository. In theory you can run your own forked version with custom patches, but that's not good use of your time. Please submit the code upstream so everyone can use the toolchain.

Your question is about linking and not compiling but I think the answer is still the same.