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

Last change on this file since 4946 was 4946, checked in by cameron, 3 years ago

Refactor and encapsulate grep codegen/execution in GrepEngine?

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