Nicolas Capens | 3e6c031 | 2022-01-26 10:13:33 -0500 | [diff] [blame] | 1 | LLVM Dependency |
| 2 | =============== |
| 3 | |
| 4 | Overview |
| 5 | -------- |
| 6 | |
| 7 | SwiftShader's [Reactor](Reactor.md) library uses LLVM |
| 8 | as one of its JIT-compiler backends. This page contains notes about building and |
| 9 | upgrading LLVM. |
| 10 | |
| 11 | Directory structure |
| 12 | ------------------- |
| 13 | |
| 14 | The current version of LLVM we use is 10, and can be found in |
| 15 | `third_party/llvm-10.0`. |
| 16 | |
| 17 | In this folder you will find the following directories: |
| 18 | |
| 19 | * configs : Contains per-platform headers that LLVM sources include to |
| 20 | configure the build. These are generated by running `scripts/update.py` |
| 21 | (more on that below). |
| 22 | * llvm : Contains a subset of the LLVM source code needed to build the JIT |
| 23 | support required by SwiftShader. |
| 24 | * scripts : Contains `update.py`, which is used to update the files in the |
| 25 | `configs` folder. More on that below. |
| 26 | |
| 27 | Updating the current version of LLVM to latest |
| 28 | ---------------------------------------------- |
| 29 | |
| 30 | Updating to the latest version of LLVM can be tricky to do manually, especially |
| 31 | because the [llvm-project repo](https://github.com/llvm/llvm-project) includes |
| 32 | much more than just LLVM (e.g. it includes all the Clang source). Furthermore, |
| 33 | we may have local changes to our copy of LLVM that must be maintained, or at |
| 34 | least considered across updates. |
| 35 | |
| 36 | To ease this pain, run the script `third_party/update-llvm-10.sh` on Linux. This |
| 37 | script works by updating a separate branch of SwiftShader, `llvm10-clean`, on |
| 38 | which the latest snapshot of LLVM is fetched and committed, and then this branch |
| 39 | is merged back into `master`. During the merge, if there are conflicts to |
| 40 | resolve because of local changes we've made, these can be resolved in the usual |
| 41 | manner, and the merge can be resumed. |
| 42 | |
| 43 | The script is configured to fetch from the branch in `LLVM_REPO_BRANCH`, and |
| 44 | will automatically grab the latest commit on that branch. |
| 45 | |
| 46 | Although not always necessary, if there were new configuration variables added |
| 47 | or modified, you may need to run `update.py` as described below. Otherwise, if |
| 48 | all goes well, the update to LLVM can be committed and pushed. |
| 49 | |
| 50 | Updating LLVM configuration files |
| 51 | --------------------------------- |
| 52 | |
| 53 | The script `third_party/llvm-10.0/scripts/update.py` is used to update the |
| 54 | config files in `third_party/llvm-10.0/configs`. |
| 55 | |
| 56 | Before running this script, you must make sure to update two variables in it |
| 57 | (and commit this change): |
| 58 | |
| 59 | ``` |
| 60 | # LLVM_BRANCH must match the value of the same variable in third_party/update-llvm-10.sh |
| 61 | LLVM_BRANCH = "release/10.x" |
| 62 | |
| 63 | # LLVM_COMMIT must be set to the commit hash that we last updated to when running third_party/update-llvm-10.sh. |
| 64 | # Run 'git show -s origin/llvm10-clean' and look for 'llvm-10-update: <hash>' to retrieve it. |
| 65 | LLVM_COMMIT = "d32170dbd5b0d54436537b6b75beaf44324e0c28" |
| 66 | ``` |
| 67 | |
| 68 | The script takes a platform as argument, and extra CMake args. For example, to |
| 69 | update the Linux configs, run: |
| 70 | |
| 71 | ``` |
| 72 | python3 update.py linux -j 200 |
| 73 | ``` |
| 74 | |
| 75 | This script does the following: |
| 76 | |
| 77 | * Clones the LLVM repo and checks out `LLVM_COMMIT` from `LLVM_BRANCH`. |
| 78 | * Builds LLVM specifically for the target architectures specified in |
| 79 | `LLVM_TRIPLES` dictionary. |
| 80 | * Copies the specified platform config files to |
| 81 | `third_party/llvm-10.0/configs`, applying certain transformations to the |
| 82 | files, such as undefining macros listed in `LLVM_UNDEF_MACROS` (see the |
| 83 | `copy_platform_file` function). |
| 84 | |
| 85 | Note that certain configuration options depend on the host OS, you will need to |
| 86 | run the script on the right host OS. See the `LLVM_PLATFORM_TO_HOST_SYSTEM` |
| 87 | dictionary for the mapping, which looks like this at the time of this writing: |
| 88 | |
| 89 | ``` |
| 90 | # Mapping of target platform to the host it must be built on |
| 91 | LLVM_PLATFORM_TO_HOST_SYSTEM = { |
| 92 | 'android': 'Linux', |
| 93 | 'darwin': 'Darwin', |
| 94 | 'linux': 'Linux', |
| 95 | 'windows': 'Windows', |
| 96 | 'fuchsia': 'Linux' |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | Generally, Windows to build Window, Darwin to build Darwin (MacOS), and Linux |
| 101 | for everything else. Also note that for android and fuchsia, the config is |
| 102 | closest to that of Linux, but you will likely have to manually tweak the configs |
| 103 | (in particular, `configs/<platform>/include/llvm/Config/config.h`). |
| 104 | |
| 105 | Supported platforms, architectures, and build systems |
| 106 | ----------------------------------------------------- |
| 107 | |
| 108 | SwiftShader is used by many products on many architectures: |
| 109 | |
| 110 | * OS: Windows, Linux, MacOS, Android, Fuchsia |
| 111 | * Architecture: x64, x86, ARM, ARM64, MIPS, MIPS64 |
| 112 | * Build systems: CMake, GN, Soong, Blaze |
| 113 | |
| 114 | Upgrading/updating LLVM usually entails making sure it builds for all of these. |