This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
forked from gmh5225/LLVM_Optimized_AVX2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llvm-avx2.patch
327 lines (311 loc) · 15.3 KB
/
llvm-avx2.patch
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
diff --git a/tools/clang/scripts/build.py b/tools/clang/scripts/build.py
index 6d57def43567e..461cb2a1db4bc 100755
--- a/tools/clang/scripts/build.py
+++ b/tools/clang/scripts/build.py
@@ -78,6 +78,8 @@ BUG_REPORT_URL = ('https://crbug.com in the Tools>LLVM component,'
LIBXML2_VERSION = 'libxml2-v2.9.12'
ZSTD_VERSION = 'zstd-1.5.5'
+MIMALLOC_URL = 'https://github.com/microsoft/mimalloc/archive/refs/tags/v2.1.2.tar.gz'
+
win_sdk_dir = None
def GetWinSDKDir():
"""Get the location of the current SDK."""
@@ -167,7 +169,7 @@ def CheckoutGitRepo(name, git_url, commit, dir):
if (RunCommand(['git', 'diff-index', '--quiet', 'HEAD'], fail_hard=False)
and RunCommand(['git', 'fetch'], fail_hard=False)
and RunCommand(['git', 'checkout', commit], fail_hard=False)
- and RunCommand(['git', 'clean', '-f'], fail_hard=False)):
+ and RunCommand(['git', 'clean', '-ffd'], fail_hard=False)):
return
# If we can't use the current repo, delete it.
@@ -377,6 +379,13 @@ def BuildLibXml2():
'-DLIBXML2_WITH_XPATH=OFF',
'-DLIBXML2_WITH_XPTR=OFF',
'-DLIBXML2_WITH_ZLIB=OFF',
+ '-DCMAKE_C_COMPILER=' + os.path.join(PINNED_CLANG_DIR, 'bin', 'clang'),
+ '-DCMAKE_CXX_COMPILER=' + os.path.join(PINNED_CLANG_DIR, 'bin', 'clang++'),
+ '-DCMAKE_C_FLAGS=-O3 -march=x86-64-v3 -w ',
+ '-DCMAKE_CXX_FLAGS=-O3 -march=x86-64-v3 -w ',
+ '-DCMAKE_EXE_LINKER_FLAGS=-w ',
+ '-DCMAKE_SHARED_LINKER_FLAGS=-w ',
+ '-DCMAKE_MODULE_LINKER_FLAGS=-w ',
'..',
],
setenv=True)
@@ -455,6 +464,13 @@ def BuildZStd():
'-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded', # /MT to match LLVM.
'-DZSTD_BUILD_SHARED=OFF',
'../build/cmake',
+ '-DCMAKE_C_COMPILER=' + os.path.join(PINNED_CLANG_DIR, 'bin', 'clang'),
+ '-DCMAKE_CXX_COMPILER=' + os.path.join(PINNED_CLANG_DIR, 'bin', 'clang++'),
+ '-DCMAKE_C_FLAGS=-O3 -march=x86-64-v3 -w ',
+ '-DCMAKE_CXX_FLAGS=-O3 -march=x86-64-v3 -w ',
+ '-DCMAKE_EXE_LINKER_FLAGS=-w ',
+ '-DCMAKE_SHARED_LINKER_FLAGS=-w ',
+ '-DCMAKE_MODULE_LINKER_FLAGS=-w ',
],
setenv=True)
RunCommand(['ninja', 'install'], setenv=True)
@@ -473,6 +489,52 @@ def BuildZStd():
return extra_cmake_flags, extra_cflags
+class LibMimallocDirs:
+ """
+ The set of directories where zstd is located.
+
+ Includes the diractories where the source is unpacked, where it is built,
+ and installed.
+ """
+ def __init__(self):
+ self.unzip_dir = LLVM_BUILD_TOOLS_DIR
+ self.src_dir = os.path.join(self.unzip_dir, 'mimalloc-2.1.2')
+ self.build_dir = os.path.join(self.src_dir, 'build')
+
+def BuildLibMimalloc():
+ """Download and build mimalloc"""
+
+ dirs = LibMimallocDirs()
+ if os.path.exists(dirs.src_dir):
+ RmTree(dirs.src_dir)
+ DownloadAndUnpack(MIMALLOC_URL, dirs.unzip_dir)
+ os.mkdir(dirs.build_dir)
+ os.chdir(dirs.build_dir)
+
+ RunCommand(
+ [
+ 'cmake',
+ '-GNinja',
+ '-DCMAKE_BUILD_TYPE=Release',
+ '-DCMAKE_C_COMPILER=' + os.path.join(PINNED_CLANG_DIR, 'bin', 'clang'),
+ '-DCMAKE_CXX_COMPILER=' + os.path.join(PINNED_CLANG_DIR, 'bin', 'clang++'),
+ '-DCMAKE_C_FLAGS=-O3 -march=x86-64-v3 -pthread -w ',
+ '-DCMAKE_CXX_FLAGS=-O3 -march=x86-64-v3 -pthread -w ',
+ '-DCMAKE_EXE_LINKER_FLAGS=-pthread -w ',
+ '-DCMAKE_SHARED_LINKER_FLAGS=-pthread -w ',
+ '-DCMAKE_MODULE_LINKER_FLAGS=-pthread -w ',
+ '-DMI_OVERRIDE=ON',
+ '-DMI_SHOW_ERRORS=OFF',
+ '..',
+ ],
+ setenv=True)
+ RunCommand(['ninja'], setenv=True)
+
+ MIMALLOC_LIB = os.path.abspath(os.path.join(LLVM_DIR, '..', 'llvm-build-tools/mimalloc-2.1.2')) + '/build/mimalloc.o'
+
+ mimalloc_ldflags = [ '' + MIMALLOC_LIB ]
+
+ return mimalloc_ldflags
def DownloadRPMalloc():
"""Download rpmalloc."""
@@ -692,9 +754,6 @@ def main():
parser.add_argument('--with-android', type=gn_arg, nargs='?', const=True,
help='build the Android ASan runtime (linux only)',
default=sys.platform.startswith('linux'))
- parser.add_argument('--pic',
- action='store_true',
- help='Uses PIC when building LLVM')
parser.add_argument('--with-fuchsia',
type=gn_arg,
nargs='?',
@@ -721,8 +780,14 @@ def main():
global CLANG_REVISION, PACKAGE_VERSION, LLVM_BUILD_DIR
- if (args.pgo or args.thinlto) and not args.bootstrap:
- print('--pgo/--thinlto requires --bootstrap')
+ if not sys.platform.startswith('linux'):
+ print('Customized build script only works with Linux')
+ return 1
+ if not args.bootstrap:
+ print('Customized build script requires --bootstrap')
+ return 1
+ if args.with_goma:
+ print('Customized build script does not support --with-goma')
return 1
if args.with_android and not os.path.exists(ANDROID_NDK_DIR):
print('Android NDK not found at ' + ANDROID_NDK_DIR)
@@ -804,17 +869,12 @@ def main():
# LLVM_ENABLE_LLD).
cc, cxx, lld = None, None, None
- cflags = []
- cxxflags = []
- ldflags = []
-
- targets = 'AArch64;ARM;LoongArch;Mips;PowerPC;RISCV;SystemZ;WebAssembly;X86'
- projects = 'clang;lld;clang-tools-extra'
- if args.bolt:
- projects += ';bolt'
+ cflags = ['-O3 -march=x86-64-v3 -pthread -w']
+ cxxflags = ['-O3 -march=x86-64-v3 -pthread -w']
+ ldflags = ['-fuse-ld=lld -pthread -w']
- pic_default = sys.platform == 'win32'
- pic_mode = 'ON' if args.pic or pic_default else 'OFF'
+ targets = 'AArch64;ARM;X86'
+ projects = 'clang;lld;clang-tools-extra;bolt;polly'
base_cmake_args = [
'-GNinja',
@@ -823,7 +883,7 @@ def main():
'-DLLVM_ENABLE_PROJECTS=' + projects,
'-DLLVM_ENABLE_RUNTIMES=compiler-rt',
'-DLLVM_TARGETS_TO_BUILD=' + targets,
- f'-DLLVM_ENABLE_PIC={pic_mode}',
+ '-DLLVM_ENABLE_PIC=ON',
'-DLLVM_ENABLE_TERMINFO=OFF',
'-DLLVM_ENABLE_Z3_SOLVER=OFF',
'-DCLANG_PLUGIN_SUPPORT=OFF',
@@ -923,6 +983,10 @@ def main():
base_cmake_args.append('-DLLVM_WINSYSROOT="%s"' %
os.path.dirname(os.path.dirname(GetWinSDKDir())))
+ #Build mimalloc
+ libmimalloc_ldflags = BuildLibMimalloc()
+ ldflags += libmimalloc_ldflags
+
# Statically link libxml2 to make lld-link not require mt.exe on Windows,
# and to make sure lld-link output on other platforms is identical to
# lld-link on Windows (for cross-builds).
@@ -1021,8 +1085,8 @@ def main():
instrument_args = base_cmake_args + [
'-DLLVM_ENABLE_PROJECTS=clang',
- '-DCMAKE_C_FLAGS=' + ' '.join(cflags),
- '-DCMAKE_CXX_FLAGS=' + ' '.join(cxxflags),
+ '-DCMAKE_C_FLAGS=-mllvm -vp-counters-per-site=3 ' + ' '.join(cflags),
+ '-DCMAKE_CXX_FLAGS=-mllvm -vp-counters-per-site=3 ' + ' '.join(cxxflags),
'-DCMAKE_EXE_LINKER_FLAGS=' + ' '.join(ldflags),
'-DCMAKE_SHARED_LINKER_FLAGS=' + ' '.join(ldflags),
'-DCMAKE_MODULE_LINKER_FLAGS=' + ' '.join(ldflags),
@@ -1065,8 +1129,10 @@ def main():
with open(training_source, 'wb') as f:
DownloadUrl(CDS_URL + '/' + training_source, f)
train_cmd = [os.path.join(LLVM_INSTRUMENTED_DIR, 'bin', 'clang++'),
- '-target', 'x86_64-unknown-unknown', '-O2', '-g', '-std=c++14',
- '-fno-exceptions', '-fno-rtti', '-w', '-c', training_source]
+ '-target', 'x86_64-unknown-unknown', '-O3', '-g', '-std=c++20',
+ '-march=x86-64-v3', '-ffp-contract=fast', '-fmerge-all-constants',
+ '-fdata-sections', '-ffunction-sections', '-fno-exceptions',
+ '-fno-rtti', '-w', '-c', training_source]
if sys.platform == 'darwin':
train_cmd.extend(['-isysroot', isysroot])
RunCommand(train_cmd, setenv=True)
@@ -1116,11 +1182,6 @@ def main():
if lld is not None: base_cmake_args.append('-DCMAKE_LINKER=' + lld)
final_install_dir = args.install_dir if args.install_dir else LLVM_BUILD_DIR
cmake_args = base_cmake_args + [
- '-DCMAKE_C_FLAGS=' + ' '.join(cflags),
- '-DCMAKE_CXX_FLAGS=' + ' '.join(cxxflags),
- '-DCMAKE_EXE_LINKER_FLAGS=' + ' '.join(ldflags),
- '-DCMAKE_SHARED_LINKER_FLAGS=' + ' '.join(ldflags),
- '-DCMAKE_MODULE_LINKER_FLAGS=' + ' '.join(ldflags),
'-DCMAKE_INSTALL_PREFIX=' + final_install_dir,
]
if not args.no_tools:
@@ -1130,10 +1191,36 @@ def main():
os.path.join(CHROMIUM_DIR, 'tools', 'clang'),
'-DCHROMIUM_TOOLS=%s' % ';'.join(chrome_tools)
])
- if args.pgo:
+ if args.pgo and not args.thinlto:
+ cmake_args.append('-DLLVM_PROFDATA_FILE=-mllvm -enable-ext-tsp-block-placement ' + LLVM_PROFDATA_FILE)
+ cmake_args.append('-DCMAKE_C_FLAGS=-mllvm -enable-ext-tsp-block-placement ' + ' '.join(cflags))
+ cmake_args.append('-DCMAKE_CXX_FLAGS=-mllvm -enable-ext-tsp-block-placement ' + ' '.join(cxxflags))
+ cmake_args.append('-DCMAKE_EXE_LINKER_FLAGS=-Wl,-z,keep-text-section-prefix ' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_SHARED_LINKER_FLAGS=-Wl,-z,keep-text-section-prefix ' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_MODULE_LINKER_FLAGS=-Wl,-z,keep-text-section-prefix ' + ' '.join(ldflags))
+ if args.thinlto and not args.pgo:
+ cmake_args.append('-DLLVM_ENABLE_LTO=Thin')
+ cmake_args.append('-DLLVM_PARALLEL_LINK_JOBS=16')
+ cmake_args.append('-DCMAKE_C_FLAGS=-flto=thin -fwhole-program-vtables -fsplit-lto-unit ' + ' '.join(cflags))
+ cmake_args.append('-DCMAKE_CXX_FLAGS=-flto=thin -fwhole-program-vtables -fsplit-lto-unit ' + ' '.join(cxxflags))
+ cmake_args.append('-DCMAKE_EXE_LINKER_FLAGS=-flto=thin -fwhole-program-vtables -Wl,--lto-O3 -Wl,--lto-CGO3 -Wl,-mllvm,-import-instr-limit=30 ' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_SHARED_LINKER_FLAGS=-flto=thin -fwhole-program-vtables -Wl,--lto-O3 -Wl,--lto-CGO3 -Wl,-mllvm,-import-instr-limit=30 ' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_MODULE_LINKER_FLAGS=-flto=thin -fwhole-program-vtables -Wl,--lto-O3 -Wl,--lto-CGO3 -Wl,-mllvm,-import-instr-limit=30 ' + ' '.join(ldflags))
+ if args.thinlto and args.pgo:
cmake_args.append('-DLLVM_PROFDATA_FILE=' + LLVM_PROFDATA_FILE)
- if args.thinlto:
cmake_args.append('-DLLVM_ENABLE_LTO=Thin')
+ cmake_args.append('-DLLVM_PARALLEL_LINK_JOBS=16')
+ cmake_args.append('-DCMAKE_C_FLAGS=-flto=thin -fwhole-program-vtables -fsplit-lto-unit ' + ' '.join(cflags))
+ cmake_args.append('-DCMAKE_CXX_FLAGS=-flto=thin -fwhole-program-vtables -fsplit-lto-unit ' + ' '.join(cxxflags))
+ cmake_args.append('-DCMAKE_EXE_LINKER_FLAGS=-flto=thin -fwhole-program-vtables -Wl,--lto-O3 -Wl,--lto-CGO3 -Wl,-mllvm,-import-instr-limit=30 -Wl,-mllvm,-enable-ext-tsp-block-placement -Wl,-z,keep-text-section-prefix ' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_SHARED_LINKER_FLAGS=-flto=thin -fwhole-program-vtables -Wl,--lto-O3 -Wl,--lto-CGO3 -Wl,-mllvm,-import-instr-limit=30 -Wl,-mllvm,-enable-ext-tsp-block-placement -Wl,-z,keep-text-section-prefix ' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_MODULE_LINKER_FLAGS=-flto=thin -fwhole-program-vtables -Wl,--lto-O3 -Wl,--lto-CGO3 -Wl,-mllvm,-import-instr-limit=30 -Wl,-mllvm,-enable-ext-tsp-block-placement -Wl,-z,keep-text-section-prefix ' + ' '.join(ldflags))
+ else:
+ cmake_args.append('-DCMAKE_C_FLAGS=' + ' '.join(cflags))
+ cmake_args.append('-DCMAKE_CXX_FLAGS=' + ' '.join(cxxflags))
+ cmake_args.append('-DCMAKE_EXE_LINKER_FLAGS=' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_SHARED_LINKER_FLAGS=' + ' '.join(ldflags))
+ cmake_args.append('-DCMAKE_MODULE_LINKER_FLAGS=' + ' '.join(ldflags))
if sys.platform == 'win32':
cmake_args.append('-DLLVM_ENABLE_ZLIB=FORCE_ON')
@@ -1180,6 +1267,11 @@ def main():
'CMAKE_SYSROOT=%s' % sysroot_i386,
# TODO(https://crbug.com/1374690): pass proper flags to i386 tests so they compile correctly
'LLVM_INCLUDE_TESTS=OFF',
+ 'CMAKE_C_FLAGS=-O3 -march=x86-64-v3 -w ',
+ 'CMAKE_CXX_FLAGS=-O3 -march=x86-64-v3 -w ',
+ 'CMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld -w ',
+ 'CMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld -w ',
+ 'CMAKE_MODULE_LINKER_FLAGS=-fuse-ld=lld -w ',
],
"profile":
True,
@@ -1189,6 +1281,11 @@ def main():
runtimes_triples_args['x86_64-unknown-linux-gnu'] = {
"args": [
'CMAKE_SYSROOT=%s' % sysroot_amd64,
+ 'CMAKE_C_FLAGS=-O3 -march=x86-64-v3 -w ',
+ 'CMAKE_CXX_FLAGS=-O3 -march=x86-64-v3 -w ',
+ 'CMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld -w ',
+ 'CMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld -w ',
+ 'CMAKE_MODULE_LINKER_FLAGS=-fuse-ld=lld -w ',
],
"profile": True,
"sanitizers": True,
@@ -1308,7 +1405,6 @@ def main():
'CMAKE_BUILD_TYPE=RelWithDebInfo',
'CMAKE_C_FLAGS=' + ' '.join(android_cflags),
'CMAKE_CXX_FLAGS=' + ' '.join(android_cflags),
- 'CMAKE_ASM_FLAGS=' + ' '.join(android_cflags),
'COMPILER_RT_USE_BUILTINS_LIBRARY=ON',
'SANITIZER_CXX_ABI=libcxxabi',
'CMAKE_SHARED_LINKER_FLAGS=-Wl,-u__cxa_demangle',
@@ -1438,8 +1534,8 @@ def main():
# Instrument.
RunCommand([
'bin/llvm-bolt', 'bin/clang', '-o', 'bin/clang-bolt.inst',
- '-instrument', '--instrumentation-file-append-pid',
- '--instrumentation-file=' +
+ '-lite=false','-instrument', '-instrumentation-file-append-pid',
+ '-instrumentation-file=' +
os.path.join(bolt_profiles_dir, 'prof.fdata')
])
RunCommand([
@@ -1454,8 +1550,8 @@ def main():
bolt_train_cmake_args = base_cmake_args + [
'-DLLVM_TARGETS_TO_BUILD=X86',
'-DLLVM_ENABLE_PROJECTS=clang',
- '-DCMAKE_C_FLAGS=' + ' '.join(cflags),
- '-DCMAKE_CXX_FLAGS=' + ' '.join(cxxflags),
+ '-DCMAKE_C_FLAGS=-ffp-contract=fast -fmerge-all-constants ' + ' '.join(cflags),
+ '-DCMAKE_CXX_FLAGS=-ffp-contract=fast -fmerge-all-constants ' + ' '.join(cxxflags),
'-DCMAKE_EXE_LINKER_FLAGS=' + ' '.join(ldflags),
'-DCMAKE_SHARED_LINKER_FLAGS=' + ' '.join(ldflags),
'-DCMAKE_MODULE_LINKER_FLAGS=' + ' '.join(ldflags),
@@ -1485,7 +1581,12 @@ def main():
'bin/llvm-bolt', 'bin/clang', '-o', 'bin/clang-bolt.opt', '-data',
'merged.fdata', '-reorder-blocks=ext-tsp', '-reorder-functions=hfsort+',
'-split-functions', '-split-all-cold', '-split-eh', '-dyno-stats',
- '-icf=1', '-use-gnu-stack', '-use-old-text'
+ '-icf', '-use-gnu-stack', '-peepholes=all', '-elim-link-veneers',
+ '-group-stubs', '-align-blocks', '-sctc-mode=heuristic',
+ '-jump-tables=aggressive', '-simplify-rodata-loads', '-align-macro-fusion=all',
+ '-tail-duplication=cache', '-indirect-call-promotion=all', '-icp-eliminate-loads',
+ '-hot-data', '-x86-strip-redundant-address-size', '-reorder-functions-use-hot-size',
+ '-reorder-data-algo=funcs', '-lite=false',
])
# Overwrite clang, preserving its timestamp so ninja doesn't rebuild it.
@@ -1527,6 +1628,10 @@ def main():
WriteStampFile(PACKAGE_VERSION, STAMP_FILE)
WriteStampFile(PACKAGE_VERSION, FORCE_HEAD_REVISION_FILE)
+
+ RunCommand(['ln', '--symbolic', '--relative', '-f', 'bin/clang', 'bin/clang++'])
+ RunCommand(['ln', '--symbolic', '--relative', '-f', 'bin/clang', 'bin/clang-cl'])
+
print('Clang build was successful.')
return 0