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

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

Progress on multi-target UCD compilation

File size: 10.3 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    //Add the UTF encoding.
114    if (encoding.getType() == Encoding::Type::UTF_8) {
115        re_ast = cc::UTF8_Encoder::toUTF8(nameMap, re_ast);
116        if (PrintAllREs || PrintUTF8REs) {
117            //Print to the terminal the AST that was generated by the utf8 encoder.
118            std::cerr << "UTF8-encoder:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
119            std::cerr << "NameMap:\n" << nameMap.printMap() << std::endl;
120        }
121    }
122   
123    re_ast = re::RE_Simplifier::simplify(re_ast);
124    if (PrintAllREs || PrintSimplifiedREs) {
125        //Print to the terminal the AST that was generated by the simplifier.
126        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
127    }
128    return re_ast;
129}
130   
131pablo::PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
132   
133    pablo::PabloFunction * function = pablo::PabloFunction::Create("process_block", 8, 2);
134   
135    cc::CC_Compiler cc_compiler(*function, encoding);
136   
137    cc_compiler.compileByteClasses(re_ast);
138   
139    if (PrintCompiledCCcode) {
140        //Print to the terminal the AST that was generated by the character class compiler.
141        llvm::raw_os_ostream cerr(std::cerr);
142        cerr << "CC AST:" << "\n";
143        PabloPrinter::print(function->getEntryBlock().statements(), cerr);
144    }
145
146    re::RE_Compiler re_compiler(*function, cc_compiler);
147    re_compiler.initializeRequiredStreams();
148    re_compiler.compileUnicodeNames(re_ast);
149    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
150
151    if (PrintCompiledREcode) {
152        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
153        llvm::raw_os_ostream cerr(std::cerr);
154        cerr << "Initial Pablo AST:\n";
155        PabloPrinter::print(function->getEntryBlock().statements(), cerr);
156    }
157#ifndef NDEBUG
158    pablo::PabloVerifier::verify(*function, "creation");
159#endif
160    return function;
161}
162
163void pablo_function_passes(pablo::PabloFunction * function) {
164    // Scan through the pablo code and perform DCE and CSE
165    if (!DisablePabloCSE) {
166        pablo::Simplifier::optimize(*function);
167    }
168    if (PabloSinkingPass) {
169        pablo::CodeSinking::optimize(*function);
170    }
171#ifdef ENABLE_MULTIPLEXING   
172    if (EnableMultiplexing) {
173        pablo::BDDMinimizationPass::optimize(*function);
174        pablo::AutoMultiplexing::optimize(*function);       
175    }   
176    if (EnableReassociation) {
177        pablo::BooleanReassociationPass::optimize(*function);
178    }
179#endif
180    if (PrintOptimizedREcode) {
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 << "Final Pablo AST:\n";
184        PabloPrinter::print(function->getEntryBlock().statements(), cerr);
185    }
186}
187
188
189
190ExecutionEngine * JIT_to_ExecutionEngine (llvm::Function * f) {
191
192    InitializeNativeTarget();
193    InitializeNativeTargetAsmPrinter();
194    InitializeNativeTargetAsmParser();
195
196    std::string errMessage;
197    EngineBuilder builder(std::move(std::unique_ptr<Module>(f->getParent())));
198    builder.setErrorStr(&errMessage);
199    builder.setMCPU(sys::getHostCPUName());
200    builder.setOptLevel(CodeGenOpt::Level::None);
201#if (BLOCK_SIZE == 256)
202    if(UseAVX2){
203            std::vector<std::string> attrs;
204            attrs.push_back("avx2");
205            builder.setMAttrs(attrs);
206    }
207#endif
208    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
209    ExecutionEngine * engine = builder.create();
210    if (engine == nullptr) {
211        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
212    }
213    //engine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
214    // engine->addGlobalMapping(externalFunction, proto->getFunctionPtr());
215
216    return engine;
217}
218
219
220extern "C" {
221  void wrapped_print_register(char * regName, BitBlock bit_block) {
222      print_register<BitBlock>(regName, bit_block);
223  }
224}
225
226void icgrep_Linking(Module * m, ExecutionEngine * e) {
227    Module::FunctionListType & fns = m->getFunctionList();
228    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
229        std::string fnName = it->getName().str();
230        if (fnName == "process_block") continue;
231        if (fnName == "wrapped_print_register") {
232            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_print_register);
233        }
234#ifndef DISABLE_PREGENERATED_UCD_FUNCTIONS
235        else {
236            const UCD::ExternalProperty & ep = UCD::resolveExternalProperty(fnName);
237            e->addGlobalMapping(cast<GlobalValue>(it), std::get<0>(ep));
238        }
239#endif
240    }
241}
242
Note: See TracBrowser for help on using the repository browser.