WebKit Bugzilla
New
Browse
Search+
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
[patch]
the patch
blah.patch (text/plain), 10.97 KB, created by
Filip Pizlo
on 2013-07-13 09:53:49 PDT
(
hide
)
Description:
the patch
Filename:
MIME Type:
Creator:
Filip Pizlo
Created:
2013-07-13 09:53:49 PDT
Size:
10.97 KB
patch
obsolete
>Index: Tools/ChangeLog >=================================================================== >--- Tools/ChangeLog (revision 152610) >+++ Tools/ChangeLog (working copy) >@@ -1,3 +1,24 @@ >+2013-07-13 Filip Pizlo <fpizlo@apple.com> >+ >+ fourthTier: We should have a reduced FTL LLVM pipeline tool in the repository >+ https://bugs.webkit.org/show_bug.cgi?id=118647 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ Add a tool that takes in an LLVM bitcode file and JITs it in exactly the same >+ way that the FTL would. >+ >+ * ReducedFTL: Added. >+ * ReducedFTL/ReducedFTL.c: Added. >+ (MemorySection): >+ (mmAllocateCodeSection): >+ (mmAllocateDataSection): >+ (mmApplyPermissions): >+ (mmDestroy): >+ (symbolLookupCallback): >+ (main): >+ * ReducedFTL/build.sh: Added. >+ > 2013-06-21 Filip Pizlo <fpizlo@apple.com> > > Merge trunk r146548. >Index: Tools/ReducedFTL/ReducedFTL.c >=================================================================== >--- Tools/ReducedFTL/ReducedFTL.c (revision 0) >+++ Tools/ReducedFTL/ReducedFTL.c (working copy) >@@ -0,0 +1,267 @@ >+/* >+ * 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 <inttypes.h> >+#include <stdio.h> >+#include <stdlib.h> >+#include <string.h> >+#include <sys/mman.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> >+ >+struct MemorySection { >+ uint8_t *start; >+ size_t size; >+ struct MemorySection *next; >+}; >+ >+static struct MemorySection* sectionHead; >+ >+static uint8_t *mmAllocateCodeSection( >+ void *opaqueState, uintptr_t size, unsigned alignment, unsigned sectionID) >+{ >+ size_t pageSize = getpagesize(); >+ >+ uint8_t *start = mmap( >+ 0, (size + pageSize - 1) & ~pageSize, >+ 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; >+ } >+} >+ >+int main(int c, char **v) >+{ >+ LLVMDisasmContextRef disassembler; >+ LLVMExecutionEngineRef engine; >+ struct LLVMMCJITCompilerOptions options; >+ LLVMMemoryBufferRef buffer; >+ LLVMModuleRef module; >+ LLVMValueRef value; >+ struct MemorySection *section; >+ char *error; >+ const char *mode; >+ const char *filename; >+ LLVMPassManagerRef functionPasses = 0; >+ LLVMPassManagerRef modulePasses = 0; >+ >+ LLVMLinkInMCJIT(); >+ LLVMInitializeNativeTarget(); >+ LLVMInitializeX86AsmPrinter(); >+ LLVMInitializeX86Disassembler(); >+ >+ if (c != 3) { >+ printf("Usage: ReducedFTL <mode> <file>\n"); >+ exit(1); >+ } >+ >+ mode = v[1]; >+ filename = v[2]; >+ >+ if (LLVMCreateMemoryBufferWithContentsOfFile(filename, &buffer, &error)) { >+ fprintf(stderr, "Error reading file %s: %s\n", filename, error); >+ exit(1); >+ } >+ >+ if (LLVMParseBitcode(buffer, &module, &error)) { >+ fprintf(stderr, "Error parsing file %s: %s\n", filename, error); >+ exit(1); >+ } >+ >+ printf("Module after parsing:\n"); >+ LLVMDumpModule(module); >+ >+ LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); >+ options.OptLevel = 2; >+ options.EnableFastISel = 0; >+ 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); >+ LLVMAddConstantPropagationPass(modulePasses); >+ LLVMAddInstructionCombiningPass(modulePasses); >+ LLVMAddPromoteMemoryToRegisterPass(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); >+ } >+ >+ printf("Module after optimization:\n"); >+ 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); >+ >+ 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); >+ } >+ } >+ >+ return 0; >+} >+ >Index: Tools/ReducedFTL/build.sh >=================================================================== >--- Tools/ReducedFTL/build.sh (revision 0) >+++ Tools/ReducedFTL/build.sh (working copy) >@@ -0,0 +1,14 @@ >+#!/bin/sh >+ >+set -e >+set -x >+ >+if test "x${LLVM_PATH}" == "x" >+then >+ configPath="llvm-config" >+else >+ configPath="${LLVM_PATH}/bin/llvm-config" >+fi >+ >+clang -c -o ReducedFTL.o ReducedFTL.c `${configPath} --cppflags --cflags` >+clang++ -o ReducedFTL ReducedFTL.o -stdlib=libc++ `${configPath} --ldflags --libs` > >Property changes on: Tools/ReducedFTL/build.sh >___________________________________________________________________ >Added: svn:executable >## -0,0 +1 ## >+* >\ No newline at end of property
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 118647
:
206609
|
206610
|
206611
|
206612
|
206616
|
206618