source: icGREP/icgrep-devel/icgrep/character_deposit.cpp @ 5997

Last change on this file since 5997 was 5986, checked in by nmedfort, 17 months ago

Fix for unexpected merge conflict

File size: 6.8 KB
Line 
1
2/*
3 *  Copyright (c) 2017 International Characters.
4 *  This software is licensed to the public under the Open Software License 3.0.
5 *  icgrep is a trademark of International Characters.
6 */
7
8#include <llvm/IR/Module.h>
9#include <llvm/IR/Function.h>
10#include <llvm/Linker/Linker.h>
11#include <llvm/Support/CommandLine.h>
12#include <llvm/Support/PrettyStackTrace.h>
13#include <llvm/Support/Signals.h>
14#include <llvm/Support/ManagedStatic.h>
15#include <toolchain/toolchain.h>
16
17#include <IR_Gen/idisa_target.h>
18#include <boost/filesystem.hpp>
19#include <boost/iostreams/device/mapped_file.hpp>
20
21#include <cc/cc_compiler.h>
22#include <kernels/cc_kernel.h>
23#include <kernels/streamset.h>
24#include <kernels/s2p_kernel.h>
25#include <kernels/source_kernel.h>
26#include <kernels/stdout_kernel.h>
27#include <kernels/swizzle.h>
28
29#include <kernels/kernel_builder.h>
30#include <kernels/p2s_kernel.h>
31#include <toolchain/cpudriver.h>
32#include <iostream>
33#include <fstream>
34#include <kernels/deletion.h>
35#include <kernels/pdep_kernel.h>
36
37namespace re { class CC; }
38
39using namespace llvm;
40using namespace parabix;
41using namespace kernel;
42
43static cl::OptionCategory lz4dFlags("Command Flags", "deletion options");
44//static cl::opt<char> characterToBeExtract(cl::Positional, cl::desc("<character to be extracted>"), cl::Required, cl::cat(lz4dFlags));
45static cl::opt<char> characterToBeDeposit(cl::Positional, cl::desc("<character to be depositted>"), cl::Required, cl::cat(lz4dFlags));
46static cl::opt<std::string> inputFile(cl::Positional, cl::desc("<input file>"), cl::Required, cl::cat(lz4dFlags));
47static cl::opt<std::string> outputFile(cl::Positional, cl::desc("<output file>"), cl::Required, cl::cat(lz4dFlags));
48
49
50typedef void (*MainFunctionType)(char * byte_data, size_t filesize);
51
52/*
53 * Usage:
54 *   character_deletion <character to be deleted> <input file name>
55 * It will delete the character from the input file and then print the output to stdout
56 * */
57
58int main(int argc, char *argv[]) {
59    // This boilerplate provides convenient stack traces and clean LLVM exit
60    // handling. It also initializes the built in support for convenient
61    // command line option handling.
62    sys::PrintStackTraceOnErrorSignal(argv[0]);
63    llvm::PrettyStackTraceProgram X(argc, argv);
64    llvm_shutdown_obj shutdown;
65    codegen::ParseCommandLineOptions(argc, argv, {&lz4dFlags, codegen::codegen_flags()});
66
67    std::string fileName = inputFile;
68
69    std::ifstream f(fileName, std::ios::binary | std::ios::ate);
70    if (f.fail()) {
71        return -1;
72    }
73    size_t mFilesize = f.tellg();
74
75    boost::iostreams::mapped_file_source mappedFile;
76    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
77    mappedFile.open(fileName , mFilesize);
78
79    char *fileBuffer = const_cast<char *>(mappedFile.data());
80
81    if (codegen::SegmentSize < 2) {
82        codegen::SegmentSize = 2;
83    }
84
85    const auto bufferSize = codegen::ThreadNum * codegen::SegmentSize;
86
87    ParabixDriver pxDriver("character_deletion");
88    auto & iBuilder = pxDriver.getBuilder();
89    Module * M = iBuilder->getModule();
90    Type * const sizeTy = iBuilder->getSizeTy();
91    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
92    Type * const voidTy = iBuilder->getVoidTy();
93    Type * const inputType = iBuilder->getInt8PtrTy();
94
95    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
96    main->setCallingConv(CallingConv::C);
97    Function::arg_iterator args = main->arg_begin();
98    Value* inputStream = &*(args++);
99    inputStream->setName("input");
100
101    Value* fileSize = &*(args++);
102    fileSize->setName("fileSize");
103
104    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
105
106
107    // GeneratePipeline
108    StreamSetBuffer * ByteStream = pxDriver.addBuffer<SourceBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
109    StreamSetBuffer * BasisBits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), bufferSize);
110
111    kernel::Kernel * sourceK = pxDriver.addKernelInstance<MemorySourceKernel>(iBuilder, iBuilder->getInt8PtrTy());
112    sourceK->setInitialArguments({inputStream, fileSize});
113    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
114    Kernel * s2pk = pxDriver.addKernelInstance<S2PKernel>(iBuilder);
115    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
116
117    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), bufferSize);
118    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "extenders", std::vector<re::CC *>{re::makeCC(characterToBeDeposit)}, 8);
119    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
120
121
122    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferSize);
123    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<CircularCopybackBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferSize);
124    Kernel * delK = pxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(iBuilder, 8);
125    pxDriver.makeKernelCall(delK, {CharacterMarkerBuffer, BasisBits}, {u16Swizzle0, u16Swizzle1});
126
127    StreamSetBuffer * depositedSwizzle0 = pxDriver.addBuffer<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferSize, 1);
128    Kernel * pdep0K = pxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4, "pdep0");
129    pxDriver.makeKernelCall(pdep0K, {CharacterMarkerBuffer, u16Swizzle0}, {depositedSwizzle0});
130
131
132    StreamSetBuffer * depositedSwizzle1 = pxDriver.addBuffer<DynamicBuffer>(iBuilder, iBuilder->getStreamSetTy(4), bufferSize, 1);
133    Kernel * pdep1K = pxDriver.addKernelInstance<PDEPkernel>(iBuilder, 4, "pdep1");
134    pxDriver.makeKernelCall(pdep1K, {CharacterMarkerBuffer, u16Swizzle1}, {depositedSwizzle1});
135
136    // Produce unswizzled bit streams
137    StreamSetBuffer * resultbits = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(8), bufferSize);
138    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
139
140    pxDriver.makeKernelCall(unSwizzleK, {depositedSwizzle0, depositedSwizzle1}, {resultbits});
141
142    StreamSetBuffer * const ResultBytes = pxDriver.addBuffer<CircularBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), bufferSize);
143    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
144    pxDriver.makeKernelCall(p2sK, {resultbits}, {ResultBytes});
145
146    Kernel * outK = pxDriver.addKernelInstance<FileSink>(iBuilder, 8);
147    outK->setInitialArguments({iBuilder->GetString(outputFile)});
148    pxDriver.makeKernelCall(outK, {ResultBytes}, {});
149
150    pxDriver.generatePipelineIR();
151
152    pxDriver.deallocateBuffers();
153
154    iBuilder->CreateRetVoid();
155
156    pxDriver.finalizeObject();
157
158
159    auto mainFunc = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
160
161
162    mainFunc(fileBuffer, mFilesize);
163
164    mappedFile.close();
165    return 0;
166}
Note: See TracBrowser for help on using the repository browser.