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

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

Couple modifications to the UCD compiler. Splitting Multiplexing from BDD Minimization.

File size: 8.5 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 <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> PabloMutualExclusionPass("disable-multiplexing", cl::init(true),
64                                      cl::desc("Disable combining Advances whose inputs are mutual exclusive."),
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    if (IsPregeneratedUnicodeEnabled()) {
109        resolveProperties(re_ast);
110    }
111   
112    CC_NameMap nameMap;
113    re_ast = nameMap.process(re_ast, UnicodeClass);
114
115    if (PrintAllREs || PrintNamedREs) {
116        std::cerr << "Namer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
117        std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
118    }
119
120    //Add the UTF encoding.
121    if (encoding.getType() == Encoding::Type::UTF_8) {
122        re_ast = UTF8_Encoder::toUTF8(nameMap, re_ast);
123        if (PrintAllREs || PrintUTF8REs) {
124            //Print to the terminal the AST that was generated by the utf8 encoder.
125            std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
126            std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
127        }
128    }
129   
130    re_ast = RE_Simplifier::simplify(re_ast);
131    if (PrintAllREs || PrintSimplifiedREs) {
132      //Print to the terminal the AST that was generated by the simplifier.
133      std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
134    }
135
136    SymbolGenerator symbolGenerator;
137    PabloBlock & main = PabloBlock::Create(symbolGenerator);
138
139    CC_Compiler cc_compiler(main, encoding);
140   
141    cc_compiler.compileByteClasses(re_ast);
142   
143    auto basisBits = cc_compiler.getBasisBits(nameMap);
144    if (PrintCompiledCCcode) {
145      //Print to the terminal the AST that was generated by the character class compiler.
146      llvm::raw_os_ostream cerr(std::cerr);
147      cerr << "CC AST:" << "\n";
148      PabloPrinter::print(main.statements(), cerr);
149    }
150   
151    RE_Compiler re_compiler(cc_compiler);
152    re_compiler.initializeRequiredStreams();
153    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
154
155    if (PrintCompiledREcode) {
156        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
157        llvm::raw_os_ostream cerr(std::cerr);
158        cerr << "Initial Pablo AST:\n";
159        PabloPrinter::print(main.statements(), cerr);
160    }
161
162    // Scan through the pablo code and perform DCE and CSE
163    if (!DisablePabloCSE) {
164        Simplifier::optimize(main);
165    }
166    if (PabloSinkingPass) {
167        CodeSinking::optimize(main);
168    }
169    #ifdef ENABLE_MULTIPLEXING
170    if (PabloMutualExclusionPass) {
171        if (AutoMultiplexing::optimize(basisBits, main) && !DisablePabloCSE) {
172            Simplifier::optimize(main);
173        }
174    }
175    #endif
176    if (PrintOptimizedREcode) {
177      //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
178      llvm::raw_os_ostream cerr(std::cerr);
179      cerr << "Final Pablo AST:\n";
180      PabloPrinter::print(main.statements(), cerr);
181    }
182
183    PabloCompiler pablo_compiler(basisBits);
184    if (IsPregeneratedUnicodeEnabled()) {
185        install_property_gc_fn_ptrs(pablo_compiler);
186        install_property_sc_fn_ptrs(pablo_compiler);
187        install_property_scx_fn_ptrs(pablo_compiler);
188        install_property_blk_fn_ptrs(pablo_compiler);
189        install_property_DerivedCoreProperties_fn_ptrs(pablo_compiler);
190        install_property_PropList_fn_ptrs(pablo_compiler);
191    }
192    try {
193        CompiledPabloFunction retVal = pablo_compiler.compile(main);
194        releaseSlabAllocatorMemory();
195        return retVal;
196    }
197    catch (std::runtime_error e) {
198        releaseSlabAllocatorMemory();
199        std::cerr << "Runtime error: " << e.what() << std::endl;
200        exit(1);
201    }
202}
203
204}
Note: See TracBrowser for help on using the repository browser.