source: icGREP/icgrep-devel/icgrep/preprocess.cpp @ 5341

Last change on this file since 5341 was 5341, checked in by xuedongx, 2 years ago

LineBreak? Kernel for GPU.

File size: 4.1 KB
Line 
1/*
2 *  Copyright (c) 2016 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 <llvm/IR/Function.h>
11#include <llvm/IR/Module.h>
12#include <llvm/ExecutionEngine/ExecutionEngine.h>
13#include "llvm/Linker/Linker.h"
14#include <llvm/Support/CommandLine.h>
15#include <cc/cc_compiler.h>
16#include <IR_Gen/idisa_builder.h>
17#include <IR_Gen/idisa_target.h>
18#include <kernels/streamset.h>
19#include <kernels/mmap_kernel.h>
20#include <kernels/cc_kernel.h>
21#include <kernels/cc_scan_kernel.h>
22#include <kernels/pipeline.h>
23#include <boost/filesystem.hpp>
24#include <toolchain.h>
25#include <boost/iostreams/device/mapped_file.hpp>
26
27using namespace llvm;
28using namespace kernel;
29using namespace parabix;
30
31
32std::vector<size_t> LFPositions;
33extern "C" {
34void wrapped_report_pos(size_t match_pos, int dist) {
35        LFPositions.push_back(match_pos);
36        // std::cout << "pos: " << match_pos << "\n";
37    }
38
39}
40
41void preprocess_Linking(Module * m, ExecutionEngine * e) {
42    Module::FunctionListType & fns = m->getFunctionList();
43    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
44        std::string fnName = it->getName().str();
45        if (fnName == "wrapped_report_pos") {
46            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_pos);
47        }
48    }
49}
50
51
52Function * preprocessPipeline(Module * m, IDISA::IDISA_Builder * iBuilder){
53    Type * const size_ty = iBuilder->getSizeTy();
54    Type * const inputType = PointerType::get(ArrayType::get(iBuilder->getBitBlockType(), 1), 0);
55
56    Function * const mainFn = cast<Function>(m->getOrInsertFunction("LF", iBuilder->getVoidTy(), inputType, size_ty, inputType, nullptr));
57    mainFn->setCallingConv(CallingConv::C);
58    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", mainFn, 0));
59    Function::arg_iterator args = mainFn->arg_begin();
60    Value * const inputStream = &*(args++);
61    inputStream->setName("input");
62    Value * const fileSize = &*(args++);
63    fileSize->setName("fileSize");
64    Value * const lineBreak = &*(args++);
65    lineBreak->setName("lineBreak");
66
67    const unsigned segmentSize = codegen::SegmentSize;
68    unsigned bufferSegments = codegen::BufferSegments;
69
70    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
71    ByteStream.setStreamSetBuffer(inputStream, fileSize);
72    ExternalFileBuffer MatchResults(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments);
73    MatchResults.setStreamSetBuffer(lineBreak, fileSize);
74
75    kernel::MMapSourceKernel mmapK(iBuilder, segmentSize); 
76    mmapK.generateKernel({}, {&ByteStream});
77    mmapK.setInitialArguments({fileSize});
78
79    std::vector<re::CC *> charClasses;
80    charClasses.push_back(re::makeCC(0x0A));
81    kernel::DirectCharacterClassKernelBuilder linefeedK(iBuilder, "linefeed", charClasses, 1);
82    linefeedK.generateKernel({&ByteStream}, {&MatchResults});
83   
84    kernel::CCScanKernel scanMatchK(iBuilder, 1);
85    scanMatchK.generateKernel({&MatchResults}, {}); 
86   
87    generatePipelineLoop(iBuilder, {&mmapK, &linefeedK, &scanMatchK});
88    iBuilder->CreateRetVoid();
89
90    return mainFn;
91
92}
93
94typedef void (*preprocessFunctionType)(char * byte_data, size_t filesize, char * lb);
95
96preprocessFunctionType preprocessCodeGen() {
97                           
98    LLVMContext TheContext;
99    Module * M = new Module("preprocess", TheContext);
100    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
101
102    llvm::Function * main_IR = preprocessPipeline(M, idb);
103
104    ExecutionEngine * preprocessEngine = JIT_to_ExecutionEngine(M);
105    ApplyObjectCache(preprocessEngine);
106
107    preprocess_Linking(M, preprocessEngine);
108   
109    preprocessEngine->finalizeObject();
110
111    delete idb;
112    return reinterpret_cast<preprocessFunctionType>(preprocessEngine->getPointerToFunction(main_IR));
113}
114
115std::vector<size_t> preprocess(char * fileBuffer, size_t fileSize, char * LineBreak) {
116    preprocessFunctionType preprocess_ptr = preprocessCodeGen();
117    preprocess_ptr(fileBuffer, fileSize, LineBreak);
118    return LFPositions;
119}
Note: See TracBrowser for help on using the repository browser.