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

Last change on this file since 4839 was 4839, checked in by cameron, 4 years ago

Parameterize PabloCompiler? on bitBlockType

File size: 8.1 KB
RevLine 
[3850]1/*
[4544]2 *  Copyright (c) 2015 International Characters.
[3850]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
[4327]7#include <string>
[4328]8#include <iostream>
9#include <fstream>
[4327]10
[4803]11#include "basis_bits.h"
[4801]12#include "toolchain.h"
[3850]13#include "utf_encoding.h"
[4324]14#include "pablo/pablo_compiler.h"
[4730]15#include <llvm/IR/Function.h>
[4839]16#include <llvm/IR/Type.h>
[4732]17#include <llvm/IR/Module.h>
[4730]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>
[4734]25
[4801]26#include <re/re_re.h>
[4734]27#include <re/parsefailure.h>
28#include <re/re_parser.h>
[4801]29#include <re/re_any.h>
30#include <re/re_alt.h>
[4734]31#include <pablo/function.h>
[4730]32
[4324]33#include "do_grep.h"
[3850]34
[4544]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));
[4734]56static cl::opt<std::string> IRFileName("precompiled", cl::desc("Use precompiled regular expression"), cl::value_desc("LLVM IR file"), cl::init(""), cl::cat(aRegexSourceOptions));
[4544]57
58
[4730]59
[4750]60static unsigned firstInputFile = 1;  // Normal case when first positional arg is a regex.
[4734]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;
[4750]93    for (unsigned i = 0; i < regexVector.size(); i++) {
[4734]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        }
[4737]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        }
[4734]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
[4775]119
[4325]120int main(int argc, char *argv[]) {
[4353]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);
[4572]144    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
145    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
146
[4544]147    cl::ParseCommandLineOptions(argc, argv);
[4353]148   
[4325]149    Encoding encoding(Encoding::Type::UTF_8, 8);
[3914]150
[4734]151    llvm::Function * icgrep_IR = nullptr;
[4328]152   
[4734]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);
[4839]160        pablo::PabloCompiler pablo_compiler(VectorType::get(IntegerType::get(getGlobalContext(), 64), BLOCK_SIZE/64));
[4734]161        try {
162            icgrep_IR = pablo_compiler.compile(function);
163            releaseSlabAllocatorMemory();
[4328]164        }
[4734]165        catch (std::runtime_error e) {
166            releaseSlabAllocatorMemory();
167            std::cerr << "Runtime error: " << e.what() << std::endl;
168            exit(1);
169        }
[4328]170    }
171    else {
[4734]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");
[4328]179    }
180   
[4731]181    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(icgrep_IR);
[4730]182   
[4731]183    icgrep_Linking(icgrep_IR->getParent(), engine);
[4730]184   
[4731]185    // Ensure everything is ready to go.
186    engine->finalizeObject();
[4730]187   
[4826]188    void * icgrep_init_carry_ptr = engine->getPointerToFunction(icgrep_IR->getParent()->getFunction("process_block_initialize_carries"));
[4731]189    void * icgrep_MCptr = engine->getPointerToFunction(icgrep_IR);
190   
191    if (icgrep_MCptr) {
[4826]192        GrepExecutor grepEngine(icgrep_init_carry_ptr, icgrep_MCptr);
[4325]193        grepEngine.setCountOnlyOption(CountOnly);
[4460]194        grepEngine.setNormalizeLineBreaksOption(NormalizeLineBreaks);
[4327]195        grepEngine.setShowLineNumberOption(ShowLineNumbers);
[4328]196            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
197            grepEngine.setShowFileNameOption();
198        }
199        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
[4788]200            grepEngine.doGrep(inputFiles[i]);
[4325]201        }
[3850]202    }
[4327]203   
[4731]204    //engine->freeMachineCodeForFunction(icgrep_IR); // This function only prints a "not supported" message. Reevaluate with LLVM 3.6.
[4730]205    delete engine;
206
[3850]207    return 0;
208}
Note: See TracBrowser for help on using the repository browser.