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

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

Added pablo Prototype to compiler. All Calls must be given one instead of a Name.

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