Getting Started with the LLVM System using Microsoft Visual Studio¶
Overview¶
Welcome to LLVM on Windows! This document only covers LLVM on Windows usingVisual Studio, not WSL, mingw or cygwin. In order to get started, you first needto know some basic information.
There are many different projects that compose LLVM. The first piece is theLLVM suite. This contains all of the tools, libraries, and header files neededto use LLVM. It contains an assembler, disassembler, bitcode analyzer andbitcode optimizer. It also contains basic regression tests that can be used totest the LLVM tools and the Clang front end.
The second piece is theClang front end. Thiscomponent compiles C, C++, Objective C, and Objective C++ code into LLVMbitcode. Clang typically uses LLVM libraries to optimize the bitcode and emitmachine code. LLVM fully supports the COFF object file format, which iscompatible with all other existing Windows toolchains.
There are more LLVM projects which this document does not discuss.
Requirements¶
Before you begin to use the LLVM system, review the requirements givenbelow. This may save you some trouble by knowing ahead of time what hardwareand software you will need.
Hardware¶
Any system that can adequately run Visual Studio 2019 is fine. The LLVMsource tree including the git index consumes approximately 3GB.Object files, libraries and executables consume approximately 5GB inRelease mode and much more in Debug mode. SSD drive and >16GB RAM arerecommended.
Software¶
You will needVisual Studio 2019 orlater, with the latest Update installed. Visual Studio Community Editionsuffices.
You will also need theCMake build system since itgenerates the project files you will use to build with. CMake is bundled withVisual Studio 2019 so separate installation is not required. If you do installCMake separately, Visual Studio 2022 will require CMake Version 3.21 or later.
If you would like to run the LLVM tests you will needPython. Version 3.8 and newer are known to work. You caninstall Python with Visual Studio 2019, from the Microsoft store or fromthePython web site. We recommend the latter since itallows you to adjust installation options.
You will needGit for Windows with bash tools, too.Git for Windows is also bundled with Visual Studio 2019.
Getting Started¶
Here’s the short story for getting up and running quickly with LLVM.These instruction were tested with Visual Studio 2019 and Python 3.9.6:
Download and installVisual Studio.
In the Visual Studio installer, Workloads tab, select theDesktop development with C++ workload. Under Individual components tab,selectGit for Windows.
Complete the Visual Studio installation.
Download and install the latestPython 3 release.
In the first install screen, select bothInstall launcher for all usersandAdd Python to the PATH. This will allow installing psutil for allusers for the regression tests and make Python available from the commandline.
In the second install screen, select (again)Install for all users andif you want to developlldb, selectingDownload debug binaries is useful.
Complete the Python installation.
- Run a “Developer Command Prompt for VS 2019”as administrator. This command
prompt provides correct path and environment variables to Visual Studio andthe installed tools.
In the terminal window, type the commands:
c:cd \
You may install the llvm sources in other location than
c:\llvm
but do notinstall into a path containing spaces (e.g.c:\DocumentsandSettings\...
)as it will fail.
Register the Microsoft Debug Interface Access (DIA) DLLs
regsvr32"%VSINSTALLDIR%\DIA SDK\bin\msdia140.dll"regsvr32"%VSINSTALLDIR%\DIA SDK\bin\amd64\msdia140.dll"
The DIA library is required for LLVM PDB tests andLLDB development.
Install psutil and obtain LLVM source code:
pip install psutilgit clone https://github.com/llvm/llvm-project.git llvm
Instead of
gitclone
you may download a compressed source distributionfrom thereleases page.Select the last link:Sourcecode(zip)
and unpack the downloaded file usingWindows Explorer built-in zip support or any other unzip tool.
Finally, configure LLVM using CMake:
cmake -S llvm\llvm -B build -DLLVM_ENABLE_PROJECTS=clang -DLLVM_TARGETS_TO_BUILD=X86 -Thost=x64exit
LLVM_ENABLE_PROJECTS
specifies any additional LLVM projects you want tobuild whileLLVM_TARGETS_TO_BUILD
selects the compiler targets. IfLLVM_TARGETS_TO_BUILD
is omitted by default all targets are builtslowing compilation and using more disk space.See theLLVM CMake guide for detailed information abouthow to configure the LLVM build.The
cmake
command line tool is bundled with Visual Studio but its GUI isnot. You may installCMake to use its GUI to changeCMake variables or modify the above command line.
Once CMake is installed then the simplest way is to just start theCMake GUI, select the directory where you have LLVM extracted to, andthe default options should all be fine. One option you may reallywant to change, regardless of anything else, might be the
CMAKE_INSTALL_PREFIX
setting to select a directory to INSTALL toonce compiling is complete, although installation is not mandatory forusing LLVM. Another important option isLLVM_TARGETS_TO_BUILD
,which controls the LLVM target architectures that are included on thebuild.CMake generates project files for all build types. To select a specificbuild type, use the Configuration manager from the VS IDE or the
/property:Configuration
command line option when using MSBuild.By default, the Visual Studio project files generated by CMake use the32-bit toolset. If you are developing on a 64-bit version of Windows andwant to use the 64-bit toolset, pass the
-Thost=x64
flag whengenerating the Visual Studio solution. This requires CMake 3.8.0 or later.
Start Visual Studio and select configuration:
In the directory you created the project files will have an
llvm.sln
file, just double-click on that to open Visual Studio. The default VisualStudio configuration isDebug which is slow and generates a huge amountof debug information on disk. For now, we recommend selectingReleaseconfiguration for the LLVM project which will build the fastest orRelWithDebInfo which is also several time larger than Release.Another technique is to build all of LLVM in Release mode and changecompiler flags, disabling optimization and enabling debug information, onlyfor specific libraries or source files you actually need to debug.
Test LLVM in Visual Studio:
You can run LLVM tests by merely building the project “check-all”. The testresults will be shown in the VS output window. Once the build succeeds, youhave verified a working LLVM development environment!
You should not see any unexpected failures, but will see many unsupportedtests and expected failures:
114>Testing Time: 1124.66s114> Skipped : 39114> Unsupported : 21649114> Passed : 51615114> Expectedly Failed: 93========== Build: 114 succeeded, 0 failed, 321 up-to-date, 0 skipped ==========``
Alternatives to manual installation¶
Instead of the steps above, to simplify the installation procedure you can useChocolatey as package manager.After theinstallation of Chocolatey,run these commands in an admin shell to install the required tools:
choco install -y git cmake python3pip3 install psutil
There is also a WindowsDockerfilewith the entire build tool chain. This can be used to test the build with atool chain different from your host installation or to create build servers.
Next steps¶
Read the documentation.
Seriously, read the documentation.
Remember that you were warned twice about reading the documentation.
Test LLVM on the command line:¶
The LLVM tests can be run by changing directory to the llvm sourcedirectory and running:
c:\llvm> python ..\build\Release\bin\llvm-lit.py llvm\test
This example assumes that Python is in your PATH variable, which would beafterAdd Python to the PATH was selected during Python installation.If you had opened a command window prior to Python installation, you wouldhave to close and reopen it to get the updated PATH.
A specific test or test directory can be run with:
c:\llvm> python ..\build\Release\bin\llvm-lit.py llvm\test\Transforms\Util
Build the LLVM Suite:¶
The projects may still be built individually, but to build them all donot just select all of them in batch build (as some are meant asconfiguration projects), but rather select and build just the
ALL_BUILD
project to build everything, or theINSTALL
project,which first builds theALL_BUILD
project, then installs the LLVMheaders, libs, and other useful things to the directory set by theCMAKE_INSTALL_PREFIX
setting when you first configured CMake.The Fibonacci project is a sample program that uses the JIT. Modify theproject’s debugging properties to provide a numeric command line argumentor run it from the command line. The program will print thecorresponding fibonacci value.
Links¶
This document is just anintroduction to how to use LLVM to do some simplethings… there are many more interesting and complicated things that you cando that aren’t documented here (but we’ll gladly accept a patch if you want towrite something up!). For more information about LLVM, check out:
Additional information about the LLVM directory structure and tool chaincan be found on the mainGetting Started with the LLVM System page.
If you are having problems building or using LLVM, or if you have any othergeneral questions about LLVM, please consult theFrequently Asked Questions page.