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

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

Removing compile-time BLOCK_SIZE

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// 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 * GetNativeIDISA_Builder(Module * mod, Type * bitBlockType) {
209
210    int blockSize = bitBlockType->isIntegerTy() ? cast<IntegerType>(bitBlockType)->getIntegerBitWidth() : cast<VectorType>(bitBlockType)->getBitWidth();
211    if (blockSize == 256) {
212        if ((strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
213            return new IDISA::IDISA_AVX2_Builder(mod, bitBlockType);
214        }
215        else{
216            return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
217        }
218    }
219    else if (blockSize == 64)
220        return new IDISA::IDISA_I64_Builder(mod, bitBlockType); 
221    return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
222}
223
224
225
226ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
227
228    InitializeNativeTarget();
229    InitializeNativeTargetAsmPrinter();
230    InitializeNativeTargetAsmParser();
231
232    std::string errMessage;
233    EngineBuilder builder(std::move(std::unique_ptr<Module>(m)));
234    builder.setErrorStr(&errMessage);
235    builder.setMCPU(sys::getHostCPUName());
236    builder.setOptLevel(CodeGenOpt::Level::None);
237
238    if (!DisableAVX2 && (strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
239            std::vector<std::string> attrs;
240            attrs.push_back("avx2");
241            builder.setMAttrs(attrs);
242    }
243
244    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
245    ExecutionEngine * engine = builder.create();
246    if (engine == nullptr) {
247        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
248    }
249
250    return engine;
251}
252
253int total_count = 0;
254
255extern "C" {
256    void wrapped_report_match(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer, int filesize, char * filename) {
257        if(CountOnly){
258            total_count++;
259            return;
260        }
261
262        llvm::raw_os_ostream out(std::cout);
263        if (ShowFileNames) {
264            out << filename << ':';
265        }
266        if (ShowLineNumbers) {
267            out << lineNum << ":";
268        }
269
270        if ((buffer[line_start] == 0xA) && (line_start != line_end)) {
271            // The line "starts" on the LF of a CRLF.  Really the end of the last line.
272            line_start++;
273        }
274        if (line_end == filesize) {
275            // The match position is at end-of-file.   We have a final unterminated line.
276            out.write(&buffer[line_start], line_end - line_start);
277            if (NormalizeLineBreaks) {
278                out << '\n';  // terminate it
279            }
280            return;
281        }
282        unsigned char end_byte = (unsigned char)buffer[line_end]; 
283        if (NormalizeLineBreaks) {
284            if (end_byte == 0x85) {
285                // Line terminated with NEL, on the second byte.  Back up 1.
286                line_end--;
287            } else if (end_byte > 0xD) {
288                // Line terminated with PS or LS, on the third byte.  Back up 2.
289                line_end -= 2;
290            }
291            out.write(&buffer[line_start], line_end - line_start);
292            out << '\n';
293        }
294        else{   
295            if (end_byte == 0x0D) {
296                // Check for line_end on first byte of CRLF;  note that we don't
297                // want to access past the end of buffer.
298                if ((line_end + 1 < filesize) && (buffer[line_end + 1] == 0x0A)) {
299                    // Found CRLF; preserve both bytes.
300                    line_end++;
301                }
302            }
303            out.write(&buffer[line_start], line_end - line_start + 1);
304        }
305    }
306}
307
308
309void PrintTotalCount(){
310    if(CountOnly){
311        std::cout << total_count << std::endl;
312    }
313}
314
315re::CC * parsedCodePointSet;
316
317extern "C" {
318    void insert_codepoints(uint64_t lineNum, uint64_t line_start, uint64_t line_end, const char * buffer) {
319       re::codepoint_t c = 0;
320        ssize_t line_pos = line_start;
321        while (isxdigit(buffer[line_pos])) {
322            if (isdigit(buffer[line_pos])) {
323                c = (c << 4) | (buffer[line_pos] - '0');
324            }
325            else {
326                c = (c << 4) | (tolower(buffer[line_pos]) - 'a' + 10);
327            }
328            line_pos++;
329        }
330        assert(((line_pos - line_start) >= 4) && ((line_pos - line_start) <= 6)); // UCD format 4 to 6 hex digits.       
331        parsedCodePointSet->insert(c);
332    }
333}
334
335void setParsedCodePointSet(){
336    parsedCodePointSet = re::makeCC();
337}
338
339re::CC * getParsedCodePointSet(){
340    return parsedCodePointSet;
341}
342
343// extern "C" {
344//   void wrapped_print_register(char * regName, BitBlock bit_block) {
345//       print_register<BitBlock>(regName, bit_block);
346//   }
347// }
348
349void icgrep_Linking(Module * m, ExecutionEngine * e) {
350    Module::FunctionListType & fns = m->getFunctionList();
351    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
352        std::string fnName = it->getName().str();
353        if (fnName == "s2p_block") continue;
354        if (fnName == "process_block") continue;
355        if (fnName == "process_block_initialize_carries") continue;
356       
357        // if (fnName == "wrapped_print_register") {
358        //     e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
359        // }
360        if (fnName == "wrapped_report_match") {
361            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_match);
362        }
363        if (fnName == "insert_codepoints") {
364            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&insert_codepoints);
365        }
366#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
367        else {
368            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
369            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
370        }
371#endif
372    }
373}
Note: See TracBrowser for help on using the repository browser.