source: icGREP/icgrep-devel/icgrep/character_deletion.cpp @ 6184

Last change on this file since 6184 was 6184, checked in by nmedfort, 7 months ago

Initial version of PipelineKernel? + revised StreamSet? model.

File size: 9.2 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
36namespace re { class CC; }
37
38using namespace llvm;
39using namespace kernel;
40
41static cl::OptionCategory characterDeletionFlags("Command Flags", "deletion options");
42static cl::opt<char> characterToBeDeleted(cl::Positional, cl::desc("<character to be deletion>"), cl::Required, cl::cat(characterDeletionFlags));
43static cl::opt<std::string> inputFile(cl::Positional, cl::desc("<input file>"), cl::Required, cl::cat(characterDeletionFlags));
44static cl::opt<bool> useSwizzledDeletion("swizzle-deletion", cl::desc("Use swizzle deletion"), cl::init(false), cl::cat(characterDeletionFlags));
45
46
47typedef void (*MainFunctionType)(char * byte_data, size_t filesize);
48
49/*
50 * Usage:
51 *   character_deletion <character to be deleted> <input file name>
52 * It will delete the character from the input file and then print the output to stdout
53 * */
54
55StreamSetBuffer * loadBasisBits(CPUDriver & pxDriver, Value* inputStream, Value* fileSize, int inputBufferBlocks) {
56    auto & iBuilder = pxDriver.getBuilder();
57
58    StreamSetBuffer * ByteStream = pxDriver.addBuffer<ExternalBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8));
59    StreamSetBuffer * BasisBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8, 1), inputBufferBlocks);
60
61    kernel::Kernel * sourceK = pxDriver.addKernelInstance<MemorySourceKernel>(iBuilder);
62    sourceK->setInitialArguments({inputStream, fileSize});
63    pxDriver.makeKernelCall(sourceK, {}, {ByteStream});
64
65    Kernel * s2pk = pxDriver.addKernelInstance<S2PKernel>(iBuilder);
66    pxDriver.makeKernelCall(s2pk, {ByteStream}, {BasisBits});
67
68    return BasisBits;
69}
70
71StreamSetBuffer * generateSwizzledDeletion(CPUDriver & pxDriver, StreamSetBuffer * BasisBits, int inputBufferBlocks) {
72    auto & iBuilder = pxDriver.getBuilder();
73
74    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), inputBufferBlocks);
75    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "deletionMarker", std::vector<re::CC *>{re::makeCC(characterToBeDeleted)}, 8);
76
77    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
78
79    StreamSetBuffer * u16Swizzle0 = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks);
80    StreamSetBuffer * u16Swizzle1 = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(4), inputBufferBlocks);
81    Kernel * delK = pxDriver.addKernelInstance<SwizzledDeleteByPEXTkernel>(iBuilder, 8);
82    pxDriver.makeKernelCall(delK, {CharacterMarkerBuffer, BasisBits}, {u16Swizzle0, u16Swizzle1});
83
84    // Produce unswizzled bit streams
85    StreamSetBuffer * deletedBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
86    Kernel * unSwizzleK = pxDriver.addKernelInstance<SwizzleGenerator>(iBuilder, 8, 1, 2);
87
88    pxDriver.makeKernelCall(unSwizzleK, {u16Swizzle0, u16Swizzle1}, {deletedBits});
89    return deletedBits;
90}
91
92// TODO: It seems that there are still some bugs in DeleteByPEXTkernel
93StreamSetBuffer * generateDeletion(CPUDriver & pxDriver, StreamSetBuffer * BasisBits, int inputBufferBlocks) {
94    auto & iBuilder = pxDriver.getBuilder();
95
96    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), inputBufferBlocks);
97    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "deletionMarker", std::vector<re::CC *>{re::makeCC(characterToBeDeleted)}, 8);
98    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
99
100    StreamSetBuffer * deletedBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
101    StreamSetBuffer * deletionCounts = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
102
103    Kernel * delK = pxDriver.addKernelInstance<DeleteByPEXTkernel>(iBuilder, 64, 8);
104    pxDriver.makeKernelCall(delK, {BasisBits, CharacterMarkerBuffer}, {deletedBits, deletionCounts});
105
106    StreamSetBuffer * compressedBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
107    Kernel * streamCompressionK = pxDriver.addKernelInstance<StreamCompressKernel>(iBuilder, 64, 8);
108    pxDriver.makeKernelCall(streamCompressionK, {deletedBits, deletionCounts}, {compressedBits});
109
110    return compressedBits;
111}
112
113StreamSetBuffer * generateDeletionByCompression(CPUDriver & pxDriver, StreamSetBuffer * BasisBits, int inputBufferBlocks) {
114    auto & iBuilder = pxDriver.getBuilder();
115
116    StreamSetBuffer * const CharacterMarkerBuffer = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 1), inputBufferBlocks);
117    Kernel * ccK = pxDriver.addKernelInstance<ParabixCharacterClassKernelBuilder>(iBuilder, "deletionMarker", std::vector<re::CC *>{re::subtractCC(re::makeByte(0, 255), re::makeCC(characterToBeDeleted))}, 8);
118    pxDriver.makeKernelCall(ccK, {BasisBits}, {CharacterMarkerBuffer});
119
120    #warning TODO: replace StreamFilterCompiler with the deposit method
121
122    StreamSetBuffer * compressedBits = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(8), inputBufferBlocks);
123    StreamFilterCompiler filterCompiler(pxDriver, iBuilder->getStreamSetTy(8), inputBufferBlocks);
124    filterCompiler.makeCall(CharacterMarkerBuffer, BasisBits, compressedBits);
125    return compressedBits;
126}
127
128
129int main(int argc, char *argv[]) {
130    // This boilerplate provides convenient stack traces and clean LLVM exit
131    // handling. It also initializes the built in support for convenient
132    // command line option handling.
133    sys::PrintStackTraceOnErrorSignal(argv[0]);
134    llvm::PrettyStackTraceProgram X(argc, argv);
135    llvm_shutdown_obj shutdown;
136    codegen::ParseCommandLineOptions(argc, argv, {&characterDeletionFlags, codegen::codegen_flags()});
137
138    const std::string fileName = inputFile;
139
140    std::ifstream f(fileName, std::ios::binary | std::ios::ate);
141    if (f.fail()) {
142        return -1;
143    }
144    size_t mFilesize = f.tellg();
145
146    boost::iostreams::mapped_file_source mappedFile;
147    // Since mmap offset has to be multiples of pages, we can't use it to skip headers.
148    mappedFile.open(fileName , mFilesize);
149
150    char *fileBuffer = const_cast<char *>(mappedFile.data());
151
152    const int inputBufferBlocks = codegen::BufferSegments * codegen::ThreadNum * 16;
153
154    CPUDriver pxDriver("character_deletion");
155    auto & iBuilder = pxDriver.getBuilder();
156    Module * M = iBuilder->getModule();
157    Type * const sizeTy = iBuilder->getSizeTy();
158    Type * const boolTy = iBuilder->getIntNTy(sizeof(bool) * 8);
159    Type * const voidTy = iBuilder->getVoidTy();
160    Type * const inputType = iBuilder->getInt8PtrTy();
161
162    Function * const main = cast<Function>(M->getOrInsertFunction("Main", voidTy, inputType, sizeTy, sizeTy, boolTy, nullptr));
163    main->setCallingConv(CallingConv::C);
164    Function::arg_iterator args = main->arg_begin();
165    Value* inputStream = &*(args++);
166    inputStream->setName("input");
167
168    Value* fileSize = &*(args++);
169    fileSize->setName("fileSize");
170
171    iBuilder->SetInsertPoint(BasicBlock::Create(M->getContext(), "entry", main, 0));
172
173    // GeneratePipeline
174    StreamSetBuffer * BasisBits = loadBasisBits(pxDriver, inputStream, fileSize, inputBufferBlocks);
175    StreamSetBuffer * deletedBits = NULL;
176    if (useSwizzledDeletion) {
177        deletedBits = generateSwizzledDeletion(pxDriver, BasisBits, inputBufferBlocks);
178    } else {
179        deletedBits = generateDeletionByCompression(pxDriver, BasisBits, inputBufferBlocks);
180    }
181//    StreamSetBuffer * deletedBits = generateDeletion(pxDriver, BasisBits, inputBufferBlocks);
182
183    StreamSetBuffer * const deletedByteStream = pxDriver.addBuffer<StaticBuffer>(iBuilder, iBuilder->getStreamSetTy(1, 8), inputBufferBlocks);
184    Kernel * p2sK = pxDriver.addKernelInstance<P2SKernel>(iBuilder);
185    pxDriver.makeKernelCall(p2sK, {deletedBits}, {deletedByteStream});
186
187    // --------------------------------------------------------
188    // End
189
190    Kernel * outK = pxDriver.addKernelInstance<StdOutKernel>(iBuilder, 8);
191    pxDriver.makeKernelCall(outK, {deletedByteStream}, {});
192    pxDriver.generatePipelineIR();
193    iBuilder->CreateRetVoid();
194
195    pxDriver.finalizeObject();
196
197
198    auto mainFunc = reinterpret_cast<MainFunctionType>(pxDriver.getMain());
199
200    mainFunc(fileBuffer, mFilesize);
201
202    mappedFile.close();
203    return 0;
204}
Note: See TracBrowser for help on using the repository browser.