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

Last change on this file since 5387 was 5377, checked in by nmedfort, 2 years ago

Support for stdin. Needs more testing.

File size: 4.0 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    }
37
38}
39
40void preprocess_Linking(Module * m, ExecutionEngine * e) {
41    Module::FunctionListType & fns = m->getFunctionList();
42    for (Module::FunctionListType::iterator it = fns.begin(), it_end = fns.end(); it != it_end; ++it) {
43        std::string fnName = it->getName().str();
44        if (fnName == "wrapped_report_pos") {
45            e->addGlobalMapping(cast<GlobalValue>(it), (void *)&wrapped_report_pos);
46        }
47    }
48}
49
50
51Function * preprocessPipeline(Module * m, IDISA::IDISA_Builder * iBuilder){
52    Type * const size_ty = iBuilder->getSizeTy();
53    Type * const inputType = PointerType::get(ArrayType::get(iBuilder->getBitBlockType(), 1), 0);
54
55    Function * const mainFn = cast<Function>(m->getOrInsertFunction("LF", iBuilder->getVoidTy(), inputType, size_ty, inputType, nullptr));
56    mainFn->setCallingConv(CallingConv::C);
57    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", mainFn, 0));
58    Function::arg_iterator args = mainFn->arg_begin();
59    Value * const inputStream = &*(args++);
60    inputStream->setName("input");
61    Value * const fileSize = &*(args++);
62    fileSize->setName("fileSize");
63    Value * const lineBreak = &*(args++);
64    lineBreak->setName("lineBreak");
65
66    const unsigned segmentSize = codegen::SegmentSize;
67    unsigned bufferSegments = codegen::BufferSegments;
68
69    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
70    ByteStream.setStreamSetBuffer(inputStream);
71    ExternalFileBuffer MatchResults(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments);
72    MatchResults.setStreamSetBuffer(lineBreak);
73
74    kernel::MMapSourceKernel mmapK(iBuilder, segmentSize); 
75    mmapK.generateKernel({}, {&ByteStream});
76    mmapK.setInitialArguments({fileSize});
77
78    kernel::DirectCharacterClassKernelBuilder linefeedK(iBuilder, "linefeed", {re::makeCC(0x0A)}, 1);
79    linefeedK.generateKernel({&ByteStream}, {&MatchResults});
80   
81    kernel::CCScanKernel scanMatchK(iBuilder, 1);
82    scanMatchK.generateKernel({&MatchResults}, {}); 
83   
84    generatePipelineLoop(iBuilder, {&mmapK, &linefeedK, &scanMatchK});
85    iBuilder->CreateRetVoid();
86
87    return mainFn;
88
89}
90
91typedef void (*preprocessFunctionType)(char * byte_data, size_t filesize, char * lb);
92
93preprocessFunctionType preprocessCodeGen() {
94                           
95    LLVMContext TheContext;
96    Module * M = new Module("preprocess", TheContext);
97    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
98
99    llvm::Function * main_IR = preprocessPipeline(M, idb);
100
101    ExecutionEngine * preprocessEngine = JIT_to_ExecutionEngine(M);
102    ApplyObjectCache(preprocessEngine);
103
104    preprocess_Linking(M, preprocessEngine);
105   
106    preprocessEngine->finalizeObject();
107
108    delete idb;
109    return reinterpret_cast<preprocessFunctionType>(preprocessEngine->getPointerToFunction(main_IR));
110}
111
112std::vector<size_t> preprocess(char * fileBuffer, size_t fileSize, char * LineBreak) {
113    preprocessFunctionType preprocess_ptr = preprocessCodeGen();
114    preprocess_ptr(fileBuffer, fileSize, LineBreak);
115    return LFPositions;
116}
Note: See TracBrowser for help on using the repository browser.