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

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

getStreamSetBlockPtr

File size: 9.6 KB
Line 
1#include "p2s_kernel.h"
2#include "kernels/kernel.h"
3#include "IDISA/idisa_builder.h"
4#include <llvm/IR/TypeBuilder.h>
5#include <llvm/IR/Type.h>
6#include <iostream>
7#include <stdint.h>
8#include <llvm/Support/FileSystem.h>
9#include <llvm/Support/raw_ostream.h>
10
11
12
13namespace kernel{
14       
15void p2s_step(IDISA::IDISA_Builder * iBuilder, Value * p0, Value * p1, Value * hi_mask, unsigned shift, Value * &s1, Value * &s0) {
16    Value * t0 = iBuilder->simd_if(1, hi_mask, p0, iBuilder->simd_srli(16, p1, shift));
17    Value * t1 = iBuilder->simd_if(1, hi_mask, iBuilder->simd_slli(16, p0, shift), p1);
18    s1 = iBuilder->esimd_mergeh(8, t1, t0);
19    s0 = iBuilder->esimd_mergel(8, t1, t0);
20}
21
22inline void p2s(IDISA::IDISA_Builder * iBuilder, Value * p[], Value * s[]) {
23    Value * bit00004444[2];
24    Value * bit22226666[2];
25    Value * bit11115555[2];
26    Value * bit33337777[2];
27    p2s_step(iBuilder, p[0], p[4], iBuilder->simd_himask(8), 4, bit00004444[1], bit00004444[0]);
28    p2s_step(iBuilder, p[1], p[5], iBuilder->simd_himask(8), 4, bit11115555[1], bit11115555[0]);
29    p2s_step(iBuilder, p[2], p[6], iBuilder->simd_himask(8), 4, bit22226666[1], bit22226666[0]);
30    p2s_step(iBuilder, p[3], p[7], iBuilder->simd_himask(8), 4, bit33337777[1], bit33337777[0]);
31
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    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
45    Module * m = iBuilder->getModule();
46   
47    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
48   
49    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
50   
51    Value * self = getParameter(doBlockFunction, "self");
52    Value * blockNo = getScalarField(self, blockNoScalar);
53    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
54    Value * byteStreamBlock_ptr = getStreamSetBlockPtr(self, "byteStream", blockNo);
55
56    Value * p_bitblock[8];
57    for (unsigned i = 0; i < 8; i++) {
58        p_bitblock[i] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(i)});
59    }
60    Value * s_bytepack[8];
61    p2s(iBuilder, p_bitblock, s_bytepack);
62    for (unsigned j = 0; j < 8; ++j) {
63        iBuilder->CreateBlockAlignedStore(s_bytepack[j], byteStreamBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(j)});
64    }
65    iBuilder->CreateRetVoid();
66    iBuilder->restoreIP(savePoint);
67}
68       
69   
70void p2sKernel_withCompressedOutput::prepareKernel() {
71    setDoBlockReturnType(iBuilder->getInt32Ty());
72    KernelBuilder::prepareKernel();
73}
74
75void p2sKernel_withCompressedOutput::generateDoBlockMethod() {
76    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
77    Module * m = iBuilder->getModule();
78    Type * i8PtrTy = iBuilder->getInt8PtrTy(); 
79    Type * i32 = iBuilder->getIntNTy(32); 
80    Type * bitBlockPtrTy = llvm::PointerType::get(iBuilder->getBitBlockType(), 0); 
81   
82    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
83   
84    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
85    Value * self = getParameter(doBlockFunction, "self");
86    Value * blockNo = getScalarField(self, blockNoScalar);
87    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
88    Value * delCountBlock_ptr = getStreamSetBlockPtr(self, "deletionCounts", blockNo);
89    Value * byteStreamBlock_ptr = getStreamSetBlockPtr(self, "byteStream", blockNo);
90   
91    Value * p_bitblock[8];
92    for (unsigned i = 0; i < 8; i++) {
93        p_bitblock[i] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(i)});
94    }
95    Value * s_bytepack[8];
96    p2s(iBuilder, p_bitblock, s_bytepack);
97   
98    unsigned units_per_register = iBuilder->getBitBlockWidth()/8;
99   
100    Value * unit_counts = iBuilder->fwCast(units_per_register, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0)}));
101   
102    Value * output_ptr = iBuilder->CreateBitCast(byteStreamBlock_ptr, i8PtrTy);
103    Value * offset = ConstantInt::get(i32, 0);
104   
105    for (unsigned j = 0; j < 8; ++j) {
106        iBuilder->CreateAlignedStore(s_bytepack[j], iBuilder->CreateBitCast(iBuilder->CreateGEP(output_ptr, offset), bitBlockPtrTy), 1);
107        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(j)), i32);
108    }
109    iBuilder->CreateRet(offset);
110    iBuilder->restoreIP(savePoint);
111}
112   
113   
114void p2s_16Kernel::generateDoBlockMethod() {
115    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
116    Module * m = iBuilder->getModule();
117   
118    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
119   
120    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
121    Value * self = getParameter(doBlockFunction, "self");
122    Value * blockNo = getScalarField(self, blockNoScalar);
123    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
124    Value * i16StreamBlock_ptr = getStreamSetBlockPtr(self, "i16Stream", blockNo);
125   
126    Value * hi_input[8];
127    for (unsigned j = 0; j < 8; ++j) {
128        hi_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
129    }
130    Value * hi_bytes[8];
131    p2s(iBuilder, hi_input, hi_bytes);
132   
133    Value * lo_input[8];
134    for (unsigned j = 0; j < 8; ++j) {
135        lo_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j+8)});
136    }
137    Value * lo_bytes[8];
138    p2s(iBuilder, lo_input, lo_bytes);
139   
140    for (unsigned j = 0; j < 8; ++j) {
141        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
142        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
143        iBuilder->CreateBlockAlignedStore(merge0, i16StreamBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(2*j)});
144        iBuilder->CreateBlockAlignedStore(merge1, i16StreamBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0), iBuilder->getInt32(2*j+1)});
145    }
146    iBuilder->CreateRetVoid();
147    iBuilder->restoreIP(savePoint);
148}
149       
150
151void p2s_16Kernel_withCompressedOutput::prepareKernel() {
152    setDoBlockReturnType(iBuilder->getInt32Ty());
153    KernelBuilder::prepareKernel();
154}
155   
156void p2s_16Kernel_withCompressedOutput::generateDoBlockMethod() {
157    IDISA::IDISA_Builder::InsertPoint savePoint = iBuilder->saveIP();
158    Module * m = iBuilder->getModule();
159    Type * i32 = iBuilder->getIntNTy(32); 
160    Type * bitBlockPtrTy = llvm::PointerType::get(iBuilder->getBitBlockType(), 0); 
161   
162    Function * doBlockFunction = m->getFunction(mKernelName + doBlock_suffix);
163   
164    iBuilder->SetInsertPoint(BasicBlock::Create(iBuilder->getContext(), "entry", doBlockFunction, 0));
165    Value * self = getParameter(doBlockFunction, "self");
166    Value * blockNo = getScalarField(self, blockNoScalar);
167    Value * basisBitsBlock_ptr = getStreamSetBlockPtr(self, "basisBits", blockNo);
168    Value * delCountBlock_ptr = getStreamSetBlockPtr(self, "deletionCounts", blockNo);
169    Value * i16StreamBlock_ptr = getStreamSetBlockPtr(self, "i16Stream", blockNo);
170   
171    Value * hi_input[8];
172    for (unsigned j = 0; j < 8; ++j) {
173        hi_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j)});
174    }
175    Value * hi_bytes[8];
176    p2s(iBuilder, hi_input, hi_bytes);
177   
178    Value * lo_input[8];
179    for (unsigned j = 0; j < 8; ++j) {
180        lo_input[j] = iBuilder->CreateBlockAlignedLoad(basisBitsBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(j+8)});
181    }
182    Value * lo_bytes[8];
183    p2s(iBuilder, lo_input, lo_bytes);
184   
185    unsigned UTF_16_units_per_register = iBuilder->getBitBlockWidth()/16;
186   
187    Value * unit_counts = iBuilder->fwCast(UTF_16_units_per_register, iBuilder->CreateBlockAlignedLoad(delCountBlock_ptr, {iBuilder->getInt32(0), iBuilder->getInt32(0)}));
188   
189    Value * u16_output_ptr = iBuilder->CreateBitCast(i16StreamBlock_ptr, PointerType::get(iBuilder->getInt16Ty(), 0));
190    Value * offset = ConstantInt::get(i32, 0);
191   
192    for (unsigned j = 0; j < 8; ++j) {
193        Value * merge0 = iBuilder->bitCast(iBuilder->esimd_mergel(8, hi_bytes[j], lo_bytes[j]));
194        Value * merge1 = iBuilder->bitCast(iBuilder->esimd_mergeh(8, hi_bytes[j], lo_bytes[j]));
195        //iBuilder->CallPrintRegister("merge0", merge0);
196        iBuilder->CreateAlignedStore(merge0, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
197        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2*j)), i32);
198        //iBuilder->CallPrintInt("offset", offset);
199        iBuilder->CreateAlignedStore(merge1, iBuilder->CreateBitCast(iBuilder->CreateGEP(u16_output_ptr, offset), bitBlockPtrTy), 1);
200        //iBuilder->CallPrintRegister("merge1", merge1);
201        offset = iBuilder->CreateZExt(iBuilder->CreateExtractElement(unit_counts, iBuilder->getInt32(2*j+1)), i32);
202        //iBuilder->CallPrintInt("offset", offset);
203    }
204    iBuilder->CreateRet(offset);
205    iBuilder->restoreIP(savePoint);
206}
207       
208}
Note: See TracBrowser for help on using the repository browser.