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

Last change on this file since 4888 was 4888, checked in by nmedfort, 3 years ago

Work on adding Multiplexing Window Size.

File size: 10.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/Support/SourceMgr.h>
21#include <llvm/Support/TargetSelect.h>
22#include <llvm/Support/Host.h>
23
24#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
25#include <UCD/precompiled_properties.h>
26#endif
27#include <re/re_cc.h>
28#include <re/re_nullable.h>
29#include <re/re_simplifier.h>
30#include <re/re_alt.h>
31#include <re/parsefailure.h>
32#include <re/re_parser.h>
33#include <re/re_compiler.h>
34#include <utf8_encoder.h>
35#include <cc/cc_compiler.h>
36#include <pablo/pablo_compiler.h>
37#include <pablo/optimizers/pablo_simplifier.hpp>
38#include <pablo/optimizers/codemotionpass.h>
39#include <pablo/passes/flattenassociativedfg.h>
40#include <pablo/passes/factorizedfg.h>
41#ifdef ENABLE_MULTIPLEXING
42#include <pablo/optimizers/pablo_automultiplexing.hpp>
43#include <pablo/optimizers/pablo_bddminimization.h>
44#include <pablo/optimizers/distributivepass.h>
45#endif
46#include <pablo/function.h>
47#include <pablo/analysis/pabloverifier.hpp>
48#include <re/printer_re.h>
49#include <pablo/printer_pablos.h>
50
51#include "do_grep.h"
52
53using namespace pablo;
54
55static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
56                                              "These options control printing of intermediate regular expression structures.");
57static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
58static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
59static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
60static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
61static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
62static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
63static cl::OptionCategory dPabloDumpOptions("Pablo Dump Options",
64                                            "These options control printing of intermediate Pablo code.");
65
66static cl::opt<bool> PrintOptimizedREcode("print-pablo", cl::init(false), cl::desc("print final optimized Pablo code"), cl::cat(dPabloDumpOptions));
67static cl::opt<bool> PrintCompiledCCcode("print-CC-pablo", cl::init(false), cl::desc("print Pablo output from character class compiler"), cl::cat(dPabloDumpOptions));
68static cl::opt<bool> PrintCompiledREcode("print-RE-pablo", cl::init(false), cl::desc("print Pablo output from the regular expression compiler"), cl::cat(dPabloDumpOptions));
69
70static cl::OptionCategory cPabloOptimizationsOptions("Pablo Optimizations", "These options control Pablo optimization passes.");
71
72static cl::opt<bool> DisablePabloCSE("disable-CSE", cl::init(false),
73                                     cl::desc("Disable Pablo common subexpression elimination/dead code elimination"),
74                                     cl::cat(cPabloOptimizationsOptions));
75static cl::opt<bool> PabloSinkingPass("sinking", cl::init(false),
76                                      cl::desc("Moves all instructions into the innermost legal If-scope so that they are only executed when needed."),
77                                      cl::cat(cPabloOptimizationsOptions));
78
79#ifdef ENABLE_MULTIPLEXING
80static cl::opt<bool> EnableMultiplexing("multiplexing", cl::init(true),
81                                        cl::desc("combine Advances whose inputs are mutual exclusive into the fewest number of advances possible (expensive)."),
82                                        cl::cat(cPabloOptimizationsOptions));
83
84static cl::opt<unsigned> MultiplexingSetLimit("multiplexing-set-limit", cl::init(std::numeric_limits<unsigned>::max()),
85                                        cl::desc("maximum size of any candidate multiplexing set."),
86                                        cl::cat(cPabloOptimizationsOptions));
87static cl::opt<unsigned> MultiplexingSelectionLimit("multiplexing-selection-limit", cl::init(100),
88                                        cl::desc("maximum number of selections from any partial candidate multiplexing set."),
89                                        cl::cat(cPabloOptimizationsOptions));
90static cl::opt<unsigned> MultiplexingWindowSize("multiplexing-window-size", cl::init(100),
91                                        cl::desc("maximum depth difference for computing mutual exclusion of Advance nodes."),
92                                        cl::cat(cPabloOptimizationsOptions));
93
94static cl::opt<bool> EnableLowering("lowering", cl::init(false),
95                                         cl::desc("coalesce associative functions prior to optimization passes."),
96                                         cl::cat(cPabloOptimizationsOptions));
97static cl::opt<bool> EnableDistribution("dist", cl::init(false),
98                                         cl::desc("apply distribution law optimization."),
99                                         cl::cat(cPabloOptimizationsOptions));
100#endif
101
102static cl::opt<bool> DisableAVX2("disable-AVX2", cl::init(false), cl::desc("disable AVX2 instruction set."), cl::cat(cPabloOptimizationsOptions));
103
104re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
105    if (PrintAllREs || PrintParsedREs) {
106        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
107    }
108
109    //Optimization passes to simplify the AST.
110    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
111    if (PrintAllREs || PrintStrippedREs) {
112        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
113    }
114    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
115    if (PrintAllREs || PrintStrippedREs) {
116        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
117    }
118
119    re_ast = re::RE_Simplifier::simplify(re_ast);
120    if (PrintAllREs || PrintSimplifiedREs) {
121        //Print to the terminal the AST that was generated by the simplifier.
122        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
123    }
124    return re_ast;
125}
126   
127PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
128    PabloFunction * function = PabloFunction::Create("process_block", 8, 2);
129    cc::CC_Compiler cc_compiler(*function, encoding);
130    re::RE_Compiler re_compiler(*function, cc_compiler);
131    re_compiler.initializeRequiredStreams();
132    re_compiler.compileUnicodeNames(re_ast);
133    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
134
135    if (PrintCompiledREcode) {
136        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
137        llvm::raw_os_ostream cerr(std::cerr);
138        cerr << "Initial Pablo AST:\n";
139        PabloPrinter::print(*function, cerr);
140    }
141    #ifndef NDEBUG
142    PabloVerifier::verify(*function, "creation");
143    #endif
144    return function;
145}
146
147void pablo_function_passes(PabloFunction * function) {
148    // Scan through the pablo code and perform DCE and CSE
149    if (!DisablePabloCSE) {
150        Simplifier::optimize(*function);
151    }
152#ifdef ENABLE_MULTIPLEXING
153    if (EnableLowering || EnableMultiplexing || EnableDistribution) {
154        FlattenAssociativeDFG::transform(*function);
155    }
156#endif
157    if (PabloSinkingPass) {
158        CodeMotionPass::optimize(*function);
159    }
160#ifdef ENABLE_MULTIPLEXING   
161    if (EnableMultiplexing) {
162        MultiplexingPass::optimize(*function, MultiplexingSetLimit, MultiplexingSelectionLimit, MultiplexingWindowSize);
163    }
164    if (EnableDistribution) {
165        DistributivePass::optimize(*function);
166    }
167    if (EnableLowering || EnableMultiplexing || EnableDistribution) {
168        FactorizeDFG::transform(*function);
169    }
170#endif
171    if (PrintOptimizedREcode) {
172        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
173        llvm::raw_os_ostream cerr(std::cerr);
174        cerr << "Final Pablo AST:\n";
175        PabloPrinter::print(*function, cerr);
176    }
177}
178
179ExecutionEngine * JIT_to_ExecutionEngine (llvm::Function * f) {
180
181    InitializeNativeTarget();
182    InitializeNativeTargetAsmPrinter();
183    InitializeNativeTargetAsmParser();
184
185    std::string errMessage;
186    EngineBuilder builder(std::move(std::unique_ptr<Module>(f->getParent())));
187    builder.setErrorStr(&errMessage);
188    builder.setMCPU(sys::getHostCPUName());
189    builder.setOptLevel(CodeGenOpt::Level::None);
190#if (BLOCK_SIZE == 256)
191    if (!DisableAVX2){
192            std::vector<std::string> attrs;
193            attrs.push_back("avx2");
194            builder.setMAttrs(attrs);
195    }
196#endif
197    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
198    ExecutionEngine * engine = builder.create();
199    if (engine == nullptr) {
200        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
201    }
202    //engine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
203    // engine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
204
205    return engine;
206}
207
208
209extern "C" {
210  void wrapped_print_register(char * regName, BitBlock bit_block) {
211      print_register<BitBlock>(regName, bit_block);
212  }
213}
214
215void icgrep_Linking(Module * m, ExecutionEngine * e) {
216    Module::FunctionListType & fns = m->getFunctionList();
217    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
218        std::string fnName = it->getName().str();
219        if (fnName == "process_block") continue;
220        if (fnName == "process_block_initialize_carries") continue;
221        if (fnName == "wrapped_print_register") {
222            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
223        }
224#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
225        else {
226            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
227            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
228        }
229#endif
230    }
231}
232
Note: See TracBrowser for help on using the repository browser.