Title: [245586] trunk
Revision
245586
Author
keith_mil...@apple.com
Date
2019-05-21 10:57:11 -0700 (Tue, 21 May 2019)

Log Message

Cleanup Yarr regexp code around paren contexts.
https://bugs.webkit.org/show_bug.cgi?id=198063

Reviewed by Yusuke Suzuki.

JSTests:

* stress/regexp-many-named-sequential-capture-groups.js: Added.
(i.s):
* stress/regexp-many-unnamed-sequential-capture-groups.js: Added.

Source/_javascript_Core:

There are three refactoring changes around paren contexts:
1. Make EncodedMatchResult the same type as MatchResult on X86_64 and arm64 and uint64_t elsewhere.
2. All function pointer types for Yarr JIT generated code reserve space for paren contexts.
3. initParenContextFreeList should bail based on VM::patternContextBufferSize as that's the buffer size anyway.

* runtime/MatchResult.h:
(JSC::MatchResult::MatchResult):
* runtime/RegExpInlines.h:
(JSC::PatternContextBufferHolder::PatternContextBufferHolder):
(JSC::PatternContextBufferHolder::~PatternContextBufferHolder):
(JSC::PatternContextBufferHolder::size):
(JSC::RegExp::matchInline):
* runtime/VM.h:
* yarr/YarrJIT.cpp:
(JSC::Yarr::YarrGenerator::initParenContextFreeList):
* yarr/YarrJIT.h:
(JSC::Yarr::YarrCodeBlock::execute):

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (245585 => 245586)


--- trunk/JSTests/ChangeLog	2019-05-21 17:50:37 UTC (rev 245585)
+++ trunk/JSTests/ChangeLog	2019-05-21 17:57:11 UTC (rev 245586)
@@ -1,3 +1,14 @@
+2019-05-20  Keith Miller  <keith_mil...@apple.com>
+
+        Cleanup Yarr regexp code around paren contexts.
+        https://bugs.webkit.org/show_bug.cgi?id=198063
+
+        Reviewed by Yusuke Suzuki.
+
+        * stress/regexp-many-named-sequential-capture-groups.js: Added.
+        (i.s):
+        * stress/regexp-many-unnamed-sequential-capture-groups.js: Added.
+
 2019-05-17  Justin Michaud  <justin_mich...@apple.com>
 
         [WASM-References] Add support for Anyref in parameters and return types, Ref.null and Ref.is_null for Anyref values.

Added: trunk/JSTests/stress/regexp-many-named-sequential-capture-groups.js (0 => 245586)


--- trunk/JSTests/stress/regexp-many-named-sequential-capture-groups.js	                        (rev 0)
+++ trunk/JSTests/stress/regexp-many-named-sequential-capture-groups.js	2019-05-21 17:57:11 UTC (rev 245586)
@@ -0,0 +1,8 @@
+let s = '';
+for (let i = 0; i < 1000; i++) {
+    s += `(?<foo${i}>a){0,2}`;
+}
+
+let r = new RegExp(s);
+for (let i = 0; i < 1000; i++)
+    ''.match(r);

Added: trunk/JSTests/stress/regexp-many-unnamed-sequential-capture-groups.js (0 => 245586)


--- trunk/JSTests/stress/regexp-many-unnamed-sequential-capture-groups.js	                        (rev 0)
+++ trunk/JSTests/stress/regexp-many-unnamed-sequential-capture-groups.js	2019-05-21 17:57:11 UTC (rev 245586)
@@ -0,0 +1,8 @@
+let s = '';
+for (let i = 0; i < 1000; i++) {
+    s += '(?:a){0,2}';
+}
+
+let r = new RegExp(s);
+for (let i = 0; i < 1000; i++)
+    ''.match(r);

Modified: trunk/Source/_javascript_Core/ChangeLog (245585 => 245586)


--- trunk/Source/_javascript_Core/ChangeLog	2019-05-21 17:50:37 UTC (rev 245585)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-05-21 17:57:11 UTC (rev 245586)
@@ -1,3 +1,28 @@
+2019-05-20  Keith Miller  <keith_mil...@apple.com>
+
+        Cleanup Yarr regexp code around paren contexts.
+        https://bugs.webkit.org/show_bug.cgi?id=198063
+
+        Reviewed by Yusuke Suzuki.
+
+        There are three refactoring changes around paren contexts:
+        1. Make EncodedMatchResult the same type as MatchResult on X86_64 and arm64 and uint64_t elsewhere.
+        2. All function pointer types for Yarr JIT generated code reserve space for paren contexts.
+        3. initParenContextFreeList should bail based on VM::patternContextBufferSize as that's the buffer size anyway.
+
+        * runtime/MatchResult.h:
+        (JSC::MatchResult::MatchResult):
+        * runtime/RegExpInlines.h:
+        (JSC::PatternContextBufferHolder::PatternContextBufferHolder):
+        (JSC::PatternContextBufferHolder::~PatternContextBufferHolder):
+        (JSC::PatternContextBufferHolder::size):
+        (JSC::RegExp::matchInline):
+        * runtime/VM.h:
+        * yarr/YarrJIT.cpp:
+        (JSC::Yarr::YarrGenerator::initParenContextFreeList):
+        * yarr/YarrJIT.h:
+        (JSC::Yarr::YarrCodeBlock::execute):
+
 2019-05-20  Tadeu Zagallo  <tzaga...@apple.com>
 
         Only cache bytecode for API clients in data vaults

Modified: trunk/Source/_javascript_Core/runtime/MatchResult.h (245585 => 245586)


--- trunk/Source/_javascript_Core/runtime/MatchResult.h	2019-05-21 17:50:37 UTC (rev 245585)
+++ trunk/Source/_javascript_Core/runtime/MatchResult.h	2019-05-21 17:57:11 UTC (rev 245586)
@@ -30,7 +30,12 @@
 
 namespace JSC {
 
-typedef uint64_t EncodedMatchResult;
+struct MatchResult;
+#if CPU(ARM64) || CPU(X86_64)
+using EncodedMatchResult = MatchResult;
+#else
+using EncodedMatchResult = uint64_t;
+#endif
 
 struct MatchResult {
     MatchResult()
@@ -45,19 +50,13 @@
     {
     }
 
-    explicit ALWAYS_INLINE MatchResult(EncodedMatchResult encoded)
+#if !(CPU(ARM64) || CPU(X86_64))
+    ALWAYS_INLINE MatchResult(EncodedMatchResult match)
+        : start(bitwise_cast<MatchResult>(match).start)
+        , end(bitwise_cast<MatchResult>(match).end)
     {
-        union u {
-            uint64_t encoded;
-            struct s {
-                size_t start;
-                size_t end;
-            } split;
-        } value;
-        value.encoded = encoded;
-        start = value.split.start;
-        end = value.split.end;
     }
+#endif
 
     ALWAYS_INLINE static MatchResult failed()
     {
@@ -80,4 +79,6 @@
     size_t end;
 };
 
+static_assert(sizeof(MatchResult) == sizeof(EncodedMatchResult), "Match result and EncodedMatchResult should be the same size");
+
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/RegExpInlines.h (245585 => 245586)


--- trunk/Source/_javascript_Core/runtime/RegExpInlines.h	2019-05-21 17:50:37 UTC (rev 245585)
+++ trunk/Source/_javascript_Core/runtime/RegExpInlines.h	2019-05-21 17:57:11 UTC (rev 245586)
@@ -86,38 +86,36 @@
     return false;
 }
 
-#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
 class PatternContextBufferHolder {
+    WTF_FORBID_HEAP_ALLOCATION;
 public:
     PatternContextBufferHolder(VM& vm, bool needBuffer)
         : m_vm(vm)
-        , m_needBuffer(needBuffer)
     {
-        if (m_needBuffer) {
+#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
+        if (needBuffer)
             m_buffer = m_vm.acquireRegExpPatternContexBuffer();
-            m_size = VM::patternContextBufferSize;
-        } else {
-            m_buffer = nullptr;
-            m_size = 0;
-        }
+#endif
+
     }
 
     ~PatternContextBufferHolder()
     {
-        if (m_needBuffer)
+#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
+        if (buffer())
             m_vm.releaseRegExpPatternContexBuffer();
+#else
+        UNUSED_PARAM(m_vm);
+#endif
     }
 
     void* buffer() { return m_buffer; }
-    unsigned size() { return m_size; }
+    unsigned size() { return buffer() ? VM::patternContextBufferSize : 0; }
 
 private:
     VM& m_vm;
-    bool m_needBuffer;
     void* m_buffer;
-    unsigned m_size;
 };
-#endif
 
 ALWAYS_INLINE void RegExp::compileIfNecessary(VM& vm, Yarr::YarrCharSize charSize)
 {
@@ -158,20 +156,12 @@
     if (m_state == JITCode) {
         {
             ASSERT(m_regExpJITCode);
-#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
             PatternContextBufferHolder patternContextBufferHolder(vm, m_regExpJITCode->usesPatternContextBuffer());
 
-#define EXTRA_JIT_PARAMS  , patternContextBufferHolder.buffer(), patternContextBufferHolder.size()
-#else
-#define EXTRA_JIT_PARAMS
-#endif
-
             if (s.is8Bit())
-                result = m_regExpJITCode->execute(s.characters8(), startOffset, s.length(), offsetVector EXTRA_JIT_PARAMS).start;
+                result = m_regExpJITCode->execute(s.characters8(), startOffset, s.length(), offsetVector, patternContextBufferHolder.buffer(), patternContextBufferHolder.size()).start;
             else
-                result = m_regExpJITCode->execute(s.characters16(), startOffset, s.length(), offsetVector EXTRA_JIT_PARAMS).start;
-
-#undef EXTRA_JIT_PARAMS
+                result = m_regExpJITCode->execute(s.characters16(), startOffset, s.length(), offsetVector, patternContextBufferHolder.buffer(), patternContextBufferHolder.size()).start;
         }
 
         if (result == Yarr::JSRegExpJITCodeFailure) {
@@ -284,20 +274,11 @@
     if (m_state == JITCode) {
         {
             ASSERT(m_regExpJITCode);
-#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
             PatternContextBufferHolder patternContextBufferHolder(vm, m_regExpJITCode->usesPatternContextBuffer());
-
-#define EXTRA_JIT_PARAMS  , patternContextBufferHolder.buffer(), patternContextBufferHolder.size()
-#else
-#define EXTRA_JIT_PARAMS
-#endif
-
             if (s.is8Bit())
-                result = m_regExpJITCode->execute(s.characters8(), startOffset, s.length() EXTRA_JIT_PARAMS);
+                result = m_regExpJITCode->execute(s.characters8(), startOffset, s.length(), patternContextBufferHolder.buffer(), patternContextBufferHolder.size());
             else
-                result = m_regExpJITCode->execute(s.characters16(), startOffset, s.length() EXTRA_JIT_PARAMS);
-
-#undef EXTRA_JIT_PARAMS
+                result = m_regExpJITCode->execute(s.characters16(), startOffset, s.length(), patternContextBufferHolder.buffer(), patternContextBufferHolder.size());
         }
 
 #if ENABLE(REGEXP_TRACING)

Modified: trunk/Source/_javascript_Core/runtime/VM.h (245585 => 245586)


--- trunk/Source/_javascript_Core/runtime/VM.h	2019-05-21 17:50:37 UTC (rev 245585)
+++ trunk/Source/_javascript_Core/runtime/VM.h	2019-05-21 17:57:11 UTC (rev 245586)
@@ -799,6 +799,8 @@
     Lock m_regExpPatternContextLock;
     char* acquireRegExpPatternContexBuffer();
     void releaseRegExpPatternContexBuffer();
+#else
+    static constexpr size_t patternContextBufferSize = 0; // Space allocated to save nested parenthesis context
 #endif
 
     Ref<CompactVariableMap> m_compactVariableMap;

Modified: trunk/Source/_javascript_Core/yarr/YarrJIT.cpp (245585 => 245586)


--- trunk/Source/_javascript_Core/yarr/YarrJIT.cpp	2019-05-21 17:50:37 UTC (rev 245585)
+++ trunk/Source/_javascript_Core/yarr/YarrJIT.cpp	2019-05-21 17:57:11 UTC (rev 245586)
@@ -229,7 +229,7 @@
         parenContextSize = WTF::roundUpToMultipleOf<sizeof(uintptr_t)>(parenContextSize);
 
         // Check that the paren context is a reasonable size.
-        if (parenContextSize > INT16_MAX)
+        if (parenContextSize > VM::patternContextBufferSize)
             m_abortExecution.append(jump());
 
         Jump emptyFreeList = branchTestPtr(Zero, freelistRegister);

Modified: trunk/Source/_javascript_Core/yarr/YarrJIT.h (245585 => 245586)


--- trunk/Source/_javascript_Core/yarr/YarrJIT.h	2019-05-21 17:50:37 UTC (rev 245585)
+++ trunk/Source/_javascript_Core/yarr/YarrJIT.h	2019-05-21 17:57:11 UTC (rev 245586)
@@ -38,10 +38,6 @@
 #define YARR_CALL
 #endif
 
-#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
-constexpr size_t patternContextBufferSize = 8192; // Space caller allocates to save nested parenthesis context
-#endif
-
 namespace JSC {
 
 class VM;
@@ -61,24 +57,11 @@
 };
 
 class YarrCodeBlock {
-#if CPU(X86_64) || CPU(ARM64)
-#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
-    typedef MatchResult (*YarrJITCode8)(const LChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
-    typedef MatchResult (*YarrJITCode16)(const UChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
-    typedef MatchResult (*YarrJITCodeMatchOnly8)(const LChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
-    typedef MatchResult (*YarrJITCodeMatchOnly16)(const UChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
-#else
-    typedef MatchResult (*YarrJITCode8)(const LChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
-    typedef MatchResult (*YarrJITCode16)(const UChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
-    typedef MatchResult (*YarrJITCodeMatchOnly8)(const LChar* input, unsigned start, unsigned length) YARR_CALL;
-    typedef MatchResult (*YarrJITCodeMatchOnly16)(const UChar* input, unsigned start, unsigned length) YARR_CALL;
-#endif
-#else
-    typedef EncodedMatchResult (*YarrJITCode8)(const LChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
-    typedef EncodedMatchResult (*YarrJITCode16)(const UChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
-    typedef EncodedMatchResult (*YarrJITCodeMatchOnly8)(const LChar* input, unsigned start, unsigned length) YARR_CALL;
-    typedef EncodedMatchResult (*YarrJITCodeMatchOnly16)(const UChar* input, unsigned start, unsigned length) YARR_CALL;
-#endif
+    // Technically freeParenContext and parenContextSize are only used if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS) is set. Fortunately, all the calling conventions we support have caller save argument registers.
+    using YarrJITCode8 = EncodedMatchResult (*)(const LChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
+    using YarrJITCode16 = EncodedMatchResult (*)(const UChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
+    using YarrJITCodeMatchOnly8 = EncodedMatchResult (*)(const LChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
+    using YarrJITCodeMatchOnly16 = EncodedMatchResult (*)(const UChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL;
 
 public:
     YarrCodeBlock() = default;
@@ -96,9 +79,10 @@
     void set8BitCodeMatchOnly(MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag> matchOnly) { m_matchOnly8 = matchOnly; }
     void set16BitCodeMatchOnly(MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag> matchOnly) { m_matchOnly16 = matchOnly; }
 
+    bool usesPatternContextBuffer() { return m_usesPatternContextBuffer; }
 #if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
-    bool usesPatternContextBuffer() { return m_usesPatternContextBuffer; }
     void setUsesPatternContextBuffer() { m_usesPatternContextBuffer = true; }
+#endif
 
     MatchResult execute(const LChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize)
     {
@@ -123,32 +107,7 @@
         ASSERT(has16BitCodeMatchOnly());
         return MatchResult(untagCFunctionPtr<YarrJITCodeMatchOnly16, YarrMatchOnly16BitPtrTag>(m_matchOnly16.code().executableAddress())(input, start, length, 0, freeParenContext, parenContextSize));
     }
-#else
-    MatchResult execute(const LChar* input, unsigned start, unsigned length, int* output)
-    {
-        ASSERT(has8BitCode());
-        return MatchResult(reinterpret_cast<YarrJITCode8>(m_ref8.code().executableAddress())(input, start, length, output));
-    }
 
-    MatchResult execute(const UChar* input, unsigned start, unsigned length, int* output)
-    {
-        ASSERT(has16BitCode());
-        return MatchResult(reinterpret_cast<YarrJITCode16>(m_ref16.code().executableAddress())(input, start, length, output));
-    }
-
-    MatchResult execute(const LChar* input, unsigned start, unsigned length)
-    {
-        ASSERT(has8BitCodeMatchOnly());
-        return MatchResult(reinterpret_cast<YarrJITCodeMatchOnly8>(m_matchOnly8.code().executableAddress())(input, start, length));
-    }
-
-    MatchResult execute(const UChar* input, unsigned start, unsigned length)
-    {
-        ASSERT(has16BitCodeMatchOnly());
-        return MatchResult(reinterpret_cast<YarrJITCodeMatchOnly16>(m_matchOnly16.code().executableAddress())(input, start, length));
-    }
-#endif
-
 #if ENABLE(REGEXP_TRACING)
     void *get8BitMatchOnlyAddr()
     {
@@ -202,9 +161,7 @@
     MacroAssemblerCodeRef<Yarr16BitPtrTag> m_ref16;
     MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag> m_matchOnly8;
     MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag> m_matchOnly16;
-#if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS)
-    bool m_usesPatternContextBuffer;
-#endif
+    bool m_usesPatternContextBuffer { false };
     Optional<JITFailureReason> m_failureReason;
 };
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to