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

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

First attempt to intergrate 'generate_predefined_ucd_functions' into build process.

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