From cb7067f59d085c3737e4c3406c36b01f2866a16f Mon Sep 17 00:00:00 2001 From: Alex Coyte Date: Mon, 30 May 2016 14:00:31 +1000 Subject: [PATCH] Prevent trying to build smallwrite engine for large cases --- src/grey.cpp | 6 +++++- src/grey.h | 4 +++- src/hs.cpp | 4 ++-- src/nfagraph/ng.cpp | 5 +++-- src/nfagraph/ng.h | 5 +++-- src/smallwrite/smallwrite_build.cpp | 20 +++++++++++++++----- src/smallwrite/smallwrite_build.h | 5 +++-- 7 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/grey.cpp b/src/grey.cpp index 69dab627..e2022e74 100644 --- a/src/grey.cpp +++ b/src/grey.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Intel Corporation + * Copyright (c) 2015-2016, Intel Corporation * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -126,6 +126,8 @@ Grey::Grey(void) : // are given to rose &co smallWriteLargestBufferBad(35), limitSmallWriteOutfixSize(1048576), // 1 MB + smallWriteMaxPatterns(10000), + smallWriteMaxLiterals(10000), dumpFlags(0), limitPatternCount(8000000), // 8M patterns limitPatternLength(16000), // 16K bytes @@ -273,6 +275,8 @@ void applyGreyOverrides(Grey *g, const string &s) { G_UPDATE(smallWriteLargestBuffer); G_UPDATE(smallWriteLargestBufferBad); G_UPDATE(limitSmallWriteOutfixSize); + G_UPDATE(smallWriteMaxPatterns); + G_UPDATE(smallWriteMaxLiterals); G_UPDATE(limitPatternCount); G_UPDATE(limitPatternLength); G_UPDATE(limitGraphVertices); diff --git a/src/grey.h b/src/grey.h index a2261052..8ac9e6b1 100644 --- a/src/grey.h +++ b/src/grey.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Intel Corporation + * Copyright (c) 2015-2016, Intel Corporation * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -143,6 +143,8 @@ struct Grey { u32 smallWriteLargestBuffer; // largest buffer that can be small write u32 smallWriteLargestBufferBad;// largest buffer that can be small write u32 limitSmallWriteOutfixSize; //!< max total size of outfix DFAs + u32 smallWriteMaxPatterns; // only try small writes if fewer patterns + u32 smallWriteMaxLiterals; // only try small writes if fewer literals enum DumpFlags { DUMP_NONE = 0, diff --git a/src/hs.cpp b/src/hs.cpp index 3680e79e..07f6d2c1 100644 --- a/src/hs.cpp +++ b/src/hs.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Intel Corporation + * Copyright (c) 2015-2016, Intel Corporation * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -219,7 +219,7 @@ hs_compile_multi_int(const char *const *expressions, const unsigned *flags, : get_current_target(); CompileContext cc(isStreaming, isVectored, target_info, g); - NG ng(cc, somPrecision); + NG ng(cc, elements, somPrecision); try { for (unsigned int i = 0; i < elements; i++) { diff --git a/src/nfagraph/ng.cpp b/src/nfagraph/ng.cpp index b4b34d74..5d4f1b97 100644 --- a/src/nfagraph/ng.cpp +++ b/src/nfagraph/ng.cpp @@ -75,14 +75,15 @@ using namespace std; namespace ue2 { -NG::NG(const CompileContext &in_cc, unsigned in_somPrecision) +NG::NG(const CompileContext &in_cc, size_t num_patterns, + unsigned in_somPrecision) : maxSomRevHistoryAvailable(in_cc.grey.somMaxRevNfaLength), minWidth(depth::infinity()), rm(in_cc.grey), ssm(in_somPrecision), cc(in_cc), rose(makeRoseBuilder(rm, ssm, cc, boundary)), - smwr(makeSmallWriteBuilder(rm, cc)) { + smwr(makeSmallWriteBuilder(num_patterns, rm, cc)) { } NG::~NG() { diff --git a/src/nfagraph/ng.h b/src/nfagraph/ng.h index 52353da9..95936fcc 100644 --- a/src/nfagraph/ng.h +++ b/src/nfagraph/ng.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Intel Corporation + * Copyright (c) 2015-2016, Intel Corporation * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -87,7 +87,8 @@ class SmallWriteBuild; class NG : boost::noncopyable { public: - NG(const CompileContext &in_cc, unsigned in_somPrecision); + NG(const CompileContext &in_cc, size_t num_patterns, + unsigned in_somPrecision); ~NG(); /** \brief Consumes a pattern, returns false or throws a CompileError diff --git a/src/smallwrite/smallwrite_build.cpp b/src/smallwrite/smallwrite_build.cpp index 792a3d5b..7fb54440 100644 --- a/src/smallwrite/smallwrite_build.cpp +++ b/src/smallwrite/smallwrite_build.cpp @@ -65,7 +65,8 @@ namespace { // unnamed // Concrete impl class class SmallWriteBuildImpl : public SmallWriteBuild { public: - SmallWriteBuildImpl(const ReportManager &rm, const CompileContext &cc); + SmallWriteBuildImpl(size_t num_patterns, const ReportManager &rm, + const CompileContext &cc); // Construct a runtime implementation. aligned_unique_ptr build(u32 roseQuality) override; @@ -87,11 +88,14 @@ public: SmallWriteBuild::~SmallWriteBuild() { } -SmallWriteBuildImpl::SmallWriteBuildImpl(const ReportManager &rm_in, +SmallWriteBuildImpl::SmallWriteBuildImpl(size_t num_patterns, + const ReportManager &rm_in, const CompileContext &cc_in) : rm(rm_in), cc(cc_in), /* small write is block mode only */ - poisoned(!cc.grey.allowSmallWrite || cc.streaming) { + poisoned(!cc.grey.allowSmallWrite + || cc.streaming + || num_patterns > cc.grey.smallWriteMaxPatterns) { } void SmallWriteBuildImpl::add(const NGWrapper &w) { @@ -163,6 +167,10 @@ void SmallWriteBuildImpl::add(const ue2_literal &literal, ReportID r) { } cand_literals.push_back(make_pair(literal, r)); + + if (cand_literals.size() > cc.grey.smallWriteMaxLiterals) { + poisoned = true; + } } static @@ -181,6 +189,7 @@ void lit_to_graph(NGHolder *h, const ue2_literal &literal, ReportID r) { bool SmallWriteBuildImpl::determiniseLiterals() { DEBUG_PRINTF("handling literals\n"); assert(!poisoned); + assert(cand_literals.size() <= cc.grey.smallWriteMaxLiterals); if (cand_literals.empty()) { return true; /* nothing to do */ @@ -352,9 +361,10 @@ aligned_unique_ptr prepEngine(raw_dfa &rdfa, u32 roseQuality, } // SmallWriteBuild factory -unique_ptr makeSmallWriteBuilder(const ReportManager &rm, +unique_ptr makeSmallWriteBuilder(size_t num_patterns, + const ReportManager &rm, const CompileContext &cc) { - return ue2::make_unique(rm, cc); + return ue2::make_unique(num_patterns, rm, cc); } aligned_unique_ptr diff --git a/src/smallwrite/smallwrite_build.h b/src/smallwrite/smallwrite_build.h index 9c3de9d3..59a8528a 100644 --- a/src/smallwrite/smallwrite_build.h +++ b/src/smallwrite/smallwrite_build.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Intel Corporation + * Copyright (c) 2015-2016, Intel Corporation * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -64,7 +64,8 @@ public: }; // Construct a usable SmallWrite builder. -std::unique_ptr makeSmallWriteBuilder(const ReportManager &rm, +std::unique_ptr makeSmallWriteBuilder(size_t num_patterns, + const ReportManager &rm, const CompileContext &cc); size_t smwrSize(const SmallWriteEngine *t);