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

Last change on this file since 4587 was 4587, checked in by nmedfort, 4 years ago

More multiplexing work.

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