source: icGREP/icgrep-devel/icgrep/casefold.cpp @ 4992

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

Fix for first positional arg not a filename

File size: 8.0 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#include <sstream>
11
12#include <llvm/IR/Function.h>
13#include <llvm/IR/Module.h>
14#include <llvm/ExecutionEngine/ExecutionEngine.h>
15#include <llvm/ExecutionEngine/MCJIT.h>
16#include <llvm/IRReader/IRReader.h>
17#include <llvm/Support/CommandLine.h>
18#include <llvm/CodeGen/CommandFlags.h>
19#include <llvm/Support/SourceMgr.h>
20#include <llvm/Support/TargetSelect.h>
21#include <llvm/Support/Host.h>
22#include <llvm/Support/raw_ostream.h>
23
24#include <re/re_cc.h>
25#include <cc/cc_compiler.h>
26#include <pablo/function.h>
27#include <IDISA/idisa_builder.h>
28#include <IDISA/idisa_target.h>
29#include <kernels/casefold_pipeline.h>
30
31// Dynamic processor detection
32#define ISPC_LLVM_VERSION ISPC_LLVM_3_6
33#include <util/ispc.cpp>
34
35#include <utf_encoding.h>
36
37// mmap system
38#include <boost/filesystem.hpp>
39#include <boost/iostreams/device/mapped_file.hpp>
40using namespace boost::iostreams;
41using namespace boost::filesystem;
42
43#include <fcntl.h>
44
45static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
46
47
48static cl::OptionCategory cMachineCodeOptimization("Machine Code Optimizations", "These options control back-end compilier optimization levels.");
49
50static cl::opt<char> OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] (default = '-O0')"),
51                              cl::cat(cMachineCodeOptimization), cl::Prefix, cl::ZeroOrMore, cl::init('0'));
52
53
54//
55//  Functions taken from toolchain.cpp and modified for casefold
56//  JIT_t_ExecutionEngine : remove object cache
57//  icgrep_Linking:   unneeded?
58//  all others: definitely unneeded
59//
60
61ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
62
63    InitializeNativeTarget();
64    InitializeNativeTargetAsmPrinter();
65    InitializeNativeTargetAsmParser();
66
67    PassRegistry * Registry = PassRegistry::getPassRegistry();
68    initializeCore(*Registry);
69    initializeCodeGen(*Registry);
70    initializeLowerIntrinsicsPass(*Registry);
71
72    std::string errMessage;
73    EngineBuilder builder(std::move(std::unique_ptr<Module>(m)));
74    builder.setErrorStr(&errMessage);
75    builder.setMCPU(sys::getHostCPUName());
76    CodeGenOpt::Level optLevel = CodeGenOpt::Level::None;
77    switch (OptLevel) {
78        case '0': optLevel = CodeGenOpt::None; break;
79        case '1': optLevel = CodeGenOpt::Less; break;
80        case '2': optLevel = CodeGenOpt::Default; break;
81        case '3': optLevel = CodeGenOpt::Aggressive; break;
82        default: errs() << OptLevel << " is an invalid optimization level.\n";
83    }
84    builder.setOptLevel(optLevel);
85
86    if ((strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
87            std::vector<std::string> attrs;
88            attrs.push_back("avx2");
89            builder.setMAttrs(attrs);
90    }
91
92    // builder.selectTarget();
93
94    //builder.setOptLevel(mMaxWhileDepth ? CodeGenOpt::Level::Less : CodeGenOpt::Level::None);
95    ExecutionEngine * engine = builder.create();
96    if (engine == nullptr) {
97        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
98    }
99    return engine;
100}
101
102
103//
104//  Function replacing re2pablo_compiler.
105//
106
107pablo::PabloFunction * casefold2pablo(const Encoding encoding) {
108    pablo::PabloFunction * function = pablo::PabloFunction::Create("casefold_block", 8, 8);
109    cc::CC_Compiler cc_compiler(*function, encoding);
110   
111    pablo::PabloBuilder pBuilder(cc_compiler.getBuilder().getPabloBlock(), cc_compiler.getBuilder());
112    const std::vector<pablo::Var *> basis_bits = cc_compiler.getBasisBits();
113   
114   
115    pablo::PabloAST * alpha = cc_compiler.compileCC(re::makeCC(re::makeCC(0x41, 0x5A), re::makeCC(0x61,0x7A)));  // ASCII A-Z
116   
117   
118   
119    function->setResult(0, pBuilder.createAssign("b0", basis_bits[0]));
120    function->setResult(1, pBuilder.createAssign("b1", basis_bits[1]));
121    function->setResult(2, pBuilder.createAssign("b2", pBuilder.createXor(basis_bits[2], alpha)));
122    //function->setResult(2, pBuilder.createAssign("b2", basis_bits[2]));
123    function->setResult(3, pBuilder.createAssign("b3", basis_bits[3]));
124    function->setResult(4, pBuilder.createAssign("b4", basis_bits[4]));
125    function->setResult(5, pBuilder.createAssign("b5", basis_bits[5]));
126    function->setResult(6, pBuilder.createAssign("b6", basis_bits[6]));
127    function->setResult(7, pBuilder.createAssign("b7", basis_bits[7]));
128    return function;
129}
130
131
132
133typedef void (*casefoldFunctionType)(char * byte_data, size_t filesize);
134
135casefoldFunctionType caseFoldCodeGen(void) {
136                           
137    Module * M = new Module("casefold", getGlobalContext());
138   
139    IDISA::IDISA_Builder * idb = GetIDISA_Builder(M);
140
141    kernel::PipelineBuilder pipelineBuilder(M, idb);
142
143    Encoding encoding(Encoding::Type::UTF_8, 8);
144   
145    pablo::PabloFunction * function = casefold2pablo(encoding);
146   
147
148    pipelineBuilder.CreateKernels(function);
149
150    pipelineBuilder.ExecuteKernels();
151
152    //std::cerr << "ExecuteKernels(); done\n";
153    llvm::Function * main_IR = M->getFunction("Main");
154    ExecutionEngine * mEngine = JIT_to_ExecutionEngine(M);
155   
156    mEngine->finalizeObject();
157    //std::cerr << "finalizeObject(); done\n";
158
159    delete idb;
160
161    return reinterpret_cast<casefoldFunctionType>(mEngine->getPointerToFunction(main_IR));
162}
163
164void doCaseFold(casefoldFunctionType fn_ptr, const std::string & fileName) {
165    std::string mFileName = fileName;
166    size_t mFileSize;
167    char * mFileBuffer;
168   
169    const path file(mFileName);
170    if (exists(file)) {
171        if (is_directory(file)) {
172            return;
173        }
174    } else {
175        std::cerr << "Error: cannot open " << mFileName << " for processing. Skipped.\n";
176        return;
177    }
178   
179    mFileSize = file_size(file);
180    mapped_file mFile;
181    if (mFileSize == 0) {
182        mFileBuffer = nullptr;
183    }
184    else {
185        try {
186            mFile.open(mFileName, mapped_file::priv, mFileSize, 0);
187        } catch (std::ios_base::failure e) {
188            std::cerr << "Error: Boost mmap of " << mFileName << ": " << e.what() << std::endl;
189            return;
190        }
191        mFileBuffer = mFile.data();
192    }
193    //std::cerr << "mFileSize =" << mFileSize << "\n";
194    //std::cerr << "fn_ptr =" << std::hex << reinterpret_cast<intptr_t>(fn_ptr) << "\n";
195
196    fn_ptr(mFileBuffer, mFileSize);
197
198    mFile.close();
199   
200}
201
202
203int main(int argc, char *argv[]) {
204    StringMap<cl::Option*> Map;
205    cl::getRegisteredOptions(Map);
206    Map["time-passes"]->setHiddenFlag(cl::Hidden);
207    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
208    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
209    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
210    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
211    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
212    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
213    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
214    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
215    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
216    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
217    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
218    Map["regalloc"]->setHiddenFlag(cl::Hidden);
219    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
220    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
221    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
222    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
223    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
224    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
225    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
226    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
227    Map["x86-recip-refinement-steps"]->setHiddenFlag(cl::Hidden);
228    Map["rewrite-map-file"]->setHiddenFlag(cl::Hidden);
229
230    cl::ParseCommandLineOptions(argc, argv);
231
232    casefoldFunctionType fn_ptr = caseFoldCodeGen();
233
234    for (unsigned i = 0; i != inputFiles.size(); ++i) {
235        std::cerr << inputFiles[i] << " beginning\n";
236        doCaseFold(fn_ptr, inputFiles[i]);
237    }
238
239    return 0;
240}
241
242                       
Note: See TracBrowser for help on using the repository browser.