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

Last change on this file since 4885 was 4871, checked in by nmedfort, 4 years ago

Minor improvements to the optimizers and AST manipulation.

File size: 7.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/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        re_ast = re::RE_Parser::parse(regexVector[i], globalFlags);
95        REs.push_back(re_ast);
96    }
97    if (REs.size() > 1) {
98        re_ast = re::makeAlt(REs.begin(), REs.end());
99    }
100   
101    return re_ast;
102}
103
104
105int main(int argc, char *argv[]) {
106    StringMap<cl::Option*> Map;
107    cl::getRegisteredOptions(Map);
108    Map["time-passes"]->setHiddenFlag(cl::Hidden);
109    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
110    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
111    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
112    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
113    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
114    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
115    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
116    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
117    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
118    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
119    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
120    Map["regalloc"]->setHiddenFlag(cl::Hidden);
121    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
122    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
123    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
124    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
125    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
126    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
127    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
128    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
129    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
130    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
131
132    cl::ParseCommandLineOptions(argc, argv);
133   
134    Encoding encoding(Encoding::Type::UTF_8, 8);
135
136    llvm::Function * icgrep_IR = nullptr;
137   
138    if (IRFileName == "") {       
139        re::RE * re_ast = get_icgrep_RE();
140        re_ast = regular_expression_passes(encoding, re_ast);
141       
142        pablo::PabloFunction * function = re2pablo_compiler(encoding, re_ast);
143
144        pablo_function_passes(function);
145        pablo::PabloCompiler pablo_compiler(VectorType::get(IntegerType::get(getGlobalContext(), 64), BLOCK_SIZE/64));
146        try {
147            icgrep_IR = pablo_compiler.compile(function);
148            delete function;
149            releaseSlabAllocatorMemory();
150        } catch (std::runtime_error e) {
151            delete function;
152            releaseSlabAllocatorMemory();
153            std::cerr << "Runtime error: " << e.what() << std::endl;
154            exit(1);
155        }
156    } else {
157        firstInputFile = 0;  // No regexp arguments; first positional argument is a file to process.
158        SMDiagnostic ParseErr;
159        Module * M = parseIRFile(IRFileName, ParseErr, getGlobalContext()).release();
160        if (!M) {
161            throw std::runtime_error("Error in Parsing IR File " + IRFileName);
162        }
163        icgrep_IR = M->getFunction("process_block");
164    }
165   
166    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(icgrep_IR);
167   
168    icgrep_Linking(icgrep_IR->getParent(), engine);
169   
170    // Ensure everything is ready to go.
171    engine->finalizeObject();
172   
173    // TODO getPointerToFunction() is deprecated. Investigate getFunctionAddress(string name) instead.
174    void * icgrep_init_carry_ptr = engine->getPointerToFunction(icgrep_IR->getParent()->getFunction("process_block_initialize_carries"));
175    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
176   
177    if (icgrep_MCptr) {
178        GrepExecutor grepEngine(icgrep_init_carry_ptr, icgrep_MCptr);
179        grepEngine.setCountOnlyOption(CountOnly);
180        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
181        grepEngine.setShowLineNumberOption(ShowLineNumbers);
182        if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
183            grepEngine.setShowFileNameOption();
184        }
185        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
186            grepEngine.doGrep(inputFiles[i]);
187        }
188    }
189   
190    //engine->freeMachineCodeForFunction(icgrep_IR); // Removed in LLVM 3.6. MC will be automatically freed in destructors.
191    delete engine;
192
193    return 0;
194}
Note: See TracBrowser for help on using the repository browser.