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

Last change on this file since 4822 was 4822, checked in by nmedfort, 4 years ago

Added ability to limit the size of candidate multiplexing sets and choose a sample of the possible combinations.

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