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

Last change on this file since 4887 was 4887, checked in by nmedfort, 3 years ago

Incorporated n-ary coalescing into DistributivePass?.

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