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

Last change on this file since 5310 was 5310, checked in by nmedfort, 2 years ago

Adjusted pablo compiler to use getInputStream and getOutputStream when accessing packed stream fields.

File size: 9.9 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    IntegerType * i32 = iBuilder->getInt32Ty();
68    PointerType * bitBlockPtrTy = PointerType::get(iBuilder->getBitBlockType(), 0);
69
70    Value * basisBits[8];
71    for (unsigned i = 0; i < 8; i++) {
72        Value * basisBitsBlock_ptr = getInputStream("basisBits", iBuilder->getInt32(i));
73        basisBits[i] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr);
74    }
75    Value * bytePack[8];
76    p2s(iBuilder, basisBits, bytePack);
77
78    unsigned units_per_register = iBuilder->getBitBlockWidth()/8;
79    Value * delCountBlock_ptr = getInputStream("deletionCounts", iBuilder->getInt32(0));
80    Value * unit_counts = iBuilder->fwCast(units_per_register, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr));
81
82    Value * output_ptr = getOutputStream("byteStream", iBuilder->getInt32(0));
83    output_ptr = iBuilder->CreatePointerCast(output_ptr, iBuilder->getInt8PtrTy());
84    Value * offset = iBuilder->getInt32(0);
85    for (unsigned j = 0; j < 8; ++j) {
86        iBuilder->CreateStore(bytePack[j], iBuilder->CreateBitCast(iBuilder->CreateGEP(output_ptr, offset), bitBlockPtrTy));
87        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(j)), i32);
88    }
89
90    Value * unitsGenerated = getProducedItemCount("byteStream"); // units generated to buffer
91    unitsGenerated = iBuilder->CreateAdd(unitsGenerated, iBuilder->CreateZExt(offset, iBuilder->getSizeTy()));
92    setProducedItemCount("byteStream", unitsGenerated);
93}
94   
95P2SKernelWithCompressedOutput::P2SKernelWithCompressedOutput(IDISA::IDISA_Builder * iBuilder)
96: BlockOrientedKernel(iBuilder, "p2s_compress",
97              {Binding{iBuilder->getStreamSetTy(8, 1), "basisBits"}, Binding{iBuilder->getStreamSetTy(1, 1), "deletionCounts"}},
98              {Binding{iBuilder->getStreamSetTy(1, 8), "byteStream"}},
99              {}, {}, {}) {
100    setDoBlockUpdatesProducedItemCountsAttribute(true);
101}
102   
103   
104
105void P2S16Kernel::generateDoBlockMethod() {
106    Value * hi_input[8];
107    for (unsigned j = 0; j < 8; ++j) {
108        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(0), iBuilder->getInt32(j));
109        hi_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
110    }
111    Value * hi_bytes[8];
112    p2s(iBuilder, hi_input, hi_bytes);   
113    Value * lo_input[8];
114    for (unsigned j = 0; j < 8; ++j) {
115        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(0), iBuilder->getInt32(j + 8));
116        lo_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
117    }
118    Value * lo_bytes[8];
119    p2s(iBuilder, lo_input, lo_bytes);   
120    for (unsigned j = 0; j < 8; ++j) {
121        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
122        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
123        Value * ptr0 = getOutputStream("i16Stream", iBuilder->getInt32(2 * j));
124        iBuilder->CreateBlockAlignedStore(merge0, ptr0);
125        Value * ptr1 = getOutputStream("i16Stream", iBuilder->getInt32(2 * j + 1));
126        iBuilder->CreateBlockAlignedStore(merge1, ptr1);
127    }
128}
129   
130
131P2S16Kernel::P2S16Kernel(IDISA::IDISA_Builder * iBuilder)
132: BlockOrientedKernel(iBuilder, "p2s_16",
133              {Binding{iBuilder->getStreamSetTy(16, 1), "basisBits"}},
134              {Binding{iBuilder->getStreamSetTy(1, 16), "i16Stream"}},
135              {}, {}, {}) {
136
137}
138
139   
140void P2S16KernelWithCompressedOutput::generateDoBlockMethod() {
141    IntegerType * i32Ty = iBuilder->getInt32Ty();
142    PointerType * int16PtrTy = iBuilder->getInt16Ty()->getPointerTo();
143    PointerType * bitBlockPtrTy = iBuilder->getBitBlockType()->getPointerTo();
144    ConstantInt * stride = iBuilder->getSize(iBuilder->getStride());
145
146    Value * hi_input[8];
147    for (unsigned j = 0; j < 8; ++j) {
148        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(j));
149        hi_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
150    }
151    Value * hi_bytes[8];
152    p2s(iBuilder, hi_input, hi_bytes);
153
154    Value * lo_input[8];
155    for (unsigned j = 0; j < 8; ++j) {
156        Value * ptr = getInputStream("basisBits", iBuilder->getInt32(j + 8));
157        lo_input[j] = iBuilder->CreateBlockAlignedLoad(ptr);
158    }
159    Value * lo_bytes[8];
160    p2s(iBuilder, lo_input, lo_bytes);
161
162    Value * delCountBlock_ptr = getInputStream("deletionCounts", iBuilder->getInt32(0));
163    Value * unit_counts = iBuilder->fwCast(iBuilder->getBitBlockWidth() / 16, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr));
164
165
166    Value * u16_output_ptr = getOutputStream("i16Stream", iBuilder->getInt32(0));
167    u16_output_ptr = iBuilder->CreatePointerCast(u16_output_ptr, int16PtrTy);
168    Value * i16UnitsGenerated = getProducedItemCount("i16Stream"); // units generated to buffer
169    u16_output_ptr = iBuilder->CreateGEP(u16_output_ptr, iBuilder->CreateURem(i16UnitsGenerated, stride));
170
171    Value * offset = ConstantInt::get(i32Ty, 0);
172
173    for (unsigned j = 0; j < 8; ++j) {
174        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
175        iBuilder->CreateAlignedStore(merge0, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
176        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2 * j)), i32Ty);
177
178        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
179        iBuilder->CreateAlignedStore(merge1, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
180        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2 * j + 1)), i32Ty);
181    }   
182    Value * i16UnitsFinal = iBuilder->CreateAdd(i16UnitsGenerated, iBuilder->CreateZExt(offset, iBuilder->getSizeTy()));
183    setProducedItemCount("i16Stream", i16UnitsFinal);
184    const auto b  = getOutputStreamSetBuffer("i16Stream");
185
186    if (auto cb = dyn_cast<CircularCopybackBuffer>(b)) {
187        BasicBlock * copyBack = CreateBasicBlock("copyBack");
188        BasicBlock * p2sCompressDone = CreateBasicBlock("p2sCompressDone");
189       
190        // Check for overflow into the buffer overflow area and copy data back if so.
191        Value * accessible = cb->getLinearlyAccessibleItems(i16UnitsGenerated);
192        offset = iBuilder->CreateZExt(offset, iBuilder->getSizeTy());
193        Value * wraparound = iBuilder->CreateICmpULT(accessible, offset);
194        iBuilder->CreateCondBr(wraparound, copyBack, p2sCompressDone);
195       
196        iBuilder->SetInsertPoint(copyBack);
197        Value * copyItems = iBuilder->CreateSub(offset, accessible);
198        cb->createCopyBack(getStreamSetBufferPtr("i16Stream"), copyItems);
199        iBuilder->CreateBr(p2sCompressDone);
200        iBuilder->SetInsertPoint(p2sCompressDone);
201    }
202}
203   
204P2S16KernelWithCompressedOutput::P2S16KernelWithCompressedOutput(IDISA::IDISA_Builder * b)
205: BlockOrientedKernel(b, "p2s_16_compress",
206              {Binding{b->getStreamSetTy(16, 1), "basisBits"}, Binding{b->getStreamSetTy(1, 1), "deletionCounts"}},
207              {Binding{b->getStreamSetTy(1, 16), "i16Stream", b->getStride()}},
208              {},
209              {},
210              {Binding{b->getSizeTy(), "unitsGenerated"}, Binding{b->getSizeTy(), "unitsWritten"}}) {
211    setDoBlockUpdatesProducedItemCountsAttribute(true);
212}
213   
214   
215}
Note: See TracBrowser for help on using the repository browser.