source: icGREP/icgrep-devel/icgrep/icgrep.cpp @ 4907

Last change on this file since 4907 was 4907, checked in by cameron, 3 years ago

Kernels in icgrep: initial check-in

File size: 8.7 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 "toolchain.h"
13#include "utf_encoding.h"
14#include "pablo/pablo_compiler.h"
15#include <llvm/IR/Function.h>
16#include <llvm/IR/Type.h>
17#include <llvm/IR/Module.h>
18#include <llvm/ExecutionEngine/ExecutionEngine.h>
19#include <llvm/ExecutionEngine/MCJIT.h>
20#include <llvm/IRReader/IRReader.h>
21#include <llvm/Support/SourceMgr.h>
22#include <llvm/Support/CommandLine.h>
23#include <llvm/Support/Debug.h>
24#include <llvm/Support/TargetSelect.h>
25#include <llvm/Support/Host.h>
26#include <llvm/IR/Verifier.h>
27
28#include <kernels/s2p_gen.h>
29#include <kernels/scanmatchgen.h>
30
31#include <re/re_re.h>
32#include <re/parsefailure.h>
33#include <re/re_parser.h>
34#include <re/re_any.h>
35#include <re/re_alt.h>
36#include <pablo/function.h>
37
38#include "do_grep.h"
39
40static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
41                                       "These options control the regular expression source.");
42
43static cl::OptionCategory bGrepOutputOptions("Output Options",
44                                      "These options control the output.");
45
46static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
47
48static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
49static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
50static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
51
52static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
53static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
54
55static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
56static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
57static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
58
59static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
60static cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("regex file"), cl::init(""), cl::cat(aRegexSourceOptions));
61static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
62
63
64
65static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
66
67re::RE * get_icgrep_RE() {
68 
69    //std::vector<std::string> regexVector;
70    if (RegexFilename != "") {
71        std::ifstream regexFile(RegexFilename.c_str());
72        std::string r;
73        if (regexFile.is_open()) {
74            while (std::getline(regexFile, r)) {
75                regexVector.push_back(r);
76            }
77            regexFile.close();
78        }
79    }
80   
81    // if there are no regexes specified through -e or -f, the first positional argument
82    // must be a regex, not an input file.
83   
84    if (regexVector.size() == 0) {
85        regexVector.push_back(inputFiles[0]);
86        firstInputFile = 1;
87    }
88    else {
89        firstInputFile = 0;
90    }
91   
92    re::ModeFlagSet globalFlags = 0;
93    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
94
95 
96    std::vector<re::RE *> REs;
97    re::RE * re_ast = nullptr;
98    for (unsigned i = 0; i < regexVector.size(); i++) {
99        re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
100        REs.push_back(re_ast);
101    }
102    if (REs.size() > 1) {
103        re_ast = re::makeAlt(REs.begin(), REs.end());
104    }
105   
106    return re_ast;
107}
108
109
110int main(int argc, char *argv[]) {
111    StringMap<cl::Option*> Map;
112    cl::getRegisteredOptions(Map);
113    Map["time-passes"]->setHiddenFlag(cl::Hidden);
114    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
115    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
116    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
117    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
118    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
119    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
120    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
121    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
122    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
123    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
124    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
125    Map["regalloc"]->setHiddenFlag(cl::Hidden);
126    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
127    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
128    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
129    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
130    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
131    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
132    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
133    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
134    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
135    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
136
137    cl::ParseCommandLineOptions(argc, argv);
138   
139    Encoding encoding(Encoding::Type::UTF_8, 8);
140
141    llvm::Function * icgrep_IR = nullptr;
142   
143    Module * M = new Module("grepcode", getGlobalContext());
144   
145    IDISA::IDISA_Builder * idb = GetNativeIDISA_Builder(M, VectorType::get(IntegerType::get(getGlobalContext(), 64), BLOCK_SIZE/64));
146   
147    gen_s2p_function(M, idb);
148   
149   
150    generateScanBitBlockRoutine(M, idb, 64);
151   
152    if (IRFileName == "") {       
153        re::RE * re_ast = get_icgrep_RE();
154        re_ast = regular_expression_passes(encoding, re_ast);
155       
156        pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
157
158        pablo_function_passes(function);
159       
160       
161        pablo::PabloCompiler pablo_compiler(M, idb);
162        try {
163            icgrep_IR = pablo_compiler.compile(function);
164            delete function;
165            releaseSlabAllocatorMemory();
166        } catch (std::runtime_error e) {
167            delete function;
168            releaseSlabAllocatorMemory();
169            std::cerr << "Runtime error: " << e.what() << std::endl;
170            exit(1);
171        }
172    } else {
173        firstInputFile = 0;  // No regexp arguments; first positional argument is a file to process.
174        SMDiagnostic ParseErr;
175        M = parseIRFile(IRFileName, ParseErr, getGlobalContext()).release();
176        if (!M) {
177            throw std::runtime_error("Error in Parsing IR File " + IRFileName);
178        }
179        icgrep_IR = M->getFunction("process_block");
180    }
181    llvm::Function * s2p_IR = M->getFunction("s2p_block");
182   
183    llvm::Function * scanRoutine = M->getFunction("scan_matches_in_bitblock");
184   
185    if (s2p_IR == nullptr) {
186        std::cerr << "No s2p_IR!\n";
187        exit(1);
188    }
189   
190    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(M);
191   
192    icgrep_Linking(M, engine);
193    verifyModule(*M, &dbgs());
194   
195    // Ensure everything is ready to go.
196    engine->finalizeObject();
197   
198    // TODO getPointerToFunction() is deprecated. Investigate getFunctionAddress(string name) instead.
199    void * icgrep_init_carry_ptr = engine->getPointerToFunction(M->getFunction("process_block_initialize_carries"));
200    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
201    void * s2p_MCptr = engine->getPointerToFunction(s2p_IR);
202    void * scan_MCptr = engine->getPointerToFunction(scanRoutine);
203    if (s2p_MCptr == nullptr) {
204        std::cerr << "No s2p_MCptr!\n";
205        exit(1);
206    }
207   
208    if (icgrep_MCptr) {
209        GrepExecutor grepEngine(s2p_MCptr, icgrep_init_carry_ptr, icgrep_MCptr);
210        //GrepExecutor grepEngine(s2p_MCptr, icgrep_init_carry_ptr, icgrep_MCptr, scan_MCptr);
211        grepEngine.setCountOnlyOption(CountOnly);
212        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
213        grepEngine.setShowLineNumberOption(ShowLineNumbers);
214        if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
215            grepEngine.setShowFileNameOption();
216        }
217        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
218            grepEngine.doGrep(inputFiles[i]);
219        }
220    }
221   
222    //engine->freeMachineCodeForFunction(icgrep_IR); // Removed in LLVM 3.6. MC will be automatically freed in destructors.
223    delete engine;
224
225    return 0;
226}
Note: See TracBrowser for help on using the repository browser.