source: icGREP/icgrep-devel/icgrep/kernels/streamset.cpp @ 5188

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

Do segment method now handles partial/final segment

File size: 11.5 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6   
7#include <kernels/streamset.h>
8#include <vector>
9#include <IDISA/idisa_builder.h>
10#include <llvm/IR/Type.h>
11#include <iostream>
12   
13using namespace parabix;
14
15enum SS_struct_index {iProducer_pos = 0, iConsumer_pos = 1, iEnd_of_input = 2, iBuffer_ptr = 3};
16
17llvm::Value * parabix::getProducerPosPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr) {
18    return b->CreateGEP(bufferStructPtr, {b->getInt32(0), b->getInt32(iProducer_pos)});
19}
20
21llvm::Value * parabix::getConsumerPosPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr) {
22    return b->CreateGEP(bufferStructPtr, {b->getInt32(0), b->getInt32(iConsumer_pos)});
23}
24
25llvm::Value * parabix::hasEndOfInputPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr) {
26    return b->CreateGEP(bufferStructPtr, {b->getInt32(0), b->getInt32(iEnd_of_input)});
27}
28
29llvm::Value * parabix::getStreamSetBufferPtr(IDISA::IDISA_Builder * b, Value * bufferStructPtr) {
30    return b->CreateLoad(b->CreateGEP(bufferStructPtr, {b->getInt32(0), b->getInt32(iBuffer_ptr)}));
31}
32
33llvm::Type * StreamSetType::getStreamSetBlockType(IDISA::IDISA_Builder * iBuilder) {
34    llvm::Type * streamType = mFieldWidth == 1 ? iBuilder->getBitBlockType() : ArrayType::get(iBuilder->getBitBlockType(), mFieldWidth);
35    return ArrayType::get(streamType, mStreamCount);
36}
37
38llvm::PointerType * StreamSetBuffer::getStreamBufferPointerType() {
39    return PointerType::get(mStreamSetType.getStreamSetBlockType(iBuilder), mAddrSpace);
40}
41
42llvm::PointerType * StreamSetBuffer::getStreamSetStructPointerType() {
43    return PointerType::get(mStreamSetStructType, 0);
44}
45
46llvm::Value * StreamSetBuffer::getProducerPosPtr(Value * bufferStructPtr) {
47    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)});
48}
49
50void StreamSetBuffer::setProducerPos(Value * bufferStructPtr, llvm::Value * pos){
51    iBuilder->CreateStore(pos, iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
52}
53
54llvm::Value * StreamSetBuffer::getConsumerPosPtr(Value * bufferStructPtr) {
55    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)});
56}
57
58void StreamSetBuffer::setConsumerPos(Value * bufferStructPtr, Value * pos){
59    iBuilder->CreateStore(pos, iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
60}
61
62llvm::Value * StreamSetBuffer::hasEndOfInputPtr(Value * bufferStructPtr) {
63    return iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)});
64}
65
66void StreamSetBuffer::setEndOfInput(Value * bufferStructPtr){
67    iBuilder->CreateStore(ConstantInt::get(iBuilder->getInt1Ty(), 1), iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
68}
69
70
71llvm::Value * StreamSetBuffer::getStreamSetStructPtr(){
72    return mStreamSetStructPtr;
73}
74
75llvm::Value * StreamSetBuffer::allocateBuffer() {
76    Type * const size_ty = iBuilder->getSizeTy();
77    Type * const int1ty = iBuilder->getInt1Ty();
78    mStreamSetBufferPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetType.getStreamSetBlockType(iBuilder), ConstantInt::get(iBuilder->getSizeTy(), mBufferBlocks));
79    mStreamSetStructPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetStructType);
80    //iBuilder->CallPrintInt("mStreamSetBufferPtr", iBuilder->CreatePtrToInt(mStreamSetBufferPtr, iBuilder->getInt64Ty()));
81    //iBuilder->CallPrintInt("mStreamSetStructPtr", iBuilder->CreatePtrToInt(mStreamSetStructPtr, iBuilder->getInt64Ty()));
82    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
83    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
84    iBuilder->CreateStore(ConstantInt::get(int1ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
85    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
86   
87    return mStreamSetBufferPtr;
88}
89
90// Single Block Buffer
91// For a single block buffer, the block pointer is always the buffer base pointer.
92llvm::Value * SingleBlockBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
93    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
94    return iBuilder->CreateLoad(handle);
95}
96
97
98// External Unbounded Buffer
99
100void ExternalFileBuffer::setStreamSetBuffer(llvm::Value * ptr, Value * fileSize) {
101
102    Type * const size_ty = iBuilder->getSizeTy();
103    Type * const int1ty = iBuilder->getInt1Ty();
104
105    PointerType * t = getStreamBufferPointerType();   
106    mStreamSetBufferPtr = iBuilder->CreatePointerBitCastOrAddrSpaceCast(ptr, t);
107
108    mStreamSetStructPtr = iBuilder->CreateCacheAlignedAlloca(mStreamSetStructType);
109    iBuilder->CreateStore(fileSize, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
110    iBuilder->CreateStore(ConstantInt::get(size_ty, 0), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)}));
111    iBuilder->CreateStore(ConstantInt::get(int1ty, 1), iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iEnd_of_input)}));
112    iBuilder->CreateStore(mStreamSetBufferPtr, iBuilder->CreateGEP(mStreamSetStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)}));
113}
114
115llvm::Value * ExternalFileBuffer::allocateBuffer() {
116    throw std::runtime_error("External buffers cannot be allocated.");
117}
118
119llvm::Value * ExternalFileBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
120    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
121    return iBuilder->CreateGEP(iBuilder->CreateLoad(handle), {blockNo});
122}
123
124
125// Circular Stack Allocated Buffer
126
127llvm::Value * CircularBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
128    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
129    // Circular access is based on blockNo mod mBufferBlocks.  For power of 2 buffer sizes (required), we
130    // use bitwise masking to efficiently compute the mod function  (blockNo & (mBufferBlocks - 1)
131    Value * bufPtr = iBuilder->CreateLoad(handle);
132    //iBuilder->CallPrintInt("CircularBuffer bufPtr", iBuilder->CreatePtrToInt(bufPtr, iBuilder->getSizeTy()));
133    return iBuilder->CreateGEP(bufPtr, {iBuilder->CreateAnd(blockNo, ConstantInt::get(iBuilder->getSizeTy(), mBufferBlocks-1))});
134}
135
136llvm::Value * LinearBuffer::getStreamSetBlockPointer(llvm::Value * bufferStructPtr, llvm::Value * blockNo) {
137    Constant * blockWidth = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride());
138    Value * consumerPos_ptr = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)});
139    Value * consumerPos = iBuilder->CreateLoad(consumerPos_ptr);
140    Value * consumerBlock = iBuilder->CreateUDiv(consumerPos, blockWidth);
141    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
142    Value * bufPtr = iBuilder->CreateLoad(handle);
143    //iBuilder->CallPrintInt("LinearBuffer bufPtr", iBuilder->CreatePtrToInt(bufPtr, iBuilder->getSizeTy()));
144    return iBuilder->CreateGEP(bufPtr, {iBuilder->CreateSub(blockNo, consumerBlock)});
145}
146
147void LinearBuffer::setConsumerPos(Value * bufferStructPtr, Value * new_consumer_pos) {
148    Type * const i1 = iBuilder->getInt1Ty();
149    Type * const i8 = iBuilder->getInt8Ty();
150    Type * const i32 = iBuilder->getInt32Ty();
151    Type * const i8_ptr = PointerType::get(i8, mAddrSpace);
152    Module * M = iBuilder->getModule();
153    Function * memcpyFunc = cast<Function>(M->getOrInsertFunction("llvm.memcpy.p0i8.p0i8.i" + std::to_string(sizeof(size_t) * 8), 
154                                                                  iBuilder->getVoidTy(), i8_ptr, i8_ptr, iBuilder->getSizeTy(), i32, i1, nullptr));
155    Function * current = iBuilder->GetInsertBlock()->getParent();
156    BasicBlock * copyBackBody = BasicBlock::Create(M->getContext(), "copy_back", current, 0);
157    BasicBlock * setConsumerPosExit = BasicBlock::Create(M->getContext(), "setConsumerPos_done", current, 0);
158    Constant * blockWidth = ConstantInt::get(iBuilder->getSizeTy(), iBuilder->getStride());
159    Constant * one = ConstantInt::get(iBuilder->getSizeTy(), 1);
160    Value * consumerPos_ptr = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iConsumer_pos)});
161    Value * consumerPos = iBuilder->CreateLoad(consumerPos_ptr);
162    Value * consumerBlock = iBuilder->CreateUDiv(consumerPos, blockWidth);
163    // Ensure that the new consumer position is no less than the current position.
164    new_consumer_pos = iBuilder->CreateSelect(iBuilder->CreateICmpULT(new_consumer_pos, consumerPos), consumerPos, new_consumer_pos);
165    Value * producerPos = iBuilder->CreateLoad(iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iProducer_pos)}));
166    // Ensure that the new consumer position is no greater than the current producer position.
167    Value * new_pos_lt_producer_pos = iBuilder->CreateICmpULT(new_consumer_pos, producerPos);
168    new_consumer_pos = iBuilder->CreateSelect(new_pos_lt_producer_pos, new_consumer_pos, producerPos);
169    // Now, the new_consumer_pos is at most = to the producer_pos; if =, we're done.
170    iBuilder->CreateCondBr(new_pos_lt_producer_pos, copyBackBody, setConsumerPosExit);
171    iBuilder->SetInsertPoint(copyBackBody);
172   
173    Value * new_consumer_block = iBuilder->CreateUDiv(new_consumer_pos, blockWidth);
174   
175    Value * lastProducerBlock = iBuilder->CreateUDiv(iBuilder->CreateSub(producerPos, one), blockWidth);
176    //iBuilder->CallPrintInt("new_consumer_block", new_consumer_block);
177    //iBuilder->CallPrintInt("lastProducerBlock", lastProducerBlock);
178
179    Value * copyBlocks = iBuilder->CreateAdd(iBuilder->CreateSub(lastProducerBlock, new_consumer_block), one);
180    Constant * blockBytes = ConstantInt::get(iBuilder->getSizeTy(), mStreamSetType.StreamCount() * mStreamSetType.StreamFieldWidth() * iBuilder->getStride()/8);
181    Value * copyLength = iBuilder->CreateMul(copyBlocks, blockBytes);
182    //iBuilder->CallPrintInt("memcpy copyLength", copyLength);
183    // Must copy back one full block for each of the streams in the stream set.
184    Value * handle = iBuilder->CreateGEP(bufferStructPtr, {iBuilder->getInt32(0), iBuilder->getInt32(iBuffer_ptr)});
185    Value * bufferPtr = iBuilder->CreateLoad(handle);
186    //iBuilder->CallPrintInt("memcpy bufferPtr", iBuilder->CreatePtrToInt(bufferPtr, iBuilder->getSizeTy()));
187
188    Value * copyFrom = iBuilder->CreateGEP(bufferPtr, {iBuilder->CreateSub(new_consumer_block, consumerBlock)});
189    //iBuilder->CallPrintInt("memcpy copyFrom", iBuilder->CreatePtrToInt(copyFrom, iBuilder->getSizeTy()));
190    Value * alignment = ConstantInt::get(iBuilder->getInt32Ty(), iBuilder->getBitBlockWidth()/8);
191   
192    iBuilder->CreateCall(memcpyFunc, {iBuilder->CreateBitCast(bufferPtr, i8_ptr), iBuilder->CreateBitCast(copyFrom, i8_ptr), copyLength, alignment, ConstantInt::getNullValue(i1)});
193    iBuilder->CreateBr(setConsumerPosExit);
194    // Copy back done, store the new consumer position.
195    iBuilder->SetInsertPoint(setConsumerPosExit);
196    iBuilder->CreateStore(new_consumer_pos, consumerPos_ptr);
197}   
Note: See TracBrowser for help on using the repository browser.