forked from verificarlo/verificarlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verificarlo.in.in
323 lines (260 loc) · 11.9 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
#!/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-2021 #\
# 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__))
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@'
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']
linkers = {'clang': clang, 'flang': flang, 'clang++': clangxx}
default_linker = 'clang'
temp_files_set = set()
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 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 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 ""
internal_options = (" -S -emit-llvm " if emit_llvm else "") + \
f" -c -Wno-varargs -I {mcalib_includes} "
shell(f'{clang} -O3 -march=native {internal_options} {extra_args} {source} -o {output} ')
def linker_mode(sources, options, libraries, output, args):
vfcwrapper_o = ".vfcwrapper.o"
compile_vfcwrapper(vfcwrapper, vfcwrapper_o, args)
f = tempfile.NamedTemporaryFile(mode='w+')
sources = ' '.join([os.path.splitext(s)[0]+'.o' for s in sources])
if args.static:
cmd = f'{output} {sources} {options} {libraries} {vfcwrapper_o} -static -lgmp -lm -ldl'
else:
cmd = f'{output} {sources} {options} {libraries} {vfcwrapper_o} {mcalib_options} -ldl'
f.write(cmd)
f.flush()
linker = linkers[args.linker]
if args.show_cmd:
print('{linker} {cmd}'.format(linker=linker, cmd=cmd))
shell('{linker} @{temp}'.format(linker=linker, temp=f.name))
f.close()
# 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_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 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 {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 "
if args.inst_func:
# Apply function's instrumentation pass
if int(llvm_version) >= 13:
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 int(llvm_version) >= 13:
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:
basename_output = '-o ' + basename + '.o'
else:
basename_output = output
# Produce object file
shell(f'{compiler} -c {basename_output} {ins.name} {options}')
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-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))
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:
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)