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

Last change on this file since 6047 was 6047, checked in by nmedfort, 12 months ago

Major refactoring of buffer types. Static buffers replace Circular and CircularCopyback?. External buffers unify Source/External?.

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;
27using namespace parabix;
28
29namespace re {class RE;}
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 preprocessPipeline(ParabixDriver & pxDriver){
41    auto & iBuilder = pxDriver.getBuilder();
42    Module * m = iBuilder->getModule();
43    Type * const size_ty = iBuilder->getSizeTy();
44    Type * const inputType = iBuilder->getInt8PtrTy();
45
46    Function * const mainFn = cast<Function>(m->getOrInsertFunction("Main", iBuilder->getVoidTy(), inputType, size_ty, inputType, nullptr));
47    mainFn->setCallingConv(CallingConv::C);
48    iBuilder->SetInsertPoint(BasicBlock::Create(m->getContext(), "entry", mainFn, 0));
49    Function::arg_iterator args = mainFn->arg_begin();
50    Value * const inputStream = &*(args++);
51    inputStream->setName("input");
52    Value * const fileSize = &*(args++);
53    fileSize->setName("fileSize");
54
55    const unsigned segmentSize = codegen::SegmentSize;
56    unsigned bufferSegments = codegen::BufferSegments;
57
58    StreamSetBuffer * ByteStream = pxDriver.addBuffer<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
59    kernel::Kernel * sourceK = pxDriver.addKernelInstance<kernel::MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy());
60    sourceK->setInitialArguments({inputStream, fileSize});
61    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
62
63    StreamSetBuffer * MatchResults = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments);
64    kernel::Kernel * linefeedK = pxDriver.addKernelInstance<kernel::DirectCharacterClassKernelBuilder>(iBuilder, "linefeed", std::vector<re::CC *>{re::makeCC(0x0A)}, 1);
65    pxDriver.makeKernelCall(linefeedK, {ByteStream}, {MatchResults});
66   
67    kernel::Kernel * scanMatchK = pxDriver.addKernelInstance<kernel::CCScanKernel>(iBuilder, 1);
68    pxDriver.makeKernelCall(scanMatchK, {MatchResults}, {}); 
69   
70    pxDriver.generatePipelineIR();
71    iBuilder->CreateRetVoid();
72
73    pxDriver.LinkFunction(*scanMatchK, "wrapped_report_pos", &wrapped_report_pos);
74    pxDriver.finalizeObject();
75
76}
77
78typedef void (*preprocessFunctionType)(char * byte_data, size_t filesize);
79
80std::vector<size_t> preprocess(char * fileBuffer, size_t fileSize) {
81    ParabixDriver pxDriver("preprocess");
82    preprocessPipeline(pxDriver);
83    auto main = reinterpret_cast<preprocessFunctionType>(pxDriver.getMain());
84    main(fileBuffer, fileSize);
85    return LFPositions;
86}
Note: See TracBrowser for help on using the repository browser.