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

Last change on this file since 4207 was 4203, checked in by nmedfort, 5 years ago

Performance bug fix

File size: 6.1 KB
Line 
1/*
2 *  Copyright (c) 2014 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/*
8 *  Copyright (c) 2014 International Characters.
9 *  This software is licensed to the public under the Open Software License 3.0.
10 *  icgrep is a trademark of International Characters.
11 */
12
13#include "compiler.h"
14#include "re/re_nullable.h"
15#include "re/re_simplifier.h"
16#include "re/re_reducer.h"
17#include "re/parsefailure.h"
18#include "re/re_parser.h"
19#include "re/re_compiler.h"
20#include "hrtime.h"
21#include "utf8_encoder.h"
22#include "cc/cc_compiler.h"
23
24//FOR TESTING AND AND ANALYSIS
25//#include "pbix_counter.h"
26
27// #define DEBUG_PRINT_RE_AST
28// #define DEBUG_PRINT_PBIX_AST
29
30#ifdef DEBUG_PRINT_RE_AST
31#include "printer_re.h"
32#endif
33#ifdef DEBUG_PRINT_PBIX_AST
34#include "printer_pablos.h"
35#endif
36
37using namespace re;
38using namespace cc;
39
40namespace icgrep {
41
42LLVM_Gen_RetVal compile(bool show_compile_time, bool ascii_only, std::string basis_pattern, std::string gensym_pattern, UTF_Encoding encoding, std::string input_string)
43{
44    CC_Compiler cc_compiler(encoding, basis_pattern, gensym_pattern);
45
46    RE * re_ast = nullptr;
47    try
48    {
49        re_ast = RE_Parser::parse_re(input_string);
50    }
51    catch (ParseFailure failure)
52    {
53        std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
54        exit(1);
55    }
56
57    #ifdef DEBUG_PRINT_RE_AST
58    //Print to the terminal the AST that was generated by the parser before adding the UTF encoding:
59    std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
60    #endif
61
62    //Add the UTF encoding.
63    if (!ascii_only)
64    {
65        if (encoding.getName().compare("UTF-8") == 0)
66        {
67            re_ast = UTF8_Encoder::toUTF8(re_ast);
68        }
69        else
70        {
71            std::cerr << "Invalid encoding!" << std::endl;
72            exit(1);
73        }
74    }
75
76    #ifdef DEBUG_PRINT_RE_AST
77    //Print to the terminal the AST that was generated by the utf8 encoder.
78    std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
79    #endif
80
81    //Optimization passes to simplify the AST.
82    re_ast = RE_Nullable::removeNullablePrefix(re_ast);
83    #ifdef DEBUG_PRINT_RE_AST
84    std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
85    #endif
86
87    re_ast = RE_Nullable::removeNullableSuffix(re_ast);
88    #ifdef DEBUG_PRINT_RE_AST
89    std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
90    #endif
91
92    re_ast = RE_Simplifier::simplify(re_ast);
93    #ifdef DEBUG_PRINT_RE_AST
94    //Print to the terminal the AST that was generated by the simplifier.
95    std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
96    #endif
97
98    //Map all of the unique character classes in order to reduce redundancy.
99    std::map<std::string, RE*> re_map;
100    re_ast = RE_Reducer::reduce(re_ast, re_map);
101
102    #ifdef DEBUG_PRINT_RE_AST
103    //Print to the terminal the AST with the reduced REs.
104    std::cerr << "Reducer:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
105    #endif
106
107    //Build our list of predefined characters.
108    std::string cc_name;
109    std::map<std::string,std::string> name_map;
110
111    CC* cc_lf = makeCC(0x0A);
112    cc_name = cc_lf->getName();
113    re_map.insert(make_pair(cc_name, cc_lf));
114    name_map.insert(make_pair("LineFeed", cc_name));
115
116    CC* cc_utf8_single_byte = makeCC(0x00, 0x7F);
117    cc_name = cc_utf8_single_byte->getName();
118    re_map.insert(make_pair(cc_name, cc_utf8_single_byte));
119    name_map.insert(make_pair("UTF8-SingleByte", cc_name));
120
121    CC* cc_utf8_prefix2 = makeCC(0xC2, 0xDF);
122    cc_name = cc_utf8_prefix2->getName();
123    re_map.insert(make_pair(cc_name, cc_utf8_prefix2));
124    name_map.insert(make_pair("UTF8-Prefix2", cc_name));
125
126    CC* cc_utf8_prefix3 = makeCC(0xE0, 0xEF);
127    cc_name = cc_utf8_prefix3->getName();
128    re_map.insert(make_pair(cc_name, cc_utf8_prefix3));
129    name_map.insert(make_pair("UTF8-Prefix3", cc_name));
130
131    CC* cc_utf8_prefix4 = makeCC(0xF0, 0xF4);
132    cc_name = cc_utf8_prefix4->getName();
133    re_map.insert(make_pair(cc_name, cc_utf8_prefix4));
134    name_map.insert(make_pair("UTF8-Prefix4", cc_name));
135
136    cc_compiler.compile_from_map(re_map);
137    auto cc_stmtsl = cc_compiler.get_compiled();
138    #ifdef DEBUG_PRINT_PBIX_AST
139    //Print to the terminal the AST that was generated by the character class compiler.
140    std::cerr << "Pablo CC AST:" << std::endl << StatementPrinter::Print_CC_PabloStmts(cc_stmtsl) << std::endl;
141    #endif
142
143    RE_Compiler pbix_compiler(name_map);
144    CodeGenState re_subexpression_cg_state = pbix_compiler.compile_subexpressions(re_map);
145    #ifdef DEBUG_PRINT_PBIX_AST
146    //Print to the terminal the AST that was generated for the re subexpressions.
147    std::cerr << "Subexpressions:" << StatementPrinter::PrintStmts(re_subexpression_cg_state) << std::endl;
148    #endif
149
150    CodeGenState re_cg_state = pbix_compiler.compile(re_ast);
151    #ifdef DEBUG_PRINT_PBIX_AST
152    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
153    std::cerr << "Final Pablo AST:" << StatementPrinter::PrintStmts(re_cg_state) << ")" << std::endl;
154    //Print a count of the Pablo statements and expressions that are contained in the AST from the pbix compiler.
155    // std::cerr << "Pablo Statement Count: " << Pbix_Counter::Count_PabloStatements(re_cg_state.stmtsl) << std::endl;
156    #endif
157
158    LLVM_Generator irgen(name_map, basis_pattern, encoding.getBits());
159
160    unsigned long long cycles = 0;
161    double timer = 0;
162    if (show_compile_time)
163    {
164        cycles = get_hrcycles();
165        timer = getElapsedTime();
166    }
167
168    LLVM_Gen_RetVal retVal = irgen.Generate_LLVMIR(re_cg_state, re_subexpression_cg_state, cc_stmtsl);
169    if (show_compile_time)
170    {
171        cycles = get_hrcycles() - cycles;
172        timer = getElapsedTime() - timer;
173        std::cout << "LLVM compile time -  cycles:       " << cycles  << std::endl;
174        std::cout << "LLVM compile time -  milliseconds: " << timer << std::endl;
175    }
176
177    return retVal;
178}
179
180}
Note: See TracBrowser for help on using the repository browser.