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

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

Initial introduction of a PabloFunction? type.

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