source: icGREP/icgrep-devel/icgrep/compiler.cpp @ 4730

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

Factor out execution engine code from Pablo compiler into new JIT_to_ExecutionEngine routine.

File size: 7.7 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 <compiler.h>
8#include <re/re_cc.h>
9#include <re/re_nullable.h>
10#include <re/re_simplifier.h>
11#include <re/re_alt.h>
12#include <re/parsefailure.h>
13#include <re/re_parser.h>
14#include <re/re_compiler.h>
15#include <utf8_encoder.h>
16#include <cc/cc_compiler.h>
17#include <cc/cc_namemap.hpp>
18#include <pablo/pablo_compiler.h>
19#include <pablo/optimizers/pablo_simplifier.hpp>
20#include <pablo/optimizers/pablo_codesinking.hpp>
21#ifdef ENABLE_MULTIPLEXING
22#include <pablo/optimizers/pablo_automultiplexing.hpp>
23#endif
24#include <pablo/function.h>
25#include <re/printer_re.h>
26#include <pablo/printer_pablos.h>
27#include <iostream>
28#include <llvm/Support/CommandLine.h>
29
30static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
31                                      "These options control printing of intermediate regular expression structures.");
32
33static cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
34                                      "These options control printing of intermediate Pablo code.");
35
36static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
37static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
38static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
39static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
40static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
41static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
42static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
43static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
44static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
45
46static cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations", "These options control Pablo optimization passes.");
47
48static cl::opt<bool> DisablePabloCSE("disable-CSE", cl::init(false),
49                                      cl::desc("Disable Pablo common subexpression elimination/dead code elimination"),
50                                      cl::cat(cPabloOptimizationsOptions));
51static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
52                                      cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
53                                      cl::cat(cPabloOptimizationsOptions));
54
55#ifdef ENABLE_MULTIPLEXING
56static cl::opt<bool> EnableMultiplexing("multiplexing", cl::init(false),
57    cl::desc("combine Advances whose inputs are mutual exclusive into the fewest number of advances possible (expensive)."),
58    cl::cat(cPabloOptimizationsOptions));
59#endif
60
61using namespace re;
62using namespace cc;
63using namespace pablo;
64
65namespace icgrep {
66
67llvm::Function * compile(const Encoding encoding, const std::vector<std::string> regexps, const ModeFlagSet initialFlags) {
68    std::vector<RE *> REs;
69    RE * re_ast = nullptr;
70    for (int i = 0; i < regexps.size(); i++) {
71        try
72        {
73            re_ast = RE_Parser::parse(regexps[i], initialFlags);
74        }
75        catch (ParseFailure failure)
76        {
77            std::cerr << "Regex parsing failure: " << failure.what() << std::endl;
78            std::cerr << regexps[i] << std::endl;
79            exit(1);
80        }
81        REs.push_back(re_ast);
82    }
83    if (REs.size() > 1) {
84        re_ast = makeAlt(REs.begin(), REs.end());
85    }
86
87    if (PrintAllREs || PrintParsedREs) {
88        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
89    }
90
91    //Optimization passes to simplify the AST.
92    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
93    if (PrintAllREs || PrintStrippedREs) {
94        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
95    }
96    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
97    if (PrintAllREs || PrintStrippedREs) {
98        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
99    }
100
101    CC_NameMap nameMap;
102    re_ast = nameMap.process(re_ast, UnicodeClass);
103
104    // std::cerr << "-----------------------------" << std::endl;
105
106    if (PrintAllREs || PrintNamedREs) {
107        std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
108        std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
109    }
110
111    //Add the UTF encoding.
112    if (encoding.getType() == Encoding::Type::UTF_8) {
113        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
114        if (PrintAllREs || PrintUTF8REs) {
115            //Print to the terminal the AST that was generated by the utf8 encoder.
116            std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
117            std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
118        }
119    }
120   
121    re_ast = RE_Simplifier::simplify(re_ast);
122    if (PrintAllREs || PrintSimplifiedREs) {
123      //Print to the terminal the AST that was generated by the simplifier.
124      std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
125    }
126
127    PabloFunction function = PabloFunction::Create("process_block", 8, 2);
128
129    CC_Compiler cc_compiler(function, encoding);
130   
131    cc_compiler.compileByteClasses(re_ast);
132   
133    if (PrintCompiledCCcode) {
134      //Print to the terminal the AST that was generated by the character class compiler.
135      llvm::raw_os_ostream cerr(std::cerr);
136      cerr << "CC AST:" << "\n";
137      PabloPrinter::print(function.getEntryBlock().statements(), cerr);
138    }
139   
140    RE_Compiler re_compiler(function, cc_compiler);
141    re_compiler.initializeRequiredStreams();
142    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
143
144    if (PrintCompiledREcode) {
145        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
146        llvm::raw_os_ostream cerr(std::cerr);
147        cerr << "Initial Pablo AST:\n";
148        PabloPrinter::print(function.getEntryBlock().statements(), cerr);
149    }
150
151    // Scan through the pablo code and perform DCE and CSE
152    if (!DisablePabloCSE) {
153        Simplifier::optimize(function);
154    }
155    if (PabloSinkingPass) {
156        CodeSinking::optimize(function);
157    }
158    #ifdef ENABLE_MULTIPLEXING
159    if (EnableMultiplexing) {
160        AutoMultiplexing::optimize(function);
161    }
162    #endif
163    if (PrintOptimizedREcode) {
164      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
165      llvm::raw_os_ostream cerr(std::cerr);
166      cerr << "Final Pablo AST:\n";
167      PabloPrinter::print(function.getEntryBlock().statements(), cerr);
168    }
169
170    PabloCompiler pablo_compiler;
171    try {
172        llvm::Function * retVal = pablo_compiler.compile(function);
173        releaseSlabAllocatorMemory();
174        return retVal;
175    }
176    catch (std::runtime_error e) {
177        releaseSlabAllocatorMemory();
178        std::cerr << "Runtime error: " << e.what() << std::endl;
179        exit(1);
180    }
181}
182
183}
Note: See TracBrowser for help on using the repository browser.