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

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

Removing compile-time BLOCK_SIZE from toolchain, grep_engine, enable BlockSize? command-line parameter

File size: 15.9 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// Dynamic processor detection
53#define ISPC_LLVM_VERSION ISPC_LLVM_3_6
54#include "ispc.cpp"
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::OptionCategory dCodeGenOptions("Code Generation Options", "These options control code generation.");
115
116static cl::opt<bool> DisableAVX2("disable-AVX2", cl::init(false), cl::desc("disable AVX2 instruction set."), cl::cat(dCodeGenOptions));
117
118static 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));
119
120
121re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
122    if (PrintAllREs || PrintParsedREs) {
123        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
124    }
125
126    //Optimization passes to simplify the AST.
127    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
128    if (PrintAllREs || PrintStrippedREs) {
129        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
130    }
131    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
132    if (PrintAllREs || PrintStrippedREs) {
133        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
134    }
135
136    re_ast = re::RE_Simplifier::simplify(re_ast);
137    if (PrintAllREs || PrintSimplifiedREs) {
138        //Print to the terminal the AST that was generated by the simplifier.
139        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
140    }
141    return re_ast;
142}
143   
144PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
145    PabloFunction * function = PabloFunction::Create("process_block", 8, 2);
146    cc::CC_Compiler cc_compiler(*function, encoding);
147    re::RE_Compiler re_compiler(*function, cc_compiler);
148    re_compiler.initializeRequiredStreams();
149    re_compiler.compileUnicodeNames(re_ast);
150    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
151
152    if (PrintCompiledREcode) {
153        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
154        llvm::raw_os_ostream cerr(std::cerr);
155        cerr << "Initial Pablo AST:\n";
156        PabloPrinter::print(*function, cerr);
157    }
158    #ifndef NDEBUG
159    PabloVerifier::verify(*function, "creation");
160    #endif
161    return function;
162}
163
164void pablo_function_passes(PabloFunction * function) {
165    // Scan through the pablo code and perform DCE and CSE
166    if (!DisablePabloCSE) {
167        Simplifier::optimize(*function);
168    }
169#ifdef ENABLE_MULTIPLEXING
170    if (EnableLowering || EnablePreDistribution || EnablePostDistribution || EnableMultiplexing) {
171        FlattenAssociativeDFG::transform(*function);
172    }
173#endif
174    if (PabloSinkingPass) {
175        CodeMotionPass::optimize(*function);
176    }
177#ifdef ENABLE_MULTIPLEXING   
178    if (EnablePreDistribution) {
179        DistributivePass::optimize(*function);
180    }
181    if (EnableMultiplexing) {
182        MultiplexingPass::optimize(*function);
183    }
184    if (EnablePostDistribution) {
185        DistributivePass::optimize(*function);
186    }
187    SchedulingPrePass::optimize(*function);
188    if (PrintUnloweredCode) {
189        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
190        llvm::raw_os_ostream cerr(std::cerr);
191        cerr << "Unlowered Pablo AST:\n";
192        PabloPrinter::print(*function, cerr);
193    }
194    if (EnableLowering || EnablePreDistribution || EnablePostDistribution || EnableMultiplexing) {
195        FactorizeDFG::transform(*function);
196    }
197#endif
198    if (PrintOptimizedREcode) {
199        PabloVerifier::verify(*function, "post-optimization");
200        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
201        llvm::raw_os_ostream cerr(std::cerr);
202        cerr << "Final Pablo AST:\n";
203        PabloPrinter::print(*function, cerr);
204    }
205}
206
207
208IDISA::IDISA_Builder * GetIDISA_Builder(Module * mod) {
209    bool hasAVX2 = (strncmp(lGetSystemISA(), "avx2", 4) == 0);
210   
211    unsigned theBlockSize = BlockSize;  // from command line
212   
213    if (theBlockSize == 0) {  // No BlockSize override: use processor SIMD width
214        theBlockSize = hasAVX2 ? 256 : 128;
215    }
216    Type * bitBlockType = VectorType::get(IntegerType::get(getGlobalContext(), 64), theBlockSize/64);
217   
218    int blockSize = bitBlockType->isIntegerTy() ? cast<IntegerType>(bitBlockType)->getIntegerBitWidth() : cast<VectorType>(bitBlockType)->getBitWidth();
219    if (blockSize == 256) {
220        if (hasAVX2) {
221            return new IDISA::IDISA_AVX2_Builder(mod, bitBlockType);
222        }
223        else{
224            return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
225        }
226    }
227    else if (blockSize == 64)
228        return new IDISA::IDISA_I64_Builder(mod, bitBlockType); 
229    return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
230}
231
232
233
234ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
235
236    InitializeNativeTarget();
237    InitializeNativeTargetAsmPrinter();
238    InitializeNativeTargetAsmParser();
239
240    std::string errMessage;
241    EngineBuilder builder(std::move(std::unique_ptr<Module>(m)));
242    builder.setErrorStr(&errMessage);
243    builder.setMCPU(sys::getHostCPUName());
244    builder.setOptLevel(CodeGenOpt::Level::None);
245
246    if (!DisableAVX2 && (strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
247            std::vector<std::string> attrs;
248            attrs.push_back("avx2");
249            builder.setMAttrs(attrs);
250    }
251
252    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
253    ExecutionEngine * engine = builder.create();
254    if (engine == nullptr) {
255        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
256    }
257
258    return engine;
259}
260
261int total_count = 0;
262
263extern "C" {
264    void wrapped_report_match(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer, int filesize, char * filename) {
265        if(CountOnly){
266            total_count++;
267            return;
268        }
269
270        llvm::raw_os_ostream out(std::cout);
271        if (ShowFileNames) {
272            out << filename << ':';
273        }
274        if (ShowLineNumbers) {
275            out << lineNum << ":";
276        }
277
278        if ((buffer[line_start] == 0xA) && (line_start != line_end)) {
279            // The line "starts" on the LF of a CRLF.  Really the end of the last line.
280            line_start++;
281        }
282        if (line_end == filesize) {
283            // The match position is at end-of-file.   We have a final unterminated line.
284            out.write(&buffer[line_start], line_end - line_start);
285            if (NormalizeLineBreaks) {
286                out << '\n';  // terminate it
287            }
288            return;
289        }
290        unsigned char end_byte = (unsigned char)buffer[line_end]; 
291        if (NormalizeLineBreaks) {
292            if (end_byte == 0x85) {
293                // Line terminated with NEL, on the second byte.  Back up 1.
294                line_end--;
295            } else if (end_byte > 0xD) {
296                // Line terminated with PS or LS, on the third byte.  Back up 2.
297                line_end -= 2;
298            }
299            out.write(&buffer[line_start], line_end - line_start);
300            out << '\n';
301        }
302        else{   
303            if (end_byte == 0x0D) {
304                // Check for line_end on first byte of CRLF;  note that we don't
305                // want to access past the end of buffer.
306                if ((line_end + 1 < filesize) && (buffer[line_end + 1] == 0x0A)) {
307                    // Found CRLF; preserve both bytes.
308                    line_end++;
309                }
310            }
311            out.write(&buffer[line_start], line_end - line_start + 1);
312        }
313    }
314}
315
316
317void PrintTotalCount(){
318    if(CountOnly){
319        std::cout << total_count << std::endl;
320    }
321}
322
323re::CC * parsedCodePointSet;
324
325extern "C" {
326    void insert_codepoints(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer) {
327       re::codepoint_t c = 0;
328        ssize_t line_pos = line_start;
329        while (isxdigit(buffer[line_pos])) {
330            if (isdigit(buffer[line_pos])) {
331                c = (c << 4) | (buffer[line_pos] - '0');
332            }
333            else {
334                c = (c << 4) | (tolower(buffer[line_pos]) - 'a' + 10);
335            }
336            line_pos++;
337        }
338        assert(((line_pos - line_start) >= 4) && ((line_pos - line_start) <= 6)); // UCD format 4 to 6 hex digits.       
339        parsedCodePointSet->insert(c);
340    }
341}
342
343void setParsedCodePointSet(){
344    parsedCodePointSet = re::makeCC();
345}
346
347re::CC * getParsedCodePointSet(){
348    return parsedCodePointSet;
349}
350
351// extern "C" {
352//   void wrapped_print_register(char * regName, BitBlock bit_block) {
353//       print_register<BitBlock>(regName, bit_block);
354//   }
355// }
356
357void icgrep_Linking(Module * m, ExecutionEngine * e) {
358    Module::FunctionListType & fns = m->getFunctionList();
359    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
360        std::string fnName = it->getName().str();
361        if (fnName == "s2p_block") continue;
362        if (fnName == "process_block") continue;
363        if (fnName == "process_block_initialize_carries") continue;
364       
365        // if (fnName == "wrapped_print_register") {
366        //     e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
367        // }
368        if (fnName == "wrapped_report_match") {
369            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_match);
370        }
371        if (fnName == "insert_codepoints") {
372            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&insert_codepoints);
373        }
374#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
375        else {
376            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
377            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
378        }
379#endif
380    }
381}
Note: See TracBrowser for help on using the repository browser.