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

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

Uniquify kernel names with buffer types/sizes; update u8u16 to use ParabixDriver?

File size: 8.3 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        p_bitblock[i] = loadInputStreamBlock("basisBits", iBuilder->getInt32(i));
47    }
48    Value * s_bytepack[8];
49    p2s(iBuilder, p_bitblock, s_bytepack);
50    for (unsigned j = 0; j < 8; ++j) {
51        storeOutputStreamPack("byteStream", iBuilder->getInt32(0), iBuilder->getInt32(j), s_bytepack[j]);
52    }
53}
54
55void P2SKernelWithCompressedOutput::generateDoBlockMethod() {
56    IntegerType * i32 = iBuilder->getInt32Ty();
57    PointerType * bitBlockPtrTy = PointerType::get(iBuilder->getBitBlockType(), 0);
58
59    Value * basisBits[8];
60    for (unsigned i = 0; i < 8; i++) {
61        basisBits[i] = loadInputStreamBlock("basisBits", iBuilder->getInt32(i));
62    }
63    Value * bytePack[8];
64    p2s(iBuilder, basisBits, bytePack);
65
66    unsigned units_per_register = iBuilder->getBitBlockWidth()/8;
67    Value * delCountBlock_ptr = getInputStreamBlockPtr("deletionCounts", iBuilder->getInt32(0));
68    Value * unit_counts = iBuilder->fwCast(units_per_register, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr));
69
70    Value * output_ptr = getOutputStreamBlockPtr("byteStream", iBuilder->getInt32(0));
71    output_ptr = iBuilder->CreatePointerCast(output_ptr, iBuilder->getInt8PtrTy());
72    Value * offset = iBuilder->getInt32(0);
73    for (unsigned j = 0; j < 8; ++j) {
74        iBuilder->CreateStore(bytePack[j], iBuilder->CreateBitCast(iBuilder->CreateGEP(output_ptr, offset), bitBlockPtrTy));
75        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(j)), i32);
76    }
77
78    Value * unitsGenerated = getProducedItemCount("byteStream"); // units generated to buffer
79    unitsGenerated = iBuilder->CreateAdd(unitsGenerated, iBuilder->CreateZExt(offset, iBuilder->getSizeTy()));
80    setProducedItemCount("byteStream", unitsGenerated);
81}
82
83void P2S16Kernel::generateDoBlockMethod() {
84    Value * hi_input[8];
85    for (unsigned j = 0; j < 8; ++j) {
86        hi_input[j] = loadInputStreamBlock("basisBits", iBuilder->getInt32(j));
87    }
88    Value * hi_bytes[8];
89    p2s(iBuilder, hi_input, hi_bytes);   
90    Value * lo_input[8];
91    for (unsigned j = 0; j < 8; ++j) {
92        lo_input[j] = loadInputStreamBlock("basisBits", iBuilder->getInt32(j + 8));
93    }
94    Value * lo_bytes[8];
95    p2s(iBuilder, lo_input, lo_bytes);   
96    for (unsigned j = 0; j < 8; ++j) {
97        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
98        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
99        storeOutputStreamPack("i16Stream", iBuilder->getInt32(0), iBuilder->getInt32(2 * j), merge0);
100        storeOutputStreamPack("i16Stream", iBuilder->getInt32(0), iBuilder->getInt32(2 * j + 1), merge1);
101    }
102}
103       
104void P2S16KernelWithCompressedOutput::generateDoBlockMethod() {
105    IntegerType * i32Ty = iBuilder->getInt32Ty();
106    PointerType * int16PtrTy = iBuilder->getInt16Ty()->getPointerTo();
107    PointerType * bitBlockPtrTy = iBuilder->getBitBlockType()->getPointerTo();
108    ConstantInt * stride = iBuilder->getSize(iBuilder->getStride());
109
110    Value * hi_input[8];
111    for (unsigned j = 0; j < 8; ++j) {
112        hi_input[j] = loadInputStreamBlock("basisBits", iBuilder->getInt32(j));
113    }
114    Value * hi_bytes[8];
115    p2s(iBuilder, hi_input, hi_bytes);
116
117    Value * lo_input[8];
118    for (unsigned j = 0; j < 8; ++j) {
119        lo_input[j] = loadInputStreamBlock("basisBits", iBuilder->getInt32(j + 8));
120    }
121    Value * lo_bytes[8];
122    p2s(iBuilder, lo_input, lo_bytes);
123
124    Value * delCountBlock_ptr = getInputStreamBlockPtr("deletionCounts", iBuilder->getInt32(0));
125    Value * unit_counts = iBuilder->fwCast(iBuilder->getBitBlockWidth() / 16, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr));
126
127
128    Value * u16_output_ptr = getOutputStreamBlockPtr("i16Stream", iBuilder->getInt32(0));
129    u16_output_ptr = iBuilder->CreatePointerCast(u16_output_ptr, int16PtrTy);
130    Value * i16UnitsGenerated = getProducedItemCount("i16Stream"); // units generated to buffer
131    u16_output_ptr = iBuilder->CreateGEP(u16_output_ptr, iBuilder->CreateURem(i16UnitsGenerated, stride));
132
133    Value * offset = ConstantInt::get(i32Ty, 0);
134
135    for (unsigned j = 0; j < 8; ++j) {
136        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
137        iBuilder->CreateAlignedStore(merge0, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
138        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2 * j)), i32Ty);
139
140        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
141        iBuilder->CreateAlignedStore(merge1, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
142        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2 * j + 1)), i32Ty);
143    }   
144    Value * i16UnitsFinal = iBuilder->CreateAdd(i16UnitsGenerated, iBuilder->CreateZExt(offset, iBuilder->getSizeTy()));
145    setProducedItemCount("i16Stream", i16UnitsFinal);
146}
147
148P2SKernel::P2SKernel(IDISA::IDISA_Builder * iBuilder)
149: BlockOrientedKernel(iBuilder, "Parabix:p2s",
150              {Binding{iBuilder->getStreamSetTy(8, 1), "basisBits"}},
151              {Binding{iBuilder->getStreamSetTy(1, 8), "byteStream"}},
152              {}, {}, {}) {
153}
154
155P2SKernelWithCompressedOutput::P2SKernelWithCompressedOutput(IDISA::IDISA_Builder * iBuilder)
156: BlockOrientedKernel(iBuilder, "Parabix:p2s_compress",
157              {Binding{iBuilder->getStreamSetTy(8, 1), "basisBits"}, Binding{iBuilder->getStreamSetTy(1, 1), "deletionCounts"}},
158                      {Binding{iBuilder->getStreamSetTy(1, 8), "byteStream", MaxRatio(1)}},
159              {}, {}, {}) {
160}
161
162P2S16Kernel::P2S16Kernel(IDISA::IDISA_Builder * iBuilder)
163: BlockOrientedKernel(iBuilder, "Parabix:p2s_16",
164              {Binding{iBuilder->getStreamSetTy(16, 1), "basisBits"}},
165              {Binding{iBuilder->getStreamSetTy(1, 16), "i16Stream"}},
166              {}, {}, {}) {
167}
168
169
170P2S16KernelWithCompressedOutput::P2S16KernelWithCompressedOutput(IDISA::IDISA_Builder * b)
171: BlockOrientedKernel(b, "Parabix:p2s_16_compress",
172              {Binding{b->getStreamSetTy(16, 1), "basisBits"}, Binding{b->getStreamSetTy(1, 1), "deletionCounts"}},
173              {Binding{b->getStreamSetTy(1, 16), "i16Stream", MaxRatio(1)}},
174              {},
175              {},
176              {}) {
177
178}
179   
180   
181}
Note: See TracBrowser for help on using the repository browser.