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

Last change on this file since 4967 was 4967, checked in by lindanl, 3 years ago

Multiple input files can run on separate threads.

File size: 7.1 KB
Line 
1/*
2 *  Copyright (c) 2016 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#include <cstdio>
11
12
13#include <llvm/Support/SourceMgr.h>
14#include <llvm/Support/CommandLine.h>
15#include <llvm/Support/Debug.h>
16#include <llvm/Support/TargetSelect.h>
17#include <llvm/Support/Host.h>
18
19#include <re/re_re.h>
20#include <re/parsefailure.h>
21#include <re/re_parser.h>
22#include <re/re_any.h>
23#include <re/re_alt.h>
24
25#include <grep_engine.h>
26
27#include <boost/uuid/sha1.hpp>
28#include <toolchain.h>
29#include <atomic>
30
31static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
32                                       "These options control the regular expression source.");
33
34// static cl::OptionCategory bGrepOutputOptions("Output Options",
35//                                       "These options control the output.");
36
37static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
38
39// static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
40// static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
41// static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
42
43// static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
44// static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
45
46static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
47// static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
48// static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
49
50static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
51static 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));
52static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
53
54static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(1));
55
56
57
58static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
59static std::string allREs;
60static re::ModeFlagSet globalFlags = 0;
61
62re::RE * get_icgrep_RE() {
63 
64    //std::vector<std::string> regexVector;
65    if (RegexFilename != "") {
66        std::ifstream regexFile(RegexFilename.c_str());
67        std::string r;
68        if (regexFile.is_open()) {
69            while (std::getline(regexFile, r)) {
70                regexVector.push_back(r);
71            }
72            regexFile.close();
73        }
74    }
75   
76    // if there are no regexes specified through -e or -f, the first positional argument
77    // must be a regex, not an input file.
78   
79    if (regexVector.size() == 0) {
80        regexVector.push_back(inputFiles[0]);
81        firstInputFile = 1;
82    }
83    else {
84        firstInputFile = 0;
85    }
86   
87    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
88
89 
90    std::vector<re::RE *> REs;
91    re::RE * re_ast = nullptr;
92    for (unsigned i = 0; i < regexVector.size(); i++) {
93        re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
94        REs.push_back(re_ast);
95        allREs += regexVector[i] + "\n";
96    }
97    if (REs.size() > 1) {
98        re_ast = re::makeAlt(REs.begin(), REs.end());
99    }
100   
101    return re_ast;
102}
103
104std::string sha1sum(const std::string & str) {
105    char buffer[41];    // 40 hex-digits and the terminating null
106    unsigned int digest[5];     // 160 bits in total
107
108    boost::uuids::detail::sha1 sha1;
109    sha1.process_bytes(str.c_str(), str.size());
110    sha1.get_digest(digest);
111    snprintf(buffer, sizeof(buffer), "%.8x%.8x%.8x%.8x%.8x",
112             digest[0], digest[1], digest[2], digest[3], digest[4]);
113    return std::string(buffer);
114}
115
116GrepEngine grepEngine;
117std::atomic<int> fileCount;
118
119void *DoGrep(void *threadid)
120{
121    long tid;
122    tid = (long)threadid;
123    if (tid+1 < inputFiles.size())
124        grepEngine.doGrep(inputFiles[tid+1]);
125
126    pthread_exit(NULL);
127}
128
129int main(int argc, char *argv[]) {
130    StringMap<cl::Option*> Map;
131    cl::getRegisteredOptions(Map);
132    Map["time-passes"]->setHiddenFlag(cl::Hidden);
133    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
134    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
135    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
136    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
137    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
138    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
139    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
140    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
141    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
142    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
143    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
144    Map["regalloc"]->setHiddenFlag(cl::Hidden);
145    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
146    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
147    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
148    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
149    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
150    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
151    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
152    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
153    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
154    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
155
156    cl::ParseCommandLineOptions(argc, argv);
157   
158    re::RE * re_ast = get_icgrep_RE();
159    std::string module_name = "grepcode:" + sha1sum(allREs) + ":" + std::to_string(globalFlags);
160   
161    grepEngine.grepCodeGen(module_name, re_ast);
162
163
164    initResult(inputFiles, inputFiles.size());
165
166    if(Threads == 1){
167        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
168            grepEngine.doGrep(inputFiles[i]);
169        }       
170    }
171    else if (Threads > 1) {
172
173        pthread_t threads[100];
174        int rc;
175        long t;
176        void *status;
177
178        for(t=0; t<Threads; t++){
179          rc = pthread_create(&threads[t], NULL, DoGrep, (void *)t);
180          if (rc){
181             printf("ERROR; return code from pthread_create() is %d\n", rc);
182             exit(-1);
183          }
184        }
185
186        for(t=0; t<Threads; t++) {
187            rc = pthread_join(threads[t], &status);
188            if (rc) {
189                printf("ERROR; return code from pthread_join() is %d\n", rc);
190                exit(-1);
191            }
192        }
193    }
194    else {
195        std::cerr << "Invalid number of threads :" << Threads << std::endl;
196    }
197
198    PrintResult();   
199   
200    return 0;
201}
Note: See TracBrowser for help on using the repository browser.