forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReducedFTL.c
457 lines (383 loc) · 15.4 KB
/
ReducedFTL.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Simple tool that takes some LLVM bitcode as input and "JITs" it, in the
* same way that the FTL would use LLVM to JIT the IR that it generates.
* This is meant for use as a reduction when communicating to LLVMers
* about bugs, and for quick "what-if" testing to see how our optimization
* pipeline performs. Because of its use as a reduction, this tool is
* intentionally standalone and it would be great if it continues to fit
* in one file.
*/
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/BitReader.h>
#include <llvm-c/Core.h>
#include <llvm-c/Disassembler.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/Target.h>
#include <llvm-c/Transforms/PassManagerBuilder.h>
#include <llvm-c/Transforms/Scalar.h>
static void usage()
{
printf("Usage: ReducedFTL <file1> [<file2> ...]\n");
printf("\n");
printf("Options:\n");
printf("--verbose Display more information, including module dumps.\n");
printf("--timing Measure the time it takes to compile.\n");
printf("--disassemble Disassemble all of the generated code at the end.\n");
printf("--mode <mode> Set the optimization mode (either \"simple\" or \"opt\").\n");
printf("--contexts <arg> Set the number of contexts (either \"one\" or \"many\").\n");
printf("--loop Keep recompiling forever. Useful when attaching a profiler.\n");
printf("--fast-isel Enable the \"fast\" instruction selector.\n");
printf("--help Print this message.\n");
printf("\n");
printf("Unless you specify one of --verbose, --timing, or --disassemble, you will\n");
printf("not see any output.\n");
exit(1);
}
static double currentTime()
{
struct timeval now;
gettimeofday(&now, 0);
return now.tv_sec + now.tv_usec / 1000000.0;
}
struct MemorySection {
uint8_t *start;
size_t size;
struct MemorySection *next;
};
static struct MemorySection* sectionHead;
static size_t roundUpSize(size_t size)
{
size_t pageSize = getpagesize();
return (size + pageSize - 1) & ~pageSize;
}
static uint8_t *mmAllocateCodeSection(
void *opaqueState, uintptr_t size, unsigned alignment, unsigned sectionID)
{
uint8_t *start = mmap(
0, roundUpSize(size),
PROT_WRITE | PROT_READ | PROT_EXEC,
MAP_ANON | MAP_PRIVATE, -1, 0);
if (start == (uint8_t*)-1) {
fprintf(stderr, "Unable to allocate %" PRIuPTR " bytes of executable memory.\n", size);
exit(1);
}
struct MemorySection *section = malloc(sizeof(struct MemorySection));
section->start = start;
section->size = size;
section->next = sectionHead;
sectionHead = section;
return start;
}
static uint8_t *mmAllocateDataSection(
void *opaqueState, uintptr_t size, unsigned alignment, unsigned sectionID,
LLVMBool isReadOnly)
{
return mmAllocateCodeSection(opaqueState, size, alignment, sectionID);
}
static LLVMBool mmApplyPermissions(void *opaque, char **message)
{
return 0;
}
static void mmDestroy(void *opaque)
{
}
static const char *symbolLookupCallback(
void *opaque, uint64_t referenceValue, uint64_t *referenceType, uint64_t referencePC,
const char **referenceName)
{
static char symbolString[20];
switch (*referenceType) {
case LLVMDisassembler_ReferenceType_InOut_None:
return 0;
case LLVMDisassembler_ReferenceType_In_Branch:
*referenceName = 0;
*referenceType = LLVMDisassembler_ReferenceType_InOut_None;
snprintf(
symbolString, sizeof(symbolString), "0x%lx",
(unsigned long)referenceValue);
return symbolString;
default:
fprintf(stderr, "Unexpected reference type!\n");
exit(1);
return 0;
}
}
void webkit_osr_exit()
{
abort();
}
int main(int c, char **v)
{
LLVMContextRef *contexts;
LLVMModuleRef *modules;
char *error;
const char *mode = "opt";
const char **filenames;
unsigned numFiles;
unsigned i;
bool moreOptions;
static int verboseFlag = 0;
static int timingFlag = 0;
static int disassembleFlag = 0;
bool manyContexts = true;
bool loop = false;
double beforeAll;
bool fastIsel = false;
int jitOptLevel = 2;
struct MemorySection *section;
if (c == 1)
usage();
moreOptions = true;
while (moreOptions) {
static struct option longOptions[] = {
{"verbose", no_argument, &verboseFlag, 1},
{"timing", no_argument, &timingFlag, 1},
{"disassemble", no_argument, &disassembleFlag, 1},
{"mode", required_argument, 0, 0},
{"contexts", required_argument, 0, 0},
{"loop", no_argument, 0, 0},
{"fast-isel", no_argument, 0, 0},
{"jit-opt", required_argument, 0, 0},
{"help", no_argument, 0, 0}
};
int optionIndex;
int optionValue;
optionValue = getopt_long(c, v, "", longOptions, &optionIndex);
switch (optionValue) {
case -1:
moreOptions = false;
break;
case 0: {
const char* thisOption = longOptions[optionIndex].name;
if (!strcmp(thisOption, "help"))
usage();
if (!strcmp(thisOption, "loop"))
loop = true;
if (!strcmp(thisOption, "fast-isel"))
fastIsel = true;
if (!strcmp(thisOption, "contexts")) {
if (!strcasecmp(optarg, "one"))
manyContexts = false;
else if (!strcasecmp(optarg, "many"))
manyContexts = true;
else {
fprintf(stderr, "Invalid argument for --contexts.\n");
exit(1);
}
break;
}
if (!strcmp(thisOption, "jit-opt")) {
if (sscanf(optarg, "%d", &jitOptLevel) != 1) {
fprintf(stderr, "Invalid argument for --jit-opt.\n");
exit(1);
}
break;
}
if (!strcmp(thisOption, "mode")) {
mode = strdup(optarg);
break;
}
break;
}
case '?':
exit(0);
break;
default:
printf("optionValue = %d\n", optionValue);
abort();
break;
}
}
LLVMLinkInMCJIT();
LLVMInitializeNativeTarget();
LLVMInitializeX86AsmPrinter();
LLVMInitializeX86Disassembler();
filenames = (const char **)(v + optind);
numFiles = c - optind;
if (!numFiles)
return 0;
do {
contexts = malloc(sizeof(LLVMContextRef) * numFiles);
modules = malloc(sizeof(LLVMModuleRef) * numFiles);
if (manyContexts) {
for (i = 0; i < numFiles; ++i)
contexts[i] = LLVMContextCreate();
} else {
LLVMContextRef context = LLVMContextCreate();
for (i = 0; i < numFiles; ++i)
contexts[i] = context;
}
for (i = 0; i < numFiles; ++i) {
LLVMMemoryBufferRef buffer;
const char* filename = filenames[i];
if (LLVMCreateMemoryBufferWithContentsOfFile(filename, &buffer, &error)) {
fprintf(stderr, "Error reading file %s: %s\n", filename, error);
exit(1);
}
if (LLVMParseBitcodeInContext(contexts[i], buffer, modules + i, &error)) {
fprintf(stderr, "Error parsing file %s: %s\n", filename, error);
exit(1);
}
LLVMDisposeMemoryBuffer(buffer);
if (verboseFlag) {
printf("Module #%u (%s) after parsing:\n", i, filename);
LLVMDumpModule(modules[i]);
}
}
if (verboseFlag)
printf("Generating code for modules...\n");
if (timingFlag)
beforeAll = currentTime();
for (i = 0; i < numFiles; ++i) {
LLVMModuleRef module;
LLVMExecutionEngineRef engine;
struct LLVMMCJITCompilerOptions options;
LLVMValueRef value;
LLVMPassManagerRef functionPasses = 0;
LLVMPassManagerRef modulePasses = 0;
double before;
if (timingFlag)
before = currentTime();
module = modules[i];
LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
options.OptLevel = jitOptLevel;
options.EnableFastISel = fastIsel;
options.MCJMM = LLVMCreateSimpleMCJITMemoryManager(
0, mmAllocateCodeSection, mmAllocateDataSection, mmApplyPermissions, mmDestroy);
if (LLVMCreateMCJITCompilerForModule(&engine, module, &options, sizeof(options), &error)) {
fprintf(stderr, "Error building MCJIT: %s\n", error);
exit(1);
}
if (!strcasecmp(mode, "simple")) {
modulePasses = LLVMCreatePassManager();
LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), modulePasses);
LLVMAddPromoteMemoryToRegisterPass(modulePasses);
LLVMAddConstantPropagationPass(modulePasses);
LLVMAddInstructionCombiningPass(modulePasses);
LLVMAddBasicAliasAnalysisPass(modulePasses);
LLVMAddTypeBasedAliasAnalysisPass(modulePasses);
LLVMAddGVNPass(modulePasses);
LLVMAddCFGSimplificationPass(modulePasses);
LLVMRunPassManager(modulePasses, module);
} else if (!strcasecmp(mode, "opt")) {
LLVMPassManagerBuilderRef passBuilder;
passBuilder = LLVMPassManagerBuilderCreate();
LLVMPassManagerBuilderSetOptLevel(passBuilder, 2);
LLVMPassManagerBuilderSetSizeLevel(passBuilder, 0);
functionPasses = LLVMCreateFunctionPassManagerForModule(module);
modulePasses = LLVMCreatePassManager();
LLVMAddTargetData(LLVMGetExecutionEngineTargetData(engine), modulePasses);
LLVMPassManagerBuilderPopulateFunctionPassManager(passBuilder, functionPasses);
LLVMPassManagerBuilderPopulateModulePassManager(passBuilder, modulePasses);
LLVMPassManagerBuilderDispose(passBuilder);
LLVMInitializeFunctionPassManager(functionPasses);
for (value = LLVMGetFirstFunction(module); value; value = LLVMGetNextFunction(value))
LLVMRunFunctionPassManager(functionPasses, value);
LLVMFinalizeFunctionPassManager(functionPasses);
LLVMRunPassManager(modulePasses, module);
} else {
fprintf(stderr, "Bad optimization mode: %s.\n", mode);
fprintf(stderr, "Valid modes are: \"simple\" or \"opt\".\n");
exit(1);
}
if (verboseFlag) {
printf("Module #%d (%s) after optimization:\n", i, filenames[i]);
LLVMDumpModule(module);
}
for (value = LLVMGetFirstFunction(module); value; value = LLVMGetNextFunction(value)) {
if (LLVMIsDeclaration(value))
continue;
LLVMGetPointerToGlobal(engine, value);
}
if (functionPasses)
LLVMDisposePassManager(functionPasses);
if (modulePasses)
LLVMDisposePassManager(modulePasses);
LLVMDisposeExecutionEngine(engine);
if (timingFlag) {
double after = currentTime();
printf("Module #%d (%s) took %lf ms.\n", i, filenames[i], (after - before) * 1000);
}
}
if (manyContexts) {
for (i = 0; i < numFiles; ++i)
LLVMContextDispose(contexts[i]);
} else
LLVMContextDispose(contexts[0]);
if (timingFlag) {
double after = currentTime();
printf("Compilation took a total of %lf ms.\n", (after - beforeAll) * 1000);
}
if (disassembleFlag) {
LLVMDisasmContextRef disassembler;
disassembler = LLVMCreateDisasm("x86_64-apple-darwin", 0, 0, 0, symbolLookupCallback);
if (!disassembler) {
fprintf(stderr, "Error building disassembler.\n");
exit(1);
}
for (section = sectionHead; section; section = section->next) {
printf("Disassembly for section %p:\n", section);
char pcString[20];
char instructionString[1000];
uint8_t *pc;
uint8_t *end;
pc = section->start;
end = pc + section->size;
while (pc < end) {
snprintf(
pcString, sizeof(pcString), "0x%lx",
(unsigned long)(uintptr_t)pc);
size_t instructionSize = LLVMDisasmInstruction(
disassembler, pc, end - pc, (uintptr_t)pc,
instructionString, sizeof(instructionString));
if (!instructionSize)
snprintf(instructionString, sizeof(instructionString), ".byte 0x%02x", *pc++);
else
pc += instructionSize;
printf(" %16s: %s\n", pcString, instructionString);
}
}
}
for (section = sectionHead; section;) {
struct MemorySection* nextSection = section->next;
munmap(section->start, roundUpSize(section->size));
free(section);
section = nextSection;
}
sectionHead = 0;
} while (loop);
return 0;
}