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

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

Refined copy-back buffers, use copyback in p2s_compressed; stdout/filesink mods for circular buffers

File size: 9.8 KB
Line 
1#include "p2s_kernel.h"
2#include "IR_Gen/idisa_builder.h"  // for IDISA_Builder
3#include "llvm/IR/Constant.h"      // for Constant
4#include "llvm/IR/Constants.h"     // for ConstantInt
5#include "llvm/IR/DerivedTypes.h"  // for PointerType, VectorType
6#include "llvm/IR/Function.h"      // for Function, Function::arg_iterator
7#include <llvm/IR/Module.h>
8#include <kernels/streamset.h>
9namespace llvm { class Value; }
10
11using namespace llvm;
12using namespace parabix;
13
14namespace kernel{
15       
16void p2s_step(IDISA::IDISA_Builder * iBuilder, Value * p0, Value * p1, Value * hi_mask, unsigned shift, Value * &s1, Value * &s0) {
17    Value * t0 = iBuilder->simd_if(1, hi_mask, p0, iBuilder->simd_srli(16, p1, shift));
18    Value * t1 = iBuilder->simd_if(1, hi_mask, iBuilder->simd_slli(16, p0, shift), p1);
19    s1 = iBuilder->esimd_mergeh(8, t1, t0);
20    s0 = iBuilder->esimd_mergel(8, t1, t0);
21}
22
23inline void p2s(IDISA::IDISA_Builder * iBuilder, Value * p[], Value * s[]) {
24    Value * bit00004444[2];
25    Value * bit22226666[2];
26    Value * bit11115555[2];
27    Value * bit33337777[2];
28    p2s_step(iBuilder, p[0], p[4], iBuilder->simd_himask(8), 4, bit00004444[1], bit00004444[0]);
29    p2s_step(iBuilder, p[1], p[5], iBuilder->simd_himask(8), 4, bit11115555[1], bit11115555[0]);
30    p2s_step(iBuilder, p[2], p[6], iBuilder->simd_himask(8), 4, bit22226666[1], bit22226666[0]);
31    p2s_step(iBuilder, p[3], p[7], iBuilder->simd_himask(8), 4, bit33337777[1], bit33337777[0]);
32    Value * bit00224466[4];
33    Value * bit11335577[4];
34    for (unsigned j = 0; j<2; j++) {
35        p2s_step(iBuilder, bit00004444[j], bit22226666[j],iBuilder->simd_himask(4), 2, bit00224466[2*j+1], bit00224466[2*j]);
36        p2s_step(iBuilder, bit11115555[j], bit33337777[j],iBuilder->simd_himask(4), 2, bit11335577[2*j+1], bit11335577[2*j]);
37    }
38    for (unsigned j = 0; j<4; j++) {
39        p2s_step(iBuilder, bit00224466[j], bit11335577[j], iBuilder->simd_himask(2), 1, s[2*j+1], s[2*j]);
40    }
41}
42               
43void P2SKernel::generateDoBlockMethod() {
44    Value * p_bitblock[8];
45    for (unsigned i = 0; i < 8; i++) {
46        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(i));
47        p_bitblock[i] = iBuilder->CreateBlockAlignedLoad(ptr);
48    }
49    Value * s_bytepack[8];
50    p2s(iBuilder, p_bitblock, s_bytepack);
51    for (unsigned j = 0; j < 8; ++j) {
52        Value * ptr = getOutputStream("byteStream", iBuilder->getInt32(0), iBuilder->getInt32(j));
53        iBuilder->CreateBlockAlignedStore(s_bytepack[j], ptr);
54    }
55}
56
57P2SKernel::P2SKernel(IDISA::IDISA_Builder * iBuilder)
58: BlockOrientedKernel(iBuilder, "p2s",
59              {Binding{iBuilder->getStreamSetTy(8, 1), "basisBits"}},
60              {Binding{iBuilder->getStreamSetTy(1, 8), "byteStream"}},
61              {}, {}, {}) {
62
63}
64   
65
66void P2SKernelWithCompressedOutput::generateDoBlockMethod() {
67    PointerType * i8PtrTy = iBuilder->getInt8PtrTy();
68    IntegerType * i32 = iBuilder->getInt32Ty();
69    PointerType * bitBlockPtrTy = PointerType::get(iBuilder->getBitBlockType(), 0);
70
71    Value * basisBits[8];
72    for (unsigned i = 0; i < 8; i++) {
73        Value * basisBitsBlock_ptr = getInputStream("basisBits", iBuilder->getInt32(i));
74        basisBits[i] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr);
75    }
76    Value * bytePack[8];
77    p2s(iBuilder, basisBits, bytePack);
78
79    unsigned units_per_register = iBuilder->getBitBlockWidth()/8;
80    Value * delCountBlock_ptr = getInputStream("deletionCounts", iBuilder->getInt32(0));
81    Value * unit_counts = iBuilder->fwCast(units_per_register, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr));
82
83    Value * unitsGenerated = getProducedItemCount("byteStream"); // units generated to buffer
84    Value * output_ptr = getStreamView(i8PtrTy, "byteStream", getBlockNo(), iBuilder->getInt32(0));
85    Value * offset = iBuilder->getInt32(0);
86    for (unsigned j = 0; j < 8; ++j) {
87        iBuilder->CreateStore(bytePack[j], iBuilder->CreateBitCast(iBuilder->CreateGEP(output_ptr, offset), bitBlockPtrTy));
88        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(j)), i32);
89    }
90    unitsGenerated = iBuilder->CreateAdd(unitsGenerated, iBuilder->CreateZExt(offset, iBuilder->getSizeTy()));
91    setProducedItemCount("byteStream", unitsGenerated);
92}
93   
94P2SKernelWithCompressedOutput::P2SKernelWithCompressedOutput(IDISA::IDISA_Builder * iBuilder)
95: BlockOrientedKernel(iBuilder, "p2s_compress",
96              {Binding{iBuilder->getStreamSetTy(8, 1), "basisBits"}, Binding{iBuilder->getStreamSetTy(1, 1), "deletionCounts"}},
97              {Binding{iBuilder->getStreamSetTy(1, 8), "byteStream"}},
98              {}, {}, {}) {
99    setDoBlockUpdatesProducedItemCountsAttribute(true);
100}
101   
102   
103
104void P2S16Kernel::generateDoBlockMethod() {
105    Value * hi_input[8];
106    for (unsigned j = 0; j < 8; ++j) {
107        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(0), iBuilder->getInt32(j));
108        hi_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
109    }
110    Value * hi_bytes[8];
111    p2s(iBuilder, hi_input, hi_bytes);   
112    Value * lo_input[8];
113    for (unsigned j = 0; j < 8; ++j) {
114        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(0), iBuilder->getInt32(j + 8));
115        lo_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
116    }
117    Value * lo_bytes[8];
118    p2s(iBuilder, lo_input, lo_bytes);   
119    for (unsigned j = 0; j < 8; ++j) {
120        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
121        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
122        Value * ptr0 = getOutputStream("i16Stream", iBuilder->getInt32(2 * j));
123        iBuilder->CreateBlockAlignedStore(merge0, ptr0);
124        Value * ptr1 = getOutputStream("i16Stream", iBuilder->getInt32(2 * j + 1));
125        iBuilder->CreateBlockAlignedStore(merge1, ptr1);
126    }
127}
128   
129
130P2S16Kernel::P2S16Kernel(IDISA::IDISA_Builder * iBuilder)
131: BlockOrientedKernel(iBuilder, "p2s_16",
132              {Binding{iBuilder->getStreamSetTy(16, 1), "basisBits"}},
133              {Binding{iBuilder->getStreamSetTy(1, 16), "i16Stream"}},
134              {}, {}, {}) {
135
136}
137
138   
139void P2S16KernelWithCompressedOutput::generateDoBlockMethod() {
140    IntegerType * i32Ty = iBuilder->getInt32Ty();
141    PointerType * bitBlockPtrTy = iBuilder->getBitBlockType()->getPointerTo();
142
143    Value * hi_input[8];
144    for (unsigned j = 0; j < 8; ++j) {
145        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(j));
146        hi_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
147    }
148    Value * hi_bytes[8];
149    p2s(iBuilder, hi_input, hi_bytes);
150
151    Value * lo_input[8];
152    for (unsigned j = 0; j < 8; ++j) {
153        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(j + 8));
154        lo_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
155    }
156    Value * lo_bytes[8];
157    p2s(iBuilder, lo_input, lo_bytes);
158
159    Value * delCountBlock_ptr = getInputStream("deletionCounts", iBuilder->getInt32(0));
160    Value * unit_counts = iBuilder->fwCast(iBuilder->getBitBlockWidth() / 16, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr));
161    PointerType * int16PtrTy = PointerType::get(iBuilder->getInt16Ty(), 0);
162
163    ConstantInt * stride = iBuilder->getSize(iBuilder->getStride());
164    Value * i16UnitsGenerated = getProducedItemCount("i16Stream"); // units generated to buffer
165    Value * i16BlockNo = iBuilder->CreateUDiv(i16UnitsGenerated, stride);
166    Value * u16_output_ptr = getStreamView(int16PtrTy, "i16Stream", i16BlockNo, iBuilder->CreateURem(i16UnitsGenerated, stride));
167
168    Value * offset = ConstantInt::get(i32Ty, 0);
169
170    for (unsigned j = 0; j < 8; ++j) {
171        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
172        iBuilder->CreateAlignedStore(merge0, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
173        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2 * j)), i32Ty);
174
175        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
176        iBuilder->CreateAlignedStore(merge1, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
177        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2 * j + 1)), i32Ty);
178    }
179    Value * i16UnitsFinal = iBuilder->CreateAdd(i16UnitsGenerated, iBuilder->CreateZExt(offset, iBuilder->getSizeTy()));
180    setProducedItemCount("i16Stream", i16UnitsFinal);
181    auto const &= getStreamSetBuffer("i16Stream");
182
183    if (auto cb = dyn_cast<CircularCopybackBuffer>(b)) {
184        BasicBlock * copyBack = CreateBasicBlock("copyBack");
185        BasicBlock * p2sCompressDone = CreateBasicBlock("p2sCompressDone");
186       
187        // Check for overflow into the buffer overflow area and copy data back if so.
188        Value * accessible = cb->getLinearlyAccessibleItems(i16UnitsGenerated);
189        offset = iBuilder->CreateZExt(offset, iBuilder->getSizeTy());
190        Value * wraparound = iBuilder->CreateICmpULT(accessible, offset);
191        iBuilder->CreateCondBr(wraparound, copyBack, p2sCompressDone);
192       
193        iBuilder->SetInsertPoint(copyBack);
194        Value * copyItems = iBuilder->CreateSub(offset, accessible);
195        cb->createCopyBack(getStreamSetBufferPtr("i16Stream"), copyItems);
196        iBuilder->CreateBr(p2sCompressDone);
197        iBuilder->SetInsertPoint(p2sCompressDone);
198    }
199}
200   
201P2S16KernelWithCompressedOutput::P2S16KernelWithCompressedOutput(IDISA::IDISA_Builder * iBuilder)
202: BlockOrientedKernel(iBuilder, "p2s_16_compress",
203              {Binding{iBuilder->getStreamSetTy(16, 1), "basisBits"}, Binding{iBuilder->getStreamSetTy(1, 1), "deletionCounts"}},
204              {Binding{iBuilder->getStreamSetTy(1, 16), "i16Stream"}},
205              {},
206              {},
207              {Binding{iBuilder->getSizeTy(), "unitsGenerated"}, Binding{iBuilder->getSizeTy(), "unitsWritten"}}) {
208    setDoBlockUpdatesProducedItemCountsAttribute(true);
209}
210   
211   
212}
Note: See TracBrowser for help on using the repository browser.