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

Last change on this file since 4900 was 4900, checked in by cameron, 3 years ago

Dynamic generation of s2p code

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