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

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

Work on lowering + some timing and papi information that will be cleaned up later.

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