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

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

Kernels in icgrep: initial check-in

File size: 12.5 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 "basis_bits.h"
12#include "utf_encoding.h"
13#include "pablo/pablo_compiler.h"
14#include <llvm/IR/Function.h>
15#include <llvm/IR/Module.h>
16#include <llvm/ExecutionEngine/ExecutionEngine.h>
17#include <llvm/ExecutionEngine/MCJIT.h>
18#include <llvm/IRReader/IRReader.h>
19#include <llvm/Support/CommandLine.h>
20#include <llvm/CodeGen/CommandFlags.h>
21#include <llvm/Support/SourceMgr.h>
22#include <llvm/Support/TargetSelect.h>
23#include <llvm/Support/Host.h>
24
25#include <IDISA/idisa_avx_builder.h>
26#include <IDISA/idisa_sse_builder.h>
27#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
28#include <UCD/precompiled_properties.h>
29#endif
30#include <re/re_cc.h>
31#include <re/re_nullable.h>
32#include <re/re_simplifier.h>
33#include <re/re_alt.h>
34#include <re/parsefailure.h>
35#include <re/re_parser.h>
36#include <re/re_compiler.h>
37#include <utf8_encoder.h>
38#include <cc/cc_compiler.h>
39#include <pablo/pablo_compiler.h>
40#include <pablo/optimizers/pablo_simplifier.hpp>
41#include <pablo/optimizers/codemotionpass.h>
42#include <pablo/passes/flattenassociativedfg.h>
43#include <pablo/passes/factorizedfg.h>
44#ifdef ENABLE_MULTIPLEXING
45#include <pablo/optimizers/pablo_automultiplexing.hpp>
46#include <pablo/optimizers/pablo_bddminimization.h>
47#include <pablo/optimizers/distributivepass.h>
48#include <pablo/optimizers/schedulingprepass.h>
49#endif
50#include <pablo/function.h>
51#include <pablo/analysis/pabloverifier.hpp>
52#include <re/printer_re.h>
53#include <pablo/printer_pablos.h>
54
55#include "do_grep.h"
56
57using namespace pablo;
58
59static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
60                                              "These options control printing of intermediate regular expression structures.");
61static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
62static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
63static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
64static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
65static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
66static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
67static cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
68                                            "These options control printing of intermediate Pablo code.");
69
70static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
71static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
72static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
73
74static cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations", "These options control Pablo optimization passes.");
75
76static cl::opt<bool> DisablePabloCSE("disable-CSE", cl::init(false),
77                                     cl::desc("Disable Pablo common subexpression elimination/dead code elimination"),
78                                     cl::cat(cPabloOptimizationsOptions));
79static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
80                                      cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
81                                      cl::cat(cPabloOptimizationsOptions));
82
83#ifdef ENABLE_MULTIPLEXING
84static cl::opt<bool> PrintUnloweredCode("print-unlowered-pablo", cl::init(false), cl::desc("print Pablo output prior to lowering. "), cl::cat(dPabloDumpOptions));
85
86static cl::opt<bool> EnableMultiplexing("multiplexing", cl::init(false),
87                                        cl::desc("combine Advances whose inputs are mutual exclusive into the fewest number of advances possible (expensive)."),
88                                        cl::cat(cPabloOptimizationsOptions));
89
90static cl::opt<unsigned> MultiplexingSetLimit("multiplexing-set-limit", cl::init(std::numeric_limits<unsigned>::max()),
91                                        cl::desc("maximum size of any candidate multiplexing set."),
92                                        cl::cat(cPabloOptimizationsOptions));
93static cl::opt<unsigned> MultiplexingSelectionLimit("multiplexing-selection-limit", cl::init(100),
94                                        cl::desc("maximum number of selections from any partial candidate multiplexing set."),
95                                        cl::cat(cPabloOptimizationsOptions));
96static cl::opt<unsigned> MultiplexingWindowSize("multiplexing-window-size", cl::init(1),
97                                        cl::desc("maximum depth difference for computing mutual exclusion of Advance nodes."),
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::opt<bool> DisableAVX2("disable-AVX2", cl::init(false), cl::desc("disable AVX2 instruction set."), cl::cat(cPabloOptimizationsOptions));
112
113re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
114    if (PrintAllREs || PrintParsedREs) {
115        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
116    }
117
118    //Optimization passes to simplify the AST.
119    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
120    if (PrintAllREs || PrintStrippedREs) {
121        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
122    }
123    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
124    if (PrintAllREs || PrintStrippedREs) {
125        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
126    }
127
128    re_ast = re::RE_Simplifier::simplify(re_ast);
129    if (PrintAllREs || PrintSimplifiedREs) {
130        //Print to the terminal the AST that was generated by the simplifier.
131        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
132    }
133    return re_ast;
134}
135   
136PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
137    PabloFunction * function = PabloFunction::Create("process_block", 8, 2);
138    cc::CC_Compiler cc_compiler(*function, encoding);
139    re::RE_Compiler re_compiler(*function, cc_compiler);
140    re_compiler.initializeRequiredStreams();
141    re_compiler.compileUnicodeNames(re_ast);
142    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
143
144    if (PrintCompiledREcode) {
145        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
146        llvm::raw_os_ostream cerr(std::cerr);
147        cerr << "Initial Pablo AST:\n";
148        PabloPrinter::print(*function, cerr);
149    }
150    #ifndef NDEBUG
151    PabloVerifier::verify(*function, "creation");
152    #endif
153    return function;
154}
155
156void pablo_function_passes(PabloFunction * function) {
157    // Scan through the pablo code and perform DCE and CSE
158    if (!DisablePabloCSE) {
159        Simplifier::optimize(*function);
160    }
161#ifdef ENABLE_MULTIPLEXING
162    if (EnableLowering || EnablePreDistribution || EnablePostDistribution || EnableMultiplexing) {
163        FlattenAssociativeDFG::transform(*function);
164    }
165#endif
166    if (PabloSinkingPass) {
167        CodeMotionPass::optimize(*function);
168    }
169#ifdef ENABLE_MULTIPLEXING   
170    if (EnablePreDistribution) {
171        DistributivePass::optimize(*function);
172    }
173    if (EnableMultiplexing) {
174        MultiplexingPass::optimize(*function, MultiplexingSetLimit, MultiplexingSelectionLimit, MultiplexingWindowSize);
175    }
176    if (EnablePostDistribution) {
177        DistributivePass::optimize(*function);
178    }
179    SchedulingPrePass::optimize(*function);
180    if (PrintUnloweredCode) {
181        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
182        llvm::raw_os_ostream cerr(std::cerr);
183        cerr << "Unlowered Pablo AST:\n";
184        PabloPrinter::print(*function, cerr);
185    }
186    if (EnableLowering || EnablePreDistribution || EnablePostDistribution || EnableMultiplexing) {
187        FactorizeDFG::transform(*function);
188    }
189#endif
190    if (PrintOptimizedREcode) {
191        PabloVerifier::verify(*function, "post-optimization");
192        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
193        llvm::raw_os_ostream cerr(std::cerr);
194        cerr << "Final Pablo AST:\n";
195        PabloPrinter::print(*function, cerr);
196    }
197}
198
199// Dynamic AVX2 confirmation
200#if (BLOCK_SIZE == 256)
201#define ISPC_LLVM_VERSION ISPC_LLVM_3_6
202#include "ispc.cpp"
203#endif
204
205
206IDISA::IDISA_Builder * GetNativeIDISA_Builder(Module * mod, Type * bitBlockType) {
207
208#if (BLOCK_SIZE == 256)
209    if ((strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
210        return new IDISA::IDISA_AVX2_Builder(mod, bitBlockType);
211        //std::cerr << "IDISA_AVX2_Builder selected\n";
212    }
213    else{
214        return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
215        //std::cerr << "Generic IDISA_Builder selected\n";
216    }
217#else   
218    return new IDISA::IDISA_SSE2_Builder(mod, bitBlockType);
219#endif
220}
221
222
223
224ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
225
226    InitializeNativeTarget();
227    InitializeNativeTargetAsmPrinter();
228    InitializeNativeTargetAsmParser();
229
230    std::string errMessage;
231    EngineBuilder builder(std::move(std::unique_ptr<Module>(m)));
232    builder.setErrorStr(&errMessage);
233    builder.setMCPU(sys::getHostCPUName());
234    builder.setOptLevel(CodeGenOpt::Level::None);
235
236#if (BLOCK_SIZE == 256)
237    if (!DisableAVX2 && (strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
238            std::vector<std::string> attrs;
239            attrs.push_back("avx2");
240            builder.setMAttrs(attrs);
241    //std::cerr << "+avx2 set" << std::endl;
242    }
243#endif
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
253extern "C" {
254    void wrapped_report_match(uint64_t recordNum, uint64_t recordStart, uint64_t recordEnd) {
255        printf("line %llu: (%llu, %llu)\n", recordNum, recordStart, recordEnd);
256    }
257}
258
259
260extern "C" {
261  void wrapped_print_register(char * regName, BitBlock bit_block) {
262      print_register<BitBlock>(regName, bit_block);
263  }
264}
265
266void icgrep_Linking(Module * m, ExecutionEngine * e) {
267    Module::FunctionListType & fns = m->getFunctionList();
268    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
269        std::string fnName = it->getName().str();
270        if (fnName == "s2p_block") continue;
271        if (fnName == "process_block") continue;
272        if (fnName == "process_block_initialize_carries") continue;
273       
274        if (fnName == "wrapped_print_register") {
275            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
276        }
277        if (fnName == "wrapped_report_match") {
278            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_match);
279        }
280#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
281        else {
282            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
283            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
284        }
285#endif
286    }
287}
288
Note: See TracBrowser for help on using the repository browser.