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

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

First stage of CC_NameMap removal

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