source: icGREP/icgrep-devel/icgrep/kernels/p2s_kernel.cpp @ 5257

Last change on this file since 5257 was 5247, checked in by cameron, 3 years ago

Separate processedItemCounts and producedItemCounts for each stream set

File size: 10.8 KB
Line 
1#include "p2s_kernel.h"
2#include "kernels/kernel.h"
3#include "IR_Gen/idisa_builder.h"
4#include <llvm/IR/Type.h>
5#include <iostream>
6#include <stdint.h>
7#include <llvm/Support/FileSystem.h>
8#include <llvm/Support/raw_ostream.h>
9
10
11
12namespace kernel{
13       
14void p2s_step(IDISA::IDISA_Builder * iBuilder, Value * p0, Value * p1, Value * hi_mask, unsigned shift, Value * &s1, Value * &s0) {
15    Value * t0 = iBuilder->simd_if(1, hi_mask, p0, iBuilder->simd_srli(16, p1, shift));
16    Value * t1 = iBuilder->simd_if(1, hi_mask, iBuilder->simd_slli(16, p0, shift), p1);
17    s1 = iBuilder->esimd_mergeh(8, t1, t0);
18    s0 = iBuilder->esimd_mergel(8, t1, t0);
19}
20
21inline void p2s(IDISA::IDISA_Builder * iBuilder, Value * p[], Value * s[]) {
22    Value * bit00004444[2];
23    Value * bit22226666[2];
24    Value * bit11115555[2];
25    Value * bit33337777[2];
26    p2s_step(iBuilder, p[0], p[4], iBuilder->simd_himask(8), 4, bit00004444[1], bit00004444[0]);
27    p2s_step(iBuilder, p[1], p[5], iBuilder->simd_himask(8), 4, bit11115555[1], bit11115555[0]);
28    p2s_step(iBuilder, p[2], p[6], iBuilder->simd_himask(8), 4, bit22226666[1], bit22226666[0]);
29    p2s_step(iBuilder, p[3], p[7], iBuilder->simd_himask(8), 4, bit33337777[1], bit33337777[0]);
30
31    Value * bit00224466[4];
32    Value * bit11335577[4];
33    for (unsigned j = 0; j<2; j++) {
34        p2s_step(iBuilder, bit00004444[j], bit22226666[j],iBuilder->simd_himask(4), 2, bit00224466[2*j+1], bit00224466[2*j]);
35        p2s_step(iBuilder, bit11115555[j], bit33337777[j],iBuilder->simd_himask(4), 2, bit11335577[2*j+1], bit11335577[2*j]);
36    }
37    for (unsigned j = 0; j<4; j++) {
38        p2s_step(iBuilder, bit00224466[j], bit11335577[j], iBuilder->simd_himask(2), 1, s[2*j+1], s[2*j]);
39    }
40}
41               
42void p2sKernel::generateDoBlockMethod() const {
43    auto savePoint = iBuilder->saveIP();
44    Module * m = iBuilder->getModule();
45   
46    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
47   
48    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
49   
50    Value * self = getParameter(doBlockFunction, "self");
51    Value * blockNo = getScalarField(self, blockNoScalar);
52    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
53    Value * byteStreamBlock_ptr = getStreamSetBlockPtr(self, "byteStream", blockNo);
54
55    Value * p_bitblock[8];
56    for (unsigned i = 0; i < 8; i++) {
57        p_bitblock[i] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(i)});
58    }
59    Value * s_bytepack[8];
60    p2s(iBuilder, p_bitblock, s_bytepack);
61    for (unsigned j = 0; j < 8; ++j) {
62        iBuilder->CreateBlockAlignedStore(s_bytepack[j], byteStreamBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(j)});
63    }
64    iBuilder->CreateRetVoid();
65    iBuilder->restoreIP(savePoint);
66}
67       
68void p2sKernel_withCompressedOutput::generateDoBlockMethod() const {
69    auto savePoint = iBuilder->saveIP();
70    Module * m = iBuilder->getModule();
71    Type * i8PtrTy = iBuilder->getInt8PtrTy(); 
72    Type * i32 = iBuilder->getIntNTy(32); 
73    Type * bitBlockPtrTy = llvm::PointerType::get(iBuilder->getBitBlockType(), 0); 
74   
75    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
76   
77    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
78    Value * self = getParameter(doBlockFunction, "self");
79    Value * blockNo = getScalarField(self, blockNoScalar);
80    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
81    Value * delCountBlock_ptr = getStreamSetBlockPtr(self, "deletionCounts", blockNo);
82    Value * byteStreamBlock_ptr = getStreamSetBlockPtr(self, "byteStream", blockNo);
83   
84    Value * basisBits[8];
85    for (unsigned i = 0; i < 8; i++) {
86        basisBits[i] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(i)});
87    }
88    Value * bytePack[8];
89    p2s(iBuilder, basisBits, bytePack);
90   
91    unsigned units_per_register = iBuilder->getBitBlockWidth()/8;
92   
93    Value * unit_counts = iBuilder->fwCast(units_per_register, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0)}));
94   
95    Value * output_ptr = iBuilder->CreateBitCast(byteStreamBlock_ptr, i8PtrTy);
96    Value * offset = ConstantInt::get(i32, 0);
97   
98    for (unsigned j = 0; j < 8; ++j) {
99        iBuilder->CreateAlignedStore(bytePack[j], iBuilder->CreateBitCast(iBuilder->CreateGEP(output_ptr, offset), bitBlockPtrTy), 1);
100        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(j)), i32);
101    }
102    iBuilder->CreateRetVoid();
103    iBuilder->restoreIP(savePoint);
104}
105
106void p2s_16Kernel::generateDoBlockMethod() const {
107    auto savePoint = iBuilder->saveIP();
108    Module * m = iBuilder->getModule();
109   
110    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
111   
112    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
113    Value * self = getParameter(doBlockFunction, "self");
114    Value * blockNo = getScalarField(self, blockNoScalar);
115    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
116    Value * i16StreamBlock_ptr = getStreamSetBlockPtr(self, "i16Stream", blockNo);
117   
118    Value * hi_input[8];
119    for (unsigned j = 0; j < 8; ++j) {
120        hi_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(j)});
121    }
122    Value * hi_bytes[8];
123    p2s(iBuilder, hi_input, hi_bytes);
124   
125    Value * lo_input[8];
126    for (unsigned j = 0; j < 8; ++j) {
127        lo_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(j+8)});
128    }
129    Value * lo_bytes[8];
130    p2s(iBuilder, lo_input, lo_bytes);
131   
132    for (unsigned j = 0; j < 8; ++j) {
133        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
134        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
135        // iBuilder->getInt32(0),
136        iBuilder->CreateBlockAlignedStore(merge0, i16StreamBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(2*j)});
137        iBuilder->CreateBlockAlignedStore(merge1, i16StreamBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(2*j+1)});
138    }
139    iBuilder->CreateRetVoid();
140    iBuilder->restoreIP(savePoint);
141}
142
143void p2s_16Kernel_withCompressedOutput::generateDoBlockMethod() const {
144    auto savePoint = iBuilder->saveIP();
145    Module * m = iBuilder->getModule();
146    Type * i32 = iBuilder->getIntNTy(32);
147    Type * bitBlockPtrTy = llvm::PointerType::get(iBuilder->getBitBlockType(), 0);
148
149    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
150
151    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
152    Constant * stride = iBuilder->getSize(iBuilder->getStride());
153
154    Value * self = getParameter(doBlockFunction, "self");
155    Value * blockNo = getScalarField(self, blockNoScalar);
156    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
157    Value * delCountBlock_ptr = getStreamSetBlockPtr(self, "deletionCounts", blockNo);
158    Value * i16UnitsGenerated = getProducedItemCount(self, "i16Stream"); // units generated to buffer
159    Value * i16BlockNo = iBuilder->CreateUDiv(i16UnitsGenerated, stride);
160
161    Value * i16StreamBase_ptr = iBuilder->CreateBitCast(getStreamSetBlockPtr(self, "i16Stream", i16BlockNo), PointerType::get(iBuilder->getInt16Ty(), 0));
162
163    Value * u16_output_ptr = iBuilder->CreateGEP(i16StreamBase_ptr, iBuilder->CreateURem(i16UnitsGenerated, stride));
164
165
166    Value * hi_input[8];
167    for (unsigned j = 0; j < 8; ++j) {
168        hi_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
169    }
170    Value * hi_bytes[8];
171    p2s(iBuilder, hi_input, hi_bytes);
172
173    Value * lo_input[8];
174    for (unsigned j = 0; j < 8; ++j) {
175        lo_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j+8)});
176    }
177    Value * lo_bytes[8];
178    p2s(iBuilder, lo_input, lo_bytes);
179
180    const auto UTF_16_units_per_register = iBuilder->getBitBlockWidth() / 16;
181
182    Value * unit_counts = iBuilder->fwCast(UTF_16_units_per_register, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0)}));
183
184    Value * offset = ConstantInt::get(i32, 0);
185
186    for (unsigned j = 0; j < 8; ++j) {
187        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
188        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
189        iBuilder->CreateAlignedStore(merge0, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
190        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2*j)), i32);
191        iBuilder->CreateAlignedStore(merge1, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
192        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2*j+1)), i32);
193    }
194
195    i16UnitsGenerated = iBuilder->CreateAdd(i16UnitsGenerated, iBuilder->CreateZExt(offset, iBuilder->getSizeTy()));
196    setProducedItemCount(self, "i16Stream", i16UnitsGenerated);
197    iBuilder->CreateRetVoid();
198    iBuilder->restoreIP(savePoint);
199}
200
201void p2s_16Kernel_withCompressedOutput::generateFinalBlockMethod() const {
202    auto savePoint = iBuilder->saveIP();
203    Module * m = iBuilder->getModule();
204    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
205    Function * finalBlockFunction = m->getFunction(mKernelName + finalBlock_suffix);
206    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "fb_entry", finalBlockFunction, 0));
207    // Final Block arguments: self, remaining, then the standard DoBlock args.
208    Function::arg_iterator args = finalBlockFunction->arg_begin();
209    Value * self = &*(args++);
210    /* Skip "remaining" arg */ args++;
211    std::vector<Value *> doBlockArgs = {self};
212    while (args != finalBlockFunction->arg_end()){
213        doBlockArgs.push_back(&*args++);
214    }
215    Value * i16UnitsGenerated = getProducedItemCount(self, "i16Stream"); // units generated to buffer
216
217    iBuilder->CreateCall(doBlockFunction, doBlockArgs);
218    i16UnitsGenerated = getProducedItemCount(self, "i16Stream"); // units generated to buffer
219    for (unsigned i = 0; i < mStreamSetOutputs.size(); i++) {
220        Value * ssStructPtr = getStreamSetStructPtr(self, mStreamSetOutputs[i].name);
221        Value * producerPosPtr = mStreamSetOutputBuffers[i]->getProducerPosPtr(ssStructPtr);
222        iBuilder->CreateAtomicStoreRelease(i16UnitsGenerated, producerPosPtr);
223    }
224    iBuilder->CreateRetVoid();
225    iBuilder->restoreIP(savePoint);
226}
227   
228   
229}
Note: See TracBrowser for help on using the repository browser.