]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
Provide RoseResources to roseQuality.
authorAlex Coyte <a.coyte@intel.com>
Mon, 31 Jul 2017 00:38:30 +0000 (10:38 +1000)
committerMatthew Barr <matthew.barr@intel.com>
Mon, 21 Aug 2017 01:24:52 +0000 (11:24 +1000)
RoseResources is an alternative to manually digging through the bytecode.

src/rose/rose_build_bytecode.cpp
src/rose/rose_build_misc.cpp
src/rose/rose_build_misc.h
src/rose/rose_build_resources.h

index 4d9c3d7eb22d6bbd12ceb5f65c956c28b183b9a4..e62c9e18bd252a25611c5c95dd17777b01eafbd4 100644 (file)
@@ -2333,6 +2333,7 @@ void addSomRevNfas(build_context &bc, RoseEngine &proto,
 
 static
 void recordResources(RoseResources &resources, const RoseBuildImpl &build,
+                     const vector<raw_dfa> &anchored_dfas,
                      const vector<LitFragment> &fragments) {
     if (!build.outfixes.empty()) {
         resources.has_outfixes = true;
@@ -2351,6 +2352,15 @@ void recordResources(RoseResources &resources, const RoseBuildImpl &build,
             break;
         }
     }
+
+    resources.has_anchored = !anchored_dfas.empty();
+    resources.has_anchored_multiple = anchored_dfas.size() > 1;
+    for (const auto &rdfa : anchored_dfas) {
+        if (rdfa.states.size() > 256) {
+            resources.has_anchored_large = true;
+        }
+    }
+
 }
 
 static
@@ -3413,6 +3423,7 @@ u32 writeEagerQueueIter(const set<u32> &eager, u32 leftfixBeginQueue,
 
 static
 bytecode_ptr<RoseEngine> addSmallWriteEngine(const RoseBuildImpl &build,
+                                             const RoseResources &res,
                                              bytecode_ptr<RoseEngine> rose) {
     assert(rose);
 
@@ -3421,7 +3432,7 @@ bytecode_ptr<RoseEngine> addSmallWriteEngine(const RoseBuildImpl &build,
         return rose;
     }
 
-    u32 qual = roseQuality(rose.get());
+    u32 qual = roseQuality(res, rose.get());
     auto smwr_engine = build.smwr.build(qual);
     if (!smwr_engine) {
         DEBUG_PRINTF("no smwr built\n");
@@ -3561,10 +3572,7 @@ bytecode_ptr<RoseEngine> RoseBuildImpl::buildFinalEngine(u32 minWidth) {
     build_context bc;
     u32 floatingMinLiteralMatchOffset
         = findMinFloatingLiteralMatch(*this, anchored_dfas);
-    recordResources(bc.resources, *this, fragments);
-    if (!anchored_dfas.empty()) {
-        bc.resources.has_anchored = true;
-    }
+    recordResources(bc.resources, *this, anchored_dfas, fragments);
     bc.needs_mpv_catchup = needsMpvCatchup(*this);
 
     makeBoundaryPrograms(*this, bc, boundary, dboundary, proto.boundary);
@@ -3803,7 +3811,7 @@ bytecode_ptr<RoseEngine> RoseBuildImpl::buildFinalEngine(u32 minWidth) {
     bc.engine_blob.write_bytes(engine.get());
 
     // Add a small write engine if appropriate.
-    engine = addSmallWriteEngine(*this, move(engine));
+    engine = addSmallWriteEngine(*this, bc.resources, move(engine));
 
     DEBUG_PRINTF("rose done %p\n", engine.get());
 
index d8a3999456368c228e3bb56180b465688464f394..450b2efbb94fb577c3dda5e80da13380de7d87b1 100644 (file)
@@ -29,6 +29,7 @@
 #include "rose_build_misc.h"
 #include "rose_build_impl.h"
 
+#include "rose_build_resources.h"
 #include "hwlm/hwlm_literal.h"
 #include "nfa/castlecompile.h"
 #include "nfa/goughcompile.h"
@@ -788,18 +789,16 @@ LeftEngInfo::operator bool() const {
     return graph || castle || dfa || haig;
 }
 
-u32 roseQuality(const RoseEngine *t) {
+u32 roseQuality(const RoseResources &res, const RoseEngine *t) {
     /* Rose is low quality if the atable is a Mcclellan 16 or has multiple DFAs
      */
-    const anchored_matcher_info *atable = getALiteralMatcher(t);
-    if (atable) {
-        if (atable->next_offset) {
+    if (res.has_anchored) {
+        if (res.has_anchored_multiple) {
             DEBUG_PRINTF("multiple atable engines\n");
             return 0;
         }
-        const NFA *nfa = (const NFA *)((const char *)atable + sizeof(*atable));
 
-        if (!isSmallDfaType(nfa->type)) {
+        if (res.has_anchored_large) {
             DEBUG_PRINTF("m16 atable engine\n");
             return 0;
         }
@@ -808,7 +807,7 @@ u32 roseQuality(const RoseEngine *t) {
     /* if we always run multiple engines then we are slow */
     u32 always_run = 0;
 
-    if (atable) {
+    if (res.has_anchored) {
         always_run++;
     }
 
@@ -817,8 +816,7 @@ u32 roseQuality(const RoseEngine *t) {
         always_run++;
     }
 
-    const HWLM *ftable = getFLiteralMatcher(t);
-    if (ftable) {
+    if (res.has_floating) {
         /* TODO: ignore conditional ftables, or ftables beyond smwr region */
         always_run++;
     }
index b9c6d5ca675f732e5bc481052890651959f40a65..f34b8292006aa320064001434fbe2edf2805ebcc 100644 (file)
@@ -35,9 +35,11 @@ struct RoseEngine;
 
 namespace ue2 {
 
+struct RoseResources;
+
 /* used by heuristics to determine the small write engine. High numbers are
  * intended to indicate a lightweight rose. */
-u32 roseQuality(const RoseEngine *rose);
+u32 roseQuality(const RoseResources &res, const RoseEngine *rose);
 
 }
 
index 3edb81b9644c73900e257bcfffb31f027d8943b7..4fa102f3ee757d67ed4fcd5f92155950716e57ac 100644 (file)
@@ -48,6 +48,8 @@ struct RoseResources {
     bool has_lit_delay = false;
     bool has_lit_check = false; // long literal support
     bool has_anchored = false;
+    bool has_anchored_multiple = false; /* multiple anchored dfas */
+    bool has_anchored_large = false; /* mcclellan 16 anchored dfa */
     bool has_floating = false;
     bool has_eod = false;
 };