forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyo_llvm.sh
executable file
·225 lines (197 loc) · 7.98 KB
/
byo_llvm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env bash
# Copyright 2023 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# This script illustrates how IREE can be built with various forms of LLVM
# installations with the option -DIREE_BUILD_BUNDLED_LLVM=OFF set, disabling
# a source dependency on the bundled LLVM submodule.
#
# Usage:
# byo_llvm.sh build_llvm && \
# byo_llvm.sh build_mlir && \
# byo_llvm.sh build_iree
#
# Additionally, to run tests:
# byo_llvm.sh test_iree
#
# This script has minimal configurability, which can be extended as needed. The
# defaults should suffice for testing on CI. Different configurations are
# possible (i.e. building MLIR bundled with LLVM vs standalone), and this
# is just normal CMake package management options.
#
# Fully separating LLVM+LLD+CLANG from MLIR and from IREE enables maximum
# flexibility for the cases where multiple teams are responsible for
# different parts. Note that IREE often has a tight dependency on specific
# MLIR commits, and the bundled submodule often carries patches and fixes
# required for full functionality of all backends.
TD="$(cd $(dirname $0) && pwd)"
REPO_ROOT="$(cd $TD/../.. && pwd)"
LLVM_SOURCE_DIR="${LLVM_SOURCE_DIR:-${REPO_ROOT}/third_party/llvm-project}"
IREE_BYOLLVM_BUILD_DIR="${IREE_BYOLLVM_BUILD_DIR:-${REPO_ROOT}/../iree-byollvm-build}"
IREE_BYOLLVM_INSTALL_DIR="${IREE_BYOLLVM_INSTALL_DIR:-${REPO_ROOT}/../iree-byollvm-install}"
# Canonicalize as absolute paths. These end up in CMake variables such as
# CMAKE_MODULE_PATH, where relative paths are a footgun as CMake gets invoked
# from different directories.
LLVM_SOURCE_DIR="$(realpath -m "${LLVM_SOURCE_DIR}")"
IREE_BYOLLVM_BUILD_DIR="$(realpath -m "${IREE_BYOLLVM_BUILD_DIR}")"
IREE_BYOLLVM_INSTALL_DIR="$(realpath -m "${IREE_BYOLLVM_INSTALL_DIR}")"
echo "Paths canonicalized as:"
echo "LLVM_SOURCE_DIR=${LLVM_SOURCE_DIR}"
echo "IREE_BYOLLVM_BUILD_DIR=${IREE_BYOLLVM_BUILD_DIR}"
echo "IREE_BYOLLVM_INSTALL_DIR=${IREE_BYOLLVM_INSTALL_DIR}"
command="$1"
shift
# Detect commands.
has_ccache=false
if (command -v ccache &> /dev/null); then
has_ccache=true
fi
has_clang=false
if (command -v clang &> /dev/null) && (command -v clang++ &> /dev/null); then
has_clang=true
fi
has_lld=false
if (command -v lld &> /dev/null); then
has_lld=true
fi
python3_command=""
if (command -v python3 &> /dev/null); then
python3_command="python3"
elif (command -v python &> /dev/null); then
python3_command="python"
fi
set -euo pipefail
print_toolchain_config() {
if $has_ccache; then
echo -n "-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache "
fi
if $has_clang; then
echo "-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++"
fi
}
do_build_llvm() {
echo "*********************** BUILDING LLVM *********************************"
main_build_dir="${IREE_BYOLLVM_BUILD_DIR}/llvm"
main_install_dir="${IREE_BYOLLVM_INSTALL_DIR}/llvm"
targets_to_build="${LLVM_TARGETS_TO_BUILD:-X86}"
cmake_options="-DLLVM_TARGETS_TO_BUILD='${targets_to_build}'"
cmake_options="${cmake_options} -DCMAKE_BUILD_TYPE=Release"
cmake_options="${cmake_options} -C $TD/llvm_config.cmake"
cmake_options="${cmake_options} -DCMAKE_INSTALL_PREFIX=${main_install_dir}"
cmake_options="${cmake_options} $(print_toolchain_config)"
if $has_lld; then
cmake_options="${cmake_options} -DLLVM_ENABLE_LLD=ON"
fi
echo "Source Directory: ${LLVM_SOURCE_DIR}"
echo "Build Directory: ${main_build_dir}"
echo "CMake Options: ${cmake_options}"
cmake -GNinja -S "${LLVM_SOURCE_DIR}/llvm" -B "${main_build_dir}" \
$cmake_options
cmake --build "${main_build_dir}" \
--target install-toolchain-distribution \
--target install-development-distribution
}
do_build_mlir() {
echo "*********************** BUILDING MLIR *********************************"
main_install_dir="${IREE_BYOLLVM_INSTALL_DIR}/llvm"
mlir_build_dir="${IREE_BYOLLVM_BUILD_DIR}/mlir"
mlir_install_dir="${IREE_BYOLLVM_INSTALL_DIR}/mlir"
cmake_options="-DLLVM_DIR='${main_install_dir}/lib/cmake/llvm'"
cmake_options="${cmake_options} -DPython3_EXECUTABLE='$(which $python3_command)'"
cmake_options="${cmake_options} -DMLIR_ENABLE_BINDINGS_PYTHON=ON"
cmake_options="${cmake_options} -DCMAKE_INSTALL_PREFIX=${mlir_install_dir}"
cmake_options="${cmake_options} -C $TD/mlir_config.cmake"
cmake_options="${cmake_options} $(print_toolchain_config)"
if $has_lld; then
cmake_options="${cmake_options} -DLLVM_ENABLE_LLD=ON"
fi
echo "Source Directory: ${LLVM_SOURCE_DIR}"
echo "Build Directory: ${mlir_build_dir}"
echo "CMake Options: ${cmake_options}"
cmake -GNinja -S "${LLVM_SOURCE_DIR}/mlir" -B "${mlir_build_dir}" \
$cmake_options
cmake --build "${mlir_build_dir}" \
--target install-mlirdevelopment-distribution
}
print_iree_config() {
llvm_cmake_dir="${IREE_BYOLLVM_INSTALL_DIR}/llvm/lib/cmake/llvm"
lld_cmake_dir="${IREE_BYOLLVM_INSTALL_DIR}/llvm/lib/cmake/lld"
clang_cmake_dir="${IREE_BYOLLVM_INSTALL_DIR}/llvm/lib/cmake/clang"
mlir_cmake_dir="${IREE_BYOLLVM_BUILD_DIR}/mlir/lib/cmake/mlir"
if ! [ -d "$llvm_cmake_dir" ]; then
echo "WARNING: CMake dir does not exist ($llvm_cmake_dir)" >&2
return 1
fi
if ! [ -d "$lld_cmake_dir" ]; then
echo "WARNING: CMake dir does not exist ($lld_cmake_dir)" >&2
return 1
fi
if ! [ -d "$clang_cmake_dir" ]; then
echo "WARNING: CMake dir does not exist ($clang_cmake_dir)" >&2
return 1
fi
if ! [ -d "$mlir_cmake_dir" ]; then
echo "WARNING: CMake dir does not exist ($mlir_cmake_dir)" >&2
return 1
fi
echo "-DLLVM_DIR='$llvm_cmake_dir' -DLLD_DIR='$lld_cmake_dir' -DMLIR_DIR='$mlir_cmake_dir' -DClang_DIR='$clang_cmake_dir' -DIREE_BUILD_BUNDLED_LLVM=OFF"
}
do_build_iree() {
echo "*********************** BUILDING IREE *********************************"
iree_build_dir="${IREE_BYOLLVM_BUILD_DIR}/iree"
iree_install_dir="${IREE_BYOLLVM_INSTALL_DIR}/iree"
cmake_options="$(print_iree_config)"
cmake_options="${cmake_options} -DPython3_EXECUTABLE='$(which $python3_command)'"
cmake_options="${cmake_options} -DIREE_BUILD_PYTHON_BINDINGS=ON"
# Feel free to manually enable or disable any backend, for example
# -DIREE_TARGET_BACKEND_LLVM_CPU=OFF
# Be aware though that several tests in IREE's own suite are currently
# assuming that certain backends are enabled (#14034), so that may cause test
# failures, but that's a test-only issue.
cmake_options="${cmake_options} -DIREE_TARGET_BACKEND_DEFAULTS=OFF"
cmake_options="${cmake_options} -DIREE_TARGET_BACKEND_LLVM_CPU=ON"
cmake_options="${cmake_options} -DIREE_HAL_DRIVER_DEFAULTS=OFF"
cmake_options="${cmake_options} -DIREE_HAL_DRIVER_LOCAL_SYNC=ON"
cmake_options="${cmake_options} -DIREE_HAL_DRIVER_LOCAL_TASK=ON"
cmake_options="${cmake_options} -DCMAKE_BUILD_TYPE=Release"
cmake_options="${cmake_options} $(print_toolchain_config)"
if $has_lld; then
cmake_options="${cmake_options} -DIREE_ENABLE_LLD=ON"
fi
echo "Source Directory: ${REPO_ROOT}"
echo "Build Directory: ${iree_build_dir}"
echo "CMake Options: ${cmake_options}"
cmake -GNinja -S "${REPO_ROOT}" -B "${iree_build_dir}" \
$cmake_options
cmake --build "${iree_build_dir}"
cmake -DCMAKE_INSTALL_PREFIX="${iree_install_dir}" -P "${iree_build_dir}/cmake_install.cmake"
}
do_test_iree() {
echo "*********************** TESTING IREE **********************************"
iree_build_dir="${IREE_BYOLLVM_BUILD_DIR}/iree"
iree_install_dir="${IREE_BYOLLVM_INSTALL_DIR}/iree"
echo "Source Directory: ${REPO_ROOT}"
echo "Build Directory: ${iree_build_dir}"
cmake --build "${iree_build_dir}" --target iree-test-deps
"${REPO_ROOT}/build_tools/cmake/ctest_all.sh" "${iree_build_dir}"
}
case "${command}" in
build_llvm)
do_build_llvm
;;
build_mlir)
do_build_mlir
;;
build_iree)
do_build_iree
;;
test_iree)
do_test_iree
;;
*)
echo "ERROR: Expected command of 'build_llvm', 'clean_install' (got '${command}')"
exit 1
;;
esac