source: icGREP/icgrep-devel/icgrep/icgrep.cpp @ 4734

Last change on this file since 4734 was 4734, checked in by cameron, 4 years ago

Reorganize icgrep into RE/Pablo/IR passes, support -precompiled IR

File size: 16.0 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <string>
8#include <iostream>
9#include <fstream>
10
11#include "icgrep.h"
12#include "utf_encoding.h"
13#include "pablo/pablo_compiler.h"
14#include <llvm/IR/Function.h>
15#include <llvm/IR/Module.h>
16#include <llvm/ExecutionEngine/ExecutionEngine.h>
17#include <llvm/ExecutionEngine/MCJIT.h>
18#include <llvm/IRReader/IRReader.h>
19#include <llvm/Support/SourceMgr.h>
20#include <llvm/Support/CommandLine.h>
21#include <llvm/Support/TargetSelect.h>
22#include <llvm/Support/Host.h>
23
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>
31#include <re/parsefailure.h>
32#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#endif
43#include <pablo/function.h>
44#include <re/printer_re.h>
45#include <pablo/printer_pablos.h>
46
47#include "do_grep.h"
48
49static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
50                                       "These options control the regular expression source.");
51
52static cl::OptionCategory bGrepOutputOptions("Output Options",
53                                      "These options control the output.");
54
55static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
56
57static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
58static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
59static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
60
61static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
62static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
63
64static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
65static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
66static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
67
68static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
69static cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("regex file"), cl::init(""), cl::cat(aRegexSourceOptions));
70static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
71
72static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
73                                      "These options control printing of intermediate regular expression structures.");
74
75static cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
76                                      "These options control printing of intermediate Pablo code.");
77
78static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
79static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
80static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
81static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
82static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
83static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
84static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
85static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
86static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
87
88static cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations", "These options control Pablo optimization passes.");
89
90static cl::opt<bool> DisablePabloCSE("disable-CSE", cl::init(false),
91                                      cl::desc("Disable Pablo common subexpression elimination/dead code elimination"),
92                                      cl::cat(cPabloOptimizationsOptions));
93static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
94                                      cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
95                                      cl::cat(cPabloOptimizationsOptions));
96
97#ifdef ENABLE_MULTIPLEXING
98static cl::opt<bool> EnableMultiplexing("multiplexing", cl::init(false),
99    cl::desc("combine Advances whose inputs are mutual exclusive into the fewest number of advances possible (expensive)."),
100    cl::cat(cPabloOptimizationsOptions));
101#endif
102
103static int firstInputFile = 1;  // Normal case when first positional arg is a regex.
104
105re::RE * get_icgrep_RE() {
106 
107    //std::vector<std::string> regexVector;
108    if (RegexFilename != "") {
109        std::ifstream regexFile(RegexFilename.c_str());
110        std::string r;
111        if (regexFile.is_open()) {
112            while (std::getline(regexFile, r)) {
113                regexVector.push_back(r);
114            }
115            regexFile.close();
116        }
117    }
118   
119    // if there are no regexes specified through -e or -f, the first positional argument
120    // must be a regex, not an input file.
121   
122    if (regexVector.size() == 0) {
123        regexVector.push_back(inputFiles[0]);
124        firstInputFile = 1;
125    }
126    else {
127        firstInputFile = 0;
128    }
129   
130    re::ModeFlagSet globalFlags = 0;
131    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
132
133 
134    std::vector<re::RE *> REs;
135    re::RE * re_ast = nullptr;
136    for (int i = 0; i < regexVector.size(); i++) {
137        try
138        {
139            re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
140        }
141        catch (ParseFailure failure)
142        {
143            std::cerr << "Regex parsing failure: " << failure.what() << std::endl;
144            std::cerr << regexVector[i] << std::endl;
145            exit(1);
146        }
147        REs.push_back(re_ast);
148    }
149    if (REs.size() > 1) {
150        re_ast = re::makeAlt(REs.begin(), REs.end());
151    }
152   
153    if (PrintAllREs || PrintParsedREs) {
154        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
155    }
156    return re_ast;
157}
158
159re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
160    //Optimization passes to simplify the AST.
161    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
162    if (PrintAllREs || PrintStrippedREs) {
163        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
164    }
165    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
166    if (PrintAllREs || PrintStrippedREs) {
167        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
168    }
169   
170    cc::CC_NameMap nameMap;
171    re_ast = nameMap.process(re_ast, re::UnicodeClass);
172   
173    // std::cerr << "-----------------------------" << std::endl;
174   
175    if (PrintAllREs || PrintNamedREs) {
176        std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
177        std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
178    }
179   
180    //Add the UTF encoding.
181    if (encoding.getType() == Encoding::Type::UTF_8) {
182        re_ast = cc::UTF8_Encoder::toUTF8(nameMap, re_ast);
183        if (PrintAllREs || PrintUTF8REs) {
184            //Print to the terminal the AST that was generated by the utf8 encoder.
185            std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
186            std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
187        }
188    }
189   
190    re_ast = re::RE_Simplifier::simplify(re_ast);
191    if (PrintAllREs || PrintSimplifiedREs) {
192        //Print to the terminal the AST that was generated by the simplifier.
193        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
194    }
195    return re_ast;
196}
197   
198pablo::PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
199   
200    pablo::PabloFunction * function = pablo::PabloFunction::Create("process_block", 8, 2);
201   
202    cc::CC_Compiler cc_compiler(*function, encoding);
203   
204    cc_compiler.compileByteClasses(re_ast);
205   
206    if (PrintCompiledCCcode) {
207        //Print to the terminal the AST that was generated by the character class compiler.
208        llvm::raw_os_ostream cerr(std::cerr);
209        cerr << "CC AST:" << "\n";
210        PabloPrinter::print(function->getEntryBlock().statements(), cerr);
211    }
212   
213    re::RE_Compiler re_compiler(*function, cc_compiler);
214    re_compiler.initializeRequiredStreams();
215    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
216   
217    if (PrintCompiledREcode) {
218        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
219        llvm::raw_os_ostream cerr(std::cerr);
220        cerr << "Initial Pablo AST:\n";
221        PabloPrinter::print(function->getEntryBlock().statements(), cerr);
222    }
223    return function;
224}
225
226void pablo_function_passes(pablo::PabloFunction * function) {
227    // Scan through the pablo code and perform DCE and CSE
228    if (!DisablePabloCSE) {
229        pablo::Simplifier::optimize(*function);
230    }
231    if (PabloSinkingPass) {
232        pablo::CodeSinking::optimize(*function);
233    }
234#ifdef ENABLE_MULTIPLEXING
235    if (EnableMultiplexing) {
236        pablo::AutoMultiplexing::optimize(*function);
237    }
238#endif
239    if (PrintOptimizedREcode) {
240        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
241        llvm::raw_os_ostream cerr(std::cerr);
242        cerr << "Final Pablo AST:\n";
243        PabloPrinter::print(function->getEntryBlock().statements(), cerr);
244    }
245}
246
247
248
249ExecutionEngine * JIT_to_ExecutionEngine (llvm::Function * f) {
250
251    InitializeNativeTarget();
252    InitializeNativeTargetAsmPrinter();
253    InitializeNativeTargetAsmParser();
254
255    std::string errMessage;
256    EngineBuilder builder(std::move(std::unique_ptr<Module>(f->getParent())));
257    builder.setErrorStr(&errMessage);
258    builder.setMCPU(sys::getHostCPUName());
259    builder.setOptLevel(CodeGenOpt::Level::None);
260    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
261    ExecutionEngine * engine = builder.create();
262    if (engine == nullptr) {
263        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
264    }
265    //engine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
266    // engine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
267
268    return engine;
269}
270
271
272extern "C" {
273  void wrapped_print_register(char * regName, BitBlock bit_block) {
274      print_register<BitBlock>(regName, bit_block);
275  }
276}
277
278void icgrep_Linking(Module * m, ExecutionEngine * e) {
279    Module::FunctionListType & fns = m->getFunctionList();
280    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
281        std::string fnName = it->getName().str();
282        if (fnName == "process_block") continue;
283        if (fnName == "wrapped_print_register") {
284            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
285        }
286#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
287        else {
288            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
289            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
290        }
291#endif
292    }
293}
294
295
296int main(int argc, char *argv[]) {
297    StringMap<cl::Option*> Map;
298    cl::getRegisteredOptions(Map);
299    Map["time-passes"]->setHiddenFlag(cl::Hidden);
300    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
301    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
302    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
303    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
304    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
305    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
306    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
307    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
308    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
309    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
310    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
311    Map["regalloc"]->setHiddenFlag(cl::Hidden);
312    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
313    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
314    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
315    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
316    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
317    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
318    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
319    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
320    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
321    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
322
323    cl::ParseCommandLineOptions(argc, argv);
324   
325    Encoding encoding(Encoding::Type::UTF_8, 8);
326
327    llvm::Function * icgrep_IR = nullptr;
328   
329    if (IRFileName == "") {       
330        re::RE * re_ast = get_icgrep_RE();
331        re_ast = regular_expression_passes(encoding, re_ast);
332       
333        pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
334
335        pablo_function_passes(function);
336        pablo::PabloCompiler pablo_compiler;
337        try {
338            icgrep_IR = pablo_compiler.compile(function);
339            releaseSlabAllocatorMemory();
340        }
341        catch (std::runtime_error e) {
342            releaseSlabAllocatorMemory();
343            std::cerr << "Runtime error: " << e.what() << std::endl;
344            exit(1);
345        }
346    }
347    else {
348        firstInputFile = 0;  // No regexp arguments; first positional argument is a file to process.
349        SMDiagnostic ParseErr;
350        Module * M = parseIRFile(IRFileName, ParseErr, getGlobalContext()).release();
351        if (!M) {
352            throw std::runtime_error("Error in Parsing IR File " + IRFileName);
353        }
354        icgrep_IR = M->getFunction("process_block");
355    }
356   
357    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(icgrep_IR);
358   
359    icgrep_Linking(icgrep_IR->getParent(), engine);
360   
361    // Ensure everything is ready to go.
362    engine->finalizeObject();
363   
364    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
365   
366    if (icgrep_MCptr) {
367        GrepExecutor grepEngine = GrepExecutor(icgrep_MCptr);
368        grepEngine.setCountOnlyOption(CountOnly);
369        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
370        grepEngine.setShowLineNumberOption(ShowLineNumbers);
371            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
372            grepEngine.setShowFileNameOption();
373        }
374        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
375            grepEngine.doGrep(inputFiles[i].c_str());
376        }
377    }
378   
379    //engine->freeMachineCodeForFunction(icgrep_IR); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
380    delete engine;
381
382    return 0;
383}
384
Note: See TracBrowser for help on using the repository browser.