forked from google/souper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsclang.in
executable file
·294 lines (229 loc) · 8.59 KB
/
sclang.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
#!/usr/bin/env perl
# Copyright 2014 The Souper Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use warnings;
use strict;
use File::Temp;
sub compiling() {
foreach my $arg (@ARGV) {
return 1
if ($arg =~ /\.c$|\.cpp$|\.CC$|\.c\+\+$|\.cc$|\.cxx$|\.C$|\.c\+$/);
}
return 0;
}
sub linkp() {
foreach my $arg (@ARGV) {
return 0 if ($arg eq "-S" || $arg eq "-c" || $arg eq "-shared");
}
return 1;
}
if ($0 =~ /clang$/) {
unshift @ARGV, "@LLVM_BINDIR@/clang";
} elsif ($0 =~ /clang\+\+$/) {
unshift @ARGV, "@LLVM_BINDIR@/clang++";
} else {
die "Didn't expect sclang to be invoked as '$0'";
}
foreach my $arg (@ARGV) {
if ($arg eq "-help" || $arg eq "--help") {
print <<EOF;
sclang / sclang++ emulate clang / clang++ except that they also invoke
Souper.
To avoid the need to mess with build systems, communication with these
programs is via environment variables. Unless noted, the value of the
environment variable is irrelevant: sclang only checks if it is set.
SOUPER_DEBUG -- Print debugging info.
SOUPER_DYNAMIC_PROFILE -- Instrument the compiled program such that,
when run, it will communicate with a running Redis instance in order
to report how many times each optimized code site executes.
SOUPER_DYNAMIC_PROFILE_ALL -- Like SOUPER_DYNAMIC_PROFILE but
instruments each site that is potentially optimizable by Souper, as
opposed to sites that actually get optimized. Should be used in
combination with SOUPER_NO_INFER.
SOUPER_EXPLOIT_BLOCKPCS -- Optimize using blockpcs.
SOUPER_FIRST_OPT / SOUPER_LAST_OPT -- Assign a number to each
optimization found in the current compilation unit and only perform
those that fall within the range [SOUPER_FIRST_OPT
.. SOUPER_LAST_OPT], inclusive. Used for debugging Souper
SOUPER_USE_CEGIS -- Try to synthesize instructions using a CEGIS-based
synthesizer.
SOUPER_ENUMERATIVE_SYNTHESIS_MAX_INSTS -- Number of instructions for
EnumerativeSynthesis synthesizer.
SOUPER_DATAFLOW_PRUNING -- Enable dataflow pruning. This only makes sense
when EnumerativeSynthesis is enabled.
SOUPER_REDIS_PORT -- Mention the port to use the redis-server on.
SOUPER_CACHE_UNIX -- Connect to Redis using a UNIX domain socket.
SOUPER_NO_EXTERNAL_CACHE -- Don't ask the running Redis instance for
cached inferences.
SOUPER_NO_HARVEST_DATAFLOW_FACTS -- Don't query LLVM's bit-level dataflow
analyses when harvesting.
SOUPER_HARVEST_USES -- This option enables the harvest of LLVM uses.
SOUPER_NO_INFER -- Don't run inference at all; used for populating the
cache.
SOUPER_NO_SOUPER -- Don't run Souper at all.
SOUPER_SAVE_BITCODE -- The value of this variable is the name of a
directory where a copy of the bitcode for the compiled file will be
stored.
SOUPER_SKIP_FILES -- Do not apply Souper (as if SOUPER_NO_SOUPER were
set) to any of the comma-separated list of files found in the value of
this variable.
SOUPER_STATIC_PROFILE -- During compilation, communicate with a running
Redis instance in order to record the number of times each optimization
was performed.
SOUPER_USE_ALIVE -- Send queries to Alive2
SOUPER_NO_DOUBLE_CHECK -- Don't use Alive2 to verify optimizations
SOUPER_DISABLE_LLVM_PEEPHOLES -- Disable the invocation of llvm peephole
transformations in compilation.
SOUPER_STATS -- Ask LLVM to print statistics.
SOUPER_TIME_REPORT -- Ask LLVM to print a time report for passes
SOUPER_VERIFY -- Run the module verifier before and after Souper runs
EOF
exit(-1);
}
}
my %whitelist = ();
# N.B. have to get environment variables using this function which has
# the side effect of adding the env var to our whitelist
sub getenv($) {
(my $e) = @_;
die "oops '$e'" unless $e =~ /^SOUPER_/;
$whitelist{$e} = 1;
return undef unless exists($ENV{$e});
return $ENV{$e};
}
my $bitcodedir = getenv("SOUPER_SAVE_BITCODE");
if (defined $bitcodedir && -d $bitcodedir && fork() == 0) {
for (my $i=0; $i<scalar(@ARGV); $i++) {
splice @ARGV, $i, 2 if ($ARGV[$i] eq "-o");
}
my $tmp = File::Temp->new(UNLINK => 1, DIR => $bitcodedir)->filename;
push @ARGV, "-c", "-emit-llvm";
push @ARGV, "-o", "${tmp}.bc";
my $ofn = "${tmp}.cmd";
open OUTF, ">$ofn" or die;
foreach my $a (@ARGV) {
print OUTF "$a ";
}
print OUTF "\n";
close OUTF;
open STDOUT, '>/dev/null';
open STDERR, '>/dev/null';
exec @ARGV;
die "bailing, exec failed";
}
my $souper = 1;
# this environment variable is a comma-separated list of source files that
# souper should avoid processing, for example because they trigger known bugs
my $skip_files = getenv("SOUPER_SKIP_FILES");
if (defined $skip_files) {
my %skips;
foreach my $f (split(',', $skip_files)) {
$skips{$f} = 1;
}
foreach my $a (@ARGV) {
$souper = 0 if ($skips{$a});
}
}
$souper = 0 if getenv("SOUPER_NO_SOUPER");
$souper = 0 unless compiling();
if ($souper) {
if (getenv("SOUPER_DYNAMIC_PROFILE_ALL")) {
die(); ## not supported right now
push @ARGV, ("-Xclang", "@SOUPER_PASS_PROFILE_ALL@");
} else {
# the first one is necessary to make plugin command line args work
push @ARGV, ("-Xclang", "-load", "-Xclang", "@SOUPER_PASS@");
push @ARGV, ("-fpass-plugin=@SOUPER_PASS@");
}
push @ARGV, ("-fplugin-arg-souper-solver-timeout=15");
if (!getenv("SOUPER_NO_DOUBLE_CHECK")) {
push @ARGV, ("-mllvm", "-souper-double-check");
}
if (getenv("SOUPER_DEBUG")) {
push @ARGV, ("-mllvm", "-souper-debug-level=".getenv("SOUPER_DEBUG"));
}
if (getenv("SOUPER_EXPLOIT_BLOCKPCS")) {
push @ARGV, ("-mllvm", "-souper-exploit-blockpcs");
}
if (!getenv("SOUPER_NO_EXTERNAL_CACHE")) {
push @ARGV, ("-mllvm", "-souper-external-cache");
}
if (getenv("SOUPER_CACHE_UNIX")) {
push @ARGV, ("-mllvm", "-souper-external-cache-unix");
}
if (getenv("SOUPER_NO_INFER")) {
push @ARGV, ("-mllvm", "-souper-no-infer");
}
if (getenv("SOUPER_FIRST_OPT")) {
push @ARGV, ("-mllvm", "-souper-first-opt=".$ENV{"SOUPER_FIRST_OPT"});
}
if (getenv("SOUPER_LAST_OPT")) {
push @ARGV, ("-mllvm", "-souper-last-opt=".$ENV{"SOUPER_LAST_OPT"});
}
if (getenv("SOUPER_STATIC_PROFILE")) {
push @ARGV, ("-mllvm", "-souper-static-profile");
}
if (getenv("SOUPER_DYNAMIC_PROFILE") ||
getenv("SOUPER_DYNAMIC_PROFILE_ALL")) {
push @ARGV, ("-g", "-mllvm", "-souper-dynamic-profile");
}
if (getenv("SOUPER_USE_ALIVE")) {
push @ARGV, ("-mllvm", "-souper-use-alive");
}
if (getenv("SOUPER_USE_CEGIS")) {
push @ARGV, ("-mllvm", "-souper-use-cegis");
}
if (getenv("SOUPER_ENUMERATIVE_SYNTHESIS_MAX_INSTS")) {
push @ARGV, ("-mllvm", "-souper-enumerative-synthesis-max-instructions=".$ENV{"SOUPER_ENUMERATIVE_SYNTHESIS_MAX_INSTS"});
}
if (getenv("SOUPER_DATAFLOW_PRUNING")) {
push @ARGV, ("-mllvm", "-souper-dataflow-pruning");
}
if (getenv("SOUPER_REDIS_PORT")) {
push @ARGV, ("-mllvm", "-souper-redis-port=".$ENV{"SOUPER_REDIS_PORT"});
}
if (getenv("SOUPER_STATS")) {
push @ARGV, ("-mllvm", "-stats");
}
if (getenv("SOUPER_TIME_REPORT")) {
push @ARGV, ("-ftime-report");
}
if (getenv("SOUPER_VERIFY")) {
push @ARGV, ("-mllvm", "-souper-verify");
}
if (getenv("SOUPER_NO_HARVEST_DATAFLOW_FACTS")) {
push @ARGV, ("-mllvm", "-souper-harvest-dataflow-facts=false");
}
if (getenv("SOUPER_HARVEST_USES")) {
push @ARGV, ("-mllvm", "-souper-harvest-uses");
}
}
if (getenv("SOUPER_DISABLE_LLVM_PEEPHOLES")) {
push @ARGV, ("-mllvm", "-disable-all-peepholes");
}
if ((getenv("SOUPER_DYNAMIC_PROFILE") ||
getenv("SOUPER_DYNAMIC_PROFILE_ALL")) && linkp() && $souper) {
push @ARGV, ("@PROFILE_LIBRARY@", "@HIREDIS_LIBRARY@");
}
if (getenv("SOUPER_DEBUG") && $souper) {
foreach my $arg (@ARGV) {
print STDERR "$arg ";
}
print STDERR "\n";
}
foreach my $e (keys %ENV) {
next unless $e =~ /^SOUPER_/;
die "unexpected Souper-related environment variable '${e}'" unless $whitelist{$e};
}
exec @ARGV;