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

Last change on this file since 4853 was 4853, checked in by hongpum, 3 years ago

Updated some comments in response to API changes of LLVM 3.6

File size: 8.2 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/TargetSelect.h>
24#include <llvm/Support/Host.h>
25
26#include <re/re_re.h>
27#include <re/parsefailure.h>
28#include <re/re_parser.h>
29#include <re/re_any.h>
30#include <re/re_alt.h>
31#include <pablo/function.h>
32
33#include "do_grep.h"
34
35static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
36                                       "These options control the regular expression source.");
37
38static cl::OptionCategory bGrepOutputOptions("Output Options",
39                                      "These options control the output.");
40
41static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
42
43static cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
44static cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
45static cl::opt<bool> NormalizeLineBreaks("normalize-line-breaks", cl::desc("Normalize line breaks to std::endl."), cl::init(false),  cl::cat(bGrepOutputOptions));
46
47static cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
48static cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
49
50static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
51static cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
52static cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
53
54static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
55static 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));
56static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
57
58
59
60static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
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    re::ModeFlagSet globalFlags = 0;
88    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
89
90 
91    std::vector<re::RE *> REs;
92    re::RE * re_ast = nullptr;
93    for (unsigned i = 0; i < regexVector.size(); i++) {
94        try
95        {
96            re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
97        }
98        catch (ParseFailure failure)
99        {
100            std::cerr << "Regex parsing failure: " << failure.what() << std::endl;
101            std::cerr << regexVector[i] << std::endl;
102            exit(1);
103        }
104        catch (UCD::UnicodePropertyExpressionError e)
105        {
106            std::cerr << "Unicode error: " << e.what() << std::endl;
107            std::cerr << regexVector[i] << std::endl;
108            exit(1);
109        }
110        REs.push_back(re_ast);
111    }
112    if (REs.size() > 1) {
113        re_ast = re::makeAlt(REs.begin(), REs.end());
114    }
115   
116    return re_ast;
117}
118
119
120int main(int argc, char *argv[]) {
121    StringMap<cl::Option*> Map;
122    cl::getRegisteredOptions(Map);
123    Map["time-passes"]->setHiddenFlag(cl::Hidden);
124    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
125    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
126    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
127    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
128    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
129    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
130    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
131    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
132    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
133    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
134    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
135    Map["regalloc"]->setHiddenFlag(cl::Hidden);
136    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
137    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
138    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
139    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
140    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
141    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
142    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
143    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
144    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
145    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
146
147    cl::ParseCommandLineOptions(argc, argv);
148   
149    Encoding encoding(Encoding::Type::UTF_8, 8);
150
151    llvm::Function * icgrep_IR = nullptr;
152   
153    if (IRFileName == "") {       
154        re::RE * re_ast = get_icgrep_RE();
155        re_ast = regular_expression_passes(encoding, re_ast);
156       
157        pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
158
159        pablo_function_passes(function);
160        pablo::PabloCompiler pablo_compiler(VectorType::get(IntegerType::get(getGlobalContext(), 64), BLOCK_SIZE/64));
161        try {
162            icgrep_IR = pablo_compiler.compile(function);
163            releaseSlabAllocatorMemory();
164        }
165        catch (std::runtime_error e) {
166            releaseSlabAllocatorMemory();
167            std::cerr << "Runtime error: " << e.what() << std::endl;
168            exit(1);
169        }
170    }
171    else {
172        firstInputFile = 0;  // No regexp arguments; first positional argument is a file to process.
173        SMDiagnostic ParseErr;
174        Module * M = parseIRFile(IRFileName, ParseErr, getGlobalContext()).release();
175        if (!M) {
176            throw std::runtime_error("Error in Parsing IR File " + IRFileName);
177        }
178        icgrep_IR = M->getFunction("process_block");
179    }
180   
181    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(icgrep_IR);
182   
183    icgrep_Linking(icgrep_IR->getParent(), engine);
184   
185    // Ensure everything is ready to go.
186    engine->finalizeObject();
187   
188    // TODO getPointerToFunction() is deprecated. Investigate getFunctionAddress(string name) instead.
189    void * icgrep_init_carry_ptr = engine->getPointerToFunction(icgrep_IR->getParent()->getFunction("process_block_initialize_carries"));
190    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
191   
192    if (icgrep_MCptr) {
193        GrepExecutor grepEngine(icgrep_init_carry_ptr, icgrep_MCptr);
194        grepEngine.setCountOnlyOption(CountOnly);
195        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
196        grepEngine.setShowLineNumberOption(ShowLineNumbers);
197        if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
198            grepEngine.setShowFileNameOption();
199        }
200        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
201            grepEngine.doGrep(inputFiles[i]);
202        }
203    }
204   
205    //engine->freeMachineCodeForFunction(icgrep_IR); // Removed in LLVM 3.6. MC will be automatically freed in destructors.
206    delete engine;
207
208    return 0;
209}
Note: See TracBrowser for help on using the repository browser.