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

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

Added union/diff/intersection functionality to RE_Compiler. Removed toUTF8 pass in favour of using the UCD_Compiler.

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