forked from verificarlo/verificarlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verificarlo.in.in
412 lines (343 loc) · 14.3 KB
/
verificarlo.in.in
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
# \
# #\
# This file is part of the Verificarlo project, #\
# under the Apache License v2.0 with LLVM Exceptions. #\
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception. #\
# See https://llvm.org/LICENSE.txt for license information. #\
# #\
# #\
# Copyright (c) 2015 #\
# Universite de Versailles St-Quentin-en-Yvelines #\
# CMLA, Ecole Normale Superieure de Cachan #\
# #\
# Copyright (c) 2018 #\
# Universite de Versailles St-Quentin-en-Yvelines #\
# #\
# Copyright (c) 2019-2024 #\
# Verificarlo Contributors #\
# #\
#############################################################################
from __future__ import print_function
import argparse
import os
import sys
import subprocess
import tempfile
PACKAGE_STRING = "@PACKAGE_STRING@"
LIBDIR = "%LIBDIR%"
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
libinterflop_stdlib = "@INTERFLOP_PREFIX@"
libinterflop_stdlib_include = "@INTERFLOP_INCLUDEDIR@"
libinterflop_stdlib_lib = "@INTERFLOP_LIBDIR@"
libvfcinstrument = LIBDIR + "/libvfcinstrument.so"
libvfcfuncinstrument = LIBDIR + "/libvfcfuncinstrument.so"
mcalib_options = "-rpath {0} -L {0}".format(LIBDIR)
mcalib_includes = PROJECT_ROOT + "/../include/"
vfcwrapper = mcalib_includes + "vfcwrapper.c"
llvm_bindir = "@LLVM_BINDIR@"
llvm_version = "@LLVM_VERSION_MAJOR@"
clang = "@CLANG_PATH@"
clangxx = "@CLANGXX_PATH@"
llvm_link = "@LLVM_LINK_PATH@"
flang = "@FLANG_PATH@"
opt = llvm_bindir + "/opt"
FORTRAN_EXTENSIONS = [".f", ".f90", ".f77"]
C_EXTENSIONS = [".c"]
CXX_EXTENSIONS = [".cc", ".cp", ".cpp", ".cxx", "c++"]
ASSEMBLY_EXTENSIONS = [".s"]
LLVM_BITCODE_EXTENSIONS = [".bc", ".ll"]
COMPILE_EXTRA_FLAGS = ""
linkers = {"clang": clang, "flang": flang, "clang++": clangxx}
default_linker = "clang"
temp_files_set = set()
march_flag = "@MARCH_FLAG@"
class NoPrefixParser(argparse.ArgumentParser):
# ignore prefix autocompletion of options
def _get_option_tuples(self, option_string):
return []
def close_tmp_files():
for tmp in temp_files_set:
try:
tmp.close()
except FileNotFoundError:
continue
def fail(msg):
close_tmp_files()
print(sys.argv[0] + ": " + msg, file=sys.stderr)
sys.exit(1)
def is_fortran(name):
return os.path.splitext(name)[1].lower() in FORTRAN_EXTENSIONS
def is_c(name):
return os.path.splitext(name)[1].lower() in C_EXTENSIONS
def is_cpp(name):
return os.path.splitext(name)[1].lower() in CXX_EXTENSIONS
def is_assembly(name):
return os.path.splitext(name)[1].lower() in ASSEMBLY_EXTENSIONS
def is_llvm_bitcode(name):
return os.path.splitext(name)[1].lower() in LLVM_BITCODE_EXTENSIONS
def shell_escape(argument):
# prevents argument expansion in shell call
return "'" + argument + "'"
def parse_extra_args(args):
sources = []
options = []
libraries = []
for a in args:
if is_fortran(a):
if not flang:
fail(
"fortran not supported. "
+ "--without-flang was used during configuration."
)
sources.append(a)
elif is_c(a):
sources.append(a)
elif is_cpp(a):
sources.append(a)
elif is_assembly(a):
sources.append(a)
elif is_llvm_bitcode(a):
sources.append(a)
elif a.startswith("-l"):
libraries.append(a)
else:
options.append(shell_escape(a))
return sources, " ".join(options), " ".join(libraries)
def shell(cmd):
try:
if args.show_cmd:
print(cmd)
subprocess.check_call(cmd, shell=True)
except subprocess.CalledProcessError:
fail("command failed:\n" + cmd)
def compile_vfcwrapper(source, output, args, emit_llvm=False):
extra_args = "-static " if args.static else "-fPIC "
extra_args += "-DINST_FCMP " if args.inst_fcmp else ""
extra_args += "-DDDEBUG " if args.ddebug else ""
extra_args += "-DINST_FUNC " if args.inst_func else ""
extra_args += "-DINST_FMA " if args.inst_fma else ""
extra_args += "-DINST_CAST " if args.inst_cast else ""
internal_options = (
" -S -emit-llvm " if emit_llvm else ""
) + f" -c -Wno-varargs -I {mcalib_includes} "
cmd = (
f"{clang} -O3 {march_flag} -g {internal_options} {extra_args} "
f"{source} -I{libinterflop_stdlib_include} -o {output} "
)
shell(cmd)
def linker_mode(sources, options, libraries, output, args):
vfcwrapper_o = get_tmp_name(".vfcwrapper.", ".o")
compile_vfcwrapper(vfcwrapper, vfcwrapper_o, args)
with tempfile.NamedTemporaryFile(mode="w+") as f:
sources = " ".join([os.path.splitext(s)[0] + ".o" for s in sources])
interflop_libs = " ".join(
["-linterflop_stdlib", "-linterflop_hashmap", "-linterflop_logger"]
)
interflop_stdlib_flags = f" -L{libinterflop_stdlib_lib} {interflop_libs} "
# Do not make Position Indenpendant Executable (PIE)
# Force no-pie when using ddebug and inst_func; since we use addresses to
# indentify the instrumented instructions, we require that the .text segment
# is loaded a a statically known address.
if args.ddebug or args.inst_func:
options += " -no-pie "
if args.static:
cmd = (
f"{output} {sources} {options} {libraries} {vfcwrapper_o} "
f" -static -lgmp -lm -ldl {interflop_stdlib_flags} "
)
else:
cmd = (
f"{output} {sources} {options} {libraries} {vfcwrapper_o} "
f" {mcalib_options} -ldl {interflop_stdlib_flags} "
)
f.write(cmd)
f.flush()
linker = linkers[args.linker]
if args.show_cmd:
print(f"{linker} {cmd}")
shell(f"{linker} @{f.name}")
# Do not instrument
def compile_only(sources, options, output, args):
compiler = linkers[args.linker]
sources = " ".join(sources)
shell(f"{compiler} {sources} {options} {output}")
def get_tmp_name(prefix, suffix):
"""Return a temporary file name with the given prefix and suffix. (do not create the file)"""
return tempfile.mktemp(suffix=suffix, prefix=prefix, dir="")
def get_tmp_filename(prefix, suffix, args):
filename_ext = os.path.basename(prefix)
basename = os.path.splitext(filename_ext)[0]
abs_prefix = os.getcwd() + os.sep + basename + "."
tmp = tempfile.NamedTemporaryFile(
mode="w+b", prefix=abs_prefix, suffix=suffix, delete=not args.save_temps
)
temp_files_set.add(tmp)
return tmp
def compiler_mode(sources, options, output, args):
extra_args = "-static " if args.static else "-fPIC "
vfcwrapper_ir = get_tmp_filename(".vfcwrapper", ".ll", args)
compile_vfcwrapper(vfcwrapper, vfcwrapper_ir.name, args, emit_llvm=True)
for source in sources:
basename = os.path.splitext(source)[0]
ir = get_tmp_filename(basename, ".1.ll", args)
ins = get_tmp_filename(basename, ".2.ll", args)
compiler = linkers[args.linker]
include = f" -I {mcalib_includes} "
debug = "-g" if args.inst_func or args.ddebug else ""
if is_assembly(source):
if not output:
basename_output = "-o " + basename + ".o"
else:
basename_output = output
compile_only([source], " -c " + options, basename_output, args)
continue
# Compile to ir (fortran uses flang, c uses clang)
shell(
f"{compiler} -c -S {debug} {source} {include} -emit-llvm {COMPILE_EXTRA_FLAGS} {options} -o {ir.name}"
)
selectfunction = ""
if args.function:
selectfunction = "-vfclibinst-function " + args.function
else:
if args.include_file:
selectfunction = "-vfclibinst-include-file " + args.include_file
if args.exclude_file:
selectfunction += " -vfclibinst-exclude-file " + args.exclude_file
extra_args = ""
# Activate verbose mode
if args.verbose:
extra_args += "-vfclibinst-verbose "
# Activate fcmp instrumentation
if args.inst_fcmp:
extra_args += "-vfclibinst-inst-fcmp "
# Activate fma instrumentation
if args.inst_fma:
extra_args += "-vfclibinst-inst-fma"
# Activate cast instrumentation
if args.inst_cast:
extra_args += "-vfclibinst-inst-cast"
if args.inst_func:
# Apply function's instrumentation pass
if 13 <= int(llvm_version) < 17:
shell(
f"{opt} -S -enable-new-pm=0 -load {libvfcfuncinstrument} -vfclibfunc {ir.name} -o {ins.name}"
)
else:
shell(
f"{opt} -S -load {libvfcfuncinstrument} -vfclibfunc {ir.name} -o {ins.name}"
)
ir = ins
ins = get_tmp_filename(basename, ".3.ll", args)
# Apply MCA instrumentation pass
# For LLVM >= 13 we fallback to the legacy pass manager
if 13 <= int(llvm_version) < 17:
shell(
(
f"{opt} -S -enable-new-pm=0 -load {libvfcinstrument} "
f" -vfclibinst-vfcwrapper-file {vfcwrapper_ir.name} "
f" -vfclibinst {extra_args} {selectfunction} "
f" {ir.name} -o {ins.name}"
)
)
else:
shell(
(
f"{opt} -S -load {libvfcinstrument} "
f" -vfclibinst-vfcwrapper-file {vfcwrapper_ir.name} "
f" -vfclibinst {extra_args} {selectfunction} "
f" {ir.name} -o {ins.name}"
)
)
if not output:
# Use a temporary file to store the object
# to avoid race conditions
cmd_output = " -o " + basename + ".o"
else:
cmd_output = output
if not args.emit_llvm:
# Produce object file
shell(f"{compiler} -c {cmd_output} {ins.name} {options}")
else:
# Produce a bc file with the wrapper bc linked in.
shell(f"{llvm_link} {ins.name} {vfcwrapper_ir.name} {cmd_output}")
if __name__ == "__main__":
parser = NoPrefixParser(
description="Compiles a program replacing floating point operation with calls to the mcalib (Montecarlo Arithmetic)."
)
parser.add_argument("-E", action="store_true", help="only run the preprocessor")
parser.add_argument(
"-S", action="store_true", help="only run preprocess and compilation steps"
)
parser.add_argument(
"-c",
action="store_true",
help="only run preprocess, compile, and assemble steps",
)
parser.add_argument("-o", metavar="file", help="write output to <file>")
parser.add_argument("--ddebug", action="store_true", help="enable delta-debug mode")
parser.add_argument(
"--function", metavar="function", help="only instrument <function>"
)
parser.add_argument(
"--include-file", metavar="file", help="include-list module and functions"
)
parser.add_argument(
"--exclude-file", metavar="file", help="exclude-list module and functions"
)
parser.add_argument(
"-static", "--static", action="store_true", help="produce a static binary"
)
parser.add_argument("--verbose", action="store_true", help="verbose output")
parser.add_argument(
"--inst-fcmp", action="store_true", help="instrument floating point comparisons"
)
parser.add_argument(
"--inst-fma", action="store_true", help="instrument floating point fma"
)
parser.add_argument(
"--inst-cast", action="store_true", help="instrument floating point castings"
)
parser.add_argument("--inst-func", action="store_true", help="instrument functions")
parser.add_argument(
"--show-cmd", action="store_true", help="show internal commands"
)
parser.add_argument(
"--save-temps", action="store_true", help="save intermediate files"
)
parser.add_argument("--version", action="version", version=PACKAGE_STRING)
parser.add_argument(
"--linker",
choices=linkers.keys(),
default=default_linker,
help="linker to use, {dl} by default".format(dl=default_linker),
)
parser.add_argument(
"--emit-llvm",
action="store_true",
help="emit an LLVM bitcode with the instrumentation built-in",
)
args, other = parser.parse_known_args()
sources, llvm_options, libraries = parse_extra_args(other)
# check input files
if (args.E or args.S or args.c) and len(sources) > 1 and args.o:
fail("cannot specify -o when generating multiple output files")
# check mutually excluding args
if args.function and (args.include_file or args.exclude_file):
fail("Cannot use --function and --include-file/--exclude-file together")
output = "-o " + args.o if args.o else ""
if args.E:
llvm_options += " -E "
compile_only(sources, llvm_options, output, args)
elif args.S:
llvm_options += " -S "
compile_only(sources, llvm_options, output, args)
elif args.c or args.emit_llvm:
if len(sources) == 0:
fail("no input files")
compiler_mode(sources, llvm_options, output, args)
else:
if len(sources) == 0 and len(llvm_options) == 0:
fail("no input files")
compiler_mode(sources, llvm_options, "", args)
linker_mode(sources, llvm_options, libraries, output, args)