FTL should allow LLVM to allocate data sections with alignment > 8
authorfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Feb 2014 23:11:06 +0000 (23:11 +0000)
committerfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 19 Feb 2014 23:11:06 +0000 (23:11 +0000)
https://bugs.webkit.org/show_bug.cgi?id=129066

Reviewed by Geoffrey Garen.

We were previously using the native allocator's alignment guarantees (which we presumed
to be 8 bytes), and further hinting our desires by using the LSectionWord type (which
was 8 bytes). This breaks now that LLVM will sometimes ask for 16 byte alignment on
some sections.

This changes our data section allocation strategy to use the new FTL::DataSection,
which can handle arbitrary 2^k alignment.

* JavaScriptCore.xcodeproj/project.pbxproj:
* ftl/FTLCompile.cpp:
(JSC::FTL::mmAllocateDataSection):
(JSC::FTL::dumpDataSection):
(JSC::FTL::compile):
* ftl/FTLDataSection.cpp: Added.
(JSC::FTL::DataSection::DataSection):
(JSC::FTL::DataSection::~DataSection):
* ftl/FTLDataSection.h: Added.
(JSC::FTL::DataSection::base):
(JSC::FTL::DataSection::size):
* ftl/FTLJITCode.cpp:
(JSC::FTL::JITCode::addDataSection):
* ftl/FTLJITCode.h:
(JSC::FTL::JITCode::dataSections):
* ftl/FTLState.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@164393 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
Source/JavaScriptCore/ftl/FTLCompile.cpp
Source/JavaScriptCore/ftl/FTLDataSection.cpp [new file with mode: 0644]
Source/JavaScriptCore/ftl/FTLDataSection.h [new file with mode: 0644]
Source/JavaScriptCore/ftl/FTLJITCode.cpp
Source/JavaScriptCore/ftl/FTLJITCode.h
Source/JavaScriptCore/ftl/FTLState.h

index 43b97c9..6acc77d 100644 (file)
@@ -1,5 +1,37 @@
 2014-02-19  Filip Pizlo  <fpizlo@apple.com>
 
+        FTL should allow LLVM to allocate data sections with alignment > 8
+        https://bugs.webkit.org/show_bug.cgi?id=129066
+
+        Reviewed by Geoffrey Garen.
+        
+        We were previously using the native allocator's alignment guarantees (which we presumed
+        to be 8 bytes), and further hinting our desires by using the LSectionWord type (which
+        was 8 bytes). This breaks now that LLVM will sometimes ask for 16 byte alignment on
+        some sections.
+        
+        This changes our data section allocation strategy to use the new FTL::DataSection,
+        which can handle arbitrary 2^k alignment.
+
+        * JavaScriptCore.xcodeproj/project.pbxproj:
+        * ftl/FTLCompile.cpp:
+        (JSC::FTL::mmAllocateDataSection):
+        (JSC::FTL::dumpDataSection):
+        (JSC::FTL::compile):
+        * ftl/FTLDataSection.cpp: Added.
+        (JSC::FTL::DataSection::DataSection):
+        (JSC::FTL::DataSection::~DataSection):
+        * ftl/FTLDataSection.h: Added.
+        (JSC::FTL::DataSection::base):
+        (JSC::FTL::DataSection::size):
+        * ftl/FTLJITCode.cpp:
+        (JSC::FTL::JITCode::addDataSection):
+        * ftl/FTLJITCode.h:
+        (JSC::FTL::JITCode::dataSections):
+        * ftl/FTLState.h:
+
+2014-02-19  Filip Pizlo  <fpizlo@apple.com>
+
         Unreviewed, fix comment.
 
         * ftl/FTLWeight.h:
index 20f72de..fe1618a 100644 (file)
                0FE228EE1436AB2C00196C48 /* Options.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FE228EA1436AB2300196C48 /* Options.cpp */; };
                0FE8534B1723CDA500B618F5 /* DFGDesiredWatchpoints.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FE853491723CDA500B618F5 /* DFGDesiredWatchpoints.cpp */; };
                0FE8534C1723CDA500B618F5 /* DFGDesiredWatchpoints.h in Headers */ = {isa = PBXBuildFile; fileRef = 0FE8534A1723CDA500B618F5 /* DFGDesiredWatchpoints.h */; settings = {ATTRIBUTES = (Private, ); }; };
+               0FE95F7918B5694700B531FB /* FTLDataSection.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FE95F7718B5694700B531FB /* FTLDataSection.cpp */; };
+               0FE95F7A18B5694700B531FB /* FTLDataSection.h in Headers */ = {isa = PBXBuildFile; fileRef = 0FE95F7818B5694700B531FB /* FTLDataSection.h */; settings = {ATTRIBUTES = (Private, ); }; };
                0FEA0A08170513DB00BB722C /* FTLAbbreviations.h in Headers */ = {isa = PBXBuildFile; fileRef = 0FEA09FD170513DB00BB722C /* FTLAbbreviations.h */; settings = {ATTRIBUTES = (Private, ); }; };
                0FEA0A09170513DB00BB722C /* FTLCapabilities.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0FEA09FE170513DB00BB722C /* FTLCapabilities.cpp */; };
                0FEA0A0A170513DB00BB722C /* FTLCapabilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 0FEA09FF170513DB00BB722C /* FTLCapabilities.h */; settings = {ATTRIBUTES = (Private, ); }; };
                0FE228EB1436AB2300196C48 /* Options.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Options.h; sourceTree = "<group>"; };
                0FE853491723CDA500B618F5 /* DFGDesiredWatchpoints.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = DFGDesiredWatchpoints.cpp; path = dfg/DFGDesiredWatchpoints.cpp; sourceTree = "<group>"; };
                0FE8534A1723CDA500B618F5 /* DFGDesiredWatchpoints.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DFGDesiredWatchpoints.h; path = dfg/DFGDesiredWatchpoints.h; sourceTree = "<group>"; };
+               0FE95F7718B5694700B531FB /* FTLDataSection.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FTLDataSection.cpp; path = ftl/FTLDataSection.cpp; sourceTree = "<group>"; };
+               0FE95F7818B5694700B531FB /* FTLDataSection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FTLDataSection.h; path = ftl/FTLDataSection.h; sourceTree = "<group>"; };
                0FEA09FD170513DB00BB722C /* FTLAbbreviations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FTLAbbreviations.h; path = ftl/FTLAbbreviations.h; sourceTree = "<group>"; };
                0FEA09FE170513DB00BB722C /* FTLCapabilities.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FTLCapabilities.cpp; path = ftl/FTLCapabilities.cpp; sourceTree = "<group>"; };
                0FEA09FF170513DB00BB722C /* FTLCapabilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FTLCapabilities.h; path = ftl/FTLCapabilities.h; sourceTree = "<group>"; };
                                0FEA0A211709606900BB722C /* FTLCommonValues.h */,
                                0FEA0A00170513DB00BB722C /* FTLCompile.cpp */,
                                0FEA0A01170513DB00BB722C /* FTLCompile.h */,
+                               0FE95F7718B5694700B531FB /* FTLDataSection.cpp */,
+                               0FE95F7818B5694700B531FB /* FTLDataSection.h */,
                                2AC922B918A16182003CE0FB /* FTLDWARFDebugLineInfo.cpp */,
                                2AC922BA18A16182003CE0FB /* FTLDWARFDebugLineInfo.h */,
                                0F235BBD17178E1C00690C7F /* FTLExitArgument.cpp */,
                                C2C8D03014A3CEFC00578E65 /* CopiedBlock.h in Headers */,
                                C2FC9BD316644DFB00810D33 /* CopiedBlockInlines.h in Headers */,
                                C2EAA3FA149A835E00FCE112 /* CopiedSpace.h in Headers */,
+                               0FE95F7A18B5694700B531FB /* FTLDataSection.h in Headers */,
                                C2C8D02D14A3C6E000578E65 /* CopiedSpaceInlines.h in Headers */,
                                0F5A52D017ADD717008ECB2D /* CopyToken.h in Headers */,
                                0F6B1CC61862C47800845D97 /* FTLUnwindInfo.h in Headers */,
                                0FD82E56141DAF0800179C94 /* DFGOSREntry.cpp in Sources */,
                                0FD8A32517D51F5700CA2C40 /* DFGOSREntrypointCreationPhase.cpp in Sources */,
                                0FC09791146A6F7100CF2442 /* DFGOSRExit.cpp in Sources */,
+                               0FE95F7918B5694700B531FB /* FTLDataSection.cpp in Sources */,
                                0F235BEB17178E7300690C7F /* DFGOSRExitBase.cpp in Sources */,
                                0FC09792146A6F7300CF2442 /* DFGOSRExitCompiler.cpp in Sources */,
                                0FC09776146943B000CF2442 /* DFGOSRExitCompiler32_64.cpp in Sources */,
index 99319f8..88127f6 100644 (file)
@@ -81,10 +81,7 @@ static uint8_t* mmAllocateDataSection(
 
     State& state = *static_cast<State*>(opaqueState);
     
-    RELEASE_ASSERT(alignment <= sizeof(LSectionWord));
-    
-    RefCountedArray<LSectionWord> section(
-        (size + sizeof(LSectionWord) - 1) / sizeof(LSectionWord));
+    RefPtr<DataSection> section = adoptRef(new DataSection(size, alignment));
     
     if (!strcmp(sectionName, "__llvm_stackmaps"))
         state.stackmapsSection = section;
@@ -92,12 +89,12 @@ static uint8_t* mmAllocateDataSection(
         state.jitCode->addDataSection(section);
         state.dataSectionNames.append(sectionName);
         if (!strcmp(sectionName, "__compact_unwind")) {
-            state.compactUnwind = section.data();
+            state.compactUnwind = section->base();
             state.compactUnwindSize = size;
         }
     }
     
-    return bitwise_cast<uint8_t*>(section.data());
+    return bitwise_cast<uint8_t*>(section->base());
 }
 
 static LLVMBool mmApplyPermissions(void*, char**)
@@ -109,12 +106,13 @@ static void mmDestroy(void*)
 {
 }
 
-static void dumpDataSection(RefCountedArray<LSectionWord> section, const char* prefix)
+static void dumpDataSection(DataSection* section, const char* prefix)
 {
-    for (unsigned j = 0; j < section.size(); ++j) {
+    for (unsigned j = 0; j < section->size() / sizeof(int64_t); ++j) {
         char buf[32];
-        snprintf(buf, sizeof(buf), "0x%lx", static_cast<unsigned long>(bitwise_cast<uintptr_t>(section.data() + j)));
-        dataLogF("%s%16s: 0x%016llx\n", prefix, buf, static_cast<long long>(section[j]));
+        int64_t* wordPointer = static_cast<int64_t*>(section->base()) + j;
+        snprintf(buf, sizeof(buf), "0x%lx", static_cast<unsigned long>(bitwise_cast<uintptr_t>(wordPointer)));
+        dataLogF("%s%16s: 0x%016llx\n", prefix, buf, static_cast<long long>(*wordPointer));
     }
 }
 
@@ -566,7 +564,7 @@ void compile(State& state)
         }
         
         for (unsigned i = 0; i < state.jitCode->dataSections().size(); ++i) {
-            const RefCountedArray<LSectionWord>& section = state.jitCode->dataSections()[i];
+            DataSection* section = state.jitCode->dataSections()[i].get();
             dataLog(
                 "Generated LLVM data section for ",
                 CodeBlockWithJITType(state.graph.m_codeBlock, JITCode::FTLJIT),
@@ -580,17 +578,17 @@ void compile(State& state)
     if (shouldShowDisassembly())
         dataLog("Unwind info for ", CodeBlockWithJITType(state.graph.m_codeBlock, JITCode::FTLJIT), ":\n    ", state.jitCode->unwindInfo, "\n");
     
-    if (state.stackmapsSection.size()) {
+    if (state.stackmapsSection && state.stackmapsSection->size()) {
         if (shouldShowDisassembly()) {
             dataLog(
                 "Generated LLVM stackmaps section for ",
                 CodeBlockWithJITType(state.graph.m_codeBlock, JITCode::FTLJIT), ":\n");
             dataLog("    Raw data:\n");
-            dumpDataSection(state.stackmapsSection, "    ");
+            dumpDataSection(state.stackmapsSection.get(), "    ");
         }
         
         RefPtr<DataView> stackmapsData = DataView::create(
-            ArrayBuffer::create(state.stackmapsSection.data(), state.stackmapsSection.byteSize()));
+            ArrayBuffer::create(state.stackmapsSection->base(), state.stackmapsSection->size()));
         state.jitCode->stackmaps.parse(stackmapsData.get());
     
         if (shouldShowDisassembly()) {
diff --git a/Source/JavaScriptCore/ftl/FTLDataSection.cpp b/Source/JavaScriptCore/ftl/FTLDataSection.cpp
new file mode 100644 (file)
index 0000000..1ea1cc5
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2014 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. 
+ */
+
+#include "config.h"
+#include "FTLDataSection.h"
+
+#if ENABLE(FTL_JIT)
+
+#include <wtf/Assertions.h>
+#include <wtf/DataLog.h>
+#include <wtf/FastMalloc.h>
+
+namespace JSC { namespace FTL {
+
+DataSection::DataSection(size_t size, unsigned alignment)
+    : m_size(size)
+{
+    RELEASE_ASSERT(WTF::bitCount(alignment) == 1);
+    
+    const unsigned nativeAlignment = 8;
+    
+    alignment = std::max(nativeAlignment, alignment);
+    
+    size_t allocatedSize = size + alignment - nativeAlignment;
+    m_allocationBase = fastMalloc(allocatedSize);
+    
+    m_base = bitwise_cast<void*>(
+        (bitwise_cast<uintptr_t>(m_allocationBase) + alignment - 1) & ~static_cast<uintptr_t>(alignment - 1));
+    
+    RELEASE_ASSERT(!(bitwise_cast<uintptr_t>(m_base) & (alignment - 1)));
+    RELEASE_ASSERT(bitwise_cast<uintptr_t>(m_base) + size <= bitwise_cast<uintptr_t>(m_allocationBase) + allocatedSize);
+}
+
+DataSection::~DataSection()
+{
+    fastFree(m_allocationBase);
+}
+
+} } // namespace JSC::FTL
+
+#endif // ENABLE(FTL_JIT)
+
diff --git a/Source/JavaScriptCore/ftl/FTLDataSection.h b/Source/JavaScriptCore/ftl/FTLDataSection.h
new file mode 100644 (file)
index 0000000..1618320
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2014 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. 
+ */
+
+#ifndef FTLDataSection_h
+#define FTLDataSection_h
+
+#if ENABLE(FTL_JIT)
+
+#include <wtf/RefCounted.h>
+
+namespace JSC { namespace FTL {
+
+class DataSection : public RefCounted<DataSection> {
+public:
+    DataSection(size_t size, unsigned alignment);
+    ~DataSection();
+    
+    void* base() { return m_base; }
+    size_t size() const { return m_size; }
+    
+private:
+    void* m_allocationBase;
+    void* m_base;
+    size_t m_size;
+};
+
+} } // namespace JSC::FTL
+
+#endif // ENABLE(FTL_JIT)
+
+#endif // FTLDataSection_h
+
index edbbb31..c031609 100644 (file)
@@ -49,7 +49,7 @@ void JITCode::addHandle(PassRefPtr<ExecutableMemoryHandle> handle)
     m_handles.append(handle);
 }
 
-void JITCode::addDataSection(RefCountedArray<LSectionWord> dataSection)
+void JITCode::addDataSection(PassRefPtr<DataSection> dataSection)
 {
     m_dataSections.append(dataSection);
 }
index 7130400..9dfad6d 100644 (file)
@@ -31,6 +31,7 @@
 #if ENABLE(FTL_JIT)
 
 #include "DFGCommonData.h"
+#include "FTLDataSection.h"
 #include "FTLOSRExit.h"
 #include "FTLStackMaps.h"
 #include "FTLUnwindInfo.h"
@@ -40,8 +41,6 @@
 
 namespace JSC { namespace FTL {
 
-typedef int64_t LSectionWord; // We refer to LLVM data sections using LSectionWord*, just to be clear about our intended alignment restrictions.
-
 class JITCode : public JSC::JITCode {
 public:
     JITCode();
@@ -56,12 +55,12 @@ public:
     
     void initializeExitThunks(CodeRef);
     void addHandle(PassRefPtr<ExecutableMemoryHandle>);
-    void addDataSection(RefCountedArray<LSectionWord>);
+    void addDataSection(PassRefPtr<DataSection>);
     void initializeArityCheckEntrypoint(CodeRef);
     void initializeAddressForCall(CodePtr);
     
     const Vector<RefPtr<ExecutableMemoryHandle>>& handles() const { return m_handles; }
-    const Vector<RefCountedArray<LSectionWord>>& dataSections() const { return m_dataSections; }
+    const Vector<RefPtr<DataSection>>& dataSections() const { return m_dataSections; }
     
     CodePtr exitThunks();
     
@@ -74,7 +73,7 @@ public:
     UnwindInfo unwindInfo;
     
 private:
-    Vector<RefCountedArray<LSectionWord>> m_dataSections;
+    Vector<RefPtr<DataSection>> m_dataSections;
     Vector<RefPtr<ExecutableMemoryHandle>> m_handles;
     CodePtr m_addressForCall;
     CodeRef m_arityCheckEntrypoint;
index ed48ebc..68da23f 100644 (file)
@@ -79,7 +79,7 @@ public:
     Vector<CString> dataSectionNames;
     void* compactUnwind;
     size_t compactUnwindSize;
-    RefCountedArray<LSectionWord> stackmapsSection;
+    RefPtr<DataSection> stackmapsSection;
     
     void dumpState(const char* when);
 };