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

Last change on this file since 5417 was 5406, checked in by cameron, 2 years ago

Progress towards adapting GPU code to ParabixDriver? framework, but need NVPTX_Driver subclass

File size: 3.6 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 <kernels/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 preprocessPipeline(ParabixDriver & pxDriver){
41    IDISA::IDISA_Builder * iBuilder = pxDriver.getIDISA_Builder();
42    Module * m = iBuilder->getModule();
43    Type * const size_ty = iBuilder->getSizeTy();
44    Type * const inputType = PointerType::get(ArrayType::get(iBuilder->getBitBlockType(), 1), 0);
45
46    Function * const mainFn = cast<Function>(m->getOrInsertFunction("LF", 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    Value * const lineBreak = &*(args++);
55    lineBreak->setName("lineBreak");
56
57    const unsigned segmentSize = codegen::SegmentSize;
58    unsigned bufferSegments = codegen::BufferSegments;
59
60    ExternalFileBuffer ByteStream(iBuilder, iBuilder->getStreamSetTy(1, 8));
61    ByteStream.setStreamSetBuffer(inputStream);
62    ExternalFileBuffer MatchResults(iBuilder, iBuilder->getStreamSetTy(1, 1), segmentSize * bufferSegments);
63    MatchResults.setStreamSetBuffer(lineBreak);
64
65    kernel::MMapSourceKernel mmapK(iBuilder, segmentSize); 
66    mmapK.setInitialArguments({fileSize});
67    pxDriver.addKernelCall(mmapK, {}, {&ByteStream});
68
69    kernel::DirectCharacterClassKernelBuilder linefeedK(iBuilder, "linefeed", {re::makeCC(0x0A)}, 1);
70    pxDriver.addKernelCall(linefeedK, {&ByteStream}, {&MatchResults});
71   
72    kernel::CCScanKernel scanMatchK(iBuilder, 1);
73    pxDriver.addKernelCall(scanMatchK, {&MatchResults}, {}); 
74   
75    pxDriver.generatePipelineIR();
76    iBuilder->CreateRetVoid();
77
78    pxDriver.addExternalLink(scanMatchK, "wrapped_report_pos", &wrapped_report_pos);
79    pxDriver.linkAndFinalize();
80
81}
82
83typedef void (*preprocessFunctionType)(char * byte_data, size_t filesize, char * lb);
84
85preprocessFunctionType preprocessCodeGen() {
86                           
87    LLVMContext TheContext;
88    Module * M = new Module("preprocess", TheContext);
89    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
90    ParabixDriver pxDriver(idb);
91
92    preprocessPipeline(pxDriver);
93   
94    preprocessFunctionType main = reinterpret_cast<preprocessFunctionType>(pxDriver.getPointerToMain());
95
96    delete idb;
97    return main;
98}
99
100std::vector<size_t> preprocess(char * fileBuffer, size_t fileSize, char * LineBreak) {
101    preprocessFunctionType preprocess_ptr = preprocessCodeGen();
102    preprocess_ptr(fileBuffer, fileSize, LineBreak);
103    return LFPositions;
104}
Note: See TracBrowser for help on using the repository browser.