source: icGREP/icgrep-devel/icgrep/toolchain.cpp @ 4942

Last change on this file since 4942 was 4942, checked in by lindanl, 3 years ago

Remove simd-lib. Print register implemented in LLVM IR.

File size: 15.3 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 <string>
8#include <iostream>
9#include <fstream>
10
11#include "utf_encoding.h"
12#include "pablo/pablo_compiler.h"
13#include <llvm/IR/Function.h>
14#include <llvm/IR/Module.h>
15#include <llvm/ExecutionEngine/ExecutionEngine.h>
16#include <llvm/ExecutionEngine/MCJIT.h>
17#include <llvm/IRReader/IRReader.h>
18#include <llvm/Support/CommandLine.h>
19#include <llvm/CodeGen/CommandFlags.h>
20#include <llvm/Support/SourceMgr.h>
21#include <llvm/Support/TargetSelect.h>
22#include <llvm/Support/Host.h>
23
24#include <IDISA/idisa_avx_builder.h>
25#include <IDISA/idisa_sse_builder.h>
26#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
27#include <UCD/precompiled_properties.h>
28#endif
29#include <re/re_cc.h>
30#include <re/re_nullable.h>
31#include <re/re_simplifier.h>
32#include <re/re_alt.h>
33#include <re/parsefailure.h>
34#include <re/re_parser.h>
35#include <re/re_compiler.h>
36#include <utf8_encoder.h>
37#include <cc/cc_compiler.h>
38#include <pablo/pablo_compiler.h>
39#include <pablo/optimizers/pablo_simplifier.hpp>
40#include <pablo/optimizers/codemotionpass.h>
41#include <pablo/passes/flattenassociativedfg.h>
42#include <pablo/passes/factorizedfg.h>
43#ifdef ENABLE_MULTIPLEXING
44#include <pablo/optimizers/pablo_automultiplexing.hpp>
45#include <pablo/optimizers/pablo_bddminimization.h>
46#include <pablo/optimizers/distributivepass.h>
47#include <pablo/optimizers/schedulingprepass.h>
48#endif
49#include <pablo/function.h>
50#include <pablo/analysis/pabloverifier.hpp>
51#include <re/printer_re.h>
52#include <pablo/printer_pablos.h>
53
54#include "do_grep.h"
55
56using namespace pablo;
57
58static cl::OptionCategory bGrepOutputOptions("Output Options",
59                                      "These options control the output.");
60
61static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
62static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
63static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
64
65static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
66static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
67
68static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
69static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
70
71
72static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
73                                              "These options control printing of intermediate regular expression structures.");
74static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
75static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
76static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
77static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
78static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
79static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
80static cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
81                                            "These options control printing of intermediate Pablo code.");
82
83static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
84static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
85static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
86
87static cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations", "These options control Pablo optimization passes.");
88
89static cl::opt<bool> DisablePabloCSE("disable-CSE", cl::init(false),
90                                     cl::desc("Disable Pablo common subexpression elimination/dead code elimination"),
91                                     cl::cat(cPabloOptimizationsOptions));
92static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
93                                      cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
94                                      cl::cat(cPabloOptimizationsOptions));
95
96#ifdef ENABLE_MULTIPLEXING
97static cl::opt<bool> PrintUnloweredCode("print-unlowered-pablo", cl::init(false), cl::desc("print Pablo output prior to lowering. "), cl::cat(dPabloDumpOptions));
98
99static cl::opt<bool> EnableMultiplexing("multiplexing", cl::init(false),
100                                        cl::desc("combine Advances whose inputs are mutual exclusive into the fewest number of advances possible (expensive)."),
101                                        cl::cat(cPabloOptimizationsOptions));
102
103static cl::opt<bool> EnableLowering("lowering", cl::init(false),
104                                         cl::desc("coalesce associative functions prior to optimization passes."),
105                                         cl::cat(cPabloOptimizationsOptions));
106static cl::opt<bool> EnablePreDistribution("pre-dist", cl::init(false),
107                                         cl::desc("apply distribution law optimization."),
108                                         cl::cat(cPabloOptimizationsOptions));
109static cl::opt<bool> EnablePostDistribution("post-dist", cl::init(false),
110                                         cl::desc("apply distribution law optimization."),
111                                         cl::cat(cPabloOptimizationsOptions));
112#endif
113
114static cl::opt<bool> DisableAVX2("disable-AVX2", cl::init(false), cl::desc("disable AVX2 instruction set."), cl::cat(cPabloOptimizationsOptions));
115
116re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
117    if (PrintAllREs || PrintParsedREs) {
118        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
119    }
120
121    //Optimization passes to simplify the AST.
122    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
123    if (PrintAllREs || PrintStrippedREs) {
124        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
125    }
126    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
127    if (PrintAllREs || PrintStrippedREs) {
128        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
129    }
130
131    re_ast = re::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    return re_ast;
137}
138   
139PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
140    PabloFunction * function = PabloFunction::Create("process_block", 8, 2);
141    cc::CC_Compiler cc_compiler(*function, encoding);
142    re::RE_Compiler re_compiler(*function, cc_compiler);
143    re_compiler.initializeRequiredStreams();
144    re_compiler.compileUnicodeNames(re_ast);
145    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
146
147    if (PrintCompiledREcode) {
148        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
149        llvm::raw_os_ostream cerr(std::cerr);
150        cerr << "Initial Pablo AST:\n";
151        PabloPrinter::print(*function, cerr);
152    }
153    #ifndef NDEBUG
154    PabloVerifier::verify(*function, "creation");
155    #endif
156    return function;
157}
158
159void pablo_function_passes(PabloFunction * function) {
160    // Scan through the pablo code and perform DCE and CSE
161    if (!DisablePabloCSE) {
162        Simplifier::optimize(*function);
163    }
164#ifdef ENABLE_MULTIPLEXING
165    if (EnableLowering || EnablePreDistribution || EnablePostDistribution || EnableMultiplexing) {
166        FlattenAssociativeDFG::transform(*function);
167    }
168#endif
169    if (PabloSinkingPass) {
170        CodeMotionPass::optimize(*function);
171    }
172#ifdef ENABLE_MULTIPLEXING   
173    if (EnablePreDistribution) {
174        DistributivePass::optimize(*function);
175    }
176    if (EnableMultiplexing) {
177        MultiplexingPass::optimize(*function);
178    }
179    if (EnablePostDistribution) {
180        DistributivePass::optimize(*function);
181    }
182    SchedulingPrePass::optimize(*function);
183    if (PrintUnloweredCode) {
184        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
185        llvm::raw_os_ostream cerr(std::cerr);
186        cerr << "Unlowered Pablo AST:\n";
187        PabloPrinter::print(*function, cerr);
188    }
189    if (EnableLowering || EnablePreDistribution || EnablePostDistribution || EnableMultiplexing) {
190        FactorizeDFG::transform(*function);
191    }
192#endif
193    if (PrintOptimizedREcode) {
194        PabloVerifier::verify(*function, "post-optimization");
195        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
196        llvm::raw_os_ostream cerr(std::cerr);
197        cerr << "Final Pablo AST:\n";
198        PabloPrinter::print(*function, cerr);
199    }
200}
201
202// Dynamic AVX2 confirmation
203#if (BLOCK_SIZE == 256)
204#define ISPC_LLVM_VERSION ISPC_LLVM_3_6
205#include "ispc.cpp"
206#endif
207
208
209IDISA::IDISA_Builder * GetNativeIDISA_Builder(Module * mod, Type * bitBlockType) {
210
211#if (BLOCK_SIZE == 256)
212    if ((strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
213        return new IDISA::IDISA_AVX2_Builder(mod, bitBlockType);
214        //std::cerr << "IDISA_AVX2_Builder selected\n";
215    }
216    else{
217        return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
218        //std::cerr << "Generic IDISA_Builder selected\n";
219    }
220#else   
221    return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
222#endif
223}
224
225
226
227ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
228
229    InitializeNativeTarget();
230    InitializeNativeTargetAsmPrinter();
231    InitializeNativeTargetAsmParser();
232
233    std::string errMessage;
234    EngineBuilder builder(std::move(std::unique_ptr<Module>(m)));
235    builder.setErrorStr(&errMessage);
236    builder.setMCPU(sys::getHostCPUName());
237    builder.setOptLevel(CodeGenOpt::Level::None);
238
239#if (BLOCK_SIZE == 256)
240    if (!DisableAVX2 && (strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
241            std::vector<std::string> attrs;
242            attrs.push_back("avx2");
243            builder.setMAttrs(attrs);
244    //std::cerr << "+avx2 set" << std::endl;
245    }
246#endif
247    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
248    ExecutionEngine * engine = builder.create();
249    if (engine == nullptr) {
250        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
251    }
252
253    return engine;
254}
255
256int total_count = 0;
257
258extern "C" {
259    void wrapped_report_match(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer, int filesize, char * filename) {
260        if(CountOnly){
261            total_count++;
262            return;
263        }
264
265        llvm::raw_os_ostream out(std::cout);
266        if (ShowFileNames) {
267            out << filename << ':';
268        }
269        if (ShowLineNumbers) {
270            out << lineNum << ":";
271        }
272
273        if ((buffer[line_start] == 0xA) && (line_start != line_end)) {
274            // The line "starts" on the LF of a CRLF.  Really the end of the last line.
275            line_start++;
276        }
277        if (line_end == filesize) {
278            // The match position is at end-of-file.   We have a final unterminated line.
279            out.write(&buffer[line_start], line_end - line_start);
280            if (NormalizeLineBreaks) {
281                out << '\n';  // terminate it
282            }
283            return;
284        }
285        unsigned char end_byte = (unsigned char)buffer[line_end]; 
286        if (NormalizeLineBreaks) {
287            if (end_byte == 0x85) {
288                // Line terminated with NEL, on the second byte.  Back up 1.
289                line_end--;
290            } else if (end_byte > 0xD) {
291                // Line terminated with PS or LS, on the third byte.  Back up 2.
292                line_end -= 2;
293            }
294            out.write(&buffer[line_start], line_end - line_start);
295            out << '\n';
296        }
297        else{   
298            if (end_byte == 0x0D) {
299                // Check for line_end on first byte of CRLF;  note that we don't
300                // want to access past the end of buffer.
301                if ((line_end + 1 < filesize) && (buffer[line_end + 1] == 0x0A)) {
302                    // Found CRLF; preserve both bytes.
303                    line_end++;
304                }
305            }
306            out.write(&buffer[line_start], line_end - line_start + 1);
307        }
308    }
309}
310
311
312void PrintTotalCount(){
313    if(CountOnly){
314        std::cout << total_count << std::endl;
315    }
316}
317
318re::CC * parsedCodePointSet;
319
320extern "C" {
321    void insert_codepoints(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer) {
322       re::codepoint_t c = 0;
323        ssize_t line_pos = line_start;
324        while (isxdigit(buffer[line_pos])) {
325            if (isdigit(buffer[line_pos])) {
326                c = (c << 4) | (buffer[line_pos] - '0');
327            }
328            else {
329                c = (c << 4) | (tolower(buffer[line_pos]) - 'a' + 10);
330            }
331            line_pos++;
332        }
333        assert(((line_pos - line_start) >= 4) && ((line_pos - line_start) <= 6)); // UCD format 4 to 6 hex digits.       
334        parsedCodePointSet->insert(c);
335    }
336}
337
338void setParsedCodePointSet(){
339    parsedCodePointSet = re::makeCC();
340}
341
342re::CC * getParsedCodePointSet(){
343    return parsedCodePointSet;
344}
345
346// extern "C" {
347//   void wrapped_print_register(char * regName, BitBlock bit_block) {
348//       print_register<BitBlock>(regName, bit_block);
349//   }
350// }
351
352void icgrep_Linking(Module * m, ExecutionEngine * e) {
353    Module::FunctionListType & fns = m->getFunctionList();
354    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
355        std::string fnName = it->getName().str();
356        if (fnName == "s2p_block") continue;
357        if (fnName == "process_block") continue;
358        if (fnName == "process_block_initialize_carries") continue;
359       
360        // if (fnName == "wrapped_print_register") {
361        //     e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
362        // }
363        if (fnName == "wrapped_report_match") {
364            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_match);
365        }
366        if (fnName == "insert_codepoints") {
367            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&insert_codepoints);
368        }
369#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
370        else {
371            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
372            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
373        }
374#endif
375    }
376}
377
Note: See TracBrowser for help on using the repository browser.