Changeset 4801


Ignore:
Timestamp:
Sep 27, 2015, 3:10:45 PM (2 years ago)
Author:
cameron
Message:

Refactor tool chain components

Location:
icGREP/icgrep-devel/icgrep
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/CMakeLists.txt

    r4799 r4801  
    6060endif()
    6161if(USE_BOOST_MMAP)
    62   SET(REQ_BOOST_COMPONENTS ${REQ_BOOST_COMPONENTS} iostreams filesystem)
     62  SET(REQ_BOOST_COMPONENTS ${REQ_BOOST_COMPONENTS} system iostreams filesystem)
    6363endif()
    6464
     
    120120ENDIF()
    121121
    122 add_executable(icgrep icgrep.cpp do_grep.cpp ${PRECOMPILED_FILES})
     122add_executable(icgrep icgrep.cpp toolchain.cpp do_grep.cpp ${PRECOMPILED_FILES})
    123123IF(NOT DISABLE_PREGENERATED_UCD_FUNCTIONS)
    124124add_dependencies(icgrep run_generate_predefined_ucd_functions)
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r4797 r4801  
    1010
    1111#include "icgrep.h"
     12#include "toolchain.h"
    1213#include "utf_encoding.h"
    1314#include "pablo/pablo_compiler.h"
     
    2223#include <llvm/Support/Host.h>
    2324
    24 #ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
    25 #include <UCD/precompiled_properties.h>
    26 #endif
    27 #include <re/re_cc.h>
    28 #include <re/re_nullable.h>
    29 #include <re/re_simplifier.h>
    30 #include <re/re_alt.h>
     25#include <re/re_re.h>
    3126#include <re/parsefailure.h>
    3227#include <re/re_parser.h>
    33 #include <re/re_compiler.h>
    34 #include <utf8_encoder.h>
    35 #include <cc/cc_compiler.h>
    36 #include <cc/cc_namemap.hpp>
    37 #include <pablo/pablo_compiler.h>
    38 #include <pablo/optimizers/pablo_simplifier.hpp>
    39 #include <pablo/optimizers/pablo_codesinking.hpp>
    40 #ifdef ENABLE_MULTIPLEXING
    41 #include <pablo/optimizers/pablo_automultiplexing.hpp>
    42 #include <pablo/optimizers/pablo_bddminimization.h>
    43 #include <pablo/optimizers/booleanreassociationpass.h>
    44 #endif
     28#include <re/re_any.h>
     29#include <re/re_alt.h>
    4530#include <pablo/function.h>
    46 #include <re/printer_re.h>
    47 #include <pablo/printer_pablos.h>
    48 #include <pablo/analysis/pabloverifier.hpp>
    4931
    5032#include "do_grep.h"
     
    7355static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
    7456
    75 static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
    76                                       "These options control printing of intermediate regular expression structures.");
    77 
    78 static cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
    79                                       "These options control printing of intermediate Pablo code.");
    80 
    81 static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
    82 static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
    83 static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
    84 static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
    85 static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
    86 static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
    87 static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
    88 static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
    89 static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
    90 
    91 static cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations", "These options control Pablo optimization passes.");
    92 
    93 static cl::opt<bool> DisablePabloCSE("disable-CSE", cl::init(false),
    94                                       cl::desc("Disable Pablo common subexpression elimination/dead code elimination"),
    95                                       cl::cat(cPabloOptimizationsOptions));
    96 static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
    97                                       cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
    98                                       cl::cat(cPabloOptimizationsOptions));
    99 
    100 #ifdef ENABLE_MULTIPLEXING
    101 static cl::opt<bool> EnableMultiplexing("multiplexing", cl::init(false),
    102     cl::desc("combine Advances whose inputs are mutual exclusive into the fewest number of advances possible (expensive)."),
    103     cl::cat(cPabloOptimizationsOptions));
    104 
    105 static cl::opt<bool> EnableReassociation("reassoc", cl::init(false),
    106     cl::desc("perform reassocation and distribution law optimization."),
    107     cl::cat(cPabloOptimizationsOptions));
    108 #endif
    109 
    110 static cl::opt<bool> UseAVX2("use-AVX2", cl::init(false), cl::desc("execute with AVX2 instruction set."), cl::cat(cRegexOutputOptions));
     57
    11158
    11259static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
     
    166113    }
    167114   
    168     if (PrintAllREs || PrintParsedREs) {
    169         std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
    170     }
    171115    return re_ast;
    172 }
    173 
    174 re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
    175     //Optimization passes to simplify the AST.
    176     re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
    177     if (PrintAllREs || PrintStrippedREs) {
    178         std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
    179     }
    180     re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
    181     if (PrintAllREs || PrintStrippedREs) {
    182         std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
    183     }
    184    
    185     cc::CC_NameMap nameMap;
    186     re_ast = nameMap.process(re_ast, re::UnicodeClass);
    187    
    188     // std::cerr << "-----------------------------" << std::endl;
    189    
    190     if (PrintAllREs || PrintNamedREs) {
    191         std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
    192         std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
    193     }
    194    
    195     //Add the UTF encoding.
    196     if (encoding.getType() == Encoding::Type::UTF_8) {
    197         re_ast = cc::UTF8_Encoder::toUTF8(nameMap, re_ast);
    198         if (PrintAllREs || PrintUTF8REs) {
    199             //Print to the terminal the AST that was generated by the utf8 encoder.
    200             std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
    201             std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
    202         }
    203     }
    204    
    205     re_ast = re::RE_Simplifier::simplify(re_ast);
    206     if (PrintAllREs || PrintSimplifiedREs) {
    207         //Print to the terminal the AST that was generated by the simplifier.
    208         std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
    209     }
    210     return re_ast;
    211 }
    212    
    213 pablo::PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
    214    
    215     pablo::PabloFunction * function = pablo::PabloFunction::Create("process_block", 8, 2);
    216    
    217     cc::CC_Compiler cc_compiler(*function, encoding);
    218    
    219     cc_compiler.compileByteClasses(re_ast);
    220    
    221     if (PrintCompiledCCcode) {
    222         //Print to the terminal the AST that was generated by the character class compiler.
    223         llvm::raw_os_ostream cerr(std::cerr);
    224         cerr << "CC AST:" << "\n";
    225         PabloPrinter::print(function->getEntryBlock().statements(), cerr);
    226     }
    227    
    228     re::RE_Compiler re_compiler(*function, cc_compiler);
    229     re_compiler.initializeRequiredStreams();
    230     re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
    231 
    232     if (PrintCompiledREcode) {
    233         //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
    234         llvm::raw_os_ostream cerr(std::cerr);
    235         cerr << "Initial Pablo AST:\n";
    236         PabloPrinter::print(function->getEntryBlock().statements(), cerr);
    237     }
    238     return function;
    239 }
    240 
    241 void pablo_function_passes(pablo::PabloFunction * function) {
    242     // Scan through the pablo code and perform DCE and CSE
    243     if (!DisablePabloCSE) {
    244         pablo::Simplifier::optimize(*function);
    245     }
    246     if (PabloSinkingPass) {
    247         pablo::CodeSinking::optimize(*function);
    248     }
    249 #ifdef ENABLE_MULTIPLEXING   
    250     if (EnableMultiplexing) {
    251         pablo::BDDMinimizationPass::optimize(*function);
    252         pablo::AutoMultiplexing::optimize(*function);
    253     }   
    254     if (EnableReassociation) {
    255         pablo::BooleanReassociationPass::optimize(*function);
    256     }
    257 #endif
    258     if (PrintOptimizedREcode) {
    259         //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
    260         llvm::raw_os_ostream cerr(std::cerr);
    261         cerr << "Final Pablo AST:\n";
    262         PabloPrinter::print(function->getEntryBlock().statements(), cerr);
    263     }
    264 }
    265 
    266 
    267 
    268 ExecutionEngine * JIT_to_ExecutionEngine (llvm::Function * f) {
    269 
    270     InitializeNativeTarget();
    271     InitializeNativeTargetAsmPrinter();
    272     InitializeNativeTargetAsmParser();
    273 
    274     std::string errMessage;
    275     EngineBuilder builder(std::move(std::unique_ptr<Module>(f->getParent())));
    276     builder.setErrorStr(&errMessage);
    277     builder.setMCPU(sys::getHostCPUName());
    278     builder.setOptLevel(CodeGenOpt::Level::None);
    279 #if (BLOCK_SIZE == 256)
    280     if(UseAVX2){
    281             std::vector<std::string> attrs;
    282             attrs.push_back("avx2");
    283             builder.setMAttrs(attrs);
    284     }
    285 #endif
    286     //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
    287     ExecutionEngine * engine = builder.create();
    288     if (engine == nullptr) {
    289         throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
    290     }
    291     //engine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
    292     // engine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
    293 
    294     return engine;
    295 }
    296 
    297 
    298 extern "C" {
    299   void wrapped_print_register(char * regName, BitBlock bit_block) {
    300       print_register<BitBlock>(regName, bit_block);
    301   }
    302 }
    303 
    304 void icgrep_Linking(Module * m, ExecutionEngine * e) {
    305     Module::FunctionListType & fns = m->getFunctionList();
    306     for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
    307         std::string fnName = it->getName().str();
    308         if (fnName == "process_block") continue;
    309         if (fnName == "wrapped_print_register") {
    310             e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
    311         }
    312 #ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
    313         else {
    314             const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
    315             e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
    316         }
    317 #endif
    318     }
    319116}
    320117
     
    358155       
    359156        pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
    360         #ifndef NDEBUG
    361         pablo::PabloVerifier::verify(*function, "creation");
    362         #endif
    363157
    364158        pablo_function_passes(function);
Note: See TracChangeset for help on using the changeset viewer.