Installing LLVM on Windows, Raspbian & macOS
Installing LLVM on Different Platforms
Get the pre-built binaries from LLVM download page.
After installing Clang for Windows, follow the Getting Started tutorial to compile a C file to LLVM bitcode file:
clang -O3 -emit-llvm hello.c -c -o hello.bc
If you don’t have a hex editor installed, you can view the bc file with wasmcodeexplorer online.
According to the tutorial, run the program as follows:
However, where is lli? There is no pre-built binary available for download online except the source code. Therefore, we can download the LLVM source code and build it ourselves. The lli project is located at **llvm-
Build the source code with CMake:
mkdir llvm-<version>\build cd build cmake .. cmake --build .
Once the build is done, we can find lli.exe under **llvm-
sudo apt-get install clang
Choose a Clang version:
sudo apt-get install clang-<version number>
sudo apt-get install llvm
Can we run the bc file built with Clang for Windows on Raspbian? I got the following error message:
lli: hello.bc: error: Invalid type for value
Not like Java bytecode, we cannot run one copy of LLVM bitcode directly on all platforms. But, we can compile the bc file to native code with Clang:
clang hello.bc -o hello
Use Homebrew to Install LLVM:
brew install LLVM
Export the path to ~/.bashrc:
Compile hello.bc file on macOS:
Emscripten vs. LLVM
G:\llvm>emcc hello.bc -o hello.js error: Invalid type for value error: Invalid type for value Traceback (most recent call last): File "E:\Program Files\Emscripten\emscripten\1.35.0\\emcc", line 1309, in <module> final = shared.Building.llvm\_opt(final, link\_opts, DEFAULT\_FINAL) File "E:\Program Files\Emscripten\emscripten\1.35.0\tools\shared.py", line 1471, in llvm\_opt assert os.path.exists(target), 'Failed to run llvm optimizations: ' + output AssertionError: Failed to run llvm optimizations:
Here is the reason:
The backend is still too new to be in the upstream LLVM repository. As such, builds from Linux distributions will not contain Fastcomp, and Emscripten will report an error if you try to use them.
We can compare the bc files that generated by emcc and Clang.
Because Emscripten has the gene of LLVM, Clang can compile the bc file built with emcc:
emcc hello.c -o hello.bc clang hello.bc -o hello.exe > hello.exe hello world