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

Last change on this file was 6193, checked in by nmedfort, 10 months ago

Check-in of partially implemented DirectorySearch? kernel

File size: 3.2 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/source_kernel.h>
20#include <kernels/cc_kernel.h>
21#include <kernels/cc_scan_kernel.h>
22#include <boost/filesystem.hpp>
23#include <boost/iostreams/device/mapped_file.hpp>
24
25using namespace llvm;
26using namespace kernel;
27
28namespace re {class RE;}
29
30
31std::vector<size_t> LFPositions;
32extern "C" {
33void wrapped_report_pos(size_t match_pos, int dist) {
34        LFPositions.push_back(match_pos);
35    }
36
37}
38
39void preprocessPipeline(CPUEngineInstance & pxDriver){
40    auto & iBuilder = pxDriver.getBuilder();
41    Module * m = iBuilder->getModule();
42    Type * const size_ty = iBuilder->getSizeTy();
43    Type * const inputType = iBuilder->getInt8PtrTy();
44
45    Function * const mainFn = cast<Function>(m->getOrInsertFunction("Main", iBuilder->getVoidTy(), inputType, size_ty, inputType, nullptr));
46    mainFn->setCallingConv(CallingConv::C);
47    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", mainFn, 0));
48    Function::arg_iterator args = mainFn->arg_begin();
49    Value * const inputStream = &*(args++);
50    inputStream->setName("input");
51    Value * const fileSize = &*(args++);
52    fileSize->setName("fileSize");
53
54    const unsigned segmentSize = codegen::SegmentSize;
55    unsigned bufferSegments = codegen::BufferSegments;
56
57    StreamSetBuffer * ByteStream = pxDriver.addBuffer<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
58    kernel::Kernel * sourceK = pxDriver.addKernelInstance<kernel::MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy());
59    sourceK->setInitialArguments({inputStream, fileSize});
60    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
61
62    StreamSetBuffer * MatchResults = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments);
63    kernel::Kernel * linefeedK = pxDriver.addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(iBuilder, "linefeed", std::vector<re::CC *>{re::makeCC(0x0A)}, 1);
64    pxDriver.makeKernelCall(linefeedK, {ByteStream}, {MatchResults});
65   
66    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance<kernel::CCScanKernel>(iBuilder, 1);
67    pxDriver.makeKernelCall(scanMatchK, {MatchResults}, {}); 
68   
69    pxDriver.generatePipelineIR();
70    iBuilder->CreateRetVoid();
71
72    pxDriver.LinkFunction(*scanMatchK, "wrapped_report_pos", wrapped_report_pos);
73    pxDriver.finalizeObject();
74
75}
76
77typedef void (*preprocessFunctionType)(char * byte_data, size_t filesize);
78
79std::vector<size_t> preprocess(char * fileBuffer, size_t fileSize) {
80    CPUEngineInstance pxDriver("preprocess");
81    preprocessPipeline(pxDriver);
82    auto main = reinterpret_cast<preprocessFunctionType>(pxDriver.getMain());
83    main(fileBuffer, fileSize);
84    return LFPositions;
85}
Note: See TracBrowser for help on using the repository browser.