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

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

More work on n-ary operations.

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